0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 ** This file is
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 an amalgamation
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65 of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 C source files
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76 from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 31 2e 20 20 ersion 3.6.21.
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 on.** unit. Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 le SQLite. To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 er.** programs,
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ng interface to
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 ry. (If you do
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 , you will find
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20 a copy embedded
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65 within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65 Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20 .h" to find the
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 start.** of the
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33 embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29 .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65 files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77 eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54 g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 he code for the
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20 d-line shell is
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70 also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73 arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62 core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rary..*/.#define
04e0: 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23 SQLITE_CORE 1.#
04f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4d define SQLITE_AM
0500: 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 69 66 ALGAMATION 1.#if
0510: 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 56 ndef SQLITE_PRIV
0520: 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ATE.# define SQL
0530: 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 61 74 ITE_PRIVATE stat
0540: 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 ic.#endif.#ifnde
0550: 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 20 64 f SQLITE_API.# d
0560: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 50 49 efine SQLITE_API
0570: 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a .#endif./*******
0580: 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
0590: 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a le sqliteInt.h *
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05c0: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
05d0: 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
05e0: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
05f0: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
0600: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
0610: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
0620: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
0630: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
0640: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
0650: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
0660: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
0670: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
0680: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
0690: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
06a0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
06b0: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
06c0: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
06d0: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
06e0: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
06f0: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
0740: 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 65 72 * Internal inter
0750: 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 face definitions
0760: 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a for SQLite..**.
0770: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
0780: 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 TEINT_H_.#define
0790: 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a _SQLITEINT_H_..
07a0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 /*.** These #def
07b0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 ines should enab
07c0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 le >2GB file sup
07d0: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66 port on POSIX if
07e0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
07f0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ng operating sys
0800: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e tem supports it.
0810: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b If the OS lack
0820: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 s.** large file
0830: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74 support, or if t
0840: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73 he OS is windows
0850: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 , these should b
0860: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
0870: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54 Ticket #2739: T
0880: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f he _LARGEFILE_SO
0890: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20 URCE macro must
08a0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e appear before an
08b0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63 y.** system #inc
08c0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 ludes. Hence, t
08d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 his block of cod
08e0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65 e must be the ve
08f0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65 ry first.** code
0900: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 in all source f
0910: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 iles..**.** Larg
0920: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 e file support c
0930: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 an be disabled u
0940: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 sing the -DSQLIT
0950: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 E_DISABLE_LFS sw
0960: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 itch.** on the c
0970: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 ompiler command
0980: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e line. This is n
0990: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 ecessary if you
09a0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a are compiling.**
09b0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 on a recent mac
09c0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61 hine (ex: Red Ha
09d0: 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 t 7.2) but you w
09e0: 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f ant your code to
09f0: 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f work.** on an o
0a00: 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 lder machine (ex
0a10: 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 2e 20 : Red Hat 6.0).
0a20: 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 If you compile
0a30: 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 0a 2a on Red Hat 7.2.*
0a40: 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f * without this o
0a50: 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e ption, LFS is en
0a60: 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 able. But LFS d
0a70: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e oes not exist in
0a80: 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 the kernel.** i
0a90: 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c 20 73 n Red Hat 6.0, s
0aa0: 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 o the code won't
0ab0: 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 work. Hence, f
0ac0: 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 or maximum binar
0ad0: 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 y.** portability
0ae0: 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 you should omit
0af0: 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69 LFS..**.** Simi
0b00: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20 lar is true for
0b10: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69 Mac OS X. LFS i
0b20: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 s only supported
0b30: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61 on Mac OS X 9 a
0b40: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66 nd later..*/.#if
0b50: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 ndef SQLITE_DISA
0b60: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 BLE_LFS.# define
0b70: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 _LARGE_FILE
0b80: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 1.# ifndef _F
0b90: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a ILE_OFFSET_BITS.
0ba0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 # define _FILE
0bb0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a _OFFSET_BITS 64.
0bc0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
0bd0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 _LARGEFILE_SOUR
0be0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a CE 1.#endif../*.
0bf0: 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 ** Include the c
0c00: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 onfiguration hea
0c10: 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 der output by 'c
0c20: 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27 onfigure' if we'
0c30: 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 re using the.**
0c40: 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 autoconf-based b
0c50: 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f uild.*/.#ifdef _
0c60: 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 HAVE_SQLITE_CONF
0c70: 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 IG_H.#include "c
0c80: 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a onfig.h".#endif.
0c90: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
0ca0: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c Include sqliteL
0cb0: 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 imit.h in the mi
0cc0: 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
0cd0: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f t.h ***********/
0ce0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
0cf0: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 Begin file sqli
0d00: 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a teLimit.h ******
0d10: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
0d30: 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 ./*.** 2007 May
0d40: 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
0d50: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0d60: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0d70: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0d80: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0d90: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0da0: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0db0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
0dc0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
0dd0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
0de0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
0df0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
0e00: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
0e10: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
0e20: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0e30: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0e40: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0e50: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ea0: 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c .** .** This fil
0eb0: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 e defines variou
0ec0: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 s limits of what
0ed0: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 SQLite can proc
0ee0: 65 73 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ess..*/../*.** T
0ef0: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
0f00: 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20 42 h of a TEXT or B
0f10: 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20 20 LOB in bytes.
0f20: 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d This also.** lim
0f30: 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 its the size of
0f40: 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 a row in a table
0f50: 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a or index..**.**
0f60: 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 The hard limit
0f70: 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 6f is the ability o
0f80: 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 f a 32-bit signe
0f90: 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 d integer.** to
0fa0: 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 count the size:
0fb0: 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34 38 2^31-1 or 214748
0fc0: 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 3647..*/.#ifndef
0fd0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 SQLITE_MAX_LENG
0fe0: 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 TH.# define SQLI
0ff0: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 TE_MAX_LENGTH 10
1000: 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 00000000.#endif.
1010: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ./*.** This is t
1020: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1030: 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 r of.**.** *
1040: 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 Columns in a tab
1050: 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d le.** * Colum
1060: 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a ns in an index.*
1070: 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 * * Columns i
1080: 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a n a view.** *
1090: 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53 45 Terms in the SE
10a0: 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 T clause of an U
10b0: 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 0a PDATE statement.
10c0: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e ** * Terms in
10d0: 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
10e0: 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 of a SELECT stat
10f0: 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 ement.** * Te
1100: 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50 rms in the GROUP
1110: 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20 BY or ORDER BY
1120: 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45 4c clauses of a SEL
1130: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
1140: 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 * * Terms in
1150: 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75 73 the VALUES claus
1160: 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 73 e of an INSERT s
1170: 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 tatement.**.** T
1180: 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 he hard upper li
1190: 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36 37 mit here is 3267
11a0: 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61 73 6. Most databas
11b0: 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a e people will.**
11c0: 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 tell you that i
11d0: 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 n a well-normali
11e0: 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79 6f zed database, yo
11f0: 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 u usually should
1200: 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 .** not have mor
1210: 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f e than a dozen o
1220: 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r so columns in
1230: 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 any table. And
1240: 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74 68 if.** that is th
1250: 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69 73 e case, there is
1260: 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 no point in hav
1270: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 ing more than a
1280: 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c few.** dozen val
1290: 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 ues in any of th
12a0: 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69 6f e other situatio
12b0: 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f ns described abo
12c0: 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ve..*/.#ifndef S
12d0: 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e QLITE_MAX_COLUMN
12e0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
12f0: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 _MAX_COLUMN 2000
1300: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1310: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
1320: 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51 h of a single SQ
1330: 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 L statement in b
1340: 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 ytes..**.** It u
1350: 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63 61 sed to be the ca
1360: 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67 20 se that setting
1370: 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 this value to ze
1380: 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e ro would.** turn
1390: 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 the limit off.
13a0: 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 That is no long
13b0: 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73 20 er true. It is
13c0: 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 not possible.**
13d0: 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69 6d to turn this lim
13e0: 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 it off..*/.#ifnd
13f0: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 ef SQLITE_MAX_SQ
1400: 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e L_LENGTH.# defin
1410: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c e SQLITE_MAX_SQL
1420: 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30 _LENGTH 10000000
1430: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1440: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 The maximum dep
1450: 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 th of an express
1460: 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20 69 ion tree. This i
1470: 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a s limited to .**
1480: 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79 20 some extent by
1490: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c SQLITE_MAX_SQL_L
14a0: 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 ENGTH. But somet
14b0: 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a ime you might .*
14c0: 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 * want to place
14d0: 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d 69 more severe limi
14e0: 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 ts on the comple
14f0: 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 xity of an .** e
1500: 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a xpression..**.**
1510: 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75 73 A value of 0 us
1520: 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 ed to mean that
1530: 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f the limit was no
1540: 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 t enforced..** B
1550: 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f ut that is no lo
1560: 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65 20 nger true. The
1570: 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74 72 limit is now str
1580: 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a ictly enforced.*
1590: 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a * at all times..
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
15b0: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
15c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
15d0: 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 20 _MAX_EXPR_DEPTH
15e0: 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 1000.#endif../*.
15f0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1600: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 umber of terms i
1610: 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c n a compound SEL
1620: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
1630: 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 * The code gener
1640: 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e ator for compoun
1650: 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 d SELECT stateme
1660: 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 nts does one.**
1670: 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 69 level of recursi
1680: 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d on for each term
1690: 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 66 . A stack overf
16a0: 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a low can result.*
16b0: 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 * if the number
16c0: 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 of terms is too
16d0: 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 74 large. In pract
16e0: 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a ice, most SQL.**
16f0: 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65 20 never has more
1700: 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72 6d than 3 or 4 term
1710: 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65 20 s. Use a value
1720: 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65 0a of 0 to disable.
1730: 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 ** any limit on
1740: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 the number of te
1750: 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e rms in a compoun
1760: 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 t SELECT..*/.#if
1770: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1780: 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a COMPOUND_SELECT.
1790: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
17a0: 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c MAX_COMPOUND_SEL
17b0: 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a ECT 500.#endif..
17c0: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
17d0: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f m number of opco
17e0: 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70 72 des in a VDBE pr
17f0: 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 ogram..** Not cu
1800: 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 rrently enforced
1810: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1820: 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a ITE_MAX_VDBE_OP.
1830: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1840: 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 MAX_VDBE_OP 2500
1850: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
1860: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1870: 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1880: 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 to an SQL functi
1890: 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 on..*/.#ifndef S
18a0: 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 QLITE_MAX_FUNCTI
18b0: 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65 20 ON_ARG.# define
18c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 SQLITE_MAX_FUNCT
18d0: 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 ION_ARG 127.#end
18e0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
18f0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1900: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 in-memory pages
1910: 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 6d to use for the m
1920: 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ain database.**
1930: 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 table and for te
1940: 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 mporary tables.
1950: 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 The SQLITE_DEFA
1960: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a ULT_CACHE_SIZE.*
1970: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1980: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1990: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
19a0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 ITE_DEFAULT_CACH
19b0: 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65 6e E_SIZE 2000.#en
19c0: 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
19d0: 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f TE_DEFAULT_TEMP_
19e0: 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 CACHE_SIZE.# def
19f0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
1a00: 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 LT_TEMP_CACHE_SI
1a10: 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a ZE 500.#endif..
1a20: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
1a30: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74 61 m number of atta
1a40: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 20 ched databases.
1a50: 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62 65 This must be be
1a60: 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 tween 0.** and 3
1a70: 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 6f 0. The upper bo
1a80: 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65 63 und on 30 is bec
1a90: 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69 6e ause a 32-bit in
1aa0: 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 teger bitmap.**
1ab0: 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c is used internal
1ac0: 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74 61 ly to track atta
1ad0: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a ched databases..
1ae0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1af0: 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a 23 E_MAX_ATTACHED.#
1b00: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
1b10: 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a 23 AX_ATTACHED 10.#
1b20: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
1b30: 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 e maximum value
1b40: 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 of a ?nnn wildca
1b50: 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72 73 rd that the pars
1b60: 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a er will accept..
1b70: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1b80: 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e E_MAX_VARIABLE_N
1b90: 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20 53 UMBER.# define S
1ba0: 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 QLITE_MAX_VARIAB
1bb0: 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 LE_NUMBER 999.#e
1bc0: 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d ndif../* Maximum
1bd0: 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68 65 page size. The
1be0: 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 upper bound on
1bf0: 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33 32 this value is 32
1c00: 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 768. This a lim
1c10: 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 it.** imposed by
1c20: 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20 6f the necessity o
1c30: 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76 61 f storing the va
1c40: 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65 20 lue in a 2-byte
1c50: 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
1c60: 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63 74 .** and the fact
1c70: 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73 that the page s
1c80: 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f ize must be a po
1c90: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 wer of 2..**.**
1ca0: 49 66 20 74 68 69 73 20 6c 69 6d 69 74 20 69 73 If this limit is
1cb0: 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 changed, then t
1cc0: 68 65 20 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 he compiled libr
1cd0: 61 72 79 20 69 73 20 74 65 63 68 6e 69 63 61 6c ary is technical
1ce0: 6c 79 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 ly.** incompatib
1cf0: 6c 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69 74 le with an SQLit
1d00: 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69 6c e library compil
1d10: 65 64 20 77 69 74 68 20 61 20 64 69 66 66 65 72 ed with a differ
1d20: 65 6e 74 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a ent limit. If.**
1d30: 20 61 20 70 72 6f 63 65 73 73 20 6f 70 65 72 61 a process opera
1d40: 74 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61 ting on a databa
1d50: 73 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 73 se with a page-s
1d60: 69 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74 ize of 65536 byt
1d70: 65 73 20 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 es .** crashes,
1d80: 74 68 65 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 then an instance
1d90: 20 6f 66 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 of SQLite compi
1da0: 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66 led with the def
1db0: 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 0a ault page-size .
1dc0: 2a 2a 20 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f ** limit will no
1dd0: 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c t be able to rol
1de0: 6c 62 61 63 6b 20 74 68 65 20 61 62 6f 72 74 65 lback the aborte
1df0: 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 d transaction. T
1e00: 68 69 73 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 his could.** lea
1e10: 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f d to database co
1e20: 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 rruption..*/.#if
1e30: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1e40: 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 PAGE_SIZE.# defi
1e50: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ne SQLITE_MAX_PA
1e60: 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23 65 GE_SIZE 32768.#e
1e70: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ndif.../*.** The
1e80: 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 default size of
1e90: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
1ea0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1eb0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 ITE_DEFAULT_PAGE
1ec0: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
1ed0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
1ee0: 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65 6e GE_SIZE 1024.#en
1ef0: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 dif.#if SQLITE_D
1f00: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1f10: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
1f20: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 _SIZE.# undef SQ
1f30: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
1f40: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1f50: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
1f60: 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f AGE_SIZE SQLITE_
1f70: 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 MAX_PAGE_SIZE.#e
1f80: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 ndif../*.** Ordi
1f90: 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 narily, if no va
1fa0: 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c lue is explicitl
1fb0: 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 y provided, SQLi
1fc0: 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61 62 te creates datab
1fd0: 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 ases.** with pag
1fe0: 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 e size SQLITE_DE
1ff0: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e FAULT_PAGE_SIZE.
2000: 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64 20 However, based
2010: 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 on certain.** de
2020: 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 vice characteris
2030: 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 7a tics (sector-siz
2040: 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 e and atomic wri
2050: 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a te() support),.*
2060: 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 6f * SQLite may cho
2070: 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 6c ose a larger val
2080: 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 6e ue. This constan
2090: 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d t is the maximum
20a0: 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 value.** SQLite
20b0: 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 will choose on
20c0: 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e its own..*/.#ifn
20d0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 def SQLITE_MAX_D
20e0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
20f0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
2100: 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
2110: 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 E_SIZE 8192.#end
2120: 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 if.#if SQLITE_MA
2130: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
2140: 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 IZE>SQLITE_MAX_P
2150: 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 AGE_SIZE.# undef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 IZE SQLITE_MAX_P
21b0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a AGE_SIZE.#endif.
21c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
21d0: 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
21e0: 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 in one database
21f0: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 file..**.** This
2200: 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 is really just
2210: 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 the default valu
2220: 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 e for the max_pa
2230: 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e ge_count pragma.
2240: 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 63 .** This value c
2250: 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 6f an be lowered (o
2260: 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 6e r raised) at run
2270: 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 74 -time using that
2280: 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 the.** max_page
2290: 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f _count macro..*/
22a0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
22b0: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 MAX_PAGE_COUNT.#
22c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
22d0: 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 AX_PAGE_COUNT 10
22e0: 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 73741823.#endif.
22f0: 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c ./*.** Maximum l
2300: 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29 ength (in bytes)
2310: 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 of the pattern
2320: 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f in a LIKE or GLO
2330: 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a B.** operator..*
2340: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
2350: 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 _MAX_LIKE_PATTER
2360: 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e N_LENGTH.# defin
2370: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b e SQLITE_MAX_LIK
2380: 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 E_PATTERN_LENGTH
2390: 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 50000.#endif../
23a0: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 *.** Maximum dep
23b0: 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 th of recursion
23c0: 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a for triggers..**
23d0: 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 31 .** A value of 1
23e0: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 74 72 means that a tr
23f0: 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 77 69 igger program wi
2400: 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
2410: 6f 20 69 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65 o itself.** fire
2420: 20 61 6e 79 20 74 72 69 67 67 65 72 73 2e 20 41 any triggers. A
2430: 20 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e value of 0 mean
2440: 73 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65 s that no trigge
2450: 72 20 70 72 6f 67 72 61 6d 73 20 61 74 20 61 6c r programs at al
2460: 6c 20 0a 2a 2a 20 6d 61 79 20 62 65 20 65 78 65 l .** may be exe
2470: 63 75 74 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 cuted..*/.#ifnde
2480: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 f SQLITE_MAX_TRI
2490: 47 47 45 52 5f 44 45 50 54 48 0a 23 20 64 65 66 GGER_DEPTH.# def
24a0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 ine SQLITE_MAX_T
24b0: 52 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30 RIGGER_DEPTH 100
24c0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 0.#endif../*****
24d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
24e0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a sqliteLimit.h *
24f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2500: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2510: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
2520: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
2530: 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
2540: 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
2550: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
2560: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 ********/../* Di
2570: 73 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 sable nuisance w
2580: 61 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 arnings on Borla
2590: 6e 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a nd compilers */.
25a0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f #if defined(__BO
25b0: 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d RLANDC__).#pragm
25c0: 61 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 a warn -rch /* u
25d0: 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 nreachable code
25e0: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 */.#pragma warn
25f0: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f -ccc /* Conditio
2600: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 n is always true
2610: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 or false */.#pr
2620: 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f agma warn -aus /
2630: 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 * Assigned value
2640: 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a is never used *
2650: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d /.#pragma warn -
2660: 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 csu /* Comparing
2670: 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 signed and unsi
2680: 67 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 gned */.#pragma
2690: 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 warn -spa /* Sus
26a0: 70 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 picious pointer
26b0: 61 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 arithmetic */.#e
26c0: 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 ndif../* Needed
26d0: 66 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 for various defi
26e0: 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 nitions... */.#i
26f0: 66 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 fndef _GNU_SOURC
2700: 45 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f E.# define _GNU_
2710: 53 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f SOURCE.#endif../
2720: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 *.** Include sta
2730: 6e 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c ndard header fil
2740: 65 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a es as necessary.
2750: 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 */.#ifdef HAVE_S
2760: 54 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 TDINT_H.#include
2770: 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 <stdint.h>.#end
2780: 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 if.#ifdef HAVE_I
2790: 4e 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 NTTYPES_H.#inclu
27a0: 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a de <inttypes.h>.
27b0: 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 #endif..#define
27c0: 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d SQLITE_INDEX_SAM
27d0: 50 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 PLES 10../*.** T
27e0: 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 his macro is use
27f0: 64 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 d to "hide" some
2800: 20 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 ugliness in cas
2810: 74 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 ting an int.** v
2820: 61 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 alue to a ptr va
2830: 6c 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 lue under the MS
2840: 56 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c VC 64-bit compil
2850: 65 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a er. Casting.**
2860: 20 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 non 64-bit valu
2870: 65 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 es to ptr types
2880: 72 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 results in a "ha
2890: 72 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a rd" error with .
28a0: 2a 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 ** the MSVC 64-b
28b0: 69 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 it compiler whic
28c0: 68 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 h this attempts
28d0: 74 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a to avoid. .**.*
28e0: 2a 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 * A simple compi
28f0: 6c 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 ler pragma or ca
2900: 73 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 sting sequence c
2910: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e ould not be foun
2920: 64 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 d.** to correct
2930: 74 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 this in all situ
2940: 61 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 ations, so this
2950: 6d 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 macro was introd
2960: 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 uced..**.** It c
2970: 6f 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 ould be argued t
2980: 68 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 hat the intptr_t
2990: 20 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 type could be u
29a0: 73 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 sed in this.** c
29b0: 61 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 ase, but that ty
29c0: 70 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 pe is not availa
29d0: 62 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 ble on all compi
29e0: 6c 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 lers, or .** req
29f0: 75 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 uires the #inclu
2a00: 64 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 de of specific h
2a10: 65 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 eaders which dif
2a20: 66 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 fers between.**
2a30: 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a platforms..**.**
2a40: 20 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20 Ticket #3860:
2a50: 54 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 The llvm-gcc-4.2
2a60: 20 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 compiler from A
2a70: 70 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a pple chokes on.*
2a80: 2a 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28 * the ((void*)&(
2a90: 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f (char*)0)[X]) co
2aa0: 6e 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53 nstruct. But MS
2ab0: 56 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 VC chokes on ((v
2ac0: 6f 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f oid*)(X))..** So
2ad0: 20 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 we have to defi
2ae0: 6e 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e ne the macros in
2af0: 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 different ways
2b00: 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 depending on the
2b10: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f .** compiler..*/
2b20: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 .#if defined(__G
2b30: 4e 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 NUC__).# if defi
2b40: 6e 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f ned(HAVE_STDINT_
2b50: 48 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 H).# define SQ
2b60: 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 LITE_INT_TO_PTR(
2b70: 58 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 X) ((void*)(int
2b80: 70 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64 ptr_t)(X)).# d
2b90: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 efine SQLITE_PTR
2ba0: 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e _TO_INT(X) ((in
2bb0: 74 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 t)(intptr_t)(X))
2bc0: 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 .# else.# defi
2bd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f ne SQLITE_INT_TO
2be0: 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a _PTR(X) ((void*
2bf0: 29 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65 )(X)).# define
2c00: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
2c10: 4e 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 NT(X) ((int)(X)
2c20: 29 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a ).# endif.#else.
2c30: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
2c40: 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 INT_TO_PTR(X)
2c50: 28 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a ((void*)&((char*
2c60: 29 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 )0)[X]).# define
2c70: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
2c80: 4e 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28 NT(X) ((int)((
2c90: 28 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a (char*)X)-(char*
2ca0: 29 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a )0)).#endif.../*
2cb0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 .** The SQLITE_T
2cc0: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 HREADSAFE macro
2cd0: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 must be defined
2ce0: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 as either 0 or 1
2cf0: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 ..** Older versi
2d00: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 ons of SQLite us
2d10: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 ed an optional T
2d20: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e HREADSAFE macro.
2d30: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 .** We support t
2d40: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a hat for legacy.*
2d50: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
2d60: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2d70: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 ).#if defined(TH
2d80: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 READSAFE).# defi
2d90: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ne SQLITE_THREAD
2da0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a SAFE THREADSAFE.
2db0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
2dc0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2dd0: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 1.#endif.#endif
2de0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
2df0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
2e00: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 ATUS macro must
2e10: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 be defined as ei
2e20: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 ther 0 or 1..**
2e30: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 It determines wh
2e40: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
2e50: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 features relate
2e60: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f d to .** SQLITE_
2e70: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 CONFIG_MEMSTATUS
2e80: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 are available b
2e90: 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 y default or not
2ea0: 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e . This value can
2eb0: 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 .** be overridde
2ec0: 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 n at runtime usi
2ed0: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 ng the sqlite3_c
2ee0: 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a onfig() API..*/.
2ef0: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
2f00: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 ITE_DEFAULT_MEMS
2f10: 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 TATUS).# define
2f20: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d SQLITE_DEFAULT_M
2f30: 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 EMSTATUS 1.#endi
2f40: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 f../*.** Exactly
2f50: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
2f60: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 owing macros mus
2f70: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 t be defined in
2f80: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 order to.** spec
2f90: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 ify which memory
2fa0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
2fb0: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a ystem to use..**
2fc0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 .** SQLITE_S
2fd0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 YSTEM_MALLOC
2fe0: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 // Use nor
2ff0: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f mal system mallo
3000: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 c().** SQLIT
3010: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 E_MEMDEBUG
3020: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 // Debu
3030: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 gging version of
3040: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 system malloc()
3050: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d .** SQLITE_M
3060: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 EMORY_SIZE
3070: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 // interna
3080: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a l allocator #1.*
3090: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 * SQLITE_MMA
30a0: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 P_HEAP_SIZE
30b0: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 // internal
30c0: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 mmap() allocator
30d0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 .** SQLITE_P
30e0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 OW2_MEMORY_SIZE
30f0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 // interna
3100: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 l power-of-two a
3110: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 llocator.**.** I
3120: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 f none of the ab
3130: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c ove are defined,
3140: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 then set SQLITE
3150: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 _SYSTEM_MALLOC a
3160: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 s.** the default
3170: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
3180: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d (SQLITE_SYSTEM_M
3190: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 ALLOC)+defined(S
31a0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b QLITE_MEMDEBUG)+
31b0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
31c0: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 LITE_MEMORY_SIZE
31d0: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 )+defined(SQLITE
31e0: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 _MMAP_HEAP_SIZE)
31f0: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 +\. defined(S
3200: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 QLITE_POW2_MEMOR
3210: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f Y_SIZE)>1.# erro
3220: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f r "At most one o
3230: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
3240: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e compile-time con
3250: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
3260: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 ns\. is allows:
3270: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3280: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d LLOC, SQLITE_MEM
3290: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 DEBUG, SQLITE_ME
32a0: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c MORY_SIZE,\. SQL
32b0: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 ITE_MMAP_HEAP_SI
32c0: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f ZE, SQLITE_POW2_
32d0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e MEMORY_SIZE".#en
32e0: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 dif.#if defined(
32f0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3300: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 LLOC)+defined(SQ
3310: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c LITE_MEMDEBUG)+\
3320: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
3330: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 ITE_MEMORY_SIZE)
3340: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f +defined(SQLITE_
3350: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b MMAP_HEAP_SIZE)+
3360: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
3370: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
3380: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 _SIZE)==0.# defi
3390: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ne SQLITE_SYSTEM
33a0: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 _MALLOC 1.#endif
33b0: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ../*.** If SQLIT
33c0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
33d0: 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c MIT is not zero,
33e0: 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 then try to kee
33f0: 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f p the.** sizes o
3400: 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 f memory allocat
3410: 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 ions below this
3420: 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 value where poss
3430: 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 ible..*/.#if !de
3440: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c fined(SQLITE_MAL
3450: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a LOC_SOFT_LIMIT).
3460: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
3470: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 MALLOC_SOFT_LIMI
3480: 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f T 1024.#endif../
3490: 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 *.** We need to
34a0: 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
34b0: 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 URCE as follows
34c0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 in order to enab
34d0: 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 le.** recursive
34e0: 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 mutexes on most
34f0: 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 Unix systems. B
3500: 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 ut Mac OS X is d
3510: 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 ifferent..** The
3520: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 _XOPEN_SOURCE d
3530: 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f efine causes pro
3540: 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 blems for Mac OS
3550: 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a X we are told,.
3560: 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 ** so it is omit
3570: 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 ted there. See
3580: 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a ticket #2673..**
3590: 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 .** Later we lea
35a0: 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 rn that _XOPEN_S
35b0: 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 OURCE is poorly
35c0: 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a or incorrectly.*
35d0: 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e * implemented on
35e0: 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 some systems.
35f0: 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 So we avoid defi
3600: 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a ning it at all.*
3610: 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 * if it is alrea
3620: 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 dy defined or if
3630: 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 it is unneeded
3640: 62 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a because we are.*
3650: 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 * not doing a th
3660: 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 readsafe build.
3670: 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a Ticket #2681..*
3680: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 *.** See also ti
3690: 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 cket #2741..*/.#
36a0: 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 if !defined(_XOP
36b0: 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 EN_SOURCE) && !d
36c0: 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f efined(__DARWIN_
36d0: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f _) && !defined(_
36e0: 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
36f0: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 ITE_THREADSAFE.#
3700: 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f define _XOPEN_
3710: 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e SOURCE 500 /* N
3720: 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 eeded to enable
3730: 70 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 pthread recursiv
3740: 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e e mutexes */.#en
3750: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 dif../*.** The T
3760: 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f CL headers are o
3770: 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 nly needed when
3780: 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 compiling the TC
3790: 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 L bindings..*/.#
37a0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
37b0: 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 E_TCL) || define
37c0: 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 d(TCLSH).# inclu
37d0: 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 de <tcl.h>.#endi
37e0: 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 f../*.** Many pe
37f0: 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 ople are failing
3800: 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 to set -DNDEBUG
3810: 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e =1 when compilin
3820: 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 g SQLite..** Set
3830: 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 ting NDEBUG make
3840: 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c s the code small
3850: 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 er and run faste
3860: 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f r. So the follo
3870: 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 wing.** lines ar
3880: 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d e added to autom
3890: 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 atically set NDE
38a0: 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d BUG unless the -
38b0: 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a DSQLITE_DEBUG=1.
38c0: 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 ** option is set
38d0: 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 . Thus NDEBUG b
38e0: 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e ecomes an opt-in
38f0: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 rather than an
3900: 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 opt-out.** featu
3910: 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 re..*/.#if !defi
3920: 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 ned(NDEBUG) && !
3930: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
3940: 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 EBUG) .# define
3950: 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a NDEBUG 1.#endif.
3960: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 ./*.** The testc
3970: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 ase() macro is u
3980: 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f sed to aid in co
3990: 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 verage testing.
39a0: 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 When .** doing
39b0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
39c0: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 , the condition
39d0: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d inside the argum
39e0: 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 ent to.** testca
39f0: 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 se() must be eva
3a00: 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 luated both true
3a10: 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 and false in or
3a20: 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 der to.** get fu
3a30: 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 ll branch covera
3a40: 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 ge. The testcas
3a50: 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 e() macro is ins
3a60: 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 erted.** to help
3a70: 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 ensure adequate
3a80: 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 test coverage i
3a90: 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 n places where s
3aa0: 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 imple.** conditi
3ab0: 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 on/decision cove
3ac0: 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 rage is inadequa
3ad0: 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 te. For example
3ae0: 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 , testcase().**
3af0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d can be used to m
3b00: 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 ake sure boundar
3b10: 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 y values are tes
3b20: 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 ted. For.** bit
3b30: 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 mask tests, test
3b40: 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 case() can be us
3b50: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
3b60: 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 each bit.** is s
3b70: 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 ignificant and u
3b80: 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 sed at least onc
3b90: 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 e. On switch st
3ba0: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 atements.** wher
3bb0: 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 e multiple cases
3bc0: 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 go to the same
3bd0: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 block of code, t
3be0: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e estcase().** can
3bf0: 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c insure that all
3c00: 20 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 cases are evalu
3c10: 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 ated..**.*/.#ifd
3c20: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ef SQLITE_COVERA
3c30: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 GE_TEST.SQLITE_P
3c40: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
3c50: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e lite3Coverage(in
3c60: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 t);.# define tes
3c70: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 tcase(X) if( X
3c80: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 ){ sqlite3Covera
3c90: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a ge(__LINE__); }.
3ca0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 #else.# define t
3cb0: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 estcase(X).#endi
3cc0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 f../*.** The TES
3cd0: 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 TONLY macro is u
3ce0: 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 sed to enclose v
3cf0: 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 ariable declarat
3d00: 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 ions or.** other
3d10: 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 bits of code th
3d20: 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f at are needed to
3d30: 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 support the arg
3d40: 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e uments.** within
3d50: 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 testcase() and
3d60: 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e assert() macros.
3d70: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
3d80: 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
3d90: 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 ned(SQLITE_COVER
3da0: 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 AGE_TEST).# defi
3db0: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 ne TESTONLY(X)
3dc0: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 X.#else.# define
3dd0: 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e TESTONLY(X).#en
3de0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 dif../*.** Somet
3df0: 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 imes we need a s
3e00: 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 mall amount of c
3e10: 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 ode such as a va
3e20: 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a riable initializ
3e30: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 ation.** to setu
3e40: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 p for a later as
3e50: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
3e60: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e . We do not wan
3e70: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a t this code to.*
3e80: 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 * appear when as
3e90: 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c sert() is disabl
3ea0: 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ed. The followi
3eb0: 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 ng macro is ther
3ec0: 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f efore.** used to
3ed0: 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 contain that se
3ee0: 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 tup code. The "
3ef0: 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 VVA" acronym sta
3f00: 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 nds for.** "Veri
3f10: 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 fication, Valida
3f20: 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 tion, and Accred
3f30: 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 itation". In ot
3f40: 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a her words, the.*
3f50: 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 * code within VV
3f60: 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e A_ONLY() will on
3f70: 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 ly run during ve
3f80: 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 rification proce
3f90: 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 sses..*/.#ifndef
3fa0: 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 NDEBUG.# define
3fb0: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a VVA_ONLY(X) X.
3fc0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 #else.# define V
3fd0: 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 VA_ONLY(X).#endi
3fe0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 f../*.** The ALW
3ff0: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 AYS and NEVER ma
4000: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f cros surround bo
4010: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e olean expression
4020: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 s which .** are
4030: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 intended to alwa
4040: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 ys be true or fa
4050: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c lse, respectivel
4060: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 y. Such.** expr
4070: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 essions could be
4080: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 omitted from th
4090: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c e code completel
40a0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 y. But they.**
40b0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 are included in
40c0: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f a few cases in o
40d0: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 rder to enhance
40e0: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a the resilience.*
40f0: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 * of SQLite to u
4100: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 nexpected behavi
4110: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 or - to make the
4120: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c code "self-heal
4130: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 ing".** or "duct
4140: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e ile" rather than
4150: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 being "brittle"
4160: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 and crashing at
4170: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 the first.** hi
4180: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 nt of unplanned
4190: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
41a0: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
41b0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 ALWAYS and NEVER
41c0: 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 are added for d
41d0: 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a efensive code..*
41e0: 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 *.** When doing
41f0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
4200: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 ALWAYS and NEVE
4210: 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 R are hard-coded
4220: 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 to.** be true a
4230: 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 nd false so that
4240: 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 the unreachable
4250: 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 code then speci
4260: 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 fy will.** not b
4270: 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 e counted as unt
4280: 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 ested code..*/.#
4290: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
42a0: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 E_COVERAGE_TEST)
42b0: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 .# define ALWAYS
42c0: 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 (X) (1).# d
42d0: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
42e0: 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 (0).#elif !
42f0: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
4300: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 # define ALWAYS(
4310: 58 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 X) ((X)?1:(
4320: 61 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20 assert(0),0)).#
4330: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 define NEVER(X)
4340: 20 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65 ((X)?(asse
4350: 72 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 rt(0),1):0).#els
4360: 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 e.# define ALWAY
4370: 53 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 S(X) (X).#
4380: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 define NEVER(X)
4390: 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 (X).#endif
43a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 ../*.** The macr
43b0: 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 o unlikely() is
43c0: 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 a hint that surr
43d0: 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a ounds a boolean.
43e0: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ** expression th
43f0: 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 at is usually fa
4400: 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 lse. Macro like
4410: 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a ly() surrounds.*
4420: 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 * a boolean expr
4430: 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 ession that is u
4440: 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 sually true. GC
4450: 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 C is able to.**
4460: 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 use these hints
4470: 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 to generate bett
4480: 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d er code, sometim
4490: 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e es..*/.#if defin
44a0: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
44b0: 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 0.# define likel
44c0: 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 y(X) __builti
44d0: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a n_expect((X),1).
44e0: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c # define unlikel
44f0: 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f y(X) __builtin_
4500: 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 expect((X),0).#e
4510: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b lse.# define lik
4520: 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a ely(X) !!(X).
4530: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c # define unlikel
4540: 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 y(X) !!(X).#end
4550: 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
4560: 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 *** Include sqli
4570: 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 te3.h in the mid
4580: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
4590: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
45a0: 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
45b0: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 *** Begin file s
45c0: 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a qlite3.h *******
45d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f0: 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
4600: 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
4610: 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
4620: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
4630: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
4640: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
4650: 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
4660: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
4670: 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
4680: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
4690: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
46a0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
46b0: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
46c0: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
46d0: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
46e0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
46f0: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
4700: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
4710: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
4720: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
4730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
4770: 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 is header file d
4780: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
4790: 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 face that the SQ
47a0: 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 Lite library.**
47b0: 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 presents to clie
47c0: 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 nt programs. If
47d0: 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 a C-function, s
47e0: 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 tructure, dataty
47f0: 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 pe,.** or consta
4800: 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f nt definition do
4810: 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e es not appear in
4820: 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e this file, then
4830: 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 it is.** not a
4840: 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 published API of
4850: 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a SQLite, is subj
4860: 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 ect to change wi
4870: 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c thout.** notice,
4880: 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 and should not
4890: 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 be referenced by
48a0: 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 programs that u
48b0: 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a se SQLite..**.**
48c0: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 Some of the def
48d0: 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 initions that ar
48e0: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 e in this file a
48f0: 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 re marked as.**
4900: 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 "experimental".
4910: 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e Experimental in
4920: 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 terfaces are nor
4930: 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 mally new.** fea
4940: 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 tures recently a
4950: 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 dded to SQLite.
4960: 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 We do not antic
4970: 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a ipate changes.**
4980: 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c to experimental
4990: 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 interfaces but
49a0: 72 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 reserve the righ
49b0: 74 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 t to make minor
49c0: 63 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 changes.** if ex
49d0: 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 perience from us
49e0: 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 e "in the wild"
49f0: 73 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 suggest such cha
4a00: 6e 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 nges are prudent
4a10: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 ..**.** The offi
4a20: 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 cial C-language
4a30: 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f API documentatio
4a40: 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 n for SQLite is
4a50: 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 derived.** from
4a60: 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 comments in this
4a70: 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c file. This fil
4a80: 65 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 e is the authori
4a90: 74 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a tative source.**
4aa0: 20 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 on how SQLite i
4ab0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 nterfaces are su
4ac0: 70 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 ppose to operate
4ad0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 ..**.** The name
4ae0: 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e of this file un
4af0: 64 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f der configuratio
4b00: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 n management is
4b10: 22 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a "sqlite.h.in"..*
4b20: 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d * The makefile m
4b30: 61 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 akes some minor
4b40: 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 changes to this
4b50: 66 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e file (such as in
4b60: 73 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 serting.** the v
4b70: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 ersion number) a
4b80: 6e 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e nd changes its n
4b90: 61 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e ame to "sqlite3.
4ba0: 68 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 h" as.** part of
4bb0: 20 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 the build proce
4bc0: 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f ss..*/.#ifndef _
4bd0: 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 SQLITE3_H_.#defi
4be0: 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 ne _SQLITE3_H_.#
4bf0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e include <stdarg.
4c00: 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 h> /* Needed
4c10: 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 for the definit
4c20: 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a ion of va_list *
4c30: 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 /../*.** Make su
4c40: 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 re we can call t
4c50: 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 his stuff from C
4c60: 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 ++..*/.#if 0.ext
4c70: 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 ern "C" {.#endif
4c80: 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 .../*.** Add the
4c90: 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 ability to over
4ca0: 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f ride 'extern'.*/
4cb0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
4cc0: 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 EXTERN.# define
4cd0: 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 SQLITE_EXTERN ex
4ce0: 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 tern.#endif..#if
4cf0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a ndef SQLITE_API.
4d00: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
4d10: 41 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a API.#endif.../*.
4d20: 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d ** These no-op m
4d30: 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69 acros are used i
4d40: 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 n front of inter
4d50: 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 faces to mark th
4d60: 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ose.** interface
4d70: 73 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72 s as either depr
4d80: 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69 ecated or experi
4d90: 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 mental. New app
4da0: 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f lications.** sho
4db0: 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72 uld not use depr
4dc0: 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 ecated interface
4dd0: 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 s - they are sup
4de0: 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 port for backwar
4df0: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c ds.** compatibil
4e00: 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 ity only. Appli
4e10: 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 cation writers s
4e20: 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 hould be aware t
4e30: 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e hat.** experimen
4e40: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 tal interfaces a
4e50: 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 re subject to ch
4e60: 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 ange in point re
4e70: 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 leases..**.** Th
4e80: 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 ese macros used
4e90: 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 to resolve to va
4ea0: 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 rious kinds of c
4eb0: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 ompiler magic th
4ec0: 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 at.** would gene
4ed0: 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 rate warning mes
4ee0: 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 sages when they
4ef0: 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 were used. But
4f00: 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 that.** compiler
4f10: 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 magic ended up
4f20: 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 generating such
4f30: 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 a flurry of bug
4f40: 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 reports.** that
4f50: 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 we have taken it
4f60: 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e all out and gon
4f70: 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 e back to using
4f80: 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d simple.** noop m
4f90: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e acros..*/.#defin
4fa0: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 e SQLITE_DEPRECA
4fb0: 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 TED.#define SQLI
4fc0: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a TE_EXPERIMENTAL.
4fd0: 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 ./*.** Ensure th
4fe0: 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 ese symbols were
4ff0: 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 not defined by
5000: 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 some previous he
5010: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 ader file..*/.#i
5020: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 fdef SQLITE_VERS
5030: 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 ION.# undef SQLI
5040: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 TE_VERSION.#endi
5050: 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
5060: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 VERSION_NUMBER.#
5070: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 undef SQLITE_VE
5080: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e RSION_NUMBER.#en
5090: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 dif../*.** CAPI3
50a0: 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d REF: Compile-Tim
50b0: 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f e Library Versio
50c0: 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 n Numbers {H1001
50d0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S60100>.**.*
50e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 * The SQLITE_VER
50f0: 53 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f SION and SQLITE_
5100: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 VERSION_NUMBER #
5110: 64 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 defines in.** th
5120: 65 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 e sqlite3.h file
5130: 20 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 specify the ver
5140: 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 sion of SQLite w
5150: 69 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 ith which.** tha
5160: 74 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 t header file is
5170: 20 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a associated..**.
5180: 2a 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 ** The "version"
5190: 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 of SQLite is a
51a0: 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f string of the fo
51b0: 72 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 rm "W.X.Y" or "W
51c0: 2e 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 .X.Y.Z"..** The
51d0: 57 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 W value is major
51e0: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 version number
51f0: 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20 and is always 3
5200: 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 in SQLite3..** T
5210: 68 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 he W value only
5220: 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 changes when bac
5230: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
5240: 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 lity is.** broke
5250: 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 n and we intend
5260: 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 to never break b
5270: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
5280: 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 bility..** The X
5290: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 value is the mi
52a0: 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 nor version numb
52b0: 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e er and only chan
52c0: 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 ges when.** ther
52d0: 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 e are major feat
52e0: 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 ure enhancements
52f0: 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 that are forwar
5300: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a ds compatible.**
5310: 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 but not backwar
5320: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a ds compatible..*
5330: 2a 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 * The Y value is
5340: 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d the release num
5350: 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 ber and is incre
5360: 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 mented with.** e
5370: 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 ach release but
5380: 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 resets back to 0
5390: 20 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69 whenever X is i
53a0: 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 ncremented..** T
53b0: 68 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 he Z value only
53c0: 61 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 appears on branc
53d0: 68 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a h releases..**.*
53e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 * The SQLITE_VER
53f0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 SION_NUMBER is a
5400: 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 n integer that i
5410: 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a s computed as.**
5420: 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
5430: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
5440: 3e 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 >.** SQLITE_VERS
5450: 49 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 ION_NUMBER = W*1
5460: 30 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20 000000 + X*1000
5470: 2b 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 + Y.** </pre></b
5480: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
5490: 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 Since version 3
54a0: 2e 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f .6.18, SQLite so
54b0: 75 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65 urce code has be
54c0: 65 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 en stored in the
54d0: 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 .** <a href="htt
54e0: 70 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 p://www.fossil-s
54f0: 63 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 cm.org/">fossil
5500: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 configuration ma
5510: 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 nagement.** syst
5520: 65 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c em</a>. The SQL
5530: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a ITE_SOURCE_ID.**
5540: 20 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69 macro is a stri
5550: 6e 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 ng which identif
5560: 69 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 ies a particular
5570: 20 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c check-in of SQL
5580: 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 ite.** within it
5590: 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 s configuration
55a0: 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65 management syste
55b0: 6d 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63 m. The string c
55c0: 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 ontains the.** d
55d0: 61 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 ate and time of
55e0: 74 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 the check-in (UT
55f0: 43 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68 C) and an SHA1 h
5600: 61 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72 ash of the entir
5610: 65 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65 e.** source tree
5620: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
5630: 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 : [sqlite3_libve
5640: 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 rsion()],.** [sq
5650: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
5660: 5f 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c _number()], [sql
5670: 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d ite3_sourceid()]
5680: 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 ,.** [sqlite_ver
5690: 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c sion()] and [sql
56a0: 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d ite_source_id()]
56b0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
56c0: 65 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b ents: [H10011] [
56d0: 48 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 H10014].*/.#defi
56e0: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f ne SQLITE_VERSIO
56f0: 4e 20 20 20 20 20 20 20 20 22 33 2e 36 2e 32 31 N "3.6.21
5700: 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ".#define SQLITE
5710: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 _VERSION_NUMBER
5720: 33 30 30 36 30 32 31 0a 23 64 65 66 69 6e 65 20 3006021.#define
5730: 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 SQLITE_SOURCE_ID
5740: 20 20 20 20 20 20 22 32 30 30 39 2d 31 32 2d 30 "2009-12-0
5750: 37 20 31 36 3a 33 39 3a 31 33 20 31 65 64 38 38 7 16:39:13 1ed88
5760: 65 39 64 30 31 65 39 65 64 61 35 63 62 63 36 32 e9d01e9eda5cbc62
5770: 32 65 37 36 31 34 32 37 37 66 32 39 62 63 63 35 2e7614277f29bcc5
5780: 35 31 63 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 51c"../*.** CAPI
5790: 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 3REF: Run-Time L
57a0: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e ibrary Version N
57b0: 75 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20 umbers {H10020}
57c0: 3c 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S60100>.** KEYW
57d0: 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 ORDS: sqlite3_ve
57e0: 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 rsion.**.** Thes
57f0: 65 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f e interfaces pro
5800: 76 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e vide the same in
5810: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 formation as the
5820: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e [SQLITE_VERSION
5830: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 ],.** [SQLITE_VE
5840: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 RSION_NUMBER], a
5850: 6e 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 nd [SQLITE_SOURC
5860: 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69 E_ID] #defines i
5870: 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a n the header,.**
5880: 20 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 but are associa
5890: 74 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 ted with the lib
58a0: 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 rary instead of
58b0: 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e the header file.
58c0: 20 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 Cautious.** pr
58d0: 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 ogrammers might
58e0: 69 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 include assert()
58f0: 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 statements in t
5900: 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e heir application
5910: 20 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 to.** verify th
5920: 61 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e at values return
5930: 65 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 ed by these inte
5940: 72 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 rfaces match the
5950: 20 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 macros in.** th
5960: 65 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 e header, and th
5970: 75 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 us insure that t
5980: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 he application i
5990: 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 s.** compiled wi
59a0: 74 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 th matching libr
59b0: 61 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 ary and header f
59c0: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f iles..**.** <blo
59d0: 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
59e0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
59f0: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 _libversion_numb
5a00: 65 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 er()==SQLITE_VER
5a10: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a SION_NUMBER );.*
5a20: 2a 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 * assert( strcmp
5a30: 28 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 (sqlite3_sourcei
5a40: 64 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 d(),SQLITE_SOURC
5a50: 45 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 E_ID)==0 );.** a
5a60: 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 ssert( strcmp(sq
5a70: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
5a80: 2c 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 ,SQLITE_VERSION)
5a90: 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e ==0 );.** </pre>
5aa0: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
5ab0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
5ac0: 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e libversion() fun
5ad0: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
5ae0: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 e same informati
5af0: 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 on as is.** in t
5b00: 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 he sqlite3_versi
5b10: 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 on[] string cons
5b20: 74 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 tant. The funct
5b30: 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a ion is provided.
5b40: 2a 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c ** for use in DL
5b50: 4c 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 Ls since DLL use
5b60: 72 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f rs usually do no
5b70: 74 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 t have direct ac
5b80: 63 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a cess to string.*
5b90: 2a 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 * constants with
5ba0: 69 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d in the DLL. Sim
5bb0: 69 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 ilarly, the sqli
5bc0: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66 te3_sourceid() f
5bd0: 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 unction.** retur
5be0: 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f ns the same info
5bf0: 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e rmation as is in
5c00: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 the [SQLITE_SOU
5c10: 52 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 RCE_ID] #define
5c20: 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 of.** the header
5c30: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 file..**.** See
5c40: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 also: [sqlite_v
5c50: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 ersion()] and [s
5c60: 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 qlite_source_id(
5c70: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
5c80: 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d ements: [H10021]
5c90: 20 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 [H10022] [H1002
5ca0: 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
5cb0: 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 const char sqli
5cc0: 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 te3_version[] =
5cd0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a SQLITE_VERSION;.
5ce0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
5cf0: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c char *sqlite3_l
5d00: 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b ibversion(void);
5d10: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
5d20: 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
5d30: 73 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a sourceid(void);.
5d40: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
5d50: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
5d60: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a n_number(void);.
5d70: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
5d80: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 Test To See If
5d90: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 The Library Is T
5da0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 hreadsafe {H1010
5db0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S60100>.**.*
5dc0: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 * SQLite can be
5dd0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 compiled with or
5de0: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 without mutexes
5df0: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b . When.** the [
5e00: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
5e10: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f E] C preprocesso
5e20: 72 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20 r macro is 1 or
5e30: 32 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 2, mutexes.** ar
5e40: 65 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 e enabled and SQ
5e50: 4c 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 Lite is threadsa
5e60: 66 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a fe. When the.**
5e70: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
5e80: 41 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c AFE] macro is 0,
5e90: 20 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 .** the mutexes
5ea0: 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 are omitted. W
5eb0: 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 ithout the mutex
5ec0: 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 es, it is not sa
5ed0: 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c fe.** to use SQL
5ee0: 69 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 ite concurrently
5ef0: 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 from more than
5f00: 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a one thread..**.*
5f10: 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 * Enabling mutex
5f20: 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 es incurs a meas
5f30: 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e urable performan
5f40: 63 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 ce penalty..** S
5f50: 6f 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 o if speed is of
5f60: 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e utmost importan
5f70: 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e ce, it makes sen
5f80: 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a se to disable.**
5f90: 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 the mutexes. B
5fa0: 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 ut for maximum s
5fb0: 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 afety, mutexes s
5fc0: 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 hould be enabled
5fd0: 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 ..** The default
5fe0: 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 behavior is for
5ff0: 20 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 mutexes to be e
6000: 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 nabled..**.** Th
6010: 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e is interface can
6020: 20 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61 be used by an a
6030: 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 pplication to ma
6040: 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
6050: 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 .** version of S
6060: 51 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73 QLite that it is
6070: 20 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 linking against
6080: 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 was compiled wi
6090: 74 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65 th.** the desire
60a0: 64 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 d setting of the
60b0: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
60c0: 41 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a AFE] macro..**.*
60d0: 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
60e0: 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e only reports on
60f0: 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d the compile-tim
6100: 65 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a e mutex setting.
6110: 2a 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 ** of the [SQLIT
6120: 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c E_THREADSAFE] fl
6130: 61 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 ag. If SQLite i
6140: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
6150: 2a 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ** SQLITE_THREAD
6160: 53 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 SAFE=1 then mute
6170: 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 xes are enabled
6180: 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a by default but.*
6190: 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f * can be fully o
61a0: 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61 r partially disa
61b0: 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c bled using a cal
61c0: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f l to [sqlite3_co
61d0: 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 nfig()].** with
61e0: 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54 the verbs [SQLIT
61f0: 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 E_CONFIG_SINGLET
6200: 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f HREAD], [SQLITE_
6210: 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
6220: 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 AD],.** or [SQLI
6230: 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d TE_CONFIG_MUTEX]
6240: 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
6250: 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 lue of this func
6260: 74 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e tion shows.** on
6270: 6c 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 ly the default c
6280: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 ompile-time sett
6290: 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e ing, not any run
62a0: 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a -time changes.**
62b0: 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 to that setting
62c0: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 ..**.** See the
62d0: 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
62e0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 documentation f
62f0: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
6300: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
6310: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b Requirements: [
6320: 48 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d H10101] [H10102]
6330: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
6340: 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 nt sqlite3_threa
6350: 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a dsafe(void);../*
6360: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 .** CAPI3REF: Da
6370: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
6380: 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 n Handle {H12000
6390: 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 } <S40200>.** KE
63a0: 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 YWORDS: {databas
63b0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 e connection} {d
63c0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
63d0: 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 ons}.**.** Each
63e0: 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 open SQLite data
63f0: 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e base is represen
6400: 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 ted by a pointer
6410: 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 to an instance
6420: 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 of.** the opaque
6430: 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 structure named
6440: 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 "sqlite3". It
6450: 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 is useful to thi
6460: 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 nk of an sqlite3
6470: 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 .** pointer as a
6480: 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b n object. The [
6490: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
64a0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 [sqlite3_open16
64b0: 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
64c0: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 ite3_open_v2()]
64d0: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 interfaces are i
64e0: 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c ts constructors,
64f0: 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c and [sqlite3_cl
6500: 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 ose()].** is its
6510: 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68 destructor. Th
6520: 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 ere are many oth
6530: 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73 er interfaces (s
6540: 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 uch as.** [sqlit
6550: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
6560: 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 , [sqlite3_creat
6570: 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 e_function()], a
6580: 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 nd.** [sqlite3_b
6590: 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 usy_timeout()] t
65a0: 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65 o name but three
65b0: 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f ) that are metho
65c0: 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 ds on an.** sqli
65d0: 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 te3 object..*/.t
65e0: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
65f0: 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a lite3 sqlite3;..
6600: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
6610: 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 64-Bit Integer T
6620: 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 ypes {H10200} <S
6630: 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10110>.** KEYWOR
6640: 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 DS: sqlite_int64
6650: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a sqlite_uint64.*
6660: 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 *.** Because the
6670: 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 re is no cross-p
6680: 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 latform way to s
6690: 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e pecify 64-bit in
66a0: 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 teger types.** S
66b0: 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 QLite includes t
66c0: 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 ypedefs for 64-b
66d0: 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e it signed and un
66e0: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e signed integers.
66f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
6700: 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c e3_int64 and sql
6710: 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20 ite3_uint64 are
6720: 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79 the preferred ty
6730: 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a pe definitions..
6740: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e ** The sqlite_in
6750: 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 t64 and sqlite_u
6760: 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20 int64 types are
6770: 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61 supported for ba
6780: 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 ckwards.** compa
6790: 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a tibility only..*
67a0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
67b0: 73 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 s: [H10201] [H10
67c0: 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 202].*/.#ifdef S
67d0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 QLITE_INT64_TYPE
67e0: 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 . typedef SQLIT
67f0: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c E_INT64_TYPE sql
6800: 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 ite_int64;. typ
6810: 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 edef unsigned SQ
6820: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 LITE_INT64_TYPE
6830: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 sqlite_uint64;.#
6840: 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
6850: 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 C_VER) || define
6860: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
6870: 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 typedef __int6
6880: 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 4 sqlite_int64;.
6890: 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e typedef unsign
68a0: 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 ed __int64 sqlit
68b0: 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a e_uint64;.#else.
68c0: 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c typedef long l
68d0: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 ong int sqlite_i
68e0: 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 nt64;. typedef
68f0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
6900: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 ng int sqlite_ui
6910: 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 nt64;.#endif.typ
6920: 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 edef sqlite_int6
6930: 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 4 sqlite3_int64;
6940: 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f .typedef sqlite_
6950: 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 uint64 sqlite3_u
6960: 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 int64;../*.** If
6970: 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 compiling for a
6980: 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 processor that
6990: 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 lacks floating p
69a0: 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a oint support,.**
69b0: 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 substitute inte
69c0: 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 ger for floating
69d0: 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 -point..*/.#ifde
69e0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
69f0: 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 OATING_POINT.# d
6a00: 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c efine double sql
6a10: 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 ite3_int64.#endi
6a20: 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 f../*.** CAPI3RE
6a30: 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 F: Closing A Dat
6a40: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
6a50: 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 {H12010} <S3010
6a60: 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 0><S40200>.**.**
6a70: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
6a80: 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
6a90: 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 for the [sqlite3
6aa0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 ] object..**.**
6ab0: 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73 Applications mus
6ac0: 74 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c t [sqlite3_final
6ad0: 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 ize | finalize]
6ae0: 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 all [prepared st
6af0: 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 atements].** and
6b00: 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
6b10: 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c lose | close] al
6b20: 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d l [BLOB handles]
6b30: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
6b40: 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 .** the [sqlite3
6b50: 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 ] object prior t
6b60: 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 o attempting to
6b70: 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 close the object
6b80: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 ..**.** If [sqli
6b90: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 te3_close()] is
6ba0: 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 invoked while a
6bb0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f transaction is o
6bc0: 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e pen,.** the tran
6bd0: 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d saction is autom
6be0: 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 atically rolled
6bf0: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 back..**.** The
6c00: 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b C parameter to [
6c10: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 sqlite3_close(C)
6c20: 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 ] must be either
6c30: 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 a NULL.** point
6c40: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 er or an [sqlite
6c50: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 3] object pointe
6c60: 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 r obtained.** fr
6c70: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e om [sqlite3_open
6c80: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
6c90: 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b en16()], or.** [
6ca0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
6cb0: 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 )], and not prev
6cc0: 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a iously closed..*
6cd0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
6ce0: 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b s:.** [H12011] [
6cf0: 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d H12012] [H12013]
6d00: 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 [H12014] [H1201
6d10: 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 5] [H12019].*/.S
6d20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
6d30: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 lite3_close(sqli
6d40: 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 te3 *);../*.** T
6d50: 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 he type for a ca
6d60: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e llback function.
6d70: 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 .** This is lega
6d80: 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 cy and deprecate
6d90: 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 d. It is includ
6da0: 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 ed for historica
6db0: 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 l.** compatibili
6dc0: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f ty and is not do
6dd0: 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 cumented..*/.typ
6de0: 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 edef int (*sqlit
6df0: 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 e3_callback)(voi
6e00: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 d*,int,char**, c
6e10: 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 har**);../*.** C
6e20: 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 API3REF: One-Ste
6e30: 70 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f p Query Executio
6e40: 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 n Interface {H12
6e50: 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 100} <S10000>.**
6e60: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
6e70: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 exec() interface
6e80: 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 is a convenient
6e90: 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 way of running
6ea0: 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 one or more.** S
6eb0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 QL statements wi
6ec0: 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
6ed0: 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 write a lot of C
6ee0: 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d code. The UTF-
6ef0: 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 8 encoded.** SQL
6f00: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 statements are
6f10: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 passed in as the
6f20: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
6f30: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 r to sqlite3_exe
6f40: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 c()..** The stat
6f50: 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 ements are evalu
6f60: 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 ated one by one
6f70: 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 until either an
6f80: 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 error or.** an i
6f90: 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f nterrupt is enco
6fa0: 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 untered, or unti
6fb0: 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 l they are all d
6fc0: 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 one. The 3rd pa
6fd0: 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e rameter.** is an
6fe0: 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 optional callba
6ff0: 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b ck that is invok
7000: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
7010: 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 row of any quer
7020: 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f y.** results pro
7030: 64 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c duced by the SQL
7040: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 statements. Th
7050: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 5th parameter
7060: 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 tells where.** t
7070: 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f o write any erro
7080: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a r messages..**.*
7090: 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 * The error mess
70a0: 61 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 age passed back
70b0: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 through the 5th
70c0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c parameter is hel
70d0: 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f d.** in memory o
70e0: 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
70f0: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
7100: 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d To avoid a mem
7110: 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 ory leak,.** the
7120: 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 calling applica
7130: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c tion should call
7140: 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
7150: 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a ] on any error.*
7160: 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e * message return
7170: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 ed through the 5
7180: 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 th parameter whe
7190: 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 n it has finishe
71a0: 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 d using.** the e
71b0: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a rror message..**
71c0: 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 .** If the SQL s
71d0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
71e0: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 2nd parameter is
71f0: 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 NULL or an empt
7200: 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 y string.** or a
7210: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 string containi
7220: 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 ng only whitespa
7230: 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c ce and comments,
7240: 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 then no SQL.**
7250: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 statements are e
7260: 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 valuated and the
7270: 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
7280: 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
7290: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 The sqlite3_exec
72a0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
72b0: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 implemented in t
72c0: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 erms of.** [sqli
72d0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
72e0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ], [sqlite3_step
72f0: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
7300: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 3_finalize()]..*
7310: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 * The sqlite3_ex
7320: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 ec() routine doe
7330: 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 s nothing to the
7340: 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 database that c
7350: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a annot be done.**
7360: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 by [sqlite3_pre
7370: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c pare_v2()], [sql
7380: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e ite3_step()], an
7390: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c d [sqlite3_final
73a0: 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 ize()]..**.** Th
73b0: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
73c0: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 r to [sqlite3_ex
73d0: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e ec()] must be an
73e0: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a valid and open.
73f0: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
7400: 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 nection]..**.**
7410: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
7420: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 nection must not
7430: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 be closed while
7440: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 .** [sqlite3_exe
7450: 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e c()] is running.
7460: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 .**.** The calli
7470: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 ng function shou
7480: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f ld use [sqlite3_
7490: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a free()] to free.
74a0: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 ** the memory th
74b0: 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 at *errmsg is le
74c0: 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f ft pointing at o
74d0: 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a nce the error.**
74e0: 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c message is no l
74f0: 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a onger needed..**
7500: 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 .** The SQL stat
7510: 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 ement text in th
7520: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 e 2nd parameter
7530: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 to [sqlite3_exec
7540: 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 ()].** must rema
7550: 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 in unchanged whi
7560: 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 le [sqlite3_exec
7570: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ()] is running..
7580: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
7590: 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 ts:.** [H12101]
75a0: 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 [H12102] [H12104
75b0: 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 ] [H12105] [H121
75c0: 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 07] [H12110] [H1
75d0: 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2113] [H12116].*
75e0: 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 * [H12119] [H121
75f0: 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 22] [H12125] [H1
7600: 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 2131] [H12134] [
7610: 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d H12137] [H12138]
7620: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
7630: 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 nt sqlite3_exec(
7640: 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 . sqlite3*,
7650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7670: 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 An open databas
7680: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
7690: 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 r *sql,
76a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76b0: 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 /* SQL to be e
76c0: 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e valuated */. in
76d0: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f t (*callback)(vo
76e0: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 id*,int,char**,c
76f0: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c har**), /* Call
7700: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f back function */
7710: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 . void *,
7720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7740: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 1st argument to
7750: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 callback */. c
7760: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 har **errmsg
7770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7780: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
7790: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 or msg written h
77a0: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ere */.);../*.**
77b0: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c CAPI3REF: Resul
77c0: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d t Codes {H10210}
77d0: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 <S10700>.** KEY
77e0: 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b WORDS: SQLITE_OK
77f0: 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 {error code} {e
7800: 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b rror codes}.** K
7810: 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 EYWORDS: {result
7820: 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 code} {result c
7830: 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 odes}.**.** Many
7840: 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e SQLite function
7850: 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 s return an inte
7860: 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 ger result code
7870: 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f from the set sho
7880: 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 wn.** here in or
7890: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 der to indicates
78a0: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c success or fail
78b0: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 ure..**.** New e
78c0: 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 rror codes may b
78d0: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
78e0: 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
78f0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 Lite..**.** See
7900: 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f also: [SQLITE_IO
7910: 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e ERR_READ | exten
7920: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
7930: 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ].*/.#define SQL
7940: 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 ITE_OK
7950: 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 0 /* Successf
7960: 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 ul result */./*
7970: 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 beginning-of-err
7980: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 or-codes */.#def
7990: 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 ine SQLITE_ERROR
79a0: 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 1 /* S
79b0: 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 QL error or miss
79c0: 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ing database */.
79d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
79e0: 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 NTERNAL 2
79f0: 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 /* Internal logi
7a00: 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 c error in SQLit
7a10: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
7a20: 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 ITE_PERM
7a30: 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 3 /* Access p
7a40: 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 ermission denied
7a50: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7a60: 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 TE_ABORT
7a70: 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 4 /* Callback
7a80: 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 routine requeste
7a90: 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 d an abort */.#d
7aa0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 efine SQLITE_BUS
7ab0: 59 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a Y 5 /*
7ac0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
7ad0: 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a le is locked */.
7ae0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
7af0: 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 OCKED 6
7b00: 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 /* A table in th
7b10: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f e database is lo
7b20: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cked */.#define
7b30: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 SQLITE_NOMEM
7b40: 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 7 /* A mal
7b50: 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a loc() failed */.
7b60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
7b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 EADONLY 8
7b80: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 /* Attempt to wr
7b90: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 ite a readonly d
7ba0: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 atabase */.#defi
7bb0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 ne SQLITE_INTERR
7bc0: 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 UPT 9 /* Op
7bd0: 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 eration terminat
7be0: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e ed by sqlite3_in
7bf0: 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 terrupt()*/.#def
7c00: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
7c10: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 10 /* S
7c20: 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b ome kind of disk
7c30: 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
7c40: 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 red */.#define S
7c50: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 QLITE_CORRUPT
7c60: 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 11 /* The da
7c70: 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 tabase disk imag
7c80: 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a e is malformed *
7c90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7ca0: 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 _NOTFOUND 12
7cb0: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 /* NOT USED. T
7cc0: 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e able or record n
7cd0: 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 ot found */.#def
7ce0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 ine SQLITE_FULL
7cf0: 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 13 /* I
7d00: 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 nsertion failed
7d10: 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65 because database
7d20: 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 is full */.#def
7d30: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f ine SQLITE_CANTO
7d40: 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 PEN 14 /* U
7d50: 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 nable to open th
7d60: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
7d70: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7d80: 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 E_PROTOCOL 15
7d90: 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 /* NOT USED.
7da0: 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 Database lock pr
7db0: 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a otocol error */.
7dc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 #define SQLITE_E
7dd0: 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20 MPTY 16
7de0: 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65 /* Database is e
7df0: 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mpty */.#define
7e00: 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 SQLITE_SCHEMA
7e10: 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 17 /* The d
7e20: 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 atabase schema c
7e30: 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e hanged */.#defin
7e40: 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 e SQLITE_TOOBIG
7e50: 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 18 /* Str
7e60: 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 ing or BLOB exce
7e70: 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a eds size limit *
7e80: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7e90: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 _CONSTRAINT 19
7ea0: 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 /* Abort due t
7eb0: 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f o constraint vio
7ec0: 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e lation */.#defin
7ed0: 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 e SQLITE_MISMATC
7ee0: 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 H 20 /* Dat
7ef0: 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 a type mismatch
7f00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7f10: 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 E_MISUSE 21
7f20: 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 /* Library us
7f30: 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a ed incorrectly *
7f40: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7f50: 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 _NOLFS 22
7f60: 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 /* Uses OS fea
7f70: 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 tures not suppor
7f80: 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 ted on host */.#
7f90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 define SQLITE_AU
7fa0: 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f TH 23 /
7fb0: 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 * Authorization
7fc0: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e denied */.#defin
7fd0: 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 e SQLITE_FORMAT
7fe0: 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 24 /* Aux
7ff0: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
8000: 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a format error */.
8010: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
8020: 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20 ANGE 25
8030: 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 /* 2nd parameter
8040: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 to sqlite3_bind
8050: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f out of range */
8060: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8070: 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20 NOTADB 26
8080: 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 /* File opened
8090: 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 that is not a da
80a0: 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 tabase file */.#
80b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f define SQLITE_RO
80c0: 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f W 100 /
80d0: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 * sqlite3_step()
80e0: 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 has another row
80f0: 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e ready */.#defin
8100: 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 e SQLITE_DONE
8110: 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 101 /* sql
8120: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 ite3_step() has
8130: 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 finished executi
8140: 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d ng */./* end-of-
8150: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a error-codes */..
8160: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
8170: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 Extended Result
8180: 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c Codes {H10220} <
8190: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10700>.** KEYWO
81a0: 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 RDS: {extended e
81b0: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 rror code} {exte
81c0: 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 nded error codes
81d0: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b }.** KEYWORDS: {
81e0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
81f0: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 code} {extended
8200: 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a result codes}.**
8210: 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 .** In its defau
8220: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e lt configuration
8230: 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 , SQLite API rou
8240: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 tines return one
8250: 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a of 26 integer.*
8260: 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 * [SQLITE_OK | r
8270: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 esult codes]. H
8280: 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e owever, experien
8290: 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 ce has shown tha
82a0: 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 t many of.** the
82b0: 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 se result codes
82c0: 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 are too coarse-g
82d0: 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f rained. They do
82e0: 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a not provide as.
82f0: 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 ** much informat
8300: 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 ion about proble
8310: 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 ms as programmer
8320: 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 s might like. I
8330: 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a n an effort to.*
8340: 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 * address this,
8350: 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f newer versions o
8360: 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f f SQLite (versio
8370: 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 n 3.3.8 and late
8380: 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 r) include.** su
8390: 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 pport for additi
83a0: 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 onal result code
83b0: 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d s that provide m
83c0: 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 ore detailed inf
83d0: 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 ormation.** abou
83e0: 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 t errors. The ex
83f0: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8400: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 des are enabled
8410: 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f or disabled.** o
8420: 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65 n a per database
8430: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 connection basi
8440: 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b s using the.** [
8450: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
8460: 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d _result_codes()]
8470: 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 API..**.** Some
8480: 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c of the availabl
8490: 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c e extended resul
84a0: 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 t codes are list
84b0: 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 ed here..** One
84c0: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e may expect the n
84d0: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 umber of extende
84e0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 d result codes w
84f0: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a ill be expand.**
8500: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 over time. Sof
8510: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20 tware that uses
8520: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
8530: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 codes should exp
8540: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 ect.** to see ne
8550: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 w result codes i
8560: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
8570: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
8580: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b ** The SQLITE_OK
8590: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c result code wil
85a0: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e l never be exten
85b0: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c ded. It will al
85c0: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 ways.** be exact
85d0: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 ly zero..*/.#def
85e0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
85f0: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 _READ
8600: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8610: 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 | (1<<8)).#defi
8620: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
8630: 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 SHORT_READ
8640: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
8650: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (2<<8)).#defin
8660: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 e SQLITE_IOERR_W
8670: 52 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20 RITE
8680: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8690: 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (3<<8)).#define
86a0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 SQLITE_IOERR_FS
86b0: 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 YNC
86c0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
86d0: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (4<<8)).#define
86e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
86f0: 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 _FSYNC (
8700: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
8710: 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 5<<8)).#define S
8720: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
8730: 43 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53 CATE (S
8740: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 QLITE_IOERR | (6
8750: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8760: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
8770: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
8780: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c LITE_IOERR | (7<
8790: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
87a0: 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
87b0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
87c0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c ITE_IOERR | (8<<
87d0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
87e0: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 TE_IOERR_RDLOCK
87f0: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
8800: 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 TE_IOERR | (9<<8
8810: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8820: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 E_IOERR_DELETE
8830: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8840: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 E_IOERR | (10<<8
8850: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8860: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 E_IOERR_BLOCKED
8870: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8880: 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 E_IOERR | (11<<8
8890: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
88a0: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 E_IOERR_NOMEM
88b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
88c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 E_IOERR | (12<<8
88d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
88e0: 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 E_IOERR_ACCESS
88f0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8900: 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 E_IOERR | (13<<8
8910: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8920: 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
8930: 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 ERVEDLOCK (SQLIT
8940: 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 E_IOERR | (14<<8
8950: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8960: 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 E_IOERR_LOCK
8970: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8980: 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 E_IOERR | (15<<8
8990: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
89a0: 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 E_IOERR_CLOSE
89b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
89c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 E_IOERR | (16<<8
89d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
89e0: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 E_IOERR_DIR_CLOS
89f0: 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 E (SQLIT
8a00: 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 E_IOERR | (17<<8
8a10: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8a20: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
8a30: 41 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 ACHE (SQLIT
8a40: 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 E_LOCKED | (1<<8
8a50: 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ) )../*.** CAPI3
8a60: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 REF: Flags For F
8a70: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 ile Open Operati
8a80: 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 ons {H10230} <H1
8a90: 31 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 1120> <H12700>.*
8aa0: 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 *.** These bit v
8ab0: 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 alues are intend
8ac0: 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 ed for use in th
8ad0: 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 e.** 3rd paramet
8ae0: 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 er to the [sqlit
8af0: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e e3_open_v2()] in
8b00: 74 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 terface and.** i
8b10: 6e 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 n the 4th parame
8b20: 74 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e ter to the xOpen
8b30: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a method of the.*
8b40: 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 * [sqlite3_vfs]
8b50: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 object..*/.#defi
8b60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ne SQLITE_OPEN_R
8b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 EADONLY
8b80: 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 0x00000001 /* O
8b90: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8ba0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8bb0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ne SQLITE_OPEN_R
8bc0: 45 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20 EADWRITE
8bd0: 30 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 0x00000002 /* O
8be0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8bf0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8c00: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 ne SQLITE_OPEN_C
8c10: 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20 REATE
8c20: 30 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 0x00000004 /* O
8c30: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8c40: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8c50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 ne SQLITE_OPEN_D
8c60: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 ELETEONCLOSE
8c70: 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 0x00000008 /* V
8c80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8c90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 ne SQLITE_OPEN_E
8ca0: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 XCLUSIVE
8cb0: 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 0x00000010 /* V
8cc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8cd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8ce0: 41 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 AIN_DB
8cf0: 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 0x00000100 /* V
8d00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8d10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8d20: 45 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 EMP_DB
8d30: 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 0x00000200 /* V
8d40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8d50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8d60: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 RANSIENT_DB
8d70: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 0x00000400 /* V
8d80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8d90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8da0: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 AIN_JOURNAL
8db0: 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 0x00000800 /* V
8dc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8dd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8de0: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 EMP_JOURNAL
8df0: 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 0x00001000 /* V
8e00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8e10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 ne SQLITE_OPEN_S
8e20: 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 UBJOURNAL
8e30: 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 0x00002000 /* V
8e40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8e50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8e60: 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 ASTER_JOURNAL
8e70: 30 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 0x00004000 /* V
8e80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8e90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e ne SQLITE_OPEN_N
8ea0: 4f 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20 OMUTEX
8eb0: 30 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 0x00008000 /* O
8ec0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8ed0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8ee0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 ne SQLITE_OPEN_F
8ef0: 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 ULLMUTEX
8f00: 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 0x00010000 /* O
8f10: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8f20: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8f30: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 ne SQLITE_OPEN_S
8f40: 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 HAREDCACHE
8f50: 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 0x00020000 /* O
8f60: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8f70: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8f80: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 ne SQLITE_OPEN_P
8f90: 52 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20 RIVATECACHE
8fa0: 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 0x00040000 /* O
8fb0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8fc0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a en_v2() */../*.*
8fd0: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 * CAPI3REF: Devi
8fe0: 63 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69 ce Characteristi
8ff0: 63 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 cs {H10240} <H11
9000: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 120>.**.** The x
9010: 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 DeviceCapabiliti
9020: 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 es method of the
9030: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
9040: 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 hods].** object
9050: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
9060: 65 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65 er which is a ve
9070: 63 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 ctor of the thes
9080: 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 e.** bit values
9090: 65 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 expressing I/O c
90a0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f haracteristics o
90b0: 66 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 f the mass stora
90c0: 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 ge.** device tha
90d0: 74 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 t holds the file
90e0: 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 that the [sqlit
90f0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a e3_io_methods].*
9100: 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a * refers to..**.
9110: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f ** The SQLITE_IO
9120: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 CAP_ATOMIC prope
9130: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 rty means that a
9140: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 ll writes of.**
9150: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f any size are ato
9160: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
9170: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e _IOCAP_ATOMICnnn
9180: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 values.** mean
9190: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 that writes of b
91a0: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e locks that are n
91b0: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 nn bytes in size
91c0: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 and.** are alig
91d0: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 ned to an addres
91e0: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e s which is an in
91f0: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f teger multiple o
9200: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f f.** nnn are ato
9210: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
9220: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
9230: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a ND value means.*
9240: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 * that when data
9250: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 is appended to
9260: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 a file, the data
9270: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 is appended.**
9280: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 first then the s
9290: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
92a0: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 is extended, nev
92b0: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 er the other.**
92c0: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 way around. The
92d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 SQLITE_IOCAP_SE
92e0: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 QUENTIAL propert
92f0: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 y means that.**
9300: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 information is w
9310: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 ritten to disk i
9320: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 n the same order
9330: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 as calls.** to
9340: 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 xWrite()..*/.#de
9350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9360: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 P_ATOMIC
9370: 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 0x00000001.#de
9380: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9390: 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 P_ATOMIC512
93a0: 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 0x00000002.#de
93b0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
93c0: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 P_ATOMIC1K
93d0: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 0x00000004.#de
93e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
93f0: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 P_ATOMIC2K
9400: 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 0x00000008.#de
9410: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9420: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 P_ATOMIC4K
9430: 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 0x00000010.#de
9440: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9450: 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 P_ATOMIC8K
9460: 20 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 0x00000020.#de
9470: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9480: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 P_ATOMIC16K
9490: 20 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 0x00000040.#de
94a0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
94b0: 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 P_ATOMIC32K
94c0: 20 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 0x00000080.#de
94d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
94e0: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 P_ATOMIC64K
94f0: 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 0x00000100.#de
9500: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9510: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 P_SAFE_APPEND
9520: 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 0x00000200.#de
9530: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9540: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 P_SEQUENTIAL
9550: 20 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0x00000400../*
9560: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 .** CAPI3REF: Fi
9570: 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c le Locking Level
9580: 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 s {H10250} <H111
9590: 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 20> <H11310>.**.
95a0: 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f ** SQLite uses o
95b0: 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
95c0: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 ger values as th
95d0: 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 e second.** argu
95e0: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 ment to calls it
95f0: 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c makes to the xL
9600: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 ock() and xUnloc
9610: 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f k() methods.** o
9620: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f f an [sqlite3_io
9630: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
9640: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
9650: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 ITE_LOCK_NONE
9660: 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
9670: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 SQLITE_LOCK_SHA
9680: 52 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 RED 1.#de
9690: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
96a0: 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32 _RESERVED 2
96b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
96c0: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 LOCK_PENDING
96d0: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
96e0: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 ITE_LOCK_EXCLUSI
96f0: 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 VE 4../*.**
9700: 43 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 CAPI3REF: Synchr
9710: 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 onization Type F
9720: 6c 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 lags {H10260} <H
9730: 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 11120>.**.** Whe
9740: 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 n SQLite invokes
9750: 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 the xSync() met
9760: 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 hod of an.** [sq
9770: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
9780: 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 ] object it uses
9790: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f a combination o
97a0: 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 f.** these integ
97b0: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 er values as the
97c0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
97d0: 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
97e0: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 SQLITE_SYNC_DAT
97f0: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 AONLY flag is us
9800: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 ed, it means tha
9810: 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 t the.** sync op
9820: 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 eration only nee
9830: 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 ds to flush data
9840: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 to mass storage
9850: 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f . Inode.** info
9860: 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 rmation need not
9870: 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 be flushed. If
9880: 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 the lower four b
9890: 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a its of the flag.
98a0: 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f ** equal SQLITE_
98b0: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 SYNC_NORMAL, tha
98c0: 74 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e t means to use n
98d0: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 ormal fsync() se
98e0: 6d 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 mantics..** If t
98f0: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 he lower four bi
9900: 74 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f ts equal SQLITE_
9910: 53 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 SYNC_FULL, that
9920: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 means.** to use
9930: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 Mac OS X style f
9940: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 ullsync instead
9950: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 of fsync()..*/.#
9960: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 define SQLITE_SY
9970: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 NC_NORMAL
9980: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 0x00002.#define
9990: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
99a0: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 L 0x000
99b0: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 03.#define SQLIT
99c0: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 E_SYNC_DATAONLY
99d0: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0x00010../*
99e0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 .** CAPI3REF: OS
99f0: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 Interface Open
9a00: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 File Handle {H11
9a10: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 110} <S20110>.**
9a20: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f .** An [sqlite3_
9a30: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 file] object rep
9a40: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
9a50: 66 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 file in the .**
9a60: 5b 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f [sqlite3_vfs | O
9a70: 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 S interface laye
9a80: 72 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 r]. Individual
9a90: 4f 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 OS interface.**
9aa0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
9ab0: 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 will.** want to
9ac0: 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 subclass this ob
9ad0: 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e ject by appendin
9ae0: 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 g additional fie
9af0: 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 lds.** for their
9b00: 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 own use. The p
9b10: 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 Methods entry is
9b20: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
9b30: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f .** [sqlite3_io_
9b40: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
9b50: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 that defines met
9b60: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d hods for perform
9b70: 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 ing.** I/O opera
9b80: 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 tions on the ope
9b90: 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 n file..*/.typed
9ba0: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
9bb0: 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 3_file sqlite3_f
9bc0: 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 ile;.struct sqli
9bd0: 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e te3_file {. con
9be0: 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 st struct sqlite
9bf0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
9c00: 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 ethods; /* Meth
9c10: 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 ods for an open
9c20: 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a file */.};../*.*
9c30: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 * CAPI3REF: OS I
9c40: 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 nterface File Vi
9c50: 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 rtual Methods Ob
9c60: 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 ject {H11120} <S
9c70: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 20110>.**.** Eve
9c80: 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 ry file opened b
9c90: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 y the [sqlite3_v
9ca0: 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 fs] xOpen method
9cb0: 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a populates an.**
9cc0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
9cd0: 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 object (or, more
9ce0: 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 commonly, a sub
9cf0: 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 class of the.**
9d00: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
9d10: 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f bject) with a po
9d20: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
9d30: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
9d40: 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ect..** This obj
9d50: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 ect defines the
9d60: 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 methods used to
9d70: 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 perform various
9d80: 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 operations.** ag
9d90: 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 ainst the open f
9da0: 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 ile represented
9db0: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f by the [sqlite3_
9dc0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a file] object..**
9dd0: 0a 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e .** If the xOpen
9de0: 20 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 method sets the
9df0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d sqlite3_file.pM
9e00: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a ethods element .
9e10: 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c ** to a non-NULL
9e20: 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 pointer, then t
9e30: 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
9e40: 74 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 thods.xClose met
9e50: 68 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e hod.** may be in
9e60: 76 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 voked even if th
9e70: 65 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 e xOpen reported
9e80: 20 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e that it failed.
9e90: 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 The.** only wa
9ea0: 79 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 y to prevent a c
9eb0: 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f all to xClose fo
9ec0: 6c 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 llowing a failed
9ed0: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 xOpen.** is for
9ee0: 20 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 the xOpen to se
9ef0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 t the sqlite3_fi
9f00: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d le.pMethods elem
9f10: 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a ent to NULL..**.
9f20: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 ** The flags arg
9f30: 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d ument to xSync m
9f40: 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 ay be one of [SQ
9f50: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
9f60: 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ] or.** [SQLITE_
9f70: 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 SYNC_FULL]. The
9f80: 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 first choice is
9f90: 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e the normal fsyn
9fa0: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f c()..** The seco
9fb0: 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d nd choice is a M
9fc0: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 ac OS X style fu
9fd0: 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 llsync. The [SQ
9fe0: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
9ff0: 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 LY].** flag may
a000: 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e be ORed in to in
a010: 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 dicate that only
a020: 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 the data of the
a030: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 file.** and not
a040: 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 its inode needs
a050: 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a to be synced..*
a060: 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 *.** The integer
a070: 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b values to xLock
a080: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 () and xUnlock()
a090: 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c are one of.** <
a0a0: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c ul>.** <li> [SQL
a0b0: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a ITE_LOCK_NONE],.
a0c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a0d0: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a LOCK_SHARED],.**
a0e0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a0f0: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a CK_RESERVED],.**
a100: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a110: 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a CK_PENDING], or.
a120: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a130: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e LOCK_EXCLUSIVE].
a140: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f .** </ul>.** xLo
a150: 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 ck() increases t
a160: 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b he lock. xUnlock
a170: 28 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65 () decreases the
a180: 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 lock..** The xC
a190: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
a1a0: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 () method checks
a1b0: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 whether any dat
a1c0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
a1d0: 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 ,.** either in t
a1e0: 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 his process or i
a1f0: 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f n some other pro
a200: 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 cess, is holding
a210: 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 a RESERVED,.**
a220: 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c PENDING, or EXCL
a230: 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
a240: 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 e file. It retu
a250: 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 rns true.** if s
a260: 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 uch a lock exist
a270: 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 s and false othe
a280: 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rwise..**.** The
a290: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 xFileControl()
a2a0: 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 method is a gene
a2b0: 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 ric interface th
a2c0: 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d at allows custom
a2d0: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e .** VFS implemen
a2e0: 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 tations to direc
a2f0: 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f tly control an o
a300: 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 pen file using t
a310: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 he.** [sqlite3_f
a320: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 ile_control()] i
a330: 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 nterface. The s
a340: 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d econd "op" argum
a350: 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 ent is an.** int
a360: 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 eger opcode. Th
a370: 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
a380: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f is a generic po
a390: 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 inter intended t
a3a0: 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 o.** point to a
a3b0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d structure that m
a3c0: 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d ay contain argum
a3d0: 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e ents or space in
a3e0: 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 which to.** wri
a3f0: 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 te return values
a400: 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 . Potential use
a410: 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 s for xFileContr
a420: 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a ol() might be.**
a430: 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e functions to en
a440: 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f able blocking lo
a450: 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 cks with timeout
a460: 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 s, to change the
a470: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 .** locking stra
a480: 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c tegy (for exampl
a490: 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c e to use dot-fil
a4a0: 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 e locks), to inq
a4b0: 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 uire.** about th
a4c0: 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f e status of a lo
a4d0: 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 ck, or to break
a4e0: 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 stale locks. Th
a4f0: 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 e SQLite.** core
a500: 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 reserves all op
a510: 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 codes less than
a520: 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 100 for its own
a530: 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 use..** A [SQLIT
a540: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
a550: 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f E | list of opco
a560: 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 des] less than 1
a570: 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 00 is available.
a580: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 .** Applications
a590: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 that define a c
a5a0: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 ustom xFileContr
a5b0: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 ol method should
a5c0: 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 use opcodes.**
a5d0: 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 greater than 100
a5e0: 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 to avoid confli
a5f0: 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 cts..**.** The x
a600: 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 SectorSize() met
a610: 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 hod returns the
a620: 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 sector size of t
a630: 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 he.** device tha
a640: 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 t underlies the
a650: 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f file. The secto
a660: 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a r size is the.**
a670: 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 minimum write t
a680: 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f hat can be perfo
a690: 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 rmed without dis
a6a0: 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 turbing.** other
a6b0: 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 bytes in the fi
a6c0: 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 le. The xDevice
a6d0: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
a6e0: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 ).** method retu
a6f0: 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 rns a bit vector
a700: 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 describing beha
a710: 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 viors of the.**
a720: 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 underlying devic
a730: 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a e:.**.** <ul>.**
a740: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a750: 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c CAP_ATOMIC].** <
a760: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a770: 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 P_ATOMIC512].**
a780: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a790: 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 AP_ATOMIC1K].**
a7a0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a7b0: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 AP_ATOMIC2K].**
a7c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a7d0: 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 AP_ATOMIC4K].**
a7e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a7f0: 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 AP_ATOMIC8K].**
a800: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a810: 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a AP_ATOMIC16K].**
a820: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a830: 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a CAP_ATOMIC32K].*
a840: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
a850: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a OCAP_ATOMIC64K].
a860: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a870: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
a880: 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 D].** <li> [SQLI
a890: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
a8a0: 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a IAL].** </ul>.**
a8b0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 .** The SQLITE_I
a8c0: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 OCAP_ATOMIC prop
a8d0: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 erty means that
a8e0: 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a all writes of.**
a8f0: 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 any size are at
a900: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 omic. The SQLIT
a910: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e E_IOCAP_ATOMICnn
a920: 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e n values.** mean
a930: 20 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 that writes of
a940: 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 blocks that are
a950: 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a nnn bytes in siz
a960: 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 e and.** are ali
a970: 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 gned to an addre
a980: 73 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 ss which is an i
a990: 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 nteger multiple
a9a0: 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 of.** nnn are at
a9b0: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 omic. The SQLIT
a9c0: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
a9d0: 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a END value means.
a9e0: 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 ** that when dat
a9f0: 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f a is appended to
aa00: 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 a file, the dat
aa10: 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a a is appended.**
aa20: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 first then the
aa30: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
aa40: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 is extended, ne
aa50: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a ver the other.**
aa60: 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 way around. Th
aa70: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
aa80: 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 EQUENTIAL proper
aa90: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a ty means that.**
aaa0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
aab0: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 written to disk
aac0: 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 in the same orde
aad0: 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f r as calls.** to
aae0: 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a xWrite()..**.**
aaf0: 20 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75 If xRead() retu
ab00: 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 rns SQLITE_IOERR
ab10: 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d _SHORT_READ it m
ab20: 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a ust also fill.**
ab30: 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 in the unread p
ab40: 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 ortions of the b
ab50: 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 uffer with zeros
ab60: 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a . A VFS that.**
ab70: 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 fails to zero-f
ab80: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 ill short reads
ab90: 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f might seem to wo
aba0: 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a rk. However,.**
abb0: 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f failure to zero
abc0: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 -fill short read
abd0: 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c s will eventuall
abe0: 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 y lead to.** dat
abf0: 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
ac00: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
ac10: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d uct sqlite3_io_m
ac20: 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 ethods sqlite3_i
ac30: 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 o_methods;.struc
ac40: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
ac50: 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 hods {. int iVe
ac60: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 rsion;. int (*x
ac70: 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 Close)(sqlite3_f
ac80: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ile*);. int (*x
ac90: 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 Read)(sqlite3_fi
aca0: 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 le*, void*, int
acb0: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e iAmt, sqlite3_in
acc0: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e t64 iOfst);. in
acd0: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 t (*xWrite)(sqli
ace0: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 te3_file*, const
acf0: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 void*, int iAmt
ad00: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
ad10: 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a iOfst);. int (*
ad20: 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 xTruncate)(sqlit
ad30: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 e3_file*, sqlite
ad40: 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 3_int64 size);.
ad50: 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 int (*xSync)(sq
ad60: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
ad70: 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 flags);. int (
ad80: 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 *xFileSize)(sqli
ad90: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 te3_file*, sqlit
ada0: 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 e3_int64 *pSize)
adb0: 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 ;. int (*xLock)
adc0: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
add0: 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 int);. int (*xU
ade0: 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 nlock)(sqlite3_f
adf0: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e ile*, int);. in
ae00: 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 t (*xCheckReserv
ae10: 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f edLock)(sqlite3_
ae20: 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 file*, int *pRes
ae30: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 Out);. int (*xF
ae40: 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 ileControl)(sqli
ae50: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f te3_file*, int o
ae60: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a p, void *pArg);.
ae70: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 int (*xSectorS
ae80: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ize)(sqlite3_fil
ae90: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 e*);. int (*xDe
aea0: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
aeb0: 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ics)(sqlite3_fil
aec0: 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 e*);. /* Additi
aed0: 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 onal methods may
aee0: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
aef0: 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a ure releases */.
af00: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 };../*.** CAPI3R
af10: 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c EF: Standard Fil
af20: 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 e Control Opcode
af30: 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 s {H11310} <S308
af40: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
af50: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
af60: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f s are opcodes fo
af70: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 r the xFileContr
af80: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 ol method.** of
af90: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
afa0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
afb0: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c and for the [sql
afc0: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
afd0: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 l()].** interfac
afe0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 e..**.** The [SQ
aff0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
b000: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 TATE] opcode is
b010: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 used for debuggi
b020: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 ng. This.** opc
b030: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78 ode causes the x
b040: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
b050: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 od to write the
b060: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
b070: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e .** the lock (on
b080: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 e of [SQLITE_LOC
b090: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 K_NONE], [SQLITE
b0a0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a _LOCK_SHARED],.*
b0b0: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 * [SQLITE_LOCK_R
b0c0: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 ESERVED], [SQLIT
b0d0: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c E_LOCK_PENDING],
b0e0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b or [SQLITE_LOCK
b0f0: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 _EXCLUSIVE]).**
b100: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 into an integer
b110: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72 that the pArg ar
b120: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f gument points to
b130: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 . This capabilit
b140: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 y.** is used dur
b150: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 ing testing and
b160: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 only needs to be
b170: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 supported when
b180: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 SQLITE_TEST.** i
b190: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 s defined..*/.#d
b1a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e efine SQLITE_FCN
b1b0: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 TL_LOCKSTATE
b1c0: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
b1d0: 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f LITE_GET_LOCKPRO
b1e0: 58 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 XYFILE 2.#d
b1f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 efine SQLITE_SET
b200: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 _LOCKPROXYFILE
b210: 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
b220: 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 LITE_LAST_ERRNO
b230: 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 4../
b240: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d *.** CAPI3REF: M
b250: 75 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 utex Handle {H17
b260: 31 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 110} <S20130>.**
b270: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f .** The mutex mo
b280: 64 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 dule within SQLi
b290: 74 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 te defines [sqli
b2a0: 74 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 te3_mutex] to be
b2b0: 20 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 an.** abstract
b2c0: 74 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 type for a mutex
b2d0: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 object. The SQ
b2e0: 4c 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 Lite core never
b2f0: 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 looks.** at the
b300: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 internal represe
b310: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 ntation of an [s
b320: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 qlite3_mutex].
b330: 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 It only.** deals
b340: 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
b350: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d o the [sqlite3_m
b360: 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a utex] object..**
b370: 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 .** Mutexes are
b380: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 created using [s
b390: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
b3a0: 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 oc()]..*/.typede
b3b0: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
b3c0: 5f 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d _mutex sqlite3_m
b3d0: 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 utex;../*.** CAP
b3e0: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 I3REF: OS Interf
b3f0: 61 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 ace Object {H111
b400: 34 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 40} <S20100>.**.
b410: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
b420: 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 f the sqlite3_vf
b430: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 s object defines
b440: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 the interface b
b450: 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 etween.** the SQ
b460: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 Lite core and th
b470: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
b480: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
b490: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 The "vfs".** in
b4a0: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
b4b0: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f object stands fo
b4c0: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 r "virtual file
b4d0: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 system"..**.** T
b4e0: 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
b4f0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 iVersion field i
b500: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 s initially 1 bu
b510: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 t may be larger
b520: 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 in.** future ver
b530: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e sions of SQLite.
b540: 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 Additional fie
b550: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e lds may be appen
b560: 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f ded to this.** o
b570: 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 bject when the i
b580: 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 Version value is
b590: 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 increased. Not
b5a0: 65 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63 e that the struc
b5b0: 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 ture.** of the s
b5c0: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
b5d0: 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 t changes in the
b5e0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 transaction bet
b5f0: 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 ween.** SQLite v
b600: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 ersion 3.5.9 and
b610: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 3.6.0 and yet t
b620: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c he iVersion fiel
b630: 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 d was not.** mod
b640: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ified..**.** The
b650: 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 szOsFile field
b660: 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
b670: 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 he subclassed [s
b680: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 qlite3_file].**
b690: 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62 structure used b
b6a0: 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 y this VFS. mxP
b6b0: 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d athname is the m
b6c0: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
b6d0: 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 .** a pathname i
b6e0: 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a n this VFS..**.*
b6f0: 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c * Registered sql
b700: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 ite3_vfs objects
b710: 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c are kept on a l
b720: 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 inked list forme
b730: 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 d by.** the pNex
b740: 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 t pointer. The
b750: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 [sqlite3_vfs_reg
b760: 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 ister()].** and
b770: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 [sqlite3_vfs_unr
b780: 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 egister()] inter
b790: 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 faces manage thi
b7a0: 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 s list.** in a t
b7b0: 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 hread-safe way.
b7c0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 The [sqlite3_vf
b7d0: 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 s_find()] interf
b7e0: 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 ace.** searches
b7f0: 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 the list. Neith
b800: 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 er the applicati
b810: 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 on code nor the
b820: 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 VFS.** implement
b830: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 ation should use
b840: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 the pNext point
b850: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e er..**.** The pN
b860: 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 ext field is the
b870: 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 only field in t
b880: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a he sqlite3_vfs.*
b890: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 * structure that
b8a0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 SQLite will eve
b8b0: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 r modify. SQLit
b8c0: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 e will only acce
b8d0: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 ss.** or modify
b8e0: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 this field while
b8f0: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 holding a parti
b900: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 cular static mut
b910: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 ex..** The appli
b920: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 cation should ne
b930: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 ver modify anyth
b940: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 ing within the s
b950: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 qlite3_vfs.** ob
b960: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 ject once the ob
b970: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 ject has been re
b980: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 gistered..**.**
b990: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 The zName field
b9a0: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f holds the name o
b9b0: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 f the VFS module
b9c0: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 . The name must
b9d0: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 .** be unique ac
b9e0: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 ross all VFS mod
b9f0: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ules..**.** SQLi
ba00: 74 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 te will guarante
ba10: 65 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 e that the zFile
ba20: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 name parameter t
ba30: 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 o xOpen.** is ei
ba40: 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e ther a NULL poin
ba50: 74 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 ter or string ob
ba60: 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 tained.** from x
ba70: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 FullPathname().
ba80: 20 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 SQLite further
ba90: 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a guarantees that.
baa0: 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 ** the string wi
bab0: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 ll be valid and
bac0: 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 unchanged until
bad0: 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 xClose() is.** c
bae0: 61 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f alled. Because o
baf0: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 f the previous s
bb00: 65 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 entence,.** the
bb10: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 [sqlite3_file] c
bb20: 61 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 an safely store
bb30: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
bb40: 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 .** filename if
bb50: 69 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 it needs to reme
bb60: 6d 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d mber the filenam
bb70: 65 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f e for some reaso
bb80: 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 n..** If the zFi
bb90: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 lename parameter
bba0: 20 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e is xOpen is a N
bbb0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e ULL pointer then
bbc0: 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 xOpen.** must i
bbd0: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 nvent its own te
bbe0: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 mporary name for
bbf0: 20 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e the file. When
bc00: 65 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 ever the .** xFi
bc10: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 lename parameter
bc20: 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c is NULL it will
bc30: 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 also be the cas
bc40: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c e that the.** fl
bc50: 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 ags parameter wi
bc60: 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 ll include [SQLI
bc70: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
bc80: 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 CLOSE]..**.** Th
bc90: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 e flags argument
bca0: 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c to xOpen() incl
bcb0: 75 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 udes all bits se
bcc0: 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 t in.** the flag
bcd0: 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 s argument to [s
bce0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
bcf0: 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 ]. Or if [sqlit
bd00: 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 e3_open()].** or
bd10: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 [sqlite3_open16
bd20: 28 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 ()] is used, the
bd30: 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 n flags includes
bd40: 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 at least.** [SQ
bd50: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
bd60: 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f ITE] | [SQLITE_O
bd70: 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a PEN_CREATE]. .**
bd80: 20 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e If xOpen() open
bd90: 73 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e s a file read-on
bda0: 6c 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 ly then it sets
bdb0: 2a 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a *pOutFlags to.**
bdc0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 include [SQLITE
bdd0: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e _OPEN_READONLY].
bde0: 20 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 Other bits in
bdf0: 2a 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 *pOutFlags may b
be00: 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c e set..**.** SQL
be10: 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 ite will also ad
be20: 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c d one of the fol
be30: 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 lowing flags to
be40: 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 the xOpen().** c
be50: 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f all, depending o
be60: 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 n the object bei
be70: 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a ng opened:.**.**
be80: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b <ul>.** <li> [
be90: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
bea0: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 _DB].** <li> [S
beb0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
bec0: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e JOURNAL].** <li>
bed0: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
bee0: 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 EMP_DB].** <li>
bef0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 [SQLITE_OPEN_TE
bf00: 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c MP_JOURNAL].** <
bf10: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 li> [SQLITE_OPE
bf20: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a N_TRANSIENT_DB].
bf30: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
bf40: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c _OPEN_SUBJOURNAL
bf50: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 ].** <li> [SQLI
bf60: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
bf70: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e OURNAL].** </ul>
bf80: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 .**.** The file
bf90: 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 I/O implementati
bfa0: 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f on can use the o
bfb0: 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 bject type flags
bfc0: 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 to.** change th
bfd0: 65 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 e way it deals w
bfe0: 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 ith files. For
bff0: 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c example, an appl
c000: 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 ication.** that
c010: 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 does not care ab
c020: 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 out crash recove
c030: 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d ry or rollback m
c040: 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 ight make.** the
c050: 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e open of a journ
c060: 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e al file a no-op.
c070: 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 Writes to this
c080: 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a journal would.*
c090: 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 * also be no-ops
c0a0: 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 , and any attemp
c0b0: 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f t to read the jo
c0c0: 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 urnal would retu
c0d0: 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 rn.** SQLITE_IOE
c0e0: 52 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c RR. Or the impl
c0f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 ementation might
c100: 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 recognize that
c110: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 a database.** fi
c120: 6c 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 le will be doing
c130: 20 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 page-aligned se
c140: 63 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 ctor reads and w
c150: 72 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f rites in a rando
c160: 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 m.** order and s
c170: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 et up its I/O su
c180: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e bsystem accordin
c190: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 gly..**.** SQLit
c1a0: 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 e might also add
c1b0: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
c1c0: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 owing flags to t
c1d0: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a he xOpen method:
c1e0: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
c1f0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e li> [SQLITE_OPEN
c200: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a _DELETEONCLOSE].
c210: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c220: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a OPEN_EXCLUSIVE].
c230: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
c240: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f he [SQLITE_OPEN_
c250: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 DELETEONCLOSE] f
c260: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 lag means the fi
c270: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 le should be.**
c280: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 deleted when it
c290: 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 is closed. The
c2a0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c [SQLITE_OPEN_DEL
c2b0: 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 ETEONCLOSE].** w
c2c0: 69 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 ill be set for T
c2d0: 45 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 EMP databases,
c2e0: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 journals and for
c2f0: 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a subjournals..**
c300: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
c310: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 OPEN_EXCLUSIVE]
c320: 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 flag is always u
c330: 73 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 sed in conjuncti
c340: 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b on.** with the [
c350: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
c360: 54 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 TE] flag, which
c370: 61 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c are both directl
c380: 79 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 y.** analogous t
c390: 6f 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 o the O_EXCL and
c3a0: 20 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f O_CREAT flags o
c3b0: 66 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e f the POSIX open
c3c0: 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 ().** API. The
c3d0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
c3e0: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e USIVE flag, when
c3f0: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65 paired with the
c400: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e .** SQLITE_OPEN
c410: 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 _CREATE, is used
c420: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
c430: 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c t file should al
c440: 77 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 ways.** be creat
c450: 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 ed, and that it
c460: 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 is an error if i
c470: 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
c480: 2e 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f ..** It is <i>no
c490: 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e t</i> used to in
c4a0: 64 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 dicate the file
c4b0: 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 should be opened
c4c0: 20 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 .** for exclusi
c4d0: 76 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a ve access..**.**
c4e0: 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 At least szOsFi
c4f0: 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f le bytes of memo
c500: 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 ry are allocated
c510: 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f by SQLite.** to
c520: 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 hold the [sqli
c530: 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 te3_file] struct
c540: 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 ure passed as th
c550: 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d e third.** argum
c560: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 ent to xOpen. T
c570: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 he xOpen method
c580: 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f does not have to
c590: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 .** allocate the
c5a0: 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 structure; it s
c5b0: 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 hould just fill
c5c0: 69 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 it in. Note tha
c5d0: 74 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d t.** the xOpen m
c5e0: 65 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 ethod must set t
c5f0: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e he sqlite3_file.
c600: 70 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 pMethods to eith
c610: 65 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 er.** a valid [s
c620: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
c630: 73 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 s] object or to
c640: 4e 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 NULL. xOpen mus
c650: 74 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 t do.** this eve
c660: 6e 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 n if the open fa
c670: 69 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 ils. SQLite exp
c680: 65 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71 ects that the sq
c690: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 lite3_file.pMeth
c6a0: 6f 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 ods.** element w
c6b0: 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 ill be valid aft
c6c0: 65 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 er xOpen returns
c6d0: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 regardless of t
c6e0: 68 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 he success.** or
c6f0: 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 failure of the
c700: 78 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a xOpen call..**.*
c710: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 * The flags argu
c720: 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 ment to xAccess(
c730: 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 ) may be [SQLITE
c740: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a _ACCESS_EXISTS].
c750: 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 ** to test for t
c760: 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 he existence of
c770: 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 a file, or [SQLI
c780: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
c790: 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 ITE] to.** test
c7a0: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 whether a file i
c7b0: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 s readable and w
c7c0: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c ritable, or [SQL
c7d0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d ITE_ACCESS_READ]
c7e0: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 .** to test whet
c7f0: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 her a file is at
c800: 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e least readable.
c810: 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 The file can
c820: 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 be a.** director
c830: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 y..**.** SQLite
c840: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f will always allo
c850: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 cate at least mx
c860: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 Pathname+1 bytes
c870: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 for the.** outp
c880: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 ut buffer xFullP
c890: 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 athname. The ex
c8a0: 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 act size of the
c8b0: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a output buffer.**
c8c0: 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 is also passed
c8d0: 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 as a parameter t
c8e0: 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e o both methods.
c8f0: 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 If the output b
c900: 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 uffer.** is not
c910: 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 large enough, [S
c920: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 QLITE_CANTOPEN]
c930: 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e should be return
c940: 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 ed. Since this i
c950: 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 s.** handled as
c960: 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 a fatal error by
c970: 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 SQLite, vfs imp
c980: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f lementations sho
c990: 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 uld endeavor.**
c9a0: 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 to prevent this
c9b0: 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 by setting mxPat
c9c0: 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 hname to a suffi
c9d0: 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 ciently large va
c9e0: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 lue..**.** The x
c9f0: 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 Randomness(), xS
ca00: 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 leep(), and xCur
ca10: 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 rentTime() inter
ca20: 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 faces.** are not
ca30: 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 strictly a part
ca40: 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 of the filesyst
ca50: 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 em, but they are
ca60: 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 .** included in
ca70: 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 the VFS structur
ca80: 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 e for completene
ca90: 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 ss..** The xRand
caa0: 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f omness() functio
cab0: 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 n attempts to re
cac0: 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 turn nBytes byte
cad0: 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 s.** of good-qua
cae0: 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 lity randomness
caf0: 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 into zOut. The
cb00: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a return value is.
cb10: 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 ** the actual nu
cb20: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
cb30: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 randomness obta
cb40: 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c ined..** The xSl
cb50: 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 eep() method cau
cb60: 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ses the calling
cb70: 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 thread to sleep
cb80: 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 for at.** least
cb90: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
cba0: 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e croseconds given
cbb0: 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 . The xCurrentT
cbc0: 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 ime().** method
cbd0: 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e returns a Julian
cbe0: 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 Day Number for
cbf0: 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 the current date
cc00: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f and time..**.*/
cc10: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
cc20: 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 sqlite3_vfs sqli
cc30: 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 te3_vfs;.struct
cc40: 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 sqlite3_vfs {.
cc50: 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 int iVersion;
cc60: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 /* Stru
cc70: 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 cture version nu
cc80: 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a mber */. int sz
cc90: 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 OsFile;
cca0: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 /* Size of su
ccb0: 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 bclassed sqlite3
ccc0: 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d _file */. int m
ccd0: 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 xPathname;
cce0: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 /* Maximum f
ccf0: 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e ile pathname len
cd00: 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 gth */. sqlite3
cd10: 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 _vfs *pNext;
cd20: 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 /* Next regist
cd30: 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f ered VFS */. co
cd40: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b nst char *zName;
cd50: 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
cd60: 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 f this virtual f
cd70: 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 ile system */.
cd80: 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 void *pAppData;
cd90: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
cda0: 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 ter to applicati
cdb0: 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 on-specific data
cdc0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 */. int (*xOpe
cdd0: 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c n)(sqlite3_vfs*,
cde0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
cdf0: 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 me, sqlite3_file
ce00: 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 *,.
ce10: 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 int flags, int
ce20: 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 *pOutFlags);.
ce30: 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 int (*xDelete)(s
ce40: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
ce50: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
ce60: 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 int syncDir);.
ce70: 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 int (*xAccess)(s
ce80: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
ce90: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
cea0: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a int flags, int *
ceb0: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 pResOut);. int
cec0: 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 (*xFullPathname)
ced0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 (sqlite3_vfs*, c
cee0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
cef0: 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 , int nOut, char
cf00: 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 *zOut);. void
cf10: 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 *(*xDlOpen)(sqli
cf20: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 te3_vfs*, const
cf30: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
cf40: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 ;. void (*xDlEr
cf50: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ror)(sqlite3_vfs
cf60: 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 *, int nByte, ch
cf70: 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 ar *zErrMsg);.
cf80: 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 void (*(*xDlSym)
cf90: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f (sqlite3_vfs*,vo
cfa0: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 id*, const char
cfb0: 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 *zSymbol))(void)
cfc0: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c ;. void (*xDlCl
cfd0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ose)(sqlite3_vfs
cfe0: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 *, void*);. int
cff0: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 (*xRandomness)(
d000: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
d010: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a t nByte, char *z
d020: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 Out);. int (*xS
d030: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 leep)(sqlite3_vf
d040: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 s*, int microsec
d050: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 onds);. int (*x
d060: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c CurrentTime)(sql
d070: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c ite3_vfs*, doubl
d080: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 e*);. int (*xGe
d090: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 tLastError)(sqli
d0a0: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 te3_vfs*, int, c
d0b0: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 har *);. /* New
d0c0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 fields may be a
d0d0: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 ppended in figur
d0e0: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 e versions. The
d0f0: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 iVersion. ** v
d100: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d alue will increm
d110: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 ent whenever thi
d120: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b s happens. */.};
d130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
d140: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 : Flags for the
d150: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 xAccess VFS meth
d160: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 od {H11190} <H11
d170: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 140>.**.** These
d180: 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
d190: 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 ts can be used a
d1a0: 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 s the third para
d1b0: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 meter to.** the
d1c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f xAccess method o
d1d0: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 f an [sqlite3_vf
d1e0: 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d s] object. {END}
d1f0: 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 They determine
d200: 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 .** what kind of
d210: 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 permissions the
d220: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 xAccess method
d230: 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a is looking for..
d240: 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 ** With SQLITE_A
d250: 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 CCESS_EXISTS, th
d260: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 e xAccess method
d270: 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b .** simply check
d280: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 s whether the fi
d290: 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 le exists..** Wi
d2a0: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 th SQLITE_ACCESS
d2b0: 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 _READWRITE, the
d2c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a xAccess method.*
d2d0: 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 * checks whether
d2e0: 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 the file is bot
d2f0: 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 h readable and w
d300: 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 ritable..** With
d310: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
d320: 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 EAD, the xAccess
d330: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b method.** check
d340: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 s whether the fi
d350: 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a le is readable..
d360: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
d370: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 E_ACCESS_EXISTS
d380: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
d390: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 ITE_ACCESS_READW
d3a0: 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 RITE 1.#define S
d3b0: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
d3c0: 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 D 2../*.**
d3d0: 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 CAPI3REF: Initia
d3e0: 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 lize The SQLite
d3f0: 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d Library {H10130}
d400: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 <S20000><S30100
d410: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
d420: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d430: 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c routine initial
d440: 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 izes the.** SQLi
d450: 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 te library. The
d460: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d470: 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 n() routine.** d
d480: 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 eallocates any r
d490: 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 esources that we
d4a0: 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 re allocated by
d4b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d4c0: 7a 65 28 29 2e 0a 2a 2a 20 54 68 65 73 65 20 72 ze()..** These r
d4d0: 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 73 69 outines are desi
d4e0: 67 6e 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70 gned to aid in p
d4f0: 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a rocess initializ
d500: 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 ation and.** shu
d510: 74 64 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65 tdown on embedde
d520: 64 20 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b d systems. Work
d530: 73 74 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74 station applicat
d540: 69 6f 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51 ions using.** SQ
d550: 4c 69 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f Lite normally do
d560: 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76 not need to inv
d570: 6f 6b 65 20 65 69 74 68 65 72 20 6f 66 20 74 68 oke either of th
d580: 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ese routines..**
d590: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 .** A call to sq
d5a0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
d5b0: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 () is an "effect
d5c0: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 ive" call if it
d5d0: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 is.** the first
d5e0: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
d5f0: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
d600: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 oked during the
d610: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
d620: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 he process, or i
d630: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
d640: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 t time sqlite3_i
d650: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
d660: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 nvoked.** follow
d670: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
d680: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
d690: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 . Only an effec
d6a0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 tive call.** of
d6b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d6c0: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e ze() does any in
d6d0: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 itialization. A
d6e0: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a ll other calls.*
d6f0: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e * are harmless n
d700: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 o-ops..**.** A c
d710: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 all to sqlite3_s
d720: 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20 hutdown() is an
d730: 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c "effective" call
d740: 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69 if it is the fi
d750: 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 rst.** call to s
d760: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d770: 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74 ) since the last
d780: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d790: 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 ize(). Only.**
d7a0: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c an effective cal
d7b0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 l to sqlite3_shu
d7c0: 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79 tdown() does any
d7d0: 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f deinitializatio
d7e0: 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 n..** All other
d7f0: 76 61 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73 valid calls to s
d800: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d810: 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e ) are harmless n
d820: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 o-ops..**.** The
d830: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d840: 69 7a 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 ize() interface
d850: 69 73 20 74 68 72 65 61 64 73 61 66 65 2c 20 62 is threadsafe, b
d860: 75 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 ut sqlite3_shutd
d870: 6f 77 6e 28 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e own().** is not.
d880: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68 The sqlite3_sh
d890: 75 74 64 6f 77 6e 28 29 20 69 6e 74 65 72 66 61 utdown() interfa
d8a0: 63 65 20 6d 75 73 74 20 6f 6e 6c 79 20 62 65 20 ce must only be
d8b0: 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 0a 2a 2a called from a.**
d8c0: 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 single thread.
d8d0: 20 41 6c 6c 20 6f 70 65 6e 20 5b 64 61 74 61 62 All open [datab
d8e0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d ase connections]
d8f0: 20 6d 75 73 74 20 62 65 20 63 6c 6f 73 65 64 20 must be closed
d900: 61 6e 64 20 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72 and all.** other
d910: 20 53 51 4c 69 74 65 20 72 65 73 6f 75 72 63 65 SQLite resource
d920: 73 20 6d 75 73 74 20 62 65 20 64 65 61 6c 6c 6f s must be deallo
d930: 63 61 74 65 64 20 70 72 69 6f 72 20 74 6f 20 69 cated prior to i
d940: 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 nvoking.** sqlit
d950: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 0a 2a e3_shutdown()..*
d960: 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 72 *.** Among other
d970: 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 33 things, sqlite3
d980: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 77 69 _initialize() wi
d990: 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c ll invoke.** sql
d9a0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 ite3_os_init().
d9b0: 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 71 6c 69 Similarly, sqli
d9c0: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a te3_shutdown().*
d9d0: 2a 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 * will invoke sq
d9e0: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a lite3_os_end()..
d9f0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
da00: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 3_initialize() r
da10: 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b outine returns [
da20: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 SQLITE_OK] on su
da30: 63 63 65 73 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 ccess..** If for
da40: 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c 20 73 71 some reason, sq
da50: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
da60: 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 () is unable to
da70: 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 initialize.** th
da80: 65 20 6c 69 62 72 61 72 79 20 28 70 65 72 68 61 e library (perha
da90: 70 73 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 ps it is unable
daa0: 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
dab0: 65 64 65 64 20 72 65 73 6f 75 72 63 65 20 73 75 eded resource su
dac0: 63 68 0a 2a 2a 20 61 73 20 61 20 6d 75 74 65 78 ch.** as a mutex
dad0: 29 20 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 ) it returns an
dae0: 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68 [error code] oth
daf0: 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f er than [SQLITE_
db00: 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 OK]..**.** The s
db10: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
db20: 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 e() routine is c
db30: 61 6c 6c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 alled internally
db40: 20 62 79 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a by many other.*
db50: 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 * SQLite interfa
db60: 63 65 73 20 73 6f 20 74 68 61 74 20 61 6e 20 61 ces so that an a
db70: 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 75 61 6c pplication usual
db80: 6c 79 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 ly does not need
db90: 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 to.** invoke sq
dba0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
dbb0: 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 46 6f () directly. Fo
dbc0: 72 20 65 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 r example, [sqli
dbd0: 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 te3_open()].** c
dbe0: 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 alls sqlite3_ini
dbf0: 74 69 61 6c 69 7a 65 28 29 20 73 6f 20 74 68 65 tialize() so the
dc00: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 SQLite library
dc10: 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
dc20: 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c cally.** initial
dc30: 69 7a 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 ized when [sqlit
dc40: 65 33 5f 6f 70 65 6e 28 29 5d 20 69 73 20 63 61 e3_open()] is ca
dc50: 6c 6c 65 64 20 69 66 20 69 74 20 68 61 73 20 6e lled if it has n
dc60: 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 ot be initialize
dc70: 64 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 20 48 d.** already. H
dc80: 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 owever, if SQLit
dc90: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 e is compiled wi
dca0: 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f th the [SQLITE_O
dcb0: 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a MIT_AUTOINIT].**
dcc0: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 compile-time op
dcd0: 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 tion, then the a
dce0: 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 utomatic calls t
dcf0: 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 o sqlite3_initia
dd00: 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d lize().** are om
dd10: 69 74 74 65 64 20 61 6e 64 20 74 68 65 20 61 70 itted and the ap
dd20: 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63 plication must c
dd30: 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 all sqlite3_init
dd40: 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c ialize() directl
dd50: 79 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73 y.** prior to us
dd60: 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 ing any other SQ
dd70: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 Lite interface.
dd80: 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72 For maximum por
dd90: 74 61 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 tability,.** it
dda0: 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 is recommended t
ddb0: 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 hat applications
ddc0: 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73 always invoke s
ddd0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
dde0: 65 28 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 e().** directly
ddf0: 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 prior to using a
de00: 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 ny other SQLite
de10: 69 6e 74 65 72 66 61 63 65 2e 20 20 46 75 74 75 interface. Futu
de20: 72 65 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f re releases.** o
de30: 66 20 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71 f SQLite may req
de40: 75 69 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f uire this. In o
de50: 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
de60: 62 65 68 61 76 69 6f 72 20 65 78 68 69 62 69 74 behavior exhibit
de70: 65 64 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 ed.** when SQLit
de80: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 e is compiled wi
de90: 74 68 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f th [SQLITE_OMIT_
dea0: 41 55 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20 AUTOINIT] might
deb0: 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65 become the.** de
dec0: 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 fault behavior i
ded0: 6e 20 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65 n some future re
dee0: 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e lease of SQLite.
def0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
df00: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75 e3_os_init() rou
df10: 74 69 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74 tine does operat
df20: 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 ing-system speci
df30: 66 69 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a fic.** initializ
df40: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c ation of the SQL
df50: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 ite library. Th
df60: 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 e sqlite3_os_end
df70: 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e ().** routine un
df80: 64 6f 65 73 20 74 68 65 20 65 66 66 65 63 74 20 does the effect
df90: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e of sqlite3_os_in
dfa0: 69 74 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74 it(). Typical t
dfb0: 61 73 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 asks.** performe
dfc0: 64 20 62 79 20 74 68 65 73 65 20 72 6f 75 74 69 d by these routi
dfd0: 6e 65 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f nes include allo
dfe0: 63 61 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f cation or deallo
dff0: 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 cation.** of sta
e000: 74 69 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69 tic resources, i
e010: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
e020: 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
e030: 73 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 s,.** setting up
e040: 20 61 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 a default [sqli
e050: 74 65 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c te3_vfs] module,
e060: 20 6f 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a or setting up.*
e070: 2a 20 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 * a default conf
e080: 69 67 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 iguration using
e090: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
e0a0: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 )]..**.** The ap
e0b0: 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 plication should
e0c0: 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69 never invoke ei
e0d0: 74 68 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f ther sqlite3_os_
e0e0: 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c init().** or sql
e0f0: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69 ite3_os_end() di
e100: 72 65 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70 rectly. The app
e110: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 lication should
e120: 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 only invoke.** s
e130: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
e140: 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f e() and sqlite3_
e150: 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65 shutdown(). The
e160: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
e170: 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ().** interface
e180: 69 73 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 is called automa
e190: 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 tically by sqlit
e1a0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
e1b0: 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f and.** sqlite3_o
e1c0: 73 5f 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65 s_end() is calle
e1d0: 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75 d by sqlite3_shu
e1e0: 74 64 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70 tdown(). Approp
e1f0: 72 69 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 riate.** impleme
e200: 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c ntations for sql
e210: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 ite3_os_init() a
e220: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nd sqlite3_os_en
e230: 64 28 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 d().** are built
e240: 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65 into SQLite whe
e250: 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 n it is compiled
e260: 20 66 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f for Unix, Windo
e270: 77 73 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 ws, or OS/2..**
e280: 57 68 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69 When [custom bui
e290: 6c 64 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20 lds | built for
e2a0: 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d other platforms]
e2b0: 0a 2a 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b .** (using the [
e2c0: 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d SQLITE_OS_OTHER=
e2d0: 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 1] compile-time.
e2e0: 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 ** option) the a
e2f0: 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 pplication must
e300: 73 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c supply a suitabl
e310: 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
e320: 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f for.** sqlite3_
e330: 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 os_init() and sq
e340: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 lite3_os_end().
e350: 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d An application-
e360: 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c supplied.** impl
e370: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
e380: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 lite3_os_init()
e390: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e or sqlite3_os_en
e3a0: 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 d().** must retu
e3b0: 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f rn [SQLITE_OK] o
e3c0: 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f n success and so
e3d0: 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 me other [error
e3e0: 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 code] upon.** fa
e3f0: 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ilure..*/.SQLITE
e400: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
e410: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 _initialize(void
e420: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
e430: 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f t sqlite3_shutdo
e440: 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 wn(void);.SQLITE
e450: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
e460: 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a _os_init(void);.
e470: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
e480: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f qlite3_os_end(vo
e490: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 id);../*.** CAPI
e4a0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 3REF: Configurin
e4b0: 67 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 g The SQLite Lib
e4c0: 72 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 rary {H14100} <S
e4d0: 32 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 20000><S30200>.*
e4e0: 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
e4f0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
e500: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 _config() interf
e510: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d ace is used to m
e520: 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 ake global confi
e530: 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e guration.** chan
e540: 67 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e ges to SQLite in
e550: 20 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 order to tune S
e560: 51 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 QLite to the spe
e570: 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a cific needs of.*
e580: 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
e590: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 n. The default
e5a0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 configuration is
e5b0: 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 recommended for
e5c0: 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 most.** applica
e5d0: 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 tions and so thi
e5e0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 s routine is usu
e5f0: 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 ally not necessa
e600: 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 ry. It is.** pr
e610: 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 ovided to suppor
e620: 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 t rare applicati
e630: 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c ons with unusual
e640: 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 needs..**.** Th
e650: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 e sqlite3_config
e660: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
e670: 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 not threadsafe.
e680: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
e690: 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 .** must insure
e6a0: 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 that no other SQ
e6b0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
e6c0: 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f are invoked by o
e6d0: 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 ther.** threads
e6e0: 77 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f while sqlite3_co
e6f0: 6e 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e nfig() is runnin
e700: 67 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c g. Furthermore,
e710: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
e720: 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 ).** may only be
e730: 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 invoked prior t
e740: 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 o library initia
e750: 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a lization using.*
e760: 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 * [sqlite3_initi
e770: 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 alize()] or afte
e780: 72 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 r shutdown by [s
e790: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
e7a0: 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 )]..** Note, how
e7b0: 65 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 ever, that sqlit
e7c0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 e3_config() can
e7d0: 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 be called as par
e7e0: 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c t of the.** impl
e7f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e ementation of an
e800: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
e810: 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 ined [sqlite3_os
e820: 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _init()]..**.**
e830: 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
e840: 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f nt to sqlite3_co
e850: 6e 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 nfig() is an int
e860: 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f eger.** [SQLITE_
e870: 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 CONFIG_SINGLETHR
e880: 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 EAD | configurat
e890: 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 ion option] that
e8a0: 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 determines.** w
e8b0: 68 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 hat property of
e8c0: 53 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 SQLite is to be
e8d0: 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 configured. Sub
e8e0: 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 sequent argument
e8f0: 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 s.** vary depend
e900: 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 ing on the [SQLI
e910: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
e920: 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 THREAD | configu
e930: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a ration option].*
e940: 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 * in the first a
e950: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 rgument..**.** W
e960: 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 hen a configurat
e970: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 ion option is se
e980: 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 t, sqlite3_confi
e990: 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c g() returns [SQL
e9a0: 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 ITE_OK]..** If t
e9b0: 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b he option is unk
e9c0: 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 nown or SQLite i
e9d0: 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 s unable to set
e9e0: 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 the option.** th
e9f0: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
ea00: 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 returns a non-ze
ea10: 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e ro [error code].
ea20: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
ea30: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d nts:.** [H14103]
ea40: 20 5b 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 [H14106] [H1412
ea50: 30 5d 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 0] [H14123] [H14
ea60: 31 32 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 126] [H14129] [H
ea70: 31 34 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 14132] [H14135].
ea80: 2a 2a 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 ** [H14138] [H14
ea90: 31 34 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 141] [H14144] [H
eaa0: 31 34 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 14147] [H14150]
eab0: 5b 48 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 [H14153] [H14156
eac0: 5d 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 ] [H14159].** [H
ead0: 31 34 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 14162] [H14165]
eae0: 5b 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 [H14168].*/.SQLI
eaf0: 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
eb00: 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
eb10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e qlite3_config(in
eb20: 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t, ...);../*.**
eb30: 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 CAPI3REF: Config
eb40: 75 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ure database con
eb50: 6e 65 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 nections {H1420
eb60: 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 0} <S20000>.** E
eb70: 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
eb80: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 * The sqlite3_db
eb90: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 _config() interf
eba0: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d ace is used to m
ebb0: 61 6b 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ake configuratio
ebc0: 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 n.** changes to
ebd0: 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
ebe0: 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e ection]. The in
ebf0: 74 65 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c terface is simil
ec00: 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 ar to.** [sqlite
ec10: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 3_config()] exce
ec20: 70 74 20 74 68 61 74 20 74 68 65 20 63 68 61 6e pt that the chan
ec30: 67 65 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 ges apply to a s
ec40: 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 ingle.** [databa
ec50: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 se connection] (
ec60: 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
ec70: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 first argument)
ec80: 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 . The.** sqlite
ec90: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 3_db_config() in
eca0: 74 65 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 terface can only
ecb0: 20 62 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 be used immedia
ecc0: 74 65 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 tely after.** th
ecd0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
ece0: 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 ction is created
ecf0: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
ed00: 6f 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c open()],.** [sql
ed10: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
ed20: 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e or [sqlite3_open
ed30: 5f 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 _v2()]. .**.**
ed40: 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
ed50: 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 ent to sqlite3_d
ed60: 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e b_config(D,V,...
ed70: 29 20 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e ) is the.** con
ed80: 66 69 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 figuration verb
ed90: 2d 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 - an integer cod
eda0: 65 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 e that indicates
edb0: 20 77 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 what.** aspect
edc0: 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 of the [database
edd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 connection] is
ede0: 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 being configured
edf0: 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 ..** The only ch
ee00: 6f 69 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 oice for this va
ee10: 6c 75 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 lue is [SQLITE_D
ee20: 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 BCONFIG_LOOKASID
ee30: 45 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 E]..** New verbs
ee40: 20 61 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 are likely to b
ee50: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
ee60: 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
ee70: 4c 69 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f Lite..** Additio
ee80: 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 nal arguments de
ee90: 70 65 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 pend on the verb
eea0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
eeb0: 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 ents:.** [H14203
eec0: 5d 20 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 ] [H14206] [H142
eed0: 30 39 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 09] [H14212] [H1
eee0: 34 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 4215].*/.SQLITE_
eef0: 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
ef00: 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
ef10: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 te3_db_config(sq
ef20: 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 lite3*, int op,
ef30: 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ...);../*.** CAP
ef40: 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c I3REF: Memory Al
ef50: 6c 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 location Routine
ef60: 73 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 s {H10155} <S201
ef70: 32 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 20>.** EXPERIMEN
ef80: 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 TAL.**.** An ins
ef90: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
efa0: 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 ject defines the
efb0: 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 interface betwe
efc0: 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 en SQLite.** and
efd0: 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
efe0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 y allocation rou
eff0: 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 tines..**.** Thi
f000: 73 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 s object is used
f010: 20 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 in only one pla
f020: 63 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 ce in the SQLite
f030: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 interface..** A
f040: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
f050: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
f060: 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 object is the ar
f070: 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 gument to.** [sq
f080: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
f090: 77 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 when the configu
f0a0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 ration option is
f0b0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
f0c0: 49 47 5f 4d 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 IG_MALLOC] or [S
f0d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
f0e0: 4d 41 4c 4c 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 MALLOC]. .** By
f0f0: 20 63 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 creating an ins
f100: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
f110: 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 ject.** and pass
f120: 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 ing it to [sqlit
f130: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
f140: 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
f150: 5d 29 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e ]).** during con
f160: 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61 figuration, an a
f170: 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73 pplication can s
f180: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e pecify an altern
f190: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 ative.** memory
f1a0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
f1b0: 73 74 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20 stem for SQLite
f1c0: 74 6f 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f to use for all o
f1d0: 66 20 69 74 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 f its.** dynamic
f1e0: 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a 2a memory needs..*
f1f0: 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 53 *.** Note that S
f200: 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 69 74 68 QLite comes with
f210: 20 73 65 76 65 72 61 6c 20 5b 62 75 69 6c 74 2d several [built-
f220: 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 in memory alloca
f230: 74 6f 72 73 5d 0a 2a 2a 20 74 68 61 74 20 61 72 tors].** that ar
f240: 65 20 70 65 72 66 65 63 74 6c 79 20 61 64 65 71 e perfectly adeq
f250: 75 61 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65 uate for the ove
f260: 72 77 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 rwhelming majori
f270: 74 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f ty of applicatio
f280: 6e 73 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 ns.** and that t
f290: 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e his object is on
f2a0: 6c 79 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74 ly useful to a t
f2b0: 69 6e 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20 iny minority of
f2c0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 applications.**
f2d0: 77 69 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64 with specialized
f2e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f2f0: 6f 6e 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e on requirements.
f300: 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 This object is
f310: 0a 2a 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75 .** also used du
f320: 72 69 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20 ring testing of
f330: 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20 SQLite in order
f340: 74 6f 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c to specify an al
f350: 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d ternative.** mem
f360: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 ory allocator th
f370: 61 74 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d at simulates mem
f380: 6f 72 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 ory out-of-memor
f390: 79 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a y conditions in.
f3a0: 2a 2a 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 ** order to veri
f3b0: 66 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 72 fy that SQLite r
f3c0: 65 63 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c ecovers graceful
f3d0: 6c 79 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 ly from such.**
f3e0: 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a conditions..**.*
f3f0: 2a 20 54 68 65 20 78 4d 61 6c 6c 6f 63 20 61 6e * The xMalloc an
f400: 64 20 78 46 72 65 65 20 6d 65 74 68 6f 64 73 20 d xFree methods
f410: 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65 20 74 must work like t
f420: 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 he.** malloc() a
f430: 6e 64 20 66 72 65 65 28 29 20 66 75 6e 63 74 69 nd free() functi
f440: 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 ons from the sta
f450: 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e ndard C library.
f460: 0a 2a 2a 20 54 68 65 20 78 52 65 61 6c 6c 6f 63 .** The xRealloc
f470: 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 77 6f 72 method must wor
f480: 6b 20 6c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 k like realloc()
f490: 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 from the standa
f4a0: 72 64 20 43 20 6c 69 62 72 61 72 79 0a 2a 2a 20 rd C library.**
f4b0: 77 69 74 68 20 74 68 65 20 65 78 63 65 70 74 69 with the excepti
f4c0: 6f 6e 20 74 68 61 74 20 69 66 20 74 68 65 20 73 on that if the s
f4d0: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
f4e0: 6f 20 78 52 65 61 6c 6c 6f 63 20 69 73 20 7a 65 o xRealloc is ze
f4f0: 72 6f 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 ro,.** xRealloc
f500: 6d 75 73 74 20 62 65 20 61 20 6e 6f 2d 6f 70 20 must be a no-op
f510: 2d 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 70 65 - it must not pe
f520: 72 66 6f 72 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 rform any alloca
f530: 74 69 6f 6e 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c tion or.** deall
f540: 6f 63 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 ocation. SQLite
f550: 20 67 75 61 72 61 6e 74 65 65 64 73 20 74 68 61 guaranteeds tha
f560: 74 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 t the second arg
f570: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 78 52 65 61 ument to.** xRea
f580: 6c 6c 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61 lloc is always a
f590: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
f5a0: 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 by a prior call
f5b0: 74 6f 20 78 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 to xRoundup..**
f5c0: 41 6e 64 20 73 6f 20 69 6e 20 63 61 73 65 73 20 And so in cases
f5d0: 77 68 65 72 65 20 78 52 6f 75 6e 64 75 70 20 61 where xRoundup a
f5e0: 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 lways returns a
f5f0: 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 72 2c positive number,
f600: 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 63 61 6e .** xRealloc can
f610: 20 70 65 72 66 6f 72 6d 20 65 78 61 63 74 6c 79 perform exactly
f620: 20 61 73 20 74 68 65 20 73 74 61 6e 64 61 72 64 as the standard
f630: 20 6c 69 62 72 61 72 79 20 72 65 61 6c 6c 6f 63 library realloc
f640: 28 29 20 61 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 () and.** still
f650: 62 65 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 be in compliance
f660: 20 77 69 74 68 20 74 68 69 73 20 73 70 65 63 69 with this speci
f670: 66 69 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 fication..**.**
f680: 78 53 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74 xSize should ret
f690: 75 72 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 urn the allocate
f6a0: 64 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f d size of a memo
f6b0: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ry allocation.**
f6c0: 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 previously obta
f6d0: 69 6e 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f ined from xMallo
f6e0: 63 20 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20 c or xRealloc.
f6f0: 54 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 The allocated si
f700: 7a 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 ze.** is always
f710: 61 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 at least as big
f720: 61 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64 as the requested
f730: 20 73 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65 size but may be
f740: 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 larger..**.** T
f750: 68 65 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68 he xRoundup meth
f760: 6f 64 20 72 65 74 75 72 6e 73 20 77 68 61 74 20 od returns what
f770: 77 6f 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c would be the all
f780: 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a ocated size of.*
f790: 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 * a memory alloc
f7a0: 61 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61 ation given a pa
f7b0: 72 74 69 63 75 6c 61 72 20 72 65 71 75 65 73 74 rticular request
f7c0: 65 64 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d ed size. Most m
f7d0: 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
f7e0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d ors round up mem
f7f0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ory allocations
f800: 61 74 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20 at least to the
f810: 6e 65 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a next multiple.**
f820: 20 6f 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c of 8. Some all
f830: 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 ocators round up
f840: 20 74 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c to a larger mul
f850: 74 69 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f tiple or to a po
f860: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 wer of 2..** Eve
f870: 72 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ry memory alloca
f880: 74 69 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d tion request com
f890: 69 6e 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b ing in through [
f8a0: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
f8b0: 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 ].** or [sqlite3
f8c0: 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 _realloc()] firs
f8d0: 74 20 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 t calls xRoundup
f8e0: 2e 20 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72 . If xRoundup r
f8f0: 65 74 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 eturns 0, .** th
f900: 61 74 20 63 61 75 73 65 73 20 74 68 65 20 63 6f at causes the co
f910: 72 72 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f rresponding memo
f920: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f ry allocation to
f930: 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 fail..**.** The
f940: 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e xInit method in
f950: 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 itializes the me
f960: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 mory allocator.
f970: 20 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a (For example,.*
f980: 2a 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 * it might alloc
f990: 61 74 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 ate any require
f9a0: 6d 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 mutexes or initi
f9b0: 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 alize internal d
f9c0: 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ata.** structure
f9d0: 73 2e 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 s. The xShutdow
f9e0: 6e 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f n method is invo
f9f0: 6b 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 ked (indirectly)
fa00: 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f by.** [sqlite3_
fa10: 73 68 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 shutdown()] and
fa20: 73 68 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 should deallocat
fa30: 65 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 e any resources
fa40: 61 63 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 acquired.** by x
fa50: 49 6e 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 Init. The pAppD
fa60: 61 74 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 ata pointer is u
fa70: 73 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 sed as the only
fa80: 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 parameter to.**
fa90: 78 49 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 xInit and xShutd
faa0: 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 own..**.** SQLit
fab0: 65 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c e holds the [SQL
fac0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
fad0: 5f 4d 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77 _MASTER] mutex w
fae0: 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a hen it invokes.*
faf0: 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 * the xInit meth
fb00: 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74 od, so the xInit
fb10: 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74 method need not
fb20: 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 be threadsafe.
fb30: 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77 The.** xShutdow
fb40: 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79 n method is only
fb50: 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71 called from [sq
fb60: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
fb70: 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20 ] so it does.**
fb80: 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74 not need to be t
fb90: 68 72 65 61 64 73 61 66 65 20 65 69 74 68 65 72 hreadsafe either
fba0: 2e 20 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72 . For all other
fbb0: 20 6d 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65 methods, SQLite
fbc0: 0a 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 .** holds the [S
fbd0: 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
fbe0: 49 43 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73 IC_MEM] mutex as
fbf0: 20 6c 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20 long as the.**
fc00: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
fc10: 45 4d 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67 EMSTATUS] config
fc20: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 uration option i
fc30: 73 20 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69 s turned on (whi
fc40: 63 68 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64 ch.** it is by d
fc50: 65 66 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 efault) and so t
fc60: 68 65 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 he methods are a
fc70: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 utomatically ser
fc80: 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 ialized..** Howe
fc90: 76 65 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f ver, if [SQLITE_
fca0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 CONFIG_MEMSTATUS
fcb0: 5d 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 ] is disabled, t
fcc0: 68 65 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a hen the other.**
fcd0: 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 methods must be
fce0: 20 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 threadsafe or e
fcf0: 6c 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f lse make their o
fd00: 77 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 wn arrangements
fd10: 66 6f 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61 for.** serializa
fd20: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 tion..**.** SQLi
fd30: 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e te will never in
fd40: 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 voke xInit() mor
fd50: 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 e than once with
fd60: 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 out an interveni
fd70: 6e 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 ng.** call to xS
fd80: 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 hutdown()..*/.ty
fd90: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
fda0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
fdb0: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
fdc0: 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c hods;.struct sql
fdd0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
fde0: 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 {. void *(*xMa
fdf0: 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 lloc)(int);
fe00: 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c /* Memory al
fe10: 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f location functio
fe20: 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 n */. void (*xF
fe30: 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 ree)(void*);
fe40: 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 /* Free a
fe50: 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
fe60: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 */. void *(*xR
fe70: 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e ealloc)(void*,in
fe80: 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 t); /* Resize a
fe90: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a n allocation */.
fea0: 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 int (*xSize)(v
feb0: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 oid*);
fec0: 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 /* Return the s
fed0: 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 ize of an alloca
fee0: 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a tion */. int (*
fef0: 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 xRoundup)(int);
ff00: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e /* Roun
ff10: 64 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a d up request siz
ff20: 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e to allocation
ff30: 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a size */. int (*
ff40: 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 xInit)(void*);
ff50: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 /* Init
ff60: 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
ff70: 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 y allocator */.
ff80: 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 void (*xShutdow
ff90: 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 n)(void*);
ffa0: 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 /* Deinitialize
ffb0: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
ffc0: 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ator */. void *
ffd0: 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 pAppData;
ffe0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
fff0: 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 ment to xInit()
10000 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 and xShutdown()
10010 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.};../*.** CAP
10020 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 I3REF: Configura
10030 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 tion Options {H1
10040 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 0160} <S20000>.*
10050 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
10060 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 *.** These const
10070 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61 ants are the ava
10080 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 ilable integer c
10090 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
100a0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e ions that.** can
100b0 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 be passed as th
100c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
100d0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
100e0 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 _config()] inter
100f0 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 face..**.** New
10100 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
10110 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 tions may be add
10120 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
10130 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e eases of SQLite.
10140 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e .** Existing con
10150 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
10160 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 ns might be disc
10170 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 ontinued. Appli
10180 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c cations.** shoul
10190 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 d check the retu
101a0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 rn code from [sq
101b0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
101c0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
101d0 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f t.** the call wo
101e0 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 rked. The [sqli
101f0 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e te3_config()] in
10200 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 terface will ret
10210 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 urn a.** non-zer
10220 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 o [error code] i
10230 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 f a discontinued
10240 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 or unsupported
10250 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
10260 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b tion.** is invok
10270 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a ed..**.** <dl>.*
10280 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
10290 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
102a0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
102b0 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 re are no argume
102c0 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 nts to this opti
102d0 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e on. This option
102e0 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c disables.** all
102f0 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75 mutexing and pu
10300 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61 ts SQLite into a
10310 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 20 63 mode where it c
10320 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a an only be used.
10330 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 ** by a single t
10340 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a hread.</dd>.**.*
10350 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
10360 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c FIG_MULTITHREAD<
10370 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 /dt>.** <dd>Ther
10380 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e e are no argumen
10390 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f ts to this optio
103a0 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 n. This option
103b0 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 disables.** mute
103c0 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 xing on [databas
103d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e e connection] an
103e0 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 d [prepared stat
103f0 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a ement] objects..
10400 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 ** The applicati
10410 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c on is responsibl
10420 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e e for serializin
10430 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b g access to.** [
10440 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
10450 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 ions] and [prepa
10460 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e red statements].
10470 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65 But other mute
10480 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c xes.** are enabl
10490 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74 ed so that SQLit
104a0 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 e will be safe t
104b0 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 o use in a multi
104c0 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 -threaded.** env
104d0 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 ironment as long
104e0 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 as no two threa
104f0 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ds attempt to us
10500 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 e the same.** [d
10510 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
10520 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20 on] at the same
10530 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b time. See the [
10540 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a threading mode].
10550 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e ** documentation
10560 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
10570 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 information.</dd
10580 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10590 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c TE_CONFIG_SERIAL
105a0 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 IZED</dt>.** <dd
105b0 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 >There are no ar
105c0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 guments to this
105d0 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 option. This op
105e0 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 tion enables.**
105f0 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c all mutexes incl
10600 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 72 73 uding the recurs
10610 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f ive.** mutexes o
10620 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
10630 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 ection] and [pre
10640 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
10650 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 objects..** In
10660 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68 this mode (which
10670 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 is the default
10680 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
10690 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
106a0 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
106b0 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74 FE=1]) the SQLit
106c0 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69 e library will i
106d0 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 tself serialize
106e0 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 access.** to [da
106f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
10700 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 ns] and [prepare
10710 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f d statements] so
10720 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 that the.** app
10730 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 lication is free
10740 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 to use the same
10750 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
10760 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a ction] or the.**
10770 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 same [prepared
10780 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 statement] in di
10790 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 fferent threads
107a0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
107b0 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 ..** See the [th
107c0 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f reading mode] do
107d0 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 cumentation for
107e0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
107f0 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a mation.</dd>.**.
10800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
10810 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e NFIG_MALLOC</dt>
10820 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
10830 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
10840 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
10850 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
10860 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
10870 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
10880 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 _mem_methods] st
10890 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 ructure. The ar
108a0 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 gument specifies
108b0 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 .** alternative
108c0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
108d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
108e0 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 ines to be used
108f0 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 in place of.** t
10900 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
10910 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75 tion routines bu
10920 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e ilt into SQLite.
10930 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
10940 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
10950 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 TMALLOC</dt>.**
10960 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
10970 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
10980 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
10990 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
109a0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
109b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d the [sqlite3_mem
109c0 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 _methods] struct
109d0 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 ure. The [sqlit
109e0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a e3_mem_methods].
109f0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ** structure is
10a00 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 filled with the
10a10 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 currently define
10a20 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 d memory allocat
10a30 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ion routines..**
10a40 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e This option can
10a50 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 be used to over
10a60 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 load the default
10a70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
10a80 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
10a90 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 68 ith a wrapper th
10aa0 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d at simulations m
10ab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10ac0 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 failure or.** t
10ad0 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61 racks memory usa
10ae0 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e ge, for example.
10af0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
10b00 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
10b10 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 MSTATUS</dt>.**
10b20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
10b30 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67 takes single arg
10b40 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e ument of type in
10b50 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 t, interpreted a
10b60 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c s a .** boolean,
10b70 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f which enables o
10b80 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 r disables the c
10b90 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d ollection of mem
10ba0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a ory allocation .
10bb0 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 57 ** statistics. W
10bc0 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68 hen disabled, th
10bd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 e following SQLi
10be0 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 62 65 te interfaces be
10bf0 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 come .** non-ope
10c00 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c rational:.** <
10c10 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 ul>.** <li> [s
10c20 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
10c30 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 ed()].** <li>
10c40 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f [sqlite3_memory_
10c50 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20 highwater()].**
10c60 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
10c70 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 soft_heap_limit(
10c80 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 )].** <li> [sq
10c90 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a lite3_status()].
10ca0 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f ** </ul>.** </
10cb0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
10cc0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
10cd0 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TCH</dt>.** <dd>
10ce0 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 This option spec
10cf0 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d ifies a static m
10d00 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 emory buffer tha
10d10 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 t SQLite can use
10d20 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20 for.** scratch
10d30 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61 memory. There a
10d40 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e re three argumen
10d50 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 61 ts: A pointer a
10d60 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 67 n 8-byte.** alig
10d70 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 ned memory buffe
10d80 72 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 r from which the
10d90 20 73 63 72 61 63 68 20 61 6c 6c 6f 63 61 74 69 scrach allocati
10da0 6f 6e 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 ons will be.** d
10db0 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f rawn, the size o
10dc0 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 61 f each scratch a
10dd0 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0a llocation (sz),.
10de0 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d ** and the maxim
10df0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 63 72 um number of scr
10e00 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 atch allocations
10e10 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a (N). The sz.**
10e20 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 argument must b
10e30 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 e a multiple of
10e40 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d 16. The sz param
10e50 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61 eter should be a
10e60 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61 few bytes.** la
10e70 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 61 63 rger than the ac
10e80 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 70 61 tual scratch spa
10e90 63 65 20 72 65 71 75 69 72 65 64 20 64 75 65 20 ce required due
10ea0 74 6f 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 to internal over
10eb0 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 head..** The fir
10ec0 73 74 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 st argument shou
10ed0 6c 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e ld pointer to an
10ee0 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 8-byte aligned
10ef0 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20 61 74 20 buffer.** of at
10f00 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 least sz*N bytes
10f10 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 of memory..** S
10f20 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e QLite will use n
10f30 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 o more than one
10f40 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 61 scratch buffer a
10f50 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61 t once per threa
10f60 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c d, so.** N shoul
10f70 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 d be set to the
10f80 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d expected maximum
10f90 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 number of threa
10fa0 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70 ds. The sz.** p
10fb0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
10fc0 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 20 73 be 6 times the s
10fd0 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 ize of the large
10fe0 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65 st database page
10ff0 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63 size..** Scratc
11000 68 20 62 75 66 66 65 72 73 20 61 72 65 20 75 73 h buffers are us
11010 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
11020 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20 e btree balance
11030 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a operation. If.*
11040 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 6c 61 * The btree bala
11050 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 69 74 ncer needs addit
11060 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79 ional memory bey
11070 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 ond what is prov
11080 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74 ided by.** scrat
11090 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 69 66 ch buffers or if
110a0 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 no scratch buff
110b0 65 72 20 73 70 61 63 65 20 69 73 20 73 70 65 63 er space is spec
110c0 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69 ified, then SQLi
110d0 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73 te.** goes to [s
110e0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
110f0 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d to obtain the m
11100 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c emory it needs.<
11110 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
11120 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
11130 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c ECACHE</dt>.** <
11140 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 dd>This option s
11150 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 pecifies a stati
11160 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 c memory buffer
11170 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 that SQLite can
11180 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64 use for.** the d
11190 61 74 61 62 61 73 65 20 70 61 67 65 20 63 61 63 atabase page cac
111a0 68 65 20 77 69 74 68 20 74 68 65 20 64 65 66 61 he with the defa
111b0 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 69 ult page cache i
111c0 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a mplemenation. .
111d0 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72 ** This configur
111e0 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ation should not
111f0 20 62 65 20 75 73 65 64 20 69 66 20 61 6e 20 61 be used if an a
11200 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
11210 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20 e page.** cache
11220 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
11230 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74 s loaded using t
11240 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 he SQLITE_CONFIG
11250 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a _PCACHE option..
11260 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 ** There are thr
11270 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 ee arguments to
11280 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70 this option: A p
11290 6f 69 6e 74 65 72 20 74 6f 20 38 2d 62 79 74 65 ointer to 8-byte
112a0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f aligned.** memo
112b0 72 79 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ry, the size of
112c0 65 61 63 68 20 70 61 67 65 20 62 75 66 66 65 72 each page buffer
112d0 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e (sz), and the n
112e0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 28 umber of pages (
112f0 4e 29 2e 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 N)..** The sz ar
11300 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 gument should be
11310 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
11320 20 6c 61 72 67 65 73 74 20 64 61 74 61 62 61 73 largest databas
11330 65 20 70 61 67 65 0a 2a 2a 20 28 61 20 70 6f 77 e page.** (a pow
11340 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 er of two betwee
11350 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 29 n 512 and 32768)
11360 20 70 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65 plus a little e
11370 78 74 72 61 20 66 6f 72 20 65 61 63 68 0a 2a 2a xtra for each.**
11380 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 54 page header. T
11390 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 73 he page header s
113a0 69 7a 65 20 69 73 20 32 30 20 74 6f 20 34 30 20 ize is 20 to 40
113b0 62 79 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20 bytes depending
113c0 6f 6e 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 61 on.** the host a
113d0 72 63 68 69 74 65 63 74 75 72 65 2e 20 20 49 74 rchitecture. It
113e0 20 69 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70 is harmless, ap
113f0 61 72 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73 art from the was
11400 74 65 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 ted memory,.** t
11410 6f 20 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74 o make sz a litt
11420 6c 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54 le too large. T
11430 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
11440 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e ment should poin
11450 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 t to an allocati
11460 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 on of at least s
11470 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d z*N bytes of mem
11480 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ory..** SQLite w
11490 69 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f ill use the memo
114a0 72 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 ry provided by t
114b0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
114c0 74 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 t to satisfy its
114d0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 .** memory needs
114e0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e for the first N
114f0 20 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 pages that it a
11500 64 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 dds to cache. I
11510 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 f additional.**
11520 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 page cache memor
11530 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f y is needed beyo
11540 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 nd what is provi
11550 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 ded by this opti
11560 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 on, then.** SQLi
11570 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 te goes to [sqli
11580 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f te3_malloc()] fo
11590 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c r the additional
115a0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a storage space..
115b0 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ** The implement
115c0 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 ation might use
115d0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
115e0 68 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 he N buffers to
115f0 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 hold .** memory
11600 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 accounting infor
11610 6d 61 74 69 6f 6e 2e 20 54 68 65 20 70 6f 69 6e mation. The poin
11620 74 65 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 ter in the first
11630 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 0a 2a argument must.*
11640 2a 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 * be aligned to
11650 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 an 8-byte bounda
11660 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 ry or subsequent
11670 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c behavior of SQL
11680 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 75 ite.** will be u
11690 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
116a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
116b0 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e CONFIG_HEAP</dt>
116c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
116d0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 ion specifies a
116e0 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 static memory bu
116f0 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 ffer that SQLite
11700 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 will use.** for
11710 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 all of its dyna
11720 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 mic memory alloc
11730 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f ation needs beyo
11740 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 nd those provide
11750 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c d.** for by [SQL
11760 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
11770 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f CH] and [SQLITE_
11780 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
11790 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 ]..** There are
117a0 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a three arguments:
117b0 20 41 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e An 8-byte align
117c0 65 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 ed pointer to th
117d0 65 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 e memory,.** the
117e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
117f0 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 in the memory b
11800 75 66 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d uffer, and the m
11810 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f inimum allocatio
11820 6e 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 n size..** If th
11830 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 e first pointer
11840 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e (the memory poin
11850 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 ter) is NULL, th
11860 65 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 en SQLite revert
11870 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 s.** to using it
11880 73 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 s default memory
11890 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 allocator (the
118a0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 system malloc()
118b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c implementation),
118c0 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 .** undoing any
118d0 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e prior invocation
118e0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 of [SQLITE_CONF
118f0 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 IG_MALLOC]. If
11900 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f the.** memory po
11910 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c inter is not NUL
11920 4c 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 L and either [SQ
11930 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
11940 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 YS3] or.** [SQLI
11950 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
11960 35 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 5] are defined,
11970 74 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 then the alterna
11980 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 tive memory.** a
11990 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 llocator is enga
119a0 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c ged to handle al
119b0 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d l of SQLites mem
119c0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e ory allocation n
119d0 65 65 64 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 eeds..** The fir
119e0 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 st pointer (the
119f0 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 memory pointer)
11a00 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 must be aligned
11a10 74 6f 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 to an 8-byte.**
11a20 62 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73 boundary or subs
11a30 65 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20 equent behavior
11a40 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 of SQLite will b
11a50 65 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 e undefined.</dd
11a60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
11a70 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c TE_CONFIG_MUTEX<
11a80 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
11a90 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
11aa0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
11ab0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
11ac0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
11ad0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
11ae0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
11af0 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 ds] structure.
11b00 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 The argument spe
11b10 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e cifies.** altern
11b20 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 ative low-level
11b30 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74 mutex routines t
11b40 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 o be used in pla
11b50 63 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 ce.** the mutex
11b60 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 routines built i
11b70 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e nto SQLite.</dd>
11b80 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
11b90 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 E_CONFIG_GETMUTE
11ba0 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 X</dt>.** <dd>Th
11bb0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
11bc0 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e a single argumen
11bd0 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 t which is a poi
11be0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e nter to an.** in
11bf0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 stance of the [s
11c00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
11c10 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e hods] structure.
11c20 20 20 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 The.** [sqlite
11c30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 3_mutex_methods]
11c40 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 .** structure is
11c50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 filled with the
11c60 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
11c70 65 64 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 ed mutex routine
11c80 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f s..** This optio
11c90 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f n can be used to
11ca0 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 overload the de
11cb0 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f fault mutex allo
11cc0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e cation.** routin
11cd0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 es with a wrappe
11ce0 72 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 r used to track
11cf0 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f 72 20 mutex usage for
11d00 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 performance.** p
11d10 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 rofiling or test
11d20 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 ing, for example
11d30 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
11d40 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c >SQLITE_CONFIG_L
11d50 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a OOKASIDE</dt>.**
11d60 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
11d70 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d takes two argum
11d80 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d ents that determ
11d90 69 6e 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a ine the default.
11da0 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** memory alloca
11db0 74 69 6f 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 6f tion lookaside o
11dc0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 ptimization. Th
11dd0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
11de0 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 is the.** size
11df0 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 of each lookasid
11e00 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 61 6e e buffer slot an
11e10 64 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 d the second is
11e20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
11e30 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 slots allocated
11e40 20 74 6f 20 65 61 63 68 20 64 61 74 61 62 61 73 to each databas
11e50 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 e connection. T
11e60 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 his option sets
11e70 74 68 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c the.** <i>defaul
11e80 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 t</i> lookaside
11e90 73 69 7a 65 2e 20 20 54 68 65 20 5b 53 51 4c 49 size. The [SQLI
11ea0 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b TE_DBCONFIG_LOOK
11eb0 41 53 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74 ASIDE].** verb t
11ec0 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f o [sqlite3_db_co
11ed0 6e 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 nfig()] can be u
11ee0 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 sed to change th
11ef0 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 e lookaside.** c
11f00 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20 onfiguration on
11f10 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65 individual conne
11f20 63 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a ctions.</dd>.**.
11f30 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
11f40 4e 46 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e NFIG_PCACHE</dt>
11f50 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11f60 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
11f70 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
11f80 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
11f90 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 o.** an [sqlite3
11fa0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d _pcache_methods]
11fb0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f object. This o
11fc0 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 73 20 bject specifies
11fd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a the interface.**
11fe0 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67 to a custom pag
11ff0 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
12000 74 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 tation. SQLite
12010 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 makes a copy of
12020 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e the.** object an
12030 64 20 75 73 65 73 20 69 74 20 66 6f 72 20 70 61 d uses it for pa
12040 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 ge cache memory
12050 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 allocations.</dd
12060 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
12070 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 TE_CONFIG_GETPCA
12080 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e CHE</dt>.** <dd>
12090 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
120a0 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d s a single argum
120b0 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70 ent which is a p
120c0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
120d0 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f [sqlite3_pcache_
120e0 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e methods] object.
120f0 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20 SQLite copies
12100 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a of the current.*
12110 2a 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 * page cache imp
12120 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f lementation into
12130 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 that object.</d
12140 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a d>.**.** </dl>.*
12150 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
12160 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
12170 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 READ 1 /* nil
12180 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
12190 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 E_CONFIG_MULTITH
121a0 52 45 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c READ 2 /* nil
121b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
121c0 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c TE_CONFIG_SERIAL
121d0 49 5a 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 IZED 3 /* ni
121e0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c l */.#define SQL
121f0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f ITE_CONFIG_MALLO
12200 43 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73 C 4 /* s
12210 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
12220 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
12230 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
12240 4d 41 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a MALLOC 5 /*
12250 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
12260 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 hods* */.#define
12270 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 SQLITE_CONFIG_S
12280 43 52 41 54 43 48 20 20 20 20 20 20 20 36 20 20 CRATCH 6
12290 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a /* void*, int sz
122a0 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 , int N */.#defi
122b0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
122c0 5f 50 41 47 45 43 41 43 48 45 20 20 20 20 20 37 _PAGECACHE 7
122d0 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 /* void*, int
122e0 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 sz, int N */.#de
122f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
12300 49 47 5f 48 45 41 50 20 20 20 20 20 20 20 20 20 IG_HEAP
12310 20 38 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 8 /* void*, in
12320 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e t nByte, int min
12330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
12340 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 TE_CONFIG_MEMSTA
12350 54 55 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f TUS 9 /* bo
12360 6f 6c 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 olean */.#define
12370 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d SQLITE_CONFIG_M
12380 55 54 45 58 20 20 20 20 20 20 20 20 31 30 20 20 UTEX 10
12390 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 /* sqlite3_mutex
123a0 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 _methods* */.#de
123b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
123c0 49 47 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20 IG_GETMUTEX
123d0 31 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 11 /* sqlite3_m
123e0 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f utex_methods* */
123f0 0a 2f 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53 ./* previously S
12400 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 QLITE_CONFIG_CHU
12410 4e 4b 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 NKALLOC 12 which
12420 20 69 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 is now unused.
12430 2a 2f 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */ .#define SQLI
12440 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 TE_CONFIG_LOOKAS
12450 49 44 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e IDE 13 /* in
12460 74 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 t int */.#define
12470 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 SQLITE_CONFIG_P
12480 43 41 43 48 45 20 20 20 20 20 20 20 31 34 20 20 CACHE 14
12490 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 /* sqlite3_pcach
124a0 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 e_methods* */.#d
124b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
124c0 46 49 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 FIG_GETPCACHE
124d0 20 31 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 15 /* sqlite3_
124e0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 pcache_methods*
124f0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
12500 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f EF: Configuratio
12510 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 n Options {H1017
12520 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 0} <S20000>.** E
12530 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
12540 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 * These constant
12550 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 s are the availa
12560 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 ble integer conf
12570 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
12580 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 s that.** can be
12590 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
125a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
125b0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 o the [sqlite3_d
125c0 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 b_config()] inte
125d0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 rface..**.** New
125e0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
125f0 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 ptions may be ad
12600 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
12610 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
12620 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f ..** Existing co
12630 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
12640 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 ons might be dis
12650 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c continued. Appl
12660 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 ications.** shou
12670 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 ld check the ret
12680 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 urn code from [s
12690 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
126a0 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ()] to make sure
126b0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c that.** the cal
126c0 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b l worked. The [
126d0 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
126e0 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 g()] interface w
126f0 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 ill return a.**
12700 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 non-zero [error
12710 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f code] if a disco
12720 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 ntinued or unsup
12730 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 ported configura
12740 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 tion option.** i
12750 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a s invoked..**.**
12760 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c <dl>.** <dt>SQL
12770 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f ITE_DBCONFIG_LOO
12780 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c KASIDE</dt>.** <
12790 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 dd>This option t
127a0 61 6b 65 73 20 74 68 72 65 65 20 61 64 64 69 74 akes three addit
127b0 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 ional arguments
127c0 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 that determine t
127d0 68 65 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 he .** [lookasid
127e0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
127f0 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f or] configuratio
12800 6e 20 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 n for the [datab
12810 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
12820 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 .** The first ar
12830 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72 gument (the thir
12840 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b d parameter to [
12850 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
12860 67 28 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 g()] is a.** poi
12870 6e 74 65 72 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 nter to an memor
12880 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20 y buffer to use
12890 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 for lookaside me
128a0 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 66 69 72 mory..** The fir
128b0 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 st argument may
128c0 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 be NULL in which
128d0 20 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c case SQLite wil
128e0 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a l allocate the.*
128f0 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 * lookaside buff
12900 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20 er itself using
12910 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
12920 29 5d 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 )]. The second
12930 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a argument is the.
12940 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 ** size of each
12950 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 lookaside buffer
12960 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68 slot and the th
12970 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
12980 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
12990 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a slots. The siz
129a0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 e of the buffer
129b0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
129c0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72 ument must be gr
129d0 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 eater than.** or
129e0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72 equal to the pr
129f0 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73 65 63 oduct of the sec
12a00 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 61 72 ond and third ar
12a10 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 62 75 guments. The bu
12a20 66 66 65 72 0a 2a 2a 20 6d 75 73 74 20 62 65 20 ffer.** must be
12a30 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d aligned to an 8-
12a40 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20 byte boundary.
12a50 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
12a60 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a gument is not.**
12a70 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 a multiple of 8
12a80 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c , it is internal
12a90 6c 79 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 ly rounded down
12aa0 74 6f 20 74 68 65 20 6e 65 78 74 20 73 6d 61 6c to the next smal
12ab0 6c 65 72 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 ler.** multiple
12ac0 6f 66 20 38 2e 20 20 53 65 65 20 61 6c 73 6f 3a of 8. See also:
12ad0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
12ae0 4c 4f 4f 4b 41 53 49 44 45 5d 3c 2f 64 64 3e 0a LOOKASIDE]</dd>.
12af0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 **.** </dl>.*/.#
12b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 define SQLITE_DB
12b10 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
12b20 20 20 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69 1001 /* voi
12b30 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a d* int int */...
12b40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
12b50 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c Enable Or Disabl
12b60 65 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c e Extended Resul
12b70 74 20 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d t Codes {H12200}
12b80 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
12b90 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 The sqlite3_exte
12ba0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 nded_result_code
12bb0 73 28 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 s() routine enab
12bc0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 les or disables
12bd0 74 68 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 the.** [extended
12be0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 result codes] f
12bf0 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65 eature of SQLite
12c00 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 . The extended r
12c10 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 esult.** codes a
12c20 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 re disabled by d
12c30 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f efault for histo
12c40 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c rical compatibil
12c50 69 74 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f ity consideratio
12c60 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ns..**.** Requir
12c70 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
12c80 30 31 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 01] [H12202].*/.
12c90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
12ca0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
12cb0 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c result_codes(sql
12cc0 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 ite3*, int onoff
12cd0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
12ce0 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20 EF: Last Insert
12cf0 52 6f 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c Rowid {H12220} <
12d00 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 S10700>.**.** Ea
12d10 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53 ch entry in an S
12d20 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20 QLite table has
12d30 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20 a unique 64-bit
12d40 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
12d50 72 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65 r key called the
12d60 20 5b 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64 [ROWID | "rowid
12d70 22 5d 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 "]. The rowid is
12d80 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
12d90 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 e.** as an undec
12da0 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d lared column nam
12db0 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f ed ROWID, OID, o
12dc0 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e r _ROWID_ as lon
12dd0 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 g as those.** na
12de0 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f mes are not also
12df0 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 used by explici
12e00 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c tly declared col
12e10 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 umns. If.** the
12e20 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 table has a colu
12e30 6d 6e 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45 mn of type [INTE
12e40 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d GER PRIMARY KEY]
12e50 20 74 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d then that colum
12e60 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 n.** is another
12e70 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f alias for the ro
12e80 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 wid..**.** This
12e90 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
12ea0 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 the [rowid] of t
12eb0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a he most recent.*
12ec0 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e * successful [IN
12ed0 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64 SERT] into the d
12ee0 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 atabase from the
12ef0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
12f00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 ction].** in the
12f10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
12f20 20 20 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66 If no successf
12f30 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 ul [INSERT]s.**
12f40 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72 72 have ever occurr
12f50 65 64 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62 ed on that datab
12f60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ase connection,
12f70 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
12f80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 ..**.** If an [I
12f90 4e 53 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69 NSERT] occurs wi
12fa0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 thin a trigger,
12fb0 74 68 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d then the [rowid]
12fc0 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 65 64 of the inserted
12fd0 0a 2a 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72 .** row is retur
12fe0 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ned by this rout
12ff0 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ine as long as t
13000 68 65 20 74 72 69 67 67 65 72 20 69 73 20 72 75 he trigger is ru
13010 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e nning..** But on
13020 63 65 20 74 68 65 20 74 72 69 67 67 65 72 20 74 ce the trigger t
13030 65 72 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76 erminates, the v
13040 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
13050 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
13060 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 reverts to the
13070 6c 61 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72 last value inser
13080 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 74 ted before the t
13090 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a rigger fired..**
130a0 0a 2a 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20 .** An [INSERT]
130b0 74 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 that fails due t
130c0 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 o a constraint v
130d0 69 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 iolation is not
130e0 61 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 a.** successful
130f0 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65 [INSERT] and doe
13100 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 s not change the
13110 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
13120 62 79 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 by this.** routi
13130 6e 65 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54 ne. Thus INSERT
13140 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54 OR FAIL, INSERT
13150 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45 OR IGNORE, INSE
13160 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a RT OR ROLLBACK,.
13170 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52 ** and INSERT OR
13180 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63 ABORT make no c
13190 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65 hanges to the re
131a0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
131b0 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 is.** routine wh
131c0 65 6e 20 74 68 65 69 72 20 69 6e 73 65 72 74 69 en their inserti
131d0 6f 6e 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20 on fails. When
131e0 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 INSERT OR REPLAC
131f0 45 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20 E.** encounters
13200 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f a constraint vio
13210 6c 61 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20 lation, it does
13220 6e 6f 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a not fail. The.*
13230 2a 20 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75 * INSERT continu
13240 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e es to completion
13250 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 after deleting
13260 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 rows that caused
13270 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 .** the constrai
13280 6e 74 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e nt problem so IN
13290 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 SERT OR REPLACE
132a0 77 69 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e will always chan
132b0 67 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e ge.** the return
132c0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69 value of this i
132d0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
132e0 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
132f0 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
13300 2c 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 , an [INSERT] is
13310 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a considered to.*
13320 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20 * be successful
13330 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75 even if it is su
13340 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 bsequently rolle
13350 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
13360 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
13370 63 63 65 73 73 69 62 6c 65 20 74 6f 20 53 51 4c ccessible to SQL
13380 20 73 74 61 74 65 6d 65 6e 74 73 20 76 69 61 20 statements via
13390 74 68 65 0a 2a 2a 20 5b 6c 61 73 74 5f 69 6e 73 the.** [last_ins
133a0 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 ert_rowid() SQL
133b0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a function]..**.**
133c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
133d0 2a 20 5b 48 31 32 32 32 31 5d 20 5b 48 31 32 32 * [H12221] [H122
133e0 32 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 23].**.** If a s
133f0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 70 eparate thread p
13400 65 72 66 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49 erforms a new [I
13410 4e 53 45 52 54 5d 20 6f 6e 20 74 68 65 20 73 61 NSERT] on the sa
13420 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 me.** database c
13430 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 onnection while
13440 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 the [sqlite3_las
13450 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 t_insert_rowid()
13460 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 ].** function is
13470 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 running and thu
13480 73 20 63 68 61 6e 67 65 73 20 74 68 65 20 6c 61 s changes the la
13490 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 st insert [rowid
134a0 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 ],.** then the v
134b0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
134c0 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 [sqlite3_last_i
134d0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 nsert_rowid()] i
134e0 73 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74 61 62 s.** unpredictab
134f0 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74 le and might not
13500 20 65 71 75 61 6c 20 65 69 74 68 65 72 20 74 68 equal either th
13510 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e 65 77 e old or the new
13520 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72 74 20 .** last insert
13530 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 [rowid]..*/.SQLI
13540 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 TE_API sqlite3_i
13550 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 nt64 sqlite3_las
13560 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 t_insert_rowid(s
13570 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a qlite3*);../*.**
13580 20 43 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 CAPI3REF: Count
13590 20 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 The Number Of R
135a0 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 ows Modified {H1
135b0 32 32 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2240} <S10600>.*
135c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
135d0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e on returns the n
135e0 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 umber of databas
135f0 65 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 e rows that were
13600 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 changed.** or i
13610 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 nserted or delet
13620 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 ed by the most r
13630 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 ecently complete
13640 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a d SQL statement.
13650 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 ** on the [datab
13660 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
13670 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
13680 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
13690 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 ..** Only change
136a0 73 20 74 68 61 74 20 61 72 65 20 64 69 72 65 63 s that are direc
136b0 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 tly specified by
136c0 20 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b the [INSERT], [
136d0 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b UPDATE],.** or [
136e0 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e DELETE] statemen
136f0 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 t are counted.
13700 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65 Auxiliary change
13710 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 s caused by.** t
13720 72 69 67 67 65 72 73 20 6f 72 20 5b 66 6f 72 65 riggers or [fore
13730 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d ign key actions]
13740 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 are not counted
13750 2e 20 55 73 65 20 74 68 65 0a 2a 2a 20 5b 73 71 . Use the.** [sq
13760 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e lite3_total_chan
13770 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 ges()] function
13780 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 to find the tota
13790 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e l number of chan
137a0 67 65 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 ges.** including
137b0 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 changes caused
137c0 62 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 by triggers and
137d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 foreign key acti
137e0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 ons..**.** Chang
137f0 65 73 20 74 6f 20 61 20 76 69 65 77 20 74 68 61 es to a view tha
13800 74 20 61 72 65 20 73 69 6d 75 6c 61 74 65 64 20 t are simulated
13810 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f by an [INSTEAD O
13820 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a 20 61 72 F trigger].** ar
13830 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20 e not counted.
13840 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 6c 65 20 Only real table
13850 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e changes are coun
13860 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f ted..**.** A "ro
13870 77 20 63 68 61 6e 67 65 22 20 69 73 20 61 20 63 w change" is a c
13880 68 61 6e 67 65 20 74 6f 20 61 20 73 69 6e 67 6c hange to a singl
13890 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c e row of a singl
138a0 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 e table.** cause
138b0 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 d by an INSERT,
138c0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 DELETE, or UPDAT
138d0 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f E statement. Ro
138e0 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 ws that.** are c
138f0 68 61 6e 67 65 64 20 61 73 20 73 69 64 65 20 65 hanged as side e
13900 66 66 65 63 74 73 20 6f 66 20 5b 52 45 50 4c 41 ffects of [REPLA
13910 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 CE] constraint r
13920 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f esolution,.** ro
13930 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 llback, ABORT pr
13940 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20 ocessing, [DROP
13950 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e TABLE], or by an
13960 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61 y other.** mecha
13970 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75 nisms do not cou
13980 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 6f 77 nt as direct row
13990 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 changes..**.**
139a0 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e 74 65 A "trigger conte
139b0 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 20 6f xt" is a scope o
139c0 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 61 74 f execution that
139d0 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 begins and.** e
139e0 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 63 72 nds with the scr
139f0 69 70 74 20 6f 66 20 61 20 5b 43 52 45 41 54 45 ipt of a [CREATE
13a00 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 TRIGGER | trigg
13a10 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 20 53 51 er]. .** Most SQ
13a20 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 L statements are
13a30 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f 75 .** evaluated ou
13a40 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 69 tside of any tri
13a50 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 74 gger. This is t
13a60 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a he "top level".*
13a70 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 * trigger contex
13a80 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 72 t. If a trigger
13a90 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 20 fires from the
13aa0 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 top level, a.**
13ab0 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74 new trigger cont
13ac0 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 66 ext is entered f
13ad0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 or the duration
13ae0 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 of that one.** t
13af0 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 67 rigger. Subtrig
13b00 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 63 gers create subc
13b10 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 69 ontexts for thei
13b20 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a r duration..**.*
13b30 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 * Calling [sqlit
13b40 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 e3_exec()] or [s
13b50 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72 qlite3_step()] r
13b60 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 0a ecursively does.
13b70 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20 ** not create a
13b80 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74 new trigger cont
13b90 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ext..**.** This
13ba0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
13bb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 the number of d
13bc0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 irect row change
13bd0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 s in the.** most
13be0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 recent INSERT,
13bf0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 UPDATE, or DELET
13c00 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 E statement with
13c10 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 in the same.** t
13c20 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a rigger context..
13c30 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e **.** Thus, when
13c40 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
13c50 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 top level, this
13c60 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
13c70 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 s the.** number
13c80 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 of changes in th
13c90 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e e most recent IN
13ca0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 SERT, UPDATE, or
13cb0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 DELETE.** that
13cc0 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74 also occurred at
13cd0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 the top level.
13ce0 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 Within the body
13cf0 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a 2a of a trigger,.*
13d00 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 * the sqlite3_ch
13d10 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63 anges() interfac
13d20 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 e can be called
13d30 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 to find the numb
13d40 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73 er of.** changes
13d50 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 in the most rec
13d60 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 ently completed
13d70 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
13d80 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 or DELETE.** sta
13d90 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 tement within th
13da0 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 61 e body of the sa
13db0 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48 me trigger..** H
13dc0 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d 62 owever, the numb
13dd0 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73 er returned does
13de0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 not include cha
13df0 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 62 nges.** caused b
13e00 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 69 y subtriggers si
13e10 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 74 nce those have t
13e20 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74 heir own context
13e30 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
13e40 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f the [sqlite3_to
13e50 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 tal_changes()] i
13e60 6e 74 65 72 66 61 63 65 2c 20 74 68 65 0a 2a 2a nterface, the.**
13e70 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 [count_changes
13e80 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 74 68 65 pragma], and the
13e90 20 5b 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 [changes() SQL
13ea0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a function]..**.**
13eb0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
13ec0 2a 20 5b 48 31 32 32 34 31 5d 20 5b 48 31 32 32 * [H12241] [H122
13ed0 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 43].**.** If a s
13ee0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d eparate thread m
13ef0 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 akes changes on
13f00 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
13f10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
13f20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 while [sqlite3_c
13f30 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e hanges()] is run
13f40 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 ning then the va
13f50 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 lue returned.**
13f60 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 is unpredictable
13f70 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 and not meaning
13f80 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ful..*/.SQLITE_A
13f90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
13fa0 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 hanges(sqlite3*)
13fb0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
13fc0 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20 F: Total Number
13fd0 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 Of Rows Modified
13fe0 20 7b 48 31 32 32 36 30 7d 20 3c 53 31 30 36 30 {H12260} <S1060
13ff0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 0>.**.** This fu
14000 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
14010 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
14020 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 changes caused
14030 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20 by [INSERT],.**
14040 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b 44 45 4c [UPDATE] or [DEL
14050 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 73 20 ETE] statements
14060 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 since the [datab
14070 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14080 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 was opened..** T
14090 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 he count include
140a0 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 s all changes fr
140b0 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54 om all [CREATE T
140c0 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 RIGGER | trigger
140d0 5d 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 20 61 ] .** contexts a
140e0 6e 64 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 nd changes made
140f0 62 79 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 by [foreign key
14100 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 actions]. Howeve
14110 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 r,.** the count
14120 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 does not include
14130 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f changes used to
14140 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c implement [REPL
14150 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 ACE] constraints
14160 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b ,.** do rollback
14170 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 s or ABORT proce
14180 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 ssing, or [DROP
14190 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e TABLE] processin
141a0 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 g. The.** count
141b0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
141c0 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77 73 20 e rows of views
141d0 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b 49 4e that fire an [IN
141e0 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 STEAD OF trigger
141f0 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 20 ],.** though if
14200 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 the INSTEAD OF t
14210 72 69 67 67 65 72 20 6d 61 6b 65 73 20 63 68 61 rigger makes cha
14220 6e 67 65 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c nges of its own,
14230 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 0a those changes .
14240 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a ** are counted..
14250 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 ** The changes a
14260 72 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f re counted as so
14270 6f 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d on as the statem
14280 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 ent that makes t
14290 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 hem is.** comple
142a0 74 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 ted (when the st
142b0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 atement handle i
142c0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c s passed to [sql
142d0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
142e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e .** [sqlite3_fin
142f0 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a alize()])..**.**
14300 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 See also the [s
14310 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 qlite3_changes()
14320 5d 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 ] interface, the
14330 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 .** [count_chang
14340 65 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 es pragma], and
14350 74 68 65 20 5b 74 6f 74 61 6c 5f 63 68 61 6e 67 the [total_chang
14360 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f es() SQL functio
14370 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 n]..**.** Requir
14380 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
14390 36 31 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a 61] [H12263].**.
143a0 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 ** If a separate
143b0 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 thread makes ch
143c0 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d anges on the sam
143d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
143e0 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b ction.** while [
143f0 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
14400 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e anges()] is runn
14410 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c ing then the val
14420 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 ue.** returned i
14430 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 s unpredictable
14440 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 and not meaningf
14450 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ul..*/.SQLITE_AP
14460 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f I int sqlite3_to
14470 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 tal_changes(sqli
14480 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
14490 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70 PI3REF: Interrup
144a0 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 t A Long-Running
144b0 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20 Query {H12270}
144c0 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30500>.**.** T
144d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 his function cau
144e0 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 ses any pending
144f0 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 database operati
14500 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a on to abort and.
14510 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 ** return at its
14520 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 earliest opport
14530 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 unity. This rout
14540 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 ine is typically
14550 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 .** called in re
14560 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 sponse to a user
14570 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 action such as
14580 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c pressing "Cancel
14590 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 ".** or Ctrl-C w
145a0 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61 here the user wa
145b0 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 nts a long query
145c0 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 operation to ha
145d0 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c lt.** immediatel
145e0 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 y..**.** It is s
145f0 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 afe to call this
14600 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 routine from a
14610 74 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 thread different
14620 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 from the.** thr
14630 65 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72 ead that is curr
14640 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 ently running th
14650 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 e database opera
14660 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a tion. But it.**
14670 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 is not safe to
14680 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e call this routin
14690 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 e with a [databa
146a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
146b0 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64 hat.** is closed
146c0 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20 or might close
146d0 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69 before sqlite3_i
146e0 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72 nterrupt() retur
146f0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ns..**.** If an
14700 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 SQL operation is
14710 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e very nearly fin
14720 69 73 68 65 64 20 61 74 20 74 68 65 20 74 69 6d ished at the tim
14730 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 e when.** sqlite
14740 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 73 3_interrupt() is
14750 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74 called, then it
14760 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 might not have
14770 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a an opportunity.*
14780 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 * to be interrup
14790 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f ted and might co
147a0 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 ntinue to comple
147b0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53 tion..**.** An S
147c0 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 QL operation tha
147d0 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 t is interrupted
147e0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 will return [SQ
147f0 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e LITE_INTERRUPT].
14800 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 .** If the inter
14810 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 rupted SQL opera
14820 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 tion is an INSER
14830 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 T, UPDATE, or DE
14840 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 LETE.** that is
14850 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 inside an explic
14860 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 it transaction,
14870 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 then the entire
14880 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 transaction.** w
14890 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 ill be rolled ba
148a0 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ck automatically
148b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
148c0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 te3_interrupt(D)
148d0 20 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 call is in effe
148e0 63 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 ct until all cur
148f0 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a rently running.*
14900 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 * SQL statements
14910 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f on [database co
14920 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 nnection] D comp
14930 6c 65 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53 lete. Any new S
14940 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
14950 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 that are starte
14960 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 d after the sqli
14970 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
14980 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 call and before
14990 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 the .** running
149a0 73 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 statements reach
149b0 65 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 es zero are inte
149c0 72 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68 rrupted as if th
149d0 65 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 ey had been.** r
149e0 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 unning prior to
149f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 the sqlite3_inte
14a00 72 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e rrupt() call. N
14a10 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ew SQL statement
14a20 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 s.** that are st
14a30 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 arted after the
14a40 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e running statemen
14a50 74 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 t count reaches
14a60 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 zero are.** not
14a70 65 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20 effected by the
14a80 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
14a90 74 28 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 t()..** A call t
14aa0 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 o sqlite3_interr
14ab0 75 70 74 28 44 29 20 74 68 61 74 20 6f 63 63 75 upt(D) that occu
14ac0 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 rs when there ar
14ad0 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 e no running.**
14ae0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 SQL statements i
14af0 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61 s a no-op and ha
14b00 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 s no effect on S
14b10 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
14b20 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 that are starte
14b30 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 d after the sqli
14b40 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
14b50 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a call returns..**
14b60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
14b70 3a 0a 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48 :.** [H12271] [H
14b80 31 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 12272].**.** If
14b90 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
14ba0 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 nection closes w
14bb0 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e hile [sqlite3_in
14bc0 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73 terrupt()].** is
14bd0 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 running then ba
14be0 64 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 d things will li
14bf0 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a kely happen..*/.
14c00 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
14c10 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
14c20 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a t(sqlite3*);../*
14c30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 .** CAPI3REF: De
14c40 74 65 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 termine If An SQ
14c50 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 L Statement Is C
14c60 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d omplete {H10510}
14c70 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S70200>.**.**
14c80 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
14c90 72 65 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67 re useful during
14ca0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e command-line in
14cb0 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 put to determine
14cc0 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 if the.** curre
14cd0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 ntly entered tex
14ce0 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 t seems to form
14cf0 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 a complete SQL s
14d00 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 tatement or.** i
14d10 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 f additional inp
14d20 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 ut is needed bef
14d30 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 ore sending the
14d40 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c text into.** SQL
14d50 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e ite for parsing.
14d60 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
14d70 20 72 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 return 1 if the
14d80 20 69 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a input string.**
14d90 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 61 appears to be a
14da0 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 complete SQL st
14db0 61 74 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74 atement. A stat
14dc0 65 6d 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20 ement is judged
14dd0 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 to be.** complet
14de0 65 20 69 66 20 69 74 20 65 6e 64 73 20 77 69 74 e if it ends wit
14df0 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f h a semicolon to
14e00 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 ken and is not a
14e10 20 70 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20 prefix of a.**
14e20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41 well-formed CREA
14e30 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 TE TRIGGER state
14e40 6d 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e ment. Semicolon
14e50 73 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 s that are embed
14e60 64 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 ded within.** st
14e70 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 ring literals or
14e80 20 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 quoted identifi
14e90 65 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d er names or comm
14ea0 65 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 ents are not.**
14eb0 69 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 independent toke
14ec0 6e 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72 ns (they are par
14ed0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 t of the token i
14ee0 6e 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 n which they are
14ef0 0a 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e .** embedded) an
14f00 64 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f d thus do not co
14f10 75 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 unt as a stateme
14f20 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 nt terminator.
14f30 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e Whitespace.** an
14f40 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20 d comments that
14f50 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c follow the final
14f60 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69 semicolon are i
14f70 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 gnored..**.** Th
14f80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 ese routines ret
14f90 75 72 6e 20 30 20 69 66 20 74 68 65 20 73 74 61 urn 0 if the sta
14fa0 74 65 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70 tement is incomp
14fb0 6c 65 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d lete. If a.** m
14fc0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
14fd0 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c fails, then SQL
14fe0 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
14ff0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
15000 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e se routines do n
15010 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51 4c ot parse the SQL
15020 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 statements thus
15030 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 .** will not det
15040 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c ect syntacticall
15050 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e y incorrect SQL.
15060 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 .**.** If SQLite
15070 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e has not been in
15080 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 itialized using
15090 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
150a0 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a ize()] prior .**
150b0 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c to invoking sql
150c0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
150d0 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 ) then sqlite3_i
150e0 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
150f0 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 nvoked.** automa
15100 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 tically by sqlit
15110 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e e3_complete16().
15120 20 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 If that initia
15130 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a lization fails,.
15140 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 ** then the retu
15150 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 rn value from sq
15160 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 lite3_complete16
15170 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a () will be non-z
15180 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 ero.** regardles
15190 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 s of whether or
151a0 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 not the input SQ
151b0 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a L is complete..*
151c0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
151d0 73 3a 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30 s: [H10511] [H10
151e0 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 512].**.** The i
151f0 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 nput to [sqlite3
15200 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 _complete()] mus
15210 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d t be a zero-term
15220 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20 inated.** UTF-8
15230 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 string..**.** Th
15240 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 e input to [sqli
15250 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 te3_complete16()
15260 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f ] must be a zero
15270 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 -terminated.** U
15280 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 TF-16 string in
15290 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
152a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
152b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d int sqlite3_com
152c0 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 plete(const char
152d0 20 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 *sql);.SQLITE_A
152e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
152f0 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 omplete16(const
15300 76 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a void *sql);../*.
15310 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 ** CAPI3REF: Reg
15320 69 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b ister A Callback
15330 20 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 To Handle SQLIT
15340 45 5f 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48 E_BUSY Errors {H
15350 31 32 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a 12310} <S40400>.
15360 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
15370 6e 65 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61 ne sets a callba
15380 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 ck function that
15390 20 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 might be invoke
153a0 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e d whenever.** an
153b0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
153c0 20 74 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62 to open a datab
153d0 61 73 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 ase table that a
153e0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a nother thread.**
153f0 20 6f 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 or process has
15400 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 locked..**.** If
15410 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 the busy callba
15420 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e ck is NULL, then
15430 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
15440 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f r [SQLITE_IOERR_
15450 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 BLOCKED].** is r
15460 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 eturned immediat
15470 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 ely upon encount
15480 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 ering the lock.
15490 49 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c If the busy call
154a0 62 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e back.** is not N
154b0 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 ULL, then the ca
154c0 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 llback will be i
154d0 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 nvoked with two
154e0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a arguments..**.**
154f0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
15500 65 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c ent to the handl
15510 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 er is a copy of
15520 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 the void* pointe
15530 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68 r which.** is th
15540 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
15550 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 to sqlite3_busy
15560 5f 68 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65 _handler(). The
15570 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
15580 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c to.** the handl
15590 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 er callback is t
155a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d he number of tim
155b0 65 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 es that the busy
155c0 20 68 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 handler has.**
155d0 62 65 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 been invoked for
155e0 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 this locking ev
155f0 65 6e 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ent. If the.**
15600 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 busy callback re
15610 74 75 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f turns 0, then no
15620 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 additional atte
15630 6d 70 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f mpts are made to
15640 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 64 .** access the d
15650 61 74 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c atabase and [SQL
15660 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 ITE_BUSY] or [SQ
15670 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
15680 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e ED] is returned.
15690 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 .** If the callb
156a0 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d ack returns non-
156b0 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 zero, then anoth
156c0 65 72 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 er attempt.** is
156d0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 made to open th
156e0 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 e database for r
156f0 65 61 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63 eading and the c
15700 79 63 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a ycle repeats..**
15710 0a 2a 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65 .** The presence
15720 20 6f 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c of a busy handl
15730 65 72 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 er does not guar
15740 61 6e 74 65 65 20 74 68 61 74 20 69 74 20 77 69 antee that it wi
15750 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a ll be invoked.**
15760 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c when there is l
15770 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 ock contention.
15780 49 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d If SQLite determ
15790 69 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 ines that invoki
157a0 6e 67 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 ng the busy.** h
157b0 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 andler could res
157c0 75 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 ult in a deadloc
157d0 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 k, it will go ah
157e0 65 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b ead and return [
157f0 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 SQLITE_BUSY].**
15800 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 or [SQLITE_IOERR
15810 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 _BLOCKED] instea
15820 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 d of invoking th
15830 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a e busy handler..
15840 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 ** Consider a sc
15850 65 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 enario where one
15860 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
15870 69 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 ing a read lock
15880 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 that.** it is tr
15890 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 ying to promote
158a0 74 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f to a reserved lo
158b0 63 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f ck and.** a seco
158c0 6e 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f nd process is ho
158d0 6c 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 lding a reserved
158e0 20 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 lock that it is
158f0 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 trying.** to pr
15900 6f 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c omote to an excl
15910 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 usive lock. The
15920 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 first process c
15930 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a annot proceed.**
15940 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62 because it is b
15950 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 locked by the se
15960 63 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 cond and the sec
15970 6f 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e ond process cann
15980 6f 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 ot.** proceed be
15990 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 cause it is bloc
159a0 6b 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ked by the first
159b0 2e 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 . If both proce
159c0 73 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 sses.** invoke t
159d0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 he busy handlers
159e0 2c 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d , neither will m
159f0 61 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 ake any progress
15a00 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a . Therefore,.**
15a10 20 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 SQLite returns
15a20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f [SQLITE_BUSY] fo
15a30 72 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 r the first proc
15a40 65 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 ess, hoping that
15a50 20 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e this.** will in
15a60 64 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70 duce the first p
15a70 72 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 rocess to releas
15a80 65 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 e its read lock
15a90 61 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 and allow.** the
15aa0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 second process
15ab0 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a to proceed..**.*
15ac0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 * The default bu
15ad0 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e sy callback is N
15ae0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b ULL..**.** The [
15af0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 SQLITE_BUSY] err
15b00 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 or is converted
15b10 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 to [SQLITE_IOERR
15b20 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 _BLOCKED].** whe
15b30 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 n SQLite is in t
15b40 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c he middle of a l
15b50 61 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e arge transaction
15b60 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a where all the.*
15b70 2a 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e * changes will n
15b80 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 ot fit into the
15b90 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e in-memory cache.
15ba0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a SQLite will.**
15bb0 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 already hold a
15bc0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e RESERVED lock on
15bd0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15be0 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 le, but it needs
15bf0 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 .** to promote t
15c00 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c his lock to EXCL
15c10 55 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 USIVE so that it
15c20 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 can spill cache
15c30 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 .** pages into t
15c40 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
15c50 20 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f without harm to
15c60 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 concurrent.** r
15c70 65 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 eaders. If it i
15c80 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d s unable to prom
15c90 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 ote the lock, th
15ca0 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 en the in-memory
15cb0 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 .** cache will b
15cc0 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 e left in an inc
15cd0 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 onsistent state
15ce0 61 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 and so the error
15cf0 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d .** code is prom
15d00 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 oted from the re
15d10 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 latively benign
15d20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f [SQLITE_BUSY] to
15d30 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 .** the more sev
15d40 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 ere [SQLITE_IOER
15d50 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 R_BLOCKED]. Thi
15d60 73 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f s error code pro
15d70 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 motion.** forces
15d80 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f an automatic ro
15d90 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 llback of the ch
15da0 61 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a anges. See the.
15db0 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 ** <a href="/cvs
15dc0 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 trac/wiki?p=Corr
15dd0 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 uptionFollowingB
15de0 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f usyError">.** Co
15df0 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e rruptionFollowin
15e00 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 gBusyError</a> w
15e10 69 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 iki page for a d
15e20 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 iscussion of why
15e30 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f .** this is impo
15e40 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rtant..**.** The
15e50 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 re can only be a
15e60 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e single busy han
15e70 64 6c 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72 dler defined for
15e80 20 65 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61 each.** [databa
15e90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 se connection].
15ea0 20 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62 Setting a new b
15eb0 75 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61 usy handler clea
15ec0 72 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f rs any.** previo
15ed0 75 73 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72 usly set handler
15ee0 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c . Note that cal
15ef0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 ling [sqlite3_bu
15f00 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a sy_timeout()].**
15f10 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f will also set o
15f20 72 20 63 6c 65 61 72 20 74 68 65 20 62 75 73 79 r clear the busy
15f30 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 handler..**.**
15f40 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 The busy callbac
15f50 6b 20 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b k should not tak
15f60 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68 e any actions wh
15f70 69 63 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a ich modify the.*
15f80 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
15f90 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b ction that invok
15fa0 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ed the busy hand
15fb0 6c 65 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61 ler. Any such a
15fc0 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74 ctions.** result
15fd0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 in undefined be
15fe0 68 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52 havior..** .** R
15ff0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
16000 5b 48 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32 [H12311] [H12312
16010 5d 20 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33 ] [H12314] [H123
16020 31 36 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a 16] [H12318].**.
16030 2a 2a 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 ** A busy handle
16040 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 r must not close
16050 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
16060 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b nnection.** or [
16070 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
16080 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 nt] that invoked
16090 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
160a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
160b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 int sqlite3_bus
160c0 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 y_handler(sqlite
160d0 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 3*, int(*)(void*
160e0 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a ,int), void*);..
160f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
16100 53 65 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f Set A Busy Timeo
16110 75 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30 ut {H12340} <S40
16120 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 410>.**.** This
16130 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b routine sets a [
16140 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e sqlite3_busy_han
16150 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 dler | busy hand
16160 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73 ler] that sleeps
16170 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66 .** for a specif
16180 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 ied amount of ti
16190 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20 me when a table
161a0 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 is locked. The
161b0 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20 handler.** will
161c0 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74 sleep multiple t
161d0 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65 imes until at le
161e0 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 ast "ms" millise
161f0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e conds of sleepin
16200 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75 g.** have accumu
16210 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20 lated. {H12343}
16220 41 66 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69 After "ms" milli
16230 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
16240 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64 ing,.** the hand
16250 6c 65 72 20 72 65 74 75 72 6e 73 20 30 20 77 68 ler returns 0 wh
16260 69 63 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69 ich causes [sqli
16270 74 65 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 te3_step()] to r
16280 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 eturn.** [SQLITE
16290 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 _BUSY] or [SQLIT
162a0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
162b0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 ..**.** Calling
162c0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 this routine wit
162d0 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 h an argument le
162e0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
162f0 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e to zero.** turn
16300 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 s off all busy h
16310 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 andlers..**.** T
16320 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 here can only be
16330 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 a single busy h
16340 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72 andler for a par
16350 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 ticular.** [data
16360 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
16370 20 61 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d any any given m
16380 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68 oment. If anoth
16390 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a er busy handler.
163a0 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 ** was defined
163b0 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f (using [sqlite3_
163c0 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29 busy_handler()])
163d0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
163e0 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e g.** this routin
163f0 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20 62 75 e, that other bu
16400 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c sy handler is cl
16410 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 eared..**.** Req
16420 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
16430 31 32 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20 12341] [H12343]
16440 5b 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49 [H12344].*/.SQLI
16450 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
16460 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 e3_busy_timeout(
16470 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 sqlite3*, int ms
16480 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
16490 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 EF: Convenience
164a0 52 6f 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e Routines For Run
164b0 6e 69 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31 ning Queries {H1
164c0 32 33 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2370} <S10000>.*
164d0 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a *.** Definition:
164e0 20 41 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 A <b>result tab
164f0 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 le</b> is memory
16500 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
16510 63 72 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a created by the.*
16520 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 * [sqlite3_get_t
16530 61 62 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 able()] interfac
16540 65 2e 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 e. A result tab
16550 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a le records the.*
16560 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 * complete query
16570 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e results from on
16580 65 20 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 e or more querie
16590 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 s..**.** The tab
165a0 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 le conceptually
165b0 68 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 has a number of
165c0 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 rows and columns
165d0 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 . But.** these
165e0 6e 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 numbers are not
165f0 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 part of the resu
16600 6c 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e lt table itself.
16610 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 These.** numbe
16620 72 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 rs are obtained
16630 73 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 separately. Let
16640 20 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 N be the number
16650 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 of rows.** and
16660 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 M be the number
16670 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a of columns..**.*
16680 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 * A result table
16690 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 is an array of
166a0 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f pointers to zero
166b0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
166c0 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 8 strings..** Th
166d0 65 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 ere are (N+1)*M
166e0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 elements in the
166f0 61 72 72 61 79 2e 20 20 54 68 65 20 66 69 72 73 array. The firs
16700 74 20 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 t M pointers poi
16710 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 nt.** to zero-te
16720 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 rminated strings
16730 20 74 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 that contain t
16740 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 he names of the
16750 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 columns..** The
16760 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 remaining entrie
16770 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 s all point to q
16780 75 65 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e uery results. N
16790 55 4c 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c ULL values resul
167a0 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 t.** in NULL poi
167b0 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 nters. All othe
167c0 72 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20 r values are in
167d0 74 68 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f their UTF-8 zero
167e0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 -terminated.** s
167f0 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
16800 74 69 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64 tion as returned
16810 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c by [sqlite3_col
16820 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a umn_text()]..**.
16830 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c ** A result tabl
16840 65 20 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20 e might consist
16850 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d of one or more m
16860 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
16870 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 s..** It is not
16880 73 61 66 65 20 74 6f 20 70 61 73 73 20 61 20 72 safe to pass a r
16890 65 73 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65 esult table dire
168a0 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 ctly to [sqlite3
168b0 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 _free()]..** A r
168c0 65 73 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 esult table shou
168d0 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 ld be deallocate
168e0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
168f0 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a _free_table()]..
16900 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d **.** As an exam
16910 70 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c ple of the resul
16920 74 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 t table format,
16930 73 75 70 70 6f 73 65 20 61 20 71 75 65 72 79 20 suppose a query
16940 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 result.** is as
16950 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
16960 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
16970 0a 2a 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20 .** Name
16980 20 20 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 | Age.**
16990 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------
169a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
169b0 2a 20 20 20 20 20 20 20 20 41 6c 69 63 65 20 20 * Alice
169c0 20 20 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 | 43.**
169d0 20 20 20 20 42 6f 62 20 20 20 20 20 20 20 20 20 Bob
169e0 7c 20 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 | 28.** C
169f0 69 6e 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a indy | 21.
16a00 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
16a10 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 quote>.**.** The
16a20 72 65 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d re are two colum
16a30 6e 20 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 n (M==2) and thr
16a40 65 65 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 ee rows (N==3).
16a50 20 54 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 Thus the.** res
16a60 75 6c 74 20 74 61 62 6c 65 20 68 61 73 20 38 20 ult table has 8
16a70 65 6e 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73 entries. Suppos
16a80 65 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 e the result tab
16a90 6c 65 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 le is stored.**
16aa0 69 6e 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 in an array name
16ab0 73 20 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 s azResult. The
16ac0 6e 20 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 n azResult holds
16ad0 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a this content:.*
16ae0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
16af0 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 ><pre>.**
16b00 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d azResult[0]
16b10 20 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 = "Name";.**
16b20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
16b30 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 1;1] = "Age";.**
16b40 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
16b50 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 [2] = "Alice
16b60 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
16b70 65 73 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 esult[3] = "
16b80 34 33 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 43";.** a
16b90 7a 52 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d zResult[4] =
16ba0 20 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 "Bob";.**
16bb0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 azResult[5
16bc0 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 ] = "28";.**
16bd0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
16be0 3b 36 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a ;6] = "Cindy";.*
16bf0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
16c00 74 26 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b t[7] = "21";
16c10 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
16c20 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 kquote>.**.** Th
16c30 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 e sqlite3_get_ta
16c40 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 ble() function e
16c50 76 61 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 valuates one or
16c60 6d 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f more.** semicolo
16c70 6e 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 n-separated SQL
16c80 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 statements in th
16c90 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 e zero-terminate
16ca0 64 20 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e d UTF-8.** strin
16cb0 67 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72 g of its 2nd par
16cc0 61 6d 65 74 65 72 2e 20 20 49 74 20 72 65 74 75 ameter. It retu
16cd0 72 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 rns a result tab
16ce0 6c 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 le to the.** poi
16cf0 6e 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74 nter given in it
16d00 73 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e s 3rd parameter.
16d10 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 .**.** After the
16d20 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
16d30 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 n has finished u
16d40 73 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c sing the result,
16d50 20 69 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 it should.** pa
16d60 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 ss the pointer t
16d70 6f 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 o the result tab
16d80 6c 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 le to sqlite3_fr
16d90 65 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 ee_table() in or
16da0 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 der to.** releas
16db0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 e the memory tha
16dc0 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 t was malloced.
16dd0 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 Because of the
16de0 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 way the.** [sqli
16df0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 te3_malloc()] ha
16e00 70 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c ppens within sql
16e10 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 ite3_get_table()
16e20 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a , the calling.**
16e30 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e function must n
16e40 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b ot try to call [
16e50 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 sqlite3_free()]
16e60 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a directly. Only.
16e70 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 ** [sqlite3_free
16e80 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c _table()] is abl
16e90 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 e to release the
16ea0 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 memory properly
16eb0 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a and safely..**.
16ec0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 ** The sqlite3_g
16ed0 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72 et_table() inter
16ee0 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e face is implemen
16ef0 74 65 64 20 61 73 20 61 20 77 72 61 70 70 65 72 ted as a wrapper
16f00 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 around.** [sqli
16f10 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 te3_exec()]. Th
16f20 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 e sqlite3_get_ta
16f30 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f ble() routine do
16f40 65 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65 es not have acce
16f50 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 ss.** to any int
16f60 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 ernal data struc
16f70 74 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e tures of SQLite.
16f80 20 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 It uses only t
16f90 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 he public.** int
16fa0 65 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 68 erface defined h
16fb0 65 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 ere. As a conse
16fc0 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 quence, errors t
16fd0 68 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65 hat occur in the
16fe0 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65 .** wrapper laye
16ff0 72 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 r outside of the
17000 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 internal [sqlit
17010 65 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 e3_exec()] call
17020 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 are not.** refle
17030 63 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 cted in subseque
17040 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c nt calls to [sql
17050 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 ite3_errcode()]
17060 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d or [sqlite3_errm
17070 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 sg()]..**.** Req
17080 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
17090 31 32 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20 12371] [H12373]
170a0 5b 48 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36 [H12374] [H12376
170b0 5d 20 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33 ] [H12379] [H123
170c0 38 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 82].*/.SQLITE_AP
170d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 I int sqlite3_ge
170e0 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 t_table(. sqlit
170f0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
17100 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 /* An open data
17110 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 base */. const
17120 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 char *zSql,
17130 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 /* SQL to be eva
17140 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 luated */. char
17150 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 ***pazResult,
17160 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 /* Results of
17170 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 the query */. i
17180 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 nt *pnRow,
17190 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
171a0 66 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 f result rows wr
171b0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
171c0 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 int *pnColumn,
171d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
171e0 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e of result column
171f0 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a s written here *
17200 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 /. char **pzErr
17210 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 msg /* Err
17220 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 or msg written h
17230 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 ere */.);.SQLITE
17240 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
17250 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 3_free_table(cha
17260 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a r **result);../*
17270 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f .** CAPI3REF: Fo
17280 72 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 rmatted String P
17290 72 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e rinting Function
172a0 73 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 s {H17400} <S700
172b0 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 00><S20000>.**.*
172c0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
172d0 20 61 72 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73 are work-alikes
172e0 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 of the "printf(
172f0 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e )" family of fun
17300 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 ctions.** from t
17310 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 he standard C li
17320 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 brary..**.** The
17330 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
17340 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 () and sqlite3_v
17350 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e mprintf() routin
17360 65 73 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a es write their.*
17370 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d * results into m
17380 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
17390 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
173a0 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 loc()]..** The s
173b0 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 trings returned
173c0 62 79 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 by these two rou
173d0 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a tines should be.
173e0 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b ** released by [
173f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e sqlite3_free()].
17400 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 Both routines
17410 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c return a.** NULL
17420 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c pointer if [sql
17430 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 ite3_malloc()] i
17440 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f s unable to allo
17450 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d cate enough.** m
17460 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 emory to hold th
17470 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 e resulting stri
17480 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c ng..**.** In sql
17490 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 ite3_snprintf()
174a0 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c routine is simil
174b0 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 ar to "snprintf(
174c0 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 )" from.** the s
174d0 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
174e0 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 y. The result i
174f0 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 s written into t
17500 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 he.** buffer sup
17510 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 65 63 plied as the sec
17520 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 ond parameter wh
17530 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 ose size is give
17540 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 n by.** the firs
17550 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 t parameter. Not
17560 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 e that the order
17570 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 of the.** first
17580 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 two parameters
17590 69 73 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d is reversed from
175a0 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 snprintf(). Th
175b0 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 is is an.** hist
175c0 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 orical accident
175d0 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 that cannot be f
175e0 69 78 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 ixed without bre
175f0 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 aking.** backwar
17600 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ds compatibility
17610 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 . Note also tha
17620 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e t sqlite3_snprin
17630 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 tf().** returns
17640 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 a pointer to its
17650 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 buffer instead
17660 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
17670 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 .** characters a
17680 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 ctually written
17690 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e into the buffer.
176a0 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a We admit that.
176b0 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
176c0 20 63 68 61 72 61 63 74 65 72 73 20 77 72 69 74 characters writ
176d0 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d ten would be a m
176e0 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 ore useful retur
176f0 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 n.** value but w
17700 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 e cannot change
17710 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
17720 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e on of sqlite3_sn
17730 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 printf().** now
17740 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 without breaking
17750 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a compatibility..
17760 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 **.** As long as
17770 20 74 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 the buffer size
17780 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
17790 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 zero, sqlite3_s
177a0 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 nprintf().** gua
177b0 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 rantees that the
177c0 20 62 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 buffer is alway
177d0 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 s zero-terminate
177e0 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a d. The first.**
177f0 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 parameter "n" i
17800 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 s the total size
17810 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 of the buffer,
17820 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 including space
17830 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 for.** the zero
17840 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 terminator. So
17850 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 the longest stri
17860 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 ng that can be c
17870 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 ompletely.** wri
17880 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 tten will be n-1
17890 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a characters..**.
178a0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
178b0 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 s all implement
178c0 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 some additional
178d0 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 formatting.** op
178e0 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 tions that are u
178f0 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 seful for constr
17900 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 ucting SQL state
17910 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 ments..** All of
17920 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 the usual print
17930 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f f() formatting o
17940 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 ptions apply. I
17950 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 n addition, ther
17960 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 e.** is are "%q"
17970 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 , "%Q", and "%z"
17980 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 options..**.**
17990 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f The %q option wo
179a0 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 rks like %s in t
179b0 68 61 74 20 69 74 20 73 75 62 73 74 69 74 75 74 hat it substitut
179c0 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e es a null-termin
179d0 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 ated.** string f
179e0 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 rom the argument
179f0 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 list. But %q a
17a00 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 lso doubles ever
17a10 79 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 y '\'' character
17a20 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 ..** %q is desig
17a30 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 ned for use insi
17a40 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 de a string lite
17a50 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e ral. By doublin
17a60 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 g each '\''.** c
17a70 68 61 72 61 63 74 65 72 20 69 74 20 65 73 63 61 haracter it esca
17a80 70 65 73 20 74 68 61 74 20 63 68 61 72 61 63 74 pes that charact
17a90 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 er and allows it
17aa0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 to be inserted
17ab0 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 into.** the stri
17ac0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 ng..**.** For ex
17ad0 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 ample, assume th
17ae0 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c e string variabl
17af0 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 e zText contains
17b00 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 text as follows
17b10 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
17b20 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 ote><pre>.** ch
17b30 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 ar *zText = "It'
17b40 73 20 61 20 68 61 70 70 79 20 64 61 79 21 22 3b s a happy day!";
17b50 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
17b60 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e kquote>.**.** On
17b70 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 e can use this t
17b80 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 ext in an SQL st
17b90 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f atement as follo
17ba0 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
17bb0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
17bc0 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c char *zSQL = sql
17bd0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e ite3_mprintf("IN
17be0 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 SERT INTO table
17bf0 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a VALUES('%q')", z
17c00 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 Text);.** sqlit
17c10 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c e3_exec(db, zSQL
17c20 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 , 0, 0, 0);.**
17c30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 sqlite3_free(zSQ
17c40 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 L);.** </pre></b
17c50 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
17c60 20 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20 Because the %q
17c70 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 format string is
17c80 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 used, the '\''
17c90 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 character in zTe
17ca0 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 xt.** is escaped
17cb0 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e and the SQL gen
17cc0 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c erated is as fol
17cd0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lows:.**.** <blo
17ce0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
17cf0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 INSERT INTO ta
17d00 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 ble1 VALUES('It'
17d10 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27 's a happy day!'
17d20 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ).** </pre></blo
17d30 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 ckquote>.**.** T
17d40 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 his is correct.
17d50 20 48 61 64 20 77 65 20 75 73 65 64 20 25 73 20 Had we used %s
17d60 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 instead of %q, t
17d70 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c he generated SQL
17d80 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c .** would have l
17d90 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a ooked like this:
17da0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
17db0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 te><pre>.** INS
17dc0 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 ERT INTO table1
17dd0 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 VALUES('It's a h
17de0 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 appy day!');.**
17df0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
17e00 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 te>.**.** This s
17e10 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 econd example is
17e20 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 an SQL syntax e
17e30 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 rror. As a gene
17e40 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f ral rule you sho
17e50 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 uld.** always us
17e60 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 e %q instead of
17e70 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e %s when insertin
17e80 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 g text into a st
17e90 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a ring literal..**
17ea0 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f .** The %Q optio
17eb0 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 n works like %q
17ec0 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 except it also a
17ed0 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 dds single quote
17ee0 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 s around.** the
17ef0 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 outside of the t
17f00 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 otal string. Ad
17f10 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 ditionally, if t
17f20 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 he parameter in
17f30 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 the.** argument
17f40 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 list is a NULL p
17f50 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 ointer, %Q subst
17f60 69 74 75 74 65 73 20 74 68 65 20 74 65 78 74 20 itutes the text
17f70 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a "NULL" (without.
17f80 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 ** single quotes
17f90 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 ) in place of th
17fa0 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f e %Q option. So
17fb0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f , for example, o
17fc0 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a ne could say:.**
17fd0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
17fe0 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a <pre>.** char *
17ff0 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d zSQL = sqlite3_m
18000 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 printf("INSERT I
18010 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 NTO table VALUES
18020 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a (%Q)", zText);.*
18030 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 * sqlite3_exec(
18040 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 db, zSQL, 0, 0,
18050 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 0);.** sqlite3_
18060 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c free(zSQL);.** <
18070 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 /pre></blockquot
18080 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 e>.**.** The cod
18090 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e e above will ren
180a0 64 65 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 der a correct SQ
180b0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 L statement in t
180c0 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 he zSQL.** varia
180d0 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 ble even if the
180e0 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 zText variable i
180f0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
18100 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 ..**.** The "%z"
18110 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 formatting opti
18120 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 on works exactly
18130 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 like "%s" with
18140 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 the.** addition
18150 74 68 61 74 20 61 66 74 65 72 20 74 68 65 20 73 that after the s
18160 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 tring has been r
18170 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 ead and copied i
18180 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c nto.** the resul
18190 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 t, [sqlite3_free
181a0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e ()] is called on
181b0 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
181c0 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52 g. {END}.**.** R
181d0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
181e0 5b 48 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36 [H17403] [H17406
181f0 5d 20 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51 ] [H17407].*/.SQ
18200 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
18210 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 qlite3_mprintf(c
18220 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
18230 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 .SQLITE_API char
18240 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e *sqlite3_vmprin
18250 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 tf(const char*,
18260 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 va_list);.SQLITE
18270 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 _API char *sqlit
18280 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c e3_snprintf(int,
18290 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
182a0 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *, ...);../*.**
182b0 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 CAPI3REF: Memory
182c0 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 Allocation Subs
182d0 79 73 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c ystem {H17300} <
182e0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20000>.**.** Th
182f0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75 e SQLite core u
18300 73 65 73 20 74 68 65 73 65 20 74 68 72 65 65 20 ses these three
18310 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c routines for all
18320 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 of its own.** i
18330 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 nternal memory a
18340 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e llocation needs.
18350 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 "Core" in the p
18360 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 revious sentence
18370 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 .** does not inc
18380 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 lude operating-s
18390 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 ystem specific V
183a0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
183b0 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f n. The.** Windo
183c0 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 74 69 ws VFS uses nati
183d0 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 ve malloc() and
183e0 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 free() for some
183f0 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a operations..**.*
18400 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 * The sqlite3_ma
18410 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72 lloc() routine r
18420 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
18430 20 74 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f to a block.** o
18440 66 20 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 f memory at leas
18450 74 20 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e t N bytes in len
18460 67 74 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20 gth, where N is
18470 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a the parameter..*
18480 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c * If sqlite3_mal
18490 6c 6f 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20 loc() is unable
184a0 74 6f 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63 to obtain suffic
184b0 69 65 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d ient free.** mem
184c0 6f 72 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20 ory, it returns
184d0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 a NULL pointer.
184e0 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 If the paramete
184f0 72 20 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 r N to.** sqlite
18500 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 3_malloc() is ze
18510 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74 ro or negative t
18520 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c hen sqlite3_mall
18530 6f 63 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 oc() returns.**
18540 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
18550 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 **.** Calling sq
18560 6c 69 74 65 33 5f 66 72 65 65 28 29 20 77 69 74 lite3_free() wit
18570 68 20 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76 h a pointer prev
18580 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a iously returned.
18590 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 ** by sqlite3_ma
185a0 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 lloc() or sqlite
185b0 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 3_realloc() rele
185c0 61 73 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 ases that memory
185d0 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d so.** that it m
185e0 69 67 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 ight be reused.
185f0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65 The sqlite3_fre
18600 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a e() routine is.*
18610 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 * a no-op if is
18620 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 called with a NU
18630 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 LL pointer. Pas
18640 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e sing a NULL poin
18650 74 65 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ter.** to sqlite
18660 33 5f 66 72 65 65 28 29 20 69 73 20 68 61 72 6d 3_free() is harm
18670 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62 65 69 less. After bei
18680 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 ng freed, memory
18690 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 .** should neith
186a0 65 72 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77 er be read nor w
186b0 72 69 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 ritten. Even re
186c0 61 64 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 ading previously
186d0 20 66 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 freed.** memory
186e0 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e might result in
186f0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 a segmentation
18700 66 61 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 fault or other s
18710 65 76 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 evere error..**
18720 4d 65 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f Memory corruptio
18730 6e 2c 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f n, a segmentatio
18740 6e 20 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 n fault, or othe
18750 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a r severe error.*
18760 2a 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 * might result i
18770 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 f sqlite3_free()
18780 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 is called with
18790 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 a non-NULL point
187a0 65 72 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e er that.** was n
187b0 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ot obtained from
187c0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
187d0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 ) or sqlite3_rea
187e0 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 lloc()..**.** Th
187f0 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f e sqlite3_reallo
18800 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 c() interface at
18810 74 65 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65 tempts to resize
18820 20 61 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f a.** prior memo
18830 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f ry allocation to
18840 20 62 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62 be at least N b
18850 79 74 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 ytes, where N is
18860 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 the.** second p
18870 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d arameter. The m
18880 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
18890 20 74 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69 to be resized i
188a0 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 s the first.** p
188b0 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68 arameter. If th
188c0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
188d0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 r to sqlite3_rea
188e0 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e lloc().** is a N
188f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e ULL pointer then
18900 20 69 74 73 20 62 65 68 61 76 69 6f 72 20 69 73 its behavior is
18910 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 identical to ca
18920 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
18930 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 _malloc(N) where
18940 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 N is the second
18950 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
18960 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e lite3_realloc().
18970 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e .** If the secon
18980 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 d parameter to s
18990 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
189a0 20 69 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e is zero or.** n
189b0 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 egative then the
189c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 behavior is exa
189d0 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 ctly the same as
189e0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 calling.** sqli
189f0 74 65 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 te3_free(P) wher
18a00 65 20 50 20 69 73 20 74 68 65 20 66 69 72 73 74 e P is the first
18a10 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
18a20 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e lite3_realloc().
18a30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c .** sqlite3_real
18a40 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 loc() returns a
18a50 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d pointer to a mem
18a60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ory allocation.*
18a70 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 * of at least N
18a80 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 bytes in size or
18a90 20 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 NULL if suffici
18aa0 65 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e ent memory is un
18ab0 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 available..** If
18ac0 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f M is the size o
18ad0 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f f the prior allo
18ae0 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e cation, then min
18af0 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f (N,M) bytes.** o
18b00 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f f the prior allo
18b10 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 cation are copie
18b20 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e d into the begin
18b30 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 ning of buffer r
18b40 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 eturned.** by sq
18b50 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 lite3_realloc()
18b60 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c and the prior al
18b70 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
18b80 64 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 d..** If sqlite3
18b90 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 _realloc() retur
18ba0 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 ns NULL, then th
18bb0 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 e prior allocati
18bc0 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 on.** is not fre
18bd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 ed..**.** The me
18be0 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 mory returned by
18bf0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
18c00 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 ) and sqlite3_re
18c10 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c alloc().** is al
18c20 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 ways aligned to
18c30 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79 at least an 8 by
18c40 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e te boundary. {EN
18c50 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 D}.**.** The def
18c60 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ault implementat
18c70 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
18c80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
18c90 73 79 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 system uses.** t
18ca0 68 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 he malloc(), rea
18cb0 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 lloc() and free(
18cc0 29 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 ) provided by th
18cd0 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 e standard C lib
18ce0 72 61 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 rary..** {H17382
18cf0 7d 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 } However, if SQ
18d00 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
18d10 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c with the.** SQL
18d20 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d ITE_MEMORY_SIZE=
18d30 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 <i>NNN</i> C pre
18d40 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 processor macro
18d50 28 77 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 (where <i>NNN</i
18d60 3e 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 >.** is an integ
18d70 65 72 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 er), then SQLite
18d80 20 63 72 65 61 74 65 20 61 20 73 74 61 74 69 63 create a static
18d90 20 61 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61 array of at lea
18da0 73 74 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e st.** <i>NNN</i>
18db0 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 bytes in size a
18dc0 6e 64 20 75 73 65 73 20 74 68 61 74 20 61 72 72 nd uses that arr
18dd0 61 79 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 ay for all of it
18de0 73 20 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d s dynamic.** mem
18df0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e ory allocation n
18e00 65 65 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 eeds. {END} Add
18e10 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 itional memory a
18e20 6c 6c 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 llocator options
18e30 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64 .** may be added
18e40 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
18e50 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 ses..**.** In SQ
18e60 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 Lite version 3.5
18e70 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 .0 and 3.5.1, it
18e80 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f was possible to
18e90 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 define.** the S
18ea0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
18eb0 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 Y_ALLOCATION whi
18ec0 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74 ch would cause t
18ed0 68 65 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 he built-in.** i
18ee0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
18ef0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
18f00 74 6f 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 to be omitted.
18f10 54 68 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a That capability.
18f20 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 ** is no longer
18f30 70 72 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 provided. Only
18f40 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 built-in memory
18f50 61 6c 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 allocators can b
18f60 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 e used..**.** Th
18f70 65 20 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 e Windows OS int
18f80 65 72 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c erface layer cal
18f90 6c 73 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d ls.** the system
18fa0 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 malloc() and fr
18fb0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 77 68 ee() directly wh
18fc0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a en converting.**
18fd0 20 66 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 filenames betwe
18fe0 65 6e 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 en the UTF-8 enc
18ff0 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 53 51 oding used by SQ
19000 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 Lite.** and what
19010 65 76 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e ever filename en
19020 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 20 62 coding is used b
19030 79 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 y the particular
19040 20 57 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 Windows.** inst
19050 61 6c 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 allation. Memor
19060 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
19070 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 ors are detected
19080 2c 20 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 , but.** they ar
19090 65 20 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 e reported back
190a0 61 73 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f as [SQLITE_CANTO
190b0 50 45 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 PEN] or.** [SQLI
190c0 54 45 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 TE_IOERR] rather
190d0 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f than [SQLITE_NO
190e0 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 MEM]..**.** Requ
190f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
19100 37 33 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b 7303] [H17304] [
19110 48 31 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d H17305] [H17306]
19120 20 5b 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31 [H17310] [H1731
19130 32 5d 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37 2] [H17315] [H17
19140 33 31 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d 318].** [H17321]
19150 20 5b 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32 [H17322] [H1732
19160 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 3].**.** The poi
19170 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74 nter arguments t
19180 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
19190 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
191a0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75 realloc()].** mu
191b0 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c st be either NUL
191c0 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65 L or else pointe
191d0 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d rs obtained from
191e0 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f a prior.** invo
191f0 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 cation of [sqlit
19200 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 e3_malloc()] or
19210 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
19220 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a ()] that have.**
19230 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 not yet been re
19240 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 leased..**.** Th
19250 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
19260 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 st not read or w
19270 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 rite any part of
19280 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d .** a block of m
19290 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 emory after it h
192a0 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 as been released
192b0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 using.** [sqlit
192c0 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 e3_free()] or [s
192d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
192e0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
192f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d void *sqlite3_m
19300 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 alloc(int);.SQLI
19310 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
19320 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 ite3_realloc(voi
19330 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 d*, int);.SQLITE
19340 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
19350 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 3_free(void*);..
19360 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
19370 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 Memory Allocator
19380 20 53 74 61 74 69 73 74 69 63 73 20 7b 48 31 37 Statistics {H17
19390 33 37 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a 370} <S30210>.**
193a0 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 .** SQLite provi
193b0 64 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e des these two in
193c0 74 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 terfaces for rep
193d0 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 orting on the st
193e0 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b atus.** of the [
193f0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
19400 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 ], [sqlite3_free
19410 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
19420 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 3_realloc()].**
19430 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 routines, which
19440 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 form the built-i
19450 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 n memory allocat
19460 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
19470 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
19480 73 3a 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b s:.** [H17371] [
19490 48 31 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d H17373] [H17374]
194a0 20 5b 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c [H17375].*/.SQL
194b0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
194c0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 int64 sqlite3_me
194d0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b mory_used(void);
194e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
194f0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
19500 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 3_memory_highwat
19510 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 er(int resetFlag
19520 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
19530 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f EF: Pseudo-Rando
19540 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 m Number Generat
19550 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 or {H17390} <S20
19560 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 000>.**.** SQLit
19570 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 e contains a hig
19580 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f h-quality pseudo
19590 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 -random number g
195a0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 enerator (PRNG)
195b0 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 used to.** selec
195c0 74 20 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 t random [ROWID
195d0 7c 20 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 | ROWIDs] when i
195e0 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63 nserting new rec
195f0 6f 72 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c ords into a tabl
19600 65 20 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 e that.** alread
19610 79 20 75 73 65 73 20 74 68 65 20 6c 61 72 67 65 y uses the large
19620 73 74 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 st possible [ROW
19630 49 44 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69 ID]. The PRNG i
19640 73 20 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a s also used for.
19650 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 ** the build-in
19660 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e random() and ran
19670 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 domblob() SQL fu
19680 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 nctions. This i
19690 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a nterface allows.
196a0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ** applications
196b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 61 to access the sa
196c0 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 me PRNG for othe
196d0 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a r purposes..**.*
196e0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 * A call to this
196f0 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 routine stores
19700 4e 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f N bytes of rando
19710 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 mness into buffe
19720 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 r P..**.** The f
19730 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 irst time this r
19740 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 outine is invoke
19750 64 20 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e d (either intern
19760 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 ally or by.** th
19770 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 e application) t
19780 68 65 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 he PRNG is seede
19790 64 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 d using randomne
197a0 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 ss obtained.** f
197b0 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e rom the xRandomn
197c0 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 ess method of th
197d0 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 e default [sqlit
197e0 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a e3_vfs] object..
197f0 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 ** On all subseq
19800 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 uent invocations
19810 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e , the pseudo-ran
19820 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 domness is gener
19830 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c ated.** internal
19840 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 ly and without r
19850 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b ecourse to the [
19860 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 sqlite3_vfs] xRa
19870 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 ndomness.** meth
19880 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 od..**.** Requir
19890 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 ements:.** [H173
198a0 39 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 92].*/.SQLITE_AP
198b0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
198c0 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c andomness(int N,
198d0 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a void *P);../*.*
198e0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 * CAPI3REF: Comp
198f0 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 ile-Time Authori
19900 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 zation Callbacks
19910 20 7b 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30 {H12500} <S7010
19920 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
19930 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 utine registers
19940 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c a authorizer cal
19950 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 lback with a par
19960 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 ticular.** [data
19970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
19980 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 , supplied in th
19990 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
199a0 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 ..** The authori
199b0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 zer callback is
199c0 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 invoked as SQL s
199d0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 tatements are be
199e0 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 ing compiled.**
199f0 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 by [sqlite3_prep
19a00 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 are()] or its va
19a10 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f riants [sqlite3_
19a20 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a prepare_v2()],.*
19a30 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
19a40 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c re16()] and [sql
19a50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
19a60 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 2()]. At variou
19a70 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 s.** points duri
19a80 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 ng the compilati
19a90 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c on process, as l
19aa0 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 ogic is being cr
19ab0 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 eated.** to perf
19ac0 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 orm various acti
19ad0 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 ons, the authori
19ae0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 zer callback is
19af0 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 invoked to.** se
19b00 65 20 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f e if those actio
19b10 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 ns are allowed.
19b20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 The authorizer
19b30 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a callback should.
19b40 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 ** return [SQLIT
19b50 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74 E_OK] to allow t
19b60 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 he action, [SQLI
19b70 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 TE_IGNORE] to di
19b80 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 sallow the.** sp
19b90 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75 ecific action bu
19ba0 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 t allow the SQL
19bb0 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e statement to con
19bc0 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 tinue to be.** c
19bd0 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c ompiled, or [SQL
19be0 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 ITE_DENY] to cau
19bf0 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 53 51 se the entire SQ
19c00 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 L statement to b
19c10 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69 e.** rejected wi
19c20 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66 th an error. If
19c30 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
19c40 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
19c50 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 .** any value ot
19c60 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 her than [SQLITE
19c70 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 _IGNORE], [SQLIT
19c80 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 E_OK], or [SQLIT
19c90 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20 E_DENY].** then
19ca0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 the [sqlite3_pre
19cb0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 pare_v2()] or eq
19cc0 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 uivalent call th
19cd0 61 74 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 at triggered.**
19ce0 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 the authorizer w
19cf0 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
19d00 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
19d10 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
19d20 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
19d30 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 [SQLITE_OK], tha
19d40 74 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 t means the oper
19d50 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 ation.** request
19d60 65 64 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 ed is ok. When
19d70 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 the callback ret
19d80 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e urns [SQLITE_DEN
19d90 59 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 Y], the.** [sqli
19da0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
19db0 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 ] or equivalent
19dc0 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 call that trigge
19dd0 72 65 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f red the.** autho
19de0 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 rizer will fail
19df0 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 with an error me
19e00 73 73 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 ssage explaining
19e10 20 74 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 that.** access
19e20 69 73 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a is denied. .**.*
19e30 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 * The first para
19e40 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 meter to the aut
19e50 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
19e60 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
19e70 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d e third.** param
19e80 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 eter to the sqli
19e90 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
19ea0 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 er() interface.
19eb0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d The second param
19ec0 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 eter.** to the c
19ed0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e allback is an in
19ee0 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f teger [SQLITE_CO
19ef0 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 PY | action code
19f00 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73 ] that specifies
19f10 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c .** the particul
19f20 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 ar action to be
19f30 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20 authorized. The
19f40 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 third through si
19f50 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a xth parameters.*
19f60 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 * to the callbac
19f70 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 k are zero-termi
19f80 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 nated strings th
19f90 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 at contain addit
19fa0 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73 ional.** details
19fb0 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f about the actio
19fc0 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a n to be authoriz
19fd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
19fe0 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 action code is
19ff0 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a [SQLITE_READ].**
1a000 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 and the callbac
1a010 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 k returns [SQLIT
1a020 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 E_IGNORE] then t
1a030 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 he.** [prepared
1a040 73 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 statement] state
1a050 6d 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 ment is construc
1a060 74 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 ted to substitut
1a070 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 e.** a NULL valu
1a080 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 e in place of th
1a090 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 e table column t
1a0a0 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a hat would have.*
1a0b0 2a 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b * been read if [
1a0c0 53 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 SQLITE_OK] had b
1a0d0 65 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 een returned. T
1a0e0 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 he [SQLITE_IGNOR
1a0f0 45 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e E].** return can
1a100 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 be used to deny
1a110 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 an untrusted us
1a120 65 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 er access to ind
1a130 69 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d ividual.** colum
1a140 6e 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a ns of a table..*
1a150 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 * If the action
1a160 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f code is [SQLITE_
1a170 44 45 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20 DELETE] and the
1a180 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
1a190 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f .** [SQLITE_IGNO
1a1a0 52 45 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 RE] then the [DE
1a1b0 4c 45 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 LETE] operation
1a1c0 70 72 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 proceeds but the
1a1d0 0a 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 .** [truncate op
1a1e0 74 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 timization] is d
1a1f0 69 73 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 isabled and all
1a200 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 rows are deleted
1a210 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a individually..*
1a220 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a *.** An authoriz
1a230 65 72 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 er is used when
1a240 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
1a250 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a | preparing].**
1a260 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
1a270 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65 from an untruste
1a280 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73 d source, to ens
1a290 75 72 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ure that the SQL
1a2a0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64 statements.** d
1a2b0 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63 o not try to acc
1a2c0 65 73 73 20 64 61 74 61 20 74 68 65 79 20 61 72 ess data they ar
1a2d0 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f e not allowed to
1a2e0 20 73 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 see, or that th
1a2f0 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79 ey do not.** try
1a300 20 74 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69 to execute mali
1a310 63 69 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 cious statements
1a320 20 74 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 that damage the
1a330 20 64 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a database. For.
1a340 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 ** example, an a
1a350 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 pplication may a
1a360 6c 6c 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 llow a user to e
1a370 6e 74 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a nter arbitrary.*
1a380 2a 20 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f * SQL queries fo
1a390 72 20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 r evaluation by
1a3a0 61 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 a database. But
1a3b0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
1a3c0 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e does.** not wan
1a3d0 74 20 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 t the user to be
1a3e0 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 able to make ar
1a3f0 62 69 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 bitrary changes
1a400 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 to the.** databa
1a410 73 65 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a se. An authoriz
1a420 65 72 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 er could then be
1a430 20 70 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 put in place wh
1a440 69 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d ile the.** user-
1a450 65 6e 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 entered SQL is b
1a460 65 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 eing [sqlite3_pr
1a470 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 65 64 epare | prepared
1a480 5d 20 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c ] that.** disall
1a490 6f 77 73 20 65 76 65 72 79 74 68 69 6e 67 20 65 ows everything e
1a4a0 78 63 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73 xcept [SELECT] s
1a4b0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
1a4c0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 Applications th
1a4d0 61 74 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 at need to proce
1a4e0 73 73 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72 ss SQL from untr
1a4f0 75 73 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a usted sources.**
1a500 20 6d 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 might also cons
1a510 69 64 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 ider lowering re
1a520 73 6f 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73 source limits us
1a530 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d ing [sqlite3_lim
1a540 69 74 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d it()].** and lim
1a550 69 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 iting database s
1a560 69 7a 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d ize using the [m
1a570 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b ax_page_count] [
1a580 50 52 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64 PRAGMA].** in ad
1a590 64 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 dition to using
1a5a0 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a an authorizer..*
1a5b0 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67 *.** Only a sing
1a5c0 6c 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 le authorizer ca
1a5d0 6e 20 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e n be in place on
1a5e0 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a database conn
1a5f0 65 63 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 ection.** at a t
1a600 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 ime. Each call
1a610 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 to sqlite3_set_a
1a620 75 74 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69 uthorizer overri
1a630 64 65 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 des the.** previ
1a640 6f 75 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62 ous call. Disab
1a650 6c 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 le the authorize
1a660 72 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 r by installing
1a670 61 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e a NULL callback.
1a680 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a .** The authoriz
1a690 65 72 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 er is disabled b
1a6a0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a y default..**.**
1a6b0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 The authorizer
1a6c0 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f callback must no
1a6d0 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 t do anything th
1a6e0 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a at will modify.*
1a6f0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
1a700 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 onnection that i
1a710 6e 76 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f nvoked the autho
1a720 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a rizer callback..
1a730 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 ** Note that [sq
1a740 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1a750 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
1a760 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f _step()] both mo
1a770 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 dify their.** da
1a780 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1a790 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 ns for the meani
1a7a0 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 ng of "modify" i
1a7b0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
1a7c0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71 ..**.** When [sq
1a7d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1a7e0 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 ()] is used to p
1a7f0 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 repare a stateme
1a800 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 nt, the.** state
1a810 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 ment might be re
1a820 2d 70 72 65 70 61 72 65 64 20 64 75 72 69 6e 67 -prepared during
1a830 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1a840 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 ] due to a .** s
1a850 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 chema change. H
1a860 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63 ence, the applic
1a870 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 ation should ens
1a880 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ure that the.**
1a890 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a correct authoriz
1a8a0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 er callback rema
1a8b0 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72 ins in place dur
1a8c0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ing the [sqlite3
1a8d0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _step()]..**.**
1a8e0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 75 Note that the au
1a8f0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1a900 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c k is invoked onl
1a910 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c y during.** [sql
1a920 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
1a930 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e or its variants.
1a940 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 Authorization
1a950 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 is not.** perfor
1a960 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 med during state
1a970 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 ment evaluation
1a980 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 in [sqlite3_step
1a990 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61 ()], unless.** a
1a9a0 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65 20 s stated in the
1a9b0 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61 previous paragra
1a9c0 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ph, sqlite3_step
1a9d0 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71 () invokes.** sq
1a9e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1a9f0 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65 20 () to reprepare
1aa00 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 a statement afte
1aa10 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 r a schema chang
1aa20 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 e..**.** Require
1aa30 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30 ments:.** [H1250
1aa40 31 5d 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 32 1] [H12502] [H12
1aa50 35 30 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48 503] [H12504] [H
1aa60 31 32 35 30 35 5d 20 5b 48 31 32 35 30 36 5d 20 12505] [H12506]
1aa70 5b 48 31 32 35 30 37 5d 20 5b 48 31 32 35 31 30 [H12507] [H12510
1aa80 5d 0a 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48 ].** [H12511] [H
1aa90 31 32 35 31 32 5d 20 5b 48 31 32 35 32 30 5d 20 12512] [H12520]
1aaa0 5b 48 31 32 35 32 31 5d 20 5b 48 31 32 35 32 32 [H12521] [H12522
1aab0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
1aac0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f int sqlite3_set_
1aad0 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 authorizer(. sq
1aae0 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a lite3*,. int (*
1aaf0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 xAuth)(void*,int
1ab00 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
1ab10 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
1ab20 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
1ab30 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 ),. void *pUser
1ab40 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 Data.);../*.** C
1ab50 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 API3REF: Authori
1ab60 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 zer Return Codes
1ab70 20 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 {H12590} <H1250
1ab80 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 0>.**.** The [sq
1ab90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1aba0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 izer | authorize
1abb0 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 r callback funct
1abc0 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 ion] must.** ret
1abd0 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 urn either [SQLI
1abe0 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 TE_OK] or one of
1abf0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 these two const
1ac00 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a ants in order.**
1ac10 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 to signal SQLit
1ac20 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
1ac30 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 the action is p
1ac40 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 ermitted. See t
1ac50 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 he.** [sqlite3_s
1ac60 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 et_authorizer |
1ac70 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d authorizer docum
1ac80 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 entation] for ad
1ac90 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f ditional.** info
1aca0 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 rmation..*/.#def
1acb0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 ine SQLITE_DENY
1acc0 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 1 /* Abort t
1acd0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1ace0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a with an error *
1acf0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1ad00 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 _IGNORE 2 /* D
1ad10 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 on't allow acces
1ad20 73 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e s, but don't gen
1ad30 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a erate an error *
1ad40 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
1ad50 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 F: Authorizer Ac
1ad60 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 tion Codes {H125
1ad70 35 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 50} <H12500>.**.
1ad80 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
1ad90 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 set_authorizer()
1ada0 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ] interface regi
1adb0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b sters a callback
1adc0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 function.** tha
1add0 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 t is invoked to
1ade0 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 authorize certai
1adf0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1ae00 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a actions. The.**
1ae10 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1ae20 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 r to the callbac
1ae30 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 k is an integer
1ae40 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66 code that specif
1ae50 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 ies.** what acti
1ae60 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 on is being auth
1ae70 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 orized. These a
1ae80 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 re the integer a
1ae90 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 ction codes that
1aea0 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a .** the authoriz
1aeb0 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 er callback may
1aec0 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a be passed..**.**
1aed0 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f These action co
1aee0 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 de values signif
1aef0 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f y what kind of o
1af00 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 peration is to b
1af10 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e e.** authorized.
1af20 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 The 3rd and 4t
1af30 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 h parameters to
1af40 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f the authorizatio
1af50 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 n.** callback fu
1af60 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 nction will be p
1af70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c arameters or NUL
1af80 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 L depending on w
1af90 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a hich of these.**
1afa0 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 codes is used a
1afb0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 s the second par
1afc0 61 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 ameter. The 5th
1afd0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
1afe0 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 e.** authorizer
1aff0 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 callback is the
1b000 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
1b010 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 base ("main", "t
1b020 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 emp",.** etc.) i
1b030 66 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 f applicable. T
1b040 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 he 6th parameter
1b050 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a to the authoriz
1b060 65 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 er callback.** i
1b070 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1b080 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 e inner-most tri
1b090 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 gger or view tha
1b0a0 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 t is responsible
1b0b0 20 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 for.** the acce
1b0c0 73 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 ss attempt or NU
1b0d0 4c 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 LL if this acces
1b0e0 73 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 s attempt is dir
1b0f0 65 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f ectly from.** to
1b100 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 p-level SQL code
1b110 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
1b120 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31 ents:.** [H12551
1b130 5d 20 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 35 ] [H12552] [H125
1b140 35 33 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a 53] [H12554].*/.
1b150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1b160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 ************ 3rd
1b180 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 ************ 4t
1b190 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 h ***********/.#
1b1a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
1b1b0 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20 20 EATE_INDEX
1b1c0 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 1 /* Index
1b1d0 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 Name Table
1b1e0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 Name */.#d
1b1f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 efine SQLITE_CRE
1b200 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20 20 20 ATE_TABLE
1b210 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 2 /* Table
1b220 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1b230 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b240 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 fine SQLITE_CREA
1b250 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 TE_TEMP_INDEX
1b260 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 3 /* Index N
1b270 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
1b280 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1b290 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 ine SQLITE_CREAT
1b2a0 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 E_TEMP_TABLE
1b2b0 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 4 /* Table Na
1b2c0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b2d0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b2e0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
1b2f0 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20 _TEMP_TRIGGER
1b300 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 5 /* Trigger N
1b310 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d ame Table Nam
1b320 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b330 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
1b340 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20 36 TEMP_VIEW 6
1b350 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 /* View Name
1b360 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b370 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b380 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
1b390 52 49 47 47 45 52 20 20 20 20 20 20 20 20 37 20 RIGGER 7
1b3a0 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
1b3b0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
1b3c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b3d0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 SQLITE_CREATE_VI
1b3e0 45 57 20 20 20 20 20 20 20 20 20 20 20 38 20 20 EW 8
1b3f0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
1b400 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b410 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b420 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20 20 20 QLITE_DELETE
1b430 20 20 20 20 20 20 20 20 20 20 20 20 39 20 20 20 9
1b440 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 /* Table Name
1b450 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b460 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b470 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 LITE_DROP_INDEX
1b480 20 20 20 20 20 20 20 20 20 20 31 30 20 20 20 2f 10 /
1b490 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
1b4a0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1b4b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b4c0 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 ITE_DROP_TABLE
1b4d0 20 20 20 20 20 20 20 20 20 31 31 20 20 20 2f 2a 11 /*
1b4e0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b4f0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b500 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b510 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 TE_DROP_TEMP_IND
1b520 45 58 20 20 20 20 20 20 31 32 20 20 20 2f 2a 20 EX 12 /*
1b530 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
1b540 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b550 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b560 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c E_DROP_TEMP_TABL
1b570 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 54 E 13 /* T
1b580 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1b590 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b5a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b5b0 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 _DROP_TEMP_TRIGG
1b5c0 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72 ER 14 /* Tr
1b5d0 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 igger Name Ta
1b5e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1b5f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b600 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 DROP_TEMP_VIEW
1b610 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65 15 /* Vie
1b620 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c w Name NUL
1b630 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b640 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
1b650 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20 20 ROP_TRIGGER
1b660 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67 16 /* Trig
1b670 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
1b680 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
1b690 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
1b6a0 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 OP_VIEW
1b6b0 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77 20 17 /* View
1b6c0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1b6d0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b6e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 efine SQLITE_INS
1b6f0 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ERT
1b700 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 18 /* Table
1b710 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1b720 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b730 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 47 fine SQLITE_PRAG
1b740 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MA
1b750 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 19 /* Pragma
1b760 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61 72 67 Name 1st arg
1b770 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 or NULL */.#def
1b780 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 20 ine SQLITE_READ
1b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b7a0 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 20 /* Table Na
1b7b0 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e me Column N
1b7c0 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 ame */.#defi
1b7d0 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 ne SQLITE_SELECT
1b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
1b7f0 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 1 /* NULL
1b800 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 NULL
1b810 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1b820 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 e SQLITE_TRANSAC
1b830 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 32 32 TION 22
1b840 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 /* Operation
1b850 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b860 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b870 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 20 SQLITE_UPDATE
1b880 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 20 23
1b890 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1b8a0 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 Column Name
1b8b0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b8c0 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 20 20 SQLITE_ATTACH
1b8d0 20 20 20 20 20 20 20 20 20 20 20 20 32 34 20 20 24
1b8e0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 /* Filename
1b8f0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b900 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b910 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20 20 20 QLITE_DETACH
1b920 20 20 20 20 20 20 20 20 20 20 20 32 35 20 20 20 25
1b930 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 /* Database Name
1b940 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b950 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b960 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 LITE_ALTER_TABLE
1b970 20 20 20 20 20 20 20 20 20 20 32 36 20 20 20 2f 26 /
1b980 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 * Database Name
1b990 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1b9a0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b9b0 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20 20 20 ITE_REINDEX
1b9c0 20 20 20 20 20 20 20 20 20 32 37 20 20 20 2f 2a 27 /*
1b9d0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
1b9e0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b9f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1ba00 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 TE_ANALYZE
1ba10 20 20 20 20 20 20 20 20 32 38 20 20 20 2f 2a 20 28 /*
1ba20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1ba30 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1ba40 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1ba50 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 20 E_CREATE_VTABLE
1ba60 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a 20 54 29 /* T
1ba70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d able Name M
1ba80 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a odule Name *
1ba90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1baa0 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 _DROP_VTABLE
1bab0 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61 30 /* Ta
1bac0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f ble Name Mo
1bad0 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f dule Name */
1bae0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1baf0 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 FUNCTION
1bb00 20 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 31 /* NUL
1bb10 4c 20 20 20 20 20 20 20 20 20 20 20 20 46 75 6e L Fun
1bb20 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a ction Name */.
1bb30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
1bb40 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 AVEPOINT
1bb50 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72 32 /* Oper
1bb60 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65 ation Save
1bb70 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 point Name */.#
1bb80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
1bb90 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 PY
1bba0 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 0 /* No lo
1bbb0 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a nger used */../*
1bbc0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72 .** CAPI3REF: Tr
1bbd0 61 63 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c acing And Profil
1bbe0 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 ing Functions {H
1bbf0 31 32 32 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a 12280} <S60400>.
1bc00 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
1bc10 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
1bc20 69 6e 65 73 20 72 65 67 69 73 74 65 72 20 63 61 ines register ca
1bc30 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73 llback functions
1bc40 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 that can be use
1bc50 64 20 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67 d for.** tracing
1bc60 20 61 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 74 and profiling t
1bc70 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 he execution of
1bc80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a SQL statements..
1bc90 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 **.** The callba
1bca0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 ck function regi
1bcb0 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 stered by sqlite
1bcc0 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e 76 3_trace() is inv
1bcd0 6f 6b 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f oked at.** vario
1bce0 75 73 20 74 69 6d 65 73 20 77 68 65 6e 20 61 6e us times when an
1bcf0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 SQL statement i
1bd00 73 20 62 65 69 6e 67 20 72 75 6e 20 62 79 20 5b s being run by [
1bd10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e sqlite3_step()].
1bd20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
1bd30 20 72 65 74 75 72 6e 73 20 61 20 55 54 46 2d 38 returns a UTF-8
1bd40 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 rendering of th
1bd50 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1bd60 74 65 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 73 text.** as the s
1bd70 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 62 tatement first b
1bd80 65 67 69 6e 73 20 65 78 65 63 75 74 69 6e 67 2e egins executing.
1bd90 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c Additional cal
1bda0 6c 62 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20 lbacks occur.**
1bdb0 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72 65 as each triggere
1bdc0 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20 d subprogram is
1bdd0 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63 61 entered. The ca
1bde0 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67 llbacks for trig
1bdf0 67 65 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 gers.** contain
1be00 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d a UTF-8 SQL comm
1be10 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69 66 ent that identif
1be20 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72 2e ies the trigger.
1be30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 .**.** The callb
1be40 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 ack function reg
1be50 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 istered by sqlit
1be60 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 20 e3_profile() is
1be70 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61 invoked.** as ea
1be80 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ch SQL statement
1be90 20 66 69 6e 69 73 68 65 73 2e 20 20 54 68 65 20 finishes. The
1bea0 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b profile callback
1beb0 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 contains.** the
1bec0 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d original statem
1bed0 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 6e 20 ent text and an
1bee0 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c estimate of wall
1bef0 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f -clock time.** o
1bf00 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20 f how long that
1bf10 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74 statement took t
1bf20 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 o run..**.** Req
1bf30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
1bf40 31 32 32 38 31 5d 20 5b 48 31 32 32 38 32 5d 20 12281] [H12282]
1bf50 5b 48 31 32 32 38 33 5d 20 5b 48 31 32 32 38 34 [H12283] [H12284
1bf60 5d 20 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 32 ] [H12285] [H122
1bf70 38 37 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 31 87] [H12288] [H1
1bf80 32 32 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30 2289].** [H12290
1bf90 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
1bfa0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
1bfb0 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 TAL void *sqlite
1bfc0 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 2a 3_trace(sqlite3*
1bfd0 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28 , void(*xTrace)(
1bfe0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
1bff0 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 *), void*);.SQLI
1c000 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
1c010 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 PERIMENTAL void
1c020 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 *sqlite3_profile
1c030 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f (sqlite3*,. vo
1c040 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f id(*xProfile)(vo
1c050 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c id*,const char*,
1c060 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 2c sqlite3_uint64),
1c070 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 void*);../*.**
1c080 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79 20 CAPI3REF: Query
1c090 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63 Progress Callbac
1c0a0 6b 73 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 30 ks {H12910} <S60
1c0b0 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 400>.**.** This
1c0c0 72 6f 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 routine configur
1c0d0 65 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 es a callback fu
1c0e0 6e 63 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20 nction - the.**
1c0f0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
1c100 6b 20 2d 20 74 68 61 74 20 69 73 20 69 6e 76 6f k - that is invo
1c110 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 ked periodically
1c120 20 64 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20 during long.**
1c130 72 75 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f running calls to
1c140 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
1c150 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ], [sqlite3_step
1c160 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 ()] and.** [sqli
1c170 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d te3_get_table()]
1c180 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 . An example us
1c190 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e e for this.** in
1c1a0 74 65 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65 terface is to ke
1c1b0 65 70 20 61 20 47 55 49 20 75 70 64 61 74 65 64 ep a GUI updated
1c1c0 20 64 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20 during a large
1c1d0 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 query..**.** If
1c1e0 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1c1f0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
1c200 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 n-zero, the oper
1c210 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65 ation is.** inte
1c220 72 72 75 70 74 65 64 2e 20 20 54 68 69 73 20 66 rrupted. This f
1c230 65 61 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 eature can be us
1c240 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
1c250 61 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75 a.** "Cancel" bu
1c260 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72 tton on a GUI pr
1c270 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f ogress dialog bo
1c280 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f x..**.** The pro
1c290 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 75 gress handler mu
1c2a0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 st not do anythi
1c2b0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 ng that will mod
1c2c0 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ify.** the datab
1c2d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
1c2e0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
1c2f0 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 progress handler
1c300 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b ..** Note that [
1c310 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
1c320 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 v2()] and [sqlit
1c330 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 e3_step()] both
1c340 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 modify their.**
1c350 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1c360 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 ions for the mea
1c370 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 ning of "modify"
1c380 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 in this paragra
1c390 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ph..**.** Requir
1c3a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 ements:.** [H129
1c3b0 31 31 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 31 11] [H12912] [H1
1c3c0 32 39 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b 2913] [H12914] [
1c3d0 48 31 32 39 31 35 5d 20 5b 48 31 32 39 31 36 5d H12915] [H12916]
1c3e0 20 5b 48 31 32 39 31 37 5d 20 5b 48 31 32 39 31 [H12917] [H1291
1c3f0 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 8].**.*/.SQLITE_
1c400 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1c410 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 _progress_handle
1c420 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c r(sqlite3*, int,
1c430 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 int(*)(void*),
1c440 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
1c450 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 API3REF: Opening
1c460 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 20 A New Database
1c470 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37 Connection {H127
1c480 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 00} <S40200>.**.
1c490 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
1c4a0 73 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 s open an SQLite
1c4b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
1c4c0 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 67 69 76 hose name is giv
1c4d0 65 6e 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c en by the.** fil
1c4e0 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20 ename argument.
1c4f0 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 The filename arg
1c500 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72 ument is interpr
1c510 65 74 65 64 20 61 73 20 55 54 46 2d 38 20 66 6f eted as UTF-8 fo
1c520 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 r.** sqlite3_ope
1c530 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f n() and sqlite3_
1c540 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 73 open_v2() and as
1c550 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e UTF-16 in the n
1c560 61 74 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72 ative byte.** or
1c570 64 65 72 20 66 6f 72 20 73 71 6c 69 74 65 33 5f der for sqlite3_
1c580 6f 70 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 74 open16(). A [dat
1c590 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1c5a0 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75 61 ] handle is usua
1c5b0 6c 6c 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 lly.** returned
1c5c0 69 6e 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 69 in *ppDb, even i
1c5d0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1c5e0 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 s. The only exc
1c5f0 65 70 74 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a eption is that.*
1c600 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 75 * if SQLite is u
1c610 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 nable to allocat
1c620 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 e memory to hold
1c630 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
1c640 62 6a 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c bject,.** a NULL
1c650 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e will be written
1c660 20 69 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 74 into *ppDb inst
1c670 65 61 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 ead of a pointer
1c680 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
1c690 5d 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20 ].** object. If
1c6a0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
1c6b0 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 opened (and/or c
1c6c0 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 66 reated) successf
1c6d0 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 ully, then.** [S
1c6e0 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 QLITE_OK] is ret
1c6f0 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 urned. Otherwis
1c700 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 e an [error code
1c710 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 ] is returned.
1c720 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f The.** [sqlite3_
1c730 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 errmsg()] or [sq
1c740 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
1c750 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 ] routines can b
1c760 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e e used to obtain
1c770 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c .** an English l
1c780 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70 74 anguage descript
1c790 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 ion of the error
1c7a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ..**.** The defa
1c7b0 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 ult encoding for
1c7c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69 the database wi
1c7d0 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a ll be UTF-8 if.*
1c7e0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
1c7f0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e or sqlite3_open
1c800 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 _v2() is called
1c810 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e and.** UTF-16 in
1c820 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 the native byte
1c830 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 65 order if sqlite
1c840 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73 3_open16() is us
1c850 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 ed..**.** Whethe
1c860 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f r or not an erro
1c870 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74 r occurs when it
1c880 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f is opened, reso
1c890 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 urces.** associa
1c8a0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 ted with the [da
1c8b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1c8c0 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 n] handle should
1c8d0 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a be released by.
1c8e0 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f ** passing it to
1c8f0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
1c900 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f )] when it is no
1c910 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 longer required
1c920 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
1c930 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e te3_open_v2() in
1c940 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 terface works li
1c950 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ke sqlite3_open(
1c960 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 ).** except that
1c970 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f 20 it accepts two
1c980 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d additional param
1c990 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69 eters for additi
1c9a0 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 onal control.**
1c9b0 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 over the new dat
1c9c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1c9d0 2e 20 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 . The flags par
1c9e0 61 6d 65 74 65 72 20 63 61 6e 20 74 61 6b 65 20 ameter can take
1c9f0 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f one of.** the fo
1ca00 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 76 61 llowing three va
1ca10 6c 75 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 lues, optionally
1ca20 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 74 combined with t
1ca30 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f he .** [SQLITE_O
1ca40 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 PEN_NOMUTEX], [S
1ca50 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d QLITE_OPEN_FULLM
1ca60 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f UTEX], [SQLITE_O
1ca70 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d PEN_SHAREDCACHE]
1ca80 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 53 51 4c ,.** and/or [SQL
1ca90 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 ITE_OPEN_PRIVATE
1caa0 43 41 43 48 45 5d 20 66 6c 61 67 73 3a 0a 2a 2a CACHE] flags:.**
1cab0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
1cac0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 [SQLITE_OPEN_REA
1cad0 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c DONLY]</dt>.** <
1cae0 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 dd>The database
1caf0 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61 is opened in rea
1cb00 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66 d-only mode. If
1cb10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 6f the database do
1cb20 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 es not.** alread
1cb30 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f y exist, an erro
1cb40 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f r is returned.</
1cb50 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 dd>.**.** <dt>[S
1cb60 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
1cb70 52 49 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 RITE]</dt>.** <d
1cb80 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 d>The database i
1cb90 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 s opened for rea
1cba0 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 ding and writing
1cbb0 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 if possible, or
1cbc0 20 72 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 reading.** only
1cbd0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 if the file is
1cbe0 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 write protected
1cbf0 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 by the operating
1cc00 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74 system. In eit
1cc10 68 65 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 her.** case the
1cc20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c database must al
1cc30 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68 ready exist, oth
1cc40 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 erwise an error
1cc50 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 is returned.</dd
1cc60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c >.**.** <dt>[SQL
1cc70 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
1cc80 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 TE] | [SQLITE_OP
1cc90 45 4e 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a EN_CREATE]</dt>.
1cca0 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 ** <dd>The datab
1ccb0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f ase is opened fo
1ccc0 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 r reading and wr
1ccd0 69 74 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72 iting, and is cr
1cce0 65 61 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69 eates it if.** i
1ccf0 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 t does not alrea
1cd00 64 79 20 65 78 69 73 74 2e 20 54 68 69 73 20 69 dy exist. This i
1cd10 73 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 74 s the behavior t
1cd20 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 hat is always us
1cd30 65 64 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 ed for.** sqlite
1cd40 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 3_open() and sql
1cd50 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f ite3_open16().</
1cd60 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a dd>.** </dl>.**.
1cd70 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 ** If the 3rd pa
1cd80 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
1cd90 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
1cda0 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a not one of the.*
1cdb0 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 * combinations s
1cdc0 68 6f 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e hown above or on
1cdd0 65 20 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61 e of the combina
1cde0 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 tions shown abov
1cdf0 65 20 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69 e combined.** wi
1ce00 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f th the [SQLITE_O
1ce10 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 PEN_NOMUTEX], [S
1ce20 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d QLITE_OPEN_FULLM
1ce30 55 54 45 58 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 UTEX],.** [SQLIT
1ce40 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 E_OPEN_SHAREDCAC
1ce50 48 45 5d 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49 HE] and/or [SQLI
1ce60 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
1ce70 43 48 45 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 74 CHE] flags,.** t
1ce80 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 hen the behavior
1ce90 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
1cea0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 4c *.** If the [SQL
1ceb0 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 ITE_OPEN_NOMUTEX
1cec0 5d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 ] flag is set, t
1ced0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
1cee0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f connection.** o
1cef0 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74 pens in the mult
1cf00 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64 i-thread [thread
1cf10 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e ing mode] as lon
1cf20 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d g as the single-
1cf30 74 68 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 68 thread.** mode h
1cf40 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 20 as not been set
1cf50 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 at compile-time
1cf60 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 or start-time.
1cf70 49 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 If the.** [SQLIT
1cf80 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 E_OPEN_FULLMUTEX
1cf90 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 ] flag is set th
1cfa0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
1cfb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 73 connection opens
1cfc0 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 72 69 61 .** in the seria
1cfd0 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 lized [threading
1cfe0 20 6d 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 69 mode] unless si
1cff0 6e 67 6c 65 2d 74 68 72 65 61 64 20 77 61 73 0a ngle-thread was.
1d000 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 ** previously se
1d010 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c lected at compil
1d020 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d e-time or start-
1d030 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 time..** The [SQ
1d040 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 LITE_OPEN_SHARED
1d050 43 41 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73 CACHE] flag caus
1d060 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 es the database
1d070 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65 connection to be
1d080 0a 2a 2a 20 65 6c 69 67 69 62 6c 65 20 74 6f 20 .** eligible to
1d090 75 73 65 20 5b 73 68 61 72 65 64 20 63 61 63 68 use [shared cach
1d0a0 65 20 6d 6f 64 65 5d 2c 20 72 65 67 61 72 64 6c e mode], regardl
1d0b0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f ess of whether o
1d0c0 72 20 6e 6f 74 20 73 68 61 72 65 64 0a 2a 2a 20 r not shared.**
1d0d0 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 cache is enabled
1d0e0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1d0f0 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 enable_shared_ca
1d100 63 68 65 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 che()]. The.**
1d110 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 [SQLITE_OPEN_PRI
1d120 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 67 20 VATECACHE] flag
1d130 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 62 causes the datab
1d140 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
1d150 6f 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 69 63 69 o not.** partici
1d160 70 61 74 65 20 69 6e 20 5b 73 68 61 72 65 64 20 pate in [shared
1d170 63 61 63 68 65 20 6d 6f 64 65 5d 20 65 76 65 6e cache mode] even
1d180 20 69 66 20 69 74 20 69 73 20 65 6e 61 62 6c 65 if it is enable
1d190 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1d1a0 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 filename is ":me
1d1b0 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 mory:", then a p
1d1c0 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 rivate, temporar
1d1d0 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 y in-memory data
1d1e0 62 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 base.** is creat
1d1f0 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 ed for the conne
1d200 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d ction. This in-
1d210 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
1d220 77 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e will vanish when
1d230 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1d240 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 connection is c
1d250 6c 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 losed. Future v
1d260 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
1d270 65 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 e might.** make
1d280 75 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 use of additiona
1d290 6c 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 l special filena
1d2a0 6d 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 mes that begin w
1d2b0 69 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 ith the ":" char
1d2c0 61 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 acter..** It is
1d2d0 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 recommended that
1d2e0 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 when a database
1d2f0 20 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c filename actual
1d300 6c 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 ly does begin wi
1d310 74 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 th.** a ":" char
1d320 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 acter you should
1d330 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 prefix the file
1d340 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 name with a path
1d350 6e 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 name such as.**
1d360 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d "./" to avoid am
1d370 62 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 biguity..**.** I
1d380 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 f the filename i
1d390 73 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e s an empty strin
1d3a0 67 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 g, then a privat
1d3b0 65 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 e, temporary.**
1d3c0 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 on-disk database
1d3d0 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 will be created
1d3e0 2e 20 20 54 68 69 73 20 70 72 69 76 61 74 65 20 . This private
1d3f0 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 database will be
1d400 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .** automaticall
1d410 79 20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f y deleted as soo
1d420 6e 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 n as the databas
1d430 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
1d440 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 closed..**.** Th
1d450 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 e fourth paramet
1d460 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 er to sqlite3_op
1d470 65 6e 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e en_v2() is the n
1d480 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 ame of the.** [s
1d490 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
1d4a0 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 ct that defines
1d4b0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
1d4c0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 stem interface t
1d4d0 68 61 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 hat.** the new d
1d4e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1d4f0 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 on should use.
1d500 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 If the fourth pa
1d510 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 rameter is.** a
1d520 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 NULL pointer the
1d530 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 n the default [s
1d540 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
1d550 63 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a ct is used..**.*
1d560 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e * <b>Note to Win
1d570 64 6f 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 dows users:</b>
1d580 20 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 The encoding us
1d590 65 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e ed for the filen
1d5a0 61 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 ame argument.**
1d5b0 6f 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 of sqlite3_open(
1d5c0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 ) and sqlite3_op
1d5d0 65 6e 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 en_v2() must be
1d5e0 55 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 UTF-8, not whate
1d5f0 76 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 ver.** codepage
1d600 69 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 is currently def
1d610 69 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 ined. Filenames
1d620 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 containing inte
1d630 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 rnational.** cha
1d640 72 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 racters must be
1d650 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 converted to UTF
1d660 2d 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 -8 prior to pass
1d670 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a ing them into.**
1d680 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
1d690 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f or sqlite3_open_
1d6a0 76 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 v2()..**.** Requ
1d6b0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
1d6c0 32 37 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b 2701] [H12702] [
1d6d0 48 31 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d H12703] [H12704]
1d6e0 20 5b 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30 [H12706] [H1270
1d6f0 37 5d 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32 7] [H12709] [H12
1d700 37 31 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d 711].** [H12712]
1d710 20 5b 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31 [H12713] [H1271
1d720 34 5d 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32 4] [H12717] [H12
1d730 37 31 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48 719] [H12721] [H
1d740 31 32 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12723].*/.SQLITE
1d750 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1d760 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 _open(. const c
1d770 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 har *filename,
1d780 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
1d790 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f ename (UTF-8) */
1d7a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 . sqlite3 **ppD
1d7b0 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 b /* OU
1d7c0 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e T: SQLite db han
1d7d0 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 dle */.);.SQLITE
1d7e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1d7f0 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 _open16(. const
1d800 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c void *filename,
1d810 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 /* Database f
1d820 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 ilename (UTF-16)
1d830 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a */. sqlite3 **
1d840 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a ppDb /*
1d850 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 OUT: SQLite db
1d860 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c handle */.);.SQL
1d870 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1d880 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 te3_open_v2(. c
1d890 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e onst char *filen
1d8a0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 ame, /* Databa
1d8b0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 se filename (UTF
1d8c0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 -8) */. sqlite3
1d8d0 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 **ppDb,
1d8e0 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 /* OUT: SQLite
1d8f0 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 db handle */. i
1d900 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
1d910 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1d920 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1d930 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 *zVfs /*
1d940 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 Name of VFS modu
1d950 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a le to use */.);.
1d960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1d970 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 Error Codes And
1d980 20 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 Messages {H1280
1d990 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 0} <S60200>.**.*
1d9a0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 * The sqlite3_er
1d9b0 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 rcode() interfac
1d9c0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 e returns the nu
1d9d0 6d 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f meric [result co
1d9e0 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e de] or.** [exten
1d9f0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d ded result code]
1da00 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 for the most re
1da10 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 cent failed sqli
1da20 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a te3_* API call.*
1da30 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1da40 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f h a [database co
1da50 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 nnection]. If a
1da60 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 prior API call f
1da70 61 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 ailed.** but the
1da80 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 most recent API
1da90 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c call succeeded,
1daa0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
1dab0 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 e from.** sqlite
1dac0 33 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 3_errcode() is u
1dad0 6e 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 ndefined. The s
1dae0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
1daf0 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 errcode().** int
1db00 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 erface is the sa
1db10 6d 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 me except that i
1db20 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 t always returns
1db30 20 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 the .** [extend
1db40 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 ed result code]
1db50 65 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 even when extend
1db60 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 ed result codes
1db70 61 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e are.** disabled.
1db80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
1db90 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 e3_errmsg() and
1dba0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
1dbb0 28 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 () return Englis
1dbc0 68 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 h-language.** te
1dbd0 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 xt that describe
1dbe0 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 s the error, as
1dbf0 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 either UTF-8 or
1dc00 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 UTF-16 respectiv
1dc10 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 ely..** Memory t
1dc20 6f 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 o hold the error
1dc30 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 message string
1dc40 69 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 is managed inter
1dc50 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 nally..** The ap
1dc60 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e plication does n
1dc70 6f 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 ot need to worry
1dc80 20 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 about freeing t
1dc90 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f he result..** Ho
1dca0 77 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 wever, the error
1dcb0 20 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 string might be
1dcc0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 overwritten or
1dcd0 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a deallocated by.*
1dce0 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c * subsequent cal
1dcf0 6c 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 ls to other SQLi
1dd00 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e te interface fun
1dd10 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 ctions..**.** Wh
1dd20 65 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 en the serialize
1dd30 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 d [threading mod
1dd40 65 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 e] is in use, it
1dd50 20 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a might be the.**
1dd60 20 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 case that a sec
1dd70 6f 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ond error occurs
1dd80 20 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 on a separate t
1dd90 68 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e hread in between
1dda0 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 .** the time of
1ddb0 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 the first error
1ddc0 61 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 and the call to
1ddd0 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 these interfaces
1dde0 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 ..** When that h
1ddf0 61 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f appens, the seco
1de00 6e 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 nd error will be
1de10 20 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 reported since
1de20 74 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 these.** interfa
1de30 63 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 ces always repor
1de40 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e t the most recen
1de50 74 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 t result. To av
1de60 6f 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 oid.** this, eac
1de70 68 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 h thread can obt
1de80 61 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 ain exclusive us
1de90 65 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 e of the [databa
1dea0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
1deb0 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 .** by invoking
1dec0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 [sqlite3_mutex_e
1ded0 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 nter]([sqlite3_d
1dee0 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 b_mutex](D)) bef
1def0 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a ore beginning.**
1df00 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e to use D and in
1df10 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f voking [sqlite3_
1df20 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 mutex_leave]([sq
1df30 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 lite3_db_mutex](
1df40 44 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c D)) after.** all
1df50 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e calls to the in
1df60 74 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 terfaces listed
1df70 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 here are complet
1df80 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
1df90 69 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 interface fails
1dfa0 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 with SQLITE_MISU
1dfb0 53 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 SE, that means t
1dfc0 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 he interface.**
1dfd0 77 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f was invoked inco
1dfe0 72 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 rrectly by the a
1dff0 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 pplication. In
1e000 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a that case, the.*
1e010 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 * error code and
1e020 20 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 message may or
1e030 6d 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a may not be set..
1e040 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
1e050 74 73 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20 ts:.** [H12801]
1e060 5b 48 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33 [H12802] [H12803
1e070 5d 20 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38 ] [H12807] [H128
1e080 30 38 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a 08] [H12809].*/.
1e090 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1e0a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 qlite3_errcode(s
1e0b0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
1e0c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1e0d0 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 te3_extended_err
1e0e0 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 code(sqlite3 *db
1e0f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
1e100 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1e110 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 3_errmsg(sqlite3
1e120 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
1e130 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1e140 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 e3_errmsg16(sqli
1e150 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
1e160 50 49 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 PI3REF: SQL Stat
1e170 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 ement Object {H1
1e180 33 30 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a 3000} <H13010>.*
1e190 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65 * KEYWORDS: {pre
1e1a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d pared statement}
1e1b0 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 {prepared state
1e1c0 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 ments}.**.** An
1e1d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
1e1e0 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e object represen
1e1f0 74 73 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 ts a single SQL
1e200 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 statement..** Th
1e210 69 73 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72 is object is var
1e220 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 iously known as
1e230 61 20 22 70 72 65 70 61 72 65 64 20 73 74 61 74 a "prepared stat
1e240 65 6d 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22 ement" or a.** "
1e250 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 compiled SQL sta
1e260 74 65 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c tement" or simpl
1e270 79 20 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e y as a "statemen
1e280 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 t"..**.** The li
1e290 66 65 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e fe of a statemen
1e2a0 74 20 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f t object goes so
1e2b0 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 mething like thi
1e2c0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a s:.**.** <ol>.**
1e2d0 20 3c 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65 <li> Create the
1e2e0 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 object using [s
1e2f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
1e300 32 28 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 2()] or a relate
1e310 64 0a 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69 d.** functi
1e320 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 on..** <li> Bind
1e330 20 76 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74 values to [host
1e340 20 70 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69 parameters] usi
1e350 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 ng the sqlite3_b
1e360 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20 ind_*().**
1e370 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c interfaces..** <
1e380 6c 69 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20 li> Run the SQL
1e390 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 by calling [sqli
1e3a0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 te3_step()] one
1e3b0 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a or more times..*
1e3c0 2a 20 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 * <li> Reset the
1e3d0 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 statement using
1e3e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
1e3f0 29 5d 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a )] then go back.
1e400 2a 2a 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 ** to step
1e410 32 2e 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 2. Do this zero
1e420 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a or more times..
1e430 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20 ** <li> Destroy
1e440 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 the object using
1e450 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
1e460 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a ze()]..** </ol>.
1e470 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64 **.** Refer to d
1e480 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 ocumentation on
1e490 69 6e 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f individual metho
1e4a0 64 73 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64 ds above for add
1e4b0 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 itional.** infor
1e4c0 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 mation..*/.typed
1e4d0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
1e4e0 33 5f 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73 3_stmt sqlite3_s
1e4f0 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 tmt;../*.** CAPI
1e500 33 52 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c 3REF: Run-time L
1e510 69 6d 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c imits {H12760} <
1e520 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20600>.**.** Th
1e530 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c is interface all
1e540 6f 77 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ows the size of
1e550 76 61 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63 various construc
1e560 74 73 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64 ts to be limited
1e570 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 .** on a connect
1e580 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f ion by connectio
1e590 6e 20 62 61 73 69 73 2e 20 20 54 68 65 20 66 69 n basis. The fi
1e5a0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 rst parameter is
1e5b0 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 the.** [databas
1e5c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 e connection] wh
1e5d0 6f 73 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 ose limit is to
1e5e0 62 65 20 73 65 74 20 6f 72 20 71 75 65 72 69 65 be set or querie
1e5f0 64 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e d. The.** secon
1e600 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f d parameter is o
1e610 6e 65 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 ne of the [limit
1e620 20 63 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 categories] tha
1e630 74 20 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c t define a.** cl
1e640 61 73 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 ass of construct
1e650 73 20 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d s to be size lim
1e660 69 74 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 ited. The third
1e670 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
1e680 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66 e.** new limit f
1e690 6f 72 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63 or that construc
1e6a0 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e t. The function
1e6b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64 returns the old
1e6c0 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 limit..**.** If
1e6d0 20 74 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69 the new limit i
1e6e0 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d s a negative num
1e6f0 62 65 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69 ber, the limit i
1e700 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 s unchanged..**
1e710 46 6f 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61 For the limit ca
1e720 74 65 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45 tegory of SQLITE
1e730 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65 _LIMIT_XYZ there
1e740 20 69 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 is a .** [limit
1e750 73 20 7c 20 68 61 72 64 20 75 70 70 65 72 20 62 s | hard upper b
1e760 6f 75 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20 ound].** set by
1e770 61 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43 a compile-time C
1e780 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 preprocessor ma
1e790 63 72 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c cro named .** [l
1e7a0 69 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d imits | SQLITE_M
1e7b0 41 58 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65 AX_XYZ]..** (The
1e7c0 20 22 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 "_LIMIT_" in th
1e7d0 65 20 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 e name is change
1e7e0 64 20 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a d to "_MAX_".).*
1e7f0 2a 20 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e * Attempts to in
1e800 63 72 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61 crease a limit a
1e810 62 6f 76 65 20 69 74 73 20 68 61 72 64 20 75 70 bove its hard up
1e820 70 65 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a per bound are.**
1e830 20 73 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 silently trunca
1e840 74 65 64 20 74 6f 20 74 68 65 20 68 61 72 64 20 ted to the hard
1e850 75 70 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a upper limit..**.
1e860 2a 2a 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69 ** Run time limi
1e870 74 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 ts are intended
1e880 66 6f 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69 for use in appli
1e890 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e cations that man
1e8a0 61 67 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69 age.** both thei
1e8b0 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64 r own internal d
1e8c0 61 74 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f atabase and also
1e8d0 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 databases that
1e8e0 61 72 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a are controlled.*
1e8f0 2a 20 62 79 20 75 6e 74 72 75 73 74 65 64 20 65 * by untrusted e
1e900 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e xternal sources.
1e910 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70 An example app
1e920 6c 69 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62 lication might b
1e930 65 20 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73 e a.** web brows
1e940 65 72 20 74 68 61 74 20 68 61 73 20 69 74 73 20 er that has its
1e950 6f 77 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f own databases fo
1e960 72 20 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72 r storing histor
1e970 79 20 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74 y and.** separat
1e980 65 20 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74 e databases cont
1e990 72 6f 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63 rolled by JavaSc
1e9a0 72 69 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e ript application
1e9b0 73 20 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20 s downloaded.**
1e9c0 6f 66 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74 off the Internet
1e9d0 2e 20 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 . The internal
1e9e0 64 61 74 61 62 61 73 65 73 20 63 61 6e 20 62 65 databases can be
1e9f0 20 67 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 given the.** la
1ea00 72 67 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d rge, default lim
1ea10 69 74 73 2e 20 20 44 61 74 61 62 61 73 65 73 20 its. Databases
1ea20 6d 61 6e 61 67 65 64 20 62 79 20 65 78 74 65 72 managed by exter
1ea30 6e 61 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a nal sources can.
1ea40 2a 2a 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68 ** be given much
1ea50 20 73 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20 smaller limits
1ea60 64 65 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76 designed to prev
1ea70 65 6e 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 ent a denial of
1ea80 73 65 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63 service.** attac
1ea90 6b 2e 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d k. Developers m
1eaa0 69 67 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74 ight also want t
1eab0 6f 20 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74 o use the [sqlit
1eac0 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
1ead0 72 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 r()].** interfac
1eae0 65 20 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e e to further con
1eaf0 74 72 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53 trol untrusted S
1eb00 51 4c 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 QL. The size of
1eb10 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
1eb20 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 75 created by an u
1eb30 6e 74 72 75 73 74 65 64 20 73 63 72 69 70 74 20 ntrusted script
1eb40 63 61 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64 can be contained
1eb50 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d using the.** [m
1eb60 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b ax_page_count] [
1eb70 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e PRAGMA]..**.** N
1eb80 65 77 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 ew run-time limi
1eb90 74 20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 t categories may
1eba0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
1ebb0 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a ure releases..**
1ebc0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1ebd0 3a 0a 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48 :.** [H12762] [H
1ebe0 31 32 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a 12766] [H12769].
1ebf0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1ec00 74 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 t sqlite3_limit(
1ec10 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 sqlite3*, int id
1ec20 2c 20 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a , int newVal);..
1ec30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1ec40 52 75 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 Run-Time Limit C
1ec50 61 74 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 ategories {H1279
1ec60 30 7d 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 0} <H12760>.** K
1ec70 45 59 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 EYWORDS: {limit
1ec80 63 61 74 65 67 6f 72 79 7d 20 7b 2a 6c 69 6d 69 category} {*limi
1ec90 74 20 63 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a t categories}.**
1eca0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
1ecb0 6e 74 73 20 64 65 66 69 6e 65 20 76 61 72 69 6f nts define vario
1ecc0 75 73 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c us performance l
1ecd0 69 6d 69 74 73 0a 2a 2a 20 74 68 61 74 20 63 61 imits.** that ca
1ece0 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 61 74 20 n be lowered at
1ecf0 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b run-time using [
1ed00 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d sqlite3_limit()]
1ed10 2e 0a 2a 2a 20 54 68 65 20 73 79 6e 6f 70 73 69 ..** The synopsi
1ed20 73 20 6f 66 20 74 68 65 20 6d 65 61 6e 69 6e 67 s of the meaning
1ed30 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 s of the various
1ed40 20 6c 69 6d 69 74 73 20 69 73 20 73 68 6f 77 6e limits is shown
1ed50 20 62 65 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74 below..** Addit
1ed60 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1ed70 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 61 n is available a
1ed80 74 20 5b 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69 t [limits | Limi
1ed90 74 73 20 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a ts in SQLite]..*
1eda0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
1edb0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 >SQLITE_LIMIT_LE
1edc0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
1edd0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a >The maximum siz
1ede0 65 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 e of any string
1edf0 6f 72 20 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 or BLOB or table
1ee00 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20 row.<dd>.**.**
1ee10 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1ee20 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e _SQL_LENGTH</dt>
1ee30 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1ee40 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e mum length of an
1ee50 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c SQL statement.<
1ee60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1ee70 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 QLITE_LIMIT_COLU
1ee80 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 MN</dt>.** <dd>T
1ee90 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1eea0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1eeb0 61 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 a table definiti
1eec0 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 on or in the.**
1eed0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 result set of a
1eee0 5b 53 45 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 [SELECT] or the
1eef0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1ef00 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 f columns in an
1ef10 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 index.** or in a
1ef20 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 n ORDER BY or GR
1ef30 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f OUP BY clause.</
1ef40 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
1ef50 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f LITE_LIMIT_EXPR_
1ef60 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 DEPTH</dt>.** <d
1ef70 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 d>The maximum de
1ef80 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 pth of the parse
1ef90 20 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 tree on any exp
1efa0 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a ression.</dd>.**
1efb0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1efc0 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 IMIT_COMPOUND_SE
1efd0 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 LECT</dt>.** <dd
1efe0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d >The maximum num
1eff0 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
1f000 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 a compound SELEC
1f010 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 T statement.</dd
1f020 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1f030 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 TE_LIMIT_VDBE_OP
1f040 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
1f050 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1f060 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
1f070 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 in a virtual mac
1f080 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 hine program.**
1f090 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1f0a0 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 t an SQL stateme
1f0b0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c nt.</dd>.**.** <
1f0c0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1f0d0 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 FUNCTION_ARG</dt
1f0e0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1f0f0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 imum number of a
1f100 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 rguments on a fu
1f110 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a nction.</dd>.**.
1f120 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1f130 4d 49 54 5f 41 54 54 41 43 48 45 44 3c 2f 64 74 MIT_ATTACHED</dt
1f140 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1f150 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b imum number of [
1f160 41 54 54 41 43 48 20 7c 20 61 74 74 61 63 68 65 ATTACH | attache
1f170 64 20 64 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64 d databases].</d
1f180 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1f190 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 ITE_LIMIT_LIKE_P
1f1a0 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64 ATTERN_LENGTH</d
1f1b0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1f1c0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
1f1d0 74 68 65 20 70 61 74 74 65 72 6e 20 61 72 67 75 the pattern argu
1f1e0 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b ment to the [LIK
1f1f0 45 5d 20 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 E] or.** [GLOB]
1f200 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a operators.</dd>.
1f210 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1f220 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f _LIMIT_VARIABLE_
1f230 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c NUMBER</dt>.** <
1f240 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e dd>The maximum n
1f250 75 6d 62 65 72 20 6f 66 20 76 61 72 69 61 62 6c umber of variabl
1f260 65 73 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 es in an SQL sta
1f270 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61 6e 0a tement that can.
1f280 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64 ** be bound.</dd
1f290 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1f2a0 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 TE_LIMIT_TRIGGER
1f2b0 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c _DEPTH</dt>.** <
1f2c0 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 dd>The maximum d
1f2d0 65 70 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f epth of recursio
1f2e0 6e 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 3c n for triggers.<
1f2f0 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f /dd>.** </dl>.*/
1f300 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f310 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20 LIMIT_LENGTH
1f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f330 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
1f340 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 _LIMIT_SQL_LENGT
1f350 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
1f360 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
1f370 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20 E_LIMIT_COLUMN
1f380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f390 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
1f3a0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 TE_LIMIT_EXPR_DE
1f3b0 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 PTH
1f3c0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
1f3d0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
1f3e0 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 ND_SELECT
1f3f0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
1f400 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f LITE_LIMIT_VDBE_
1f410 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OP
1f420 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 5.#define S
1f430 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 QLITE_LIMIT_FUNC
1f440 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20 TION_ARG
1f450 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
1f460 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 SQLITE_LIMIT_ATT
1f470 41 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20 ACHED
1f480 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 7.#define
1f490 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 SQLITE_LIMIT_LI
1f4a0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 KE_PATTERN_LENGT
1f4b0 48 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e H 8.#defin
1f4c0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 e SQLITE_LIMIT_V
1f4d0 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 ARIABLE_NUMBER
1f4e0 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 9.#defi
1f4f0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f500 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 20 20 TRIGGER_DEPTH
1f510 20 20 20 20 20 20 20 20 20 31 30 0a 0a 2f 2a 0a 10../*.
1f520 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d ** CAPI3REF: Com
1f530 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 piling An SQL St
1f540 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d atement {H13010}
1f550 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59 <S10000>.** KEY
1f560 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74 WORDS: {SQL stat
1f570 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a ement compiler}.
1f580 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 **.** To execute
1f590 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69 an SQL query, i
1f5a0 74 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20 t must first be
1f5b0 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 compiled into a
1f5c0 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f byte-code.** pro
1f5d0 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f gram using one o
1f5e0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
1f5f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
1f600 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 62 22 t argument, "db"
1f610 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65 , is a [database
1f620 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74 connection] obt
1f630 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 ained from a.**
1f640 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c prior successful
1f650 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
1f660 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 3_open()], [sqli
1f670 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f te3_open_v2()] o
1f680 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 r.** [sqlite3_op
1f690 65 6e 31 36 28 29 5d 2e 20 20 54 68 65 20 64 61 en16()]. The da
1f6a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1f6b0 6e 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 n must not have
1f6c0 62 65 65 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a been closed..**.
1f6d0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ** The second ar
1f6e0 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 gument, "zSql",
1f6f0 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 is the statement
1f700 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c to be compiled,
1f710 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 encoded.** as e
1f720 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 ither UTF-8 or U
1f730 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69 TF-16. The sqli
1f740 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e te3_prepare() an
1f750 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 d sqlite3_prepar
1f760 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 e_v2().** interf
1f770 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20 aces use UTF-8,
1f780 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 and sqlite3_prep
1f790 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69 are16() and sqli
1f7a0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
1f7b0 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 ().** use UTF-16
1f7c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
1f7d0 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 Byte argument is
1f7e0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c less than zero,
1f7f0 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 then zSql is re
1f800 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 ad up to the.**
1f810 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 first zero termi
1f820 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 nator. If nByte
1f830 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c is non-negative,
1f840 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 then it is the
1f850 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 maximum.** numbe
1f860 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64 r of bytes read
1f870 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 from zSql. Whe
1f880 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e n nByte is non-n
1f890 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 egative, the.**
1f8a0 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 zSql string ends
1f8b0 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66 at either the f
1f8c0 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27 irst '\000' or '
1f8d0 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65 \u0000' characte
1f8e0 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 r or.** the nByt
1f8f0 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68 e-th byte, which
1f900 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 ever comes first
1f910 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 . If the caller
1f920 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 knows.** that th
1f930 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e e supplied strin
1f940 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 g is nul-termina
1f950 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 ted, then there
1f960 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 is a small.** pe
1f970 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 rformance advant
1f980 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64 age to be gained
1f990 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e by passing an n
1f9a0 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74 Byte parameter t
1f9b0 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 hat.** is equal
1f9c0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
1f9d0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e bytes in the in
1f9e0 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e put string <i>in
1f9f0 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 cluding</i>.** t
1fa00 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f he nul-terminato
1fa10 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 r bytes..**.** I
1fa20 66 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 f pzTail is not
1fa30 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 NULL then *pzTai
1fa40 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 l is made to poi
1fa50 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 nt to the first
1fa60 62 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 byte.** past the
1fa70 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 end of the firs
1fa80 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 t SQL statement
1fa90 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 in zSql. These
1faa0 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a routines only.**
1fab0 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72 compile the fir
1fac0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 st statement in
1fad0 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c zSql, so *pzTail
1fae0 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
1faf0 67 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d g to.** what rem
1fb00 61 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e ains uncompiled.
1fb10 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 .**.** *ppStmt i
1fb20 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1fb30 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 to a compiled [p
1fb40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1fb50 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a t] that can be.*
1fb60 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67 * executed using
1fb70 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1fb80 5d 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 ]. If there is
1fb90 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d an error, *ppStm
1fba0 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e t is set.** to N
1fbb0 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 69 6e 70 ULL. If the inp
1fbc0 75 74 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 ut text contains
1fbd0 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20 no SQL (if the
1fbe0 69 6e 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 input is an empt
1fbf0 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 y.** string or a
1fc00 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a comment) then *
1fc10 70 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f ppStmt is set to
1fc20 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61 NULL..** The ca
1fc30 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 lling procedure
1fc40 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
1fc50 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 or deleting the
1fc60 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 compiled.** SQL
1fc70 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 statement using
1fc80 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
1fc90 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 e()] after it ha
1fca0 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 s finished with
1fcb0 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61 it..** ppStmt ma
1fcc0 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a y not be NULL..*
1fcd0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c *.** On success,
1fce0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 [SQLITE_OK] is
1fcf0 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 65 72 77 returned, otherw
1fd00 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f ise an [error co
1fd10 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e de] is returned.
1fd20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
1fd30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 e3_prepare_v2()
1fd40 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 and sqlite3_prep
1fd50 61 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72 are16_v2() inter
1fd60 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65 63 faces are.** rec
1fd70 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c ommended for all
1fd80 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54 new programs. T
1fd90 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74 he two older int
1fda0 65 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61 erfaces are reta
1fdb0 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b ined.** for back
1fdc0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
1fdd0 69 74 79 2c 20 62 75 74 20 74 68 65 69 72 20 75 ity, but their u
1fde0 73 65 20 69 73 20 64 69 73 63 6f 75 72 61 67 65 se is discourage
1fdf0 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76 32 d..** In the "v2
1fe00 22 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 " interfaces, th
1fe10 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 e prepared state
1fe20 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 ment.** that is
1fe30 72 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b 73 returned (the [s
1fe40 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a qlite3_stmt] obj
1fe50 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61 20 ect) contains a
1fe60 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f copy of the.** o
1fe70 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 riginal SQL text
1fe80 2e 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68 . This causes th
1fe90 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 e [sqlite3_step(
1fea0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f 0a )] interface to.
1feb0 2a 2a 20 62 65 68 61 76 65 20 64 69 66 66 65 72 ** behave differ
1fec0 65 6e 74 6c 79 20 69 6e 20 74 68 72 65 65 20 77 ently in three w
1fed0 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a ays:.**.** <ol>.
1fee0 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68 ** <li>.** If th
1fef0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
1ff00 61 20 63 68 61 6e 67 65 73 2c 20 69 6e 73 74 65 a changes, inste
1ff10 61 64 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 ad of returning
1ff20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 [SQLITE_SCHEMA]
1ff30 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20 as it.** always
1ff40 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c used to do, [sql
1ff50 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c ite3_step()] wil
1ff60 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
1ff70 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 20 53 51 recompile the SQ
1ff80 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 L.** statement a
1ff90 6e 64 20 74 72 79 20 74 6f 20 72 75 6e 20 69 74 nd try to run it
1ffa0 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 again. If the
1ffb0 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 schema has chang
1ffc0 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74 ed in.** a way t
1ffd0 68 61 74 20 6d 61 6b 65 73 20 74 68 65 20 73 74 hat makes the st
1ffe0 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 atement no longe
1fff0 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65 r valid, [sqlite
20000 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 73 3_step()] will s
20010 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b till.** return [
20020 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20 SQLITE_SCHEMA].
20030 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 68 65 20 But unlike the
20040 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 2c legacy behavior,
20050 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d [SQLITE_SCHEMA]
20060 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74 is.** now a fat
20070 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69 al error. Calli
20080 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
20090 61 72 65 5f 76 32 28 29 5d 20 61 67 61 69 6e 20 are_v2()] again
200a0 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68 will not make th
200b0 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20 61 77 e.** error go aw
200c0 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b ay. Note: use [
200d0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
200e0 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 65 ] to find the te
200f0 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 xt.** of the par
20100 73 69 6e 67 20 65 72 72 6f 72 20 74 68 61 74 20 sing error that
20110 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53 results in an [S
20120 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 72 65 QLITE_SCHEMA] re
20130 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a turn..** </li>.*
20140 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 *.** <li>.** Whe
20150 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 n an error occur
20160 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 s, [sqlite3_step
20170 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ()] will return
20180 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69 one of the detai
20190 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f led.** [error co
201a0 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 des] or [extende
201b0 64 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 d error codes].
201c0 20 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 The legacy beha
201d0 76 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a vior was that.**
201e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
201f0 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 ] would only ret
20200 75 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53 urn a generic [S
20210 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 QLITE_ERROR] res
20220 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 ult code.** and
20230 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74 you would have t
20240 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 o make a second
20250 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
20260 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 6f 72 64 _reset()] in ord
20270 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 er.** to find th
20280 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 e underlying cau
20290 73 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 se of the proble
202a0 6d 2e 20 57 69 74 68 20 74 68 65 20 22 76 32 22 m. With the "v2"
202b0 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65 prepare.** inte
202c0 72 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65 rfaces, the unde
202d0 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f rlying reason fo
202e0 72 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72 r the error is r
202f0 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 eturned immediat
20300 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a ely..** </li>.**
20310 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 5e 49 66 20 .** <li>.** ^If
20320 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 5b the value of a [
20330 70 61 72 61 6d 65 74 65 72 20 7c 20 68 6f 73 74 parameter | host
20340 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 74 parameter] in t
20350 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
20360 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 20 might.** change
20370 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66 the query plan f
20380 6f 72 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 or a statement,
20390 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 then the stateme
203a0 6e 74 20 6d 61 79 20 62 65 0a 2a 2a 20 61 75 74 nt may be.** aut
203b0 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d omatically recom
203c0 70 69 6c 65 64 20 28 61 73 20 69 66 20 74 68 65 piled (as if the
203d0 72 65 20 68 61 64 20 62 65 65 6e 20 61 20 73 63 re had been a sc
203e0 68 65 6d 61 20 63 68 61 6e 67 65 29 20 6f 6e 20 hema change) on
203f0 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 5b 73 the first .** [s
20400 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 qlite3_step()] c
20410 61 6c 6c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e all following an
20420 79 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 y change to the
20430 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
20440 64 5f 74 65 78 74 20 7c 20 62 69 6e 64 69 6e 67 d_text | binding
20450 73 5d 20 6f 66 20 74 68 65 20 5b 70 61 72 61 6d s] of the [param
20460 65 74 65 72 5d 2e 20 0a 2a 2a 20 3c 2f 6c 69 3e eter]. .** </li>
20470 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ol>.**.**
20480 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
20490 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 30 31 [H13011] [H1301
204a0 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 31 33 2] [H13013] [H13
204b0 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 5b 48 014] [H13015] [H
204c0 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 5d 20 13016] [H13019]
204d0 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53 [H13021].**.*/.S
204e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
204f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 lite3_prepare(.
20500 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
20510 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
20520 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 base handle */.
20530 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 const char *zSq
20540 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 l, /* SQL
20550 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 statement, UTF-8
20560 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e encoded */. in
20570 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 t nByte,
20580 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
20590 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 length of zSql
205a0 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 in bytes. */. s
205b0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 qlite3_stmt **pp
205c0 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 Stmt, /* OUT: S
205d0 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 tatement handle
205e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
205f0 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 **pzTail /*
20600 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 OUT: Pointer to
20610 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f unused portion o
20620 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c f zSql */.);.SQL
20630 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
20640 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a te3_prepare_v2(.
20650 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
20660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
20670 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
20680 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
20690 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ql, /* SQL
206a0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d statement, UTF-
206b0 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 8 encoded */. i
206c0 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 nt nByte,
206d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
206e0 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c m length of zSql
206f0 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 in bytes. */.
20700 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 sqlite3_stmt **p
20710 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 pStmt, /* OUT:
20720 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 Statement handle
20730 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
20740 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a **pzTail /*
20750 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f OUT: Pointer to
20760 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 unused portion
20770 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 of zSql */.);.SQ
20780 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
20790 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a ite3_prepare16(.
207a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
207b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
207c0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
207d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 const void *zS
207e0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ql, /* SQL
207f0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d statement, UTF-
20800 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 16 encoded */.
20810 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 int nByte,
20820 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
20830 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 um length of zSq
20840 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 l in bytes. */.
20850 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a sqlite3_stmt **
20860 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a ppStmt, /* OUT:
20870 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c Statement handl
20880 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 e */. const voi
20890 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f d **pzTail /
208a0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 * OUT: Pointer t
208b0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e o unused portion
208c0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 of zSql */.);.S
208d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
208e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
208f0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
20900 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
20910 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
20920 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
20930 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
20940 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
20950 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a UTF-16 encoded *
20960 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 /. int nByte,
20970 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
20980 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
20990 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 zSql in bytes.
209a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d */. sqlite3_stm
209b0 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 t **ppStmt, /*
209c0 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 OUT: Statement h
209d0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
209e0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 void **pzTail
209f0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 /* OUT: Point
20a00 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 er to unused por
20a10 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a tion of zSql */.
20a20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
20a30 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53 EF: Retrieving S
20a40 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 48 31 tatement SQL {H1
20a50 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e 0a 2a 3100} <H13000>.*
20a60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
20a70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 ace can be used
20a80 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 73 61 to retrieve a sa
20a90 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20 ved copy of the
20aa0 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 original.** SQL
20ab0 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 72 65 text used to cre
20ac0 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 ate a [prepared
20ad0 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 74 68 statement] if th
20ae0 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 at statement was
20af0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 .** compiled usi
20b00 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 ng either [sqlit
20b10 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
20b20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 or [sqlite3_pre
20b30 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a pare16_v2()]..**
20b40 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
20b50 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 5b 48 :.** [H13101] [H
20b60 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 5d 0a 13102] [H13103].
20b70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
20b80 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
20b90 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 3_sql(sqlite3_st
20ba0 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a mt *pStmt);../*.
20bb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 79 6e ** CAPI3REF: Dyn
20bc0 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56 amically Typed V
20bd0 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 31 35 alue Object {H15
20be0 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 000} <S20200>.**
20bf0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74 KEYWORDS: {prot
20c00 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
20c10 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65 lue} {unprotecte
20c20 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d d sqlite3_value}
20c30 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 .**.** SQLite us
20c40 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 es the sqlite3_v
20c50 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 72 alue object to r
20c60 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c epresent all val
20c70 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 ues.** that can
20c80 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 64 be stored in a d
20c90 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 53 atabase table. S
20ca0 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d QLite uses dynam
20cb0 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 ic typing.** for
20cc0 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73 the values it s
20cd0 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 73 74 tores. Values st
20ce0 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f ored in sqlite3_
20cf0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a value objects.**
20d00 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 72 73 can be integers
20d10 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 , floating point
20d20 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 values, strings
20d30 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c , BLOBs, or NULL
20d40 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 ..**.** An sqlit
20d50 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
20d60 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70 may be either "p
20d70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e rotected" or "un
20d80 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53 protected"..** S
20d90 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72 ome interfaces r
20da0 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74 equire a protect
20db0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
20dc0 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61 . Other interfa
20dd0 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65 ces.** will acce
20de0 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f 74 pt either a prot
20df0 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70 72 ected or an unpr
20e00 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
20e10 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20 value..** Every
20e20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 61 interface that a
20e30 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 5f 76 ccepts sqlite3_v
20e40 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 20 73 alue arguments s
20e50 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 65 74 pecifies.** whet
20e60 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 72 65 her or not it re
20e70 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 63 74 quires a protect
20e80 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
20e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d ..**.** The term
20ea0 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 61 6e s "protected" an
20eb0 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 20 d "unprotected"
20ec0 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 65 72 refer to whether
20ed0 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 or not.** a mut
20ee0 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 20 69 ex is held. A i
20ef0 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 73 nternal mutex is
20f00 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 6f 74 held for a prot
20f10 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
20f20 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 62 75 _value object bu
20f30 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 68 65 t no mutex is he
20f40 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74 ld for an unprot
20f50 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ected.** sqlite3
20f60 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20 _value object.
20f70 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
20f80 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 6e 67 piled to be sing
20f90 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 28 le-threaded.** (
20fa0 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 48 52 with [SQLITE_THR
20fb0 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 20 77 EADSAFE=0] and w
20fc0 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 68 72 ith [sqlite3_thr
20fd0 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 75 72 eadsafe()] retur
20fe0 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66 ning 0).** or if
20ff0 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e 20 69 SQLite is run i
21000 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 65 64 n one of reduced
21010 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a mutex modes .**
21020 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
21030 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 6f 72 SINGLETHREAD] or
21040 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
21050 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20 MULTITHREAD].**
21060 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f then there is no
21070 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 distinction bet
21080 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 ween protected a
21090 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a nd unprotected.*
210a0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 * sqlite3_value
210b0 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79 objects and they
210c0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74 can be used int
210d0 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48 erchangeably. H
210e0 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d owever,.** for m
210f0 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74 aximum code port
21100 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65 ability it is re
21110 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 commended that a
21120 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
21130 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69 till make the di
21140 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 stinction betwee
21150 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 n between protec
21160 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 ted and unprotec
21170 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ted.** sqlite3_v
21180 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65 alue objects eve
21190 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 69 63 n when not stric
211a0 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a tly required..**
211b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
211c0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 74 68 value objects th
211d0 61 74 20 61 72 65 20 70 61 73 73 65 64 20 61 73 at are passed as
211e0 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 74 6f parameters into
211f0 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
21200 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 tation of [appli
21210 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
21220 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 QL functions] ar
21230 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 e protected..**
21240 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
21250 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 e object returne
21260 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 d by.** [sqlite3
21270 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d _column_value()]
21280 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e is unprotected.
21290 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20 .** Unprotected
212a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
212b0 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 jects may only b
212c0 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b e used with.** [
212d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
212e0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c alue()] and [sql
212f0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
21300 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 )]..** The [sqli
21310 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c te3_value_blob |
21320 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
21330 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 ype()] family of
21340 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72 .** interfaces r
21350 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 65 64 equire protected
21360 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
21370 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 bjects..*/.typed
21380 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71 ef struct Mem sq
21390 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a lite3_value;../*
213a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
213b0 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 L Function Conte
213c0 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 30 30 xt Object {H1600
213d0 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 1} <S20200>.**.*
213e0 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e * The context in
213f0 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75 which an SQL fu
21400 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20 nction executes
21410 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a is stored in an.
21420 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ** sqlite3_conte
21430 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f xt object. A po
21440 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 inter to an sqli
21450 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 te3_context obje
21460 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 ct.** is always
21470 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
21480 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d to [application-
21490 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 defined SQL func
214a0 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 tions]..** The a
214b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
214c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ed SQL function
214d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
214e0 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a 2a 2a ill pass this.**
214f0 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68 pointer through
21500 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b into calls to [
21510 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
21520 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 nt | sqlite3_res
21530 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 ult()],.** [sqli
21540 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
21550 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 ntext()], [sqlit
21560 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c e3_user_data()],
21570 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e .** [sqlite3_con
21580 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 text_db_handle()
21590 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f ], [sqlite3_get_
215a0 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 auxdata()],.** a
215b0 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 nd/or [sqlite3_s
215c0 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a et_auxdata()]..*
215d0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
215e0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
215f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
21600 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
21610 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 F: Binding Value
21620 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53 74 s To Prepared St
21630 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 30 30 atements {H13500
21640 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 } <S70300>.** KE
21650 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 70 61 YWORDS: {host pa
21660 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 20 70 rameter} {host p
21670 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74 arameters} {host
21680 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d parameter name}
21690 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 .** KEYWORDS: {S
216a0 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 53 QL parameter} {S
216b0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b QL parameters} {
216c0 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e parameter bindin
216d0 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 g}.**.** In the
216e0 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e 70 75 SQL strings inpu
216f0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 t to [sqlite3_pr
21700 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
21710 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a its variants,.**
21720 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 62 65 literals may be
21730 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 5b replaced by a [
21740 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61 74 20 parameter] that
21750 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66 20 66 matches one of f
21760 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 65 6d 70 ollowing.** temp
21770 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c lates:.**.** <ul
21780 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a 2a 20 >.** <li> ?.**
21790 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c <li> ?NNN.** <l
217a0 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c 69 3e i> :VVV.** <li>
217b0 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20 @VVV.** <li>
217c0 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a $VVV.** </ul>.**
217d0 0a 2a 2a 20 49 6e 20 74 68 65 20 74 65 6d 70 6c .** In the templ
217e0 61 74 65 73 20 61 62 6f 76 65 2c 20 4e 4e 4e 20 ates above, NNN
217f0 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e represents an in
21800 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a teger literal,.*
21810 2a 20 61 6e 64 20 56 56 56 20 72 65 70 72 65 73 * and VVV repres
21820 65 6e 74 73 20 61 6e 20 61 6c 70 68 61 6e 75 6d ents an alphanum
21830 65 72 69 63 20 69 64 65 6e 74 69 66 65 72 2e 20 eric identifer.
21840 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 The values of t
21850 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 hese.** paramete
21860 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 rs (also called
21870 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 "host parameter
21880 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c 20 70 names" or "SQL p
21890 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a 20 63 arameters").** c
218a0 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e 67 20 an be set using
218b0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 the sqlite3_bind
218c0 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 64 65 _*() routines de
218d0 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a 0a 2a fined here..**.*
218e0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
218f0 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69 ment to the sqli
21900 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 te3_bind_*() rou
21910 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79 73 0a tines is always.
21920 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
21930 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d the [sqlite3_stm
21940 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e t] object return
21950 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 ed from.** [sqli
21960 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
21970 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 ] or its variant
21980 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 s..**.** The sec
21990 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
219a0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
219b0 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 74 SQL parameter t
219c0 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54 68 65 o be set..** The
219d0 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 leftmost SQL pa
219e0 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e 20 69 rameter has an i
219f0 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68 65 6e ndex of 1. When
21a00 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 64 0a the same named.
21a10 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 ** SQL parameter
21a20 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 74 68 is used more th
21a30 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e 64 20 an once, second
21a40 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 0a 2a and subsequent.*
21a50 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 68 61 * occurrences ha
21a60 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 ve the same inde
21a70 78 20 61 73 20 74 68 65 20 66 69 72 73 74 20 6f x as the first o
21a80 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20 54 68 ccurrence..** Th
21a90 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61 6d 65 e index for name
21aa0 64 20 70 61 72 61 6d 65 74 65 72 73 20 63 61 6e d parameters can
21ab0 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73 be looked up us
21ac0 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 ing the.** [sqli
21ad0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
21ae0 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50 49 20 er_index()] API
21af0 69 66 20 64 65 73 69 72 65 64 2e 20 20 54 68 65 if desired. The
21b00 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 22 3f index.** for "?
21b10 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72 73 20 NNN" parameters
21b20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 is the value of
21b30 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e 4e 20 NNN..** The NNN
21b40 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 62 65 value must be be
21b50 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68 65 20 tween 1 and the
21b60 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 [sqlite3_limit()
21b70 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 5b ].** parameter [
21b80 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
21b90 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 28 64 IABLE_NUMBER] (d
21ba0 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39 efault value: 99
21bb0 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 9)..**.** The th
21bc0 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
21bd0 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e the value to bin
21be0 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 d to the paramet
21bf0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 6f er..**.** In tho
21c00 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 se routines that
21c10 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 20 61 have a fourth a
21c20 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 61 6c rgument, its val
21c30 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d ue is the.** num
21c40 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
21c50 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 20 20 the parameter.
21c60 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 68 65 To be clear: the
21c70 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a value is the.**
21c80 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e 62 79 number of <u>by
21c90 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 20 76 tes</u> in the v
21ca0 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 6e 75 alue, not the nu
21cb0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
21cc0 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f rs..** If the fo
21cd0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 urth parameter i
21ce0 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 s negative, the
21cf0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 length of the st
21d00 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65 20 6e ring is.** the n
21d10 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 75 umber of bytes u
21d20 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a p to the first z
21d30 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a ero terminator..
21d40 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20 **.** The fifth
21d50 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
21d60 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 2c te3_bind_blob(),
21d70 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
21d80 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 71 6c xt(), and.** sql
21d90 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
21da0 28 29 20 69 73 20 61 20 64 65 73 74 72 75 63 74 () is a destruct
21db0 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 70 6f or used to dispo
21dc0 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 6f se of the BLOB o
21dd0 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 74 65 r.** string afte
21de0 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 69 6e r SQLite has fin
21df0 69 73 68 65 64 20 77 69 74 68 20 69 74 2e 20 49 ished with it. I
21e00 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75 f the fifth argu
21e10 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65 20 73 ment is.** the s
21e20 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b 53 51 pecial value [SQ
21e30 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 74 68 LITE_STATIC], th
21e40 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 en SQLite assume
21e50 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69 6e s that the.** in
21e60 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 6e 20 formation is in
21e70 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 67 65 static, unmanage
21e80 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73 d space and does
21e90 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
21ea0 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 freed..** If the
21eb0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 fifth argument
21ec0 68 61 73 20 74 68 65 20 76 61 6c 75 65 20 5b 53 has the value [S
21ed0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 5d QLITE_TRANSIENT]
21ee0 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 , then.** SQLite
21ef0 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e 20 70 makes its own p
21f00 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 rivate copy of t
21f10 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69 61 74 he data immediat
21f20 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a 20 74 ely, before.** t
21f30 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f he sqlite3_bind_
21f40 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 *() routine retu
21f50 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 rns..**.** The s
21f60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f qlite3_bind_zero
21f70 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65 20 62 blob() routine b
21f80 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66 20 6c inds a BLOB of l
21f90 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a 2a 20 ength N that.**
21fa0 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 7a is filled with z
21fb0 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f 62 6c eroes. A zerobl
21fc0 6f 62 20 75 73 65 73 20 61 20 66 69 78 65 64 20 ob uses a fixed
21fd0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
21fe0 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69 6e 74 .** (just an int
21ff0 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69 74 73 eger to hold its
22000 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69 74 20 size) while it
22010 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 73 73 is being process
22020 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f 62 73 ed..** Zeroblobs
22030 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f are intended to
22040 20 73 65 72 76 65 20 61 73 20 70 6c 61 63 65 68 serve as placeh
22050 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f 42 73 olders for BLOBs
22060 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e whose.** conten
22070 74 20 69 73 20 6c 61 74 65 72 20 77 72 69 74 74 t is later writt
22080 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c en using.** [sql
22090 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c ite3_blob_open |
220a0 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f incremental BLO
220b0 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65 73 2e B I/O] routines.
220c0 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 76 .** A negative v
220d0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a 65 72 alue for the zer
220e0 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 69 6e oblob results in
220f0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 a zero-length B
22100 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 LOB..**.** The s
22110 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 qlite3_bind_*()
22120 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
22130 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a 2a 2a called after.**
22140 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
22150 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69 74 73 e_v2()] (and its
22160 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20 5b 73 variants) or [s
22170 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
22180 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20 5b 73 and.** before [s
22190 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a qlite3_step()]..
221a0 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72 65 20 ** Bindings are
221b0 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 20 74 not cleared by t
221c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 he [sqlite3_rese
221d0 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a t()] routine..**
221e0 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65 74 Unbound paramet
221f0 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 72 65 ers are interpre
22200 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a ted as NULL..**.
22210 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
22220 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 s return [SQLITE
22230 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 _OK] on success
22240 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
22250 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 if.** anything
22260 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b 53 51 goes wrong. [SQ
22270 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73 20 72 LITE_RANGE] is r
22280 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 70 eturned if the p
22290 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 64 65 arameter.** inde
222a0 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 x is out of rang
222b0 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 e. [SQLITE_NOME
222c0 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 M] is returned i
222d0 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 f malloc() fails
222e0 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 ..** [SQLITE_MIS
222f0 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20 72 65 USE] might be re
22300 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 65 20 turned if these
22310 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c routines are cal
22320 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 led on a.** virt
22330 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 ual machine that
22340 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 73 74 is the wrong st
22350 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 61 73 ate or which has
22360 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69 already been fi
22370 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65 74 65 nalized..** Dete
22380 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73 65 20 ction of misuse
22390 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e 20 20 is unreliable.
223a0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f Applications sho
223b0 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64 0a 2a uld not depend.*
223c0 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 * on SQLITE_MISU
223d0 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53 51 4c SE returns. SQL
223e0 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20 69 6e ITE_MISUSE is in
223f0 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69 63 61 tended to indica
22400 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69 63 20 te a.** a logic
22410 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61 70 70 error in the app
22420 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74 75 72 lication. Futur
22430 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
22440 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 70 61 Lite might.** pa
22450 6e 69 63 20 72 61 74 68 65 72 20 74 68 61 6e 20 nic rather than
22460 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
22470 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 SUSE..**.** See
22480 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 also: [sqlite3_b
22490 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
224a0 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 unt()],.** [sqli
224b0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
224c0 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 20 er_name()], and
224d0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
224e0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d rameter_index()]
224f0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
22500 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35 30 36 ents:.** [H13506
22510 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31 33 35 ] [H13509] [H135
22520 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b 48 31 12] [H13515] [H1
22530 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d 20 5b 3518] [H13521] [
22540 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32 37 5d H13524] [H13527]
22550 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b 48 31 .** [H13530] [H1
22560 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d 20 5b 3533] [H13536] [
22570 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34 32 5d H13539] [H13542]
22580 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33 35 34 [H13545] [H1354
22590 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a 0a 2a 8] [H13551].**.*
225a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
225b0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c sqlite3_bind_bl
225c0 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a ob(sqlite3_stmt*
225d0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 , int, const voi
225e0 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28 d*, int n, void(
225f0 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
22600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22610 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 e3_bind_double(s
22620 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
22630 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 t, double);.SQLI
22640 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22650 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 e3_bind_int(sqli
22660 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
22670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
22680 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22690 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f d_int64(sqlite3_
226a0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 stmt*, int, sqli
226b0 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 te3_int64);.SQLI
226c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
226d0 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c e3_bind_null(sql
226e0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 ite3_stmt*, int)
226f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
22700 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
22710 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a xt(sqlite3_stmt*
22720 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 , int, const cha
22730 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28 r*, int n, void(
22740 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
22750 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22760 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 73 e3_bind_text16(s
22770 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
22780 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 t, const void*,
22790 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
227a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
227b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
227c0 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f d_value(sqlite3_
227d0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 stmt*, int, cons
227e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t sqlite3_value*
227f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
22800 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a t sqlite3_bind_z
22810 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f eroblob(sqlite3_
22820 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 stmt*, int, int
22830 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 n);../*.** CAPI3
22840 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20 53 REF: Number Of S
22850 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 QL Parameters {H
22860 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a 13600} <S70300>.
22870 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
22880 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 ne can be used t
22890 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65 o find the numbe
228a0 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61 6d 65 r of [SQL parame
228b0 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20 5b 70 ters].** in a [p
228c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
228d0 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d 65 74 t]. SQL paramet
228e0 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73 20 6f ers are tokens o
228f0 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 22 3f f the.** form "?
22900 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41 41 41 ", "?NNN", ":AAA
22910 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20 22 40 ", "$AAA", or "@
22920 41 41 41 22 20 74 68 61 74 20 73 65 72 76 65 20 AAA" that serve
22930 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c 64 65 as.** placeholde
22940 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20 74 68 rs for values th
22950 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65 33 5f at are [sqlite3_
22960 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f 75 6e bind_blob | boun
22970 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 72 d].** to the par
22980 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c 61 74 ameters at a lat
22990 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 er time..**.** T
229a0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 75 his routine actu
229b0 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 68 65 ally returns the
229c0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6c 61 index of the la
229d0 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f 73 74 rgest (rightmost
229e0 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 ).** parameter.
229f0 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 65 78 For all forms ex
22a00 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 73 20 cept ?NNN, this
22a10 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20 will correspond
22a20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 to the.** number
22a30 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 61 6d of unique param
22a40 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 61 6d eters. If param
22a50 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f 4e 4e eters of the ?NN
22a60 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 74 N are used,.** t
22a70 68 65 72 65 20 6d 61 79 20 62 65 20 67 61 70 73 here may be gaps
22a80 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a in the list..**
22a90 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
22aa0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
22ab0 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d |sqlite3_bind()]
22ac0 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 ,.** [sqlite3_bi
22ad0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
22ae0 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 e()], and.** [sq
22af0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
22b00 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a eter_index()]..*
22b10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
22b20 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d 0a 2a s:.** [H13601].*
22b30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
22b40 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 sqlite3_bind_pa
22b50 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 rameter_count(sq
22b60 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f lite3_stmt*);../
22b70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e *.** CAPI3REF: N
22b80 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20 50 61 ame Of A Host Pa
22b90 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32 30 7d rameter {H13620}
22ba0 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S70300>.**.**
22bb0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
22bc0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
22bd0 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 o the name of th
22be0 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c 20 70 e n-th.** [SQL p
22bf0 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61 20 5b arameter] in a [
22c00 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
22c10 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 72 61 nt]..** SQL para
22c20 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f meters of the fo
22c30 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 rm "?NNN" or ":A
22c40 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 AA" or "@AAA" or
22c50 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 65 20 "$AAA".** have
22c60 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 73 20 a name which is
22c70 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e 4e 4e the string "?NNN
22c80 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 " or ":AAA" or "
22c90 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a @AAA" or "$AAA".
22ca0 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e ** respectively.
22cb0 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
22cc0 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61 6c 20 ds, the initial
22cd0 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 22 40 ":" or "$" or "@
22ce0 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 20 69 " or "?".** is i
22cf0 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72 74 20 ncluded as part
22d00 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20 of the name..**
22d10 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 Parameters of th
22d20 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 68 6f e form "?" witho
22d30 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 ut a following i
22d40 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f 20 6e nteger have no n
22d50 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65 20 61 ame.** and are a
22d60 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74 6f 20 lso referred to
22d70 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 as "anonymous pa
22d80 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a 2a 2a rameters"..**.**
22d90 20 54 68 65 20 66 69 72 73 74 20 68 6f 73 74 20 The first host
22da0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e parameter has an
22db0 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e 6f 74 index of 1, not
22dc0 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 0..**.** If the
22dd0 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75 74 20 value n is out
22de0 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74 of range or if t
22df0 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65 74 65 he n-th paramete
22e00 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65 73 73 r is.** nameless
22e10 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 , then NULL is r
22e20 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 72 65 eturned. The re
22e30 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
22e40 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20 55 54 .** always in UT
22e50 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65 76 65 F-8 encoding eve
22e60 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 70 n if the named p
22e70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a 2a 20 arameter was.**
22e80 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65 63 69 originally speci
22e90 66 69 65 64 20 61 73 20 55 54 46 2d 31 36 20 69 fied as UTF-16 i
22ea0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 n [sqlite3_prepa
22eb0 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 re16()] or.** [s
22ec0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
22ed0 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 _v2()]..**.** Se
22ee0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
22ef0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 _bind_blob|sqlit
22f00 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b e3_bind()],.** [
22f10 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
22f20 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c ameter_count()],
22f30 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 and.** [sqlite3
22f40 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
22f50 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 index()]..**.**
22f60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
22f70 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53 51 4c [H13621].*/.SQL
22f80 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
22f90 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 ar *sqlite3_bind
22fa0 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 _parameter_name(
22fb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
22fc0 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 nt);../*.** CAPI
22fd0 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 20 41 3REF: Index Of A
22fe0 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 68 20 Parameter With
22ff0 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b 48 31 A Given Name {H1
23000 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 3640} <S70300>.*
23010 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
23020 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 index of an SQL
23030 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 6e 20 parameter given
23040 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 0a 2a its name. The.*
23050 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 72 65 * index value re
23060 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 61 62 turned is suitab
23070 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 74 68 le for use as th
23080 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 e second.** para
23090 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
230a0 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 3_bind_blob|sqli
230b0 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 41 20 te3_bind()]. A
230c0 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 75 72 zero.** is retur
230d0 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 68 69 ned if no matchi
230e0 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 ng parameter is
230f0 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 72 61 found. The para
23100 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 6d 75 meter.** name mu
23110 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e 20 55 st be given in U
23120 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 68 65 TF-8 even if the
23130 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d original statem
23140 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 70 61 ent.** was prepa
23150 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 36 20 red from UTF-16
23160 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 text using [sqli
23170 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
23180 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ()]..**.** See a
23190 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 lso: [sqlite3_bi
231a0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f nd_blob|sqlite3_
231b0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c bind()],.** [sql
231c0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
231d0 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e ter_count()], an
231e0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 d.** [sqlite3_bi
231f0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
23200 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ex()]..**.** Req
23210 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
23220 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13641].*/.SQLITE
23230 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
23240 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
23250 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 index(sqlite3_st
23260 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 mt*, const char
23270 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *zName);../*.**
23280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 CAPI3REF: Reset
23290 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20 All Bindings On
232a0 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 A Prepared State
232b0 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20 3c 53 ment {H13660} <S
232c0 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 70300>.**.** Con
232d0 74 72 61 72 79 20 74 6f 20 74 68 65 20 69 6e 74 trary to the int
232e0 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 uition of many,
232f0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
23300 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 65 74 ] does not reset
23310 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 .** the [sqlite3
23320 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 69 6e _bind_blob | bin
23330 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70 72 65 dings] on a [pre
23340 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
23350 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 72 6f ..** Use this ro
23360 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74 20 61 utine to reset a
23370 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 ll host paramete
23380 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a rs to NULL..**.*
23390 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
233a0 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f 0a 53 ** [H13661].*/.S
233b0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
233c0 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 lite3_clear_bind
233d0 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
233e0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
233f0 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20 3REF: Number Of
23400 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65 73 Columns In A Res
23410 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30 7d ult Set {H13710}
23420 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
23430 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
23440 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
23450 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 the result set r
23460 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a eturned by the.*
23470 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 * [prepared stat
23480 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f 75 ement]. This rou
23490 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 69 tine returns 0 i
234a0 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53 51 f pStmt is an SQ
234b0 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 L.** statement t
234c0 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 74 hat does not ret
234d0 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65 78 urn data (for ex
234e0 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54 45 ample an [UPDATE
234f0 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ])..**.** Requir
23500 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 ements:.** [H137
23510 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 11].*/.SQLITE_AP
23520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
23530 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 lumn_count(sqlit
23540 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b e3_stmt *pStmt);
23550 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
23560 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49 : Column Names I
23570 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b n A Result Set {
23580 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30 30 3e H13720} <S10700>
23590 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
235a0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 tines return the
235b0 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 name assigned t
235c0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 o a particular c
235d0 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 olumn.** in the
235e0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 result set of a
235f0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
23600 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 nt. The sqlite3
23610 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 0a 2a _column_name().*
23620 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 * interface retu
23630 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
23640 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
23650 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a ed UTF-8 string.
23660 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 ** and sqlite3_c
23670 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 72 olumn_name16() r
23680 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
23690 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 to a zero-termi
236a0 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 nated.** UTF-16
236b0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 69 72 string. The fir
236c0 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 st parameter is
236d0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
236e0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 61 74 atement].** that
236f0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
23700 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
23710 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 nt. The second p
23720 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a arameter is the.
23730 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 ** column number
23740 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 . The leftmost
23750 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65 72 column is number
23760 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 0..**.** The re
23770 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 70 6f turned string po
23780 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20 75 inter is valid u
23790 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 65 20 ntil either the
237a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
237b0 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 74 72 ent].** is destr
237c0 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 oyed by [sqlite3
237d0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
237e0 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63 until the next c
237f0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
23800 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 3_column_name()
23810 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d or sqlite3_colum
23820 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20 74 68 n_name16() on th
23830 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e 0a 2a e same column..*
23840 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f *.** If sqlite3_
23850 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 malloc() fails d
23860 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63 65 73 uring the proces
23870 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72 20 72 sing of either r
23880 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72 20 65 outine.** (for e
23890 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20 61 20 xample during a
238a0 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 conversion from
238b0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 29 UTF-8 to UTF-16)
238c0 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 then a.** NULL
238d0 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 pointer is retur
238e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ned..**.** The n
238f0 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c 74 20 ame of a result
23900 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 76 61 column is the va
23910 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 22 20 lue of the "AS"
23920 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 clause for.** th
23930 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 74 68 at column, if th
23940 65 72 65 20 69 73 20 61 6e 20 41 53 20 63 6c 61 ere is an AS cla
23950 75 73 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 use. If there i
23960 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65 0a 2a s no AS clause.*
23970 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20 * then the name
23980 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 of the column is
23990 20 75 6e 73 70 65 63 69 66 69 65 64 20 61 6e 64 unspecified and
239a0 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d may change from
239b0 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 .** one release
239c0 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65 of SQLite to the
239d0 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 next..**.** Req
239e0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
239f0 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33 5d 20 13721] [H13723]
23a00 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37 32 35 [H13724] [H13725
23a10 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31 33 37 ] [H13726] [H137
23a20 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 27].*/.SQLITE_AP
23a30 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
23a40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
23a50 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
23a60 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
23a70 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
23a80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
23a90 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ame16(sqlite3_st
23aa0 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a mt*, int N);../*
23ab0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 6f .** CAPI3REF: So
23ac0 75 72 63 65 20 4f 66 20 44 61 74 61 20 49 6e 20 urce Of Data In
23ad0 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b A Query Result {
23ae0 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30 30 3e H13740} <S10700>
23af0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
23b00 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 61 20 tines provide a
23b10 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 6d 69 means to determi
23b20 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e 20 6f ne what column o
23b30 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c 65 20 f what.** table
23b40 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 61 73 in which databas
23b50 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 e a result of a
23b60 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
23b70 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e 0a 2a nt comes from..*
23b80 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
23b90 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74 61 e database or ta
23ba0 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 63 61 ble or column ca
23bb0 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 n be returned as
23bc0 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55 54 46 .** either a UTF
23bd0 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73 74 72 -8 or UTF-16 str
23be0 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74 61 62 ing. The _datab
23bf0 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 ase_ routines re
23c00 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 turn.** the data
23c10 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65 20 5f base name, the _
23c20 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65 73 20 table_ routines
23c30 72 65 74 75 72 6e 20 74 68 65 20 74 61 62 6c 65 return the table
23c40 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20 74 68 name, and.** th
23c50 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74 69 6e e origin_ routin
23c60 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f es return the co
23c70 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 lumn name..** Th
23c80 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
23c90 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c g is valid until
23ca0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
23cb0 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73 tatement] is des
23cc0 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e 67 20 troyed.** using
23cd0 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
23ce0 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 e()] or until th
23cf0 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 e same informati
23d00 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 0a on is requested.
23d10 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 64 69 ** again in a di
23d20 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 fferent encoding
23d30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 ..**.** The name
23d40 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 74 s returned are t
23d50 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e 2d 61 he original un-a
23d60 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f 66 20 liased names of
23d70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2c the.** database,
23d80 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f 6c 75 table, and colu
23d90 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 mn..**.** The fi
23da0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
23db0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 the following ca
23dc0 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70 61 72 lls is a [prepar
23dd0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a ed statement]..*
23de0 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e * These function
23df0 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 s return informa
23e00 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4e tion about the N
23e10 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e th column return
23e20 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 74 61 ed by.** the sta
23e30 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 4e 20 tement, where N
23e40 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75 is the second fu
23e50 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e nction argument.
23e60 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4e 74 .**.** If the Nt
23e70 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e 65 h column returne
23e80 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 d by the stateme
23e90 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 nt is an express
23ea0 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71 75 65 ion or.** subque
23eb0 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20 ry and is not a
23ec0 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 column value, th
23ed0 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 en all of these
23ee0 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
23ef0 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65 73 65 .** NULL. These
23f00 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61 routine might a
23f10 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 lso return NULL
23f20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f if a memory allo
23f30 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a 2a 20 cation error.**
23f40 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72 77 69 occurs. Otherwi
23f50 73 65 2c 20 74 68 65 79 20 72 65 74 75 72 6e 20 se, they return
23f60 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
23f70 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
23f80 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 e, table.** and
23f90 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 65 72 column that quer
23fa0 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 y result column
23fb0 77 61 73 20 65 78 74 72 61 63 74 65 64 20 66 72 was extracted fr
23fc0 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 69 74 om..**.** As wit
23fd0 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51 4c 69 h all other SQLi
23fe0 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65 20 70 te APIs, those p
23ff0 6f 73 74 66 69 78 65 64 20 77 69 74 68 20 22 31 ostfixed with "1
24000 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55 54 46 6" return.** UTF
24010 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74 72 69 -16 encoded stri
24020 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72 20 66 ngs, the other f
24030 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
24040 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a UTF-8. {END}.**.
24050 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20 61 72 ** These APIs ar
24060 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 e only available
24070 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
24080 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 was compiled wit
24090 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 h the.** [SQLITE
240a0 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
240b0 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72 ETADATA] C-prepr
240c0 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 ocessor symbol d
240d0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41 efined..**.** {A
240e0 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74 77 6f 13751}.** If two
240f0 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 or more threads
24100 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 call one or mor
24110 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 e of these routi
24120 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 nes against the
24130 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 same.** prepared
24140 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 statement and c
24150 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73 61 6d olumn at the sam
24160 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65 20 e time then the
24170 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a 20 75 results are.** u
24180 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
24190 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
241a0 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33 37 34 [H13741] [H1374
241b0 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48 31 33 2] [H13743] [H13
241c0 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20 5b 48 744] [H13745] [H
241d0 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38 5d 0a 13746] [H13748].
241e0 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 **.** If two or
241f0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c more threads cal
24200 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a l one or more.**
24210 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
24220 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 7c _database_name |
24230 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 74 61 column metadata
24240 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a 2a 20 interfaces].**
24250 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b 70 72 for the same [pr
24260 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
24270 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 6f 6c ] and result col
24280 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 umn.** at the sa
24290 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65 me time then the
242a0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 results are und
242b0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
242c0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
242d0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
242e0 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 _database_name(s
242f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
24300 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
24310 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
24320 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 3_column_databas
24330 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 e_name16(sqlite3
24340 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
24350 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
24360 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
24370 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 mn_table_name(sq
24380 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
24390 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
243a0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
243b0 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
243c0 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
243d0 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
243e0 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
243f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f sqlite3_column_o
24400 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 rigin_name(sqlit
24410 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 e3_stmt*,int);.S
24420 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
24430 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
24440 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 lumn_origin_name
24450 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
24460 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ,int);../*.** CA
24470 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 64 PI3REF: Declared
24480 20 44 61 74 61 74 79 70 65 20 4f 66 20 41 20 51 Datatype Of A Q
24490 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48 31 33 uery Result {H13
244a0 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 760} <S10700>.**
244b0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
244c0 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b 70 72 rameter is a [pr
244d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
244e0 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 73 74 ]..** If this st
244f0 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 53 45 atement is a [SE
24500 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 LECT] statement
24510 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 and the Nth colu
24520 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 74 mn of the.** ret
24530 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 65 74 urned result set
24540 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 43 54 of that [SELECT
24550 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 6f 6c ] is a table col
24560 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a 20 65 umn (not an.** e
24570 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75 62 xpression or sub
24580 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20 query) then the
24590 64 65 63 6c 61 72 65 64 20 74 79 70 65 20 6f 66 declared type of
245a0 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 6f the table.** co
245b0 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 lumn is returned
245c0 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f . If the Nth co
245d0 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 lumn of the resu
245e0 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a 2a 20 lt set is an.**
245f0 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75 expression or su
24600 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61 20 4e bquery, then a N
24610 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 ULL pointer is r
24620 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 eturned..** The
24630 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
24640 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 20 is always UTF-8
24650 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d 0a 2a encoded. {END}.*
24660 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 *.** For example
24670 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61 74 61 , given the data
24680 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a base schema:.**.
24690 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
246a0 74 31 28 63 31 20 56 41 52 49 41 4e 54 29 3b 0a t1(c1 VARIANT);.
246b0 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 6f **.** and the fo
246c0 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e llowing statemen
246d0 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 t to be compiled
246e0 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 20 63 :.**.** SELECT c
246f0 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d 20 74 1 + 1, c1 FROM t
24700 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 72 6f 1;.**.** this ro
24710 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 74 75 utine would retu
24720 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20 22 56 rn the string "V
24730 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68 65 20 ARIANT" for the
24740 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a 2a 2a second result.**
24750 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 2c 20 column (i==1),
24760 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 and a NULL point
24770 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 er for the first
24780 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 28 result column (
24790 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c i==0)..**.** SQL
247a0 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 ite uses dynamic
247b0 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69 6e 67 run-time typing
247c0 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63 61 75 . So just becau
247d0 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 se a column.** i
247e0 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20 63 6f s declared to co
247f0 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63 75 6c ntain a particul
24800 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e 6f 74 ar type does not
24810 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 0a 2a mean that the.*
24820 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e * data stored in
24830 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 that column is
24840 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65 64 20 of the declared
24850 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20 69 73 type. SQLite is
24860 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74 79 70 .** strongly typ
24870 65 64 2c 20 62 75 74 20 74 68 65 20 74 79 70 69 ed, but the typi
24880 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20 6e 6f ng is dynamic no
24890 74 20 73 74 61 74 69 63 2e 20 20 54 79 70 65 0a t static. Type.
248a0 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 ** is associated
248b0 20 77 69 74 68 20 69 6e 64 69 76 69 64 75 61 6c with individual
248c0 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77 69 74 values, not wit
248d0 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 73 h the containers
248e0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 .** used to hold
248f0 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e 0a 2a those values..*
24900 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
24910 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d 20 5b s:.** [H13761] [
24920 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36 33 5d H13762] [H13763]
24930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
24940 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
24950 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 e3_column_declty
24960 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a pe(sqlite3_stmt*
24970 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ,int);.SQLITE_AP
24980 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
24990 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 lite3_column_dec
249a0 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f ltype16(sqlite3_
249b0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a stmt*,int);../*.
249c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 ** CAPI3REF: Eva
249d0 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 luate An SQL Sta
249e0 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 tement {H13200}
249f0 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 <S10000>.**.** A
24a00 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 fter a [prepared
24a10 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 statement] has
24a20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 been prepared us
24a30 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 ing either.** [s
24a40 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
24a50 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 2()] or [sqlite3
24a60 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d _prepare16_v2()]
24a70 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c or one of the l
24a80 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 egacy.** interfa
24a90 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 ces [sqlite3_pre
24aa0 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 pare()] or [sqli
24ab0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
24ac0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a , this function.
24ad0 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 ** must be calle
24ae0 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 d one or more ti
24af0 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 mes to evaluate
24b00 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a the statement..*
24b10 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 *.** The details
24b20 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 of the behavior
24b30 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
24b40 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 step() interface
24b50 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 depend.** on wh
24b60 65 74 68 65 72 20 74 68 65 20 73 74 61 74 65 6d ether the statem
24b70 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 64 ent was prepared
24b80 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 using the newer
24b90 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a "v2" interface.
24ba0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
24bb0 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 are_v2()] and [s
24bc0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
24bd0 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c _v2()] or the ol
24be0 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e der legacy.** in
24bf0 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 terface [sqlite3
24c00 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 _prepare()] and
24c10 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
24c20 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 16()]. The use
24c30 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 of the.** new "v
24c40 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 2" interface is
24c50 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
24c60 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 new applications
24c70 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a but the legacy.
24c80 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c ** interface wil
24c90 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 l continue to be
24ca0 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a supported..**.*
24cb0 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 * In the legacy
24cc0 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 interface, the r
24cd0 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c eturn value will
24ce0 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 be either [SQLI
24cf0 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 TE_BUSY],.** [SQ
24d00 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c LITE_DONE], [SQL
24d10 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 ITE_ROW], [SQLIT
24d20 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 E_ERROR], or [SQ
24d30 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a LITE_MISUSE]..**
24d40 20 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69 With the "v2" i
24d50 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 nterface, any of
24d60 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 the other [resu
24d70 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 lt codes] or.**
24d80 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 [extended result
24d90 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 codes] might be
24da0 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c returned as wel
24db0 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 l..**.** [SQLITE
24dc0 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 _BUSY] means tha
24dd0 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 t the database e
24de0 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 ngine was unable
24df0 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a to acquire the.
24e00 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b ** database lock
24e10 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f s it needs to do
24e20 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 its job. If th
24e30 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 e statement is a
24e40 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 [COMMIT].** or
24e50 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f occurs outside o
24e60 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 f an explicit tr
24e70 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
24e80 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 you can retry th
24e90 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 e.** statement.
24ea0 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e If the statemen
24eb0 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d t is not a [COMM
24ec0 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 IT] and occurs w
24ed0 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 ithin a.** expli
24ee0 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 cit transaction
24ef0 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 then you should
24f00 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 rollback the tra
24f10 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a nsaction before.
24f20 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a ** continuing..*
24f30 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e *.** [SQLITE_DON
24f40 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 E] means that th
24f50 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 e statement has
24f60 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 finished executi
24f70 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c ng.** successful
24f80 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 ly. sqlite3_ste
24f90 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 p() should not b
24fa0 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f e called again o
24fb0 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a n this virtual.*
24fc0 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 * machine withou
24fd0 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 t first calling
24fe0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
24ff0 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 ] to reset the v
25000 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e irtual.** machin
25010 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e e back to its in
25020 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a itial state..**.
25030 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 ** If the SQL st
25040 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 atement being ex
25050 65 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 ecuted returns a
25060 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 ny data, then [S
25070 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 QLITE_ROW].** is
25080 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 returned each t
25090 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 ime a new row of
250a0 20 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66 data is ready f
250b0 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 or processing by
250c0 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 the.** caller.
250d0 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 The values may b
250e0 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 e accessed using
250f0 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 the [column acc
25100 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a ess functions]..
25110 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 ** sqlite3_step(
25120 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 ) is called agai
25130 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 n to retrieve th
25140 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 e next row of da
25150 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 ta..**.** [SQLIT
25160 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 E_ERROR] means t
25170 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 hat a run-time e
25180 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 rror (such as a
25190 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 constraint.** vi
251a0 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 olation) has occ
251b0 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f urred. sqlite3_
251c0 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f step() should no
251d0 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 t be called agai
251e0 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 n on.** the VM.
251f0 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e More information
25200 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 may be found by
25210 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
25220 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 3_errmsg()]..**
25230 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 With the legacy
25240 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 interface, a mor
25250 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 e specific error
25260 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 code (for examp
25270 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 le,.** [SQLITE_I
25280 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 NTERRUPT], [SQLI
25290 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c TE_SCHEMA], [SQL
252a0 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e ITE_CORRUPT], an
252b0 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 d so forth).** c
252c0 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 an be obtained b
252d0 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 y calling [sqlit
252e0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 e3_reset()] on t
252f0 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 he.** [prepared
25300 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 statement]. In
25310 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
25320 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 ce,.** the more
25330 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 specific error c
25340 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
25350 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 directly by sqli
25360 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a te3_step()..**.*
25370 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 * [SQLITE_MISUSE
25380 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 ] means that the
25390 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
253a0 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f s called inappro
253b0 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 priately..** Per
253c0 68 61 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c haps it was call
253d0 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 ed on a [prepare
253e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 d statement] tha
253f0 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 t has.** already
25400 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 been [sqlite3_f
25410 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 inalize | finali
25420 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 zed] or on one t
25430 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 hat had.** previ
25440 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b ously returned [
25450 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 SQLITE_ERROR] or
25460 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 [SQLITE_DONE].
25470 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 Or it could.**
25480 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
25490 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
254a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
254b0 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 being used by t
254c0 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 wo or.** more th
254d0 72 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d reads at the sam
254e0 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 e moment in time
254f0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 ..**.** <b>Goofy
25500 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 Interface Alert
25510 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 :</b> In the leg
25520 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 acy interface, t
25530 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 he sqlite3_step(
25540 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 ).** API always
25550 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 returns a generi
25560 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 c error code, [S
25570 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f QLITE_ERROR], fo
25580 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 llowing any.** e
25590 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 rror other than
255a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e [SQLITE_BUSY] an
255b0 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 d [SQLITE_MISUSE
255c0 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c ]. You must cal
255d0 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 l.** [sqlite3_re
255e0 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 set()] or [sqlit
255f0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 e3_finalize()] i
25600 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 n order to find
25610 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 one of the.** sp
25620 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f ecific [error co
25630 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 des] that better
25640 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 describes the e
25650 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 rror..** We admi
25660 74 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61 t that this is a
25670 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 goofy design.
25680 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 The problem has
25690 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 been fixed.** wi
256a0 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 th the "v2" inte
256b0 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 rface. If you p
256c0 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f repare all of yo
256d0 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ur SQL statement
256e0 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 s.** using eithe
256f0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 r [sqlite3_prepa
25700 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c re_v2()] or [sql
25710 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
25720 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 2()] instead.**
25730 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 of the legacy [s
25740 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
25750 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 ] and [sqlite3_p
25760 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 repare16()] inte
25770 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 rfaces,.** then
25780 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 the more specifi
25790 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 c [error codes]
257a0 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 are returned dir
257b0 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 ectly.** by sqli
257c0 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 te3_step(). The
257d0 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 use of the "v2"
257e0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 interface is re
257f0 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a commended..**.**
25800 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
25810 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31 35 33 * [H13202] [H153
25820 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b 48 31 04] [H15306] [H1
25830 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d 0a 2a 5308] [H15310].*
25840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
25850 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 sqlite3_step(sq
25860 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f lite3_stmt*);../
25870 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e *.** CAPI3REF: N
25880 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
25890 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73 65 74 in a result set
258a0 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30 37 30 {H13770} <S1070
258b0 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 0>.**.** Returns
258c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 the number of v
258d0 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 75 72 alues in the cur
258e0 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 rent row of the
258f0 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a result set..**.*
25900 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
25910 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48 31 33 ** [H13771] [H13
25920 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 772].*/.SQLITE_A
25930 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
25940 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ata_count(sqlite
25950 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 3_stmt *pStmt);.
25960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
25970 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 Fundamental Dat
25980 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 atypes {H10265}
25990 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e <S10110><S10120>
259a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 .** KEYWORDS: SQ
259b0 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 LITE_TEXT.**.**
259c0 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 {H10266} Every v
259d0 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 alue in SQLite h
259e0 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 as one of five f
259f0 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 undamental datat
25a00 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ypes:.**.** <ul>
25a10 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 .** <li> 64-bit
25a20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
25a30 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 * <li> 64-bit IE
25a40 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e EE floating poin
25a50 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e t number.** <li>
25a60 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 string.** <li>
25a70 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c BLOB.** <li> NUL
25a80 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d L.** </ul> {END}
25a90 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
25aa0 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 stants are codes
25ab0 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f for each of tho
25ac0 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 se types..**.**
25ad0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 Note that the SQ
25ae0 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 LITE_TEXT consta
25af0 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 nt was also used
25b00 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 in SQLite versi
25b10 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f on 2.** for a co
25b20 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 mpletely differe
25b30 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 nt meaning. Sof
25b40 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 tware that links
25b50 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a against both.**
25b60 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
25b70 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 2 and SQLite ver
25b80 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 sion 3 should us
25b90 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 e SQLITE3_TEXT,
25ba0 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 not.** SQLITE_TE
25bb0 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 XT..*/.#define S
25bc0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 QLITE_INTEGER 1
25bd0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
25be0 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 FLOAT 2.#defi
25bf0 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 ne SQLITE_BLOB
25c00 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 4.#define SQL
25c10 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 ITE_NULL 5.#
25c20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 ifdef SQLITE_TEX
25c30 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 T.# undef SQLITE
25c40 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 _TEXT.#else.# de
25c50 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 fine SQLITE_TEXT
25c60 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 3.#endif.#d
25c70 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 efine SQLITE3_TE
25c80 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 XT 3../*.**
25c90 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 CAPI3REF: Result
25ca0 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 Values From A Q
25cb0 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 uery {H13800} <S
25cc0 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10700>.** KEYWOR
25cd0 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 DS: {column acce
25ce0 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a ss functions}.**
25cf0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
25d00 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 es form the "res
25d10 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 ult set query" i
25d20 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
25d30 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 These routines r
25d40 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f eturn informatio
25d50 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 n about a single
25d60 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 column of the c
25d70 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 urrent.** result
25d80 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e row of a query.
25d90 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 In every case
25da0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
25db0 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a nt is a pointer.
25dc0 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 ** to the [prepa
25dd0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
25de0 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 hat is being eva
25df0 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c luated (the [sql
25e00 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 ite3_stmt*].** t
25e10 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 hat was returned
25e20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 from [sqlite3_p
25e30 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
25e40 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 one of its varia
25e50 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 nts).** and the
25e60 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
25e70 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
25e80 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 the column for w
25e90 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hich information
25ea0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 .** should be re
25eb0 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 turned. The lef
25ec0 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 tmost column of
25ed0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 the result set h
25ee0 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a as the index 0..
25ef0 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** The number of
25f00 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
25f10 72 65 73 75 6c 74 20 63 61 6e 20 62 65 20 64 65 result can be de
25f20 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 0a 2a termined using.*
25f30 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d * [sqlite3_colum
25f40 6e 5f 63 6f 75 6e 74 28 29 5d 2e 0a 2a 2a 0a 2a n_count()]..**.*
25f50 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 * If the SQL sta
25f60 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 tement does not
25f70 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 currently point
25f80 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 to a valid row,
25f90 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c or if the.** col
25fa0 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74 umn index is out
25fb0 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72 of range, the r
25fc0 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e esult is undefin
25fd0 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 ed..** These rou
25fe0 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 tines may only b
25ff0 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 e called when th
26000 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 e most recent ca
26010 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 ll to.** [sqlite
26020 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 3_step()] has re
26030 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 turned [SQLITE_R
26040 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a OW] and neither.
26050 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 ** [sqlite3_rese
26060 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65 t()] nor [sqlite
26070 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61 3_finalize()] ha
26080 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 ve been called s
26090 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 ubsequently..**
260a0 49 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 If any of these
260b0 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c routines are cal
260c0 6c 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 led after [sqlit
260d0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a e3_reset()] or.*
260e0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c * [sqlite3_final
260f0 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 ize()] or after
26100 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
26110 20 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a has returned.**
26120 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 something other
26130 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f than [SQLITE_RO
26140 57 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 W], the results
26150 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a are undefined..*
26160 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 * If [sqlite3_st
26170 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 ep()] or [sqlite
26180 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 3_reset()] or [s
26190 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
261a0 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 )].** are called
261b0 20 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e from a differen
261c0 74 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 t thread while a
261d0 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ny of these rout
261e0 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 ines.** are pend
261f0 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ing, then the re
26200 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 sults are undefi
26210 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ned..**.** The s
26220 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 qlite3_column_ty
26230 70 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 pe() routine ret
26240 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c urns the.** [SQL
26250 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 ITE_INTEGER | da
26260 74 61 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 tatype code] for
26270 20 74 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 the initial dat
26280 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 a type.** of the
26290 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 result column.
262a0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 The returned va
262b0 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 lue is one of [S
262c0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a QLITE_INTEGER],.
262d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ** [SQLITE_FLOAT
262e0 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d ], [SQLITE_TEXT]
262f0 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c , [SQLITE_BLOB],
26300 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c or [SQLITE_NULL
26310 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a ]. The value.**
26320 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
26330 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 ite3_column_type
26340 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 () is only meani
26350 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 ngful if no type
26360 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 .** conversions
26370 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 have occurred as
26380 20 64 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 described below
26390 2e 20 20 41 66 74 65 72 20 61 20 74 79 70 65 20 . After a type
263a0 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 conversion,.** t
263b0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
263c0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
263d0 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e umn_type() is un
263e0 64 65 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 defined. Future
263f0 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 .** versions of
26400 53 51 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 SQLite may chang
26410 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f e the behavior o
26420 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e f sqlite3_column
26430 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f _type().** follo
26440 77 69 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 wing a type conv
26450 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 ersion..**.** If
26460 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 the result is a
26470 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 BLOB or UTF-8 s
26480 74 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 tring then the s
26490 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
264a0 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 tes().** routine
264b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
264c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
264d0 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 that BLOB or str
264e0 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 ing..** If the r
264f0 65 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 esult is a UTF-1
26500 36 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 6 string, then s
26510 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
26520 74 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a tes() converts.*
26530 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 * the string to
26540 55 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 UTF-8 and then r
26550 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
26560 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 r of bytes..** I
26570 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 f the result is
26580 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 a numeric value
26590 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c then sqlite3_col
265a0 75 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 umn_bytes() uses
265b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 .** [sqlite3_snp
265c0 72 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 rintf()] to conv
265d0 65 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 ert that value t
265e0 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 o a UTF-8 string
265f0 20 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 and returns.**
26600 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
26610 74 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 tes in that stri
26620 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 ng..** The value
26630 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e returned does n
26640 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a ot include the z
26650 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 ero terminator a
26660 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 t the end.** of
26670 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 the string. For
26680 20 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 clarity: the va
26690 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
266a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
266b0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 bytes in the st
266c0 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 ring, not the nu
266d0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
266e0 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 rs..**.** String
266f0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 s returned by sq
26700 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
26710 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f t() and sqlite3_
26720 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c column_text16(),
26730 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 .** even empty s
26740 74 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 trings, are alwa
26750 79 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 ys zero terminat
26760 65 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a ed. The return.
26770 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 ** value from sq
26780 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
26790 62 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c b() for a zero-l
267a0 65 6e 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e ength BLOB is an
267b0 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f arbitrary.** po
267c0 69 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 inter, possibly
267d0 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e even a NULL poin
267e0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ter..**.** The s
267f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
26800 74 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 tes16() routine
26810 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 is similar to sq
26820 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
26830 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 es().** but leav
26840 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e es the result in
26850 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 UTF-16 in nativ
26860 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 e byte order ins
26870 74 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a tead of UTF-8..*
26880 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 * The zero termi
26890 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63 nator is not inc
268a0 6c 75 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f luded in this co
268b0 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f unt..**.** The o
268c0 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 bject returned b
268d0 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d y [sqlite3_colum
268e0 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e n_value()] is an
268f0 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 .** [unprotected
26900 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
26910 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 object. An unpr
26920 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
26930 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 value object.**
26940 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 may only be used
26950 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 with [sqlite3_b
26960 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 ind_value()] and
26970 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
26980 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 _value()]..** If
26990 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 the [unprotecte
269a0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
269b0 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 object returned
269c0 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f by.** [sqlite3_
269d0 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 column_value()]
269e0 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f is used in any o
269f0 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 ther way, includ
26a00 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 ing calls.** to
26a10 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 routines like [s
26a20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
26a30 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 ()], [sqlite3_va
26a40 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 lue_text()],.**
26a50 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 or [sqlite3_valu
26a60 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e e_bytes()], then
26a70 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 the behavior is
26a80 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
26a90 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
26aa0 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 attempt to conv
26ab0 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 ert the value wh
26ac0 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e ere appropriate.
26ad0 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 For.** example
26ae0 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 , if the interna
26af0 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
26b00 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 is FLOAT and a
26b10 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 text result.** i
26b20 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 s requested, [sq
26b30 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
26b40 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e ] is used intern
26b50 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20 ally to perform
26b60 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f the.** conversio
26b70 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e n automatically.
26b80 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
26b90 74 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68 table details th
26ba0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a e conversions.**
26bb0 20 74 68 61 74 20 61 72 65 20 61 70 70 6c 69 65 that are applie
26bc0 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 d:.**.** <blockq
26bd0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 uote>.** <table
26be0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c border="1">.** <
26bf0 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c tr><th> Internal
26c00 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65 <br>Type <th> Re
26c10 71 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20 quested<br>Type
26c20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e <th> Conversion
26c30 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 .**.** <tr><td>
26c40 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e NULL <td> IN
26c50 54 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 TEGER <td> Res
26c60 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e ult is 0.** <tr>
26c70 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 <td> NULL <t
26c80 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 d> FLOAT <td
26c90 3e 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a > Result is 0.0.
26ca0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c ** <tr><td> NUL
26cb0 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 L <td> TEXT
26cc0 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 <td> Result
26cd0 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a is NULL pointer.
26ce0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c ** <tr><td> NUL
26cf0 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 L <td> BLOB
26d00 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 <td> Result
26d10 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a is NULL pointer.
26d20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 ** <tr><td> INTE
26d30 47 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 GER <td> FLOAT
26d40 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 <td> Convert
26d50 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f from integer to
26d60 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 float.** <tr><t
26d70 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e d> INTEGER <td>
26d80 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 TEXT <td>
26d90 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 ASCII rendering
26da0 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a of the integer.*
26db0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 * <tr><td> INTEG
26dc0 45 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 ER <td> BLOB
26dd0 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 <td> Same as
26de0 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a INTEGER->TEXT.**
26df0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 <tr><td> FLOAT
26e00 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 <td> INTEGER
26e10 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 <td> Convert f
26e20 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74 rom float to int
26e30 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e eger.** <tr><td>
26e40 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 FLOAT <td>
26e50 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 TEXT <td> AS
26e60 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 CII rendering of
26e70 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 the float.** <t
26e80 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 r><td> FLOAT
26e90 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c <td> BLOB <
26ea0 74 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41 td> Same as FLOA
26eb0 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c T->TEXT.** <tr><
26ec0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 td> TEXT <td
26ed0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e > INTEGER <td>
26ee0 20 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c Use atoi().** <
26ef0 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 tr><td> TEXT
26f00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 <td> FLOAT
26f10 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a <td> Use atof().
26f20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 ** <tr><td> TEX
26f30 54 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 T <td> BLOB
26f40 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e <td> No chan
26f50 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ge.** <tr><td>
26f60 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54 BLOB <td> INT
26f70 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 EGER <td> Conv
26f80 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e ert to TEXT then
26f90 20 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c use atoi().** <
26fa0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 tr><td> BLOB
26fb0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 <td> FLOAT
26fc0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 <td> Convert to
26fd0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 TEXT then use at
26fe0 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e of().** <tr><td>
26ff0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 BLOB <td>
27000 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64 TEXT <td> Ad
27010 64 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 d a zero termina
27020 74 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a tor if needed.**
27030 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 </table>.** </b
27040 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
27050 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65 The table above
27060 20 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65 makes reference
27070 20 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c to standard C l
27080 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 ibrary functions
27090 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61 atoi().** and a
270a0 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64 tof(). SQLite d
270b0 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75 oes not really u
270c0 73 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f se these functio
270d0 6e 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a ns. It has its.
270e0 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e ** own equivalen
270f0 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 t internal routi
27100 6e 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 nes. The atoi()
27110 20 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 and atof() name
27120 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e s are.** used in
27130 20 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 the table for b
27140 72 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 revity and becau
27150 73 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 se they are fami
27160 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 liar to most.**
27170 43 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a C programmers..*
27180 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 *.** Note that w
27190 68 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 hen type convers
271a0 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e ions occur, poin
271b0 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 ters returned by
271c0 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 prior.** calls
271d0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d to sqlite3_colum
271e0 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 n_blob(), sqlite
271f0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 3_column_text(),
27200 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 and/or.** sqlit
27210 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
27220 28 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 () may be invali
27230 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63 dated..** Type c
27240 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70 onversions and p
27250 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74 ointer invalidat
27260 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72 ions might occur
27270 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f .** in the follo
27280 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a wing cases:.**.*
27290 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 * <ul>.** <li> T
272a0 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 he initial conte
272b0 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64 nt is a BLOB and
272c0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
272d0 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20 text() or.**
272e0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
272f0 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c _text16() is cal
27300 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 led. A zero-ter
27310 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a minator might.**
27320 20 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65 need to be
27330 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 74 added to the st
27340 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c ring.</li>.** <l
27350 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 i> The initial c
27360 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 ontent is UTF-8
27370 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 text and sqlite3
27380 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
27390 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c ) or.** sql
273a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
273b0 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 16() is called.
273c0 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 The content mus
273d0 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a t be converted.*
273e0 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36 * to UTF-16
273f0 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 .</li>.** <li> T
27400 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 he initial conte
27410 6e 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 nt is UTF-16 tex
27420 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f t and sqlite3_co
27430 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a lumn_bytes() or.
27440 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ** sqlite3_
27450 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 column_text() is
27460 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f called. The co
27470 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f ntent must be co
27480 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 nverted.**
27490 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a to UTF-8.</li>.*
274a0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f * </ul>.**.** Co
274b0 6e 76 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 nversions betwee
274c0 6e 20 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 n UTF-16be and U
274d0 54 46 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 TF-16le are alwa
274e0 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 ys done in place
274f0 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 and do.** not i
27500 6e 76 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f nvalidate a prio
27510 72 20 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 r pointer, thoug
27520 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 h of course the
27530 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 content of the b
27540 75 66 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 uffer.** that th
27550 65 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 e prior pointer
27560 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 points to will h
27570 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 ave been modifie
27580 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a d. Other kinds.
27590 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e ** of conversion
275a0 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 are done in pla
275b0 63 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f ce when it is po
275c0 73 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 ssible, but some
275d0 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72 times they.** ar
275e0 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61 e not possible a
275f0 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65 nd in those case
27600 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73 s prior pointers
27610 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 are invalidated
27620 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65 ..**.** The safe
27630 73 74 20 61 6e 64 20 65 61 73 69 65 73 74 20 74 st and easiest t
27640 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63 o remember polic
27650 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74 y is to invoke t
27660 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a hese routines.**
27670 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 in one of the f
27680 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a ollowing ways:.*
27690 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c *.** <ul>.** <l
276a0 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e i>sqlite3_column
276b0 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 _text() followed
276c0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by sqlite3_colu
276d0 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a mn_bytes()</li>.
276e0 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f ** <li>sqlite3_
276f0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f column_blob() fo
27700 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 llowed by sqlite
27710 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
27720 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 </li>.** <li>sq
27730 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
27740 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 t16() followed b
27750 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e y sqlite3_column
27760 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a _bytes16()</li>.
27770 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 ** </ul>.**.** I
27780 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 n other words, y
27790 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 ou should call s
277a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
277b0 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 xt(),.** sqlite3
277c0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 _column_blob(),
277d0 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d or sqlite3_colum
277e0 6e 5f 74 65 78 74 31 36 28 29 20 66 69 72 73 74 n_text16() first
277f0 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 to force the re
27800 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 sult.** into the
27810 20 64 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c desired format,
27820 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c then invoke sql
27830 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
27840 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 s() or.** sqlite
27850 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
27860 28 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 () to find the s
27870 69 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c ize of the resul
27880 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 t. Do not mix c
27890 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 alls.** to sqlit
278a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
278b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 or sqlite3_colu
278c0 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63 mn_blob() with c
278d0 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 alls to.** sqlit
278e0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
278f0 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20 6(), and do not
27900 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c mix calls to sql
27910 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
27920 31 36 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c 16().** with cal
27930 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ls to sqlite3_co
27940 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a lumn_bytes()..**
27950 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 .** The pointers
27960 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 76 61 returned are va
27970 6c 69 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65 lid until a type
27980 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 conversion occu
27990 72 73 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62 rs as.** describ
279a0 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74 ed above, or unt
279b0 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 il [sqlite3_step
279c0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
279d0 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b reset()] or.** [
279e0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
279f0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 ()] is called.
27a00 54 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 The memory space
27a10 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74 used to hold st
27a20 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f rings.** and BLO
27a30 42 73 20 69 73 20 66 72 65 65 64 20 61 75 74 6f Bs is freed auto
27a40 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c matically. Do <
27a50 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74 b>not</b> pass t
27a60 68 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 he pointers retu
27a70 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 rned.** [sqlite3
27a80 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c _column_blob()],
27a90 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
27aa0 5f 74 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69 _text()], etc. i
27ab0 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f nto.** [sqlite3_
27ac0 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 free()]..**.** I
27ad0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
27ae0 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 ation error occu
27af0 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 rs during the ev
27b00 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a aluation of any.
27b10 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ** of these rout
27b20 69 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20 ines, a default
27b30 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 value is returne
27b40 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 d. The default
27b50 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68 value.** is eith
27b60 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 20 30 er the integer 0
27b70 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 , the floating p
27b80 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c oint number 0.0,
27b90 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f or a NULL.** po
27ba0 69 6e 74 65 72 2e 20 20 53 75 62 73 65 71 75 65 inter. Subseque
27bb0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c nt calls to [sql
27bc0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 ite3_errcode()]
27bd0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b will return.** [
27be0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a SQLITE_NOMEM]..*
27bf0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
27c00 73 3a 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b s:.** [H13803] [
27c10 48 31 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d H13806] [H13809]
27c20 20 5b 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31 [H13812] [H1381
27c30 35 5d 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33 5] [H13818] [H13
27c40 38 32 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a 821] [H13824].**
27c50 20 5b 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33 [H13827] [H1383
27c60 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 0].*/.SQLITE_API
27c70 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
27c80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
27c90 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
27ca0 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 int iCol);.SQLIT
27cb0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
27cc0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 3_column_bytes(s
27cd0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27ce0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27cf0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
27d00 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 column_bytes16(s
27d10 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27d20 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27d30 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 API double sqlit
27d40 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 e3_column_double
27d50 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
27d60 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 int iCol);.SQLIT
27d70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
27d80 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 3_column_int(sql
27d90 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
27da0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
27db0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
27dc0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
27dd0 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d nt64(sqlite3_stm
27de0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
27df0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
27e00 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 unsigned char *s
27e10 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
27e20 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a xt(sqlite3_stmt*
27e30 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
27e40 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
27e50 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
27e60 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 mn_text16(sqlite
27e70 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
27e80 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
27e90 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
27ea0 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 n_type(sqlite3_s
27eb0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
27ec0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
27ed0 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 te3_value *sqlit
27ee0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
27ef0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
27f00 6e 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a nt iCol);../*.**
27f10 20 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 CAPI3REF: Destr
27f20 6f 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74 oy A Prepared St
27f30 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b atement Object {
27f40 48 31 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e H13300} <S70300>
27f50 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30100>.**.** T
27f60 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c he sqlite3_final
27f70 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 ize() function i
27f80 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 s called to dele
27f90 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 te a [prepared s
27fa0 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 tatement]..** If
27fb0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 the statement w
27fc0 61 73 20 65 78 65 63 75 74 65 64 20 73 75 63 63 as executed succ
27fd0 65 73 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20 essfully or not
27fe0 65 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c executed at all,
27ff0 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f then.** SQLITE_
28000 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OK is returned.
28010 49 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 If execution of
28020 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 the statement fa
28030 69 6c 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20 iled then an.**
28040 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 [error code] or
28050 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 [extended error
28060 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 code] is returne
28070 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
28080 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c utine can be cal
28090 6c 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 led at any point
280a0 20 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 during the exec
280b0 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 ution of the.**
280c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
280d0 65 6e 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69 ent]. If the vi
280e0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 rtual machine ha
280f0 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74 s not.** complet
28100 65 64 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65 ed execution whe
28110 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
28120 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69 s called, that i
28130 73 20 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e s like.** encoun
28140 74 65 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 tering an error
28150 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 or an [sqlite3_i
28160 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72 nterrupt | inter
28170 72 75 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 rupt]..** Incomp
28180 6c 65 74 65 20 75 70 64 61 74 65 73 20 6d 61 79 lete updates may
28190 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 be rolled back
281a0 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 and transactions
281b0 20 63 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65 canceled,.** de
281c0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63 pending on the c
281d0 69 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e ircumstances, an
281e0 64 20 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20 d the.** [error
281f0 63 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77 code] returned w
28200 69 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 ill be [SQLITE_A
28210 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 BORT]..**.** Req
28220 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
28230 31 31 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a 11302] [H11304].
28240 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
28250 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 t sqlite3_finali
28260 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ze(sqlite3_stmt
28270 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
28280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 CAPI3REF: Reset
28290 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 A Prepared State
282a0 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 ment Object {H13
282b0 33 33 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 330} <S70300>.**
282c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
282d0 72 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e reset() function
282e0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 is called to re
282f0 73 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20 set a [prepared
28300 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 statement].** ob
28310 6a 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73 ject back to its
28320 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 initial state,
28330 72 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 ready to be re-e
28340 78 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 xecuted..** Any
28350 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 SQL statement va
28360 72 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 riables that had
28370 20 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f values bound to
28380 20 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 them using.** t
28390 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 he [sqlite3_bind
283a0 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f _blob | sqlite3_
283b0 62 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 bind_*() API] re
283c0 74 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 tain their value
283d0 73 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 s..** Use [sqlit
283e0 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 e3_clear_binding
283f0 73 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 s()] to reset th
28400 65 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a e bindings..**.*
28410 2a 20 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b * {H11332} The [
28420 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 sqlite3_reset(S)
28430 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 ] interface rese
28440 74 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 ts the [prepared
28450 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a statement] S.**
28460 20 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74 back t
28470 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 o the beginning
28480 6f 66 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a of its program..
28490 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49 **.** {H11334} I
284a0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
284b0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
284c0 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 e3_step(S)] for
284d0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
284e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
284f0 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 ent] S returned
28500 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 [SQLITE_ROW] or
28510 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a [SQLITE_DONE],.*
28520 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 69 66 * or if
28530 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
28540 29 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 )] has never bef
28550 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ore been called
28560 6f 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 on S,.**
28570 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f then [sqlite3_
28580 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e reset(S)] return
28590 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a s [SQLITE_OK]..*
285a0 2a 0a 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66 *.** {H11336} If
285b0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
285c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
285d0 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 3_step(S)] for t
285e0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
285f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
28600 6e 74 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20 nt] S indicated
28610 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a an error, then.*
28620 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
28630 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 te3_reset(S)] re
28640 74 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 turns an appropr
28650 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 iate [error code
28660 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38 ]..**.** {H11338
28670 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 } The [sqlite3_r
28680 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 eset(S)] interfa
28690 63 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e ce does not chan
286a0 67 65 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a ge the values.**
286b0 20 20 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 of any
286c0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
286d0 6c 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e lob|bindings] on
286e0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
286f0 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a tatement] S..*/.
28700 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
28710 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c qlite3_reset(sql
28720 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
28730 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
28740 45 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 EF: Create Or Re
28750 64 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 define SQL Funct
28760 69 6f 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53 ions {H16100} <S
28770 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 20200>.** KEYWOR
28780 44 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 DS: {function cr
28790 65 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d eation routines}
287a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 .** KEYWORDS: {a
287b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
287c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d ed SQL function}
287d0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 .** KEYWORDS: {a
287e0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
287f0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 ed SQL functions
28800 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77 }.**.** These tw
28810 6f 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c o functions (col
28820 6c 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20 lectively known
28830 61 73 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65 as "function cre
28840 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29 ation routines")
28850 0a 2a 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20 .** are used to
28860 61 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e add SQL function
28870 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 s or aggregates
28880 6f 72 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74 or to redefine t
28890 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f he behavior.** o
288a0 66 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 f existing SQL f
288b0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 unctions or aggr
288c0 65 67 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c egates. The onl
288d0 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 y difference bet
288e0 77 65 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20 ween the.** two
288f0 69 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f is that the seco
28900 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 nd parameter, th
28910 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73 e name of the (s
28920 63 61 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20 calar) function
28930 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c or.** aggregate,
28940 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 is encoded in U
28950 54 46 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33 TF-8 for sqlite3
28960 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
28970 28 29 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a () and UTF-16.**
28980 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 for sqlite3_cre
28990 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
289a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
289b0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 t parameter is t
289c0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
289d0 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63 nection] to whic
289e0 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e h the SQL.** fun
289f0 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61 ction is to be a
28a00 64 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67 dded. If a sing
28a10 6c 65 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20 le program uses
28a20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 more than one da
28a30 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
28a40 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c tion internally,
28a50 20 74 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69 then SQL functi
28a60 6f 6e 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 ons must be adde
28a70 64 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 d individually t
28a80 6f 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61 o.** each databa
28a90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
28aa0 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
28ab0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
28ac0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c name of the SQL
28ad0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
28ae0 63 72 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65 created or.** re
28af0 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65 defined. The le
28b00 6e 67 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 ngth of the name
28b10 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 is limited to 2
28b20 35 35 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 55 bytes, exclus
28b30 69 76 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65 ive of.** the ze
28b40 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 ro-terminator.
28b50 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 Note that the na
28b60 6d 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 me length limit
28b70 69 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 is in bytes, not
28b80 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 .** characters.
28b90 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
28ba0 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f create a functio
28bb0 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 n with a longer
28bc0 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 name.** will res
28bd0 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45 ult in [SQLITE_E
28be0 52 52 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75 RROR] being retu
28bf0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rned..**.** The
28c00 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
28c10 28 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65 (nArg).** is the
28c20 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
28c30 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 53 51 ents that the SQ
28c40 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a L function or.**
28c50 20 61 67 67 72 65 67 61 74 65 20 74 61 6b 65 73 aggregate takes
28c60 2e 20 49 66 20 74 68 69 73 20 70 61 72 61 6d 65 . If this parame
28c70 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 ter is -1, then
28c80 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
28c90 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 or.** aggregate
28ca0 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 may take any nu
28cb0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
28cc0 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 s between 0 and
28cd0 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 73 65 74 the limit.** set
28ce0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d by [sqlite3_lim
28cf0 69 74 5d 28 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 it]([SQLITE_LIMI
28d00 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 29 T_FUNCTION_ARG])
28d10 2e 20 20 49 66 20 74 68 65 20 74 68 69 72 64 0a . If the third.
28d20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 ** parameter is
28d30 6c 65 73 73 20 74 68 61 6e 20 2d 31 20 6f 72 20 less than -1 or
28d40 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 32 37 greater than 127
28d50 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
28d60 6f 72 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e or is.** undefin
28d70 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ed..**.** The fo
28d80 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 urth parameter,
28d90 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 eTextRep, specif
28da0 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c ies what.** [SQL
28db0 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 ITE_UTF8 | text
28dc0 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 encoding] this S
28dd0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 QL function pref
28de0 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 ers for.** its p
28df0 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 arameters. Any
28e00 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 SQL function imp
28e10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 lementation shou
28e20 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f ld be able to wo
28e30 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 rk.** work with
28e40 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c UTF-8, UTF-16le,
28e50 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 or UTF-16be. B
28e60 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e ut some implemen
28e70 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a tations may be.*
28e80 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 * more efficient
28e90 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 with one encodi
28ea0 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e ng than another.
28eb0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e An application
28ec0 20 6d 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 may.** invoke s
28ed0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
28ee0 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 nction() or sqli
28ef0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
28f00 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 ion16() multiple
28f10 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 .** times with t
28f20 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e he same function
28f30 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72 but with differ
28f40 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 ent values of eT
28f50 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 extRep..** When
28f60 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 multiple impleme
28f70 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
28f80 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 same function ar
28f90 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c e available, SQL
28fa0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b ite.** will pick
28fb0 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e the one that in
28fc0 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 volves the least
28fd0 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 amount of data
28fe0 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 conversion..** I
28ff0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 f there is only
29000 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 a single impleme
29010 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f ntation which do
29020 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 es not care what
29030 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e text.** encodin
29040 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 g is used, then
29050 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d the fourth argum
29060 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 ent should be [S
29070 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a QLITE_ANY]..**.*
29080 2a 20 54 68 65 20 66 69 66 74 68 20 70 61 72 61 * The fifth para
29090 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 meter is an arbi
290a0 74 72 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 trary pointer.
290b0 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 The implementati
290c0 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e on of the.** fun
290d0 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 ction can gain a
290e0 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f ccess to this po
290f0 69 6e 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c inter using [sql
29100 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 ite3_user_data()
29110 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 ]..**.** The sev
29120 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 enth, eighth and
29130 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 ninth parameter
29140 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 s, xFunc, xStep
29150 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a and xFinal, are.
29160 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 ** pointers to C
29170 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 -language functi
29180 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
29190 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 nt the SQL funct
291a0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 ion or.** aggreg
291b0 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 ate. A scalar SQ
291c0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 L function requi
291d0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 res an implement
291e0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 ation of the xFu
291f0 6e 63 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f nc.** callback o
29200 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 nly, NULL pointe
29210 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 rs should be pas
29220 73 65 64 20 61 73 20 74 68 65 20 78 53 74 65 70 sed as the xStep
29230 20 61 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 and xFinal.** p
29240 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 arameters. An ag
29250 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 gregate SQL func
29260 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e tion requires an
29270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
29280 6f 66 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 of xStep.** and
29290 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 xFinal and NULL
292a0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 should be passed
292b0 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 for xFunc. To d
292c0 65 6c 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e elete an existin
292d0 67 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f g.** SQL functio
292e0 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 n or aggregate,
292f0 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c pass NULL for al
29300 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e l three function
29310 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a callbacks..**.*
29320 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 * It is permitte
29330 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 d to register mu
29340 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 ltiple implement
29350 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 ations of the sa
29360 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 me.** functions
29370 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 with the same na
29380 6d 65 20 62 75 74 20 77 69 74 68 20 65 69 74 68 me but with eith
29390 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d er differing num
293a0 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d bers of.** argum
293b0 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e ents or differin
293c0 67 20 70 72 65 66 65 72 72 65 64 20 74 65 78 74 g preferred text
293d0 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c encodings. SQL
293e0 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 ite will use.**
293f0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
29400 6f 6e 20 74 68 61 74 20 6d 6f 73 74 20 63 6c 6f on that most clo
29410 73 65 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65 sely matches the
29420 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 74 68 way in which th
29430 65 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f e.** SQL functio
29440 6e 20 69 73 20 75 73 65 64 2e 20 20 41 20 66 75 n is used. A fu
29450 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
29460 61 74 69 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e ation with a non
29470 2d 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72 -negative.** nAr
29480 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 g parameter is a
29490 20 62 65 74 74 65 72 20 6d 61 74 63 68 20 74 68 better match th
294a0 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d an a function im
294b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 plementation wit
294c0 68 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 h.** a negative
294d0 6e 41 72 67 2e 20 20 41 20 66 75 6e 63 74 69 6f nArg. A functio
294e0 6e 20 77 68 65 72 65 20 74 68 65 20 70 72 65 66 n where the pref
294f0 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 erred text encod
29500 69 6e 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 ing.** matches t
29510 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f he database enco
29520 64 69 6e 67 20 69 73 20 61 20 62 65 74 74 65 72 ding is a better
29530 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61 .** match than a
29540 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
29550 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 the encoding is
29560 64 69 66 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20 different. .**
29570 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 A function where
29580 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 the encoding di
29590 66 66 65 72 65 6e 63 65 20 69 73 20 62 65 74 77 fference is betw
295a0 65 65 6e 20 55 54 46 31 36 6c 65 20 61 6e 64 20 een UTF16le and
295b0 55 54 46 31 36 62 65 0a 2a 2a 20 69 73 20 61 20 UTF16be.** is a
295c0 63 6c 6f 73 65 72 20 6d 61 74 63 68 20 74 68 61 closer match tha
295d0 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 n a function whe
295e0 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 re the encoding
295f0 64 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a difference is.**
29600 20 62 65 74 77 65 65 6e 20 55 54 46 38 20 61 6e between UTF8 an
29610 64 20 55 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42 d UTF16..**.** B
29620 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
29630 73 20 6d 61 79 20 62 65 20 6f 76 65 72 6c 6f 61 s may be overloa
29640 64 65 64 20 62 79 20 6e 65 77 20 61 70 70 6c 69 ded by new appli
29650 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
29660 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 unctions..** The
29670 20 66 69 72 73 74 20 61 70 70 6c 69 63 61 74 69 first applicati
29680 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
29690 69 6f 6e 20 77 69 74 68 20 61 20 67 69 76 65 6e ion with a given
296a0 20 6e 61 6d 65 20 6f 76 65 72 72 69 64 65 73 20 name overrides
296b0 61 6c 6c 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 all.** built-in
296c0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 functions in the
296d0 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 same [database
296e0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68 connection] with
296f0 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a the same name..
29700 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 61 70 ** Subsequent ap
29710 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
29720 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74 d functions of t
29730 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c he same name onl
29740 79 20 6f 76 65 72 72 69 64 65 20 0a 2a 2a 20 70 y override .** p
29750 72 69 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e rior application
29760 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
29770 6e 73 20 74 68 61 74 20 61 72 65 20 61 6e 20 65 ns that are an e
29780 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74 xact match for t
29790 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
297a0 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 70 parameters and p
297b0 72 65 66 65 72 72 65 64 20 65 6e 63 6f 64 69 6e referred encodin
297c0 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c g..**.** An appl
297d0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
297e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 65 72 6d function is perm
297f0 69 74 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74 itted to call ot
29800 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e her.** SQLite in
29810 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 terfaces. Howev
29820 65 72 2c 20 73 75 63 68 20 63 61 6c 6c 73 20 6d er, such calls m
29830 75 73 74 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65 ust not.** close
29840 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
29850 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e nnection nor fin
29860 61 6c 69 7a 65 20 6f 72 20 72 65 73 65 74 20 74 alize or reset t
29870 68 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 he prepared.** s
29880 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 tatement in whic
29890 68 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 h the function i
298a0 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a s running..**.**
298b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
298c0 2a 20 5b 48 31 36 31 30 33 5d 20 5b 48 31 36 31 * [H16103] [H161
298d0 30 36 5d 20 5b 48 31 36 31 30 39 5d 20 5b 48 31 06] [H16109] [H1
298e0 36 31 31 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b 6112] [H16118] [
298f0 48 31 36 31 32 31 5d 20 5b 48 31 36 31 32 37 5d H16121] [H16127]
29900 0a 2a 2a 20 5b 48 31 36 31 33 30 5d 20 5b 48 31 .** [H16130] [H1
29910 36 31 33 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b 6133] [H16136] [
29920 48 31 36 31 33 39 5d 20 5b 48 31 36 31 34 32 5d H16139] [H16142]
29930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
29940 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
29950 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 e_function(. sq
29960 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e lite3 *db,. con
29970 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 st char *zFuncti
29980 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 onName,. int nA
29990 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 rg,. int eTextR
299a0 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 ep,. void *pApp
299b0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 ,. void (*xFunc
299c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
299d0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
299e0 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 alue**),. void
299f0 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 (*xStep)(sqlite3
29a00 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
29a10 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a lite3_value**),.
29a20 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 void (*xFinal)
29a30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
29a40 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
29a50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
29a60 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a ate_function16(.
29a70 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 sqlite3 *db,.
29a80 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 const void *zFu
29a90 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e nctionName,. in
29aa0 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 t nArg,. int eT
29ab0 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a extRep,. void *
29ac0 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 pApp,. void (*x
29ad0 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f Func)(sqlite3_co
29ae0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 ntext*,int,sqlit
29af0 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 e3_value**),. v
29b00 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c oid (*xStep)(sql
29b10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
29b20 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
29b30 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 *),. void (*xFi
29b40 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e nal)(sqlite3_con
29b50 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a text*).);../*.**
29b60 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 20 CAPI3REF: Text
29b70 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 36 Encodings {H1026
29b80 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 36 7} <S50200> <H16
29b90 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 100>.**.** These
29ba0 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 constant define
29bb0 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74 integer codes t
29bc0 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 68 hat represent th
29bd0 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 e various.** tex
29be0 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 t encodings supp
29bf0 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e orted by SQLite.
29c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
29c10 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 20 TE_UTF8
29c20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 1.#define SQLI
29c30 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20 TE_UTF16LE
29c40 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
29c50 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 20 TE_UTF16BE
29c60 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 3.#define SQLI
29c70 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 20 TE_UTF16
29c80 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 4 /* Use na
29c90 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
29ca0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
29cb0 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 E_ANY
29cc0 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5 /* sqlite3
29cd0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
29ce0 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
29cf0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c SQLITE_UTF16_AL
29d00 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73 IGNED 8 /* s
29d10 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
29d20 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a llation only */.
29d30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
29d40 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e 63 Deprecated Func
29d50 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41 tions.** DEPRECA
29d60 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 TED.**.** These
29d70 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b 64 functions are [d
29d80 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e 20 eprecated]. In
29d90 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 order to maintai
29da0 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 n.** backwards c
29db0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 ompatibility wit
29dc0 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 68 h older code, th
29dd0 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f ese functions co
29de0 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65 ntinue .** to be
29df0 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f 77 supported. How
29e00 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 63 ever, new applic
29e10 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76 ations should av
29e20 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f oid.** the use o
29e30 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e f these function
29e40 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f s. To help enco
29e50 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f 20 urage people to
29e60 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74 avoid.** using t
29e70 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 hese functions,
29e80 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67 we are not going
29e90 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 61 to tell you wha
29ea0 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69 t they do..*/.#i
29eb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
29ec0 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51 4c T_DEPRECATED.SQL
29ed0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 ITE_API SQLITE_D
29ee0 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 EPRECATED int sq
29ef0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
29f00 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f count(sqlite3_co
29f10 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f ntext*);.SQLITE_
29f20 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 API SQLITE_DEPRE
29f30 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 CATED int sqlite
29f40 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 3_expired(sqlite
29f50 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 3_stmt*);.SQLITE
29f60 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 _API SQLITE_DEPR
29f70 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 ECATED int sqlit
29f80 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 e3_transfer_bind
29f90 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
29fa0 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 t*, sqlite3_stmt
29fb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 *);.SQLITE_API S
29fc0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 QLITE_DEPRECATED
29fd0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f int sqlite3_glo
29fe0 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 bal_recover(void
29ff0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 );.SQLITE_API SQ
2a000 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 LITE_DEPRECATED
2a010 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 void sqlite3_thr
2a020 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 ead_cleanup(void
2a030 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 );.SQLITE_API SQ
2a040 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 LITE_DEPRECATED
2a050 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f int sqlite3_memo
2a060 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29 ry_alarm(void(*)
2a070 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 (void*,sqlite3_i
2a080 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c nt64,int),void*,
2a090 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a sqlite3_int64);.
2a0a0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 #endif../*.** CA
2a0b0 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e PI3REF: Obtainin
2a0c0 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50 g SQL Function P
2a0d0 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73 20 arameter Values
2a0e0 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30 30 {H15100} <S20200
2a0f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 >.**.** The C-la
2a100 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 nguage implement
2a110 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e ation of SQL fun
2a120 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 ctions and aggre
2a130 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 gates uses.** th
2a140 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 is set of interf
2a150 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 ace routines to
2a160 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 6d access the param
2a170 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a eter values on.*
2a180 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f * the function o
2a190 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a r aggregate..**.
2a1a0 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f ** The xFunc (fo
2a1b0 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f r scalar functio
2a1c0 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f ns) or xStep (fo
2a1d0 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 61 r aggregates) pa
2a1e0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b rameters.** to [
2a1f0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2a200 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b unction()] and [
2a210 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2a220 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 unction16()].**
2a230 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 define callbacks
2a240 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
2a250 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
2a260 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 s and aggregates
2a270 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72 ..** The 4th par
2a280 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 20 ameter to these
2a290 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 callbacks is an
2a2a0 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 array of pointer
2a2b0 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 s to.** [protect
2a2c0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2a2d0 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 ] objects. Ther
2a2e0 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 e is one [sqlite
2a2f0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
2a300 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 for.** each para
2a310 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c meter to the SQL
2a320 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 function. Thes
2a330 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 e routines are u
2a340 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 sed to.** extrac
2a350 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 t values from th
2a360 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
2a370 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a ] objects..**.**
2a380 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
2a390 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b work only with [
2a3a0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
2a3b0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
2a3c0 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 ..** Any attempt
2a3d0 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f to use these ro
2a3e0 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e utines on an [un
2a3f0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
2a400 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 3_value].** obje
2a410 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e ct results in un
2a420 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
2a430 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
2a440 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74 utines work just
2a450 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 73 like the corres
2a460 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20 ponding [column
2a470 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 access functions
2a480 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 ].** except that
2a490 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 these routines
2a4a0 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b take a single [
2a4b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
2a4c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a 3_value] object.
2a4d0 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 ** pointer inste
2a4e0 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 33 ad of a [sqlite3
2a4f0 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20 _stmt*] pointer
2a500 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 63 and an integer c
2a510 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a olumn number..**
2a520 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2a530 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20 69 value_text16() i
2a540 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63 74 nterface extract
2a550 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e s a UTF-16 strin
2a560 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 69 g.** in the nati
2a570 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f 66 ve byte-order of
2a580 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69 6e the host machin
2a590 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 e. The.** sqlit
2a5a0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 e3_value_text16b
2a5b0 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f e() and sqlite3_
2a5c0 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29 value_text16le()
2a5d0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 65 interfaces.** e
2a5e0 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 74 xtract UTF-16 st
2a5f0 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64 rings as big-end
2a600 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 ian and little-e
2a610 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 65 ndian respective
2a620 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ly..**.** The sq
2a630 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
2a640 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65 72 ric_type() inter
2a650 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f face attempts to
2a660 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69 apply.** numeri
2a670 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74 68 c affinity to th
2a680 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 6d e value. This m
2a690 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74 74 eans that an att
2a6a0 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20 empt is.** made
2a6b0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 to convert the v
2a6c0 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 alue to an integ
2a6d0 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 er or floating p
2a6e0 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63 oint. If.** suc
2a6f0 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 h a conversion i
2a700 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f s possible witho
2a710 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 ut loss of infor
2a720 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72 mation (in other
2a730 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 68 .** words, if th
2a740 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 e value is a str
2a750 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c ing that looks l
2a760 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a ike a number).**
2a770 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 72 then the conver
2a780 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 sion is performe
2a790 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e 6f d. Otherwise no
2a7a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 conversion occu
2a7b0 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 rs..** The [SQLI
2a7c0 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 TE_INTEGER | dat
2a7d0 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e atype] after con
2a7e0 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72 version is retur
2a7f0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 ned..**.** Pleas
2a800 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 72 e pay particular
2a810 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 attention to th
2a820 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20 e fact that the
2a830 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 pointer returned
2a840 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 .** from [sqlite
2a850 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 3_value_blob()],
2a860 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2a870 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b text()], or.** [
2a880 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a890 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69 xt16()] can be i
2a8a0 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 20 nvalidated by a
2a8b0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 subsequent call
2a8c0 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 to.** [sqlite3_v
2a8d0 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b alue_bytes()], [
2a8e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
2a8f0 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74 tes16()], [sqlit
2a900 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d e3_value_text()]
2a910 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 ,.** or [sqlite3
2a920 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d _value_text16()]
2a930 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
2a940 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 utines must be c
2a950 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 alled from the s
2a960 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a 2a ame thread as.**
2a970 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
2a980 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64 20 n that supplied
2a990 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c the [sqlite3_val
2a9a0 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 2e ue*] parameters.
2a9b0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
2a9c0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d nts:.** [H15103]
2a9d0 20 5b 48 31 35 31 30 36 5d 20 5b 48 31 35 31 30 [H15106] [H1510
2a9e0 39 5d 20 5b 48 31 35 31 31 32 5d 20 5b 48 31 35 9] [H15112] [H15
2a9f0 31 31 35 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48 115] [H15118] [H
2aa00 31 35 31 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a 15121] [H15124].
2aa10 2a 2a 20 5b 48 31 35 31 32 37 5d 20 5b 48 31 35 ** [H15127] [H15
2aa20 31 33 30 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48 130] [H15133] [H
2aa30 31 35 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 15136].*/.SQLITE
2aa40 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
2aa50 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 *sqlite3_value_b
2aa60 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 lob(sqlite3_valu
2aa70 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
2aa80 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
2aa90 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f e_bytes(sqlite3_
2aaa0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
2aab0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2aac0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 value_bytes16(sq
2aad0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2aae0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
2aaf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 sqlite3_value_d
2ab00 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 ouble(sqlite3_va
2ab10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2ab20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
2ab30 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f lue_int(sqlite3_
2ab40 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
2ab50 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
2ab60 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 4 sqlite3_value_
2ab70 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 int64(sqlite3_va
2ab80 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2ab90 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
2aba0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 char *sqlite3_v
2abb0 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 alue_text(sqlite
2abc0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
2abd0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
2abe0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
2abf0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 text16(sqlite3_v
2ac00 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
2ac10 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
2ac20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2ac30 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 t16le(sqlite3_va
2ac40 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2ac50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
2ac60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2ac70 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 16be(sqlite3_val
2ac80 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
2ac90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
2aca0 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f ue_type(sqlite3_
2acb0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
2acc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2acd0 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 value_numeric_ty
2ace0 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 pe(sqlite3_value
2acf0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
2ad00 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 REF: Obtain Aggr
2ad10 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 egate Function C
2ad20 6f 6e 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20 ontext {H16210}
2ad30 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
2ad40 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
2ad50 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53 n of aggregate S
2ad60 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 QL functions use
2ad70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f this routine to
2ad80 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 allocate.** a s
2ad90 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f tructure for sto
2ada0 72 69 6e 67 20 74 68 65 69 72 20 73 74 61 74 65 ring their state
2adb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
2adc0 74 20 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 t time the sqlit
2add0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
2ade0 74 65 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 text() routine i
2adf0 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a s called for a.*
2ae00 2a 20 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 * particular agg
2ae10 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61 regate, SQLite a
2ae20 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20 llocates nBytes
2ae30 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 of memory, zeroe
2ae40 73 20 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 s out that.** me
2ae50 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e mory, and return
2ae60 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
2ae70 74 2e 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 t. On second and
2ae80 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
2ae90 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f s to.** sqlite3_
2aea0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
2aeb0 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 t() for the same
2aec0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
2aed0 69 6f 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 ion index,.** th
2aee0 65 20 73 61 6d 65 20 62 75 66 66 65 72 20 69 73 e same buffer is
2aef0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69 returned. The i
2af00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
2af10 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63 the aggregate c
2af20 61 6e 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 an use.** the re
2af30 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f turned buffer to
2af40 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 accumulate data
2af50 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 ..**.** SQLite a
2af60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 utomatically fre
2af70 65 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 es the allocated
2af80 20 62 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 buffer when the
2af90 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 aggregate.** qu
2afa0 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a ery concludes..*
2afb0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 *.** The first p
2afc0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
2afd0 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 be a copy of the
2afe0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e .** [sqlite3_con
2aff0 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 text | SQL funct
2b000 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 ion context] tha
2b010 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 t is the first p
2b020 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 arameter.** to t
2b030 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 he callback rout
2b040 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ine that impleme
2b050 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74 nts the aggregat
2b060 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a e function..**.*
2b070 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
2b080 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 ust be called fr
2b090 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 om the same thre
2b0a0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 ad in which.** t
2b0b0 68 65 20 61 67 67 72 65 67 61 74 65 20 53 51 4c he aggregate SQL
2b0c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e function is run
2b0d0 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ning..**.** Requ
2b0e0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
2b0f0 36 32 31 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b 6211] [H16213] [
2b100 48 31 36 32 31 35 5d 20 5b 48 31 36 32 31 37 5d H16215] [H16217]
2b110 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
2b120 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 oid *sqlite3_agg
2b130 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 regate_context(s
2b140 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2b150 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f int nBytes);../
2b160 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 *.** CAPI3REF: U
2b170 73 65 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e ser Data For Fun
2b180 63 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 ctions {H16240}
2b190 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
2b1a0 68 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f he sqlite3_user_
2b1b0 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 data() interface
2b1c0 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 returns a copy
2b1d0 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 of.** the pointe
2b1e0 72 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70 r that was the p
2b1f0 55 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 UserData paramet
2b200 65 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61 er (the 5th para
2b210 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 meter).** of the
2b220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
2b230 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 _function()].**
2b240 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 and [sqlite3_cre
2b250 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
2b260 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 ] routines that
2b270 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 originally.** re
2b280 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 gistered the app
2b290 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 lication defined
2b2a0 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d function. {END}
2b2b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
2b2c0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c ine must be call
2b2d0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 ed from the same
2b2e0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 thread in which
2b2f0 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 .** the applicat
2b300 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2b310 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e tion is running.
2b320 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
2b330 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d nts:.** [H16243]
2b340 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
2b350 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 oid *sqlite3_use
2b360 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 r_data(sqlite3_c
2b370 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a ontext*);../*.**
2b380 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 CAPI3REF: Datab
2b390 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46 ase Connection F
2b3a0 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 or Functions {H1
2b3b0 36 32 35 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53 6250} <S60600><S
2b3c0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
2b3d0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
2b3e0 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 _db_handle() int
2b3f0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
2b400 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 copy of.** the
2b410 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
2b420 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2b430 69 6f 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61 ion] (the 1st pa
2b440 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 rameter).** of t
2b450 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 he [sqlite3_crea
2b460 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a te_function()].*
2b470 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 * and [sqlite3_c
2b480 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
2b490 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ()] routines tha
2b4a0 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 t originally.**
2b4b0 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20 61 registered the a
2b4c0 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e pplication defin
2b4d0 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a ed function..**.
2b4e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2b4f0 0a 2a 2a 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a .** [H16253].*/.
2b500 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
2b510 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 e3 *sqlite3_cont
2b520 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 ext_db_handle(sq
2b530 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
2b540 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2b550 3a 20 46 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c : Function Auxil
2b560 69 61 72 79 20 44 61 74 61 20 7b 48 31 36 32 37 iary Data {H1627
2b570 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 0} <S20200>.**.*
2b580 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
2b590 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 two functions ma
2b5a0 79 20 62 65 20 75 73 65 64 20 62 79 20 73 63 61 y be used by sca
2b5b0 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e lar SQL function
2b5c0 73 20 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 s to.** associat
2b5d0 65 20 6d 65 74 61 64 61 74 61 20 77 69 74 68 20 e metadata with
2b5e0 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e argument values.
2b5f0 20 49 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c If the same val
2b600 75 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a ue is passed to.
2b610 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f ** multiple invo
2b620 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 cations of the s
2b630 61 6d 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ame SQL function
2b640 20 64 75 72 69 6e 67 20 71 75 65 72 79 20 65 78 during query ex
2b650 65 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a ecution, under.*
2b660 2a 20 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 * some circumsta
2b670 6e 63 65 73 20 74 68 65 20 61 73 73 6f 63 69 61 nces the associa
2b680 74 65 64 20 6d 65 74 61 64 61 74 61 20 6d 61 79 ted metadata may
2b690 20 62 65 20 70 72 65 73 65 72 76 65 64 2e 20 54 be preserved. T
2b6a0 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73 his may.** be us
2b6b0 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c ed, for example,
2b6c0 20 74 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61 to add a regula
2b6d0 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74 r-expression mat
2b6e0 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20 ching scalar.**
2b6f0 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f function. The co
2b700 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f mpiled version o
2b710 66 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 f the regular ex
2b720 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72 pression is stor
2b730 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74 ed as.** metadat
2b740 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
2b750 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75 65 20 h the SQL value
2b760 70 61 73 73 65 64 20 61 73 20 74 68 65 20 72 65 passed as the re
2b770 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e gular expression
2b780 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68 .** pattern. Th
2b790 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c e compiled regul
2b7a0 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 ar expression ca
2b7b0 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d n be reused on m
2b7c0 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 ultiple.** invoc
2b7d0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 ations of the sa
2b7e0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 me function so t
2b7f0 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hat the original
2b800 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0a pattern string.
2b810 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 ** does not need
2b820 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 to be recompile
2b830 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 d on each invoca
2b840 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
2b850 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
2b860 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 ata() interface
2b870 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
2b880 72 20 74 6f 20 74 68 65 20 6d 65 74 61 64 61 74 r to the metadat
2b890 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 a.** associated
2b8a0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 by the sqlite3_s
2b8b0 65 74 5f 61 75 78 64 61 74 61 28 29 20 66 75 6e et_auxdata() fun
2b8c0 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 4e ction with the N
2b8d0 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76 th argument.** v
2b8e0 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70 70 6c alue to the appl
2b8f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2b900 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 function. If no
2b910 6d 65 74 61 64 61 74 61 20 68 61 73 20 62 65 65 metadata has bee
2b920 6e 20 65 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73 n ever.** been s
2b930 65 74 20 66 6f 72 20 74 68 65 20 4e 74 68 20 61 et for the Nth a
2b940 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 rgument of the f
2b950 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74 unction, or if t
2b960 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
2b970 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 .** function par
2b980 61 6d 65 74 65 72 20 68 61 73 20 63 68 61 6e 67 ameter has chang
2b990 65 64 20 73 69 6e 63 65 20 74 68 65 20 6d 65 74 ed since the met
2b9a0 61 2d 64 61 74 61 20 77 61 73 20 73 65 74 2c 0a a-data was set,.
2b9b0 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ** then sqlite3_
2b9c0 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 72 65 get_auxdata() re
2b9d0 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 turns a NULL poi
2b9e0 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nter..**.** The
2b9f0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
2ba00 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 ata() interface
2ba10 73 61 76 65 73 20 74 68 65 20 6d 65 74 61 64 61 saves the metada
2ba20 74 61 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f ta.** pointed to
2ba30 20 62 79 20 69 74 73 20 33 72 64 20 70 61 72 61 by its 3rd para
2ba40 6d 65 74 65 72 20 61 73 20 74 68 65 20 6d 65 74 meter as the met
2ba50 61 64 61 74 61 20 66 6f 72 20 74 68 65 20 4e 2d adata for the N-
2ba60 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f th.** argument o
2ba70 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
2ba80 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
2ba90 6f 6e 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0a on. Subsequent.
2baa0 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ** calls to sqli
2bab0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
2bac0 29 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74 ) might return t
2bad0 68 69 73 20 64 61 74 61 2c 20 69 66 20 69 74 20 his data, if it
2bae0 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 has.** not been
2baf0 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66 destroyed..** If
2bb00 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c it is not NULL,
2bb10 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 SQLite will inv
2bb20 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75 63 74 oke the destruct
2bb30 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67 or.** function g
2bb40 69 76 65 6e 20 62 79 20 74 68 65 20 34 74 68 20 iven by the 4th
2bb50 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
2bb60 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
2bb70 28 29 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74 () on.** the met
2bb80 61 64 61 74 61 20 77 68 65 6e 20 74 68 65 20 63 adata when the c
2bb90 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e orresponding fun
2bba0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
2bbb0 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68 changes.** or wh
2bbc0 65 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 en the SQL state
2bbd0 6d 65 6e 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20 ment completes,
2bbe0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 whichever comes
2bbf0 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c first..**.** SQL
2bc00 69 74 65 20 69 73 20 66 72 65 65 20 74 6f 20 63 ite is free to c
2bc10 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 all the destruct
2bc20 6f 72 20 61 6e 64 20 64 72 6f 70 20 6d 65 74 61 or and drop meta
2bc30 64 61 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70 data on any.** p
2bc40 61 72 61 6d 65 74 65 72 20 6f 66 20 61 6e 79 20 arameter of any
2bc50 66 75 6e 63 74 69 6f 6e 20 61 74 20 61 6e 79 20 function at any
2bc60 74 69 6d 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20 time. The only
2bc70 67 75 61 72 61 6e 74 65 65 20 69 73 20 74 68 61 guarantee is tha
2bc80 74 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63 t.** the destruc
2bc90 74 6f 72 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c tor will be call
2bca0 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 6d 65 ed before the me
2bcb0 74 61 64 61 74 61 20 69 73 20 64 72 6f 70 70 65 tadata is droppe
2bcc0 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 d..**.** In prac
2bcd0 74 69 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69 tice, metadata i
2bce0 73 20 70 72 65 73 65 72 76 65 64 20 62 65 74 77 s preserved betw
2bcf0 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c een function cal
2bd00 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 ls for.** expres
2bd10 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 sions that are c
2bd20 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 onstant at compi
2bd30 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e le time. This in
2bd40 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a cludes literal.*
2bd50 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c * values and SQL
2bd60 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a variables..**.*
2bd70 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
2bd80 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
2bd90 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 from the same th
2bda0 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a read in which.**
2bdb0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
2bdc0 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a n is running..**
2bdd0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2bde0 3a 0a 2a 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48 :.** [H16272] [H
2bdf0 31 36 32 37 34 5d 20 5b 48 31 36 32 37 36 5d 20 16274] [H16276]
2be00 5b 48 31 36 32 37 37 5d 20 5b 48 31 36 32 37 38 [H16277] [H16278
2be10 5d 20 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51 ] [H16279].*/.SQ
2be20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
2be30 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 qlite3_get_auxda
2be40 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ta(sqlite3_conte
2be50 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c xt*, int N);.SQL
2be60 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2be70 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
2be80 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2be90 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c *, int N, void*,
2bea0 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 void (*)(void*)
2beb0 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 );.../*.** CAPI3
2bec0 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 REF: Constants D
2bed0 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 efining Special
2bee0 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61 76 Destructor Behav
2bef0 69 6f 72 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 ior {H10280} <S3
2bf00 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 0100>.**.** Thes
2bf10 65 20 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 e are special va
2bf20 6c 75 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 lues for the des
2bf30 74 72 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 tructor that is
2bf40 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 passed in as the
2bf50 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 .** final argume
2bf60 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c nt to routines l
2bf70 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 ike [sqlite3_res
2bf80 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 ult_blob()]. If
2bf90 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a the destructor.
2bfa0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 ** argument is S
2bfb0 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74 QLITE_STATIC, it
2bfc0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2bfd0 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 content pointer
2bfe0 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 is constant.** a
2bff0 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 nd will never ch
2c000 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e ange. It does n
2c010 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 ot need to be de
2c020 73 74 72 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a stroyed. The.**
2c030 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
2c040 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 T value means th
2c050 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 at the content w
2c060 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 ill likely chang
2c070 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 e in.** the near
2c080 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 61 74 future and that
2c090 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d SQLite should m
2c0a0 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 ake its own priv
2c0b0 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 ate copy of.** t
2c0c0 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 he content befor
2c0d0 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a e returning..**.
2c0e0 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 20 69 ** The typedef i
2c0f0 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 s necessary to w
2c100 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c ork around probl
2c110 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a ems in certain.*
2c120 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e * C++ compilers.
2c130 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 31 See ticket #21
2c140 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 91..*/.typedef v
2c150 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 oid (*sqlite3_de
2c160 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 28 76 structor_type)(v
2c170 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 oid*);.#define S
2c180 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 20 20 QLITE_STATIC
2c190 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 ((sqlite3_dest
2c1a0 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 ructor_type)0).#
2c1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 define SQLITE_TR
2c1c0 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 6c 69 ANSIENT ((sqli
2c1d0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 te3_destructor_t
2c1e0 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 ype)-1)../*.** C
2c1f0 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67 API3REF: Setting
2c200 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41 The Result Of A
2c210 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b n SQL Function {
2c220 48 31 36 34 30 30 7d 20 3c 53 32 30 32 30 30 3e H16400} <S20200>
2c230 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
2c240 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 tines are used b
2c250 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 y the xFunc or x
2c260 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 Final callbacks
2c270 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e that.** implemen
2c280 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 t SQL functions
2c290 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 and aggregates.
2c2a0 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 See.** [sqlite3
2c2b0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
2c2c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
2c2d0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
2c2e0 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 16()].** for add
2c2f0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
2c300 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ion..**.** These
2c310 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 functions work
2c320 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 very much like t
2c330 68 65 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69 he [parameter bi
2c340 6e 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 nding] family of
2c350 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 .** functions us
2c360 65 64 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 ed to bind value
2c370 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 s to host parame
2c380 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 ters in prepared
2c390 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 statements..**
2c3a0 52 65 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51 Refer to the [SQ
2c3b0 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 L parameter] doc
2c3c0 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 umentation for a
2c3d0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
2c3e0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
2c3f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c400 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 blob() interface
2c410 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 sets the result
2c420 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c from.** an appl
2c430 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2c440 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 function to be t
2c450 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f he BLOB whose co
2c460 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 ntent is pointed
2c470 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65 .** to by the se
2c480 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 cond parameter a
2c490 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 62 79 nd which is N by
2c4a0 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e tes long where N
2c4b0 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 is the.** third
2c4c0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
2c4d0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2c4e0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 sult_zeroblob()
2c4f0 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20 74 interfaces set t
2c500 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 he result of.**
2c510 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2c520 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2c530 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 6f to be a BLOB co
2c540 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 ntaining all zer
2c550 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 4e o.** bytes and N
2c560 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
2c570 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 76 where N is the v
2c580 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e 64 20 alue of the 2nd
2c590 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
2c5a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2c5b0 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 ult_double() int
2c5c0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 erface sets the
2c5d0 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 result from.** a
2c5e0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
2c5f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2c600 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 o be a floating
2c610 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70 65 63 point value spec
2c620 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 ified.** by its
2c630 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 2nd argument..**
2c640 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2c650 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 result_error() a
2c660 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c nd sqlite3_resul
2c670 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75 6e 63 t_error16() func
2c680 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 tions.** cause t
2c690 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 he implemented S
2c6a0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 QL function to t
2c6b0 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f hrow an exceptio
2c6c0 6e 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 n..** SQLite use
2c6d0 73 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69 s the string poi
2c6e0 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 0a 2a nted to by the.*
2c6f0 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 * 2nd parameter
2c700 6f 66 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c of sqlite3_resul
2c710 74 5f 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c t_error() or sql
2c720 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2c730 72 31 36 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 r16().** as the
2c740 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 text of an error
2c750 20 6d 65 73 73 61 67 65 2e 20 20 53 51 4c 69 74 message. SQLit
2c760 65 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 e interprets the
2c770 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 error.** messag
2c780 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 e string from sq
2c790 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2c7a0 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e 20 53 or() as UTF-8. S
2c7b0 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 QLite.** interpr
2c7c0 65 74 73 20 74 68 65 20 73 74 72 69 6e 67 20 66 ets the string f
2c7d0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 rom sqlite3_resu
2c7e0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 61 73 20 lt_error16() as
2c7f0 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 UTF-16 in native
2c800 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 .** byte order.
2c810 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 If the third pa
2c820 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
2c830 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2c840 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f ).** or sqlite3_
2c850 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 result_error16()
2c860 20 69 73 20 6e 65 67 61 74 69 76 65 20 74 68 65 is negative the
2c870 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 61 n SQLite takes a
2c880 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d s the error.** m
2c890 65 73 73 61 67 65 20 61 6c 6c 20 74 65 78 74 20 essage all text
2c8a0 75 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 up through the f
2c8b0 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63 irst zero charac
2c8c0 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 ter..** If the t
2c8d0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
2c8e0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 o sqlite3_result
2c8f0 5f 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 _error() or.** s
2c900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2c910 72 6f 72 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e ror16() is non-n
2c920 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c egative then SQL
2c930 69 74 65 20 74 61 6b 65 73 20 74 68 61 74 20 6d ite takes that m
2c940 61 6e 79 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f any.** bytes (no
2c950 74 20 63 68 61 72 61 63 74 65 72 73 29 20 66 72 t characters) fr
2c960 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d om the 2nd param
2c970 65 74 65 72 20 61 73 20 74 68 65 20 65 72 72 6f eter as the erro
2c980 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 r message..** Th
2c990 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2c9a0 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c _error() and sql
2c9b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2c9c0 72 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 r16().** routine
2c9d0 73 20 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65 s make a private
2c9e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72 copy of the err
2c9f0 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 or message text
2ca00 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 before.** they r
2ca10 65 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 eturn. Hence, t
2ca20 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
2ca30 69 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 ion can dealloca
2ca40 74 65 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 te or.** modify
2ca50 74 68 65 20 74 65 78 74 20 61 66 74 65 72 20 74 the text after t
2ca60 68 65 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f hey return witho
2ca70 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 ut harm..** The
2ca80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2ca90 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 rror_code() func
2caa0 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65 tion changes the
2cab0 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 error code.** r
2cac0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74 eturned by SQLit
2cad0 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 e as a result of
2cae0 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66 an error in a f
2caf0 75 6e 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 unction. By def
2cb00 61 75 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 ault,.** the err
2cb10 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54 or code is SQLIT
2cb20 45 5f 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73 E_ERROR. A subs
2cb30 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 equent call to s
2cb40 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2cb50 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 ror().** or sqli
2cb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2cb70 31 36 28 29 20 72 65 73 65 74 73 20 74 68 65 20 16() resets the
2cb80 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 error code to SQ
2cb90 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a LITE_ERROR..**.*
2cba0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2cbb0 73 75 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e sult_toobig() in
2cbc0 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53 terface causes S
2cbd0 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 QLite to throw a
2cbe0 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 n error.** indic
2cbf0 61 74 69 6e 67 20 74 68 61 74 20 61 20 73 74 72 ating that a str
2cc00 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 ing or BLOB is t
2cc10 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 o long to repres
2cc20 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ent..**.** The s
2cc30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f qlite3_result_no
2cc40 6d 65 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20 mem() interface
2cc50 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f causes SQLite to
2cc60 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a throw an error.
2cc70 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 ** indicating th
2cc80 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f at a memory allo
2cc90 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a cation failed..*
2cca0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2ccb0 5f 72 65 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e _result_int() in
2ccc0 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 terface sets the
2ccd0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
2cce0 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 of the applicat
2ccf0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2cd00 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 33 tion to be the 3
2cd10 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 2-bit signed int
2cd20 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 eger.** value gi
2cd30 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 ven in the 2nd a
2cd40 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 rgument..** The
2cd50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
2cd60 6e 74 36 34 28 29 20 69 6e 74 65 72 66 61 63 65 nt64() interface
2cd70 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e sets the return
2cd80 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 value.** of the
2cd90 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
2cda0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
2cdb0 20 62 65 20 74 68 65 20 36 34 2d 62 69 74 20 73 be the 64-bit s
2cdc0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
2cdd0 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 value given in
2cde0 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 the 2nd argument
2cdf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2ce00 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 te3_result_null(
2ce10 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 ) interface sets
2ce20 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
2ce30 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c e.** of the appl
2ce40 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2ce50 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e function to be N
2ce60 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ULL..**.** The s
2ce70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2ce80 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 xt(), sqlite3_re
2ce90 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a sult_text16(),.*
2cea0 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 * sqlite3_result
2ceb0 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 _text16le(), and
2cec0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2ced0 74 65 78 74 31 36 62 65 28 29 20 69 6e 74 65 72 text16be() inter
2cee0 66 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 faces.** set the
2cef0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
2cf00 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2cf10 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2cf20 6e 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 n to be.** a tex
2cf30 74 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 t string which i
2cf40 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 s represented as
2cf50 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e UTF-8, UTF-16 n
2cf60 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2cf70 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 ,.** UTF-16 litt
2cf80 6c 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 le endian, or UT
2cf90 46 2d 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c F-16 big endian,
2cfa0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a respectively..*
2cfb0 2a 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74 * SQLite takes t
2cfc0 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 20 66 he text result f
2cfd0 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74 rom the applicat
2cfe0 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ion from.** the
2cff0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 2nd parameter of
2d000 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2d010 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2d020 61 63 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 aces..** If the
2d030 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 3rd parameter to
2d040 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2d050 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2d060 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 aces.** is negat
2d070 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 ive, then SQLite
2d080 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 74 65 takes result te
2d090 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 xt from the 2nd
2d0a0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 parameter.** thr
2d0b0 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a ough the first z
2d0c0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a ero character..*
2d0d0 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 * If the 3rd par
2d0e0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2d0f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2d100 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a t* interfaces.**
2d110 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 is non-negative
2d120 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 , then as many b
2d130 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 ytes (not charac
2d140 74 65 72 73 29 20 6f 66 20 74 68 65 20 74 65 78 ters) of the tex
2d150 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 t.** pointed to
2d160 62 79 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d by the 2nd param
2d170 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61 eter are taken a
2d180 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f s the applicatio
2d190 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e n-defined.** fun
2d1a0 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a ction result..**
2d1b0 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 If the 4th para
2d1c0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
2d1d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2d1e0 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 * interfaces.**
2d1f0 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c or sqlite3_resul
2d200 74 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d t_blob is a non-
2d210 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 NULL pointer, th
2d220 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 en SQLite calls
2d230 74 68 61 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e that.** function
2d240 20 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74 as the destruct
2d250 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f or on the text o
2d260 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 r BLOB result wh
2d270 65 6e 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e en it has.** fin
2d280 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 ished using that
2d290 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 result..** If t
2d2a0 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
2d2b0 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f to the sqlite3_
2d2c0 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 result_text* int
2d2d0 65 72 66 61 63 65 73 20 6f 72 20 74 6f 0a 2a 2a erfaces or to.**
2d2e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d2f0 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 63 blob is the spec
2d300 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c ial constant SQL
2d310 49 54 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e ITE_STATIC, then
2d320 20 53 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d SQLite.** assum
2d330 65 73 20 74 68 61 74 20 74 68 65 20 74 65 78 74 es that the text
2d340 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 or BLOB result
2d350 69 73 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 is in constant s
2d360 70 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f pace and does no
2d370 74 0a 2a 2a 20 63 6f 70 79 20 74 68 65 20 63 6f t.** copy the co
2d380 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 ntent of the par
2d390 61 6d 65 74 65 72 20 6e 6f 72 20 63 61 6c 6c 20 ameter nor call
2d3a0 61 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 a destructor on
2d3b0 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 77 the content.** w
2d3c0 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 hen it has finis
2d3d0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 hed using that r
2d3e0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 esult..** If the
2d3f0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
2d400 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
2d410 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
2d420 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 faces.** or sqli
2d430 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 te3_result_blob
2d440 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 is the special c
2d450 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54 onstant SQLITE_T
2d460 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e RANSIENT.** then
2d470 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 SQLite makes a
2d480 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 copy of the resu
2d490 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 lt into space ob
2d4a0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 tained from.** f
2d4b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
2d4c0 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74 loc()] before it
2d4d0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 returns..**.**
2d4e0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2d4f0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 lt_value() inter
2d500 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
2d510 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 sult of.** the a
2d520 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2d530 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
2d540 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 e a copy the.**
2d550 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c [unprotected sql
2d560 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
2d570 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ct specified by
2d580 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2d590 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 r. The.** sqlit
2d5a0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
2d5b0 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 ) interface make
2d5c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
2d5d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a [sqlite3_value].
2d5e0 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b ** so that the [
2d5f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 sqlite3_value] s
2d600 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
2d610 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 parameter may ch
2d620 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 ange or.** be de
2d630 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 allocated after
2d640 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
2d650 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 alue() returns w
2d660 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 ithout harm..**
2d670 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c A [protected sql
2d680 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
2d690 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 ct may always be
2d6a0 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a used where an.*
2d6b0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 * [unprotected s
2d6c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
2d6d0 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64 ject is required
2d6e0 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b , so either.** k
2d6f0 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ind of [sqlite3_
2d700 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 value] object ca
2d710 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 n be used with t
2d720 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a his interface..*
2d730 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f *.** If these ro
2d740 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 utines are calle
2d750 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 d from within th
2d760 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 e different thre
2d770 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f ad.** than the o
2d780 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ne containing th
2d790 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2d7a0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2d7b0 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 hat received.**
2d7c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e the [sqlite3_con
2d7d0 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 text] pointer, t
2d7e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
2d7f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
2d800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
2d810 20 5b 48 31 36 34 30 33 5d 20 5b 48 31 36 34 30 [H16403] [H1640
2d820 36 5d 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 36 6] [H16409] [H16
2d830 34 31 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 48 412] [H16415] [H
2d840 31 36 34 31 38 5d 20 5b 48 31 36 34 32 31 5d 20 16418] [H16421]
2d850 5b 48 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 36 [H16424].** [H16
2d860 34 32 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 48 427] [H16430] [H
2d870 31 36 34 33 33 5d 20 5b 48 31 36 34 33 36 5d 20 16433] [H16436]
2d880 5b 48 31 36 34 33 39 5d 20 5b 48 31 36 34 34 32 [H16439] [H16442
2d890 5d 20 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 34 ] [H16445] [H164
2d8a0 34 38 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 20 48].** [H16451]
2d8b0 5b 48 31 36 34 35 34 5d 20 5b 48 31 36 34 35 37 [H16454] [H16457
2d8c0 5d 20 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 34 ] [H16460] [H164
2d8d0 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 63].*/.SQLITE_AP
2d8e0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d8f0 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74 esult_blob(sqlit
2d900 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
2d910 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 st void*, int, v
2d920 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2d930 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2d940 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
2d950 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f ouble(sqlite3_co
2d960 6e 74 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b ntext*, double);
2d970 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2d980 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d990 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f error(sqlite3_co
2d9a0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 ntext*, const ch
2d9b0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ar*, int);.SQLIT
2d9c0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2d9d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2d9e0 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 6(sqlite3_contex
2d9f0 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c t*, const void*,
2da00 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
2da10 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2da20 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 esult_error_toob
2da30 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ig(sqlite3_conte
2da40 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 xt*);.SQLITE_API
2da50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2da60 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d sult_error_nomem
2da70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2da80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
2da90 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2daa0 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 lt_error_code(sq
2dab0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2dac0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
2dad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2dae0 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 sult_int(sqlite3
2daf0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b _context*, int);
2db00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2db10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2db20 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f int64(sqlite3_co
2db30 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f ntext*, sqlite3_
2db40 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 int64);.SQLITE_A
2db50 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2db60 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 result_null(sqli
2db70 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 te3_context*);.S
2db80 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2db90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2dba0 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 xt(sqlite3_conte
2dbb0 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a xt*, const char*
2dbc0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
2dbd0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
2dbe0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2dbf0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71 result_text16(sq
2dc00 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2dc10 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
2dc20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
2dc30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2dc40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2dc50 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 t_text16le(sqlit
2dc60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
2dc70 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f st void*, int,vo
2dc80 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
2dc90 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2dca0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2dcb0 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 63 xt16be(sqlite3_c
2dcc0 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 ontext*, const v
2dcd0 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a oid*, int,void(*
2dce0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
2dcf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2dd00 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
2dd10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2dd20 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a , sqlite3_value*
2dd30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2dd40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2dd50 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 t_zeroblob(sqlit
2dd60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 e3_context*, int
2dd70 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 n);../*.** CAPI
2dd80 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 77 3REF: Define New
2dd90 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 Collating Seque
2dda0 6e 63 65 73 20 7b 48 31 36 36 30 30 7d 20 3c 53 nces {H16600} <S
2ddb0 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20300>.**.** The
2ddc0 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 se functions are
2ddd0 20 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77 used to add new
2dde0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2ddf0 6e 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b nces to the.** [
2de00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2de10 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61 ion] specified a
2de20 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
2de30 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
2de40 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 name of the new
2de50 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
2de60 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20 ce is specified
2de70 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e as a UTF-8 strin
2de80 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33 g.** for sqlite3
2de90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2dea0 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f n() and sqlite3_
2deb0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2dec0 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 55 _v2().** and a U
2ded0 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f 72 TF-16 string for
2dee0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2def0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 49 collation16(). I
2df00 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74 n all cases.** t
2df10 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 he name is passe
2df20 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
2df30 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
2df40 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 t..**.** The thi
2df50 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 rd argument may
2df60 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f be one of the co
2df70 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f nstants [SQLITE_
2df80 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 UTF8],.** [SQLIT
2df90 45 5f 55 54 46 31 36 4c 45 5d 2c 20 6f 72 20 5b E_UTF16LE], or [
2dfa0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c SQLITE_UTF16BE],
2dfb0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 indicating that
2dfc0 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 the user-suppli
2dfd0 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78 ed.** routine ex
2dfe0 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73 pects to be pass
2dff0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 ed pointers to s
2e000 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75 trings encoded u
2e010 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55 sing UTF-8,.** U
2e020 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 TF-16 little-end
2e030 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 ian, or UTF-16 b
2e040 69 67 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 ig-endian, respe
2e050 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 ctively. The.**
2e060 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d third argument m
2e070 69 67 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51 ight also be [SQ
2e080 4c 49 54 45 5f 55 54 46 31 36 5d 20 74 6f 20 69 LITE_UTF16] to i
2e090 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 ndicate that the
2e0a0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 78 70 65 routine.** expe
2e0b0 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 cts pointers to
2e0c0 62 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 be UTF-16 string
2e0d0 73 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 s in the native
2e0e0 62 79 74 65 20 6f 72 64 65 72 2c 20 6f 72 20 74 byte order, or t
2e0f0 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 63 he.** argument c
2e100 61 6e 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 an be [SQLITE_UT
2e110 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 69 66 20 F16_ALIGNED] if
2e120 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 the.** the routi
2e130 6e 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 ne expects point
2e140 65 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f ers to 16-bit wo
2e150 72 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e rd aligned strin
2e160 67 73 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 20 gs.** of UTF-16
2e170 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
2e180 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 te order..**.**
2e190 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
2e1a0 20 75 73 65 72 20 73 75 70 70 6c 69 65 64 20 72 user supplied r
2e1b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 70 outine must be p
2e1c0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 66 assed as the fif
2e1d0 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 th.** argument.
2e1e0 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 If it is NULL,
2e1f0 74 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 this is the same
2e200 20 61 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65 as deleting the
2e210 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 collation.** se
2e220 71 75 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20 quence (so that
2e230 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61 SQLite cannot ca
2e240 6c 6c 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 0a ll it anymore)..
2e250 2a 2a 20 45 61 63 68 20 74 69 6d 65 20 74 68 65 ** Each time the
2e260 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 75 70 application sup
2e270 70 6c 69 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 plied function i
2e280 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20 69 73 s invoked, it is
2e290 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 69 74 passed.** as it
2e2a0 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 s first paramete
2e2b0 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 r a copy of the
2e2c0 76 6f 69 64 2a 20 70 61 73 73 65 64 20 61 73 20 void* passed as
2e2d0 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d the fourth argum
2e2e0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ent.** to sqlite
2e2f0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
2e300 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f on() or sqlite3_
2e310 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2e320 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 16()..**.** The
2e330 72 65 6d 61 69 6e 69 6e 67 20 61 72 67 75 6d 65 remaining argume
2e340 6e 74 73 20 74 6f 20 74 68 65 20 61 70 70 6c 69 nts to the appli
2e350 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 20 cation-supplied
2e360 72 6f 75 74 69 6e 65 20 61 72 65 20 74 77 6f 20 routine are two
2e370 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 65 61 63 68 strings,.** each
2e380 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 represented by
2e390 61 20 28 6c 65 6e 67 74 68 2c 20 64 61 74 61 29 a (length, data)
2e3a0 20 70 61 69 72 20 61 6e 64 20 65 6e 63 6f 64 65 pair and encode
2e3b0 64 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e d in the encodin
2e3c0 67 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 70 61 g.** that was pa
2e3d0 73 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 ssed as the thir
2e3e0 64 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e 20 d argument when
2e3f0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
2e400 71 75 65 6e 63 65 20 77 61 73 0a 2a 2a 20 72 65 quence was.** re
2e410 67 69 73 74 65 72 65 64 2e 20 7b 45 4e 44 7d 20 gistered. {END}
2e420 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
2e430 20 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 defined collati
2e440 6f 6e 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c on routine shoul
2e450 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 65 67 61 d.** return nega
2e460 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f tive, zero or po
2e470 73 69 74 69 76 65 20 69 66 20 74 68 65 20 66 69 sitive if the fi
2e480 72 73 74 20 73 74 72 69 6e 67 20 69 73 20 6c 65 rst string is le
2e490 73 73 20 74 68 61 6e 2c 0a 2a 2a 20 65 71 75 61 ss than,.** equa
2e4a0 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 l to, or greater
2e4b0 20 74 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64 than the second
2e4c0 20 73 74 72 69 6e 67 2e 20 69 2e 65 2e 20 28 53 string. i.e. (S
2e4d0 54 52 49 4e 47 31 20 2d 20 53 54 52 49 4e 47 32 TRING1 - STRING2
2e4e0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c )..**.** The sql
2e4f0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2e500 61 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 ation_v2() works
2e510 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 like sqlite3_cr
2e520 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 eate_collation()
2e530 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 .** except that
2e540 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 it takes an extr
2e550 61 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 a argument which
2e560 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 is a destructor
2e570 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c for.** the coll
2e580 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 73 74 ation. The dest
2e590 72 75 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64 ructor is called
2e5a0 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 when the collat
2e5b0 69 6f 6e 20 69 73 0a 2a 2a 20 64 65 73 74 72 6f ion is.** destro
2e5c0 79 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 yed and is passe
2e5d0 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 d a copy of the
2e5e0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
2e5f0 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 0a 2a void* pointer.*
2e600 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 * of the sqlite3
2e610 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2e620 6e 5f 76 32 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 n_v2()..** Colla
2e630 74 69 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f tions are destro
2e640 79 65 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 yed when they ar
2e650 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 e overridden by
2e660 6c 61 74 65 72 20 63 61 6c 6c 73 20 74 6f 20 74 later calls to t
2e670 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 he.** collation
2e680 63 72 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f creation functio
2e690 6e 73 20 6f 72 20 77 68 65 6e 20 74 68 65 20 5b ns or when the [
2e6a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2e6b0 69 6f 6e 5d 20 69 73 20 63 6c 6f 73 65 64 0a 2a ion] is closed.*
2e6c0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
2e6d0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a _close()]..**.**
2e6e0 20 53 65 65 20 61 6c 73 6f 3a 20 20 5b 73 71 6c See also: [sql
2e6f0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2e700 65 65 64 65 64 28 29 5d 20 61 6e 64 20 5b 73 71 eeded()] and [sq
2e710 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f lite3_collation_
2e720 6e 65 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a needed16()]..**.
2e730 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2e740 0a 2a 2a 20 5b 48 31 36 36 30 33 5d 20 5b 48 31 .** [H16603] [H1
2e750 36 36 30 34 5d 20 5b 48 31 36 36 30 36 5d 20 5b 6604] [H16606] [
2e760 48 31 36 36 30 39 5d 20 5b 48 31 36 36 31 32 5d H16609] [H16612]
2e770 20 5b 48 31 36 36 31 35 5d 20 5b 48 31 36 36 31 [H16615] [H1661
2e780 38 5d 20 5b 48 31 36 36 32 31 5d 0a 2a 2a 20 5b 8] [H16621].** [
2e790 48 31 36 36 32 34 5d 20 5b 48 31 36 36 32 37 5d H16624] [H16627]
2e7a0 20 5b 48 31 36 36 33 30 5d 0a 2a 2f 0a 53 51 4c [H16630].*/.SQL
2e7b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2e7c0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
2e7d0 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a tion(. sqlite3*
2e7e0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
2e7f0 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 *zName, . int e
2e800 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 TextRep, . void
2e810 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 *,. int(*xCompa
2e820 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f re)(void*,int,co
2e830 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f nst void*,int,co
2e840 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 nst void*).);.SQ
2e850 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2e860 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2e870 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 ation_v2(. sqli
2e880 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63 te3*, . const c
2e890 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 har *zName, . i
2e8a0 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 nt eTextRep, .
2e8b0 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 void*,. int(*xC
2e8c0 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e ompare)(void*,in
2e8d0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e t,const void*,in
2e8e0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a t,const void*),.
2e8f0 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 void(*xDestroy
2e900 29 28 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 )(void*).);.SQLI
2e910 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2e920 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2e930 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 ion16(. sqlite3
2e940 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 *, . const void
2e950 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 *zName,. int e
2e960 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 TextRep, . void
2e970 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 *,. int(*xCompa
2e980 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f re)(void*,int,co
2e990 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f nst void*,int,co
2e9a0 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f nst void*).);../
2e9b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
2e9c0 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65 64 65 64 20 ollation Needed
2e9d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 36 37 30 Callbacks {H1670
2e9e0 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S20300>.**.*
2e9f0 2a 20 54 6f 20 61 76 6f 69 64 20 68 61 76 69 6e * To avoid havin
2ea00 67 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 6c g to register al
2ea10 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 l collation sequ
2ea20 65 6e 63 65 73 20 62 65 66 6f 72 65 20 61 20 64 ences before a d
2ea30 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62 atabase.** can b
2ea40 65 20 75 73 65 64 2c 20 61 20 73 69 6e 67 6c 65 e used, a single
2ea50 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
2ea60 6f 6e 20 6d 61 79 20 62 65 20 72 65 67 69 73 74 on may be regist
2ea70 65 72 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a ered with the.**
2ea80 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2ea90 63 74 69 6f 6e 5d 20 74 6f 20 62 65 20 63 61 6c ction] to be cal
2eaa0 6c 65 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20 led whenever an
2eab0 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 undefined collat
2eac0 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ion.** sequence
2ead0 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a is required..**.
2eae0 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 ** If the functi
2eaf0 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64 on is registered
2eb00 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
2eb10 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
2eb20 64 65 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 ded() API,.** th
2eb30 65 6e 20 69 74 20 69 73 20 70 61 73 73 65 64 20 en it is passed
2eb40 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 the names of und
2eb50 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e efined collation
2eb60 20 73 65 71 75 65 6e 63 65 73 20 61 73 20 73 74 sequences as st
2eb70 72 69 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 rings.** encoded
2eb80 20 69 6e 20 55 54 46 2d 38 2e 20 7b 48 31 36 37 in UTF-8. {H167
2eb90 30 33 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63 03} If sqlite3_c
2eba0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
2ebb0 36 28 29 20 69 73 20 75 73 65 64 2c 0a 2a 2a 20 6() is used,.**
2ebc0 74 68 65 20 6e 61 6d 65 73 20 61 72 65 20 70 61 the names are pa
2ebd0 73 73 65 64 20 61 73 20 55 54 46 2d 31 36 20 69 ssed as UTF-16 i
2ebe0 6e 20 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65 n machine native
2ebf0 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 20 byte order..**
2ec00 41 20 63 61 6c 6c 20 74 6f 20 65 69 74 68 65 72 A call to either
2ec10 20 66 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63 function replac
2ec20 65 73 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 es any existing
2ec30 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 callback..**.**
2ec40 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 When the callbac
2ec50 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 k is invoked, th
2ec60 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
2ec70 20 70 61 73 73 65 64 20 69 73 20 61 20 63 6f 70 passed is a cop
2ec80 79 0a 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f y.** of the seco
2ec90 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 nd argument to s
2eca0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2ecb0 5f 6e 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20 _needed() or.**
2ecc0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f sqlite3_collatio
2ecd0 6e 5f 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54 n_needed16(). T
2ece0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
2ecf0 6e 74 20 69 73 20 74 68 65 20 64 61 74 61 62 61 nt is the databa
2ed00 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
2ed10 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67 . The third arg
2ed20 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 ument is one of
2ed30 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b [SQLITE_UTF8], [
2ed40 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c SQLITE_UTF16BE],
2ed50 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 .** or [SQLITE_U
2ed60 54 46 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74 TF16LE], indicat
2ed70 69 6e 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73 ing the most des
2ed80 69 72 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74 irable form of t
2ed90 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 he collation.**
2eda0 73 65 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f sequence functio
2edb0 6e 20 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 n required. The
2edc0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
2edd0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
2ede0 20 74 68 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 the.** required
2edf0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2ee00 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 nce..**.** The c
2ee10 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
2ee20 20 73 68 6f 75 6c 64 20 72 65 67 69 73 74 65 72 should register
2ee30 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c the desired col
2ee40 6c 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 lation using.**
2ee50 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
2ee60 63 6f 6c 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73 collation()], [s
2ee70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2ee80 6c 6c 61 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72 llation16()], or
2ee90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 .** [sqlite3_cre
2eea0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
2eeb0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
2eec0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2eed0 37 30 32 5d 20 5b 48 31 36 37 30 34 5d 20 5b 48 702] [H16704] [H
2eee0 31 36 37 30 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 16706].*/.SQLITE
2eef0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2ef00 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
2ef10 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a d(. sqlite3*, .
2ef20 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 void*, . void
2ef30 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 (*)(void*,sqlite
2ef40 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
2ef50 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a const char*).);.
2ef60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2ef70 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2ef80 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c _needed16(. sql
2ef90 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c ite3*, . void*,
2efa0 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a . void(*)(void*
2efb0 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 ,sqlite3*,int eT
2efc0 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 extRep,const voi
2efd0 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 d*).);../*.** Sp
2efe0 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f ecify the key fo
2eff0 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 r an encrypted d
2f000 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 atabase. This r
2f010 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 outine should be
2f020 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74 .** called right
2f030 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f after sqlite3_o
2f040 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 pen()..**.** The
2f050 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 code to impleme
2f060 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e nt this API is n
2f070 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 ot available in
2f080 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 the public relea
2f090 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e se.** of SQLite.
2f0a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
2f0b0 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a nt sqlite3_key(.
2f0c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
2f0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f0e0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 /* Database to
2f0f0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 be rekeyed */.
2f100 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
2f110 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f , int nKey /
2f120 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a * The key */.);.
2f130 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
2f140 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e e key on an open
2f150 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 database. If t
2f160 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
2f170 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e ase is not.** en
2f180 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f crypted, this ro
2f190 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 utine will encry
2f1a0 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d pt it. If pNew=
2f1b0 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 =0 or nNew==0, t
2f1c0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 he.** database i
2f1d0 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a s decrypted..**.
2f1e0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 ** The code to i
2f1f0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 mplement this AP
2f200 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 I is not availab
2f210 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 le in the public
2f220 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 release.** of S
2f230 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 QLite..*/.SQLITE
2f240 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2f250 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 _rekey(. sqlite
2f260 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
2f270 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
2f280 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 base to be rekey
2f290 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ed */. const vo
2f2a0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b id *pKey, int nK
2f2b0 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 ey /* The ne
2f2c0 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a w key */.);../*.
2f2d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73 ** CAPI3REF: Sus
2f2e0 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 pend Execution F
2f2f0 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 or A Short Time
2f300 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30 {H10530} <S40410
2f310 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
2f320 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63 te3_sleep() func
2f330 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 tion causes the
2f340 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74 current thread t
2f350 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74 o suspend execut
2f360 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65 ion.** for at le
2f370 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 ast a number of
2f380 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65 milliseconds spe
2f390 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61 cified in its pa
2f3a0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 rameter..**.** I
2f3b0 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 f the operating
2f3c0 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 system does not
2f3d0 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65 support sleep re
2f3e0 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d quests with.** m
2f3f0 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20 illisecond time
2f400 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e resolution, then
2f410 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62 the time will b
2f420 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a e rounded up to.
2f430 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73 ** the nearest s
2f440 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65 econd. The numbe
2f450 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 r of millisecond
2f460 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 s of sleep actua
2f470 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 lly.** requested
2f480 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74 from the operat
2f490 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65 ing system is re
2f4a0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 turned..**.** SQ
2f4b0 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 Lite implements
2f4c0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 this interface b
2f4d0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 y calling the xS
2f4e0 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 leep().** method
2f4f0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 of the default
2f500 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
2f510 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ject..**.** Requ
2f520 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 33 irements: [H1053
2f530 33 5d 20 5b 48 31 30 35 33 36 5d 0a 2a 2f 0a 53 3] [H10536].*/.S
2f540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2f550 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29 lite3_sleep(int)
2f560 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2f570 46 3a 20 4e 61 6d 65 20 4f 66 20 54 68 65 20 46 F: Name Of The F
2f580 6f 6c 64 65 72 20 48 6f 6c 64 69 6e 67 20 54 65 older Holding Te
2f590 6d 70 6f 72 61 72 79 20 46 69 6c 65 73 20 7b 48 mporary Files {H
2f5a0 31 30 33 31 30 7d 20 3c 53 32 30 30 30 30 3e 0a 10310} <S20000>.
2f5b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 67 6c **.** If this gl
2f5c0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 obal variable is
2f5d0 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 made to point t
2f5e0 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 o a string which
2f5f0 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 is.** the name
2f600 6f 66 20 61 20 66 6f 6c 64 65 72 20 28 61 2e 6b of a folder (a.k
2f610 2e 61 2e 20 64 69 72 65 63 74 6f 72 79 29 2c 20 .a. directory),
2f620 74 68 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72 61 then all tempora
2f630 72 79 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 ry files.** crea
2f640 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 ted by SQLite wi
2f650 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 ll be placed in
2f660 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e 20 that directory.
2f670 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c If this variabl
2f680 65 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 e.** is a NULL p
2f690 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c ointer, then SQL
2f6a0 69 74 65 20 70 65 72 66 6f 72 6d 73 20 61 20 73 ite performs a s
2f6b0 65 61 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70 earch for an app
2f6c0 72 6f 70 72 69 61 74 65 0a 2a 2a 20 74 65 6d 70 ropriate.** temp
2f6d0 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 orary file direc
2f6e0 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 tory..**.** It i
2f6f0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 s not safe to re
2f700 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 ad or modify thi
2f710 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 6d 6f s variable in mo
2f720 72 65 20 74 68 61 6e 20 6f 6e 65 0a 2a 2a 20 74 re than one.** t
2f730 68 72 65 61 64 20 61 74 20 61 20 74 69 6d 65 2e hread at a time.
2f740 20 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 It is not safe
2f750 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 to read or modi
2f760 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 fy this variable
2f770 0a 2a 2a 20 69 66 20 61 20 5b 64 61 74 61 62 61 .** if a [databa
2f780 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 se connection] i
2f790 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 74 20 s being used at
2f7a0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 69 6e the same time in
2f7b0 20 61 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 74 a separate.** t
2f7c0 68 72 65 61 64 2e 0a 2a 2a 20 49 74 20 69 73 20 hread..** It is
2f7d0 69 6e 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 intended that th
2f7e0 69 73 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 is variable be s
2f7f0 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 et once.** as pa
2f800 72 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e rt of process in
2f810 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 itialization and
2f820 20 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 before any SQLi
2f830 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 te interface.**
2f840 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 routines have be
2f850 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 en called and th
2f860 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 at this variable
2f870 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 remain unchange
2f880 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e d.** thereafter.
2f890 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 74 65 6d 70 .**.** The [temp
2f8a0 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 _store_directory
2f8b0 20 70 72 61 67 6d 61 5d 20 6d 61 79 20 6d 6f 64 pragma] may mod
2f8c0 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c ify this variabl
2f8d0 65 20 61 6e 64 20 63 61 75 73 65 0a 2a 2a 20 69 e and cause.** i
2f8e0 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 6d 65 t to point to me
2f8f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
2f900 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c om [sqlite3_mall
2f910 6f 63 5d 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 oc]. Furthermor
2f920 65 2c 0a 2a 2a 20 74 68 65 20 5b 74 65 6d 70 5f e,.** the [temp_
2f930 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 store_directory
2f940 70 72 61 67 6d 61 5d 20 61 6c 77 61 79 73 20 61 pragma] always a
2f950 73 73 75 6d 65 73 20 74 68 61 74 20 61 6e 79 20 ssumes that any
2f960 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 string.** that t
2f970 68 69 73 20 76 61 72 69 61 62 6c 65 20 70 6f 69 his variable poi
2f980 6e 74 73 20 74 6f 20 69 73 20 68 65 6c 64 20 69 nts to is held i
2f990 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
2f9a0 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 d from .** [sqli
2f9b0 74 65 33 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20 te3_malloc] and
2f9c0 74 68 65 20 70 72 61 67 6d 61 20 6d 61 79 20 61 the pragma may a
2f9d0 74 74 65 6d 70 74 20 74 6f 20 66 72 65 65 20 74 ttempt to free t
2f9e0 68 61 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 hat memory.** us
2f9f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 ing [sqlite3_fre
2fa00 65 5d 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 e]..** Hence, if
2fa10 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 this variable i
2fa20 73 20 6d 6f 64 69 66 69 65 64 20 64 69 72 65 63 s modified direc
2fa30 74 6c 79 2c 20 65 69 74 68 65 72 20 69 74 20 73 tly, either it s
2fa40 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65 hould be.** made
2fa50 20 4e 55 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f NULL or made to
2fa60 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 point to memory
2fa70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
2fa80 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a sqlite3_malloc].
2fa90 2a 2a 20 6f 72 20 65 6c 73 65 20 74 68 65 20 75 ** or else the u
2faa0 73 65 20 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f se of the [temp_
2fab0 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 store_directory
2fac0 70 72 61 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 pragma] should b
2fad0 65 20 61 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51 e avoided..*/.SQ
2fae0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
2faf0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
2fb00 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ctory;../*.** CA
2fb10 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f 72 PI3REF: Test For
2fb20 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 Auto-Commit Mod
2fb30 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 32 e {H12930} <S602
2fb40 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
2fb50 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 {autocommit mod
2fb60 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c e}.**.** The sql
2fb70 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d ite3_get_autocom
2fb80 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 mit() interface
2fb90 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f returns non-zero
2fba0 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 or.** zero if t
2fbb0 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 he given databas
2fbc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
2fbd0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 or is not in aut
2fbe0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a ocommit mode,.**
2fbf0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 respectively.
2fc00 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 Autocommit mode
2fc10 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 is on by default
2fc20 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 ..** Autocommit
2fc30 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 mode is disabled
2fc40 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 by a [BEGIN] st
2fc50 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f atement..** Auto
2fc60 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 commit mode is r
2fc70 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b e-enabled by a [
2fc80 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c COMMIT] or [ROLL
2fc90 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 BACK]..**.** If
2fca0 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 certain kinds of
2fcb0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e errors occur on
2fcc0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 a statement wit
2fcd0 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 hin a multi-stat
2fce0 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
2fcf0 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 tion (errors inc
2fd00 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 luding [SQLITE_F
2fd10 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f ULL], [SQLITE_IO
2fd20 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 ERR],.** [SQLITE
2fd30 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 _NOMEM], [SQLITE
2fd40 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c _BUSY], and [SQL
2fd50 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 ITE_INTERRUPT])
2fd60 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e then the.** tran
2fd70 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 saction might be
2fd80 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 rolled back aut
2fd90 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 omatically. The
2fda0 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 only way to.**
2fdb0 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72 find out whether
2fdc0 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 SQLite automati
2fdd0 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 cally rolled bac
2fde0 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
2fdf0 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 n after.** an er
2fe00 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 68 ror is to use th
2fe10 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a is function..**.
2fe20 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 ** If another th
2fe30 72 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 read changes the
2fe40 20 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 autocommit stat
2fe50 75 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 us of the databa
2fe60 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
2fe70 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 while this rout
2fe80 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 ine is running,
2fe90 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 then the return
2fea0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 value.** is unde
2feb0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 fined..**.** Req
2fec0 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39 uirements: [H129
2fed0 33 31 5d 20 5b 48 31 32 39 33 32 5d 20 5b 48 31 31] [H12932] [H1
2fee0 32 39 33 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a 2933] [H12934].*
2fef0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2ff00 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 sqlite3_get_aut
2ff10 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a ocommit(sqlite3*
2ff20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2ff30 45 46 3a 20 46 69 6e 64 20 54 68 65 20 44 61 74 EF: Find The Dat
2ff40 61 62 61 73 65 20 48 61 6e 64 6c 65 20 4f 66 20 abase Handle Of
2ff50 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 A Prepared State
2ff60 6d 65 6e 74 20 7b 48 31 33 31 32 30 7d 20 3c 53 ment {H13120} <S
2ff70 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 60600>.**.** The
2ff80 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 sqlite3_db_hand
2ff90 6c 65 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 le interface ret
2ffa0 75 72 6e 73 20 74 68 65 20 5b 64 61 74 61 62 61 urns the [databa
2ffb0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 se connection] h
2ffc0 61 6e 64 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63 andle.** to whic
2ffd0 68 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 h a [prepared st
2ffe0 61 74 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73 atement] belongs
2fff0 2e 20 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 . The [database
30000 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 connection].**
30010 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
30020 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73 te3_db_handle is
30030 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 the same [datab
30040 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
30050 74 68 61 74 20 77 61 73 20 74 68 65 20 66 69 72 that was the fir
30060 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 st argument.** t
30070 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 o the [sqlite3_p
30080 72 65 70 61 72 65 5f 76 32 28 29 5d 20 63 61 6c repare_v2()] cal
30090 6c 20 28 6f 72 20 69 74 73 20 76 61 72 69 61 6e l (or its varian
300a0 74 73 29 20 74 68 61 74 20 77 61 73 20 75 73 65 ts) that was use
300b0 64 20 74 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 d to.** create t
300c0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 he statement in
300d0 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e the first place.
300e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
300f0 6e 74 73 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f nts: [H13123].*/
30100 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
30110 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f te3 *sqlite3_db_
30120 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 handle(sqlite3_s
30130 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
30140 50 49 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65 PI3REF: Find the
30150 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 next prepared s
30160 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31 34 30 tatement {H13140
30170 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a } <S60600>.**.**
30180 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
30190 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
301a0 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 r to the next [p
301b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
301c0 74 5d 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d t] after.** pStm
301d0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 t associated wit
301e0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 h the [database
301f0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e connection] pDb.
30200 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 If pStmt is NU
30210 4c 4c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 LL.** then this
30220 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
30230 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
30240 68 65 20 66 69 72 73 74 20 70 72 65 70 61 72 65 he first prepare
30250 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 d statement.** a
30260 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
30270 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
30280 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 ection pDb. If
30290 6e 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74 no prepared stat
302a0 65 6d 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 ement.** satisfi
302b0 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e es the condition
302c0 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e s of this routin
302d0 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 e, it returns NU
302e0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 LL..**.** The [d
302f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
30300 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e on] pointer D in
30310 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 a call to.** [s
30320 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
30330 28 44 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65 (D,S)] must refe
30340 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 r to an open dat
30350 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
30360 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69 ion and in parti
30370 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 cular must not b
30380 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 e a NULL pointer
30390 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
303a0 65 6e 74 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b ents: [H13143] [
303b0 48 31 33 31 34 36 5d 20 5b 48 31 33 31 34 39 5d H13146] [H13149]
303c0 20 5b 48 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c [H13152].*/.SQL
303d0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
303e0 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 stmt *sqlite3_ne
303f0 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 xt_stmt(sqlite3
30400 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 *pDb, sqlite3_st
30410 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a mt *pStmt);../*.
30420 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d ** CAPI3REF: Com
30430 6d 69 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b mit And Rollback
30440 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 Notification Ca
30450 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 35 30 7d llbacks {H12950}
30460 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60400>.**.**
30470 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d The sqlite3_comm
30480 69 74 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 it_hook() interf
30490 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 ace registers a
304a0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 callback.** func
304b0 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b tion to be invok
304c0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 ed whenever a tr
304d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b 43 4f ansaction is [CO
304e0 4d 4d 49 54 20 7c 20 63 6f 6d 6d 69 74 74 65 64 MMIT | committed
304f0 5d 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 ]..** Any callba
30500 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
30510 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c ious call to sql
30520 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
30530 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 ().** for the sa
30540 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
30550 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 ection is overri
30560 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c dden..** The sql
30570 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
30580 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
30590 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
305a0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ack.** function
305b0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
305c0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 enever a transac
305d0 74 69 6f 6e 20 69 73 20 5b 52 4f 4c 4c 42 41 43 tion is [ROLLBAC
305e0 4b 20 7c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 5d K | rolled back]
305f0 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 ..** Any callbac
30600 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 k set by a previ
30610 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ous call to sqli
30620 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
30630 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d ).** for the sam
30640 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
30650 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 ction is overrid
30660 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67 den..** The pArg
30670 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73 argument is pas
30680 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 sed through to t
30690 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 he callback..**
306a0 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 If the callback
306b0 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b on a commit hook
306c0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
306d0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 s non-zero,.** t
306e0 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 hen the commit i
306f0 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f s converted into
30700 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a a rollback..**.
30710 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 ** If another fu
30720 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 nction was previ
30730 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 ously registered
30740 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61 , its.** pArg va
30750 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e lue is returned.
30760 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
30770 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
30780 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
30790 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
307a0 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
307b0 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
307c0 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
307d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
307e0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
307f0 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 e callback. Any
30800 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d actions.** to m
30810 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 odify the databa
30820 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 se connection mu
30830 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 st be deferred u
30840 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a ntil after the.*
30850 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 * completion of
30860 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 the [sqlite3_ste
30870 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 p()] call that t
30880 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d riggered the com
30890 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 mit.** or rollba
308a0 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 ck hook in the f
308b0 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e irst place..** N
308c0 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 ote that [sqlite
308d0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
308e0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 and [sqlite3_ste
308f0 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 p()] both modify
30900 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 their.** databa
30910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 se connections f
30920 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f or the meaning o
30930 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 f "modify" in th
30940 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a is paragraph..**
30950 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20 .** Registering
30960 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 a NULL function
30970 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c disables the cal
30980 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 lback..**.** Whe
30990 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f n the commit hoo
309a0 6b 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 k callback routi
309b0 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c ne returns zero,
309c0 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a the [COMMIT].**
309d0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6c operation is al
309e0 6c 6f 77 65 64 20 74 6f 20 63 6f 6e 74 69 6e 75 lowed to continu
309f0 65 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 49 66 20 e normally. If
30a00 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0a the commit hook.
30a10 2a 2a 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a ** returns non-z
30a20 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 5b 43 ero, then the [C
30a30 4f 4d 4d 49 54 5d 20 69 73 20 63 6f 6e 76 65 72 OMMIT] is conver
30a40 74 65 64 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c ted into a [ROLL
30a50 42 41 43 4b 5d 2e 0a 2a 2a 20 54 68 65 20 72 6f BACK]..** The ro
30a60 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69 llback hook is i
30a70 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c nvoked on a roll
30a80 62 61 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74 back that result
30a90 73 20 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0a s from a commit.
30aa0 2a 2a 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e ** hook returnin
30ab0 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74 g non-zero, just
30ac0 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 as it would be
30ad0 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72 with any other r
30ae0 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 ollback..**.** F
30af0 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 or the purposes
30b00 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 of this API, a t
30b10 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 ransaction is sa
30b20 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a id to have been.
30b30 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 ** rolled back i
30b40 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 f an explicit "R
30b50 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 OLLBACK" stateme
30b60 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 nt is executed,
30b70 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f or.** an error o
30b80 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 r constraint cau
30b90 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 ses an implicit
30ba0 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 rollback to occu
30bb0 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 r..** The rollba
30bc0 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e ck callback is n
30bd0 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 ot invoked if a
30be0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a transaction is.*
30bf0 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
30c00 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 rolled back beca
30c10 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 use the database
30c20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 connection is c
30c30 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f losed..** The ro
30c40 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 llback callback
30c50 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 is not invoked i
30c60 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
30c70 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 is.** rolled bac
30c80 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d k because a comm
30c90 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 it callback retu
30ca0 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a rned non-zero..*
30cb0 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f * <todo> Check o
30cc0 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a n this </todo>.*
30cd0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 *.** See also th
30ce0 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 e [sqlite3_updat
30cf0 65 5f 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66 e_hook()] interf
30d00 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ace..**.** Requi
30d10 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
30d20 39 35 31 5d 20 5b 48 31 32 39 35 32 5d 20 5b 48 951] [H12952] [H
30d30 31 32 39 35 33 5d 20 5b 48 31 32 39 35 34 5d 20 12953] [H12954]
30d40 5b 48 31 32 39 35 35 5d 0a 2a 2a 20 5b 48 31 32 [H12955].** [H12
30d50 39 36 31 5d 20 5b 48 31 32 39 36 32 5d 20 5b 48 961] [H12962] [H
30d60 31 32 39 36 33 5d 20 5b 48 31 32 39 36 34 5d 0a 12963] [H12964].
30d70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
30d80 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d id *sqlite3_comm
30d90 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a it_hook(sqlite3*
30da0 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c , int(*)(void*),
30db0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
30dc0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
30dd0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 3_rollback_hook(
30de0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a sqlite3*, void(*
30df0 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a )(void *), void*
30e00 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
30e10 45 46 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20 EF: Data Change
30e20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c Notification Cal
30e30 6c 62 61 63 6b 73 20 7b 48 31 32 39 37 30 7d 20 lbacks {H12970}
30e40 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S60400>.**.** T
30e50 68 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 he sqlite3_updat
30e60 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 e_hook() interfa
30e70 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 ce registers a c
30e80 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
30e90 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61 .** with the [da
30ea0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
30eb0 6e 5d 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 n] identified by
30ec0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
30ed0 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 ent.** to be inv
30ee0 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 oked whenever a
30ef0 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 row is updated,
30f00 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 inserted or dele
30f10 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c ted..** Any call
30f20 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 back set by a pr
30f30 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 74 evious call to t
30f40 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 his function.**
30f50 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 for the same dat
30f60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
30f70 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a is overridden..
30f80 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
30f90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
30fa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 ointer to the fu
30fb0 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 nction to invoke
30fc0 20 77 68 65 6e 20 61 0a 2a 2a 20 72 6f 77 20 69 when a.** row i
30fd0 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 s updated, inser
30fe0 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ted or deleted..
30ff0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
31000 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c ument to the cal
31010 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20 lback is a copy
31020 6f 66 20 74 68 65 20 74 68 69 72 64 20 61 72 67 of the third arg
31030 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 ument.** to sqli
31040 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 te3_update_hook(
31050 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 )..** The second
31060 20 63 61 6c 6c 62 61 63 6b 20 61 72 67 75 6d 65 callback argume
31070 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 nt is one of [SQ
31080 4c 49 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 LITE_INSERT], [S
31090 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a QLITE_DELETE],.*
310a0 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 * or [SQLITE_UPD
310b0 41 54 45 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20 ATE], depending
310c0 6f 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e on the operation
310d0 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
310e0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20 callback.** to
310f0 62 65 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54 be invoked..** T
31100 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 he third and fou
31110 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f rth arguments to
31120 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f the callback co
31130 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 ntain pointers t
31140 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 o the.** databas
31150 65 20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d 65 e and table name
31160 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
31170 61 66 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a affected row..**
31180 20 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 The final callb
31190 61 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 ack parameter is
311a0 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 the [rowid] of
311b0 74 68 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20 74 the row..** In t
311c0 68 65 20 63 61 73 65 20 6f 66 20 61 6e 20 75 70 he case of an up
311d0 64 61 74 65 2c 20 74 68 69 73 20 69 73 20 74 68 date, this is th
311e0 65 20 5b 72 6f 77 69 64 5d 20 61 66 74 65 72 20 e [rowid] after
311f0 74 68 65 20 75 70 64 61 74 65 20 74 61 6b 65 73 the update takes
31200 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 place..**.** Th
31210 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 73 e update hook is
31220 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 not invoked whe
31230 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73 74 65 n internal syste
31240 6d 20 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20 m tables are.**
31250 6d 6f 64 69 66 69 65 64 20 28 69 2e 65 2e 20 73 modified (i.e. s
31260 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 qlite_master and
31270 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
31280 29 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 )..**.** In the
31290 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e current implemen
312a0 74 61 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 tation, the upda
312b0 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f te hook.** is no
312c0 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64 t invoked when d
312d0 75 70 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20 uplication rows
312e0 61 72 65 20 64 65 6c 65 74 65 64 20 62 65 63 61 are deleted beca
312f0 75 73 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e use of an.** [ON
31300 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43 CONFLICT | ON C
31310 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d ONFLICT REPLACE]
31320 20 63 6c 61 75 73 65 2e 20 20 4e 6f 72 20 69 73 clause. Nor is
31330 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
31340 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e .** invoked when
31350 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 rows are delete
31360 64 20 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75 d using the [tru
31370 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 ncate optimizati
31380 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65 on]..** The exce
31390 70 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 ptions defined i
313a0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
313b0 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e might change in
313c0 20 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c a future.** rel
313d0 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a ease of SQLite..
313e0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 **.** The update
313f0 20 68 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 hook implementa
31400 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f tion must not do
31410 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 anything that w
31420 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 ill modify.** th
31430 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
31440 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b ction that invok
31450 65 64 20 74 68 65 20 75 70 64 61 74 65 20 68 6f ed the update ho
31460 6f 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 ok. Any actions
31470 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 .** to modify th
31480 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
31490 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 ction must be de
314a0 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 ferred until aft
314b0 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 er the.** comple
314c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
314d0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c ite3_step()] cal
314e0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 l that triggered
314f0 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
31500 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b ..** Note that [
31510 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
31520 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 v2()] and [sqlit
31530 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 e3_step()] both
31540 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 modify their.**
31550 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
31560 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 ions for the mea
31570 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 ning of "modify"
31580 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 in this paragra
31590 70 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f ph..**.** If ano
315a0 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 ther function wa
315b0 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 s previously reg
315c0 69 73 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 istered, its pAr
315d0 67 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 g value.** is re
315e0 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 turned. Otherwi
315f0 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 se NULL is retur
31600 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ned..**.** See a
31610 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 lso the [sqlite3
31620 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 _commit_hook()]
31630 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c and [sqlite3_rol
31640 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0a 2a 2a lback_hook()].**
31650 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 0a interfaces..**.
31660 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
31670 0a 2a 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31 .** [H12971] [H1
31680 32 39 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b 2973] [H12975] [
31690 48 31 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d H12977] [H12979]
316a0 20 5b 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38 [H12981] [H1298
316b0 33 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53 3] [H12986].*/.S
316c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
316d0 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 sqlite3_update_h
316e0 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c ook(. sqlite3*,
316f0 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 . void(*)(void
31700 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e *,int ,char con
31710 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 st *,char const
31720 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *,sqlite3_int64)
31730 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a ,. void*.);../*
31740 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e .** CAPI3REF: En
31750 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 able Or Disable
31760 53 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63 Shared Pager Cac
31770 68 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 he {H10330} <S30
31780 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 900>.** KEYWORDS
31790 3a 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d : {shared cache}
317a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
317b0 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 ine enables or d
317c0 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 isables the shar
317d0 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 ing of the datab
317e0 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 ase cache.** and
317f0 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 schema data str
31800 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 uctures between
31810 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
31820 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f tion | connectio
31830 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 ns].** to the sa
31840 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 me database. Sha
31850 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 ring is enabled
31860 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 if the argument
31870 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 is true.** and d
31880 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 isabled if the a
31890 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 rgument is false
318a0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68 ..**.** Cache sh
318b0 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 aring is enabled
318c0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f and disabled fo
318d0 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63 r an entire proc
318e0 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 ess..** This is
318f0 61 20 63 68 61 6e 67 65 20 61 73 20 6f 66 20 53 a change as of S
31900 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e QLite version 3.
31910 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 20 76 65 5.0. In prior ve
31920 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
31930 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 77 61 73 ,.** sharing was
31940 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 enabled or disa
31950 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20 74 68 bled for each th
31960 72 65 61 64 20 73 65 70 61 72 61 74 65 6c 79 2e read separately.
31970 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 .**.** The cache
31980 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 20 73 65 sharing mode se
31990 74 20 62 79 20 74 68 69 73 20 69 6e 74 65 72 66 t by this interf
319a0 61 63 65 20 65 66 66 65 63 74 73 20 61 6c 6c 20 ace effects all
319b0 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 subsequent.** ca
319c0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
319d0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
319e0 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 3_open_v2()], an
319f0 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 d [sqlite3_open1
31a00 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 6()]..** Existin
31a10 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 g database conne
31a20 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 ctions continue
31a30 75 73 65 20 74 68 65 20 73 68 61 72 69 6e 67 20 use the sharing
31a40 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 20 77 61 73 mode.** that was
31a50 20 69 6e 20 65 66 66 65 63 74 20 61 74 20 74 68 in effect at th
31a60 65 20 74 69 6d 65 20 74 68 65 79 20 77 65 72 65 e time they were
31a70 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 opened..**.** V
31a80 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 irtual tables ca
31a90 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 nnot be used wit
31aa0 68 20 61 20 73 68 61 72 65 64 20 63 61 63 68 65 h a shared cache
31ab0 2e 20 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a . When shared.*
31ac0 2a 20 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c * cache is enabl
31ad0 65 64 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ed, the [sqlite3
31ae0 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 _create_module()
31af0 5d 20 41 50 49 20 75 73 65 64 20 74 6f 20 72 65 ] API used to re
31b00 67 69 73 74 65 72 0a 2a 2a 20 76 69 72 74 75 61 gister.** virtua
31b10 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 61 6c l tables will al
31b20 77 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 65 ways return an e
31b30 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rror..**.** This
31b40 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
31b50 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 [SQLITE_OK] if
31b60 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61 73 shared cache was
31b70 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 enabled or disa
31b80 62 6c 65 64 0a 2a 2a 20 73 75 63 63 65 73 73 66 bled.** successf
31b90 75 6c 6c 79 2e 20 20 41 6e 20 5b 65 72 72 6f 72 ully. An [error
31ba0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
31bb0 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a ed otherwise..**
31bc0 0a 2a 2a 20 53 68 61 72 65 64 20 63 61 63 68 65 .** Shared cache
31bd0 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 is disabled by
31be0 64 65 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 default. But thi
31bf0 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 s might change i
31c00 6e 0a 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 n.** future rele
31c10 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 ases of SQLite.
31c20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 Applications th
31c30 61 74 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 at care about sh
31c40 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 ared.** cache se
31c50 74 74 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 tting should set
31c60 20 69 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a it explicitly..
31c70 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 **.** See Also:
31c80 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d [SQLite Shared-
31c90 43 61 63 68 65 20 4d 6f 64 65 5d 0a 2a 2a 0a 2a Cache Mode].**.*
31ca0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
31cb0 5b 48 31 30 33 33 31 5d 20 5b 48 31 30 33 33 36 [H10331] [H10336
31cc0 5d 20 5b 48 31 30 33 33 37 5d 20 5b 48 31 30 33 ] [H10337] [H103
31cd0 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 39].*/.SQLITE_AP
31ce0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e I int sqlite3_en
31cf0 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 able_shared_cach
31d00 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 e(int);../*.** C
31d10 41 50 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 API3REF: Attempt
31d20 20 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 To Free Heap Me
31d30 6d 6f 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 mory {H17340} <S
31d40 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30220>.**.** The
31d50 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
31d60 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 _memory() interf
31d70 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ace attempts to
31d80 66 72 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 free N bytes.**
31d90 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 of heap memory b
31da0 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e y deallocating n
31db0 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
31dc0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a ory allocations.
31dd0 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 ** held by the d
31de0 61 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e atabase library.
31df0 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 {END} Memory u
31e00 73 65 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 sed to cache dat
31e10 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 abase.** pages t
31e20 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 o improve perfor
31e30 6d 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d mance is an exam
31e40 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e ple of non-essen
31e50 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 tial memory..**
31e60 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
31e70 6d 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 memory() returns
31e80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
31e90 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 ytes actually fr
31ea0 65 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 eed,.** which mi
31eb0 67 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c ght be more or l
31ec0 65 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f ess than the amo
31ed0 75 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a unt requested..*
31ee0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
31ef0 73 3a 20 5b 48 31 37 33 34 31 5d 20 5b 48 31 37 s: [H17341] [H17
31f00 33 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 342].*/.SQLITE_A
31f10 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
31f20 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e elease_memory(in
31f30 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
31f40 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69 REF: Impose A Li
31f50 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69 7a 65 mit On Heap Size
31f60 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 30 32 32 {H17350} <S3022
31f70 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
31f80 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
31f90 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 imit() interface
31fa0 20 70 6c 61 63 65 73 20 61 20 22 73 6f 66 74 22 places a "soft"
31fb0 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 limit.** on the
31fc0 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 amount of heap
31fd0 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20 memory that may
31fe0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 be allocated by
31ff0 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e SQLite..** If an
32000 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 internal alloca
32010 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 tion is requeste
32020 64 20 74 68 61 74 20 77 6f 75 6c 64 20 65 78 63 d that would exc
32030 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20 eed the.** soft
32040 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c heap limit, [sql
32050 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
32060 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 ory()] is invoke
32070 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 d one or.** more
32080 20 74 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75 times to free u
32090 70 20 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66 p some space bef
320a0 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 ore the allocati
320b0 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e on is performed.
320c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 .**.** The limit
320d0 20 69 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 is called "soft
320e0 22 2c 20 62 65 63 61 75 73 65 20 69 66 20 5b 73 ", because if [s
320f0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
32100 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e emory()].** cann
32110 6f 74 20 66 72 65 65 20 73 75 66 66 69 63 69 65 ot free sufficie
32120 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 nt memory to pre
32130 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66 vent the limit f
32140 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64 rom being exceed
32150 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 ed,.** the memor
32160 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 y is allocated a
32170 6e 79 77 61 79 20 61 6e 64 20 74 68 65 20 63 75 nyway and the cu
32180 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 rrent operation
32190 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 proceeds..**.**
321a0 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 A negative or ze
321b0 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d ro value for N m
321c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 eans that there
321d0 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 is no soft heap
321e0 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 limit and.** [sq
321f0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
32200 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c mory()] will onl
32210 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e y be called when
32220 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 68 61 75 memory is exhau
32230 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 sted..** The def
32240 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
32250 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
32260 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a it is zero..**.*
32270 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 * SQLite makes a
32280 20 62 65 73 74 20 65 66 66 6f 72 74 20 74 6f 20 best effort to
32290 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74 20 68 honor the soft h
322a0 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 eap limit..** Bu
322b0 74 20 69 66 20 74 68 65 20 73 6f 66 74 20 68 65 t if the soft he
322c0 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 ap limit cannot
322d0 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63 be honored, exec
322e0 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f ution will.** co
322f0 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74 20 65 ntinue without e
32300 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63 61 rror or notifica
32310 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 77 tion. This is w
32320 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69 73 0a hy the limit is.
32330 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73 6f 66 ** called a "sof
32340 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20 69 73 t" limit. It is
32350 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a advisory only..
32360 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53 **.** Prior to S
32370 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e QLite version 3.
32380 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 5.0, this routin
32390 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e e only constrain
323a0 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a ed the memory.**
323b0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20 allocated by a
323c0 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 2d 20 single thread -
323d0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
323e0 69 6e 20 77 68 69 63 68 20 74 68 69 73 20 72 6f in which this ro
323f0 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 utine.** runs.
32400 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 Beginning with S
32410 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e QLite version 3.
32420 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 20 68 65 5.0, the soft he
32430 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61 ap limit is.** a
32440 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c 20 74 68 pplied to all th
32450 72 65 61 64 73 2e 20 54 68 65 20 76 61 6c 75 65 reads. The value
32460 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 20 74 specified for t
32470 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
32480 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 70 70 65 it.** is an uppe
32490 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74 r bound on the t
324a0 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f otal memory allo
324b0 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 cation for all t
324c0 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 hreads. In.** ve
324d0 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72 rsion 3.5.0 ther
324e0 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 e is no mechanis
324f0 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74 m for limiting t
32500 68 65 20 68 65 61 70 20 75 73 61 67 65 20 66 6f he heap usage fo
32510 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 r.** individual
32520 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 threads..**.** R
32530 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
32540 5b 48 31 36 33 35 31 5d 20 5b 48 31 36 33 35 32 [H16351] [H16352
32550 5d 20 5b 48 31 36 33 35 33 5d 20 5b 48 31 36 33 ] [H16353] [H163
32560 35 34 5d 20 5b 48 31 36 33 35 35 5d 20 5b 48 31 54] [H16355] [H1
32570 36 33 35 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 6358].*/.SQLITE_
32580 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
32590 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 _soft_heap_limit
325a0 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 (int);../*.** CA
325b0 50 49 33 52 45 46 3a 20 45 78 74 72 61 63 74 20 PI3REF: Extract
325c0 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74 20 41 Metadata About A
325d0 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62 Column Of A Tab
325e0 6c 65 20 7b 48 31 32 38 35 30 7d 20 3c 53 36 30 le {H12850} <S60
325f0 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 300>.**.** This
32600 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
32610 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 20 61 metadata about a
32620 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e specific column
32630 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 0a 2a of a specific.*
32640 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 * database table
32650 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e accessible usin
32660 67 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 g the [database
32670 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 connection] hand
32680 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 le.** passed as
32690 74 68 65 20 66 69 72 73 74 20 66 75 6e 63 74 69 the first functi
326a0 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a on argument..**.
326b0 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73 ** The column is
326c0 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 identified by t
326d0 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64 he second, third
326e0 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 and fourth para
326f0 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 meters to.** thi
32700 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 s function. The
32710 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
32720 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 6e is either the n
32730 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
32740 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d 61 ase.** (i.e. "ma
32750 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 61 in", "temp" or a
32760 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 n attached datab
32770 61 73 65 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ase) containing
32780 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
32790 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20 table or NULL.
327a0 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 If it is NULL, t
327b0 68 65 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 hen all attached
327c0 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73 databases are s
327d0 65 61 72 63 68 65 64 0a 2a 2a 20 66 6f 72 20 74 earched.** for t
327e0 68 65 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 he table using t
327f0 68 65 20 73 61 6d 65 20 61 6c 67 6f 72 69 74 68 he same algorith
32800 6d 20 75 73 65 64 20 62 79 20 74 68 65 20 64 61 m used by the da
32810 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 74 6f tabase engine to
32820 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 75 6e 71 75 .** resolve unqu
32830 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 72 65 alified table re
32840 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 ferences..**.**
32850 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f The third and fo
32860 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 urth parameters
32870 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
32880 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 20 61 are the table a
32890 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d nd column.** nam
328a0 65 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 e of the desired
328b0 20 63 6f 6c 75 6d 6e 2c 20 72 65 73 70 65 63 74 column, respect
328c0 69 76 65 6c 79 2e 20 4e 65 69 74 68 65 72 20 6f ively. Neither o
328d0 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 f these paramete
328e0 72 73 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c rs.** may be NUL
328f0 4c 2e 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 64 61 74 L..**.** Metadat
32900 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 a is returned by
32910 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
32920 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 memory locations
32930 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 35 passed as the 5
32940 74 68 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71 th.** and subseq
32950 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20 uent parameters
32960 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
32970 2e 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61 . Any of these a
32980 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0a rguments may be.
32990 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69 63 ** NULL, in whic
329a0 68 20 63 61 73 65 20 74 68 65 20 63 6f 72 72 65 h case the corre
329b0 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e 74 sponding element
329c0 20 6f 66 20 6d 65 74 61 64 61 74 61 20 69 73 20 of metadata is
329d0 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c omitted..**.** <
329e0 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c blockquote>.** <
329f0 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 table border="1"
32a00 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 50 61 >.** <tr><th> Pa
32a10 72 61 6d 65 74 65 72 20 3c 74 68 3e 20 4f 75 74 rameter <th> Out
32a20 70 75 74 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e put<br>Type <th>
32a30 20 20 44 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a Description.**
32a40 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 35 74 68 .** <tr><td> 5th
32a50 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 <td> const char
32a60 2a 20 3c 74 64 3e 20 44 61 74 61 20 74 79 70 65 * <td> Data type
32a70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 36 74 68 .** <tr><td> 6th
32a80 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 <td> const char
32a90 2a 20 3c 74 64 3e 20 4e 61 6d 65 20 6f 66 20 64 * <td> Name of d
32aa0 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e efault collation
32ab0 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 3c 74 72 sequence.** <tr
32ac0 3e 3c 74 64 3e 20 37 74 68 20 3c 74 64 3e 20 69 ><td> 7th <td> i
32ad0 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 nt <td>
32ae0 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 68 True if column h
32af0 61 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f as a NOT NULL co
32b00 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e nstraint.** <tr>
32b10 3c 74 64 3e 20 38 74 68 20 3c 74 64 3e 20 69 6e <td> 8th <td> in
32b20 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 t <td> T
32b30 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 rue if column is
32b40 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 part of the PRI
32b50 4d 41 52 59 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e MARY KEY.** <tr>
32b60 3c 74 64 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e <td> 9th <td> in
32b70 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 t <td> T
32b80 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 rue if column is
32b90 20 5b 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d [AUTOINCREMENT]
32ba0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 .** </table>.**
32bb0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
32bc0 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 70 .** The memory p
32bd0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 ointed to by the
32be0 20 63 68 61 72 61 63 74 65 72 20 70 6f 69 6e 74 character point
32bf0 65 72 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72 ers returned for
32c00 20 74 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 the.** declarat
32c10 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c ion type and col
32c20 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
32c30 69 73 20 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e is valid only un
32c40 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 til the next.**
32c50 63 61 6c 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69 call to any SQLi
32c60 74 65 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e te API function.
32c70 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 .**.** If the sp
32c80 65 63 69 66 69 65 64 20 74 61 62 6c 65 20 69 73 ecified table is
32c90 20 61 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77 actually a view
32ca0 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 , an [error code
32cb0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a ] is returned..*
32cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 *.** If the spec
32cd0 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 ified column is
32ce0 22 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f "rowid", "oid" o
32cf0 72 20 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20 r "_rowid_" and
32d00 61 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 an.** [INTEGER P
32d10 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 RIMARY KEY] colu
32d20 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c mn has been expl
32d30 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c icitly declared,
32d40 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 then the output
32d50 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 .** parameters a
32d60 72 65 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 re set for the e
32d70 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 xplicitly declar
32d80 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 ed column. If th
32d90 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 70 ere is no.** exp
32da0 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 licitly declared
32db0 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 [INTEGER PRIMAR
32dc0 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74 Y KEY] column, t
32dd0 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a hen the output.*
32de0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 * parameters are
32df0 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a set as follows:
32e00 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 .**.** <pre>.**
32e10 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 22 data type: "
32e20 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20 INTEGER".**
32e30 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
32e40 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 ce: "BINARY".**
32e50 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a not null: 0.
32e60 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b ** primary k
32e70 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74 ey: 1.** aut
32e80 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a o increment: 0.*
32e90 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 * </pre>.**.** T
32ea0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
32eb0 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 load one or mor
32ec0 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64 e schemas from d
32ed0 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49 atabase files. I
32ee0 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 f an.** error oc
32ef0 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 73 curs during this
32f00 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 process, or if
32f10 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 61 the requested ta
32f20 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a ble or column.**
32f30 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 cannot be found
32f40 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 , an [error code
32f50 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
32f60 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
32f70 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68 ge left.** in th
32f80 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
32f90 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72 ection] (to be r
32fa0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 73 etrieved using s
32fb0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29 qlite3_errmsg())
32fc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 ..**.** This API
32fd0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
32fe0 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 le if the librar
32ff0 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 y was compiled w
33000 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 ith the.** [SQLI
33010 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
33020 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 _METADATA] C-pre
33030 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c processor symbol
33040 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c defined..*/.SQL
33050 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
33060 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e te3_table_column
33070 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c _metadata(. sql
33080 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
33090 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e /* Conn
330a0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f ection handle */
330b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
330c0 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f DbName, /
330d0 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 * Database name
330e0 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e or NULL */. con
330f0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e st char *zTableN
33100 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c ame, /* Tabl
33110 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 e name */. cons
33120 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e t char *zColumnN
33130 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d ame, /* Colum
33140 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 n name */. char
33150 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 const **pzDataT
33160 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 ype, /* OUTPU
33170 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61 T: Declared data
33180 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 type */. char
33190 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 const **pzCollSe
331a0 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 q, /* OUTPUT
331b0 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 : Collation sequ
331c0 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 ence name */. i
331d0 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 nt *pNotNull,
331e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
331f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f TPUT: True if NO
33200 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e T NULL constrain
33210 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e t exists */. in
33220 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 t *pPrimaryKey,
33230 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
33240 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c PUT: True if col
33250 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a umn part of PK *
33260 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e /. int *pAutoin
33270 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c
33280 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 /* OUTPUT: True
33290 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 if column is aut
332a0 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 o-increment */.)
332b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
332c0 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e F: Load An Exten
332d0 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53 sion {H12600} <S
332e0 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 20500>.**.** Thi
332f0 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64 s interface load
33300 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 s an SQLite exte
33310 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72 nsion library fr
33320 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c om the named fil
33330 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 e..**.** {H12601
33340 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f } The sqlite3_lo
33350 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 ad_extension() i
33360 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 nterface attempt
33370 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 s to load an.**
33380 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 SQLite
33390 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 extension librar
333a0 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 y contained in t
333b0 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a he file zFile..*
333c0 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68 *.** {H12602} Th
333d0 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 e entry point is
333e0 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 zProc..**.** {H
333f0 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79 12603} zProc may
33400 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20 be 0, in which
33410 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 case the name of
33420 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 the entry point
33430 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 66 .** def
33440 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65 aults to "sqlite
33450 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 3_extension_init
33460 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 "..**.** {H12604
33470 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f } The sqlite3_lo
33480 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 ad_extension() i
33490 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 nterface shall r
334a0 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 eturn.**
334b0 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e [SQLITE_OK] on
334c0 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51 success and [SQ
334d0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73 LITE_ERROR] if s
334e0 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 omething goes wr
334f0 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ong..**.** {H126
33500 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 05} If an error
33510 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 occurs and pzErr
33520 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 Msg is not 0, th
33530 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 en the.**
33540 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 [sqlite3_load
33550 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e _extension()] in
33560 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74 terface shall at
33570 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20 tempt to.**
33580 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72 fill *pzErr
33590 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d Msg with error m
335a0 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f 72 essage text stor
335b0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ed in memory.**
335c0 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 obtaine
335d0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
335e0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d malloc()]. {END}
335f0 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 The calling fu
33600 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 nction.**
33610 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 should free t
33620 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 his memory by ca
33630 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 lling [sqlite3_f
33640 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ree()]..**.** {H
33650 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e 12606} Extension
33660 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65 loading must be
33670 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a enabled using.*
33680 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
33690 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f te3_enable_load_
336a0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 extension()] pri
336b0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 or to calling th
336c0 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20 is API,.**
336d0 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e otherwise an
336e0 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 error will be r
336f0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 eturned..**.** S
33700 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 6c 6f 61 ee also the [loa
33710 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 53 51 d_extension() SQ
33720 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2f 0a L function]..*/.
33730 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
33740 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 qlite3_load_exte
33750 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 nsion(. sqlite3
33760 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f *db, /
33770 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e * Load the exten
33780 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 sion into this d
33790 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
337a0 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 on */. const ch
337b0 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a ar *zFile, /*
337c0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 Name of the sha
337d0 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 red library cont
337e0 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e aining extension
337f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
33800 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 *zProc, /* E
33810 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72 ntry point. Der
33820 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20 ived from zFile
33830 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a if 0 */. char *
33840 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 *pzErrMsg
33850 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 /* Put error mes
33860 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 sage here if not
33870 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 0 */.);../*.**
33880 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 CAPI3REF: Enable
33890 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 Or Disable Exte
338a0 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 nsion Loading {H
338b0 31 32 36 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a 12620} <S20500>.
338c0 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 **.** So as not
338d0 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 to open security
338e0 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 holes in older
338f0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 applications tha
33900 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 t are.** unprepa
33910 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 red to deal with
33920 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 extension loadi
33930 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61 ng, and as a mea
33940 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a ns of disabling.
33950 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 ** extension loa
33960 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75 ding while evalu
33970 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 ating user-enter
33980 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c ed SQL, the foll
33990 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 owing API.** is
339a0 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e provided to turn
339b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f the [sqlite3_lo
339c0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 ad_extension()]
339d0 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 mechanism on and
339e0 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 off..**.** Exte
339f0 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 nsion loading is
33a00 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e off by default.
33a10 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36 See ticket #186
33a20 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 3..**.** {H12621
33a30 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 } Call the sqlit
33a40 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 e3_enable_load_e
33a50 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 xtension() routi
33a60 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 ne with onoff==1
33a70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 .** to
33a80 74 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c turn extension l
33a90 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 oading on and ca
33aa0 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 ll it with onoff
33ab0 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 ==0 to turn.**
33ac0 20 20 20 20 20 20 20 20 69 74 20 62 61 63 6b 20 it back
33ad0 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a off again..**.**
33ae0 20 7b 48 31 32 36 32 32 7d 20 45 78 74 65 6e 73 {H12622} Extens
33af0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f ion loading is o
33b00 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a ff by default..*
33b10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
33b20 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
33b30 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 load_extension(s
33b40 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
33b50 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 onoff);../*.** C
33b60 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 API3REF: Automat
33b70 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45 ically Load An E
33b80 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34 xtensions {H1264
33b90 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S20500>.**.*
33ba0 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62 * This API can b
33bb0 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f e invoked at pro
33bc0 67 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20 gram startup in
33bd0 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65 order to registe
33be0 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 r.** one or more
33bf0 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b statically link
33c00 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 ed extensions th
33c10 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c at will be avail
33c20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e able.** to all n
33c30 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ew [database con
33c40 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d nections]. {END}
33c50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
33c60 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 ine stores a poi
33c70 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 nter to the exte
33c80 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 nsion in an arra
33c90 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74 y that is.** obt
33ca0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
33cb0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 te3_malloc()].
33cc0 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d If you run a mem
33cd0 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 6b 65 72 ory leak checker
33ce0 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 .** on your prog
33cf0 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f 72 ram and it repor
33d00 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75 73 ts a leak becaus
33d10 65 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 2c e of this array,
33d20 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 invoke.** [sqli
33d30 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 te3_reset_auto_e
33d40 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f xtension()] prio
33d50 72 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f r to shutdown to
33d60 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 free the memory
33d70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d ..**.** {H12641}
33d80 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
33d90 65 67 69 73 74 65 72 73 20 61 6e 20 65 78 74 65 egisters an exte
33da0 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e nsion entry poin
33db0 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 20 20 t that is.**
33dc0 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 automatica
33dd0 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e lly invoked when
33de0 65 76 65 72 20 61 20 6e 65 77 20 5b 64 61 74 61 ever a new [data
33df0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
33e00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 .** is
33e10 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 opened using [sq
33e20 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b lite3_open()], [
33e30 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
33e40 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ],.** o
33e50 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f r [sqlite3_open_
33e60 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 v2()]..**.** {H1
33e70 32 36 34 32 7d 20 44 75 70 6c 69 63 61 74 65 20 2642} Duplicate
33e80 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 20 64 extensions are d
33e90 65 74 65 63 74 65 64 20 73 6f 20 63 61 6c 6c 69 etected so calli
33ea0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a ng this routine.
33eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 ** mult
33ec0 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 20 iple times with
33ed0 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69 the same extensi
33ee0 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a on is harmless..
33ef0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54 **.** {H12643} T
33f00 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 his routine stor
33f10 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 es a pointer to
33f20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e the extension in
33f30 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 20 20 an array.**
33f40 20 20 20 20 20 20 74 68 61 74 20 69 73 20 6f 62 that is ob
33f50 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
33f60 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a ite3_malloc()]..
33f70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41 **.** {H12644} A
33f80 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 utomatic extensi
33f90 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 6f 73 73 ons apply across
33fa0 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f all threads..*/
33fb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
33fc0 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 sqlite3_auto_ext
33fd0 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 45 ension(void (*xE
33fe0 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 64 29 ntryPoint)(void)
33ff0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
34000 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61 EF: Reset Automa
34010 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f tic Extension Lo
34020 61 64 69 6e 67 20 7b 48 31 32 36 36 30 7d 20 3c ading {H12660} <
34030 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20500>.**.** Th
34040 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 is function disa
34050 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 bles all previou
34060 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 61 sly registered a
34070 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 74 65 utomatic.** exte
34080 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 49 nsions. {END} I
34090 74 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 t undoes the eff
340a0 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72 ect of all prior
340b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 .** [sqlite3_aut
340c0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 63 o_extension()] c
340d0 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 alls..**.** {H12
340e0 36 36 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 661} This functi
340f0 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 on disables all
34100 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 previously regis
34110 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 tered.**
34120 20 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 automatic exte
34130 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nsions..**.** {H
34140 31 32 36 36 32 7d 20 54 68 69 73 20 66 75 6e 63 12662} This func
34150 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 75 tion disables au
34160 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f tomatic extensio
34170 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 61 64 ns in all thread
34180 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
34190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
341a0 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 set_auto_extensi
341b0 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a on(void);../*.**
341c0 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 **** EXPERIMENTA
341d0 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 L - subject to c
341e0 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f hange without no
341f0 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tice ***********
34200 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e ***.**.** The in
34210 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 terface to the v
34220 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
34230 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e hanism is curren
34240 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a tly considered.*
34250 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 * to be experime
34260 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 ntal. The inter
34270 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 face might chang
34280 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c e in incompatibl
34290 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 e ways..** If th
342a0 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 is is a problem
342b0 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 for you, do not
342c0 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 use the interfac
342d0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a e at this time..
342e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 **.** When the v
342f0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
34300 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 hanism stabilize
34310 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 s, we will decla
34320 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 re the.** interf
34330 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f ace fixed, suppo
34340 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 rt it indefinite
34350 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 ly, and remove t
34360 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a his comment..*/.
34370 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 ./*.** Structure
34380 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69 s used by the vi
34390 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 rtual table inte
343a0 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 rface.*/.typedef
343b0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
343c0 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61 vtab sqlite3_vta
343d0 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 b;.typedef struc
343e0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
343f0 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64 info sqlite3_ind
34400 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66 ex_info;.typedef
34410 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
34420 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69 vtab_cursor sqli
34430 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b te3_vtab_cursor;
34440 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
34450 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 sqlite3_module s
34460 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a qlite3_module;..
34470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
34480 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62 Virtual Table Ob
34490 6a 65 63 74 20 7b 48 31 38 30 30 30 7d 20 3c 53 ject {H18000} <S
344a0 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 20400>.** KEYWOR
344b0 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 DS: sqlite3_modu
344c0 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c le {virtual tabl
344d0 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 58 50 e module}.** EXP
344e0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
344f0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 This structure,
34500 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 sometimes called
34510 20 61 20 61 20 22 76 69 72 74 75 61 6c 20 74 61 a a "virtual ta
34520 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a ble module", .**
34530 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6d 70 defines the imp
34540 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 lementation of a
34550 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 [virtual tables
34560 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 74 72 ]. .** This str
34570 75 63 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20 ucture consists
34580 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64 mostly of method
34590 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
345a0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 ..**.** A virtua
345b0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69 l table module i
345c0 73 20 63 72 65 61 74 65 64 20 62 79 20 66 69 6c s created by fil
345d0 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69 73 ling in a persis
345e0 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 tent.** instance
345f0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
34600 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67 20 61 re and passing a
34610 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 pointer to that
34620 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20 instance.** to
34630 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
34640 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71 module()] or [sq
34650 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
34660 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 54 68 ule_v2()]..** Th
34670 65 20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72 e registration r
34680 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 emains valid unt
34690 69 6c 20 69 74 20 69 73 20 72 65 70 6c 61 63 65 il it is replace
346a0 64 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 d by a different
346b0 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e .** module or un
346c0 74 69 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73 til the [databas
346d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c e connection] cl
346e0 6f 73 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 oses. The conte
346f0 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 nt.** of this st
34700 72 75 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74 ructure must not
34710 20 63 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74 change while it
34720 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 is registered w
34730 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62 ith.** any datab
34740 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ase connection..
34750 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
34760 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74 3_module {. int
34770 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 iVersion;. int
34780 20 28 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69 (*xCreate)(sqli
34790 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 te3*, void *pAux
347a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
347b0 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 int argc, const
347c0 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 char *const*arg
347d0 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 v,.
347e0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
347f0 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 *ppVTab, char**)
34800 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 ;. int (*xConne
34810 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f ct)(sqlite3*, vo
34820 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 id *pAux,.
34830 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 int arg
34840 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 c, const char *c
34850 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 onst*argv,.
34860 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
34870 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 3_vtab **ppVTab,
34880 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 char**);. int
34890 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28 73 71 (*xBestIndex)(sq
348a0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
348b0 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 b, sqlite3_index
348c0 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28 _info*);. int (
348d0 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71 *xDisconnect)(sq
348e0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
348f0 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 b);. int (*xDes
34900 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74 troy)(sqlite3_vt
34910 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
34920 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 t (*xOpen)(sqlit
34930 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 e3_vtab *pVTab,
34940 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
34950 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b sor **ppCursor);
34960 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 . int (*xClose)
34970 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 (sqlite3_vtab_cu
34980 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a rsor*);. int (*
34990 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33 xFilter)(sqlite3
349a0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69 _vtab_cursor*, i
349b0 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 nt idxNum, const
349c0 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 char *idxStr,.
349d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
349e0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 nt argc, sqlite3
349f0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a _value **argv);.
34a00 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 int (*xNext)(s
34a10 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
34a20 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45 or*);. int (*xE
34a30 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 of)(sqlite3_vtab
34a40 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 _cursor*);. int
34a50 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69 (*xColumn)(sqli
34a60 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
34a70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 , sqlite3_contex
34a80 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 t*, int);. int
34a90 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65 (*xRowid)(sqlite
34aa0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 3_vtab_cursor*,
34ab0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
34ac0 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a Rowid);. int (*
34ad0 78 55 70 64 61 74 65 29 28 73 71 6c 69 74 65 33 xUpdate)(sqlite3
34ae0 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 _vtab *, int, sq
34af0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 lite3_value **,
34b00 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 sqlite3_int64 *)
34b10 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e ;. int (*xBegin
34b20 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
34b30 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
34b40 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 xSync)(sqlite3_v
34b50 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 tab *pVTab);. i
34b60 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71 nt (*xCommit)(sq
34b70 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
34b80 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c b);. int (*xRol
34b90 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76 lback)(sqlite3_v
34ba0 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 tab *pVTab);. i
34bb0 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69 nt (*xFindFuncti
34bc0 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 on)(sqlite3_vtab
34bd0 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 *pVtab, int nAr
34be0 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a g, const char *z
34bf0 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Name,.
34c00 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 voi
34c10 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c d (**pxFunc)(sql
34c20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
34c30 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
34c40 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 *),.
34c50 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 void
34c60 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 **ppArg);. int
34c70 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74 (*xRename)(sqlit
34c80 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 e3_vtab *pVtab,
34c90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 const char *zNew
34ca0 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 );.};../*.** CAP
34cb0 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 I3REF: Virtual T
34cc0 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 49 6e able Indexing In
34cd0 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 31 30 formation {H1810
34ce0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
34cf0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
34d00 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 45 _index_info.** E
34d10 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
34d20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e * The sqlite3_in
34d30 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 dex_info structu
34d40 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 74 re and its subst
34d50 72 75 63 74 75 72 65 73 20 69 73 20 75 73 65 64 ructures is used
34d60 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f to.** pass info
34d70 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 rmation into and
34d80 20 72 65 63 65 69 76 65 20 74 68 65 20 72 65 70 receive the rep
34d90 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 42 65 ly from the [xBe
34da0 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68 stIndex].** meth
34db0 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c od of a [virtual
34dc0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 table module].
34dd0 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64 65 The fields unde
34de0 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 r **Inputs** are
34df0 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 the.** inputs t
34e00 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e 64 o xBestIndex and
34e10 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 are read-only.
34e20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73 65 xBestIndex inse
34e30 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c rts its.** resul
34e40 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 ts into the **Ou
34e50 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a tputs** fields..
34e60 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 **.** The aConst
34e70 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 raint[] array re
34e80 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75 cords WHERE clau
34e90 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f se constraints o
34ea0 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a f the form:.**.*
34eb0 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50 * <pre>column OP
34ec0 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a expr</pre>.**.*
34ed0 2a 20 77 68 65 72 65 20 4f 50 20 69 73 20 3d 2c * where OP is =,
34ee0 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 <, <=, &g
34ef0 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54 t;, or >=. T
34f00 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 he particular op
34f10 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f erator is.** sto
34f20 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 red in aConstrai
34f30 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e nt[].op. The in
34f40 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d dex of the colum
34f50 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a n is stored in.*
34f60 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e * aConstraint[].
34f70 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 iColumn. aConst
34f80 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 raint[].usable i
34f90 73 20 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a s TRUE if the.**
34fa0 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 expr on the rig
34fb0 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e ht-hand side can
34fc0 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 be evaluated (a
34fd0 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 nd thus the cons
34fe0 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 traint.** is usa
34ff0 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 ble) and false i
35000 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a f it cannot..**.
35010 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 ** The optimizer
35020 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 automatically i
35030 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20 nverts terms of
35040 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f the form "expr O
35050 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 P column".** and
35060 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d makes other sim
35070 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 plifications to
35080 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
35090 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 in an attempt t
350a0 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 o.** get as many
350b0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 WHERE clause te
350c0 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 rms into the for
350d0 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 m shown above as
350e0 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 possible..** Th
350f0 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 e aConstraint[]
35100 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 array only repor
35110 74 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 ts WHERE clause
35120 74 65 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 terms in the cor
35130 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 rect.** form tha
35140 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 t refer to the p
35150 61 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61 articular virtua
35160 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75 l table being qu
35170 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 eried..**.** Inf
35180 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
35190 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
351a0 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 se is stored in
351b0 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 aOrderBy[]..** E
351c0 61 63 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 ach term of aOrd
351d0 65 72 42 79 20 72 65 63 6f 72 64 73 20 61 20 63 erBy records a c
351e0 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 olumn of the ORD
351f0 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a ER BY clause..**
35200 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 49 6e .** The [xBestIn
35210 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 73 74 dex] method must
35220 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 69 6e fill aConstrain
35230 74 55 73 61 67 65 5b 5d 20 77 69 74 68 20 69 6e tUsage[] with in
35240 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f formation.** abo
35250 75 74 20 77 68 61 74 20 70 61 72 61 6d 65 74 65 ut what paramete
35260 72 73 20 74 6f 20 70 61 73 73 20 74 6f 20 78 46 rs to pass to xF
35270 69 6c 74 65 72 2e 20 20 49 66 20 61 72 67 76 49 ilter. If argvI
35280 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a 20 74 ndex>0 then.** t
35290 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 he right-hand si
352a0 64 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 de of the corres
352b0 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 72 61 ponding aConstra
352c0 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 61 74 int[] is evaluat
352d0 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f 6d 65 ed.** and become
352e0 73 20 74 68 65 20 61 72 67 76 49 6e 64 65 78 2d s the argvIndex-
352f0 74 68 20 65 6e 74 72 79 20 69 6e 20 61 72 67 76 th entry in argv
35300 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 61 69 6e . If aConstrain
35310 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a tUsage[].omit.**
35320 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
35330 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 he constraint is
35340 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 66 assumed to be f
35350 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 79 20 ully handled by
35360 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 the.** virtual t
35370 61 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 able and is not
35380 63 68 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79 checked again by
35390 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 SQLite..**.** T
353a0 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 69 64 he idxNum and id
353b0 78 50 74 72 20 76 61 6c 75 65 73 20 61 72 65 20 xPtr values are
353c0 72 65 63 6f 72 64 65 64 20 61 6e 64 20 70 61 73 recorded and pas
353d0 73 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 sed into the.**
353e0 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64 [xFilter] method
353f0 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 ..** [sqlite3_fr
35400 65 65 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f ee()] is used to
35410 20 66 72 65 65 20 69 64 78 50 74 72 20 69 66 20 free idxPtr if
35420 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a 2a 20 and only iff.**
35430 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50 74 72 needToFreeIdxPtr
35440 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 is true..**.**
35450 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 The orderByConsu
35460 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f med means that o
35470 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c utput from [xFil
35480 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c ter]/[xNext] wil
35490 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 l occur in.** th
354a0 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 e correct order
354b0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f to satisfy the O
354c0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 RDER BY clause s
354d0 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 o that no separa
354e0 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 te.** sorting st
354f0 65 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a ep is required..
35500 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 **.** The estima
35510 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 tedCost value is
35520 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 an estimate of
35530 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e the cost of doin
35540 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 g the.** particu
35550 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 lar lookup. A f
35560 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 ull scan of a ta
35570 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 ble with N entri
35580 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a es should have.*
35590 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 * a cost of N.
355a0 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 A binary search
355b0 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 of a table of N
355c0 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 entries should h
355d0 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 ave a.** cost of
355e0 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c approximately l
355f0 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 og(N)..*/.struct
35600 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 sqlite3_index_i
35610 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 nfo {. /* Input
35620 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 s */. int nCons
35630 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 traint;
35640 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
35650 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 ntries in aConst
35660 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63 raint */. struc
35670 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
35680 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20 constraint {.
35690 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
356a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
356b0 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61 olumn on left-ha
356c0 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74 nd side of const
356d0 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e raint */. un
356e0 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20 signed char op;
356f0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 /* Const
35700 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a raint operator *
35710 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 /. unsigned
35720 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20 char usable;
35730 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
35740 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 constraint is u
35750 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e sable */. in
35760 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20 t iTermOffset;
35770 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
35780 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65 internally - xBe
35790 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69 stIndex should i
357a0 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43 gnore */. } *aC
357b0 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 onstraint;
357c0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f /* Table o
357d0 66 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 f WHERE clause c
357e0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
357f0 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 int nOrderBy;
35800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
35810 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e mber of terms in
35820 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
35830 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 ause */. struct
35840 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f sqlite3_index_o
35850 72 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e rderby {. in
35860 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 t iColumn;
35870 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d /* Colum
35880 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 n number */.
35890 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 unsigned char d
358a0 65 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 esc; /* Tr
358b0 75 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 ue for DESC. Fa
358c0 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a lse for ASC. */.
358d0 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20 } *aOrderBy;
358e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
358f0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 The ORDER BY cla
35900 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70 use */. /* Outp
35910 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 uts */. struct
35920 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f sqlite3_index_co
35930 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b nstraint_usage {
35940 0a 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64 . int argvInd
35950 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ex; /*
35960 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 if >0, constrai
35970 6e 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72 nt is part of ar
35980 67 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f gv to xFilter */
35990 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 . unsigned ch
359a0 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a ar omit; /*
359b0 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 Do not code a t
359c0 65 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e est for this con
359d0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a straint */. } *
359e0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 aConstraintUsage
359f0 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 ;. int idxNum;
35a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
35a10 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f * Number used to
35a20 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e identify the in
35a30 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 dex */. char *i
35a40 64 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 dxStr;
35a50 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 /* String, p
35a60 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 ossibly obtained
35a70 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
35a80 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 lloc */. int ne
35a90 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 edToFreeIdxStr;
35aa0 20 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 /* Free idx
35ab0 53 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 Str using sqlite
35ac0 33 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 3_free() if true
35ad0 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 */. int orderB
35ae0 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 yConsumed;
35af0 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 /* True if outp
35b00 75 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 ut is already or
35b10 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c dered */. doubl
35b20 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b e estimatedCost;
35b30 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 /* Estimat
35b40 65 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 ed cost of using
35b50 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d this index */.}
35b60 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ;.#define SQLITE
35b70 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
35b80 54 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e T_EQ 2.#defin
35b90 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 e SQLITE_INDEX_C
35ba0 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20 ONSTRAINT_GT
35bb0 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 4.#define SQLITE
35bc0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
35bd0 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e T_LE 8.#defin
35be0 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 e SQLITE_INDEX_C
35bf0 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20 ONSTRAINT_LT
35c00 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 16.#define SQLIT
35c10 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
35c20 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66 NT_GE 32.#def
35c30 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
35c40 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 _CONSTRAINT_MATC
35c50 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 H 64../*.** CAPI
35c60 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 3REF: Register A
35c70 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 Virtual Table I
35c80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 mplementation {H
35c90 31 38 32 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18200} <S20400>.
35ca0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
35cb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
35cc0 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ne is used to re
35cd0 67 69 73 74 65 72 20 61 20 6e 65 77 20 5b 76 69 gister a new [vi
35ce0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
35cf0 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d 6f 64 le] name..** Mod
35d00 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62 ule names must b
35d10 65 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66 e registered bef
35d20 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 ore.** creating
35d30 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 74 a new [virtual t
35d40 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 65 20 able] using the
35d50 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 module, or befor
35d60 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 72 65 e using a.** pre
35d70 65 78 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61 existing [virtua
35d80 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65 l table] for the
35d90 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 module..**.** T
35da0 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 he module name i
35db0 73 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e 20 s registered on
35dc0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
35dd0 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 nnection] specif
35de0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 69 ied.** by the fi
35df0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20 rst parameter.
35e00 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
35e10 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 65 6e 20 module is given
35e20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e by the .** secon
35e30 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 d parameter. Th
35e40 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
35e50 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 r is a pointer t
35e60 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 o.** the impleme
35e70 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b ntation of the [
35e80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f virtual table mo
35e90 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 6f 75 dule]. The fou
35ea0 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 rth.** parameter
35eb0 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 is an arbitrary
35ec0 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 client data poi
35ed0 6e 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 nter that is pas
35ee0 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 sed through.** i
35ef0 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65 nto the [xCreate
35f00 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d ] and [xConnect]
35f10 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 methods of the
35f20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f virtual table mo
35f30 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e dule.** when a n
35f40 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ew virtual table
35f50 20 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65 is be being cre
35f60 61 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61 ated or reinitia
35f70 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 lized..**.** Thi
35f80 73 20 69 6e 74 65 72 66 61 63 65 20 68 61 73 20 s interface has
35f90 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 exactly the same
35fa0 20 65 66 66 65 63 74 20 61 73 20 63 61 6c 6c 69 effect as calli
35fb0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ng.** [sqlite3_c
35fc0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 reate_module_v2(
35fd0 29 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 )] with a NULL c
35fe0 6c 69 65 6e 74 20 64 61 74 61 20 64 65 73 74 72 lient data destr
35ff0 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 uctor..*/.SQLITE
36000 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
36010 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
36020 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
36030 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 le(. sqlite3 *d
36040 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
36050 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 /* SQLite conne
36060 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 ction to registe
36070 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f r module with */
36080 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
36090 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a Name, /*
360a0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 Name of the mod
360b0 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ule */. const s
360c0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
360d0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 , /* Methods f
360e0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f or the module */
360f0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 . void *pClient
36100 44 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a Data /*
36110 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 Client data for
36120 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 xCreate/xConnec
36130 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 t */.);../*.** C
36140 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 API3REF: Registe
36150 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c r A Virtual Tabl
36160 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e Implementation
36170 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 34 30 {H18210} <S2040
36180 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
36190 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f AL.**.** This ro
361a0 75 74 69 6e 65 20 69 73 20 69 64 65 6e 74 69 63 utine is identic
361b0 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 al to the [sqlit
361c0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
361d0 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 65 ()] method,.** e
361e0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 68 61 xcept that it ha
361f0 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d s an extra param
36200 65 74 65 72 20 74 6f 20 73 70 65 63 69 66 79 20 eter to specify
36210 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 .** a destructor
36220 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 function for th
36230 65 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f e client data po
36240 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 20 77 inter. SQLite w
36250 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 ill.** invoke th
36260 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e e destructor fun
36270 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20 ction (if it is
36280 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53 not NULL) when S
36290 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 QLite.** no long
362a0 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 6c er needs the pCl
362b0 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 72 ientData pointer
362c0 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 . .*/.SQLITE_AP
362d0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
362e0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
362f0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 3_create_module_
36300 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
36310 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
36320 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 /* SQLite conne
36330 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 ction to registe
36340 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f r module with */
36350 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
36360 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a Name, /*
36370 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 Name of the mod
36380 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ule */. const s
36390 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
363a0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 , /* Methods f
363b0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f or the module */
363c0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 . void *pClient
363d0 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 2f 2a Data, /*
363e0 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 Client data for
363f0 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 xCreate/xConnec
36400 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 t */. void(*xDe
36410 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 stroy)(void*)
36420 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 /* Module dest
36430 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 ructor function
36440 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
36450 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 I3REF: Virtual T
36460 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 able Instance Ob
36470 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 ject {H18010} <S
36480 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 20400>.** KEYWOR
36490 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 DS: sqlite3_vtab
364a0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
364b0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69 .**.** Every [vi
364c0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
364d0 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 le] implementati
364e0 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 on uses a subcla
364f0 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ss.** of the fol
36500 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
36510 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 to describe a p
36520 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e articular instan
36530 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69 ce.** of the [vi
36540 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45 rtual table]. E
36550 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c ach subclass wil
36560 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64 l.** be tailored
36570 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 to the specific
36580 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f needs of the mo
36590 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 dule implementat
365a0 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70 ion..** The purp
365b0 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65 ose of this supe
365c0 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66 rclass is to def
365d0 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c ine certain fiel
365e0 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 ds that are.** c
365f0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64 ommon to all mod
36600 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
36610 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 ons..**.** Virtu
36620 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64 al tables method
36630 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72 s can set an err
36640 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73 or message by as
36650 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72 signing a.** str
36660 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ing obtained fro
36670 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e m [sqlite3_mprin
36680 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67 tf()] to zErrMsg
36690 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68 . The method sh
366a0 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72 ould.** take car
366b0 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72 e that any prior
366c0 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64 string is freed
366d0 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 by a call to [s
366e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a qlite3_free()].*
366f0 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67 * prior to assig
36700 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e ning a new strin
36710 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41 g to zErrMsg. A
36720 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d fter the error m
36730 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c essage.** is del
36740 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65 ivered up to the
36750 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74 client applicat
36760 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20 ion, the string
36770 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
36780 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62 cally.** freed b
36790 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 y sqlite3_free()
367a0 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67 and the zErrMsg
367b0 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a field will be z
367c0 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 eroed..*/.struct
367d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a sqlite3_vtab {.
367e0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
367f0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
36800 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 /* The module
36810 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c for this virtual
36820 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
36830 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
36840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
36850 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 20 2a 2f O LONGER USED */
36860 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
36870 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
36880 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 /* Error mess
36890 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 age from sqlite3
368a0 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20 _mprintf() */.
368b0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
368c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
368d0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
368e0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
368f0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
36900 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
36910 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 73 6f tual Table Curso
36920 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 30 32 r Object {H1802
36930 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
36940 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
36950 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 76 69 _vtab_cursor {vi
36960 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 rtual table curs
36970 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e or}.** EXPERIMEN
36980 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 TAL.**.** Every
36990 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
369a0 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 odule] implement
369b0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 ation uses a sub
369c0 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 class of the.**
369d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
369e0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 ure to describe
369f0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 cursors that poi
36a00 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b nt into the.** [
36a10 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 61 virtual table] a
36a20 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74 nd are used.** t
36a30 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 o loop through t
36a40 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
36a50 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63 . Cursors are c
36a60 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 reated using the
36a70 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64 .** [sqlite3_mod
36a80 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f 70 65 ule.xOpen | xOpe
36a90 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 n] method of the
36aa0 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 65 20 module and are
36ab0 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 79 20 destroyed.** by
36ac0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64 the [sqlite3_mod
36ad0 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 43 6c ule.xClose | xCl
36ae0 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 43 75 ose] method. Cu
36af0 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 0a 2a ssors are used.*
36b00 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c 74 65 * by the [xFilte
36b10 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b 78 45 r], [xNext], [xE
36b20 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d 2c 20 of], [xColumn],
36b30 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d 65 74 and [xRowid] met
36b40 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 6d hods.** of the m
36b50 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64 odule. Each mod
36b60 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
36b70 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a on will define.*
36b80 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
36b90 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 a cursor struct
36ba0 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20 ure to suit its
36bb0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a own needs..**.**
36bc0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73 This superclass
36bd0 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72 exists in order
36be0 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64 to define field
36bf0 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 s of the cursor
36c00 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d that.** are comm
36c10 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d on to all implem
36c20 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 entations..*/.st
36c30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 ruct sqlite3_vta
36c40 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c b_cursor {. sql
36c50 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
36c60 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 ; /* Virtua
36c70 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 l table of this
36c80 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 cursor */. /* V
36c90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 irtual table imp
36ca0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c lementations wil
36cb0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 l typically add
36cc0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 additional field
36cd0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 s */.};../*.** C
36ce0 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 API3REF: Declare
36cf0 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 The Schema Of A
36d00 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b Virtual Table {
36d10 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e H18280} <S20400>
36d20 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
36d30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 72 65 .**.** The [xCre
36d40 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 ate] and [xConne
36d50 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 61 ct] methods of a
36d60 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 .** [virtual tab
36d70 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c 6c 20 le module] call
36d80 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a this interface.*
36d90 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65 * to declare the
36da0 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e 61 6d format (the nam
36db0 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 65 73 es and datatypes
36dc0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 29 of the columns)
36dd0 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 of.** the virtu
36de0 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 20 69 al tables they i
36df0 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c mplement..*/.SQL
36e00 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
36e10 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
36e20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f sqlite3_declare_
36e30 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 vtab(sqlite3*, c
36e40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29 onst char *zSQL)
36e50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
36e60 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 F: Overload A Fu
36e70 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 nction For A Vir
36e80 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33 tual Table {H183
36e90 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 00} <S20400>.**
36ea0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
36eb0 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 ** Virtual table
36ec0 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c s can provide al
36ed0 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d ternative implem
36ee0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e entations of fun
36ef0 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 ctions.** using
36f00 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69 the [xFindFuncti
36f10 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 on] method of th
36f20 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
36f30 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42 module]. .** B
36f40 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f ut global versio
36f50 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63 ns of those func
36f60 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78 tions.** must ex
36f70 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ist in order to
36f80 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a 2a be overloaded..*
36f90 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 *.** This API ma
36fa0 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f 62 61 kes sure a globa
36fb0 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 20 66 l version of a f
36fc0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 70 unction with a p
36fd0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e 61 6d articular.** nam
36fe0 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 e and number of
36ff0 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 73 74 parameters exist
37000 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 66 s. If no such f
37010 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 0a 2a unction exists.*
37020 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 41 50 * before this AP
37030 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 20 6e I is called, a n
37040 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 ew function is c
37050 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 6d 70 reated. The imp
37060 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f lementation.** o
37070 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69 f the new functi
37080 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 65 73 on always causes
37090 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f an exception to
370a0 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0a be thrown. So.
370b0 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 ** the new funct
370c0 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20 ion is not good
370d0 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20 for anything by
370e0 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c itself. Its onl
370f0 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20 y.** purpose is
37100 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c to be a placehol
37110 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 der function tha
37120 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61 t can be overloa
37130 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 69 72 ded.** by a [vir
37140 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a tual table]..*/.
37150 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
37160 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
37170 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c nt sqlite3_overl
37180 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c oad_function(sql
37190 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
371a0 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e r *zFuncName, in
371b0 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t nArg);../*.**
371c0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
371d0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
371e0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 le mechanism def
371f0 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b ined above (back
37200 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d up.** to a comm
37210 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 ent remarkably s
37220 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f imilar to this o
37230 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ne) is currently
37240 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 considered.** t
37250 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61 o be experimenta
37260 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 l. The interfac
37270 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 e might change i
37280 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 n incompatible w
37290 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 ays..** If this
372a0 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 is a problem for
372b0 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 you, do not use
372c0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61 the interface a
372d0 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a t this time..**.
372e0 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74 ** When the virt
372f0 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e ual-table mechan
37300 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 ism stabilizes,
37310 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 we will declare
37320 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 the.** interface
37330 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20 fixed, support
37340 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c it indefinitely,
37350 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73 and remove this
37360 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a comment..**.***
37370 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c *** EXPERIMENTAL
37380 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 - subject to ch
37390 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 ange without not
373a0 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ice ************
373b0 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 **.*/../*.** CAP
373c0 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 I3REF: A Handle
373d0 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 To An Open BLOB
373e0 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30 {H17800} <S30230
373f0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
37400 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c BLOB handle} {BL
37410 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a OB handles}.**.*
37420 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
37430 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 this object rep
37440 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
37450 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a BLOB on which.**
37460 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
37470 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 pen | incrementa
37480 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 l BLOB I/O] can
37490 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a be performed..**
374a0 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 Objects of this
374b0 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65 type are create
374c0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c d by [sqlite3_bl
374d0 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e ob_open()].** an
374e0 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b d destroyed by [
374f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
37500 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 se()]..** The [s
37510 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
37520 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
37530 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 _blob_write()] i
37540 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e nterfaces.** can
37550 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 be used to read
37560 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 or write small
37570 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 subsections of t
37580 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 he BLOB..** The
37590 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
375a0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
375b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a returns the siz
375c0 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e e of the BLOB in
375d0 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 bytes..*/.typed
375e0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
375f0 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 3_blob sqlite3_b
37600 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lob;../*.** CAPI
37610 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 3REF: Open A BLO
37620 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 B For Incrementa
37630 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c l I/O {H17810} <
37640 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30230>.**.** Th
37650 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70 is interfaces op
37660 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 ens a [BLOB hand
37670 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 le | handle] to
37680 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 the BLOB located
37690 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c .** in row iRow,
376a0 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c column zColumn,
376b0 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e table zTable in
376c0 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a database zDb;.*
376d0 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * in other words
376e0 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 , the same BLOB
376f0 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65 that would be se
37700 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a lected by:.**.**
37710 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 <pre>.** SE
37720 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f LECT zColumn FRO
37730 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 M zDb.zTable WHE
37740 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f RE [rowid] = iRo
37750 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e w;.** </pre> {EN
37760 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 D}.**.** If the
37770 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
37780 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
37790 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 n the BLOB is op
377a0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a ened for read.**
377b0 20 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 and write acces
377c0 73 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f s. If it is zero
377d0 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 , the BLOB is op
377e0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63 ened for read ac
377f0 63 65 73 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e cess..** It is n
37800 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f ot possible to o
37810 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 pen a column tha
37820 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 t is part of an
37830 69 6e 64 65 78 20 6f 72 20 70 72 69 6d 61 72 79 index or primary
37840 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 20 77 72 69 .** key for wri
37850 74 69 6e 67 2e 20 5e 49 66 20 5b 66 6f 72 65 69 ting. ^If [forei
37860 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
37870 74 73 5d 20 61 72 65 20 65 6e 61 62 6c 65 64 2c ts] are enabled,
37880 20 69 74 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 70 it is .** not p
37890 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 ossible to open
378a0 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 a column that is
378b0 20 70 61 72 74 20 6f 66 20 61 20 5b 63 68 69 6c part of a [chil
378c0 64 20 6b 65 79 5d 20 66 6f 72 20 77 72 69 74 69 d key] for writi
378d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ng..**.** Note t
378e0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
378f0 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65 name is not the
37900 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 63 filename that c
37910 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 64 ontains.** the d
37920 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74 68 atabase but rath
37930 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 er the symbolic
37940 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
37950 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 base that.** is
37960 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 68 assigned when th
37970 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f e database is co
37980 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b 41 nnected using [A
37990 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74 TTACH]..** For t
379a0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
379b0 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 62 file, the datab
379c0 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 69 ase name is "mai
379d0 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 20 n"..** For TEMP
379e0 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 61 tables, the data
379f0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 65 base name is "te
37a00 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 mp"..**.** On su
37a10 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f ccess, [SQLITE_O
37a20 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 K] is returned a
37a30 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 nd the new [BLOB
37a40 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 handle] is writ
37a50 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f ten.** to *ppBlo
37a60 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 b. Otherwise an
37a70 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 [error code] is
37a80 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 returned and *pp
37a90 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a 20 74 Blob is set.** t
37aa0 6f 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e o be a null poin
37ab0 74 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e ter..** This fun
37ac0 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 5b ction sets the [
37ad0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
37ae0 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 65 20 ion] error code
37af0 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61 and message.** a
37b00 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 ccessible via [s
37b10 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
37b20 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 ] and [sqlite3_e
37b30 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 72 65 6c rrmsg()] and rel
37b40 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ated.** function
37b50 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 s. Note that th
37b60 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 69 61 62 e *ppBlob variab
37b70 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 le is always ini
37b80 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 0a 2a 2a tialized in a.**
37b90 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20 way that makes
37ba0 69 74 20 73 61 66 65 20 74 6f 20 69 6e 76 6f 6b it safe to invok
37bb0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
37bc0 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a 70 70 42 close()] on *ppB
37bd0 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 lob.** regardles
37be0 73 20 6f 66 20 74 68 65 20 73 75 63 63 65 73 73 s of the success
37bf0 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66 20 74 or failure of t
37c00 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a his routine..**.
37c10 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 20 74 68 ** If the row th
37c20 61 74 20 61 20 42 4c 4f 42 20 68 61 6e 64 6c 65 at a BLOB handle
37c30 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 6d 6f points to is mo
37c40 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 dified by an.**
37c50 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 4c 45 54 [UPDATE], [DELET
37c60 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e 20 43 4f E], or by [ON CO
37c70 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d 65 66 66 NFLICT] side-eff
37c80 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ects.** then the
37c90 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73 20 BLOB handle is
37ca0 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 69 72 marked as "expir
37cb0 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 ed"..** This is
37cc0 74 72 75 65 20 69 66 20 61 6e 79 20 63 6f 6c 75 true if any colu
37cd0 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 20 69 73 mn of the row is
37ce0 20 63 68 61 6e 67 65 64 2c 20 65 76 65 6e 20 61 changed, even a
37cf0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 68 65 72 column.** other
37d00 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 74 68 than the one th
37d10 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73 e BLOB handle is
37d20 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 43 61 6c open on..** Cal
37d30 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ls to [sqlite3_b
37d40 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64 20 lob_read()] and
37d50 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 [sqlite3_blob_wr
37d60 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 61 20 ite()] for.** a
37d70 65 78 70 69 72 65 64 20 42 4c 4f 42 20 68 61 6e expired BLOB han
37d80 64 6c 65 20 66 61 69 6c 20 77 69 74 68 20 61 6e dle fail with an
37d90 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 return code of
37da0 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a [SQLITE_ABORT]..
37db0 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 69 74 74 ** Changes writt
37dc0 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20 70 en into a BLOB p
37dd0 72 69 6f 72 20 74 6f 20 74 68 65 20 42 4c 4f 42 rior to the BLOB
37de0 20 65 78 70 69 72 69 6e 67 20 61 72 65 20 6e 6f expiring are no
37df0 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62 79 t.** rollback by
37e00 20 74 68 65 20 65 78 70 69 72 61 74 69 6f 6e 20 the expiration
37e10 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 20 53 75 of the BLOB. Su
37e20 63 68 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 ch changes will
37e30 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f eventually.** co
37e40 6d 6d 69 74 20 69 66 20 74 68 65 20 74 72 61 6e mmit if the tran
37e50 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 saction continue
37e60 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e s to completion.
37e70 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 5b .**.** Use the [
37e80 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
37e90 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 es()] interface
37ea0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
37eb0 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20 size of.** the
37ec0 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20 54 68 opened blob. Th
37ed0 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62 e size of a blob
37ee0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e may not be chan
37ef0 67 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 69 ged by this.** i
37f00 6e 74 65 72 66 61 63 65 2e 20 20 55 73 65 20 74 nterface. Use t
37f10 68 65 20 5b 55 50 44 41 54 45 5d 20 53 51 4c 20 he [UPDATE] SQL
37f20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 61 6e 67 command to chang
37f30 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a e the size of a.
37f40 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 ** blob..**.** T
37f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 he [sqlite3_bind
37f60 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 61 6e 64 _zeroblob()] and
37f70 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
37f80 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 69 6e 74 _zeroblob()] int
37f90 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e 64 20 74 erfaces.** and t
37fa0 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b 7a 65 72 he built-in [zer
37fb0 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 6e 63 74 oblob] SQL funct
37fc0 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c ion can be used,
37fd0 20 69 66 20 64 65 73 69 72 65 64 2c 0a 2a 2a 20 if desired,.**
37fe0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 6d 70 to create an emp
37ff0 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 ty, zero-filled
38000 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 20 74 6f blob in which to
38010 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 75 read or write u
38020 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 6e 74 sing.** this int
38030 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f erface..**.** To
38040 20 61 76 6f 69 64 20 61 20 72 65 73 6f 75 72 63 avoid a resourc
38050 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 20 6f 70 e leak, every op
38060 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d en [BLOB handle]
38070 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c should eventual
38080 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 ly.** be release
38090 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b d by a call to [
380a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
380b0 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 se()]..**.** Req
380c0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
380d0 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 5d 20 17813] [H17814]
380e0 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 31 39 [H17816] [H17819
380f0 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 37 38 ] [H17821] [H178
38100 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 24].*/.SQLITE_AP
38110 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
38120 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 ob_open(. sqlit
38130 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 e3*,. const cha
38140 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 r *zDb,. const
38150 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 char *zTable,.
38160 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c const char *zCol
38170 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 umn,. sqlite3_i
38180 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 nt64 iRow,. int
38190 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 flags,. sqlite
381a0 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 3_blob **ppBlob.
381b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
381c0 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 EF: Close A BLOB
381d0 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d Handle {H17830}
381e0 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
381f0 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b Closes an open [
38200 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a BLOB handle]..**
38210 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c .** Closing a BL
38220 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 OB shall cause t
38230 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
38240 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 action to commit
38250 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 .** if there are
38260 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c no other BLOBs,
38270 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70 no pending prep
38280 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c ared statements,
38290 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 and the.** data
382a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
382b0 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 is in [autocommi
382c0 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 t mode]..** If a
382d0 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d ny writes were m
382e0 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c ade to the BLOB,
382f0 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68 they might be h
38300 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 eld in cache.**
38310 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 until the close
38320 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65 operation if the
38330 79 20 77 69 6c 6c 20 66 69 74 2e 0a 2a 2a 0a 2a y will fit..**.*
38340 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65 20 42 4c * Closing the BL
38350 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63 65 73 20 OB often forces
38360 74 68 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 6f the changes.** o
38370 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 73 ut to disk and s
38380 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20 65 72 72 o if any I/O err
38390 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 79 20 ors occur, they
383a0 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f 63 63 75 will likely occu
383b0 72 0a 2a 2a 20 61 74 20 74 68 65 20 74 69 6d 65 r.** at the time
383c0 20 77 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 when the BLOB i
383d0 73 20 63 6c 6f 73 65 64 2e 20 20 41 6e 79 20 65 s closed. Any e
383e0 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 rrors that occur
383f0 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 during.** closi
38400 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 ng are reported
38410 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 as a non-zero re
38420 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a turn value..**.*
38430 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c * The BLOB is cl
38440 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e osed uncondition
38450 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74 ally. Even if t
38460 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
38470 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 rns.** an error
38480 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 code, the BLOB i
38490 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a s still closed..
384a0 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68 **.** Calling th
384b0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
384c0 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 28 a null pointer (
384d0 77 68 69 63 68 20 61 73 20 77 6f 75 6c 64 20 62 which as would b
384e0 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 e returned.** by
384f0 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 74 6f 20 failed call to
38500 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 [sqlite3_blob_op
38510 65 6e 28 29 5d 29 20 69 73 20 61 20 68 61 72 6d en()]) is a harm
38520 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a less no-op..**.*
38530 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
38540 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b 48 31 37 ** [H17833] [H17
38550 38 33 36 5d 20 5b 48 31 37 38 33 39 5d 0a 2a 2f 836] [H17839].*/
38560 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
38570 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
38580 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 se(sqlite3_blob
38590 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
385a0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20 REF: Return The
385b0 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20 Size Of An Open
385c0 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c 53 BLOB {H17840} <S
385d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 30230>.**.** Ret
385e0 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e urns the size in
385f0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c bytes of the BL
38600 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 OB accessible vi
38610 61 20 74 68 65 20 0a 2a 2a 20 73 75 63 63 65 73 a the .** succes
38620 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 5b 42 sfully opened [B
38630 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 69 LOB handle] in i
38640 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 ts only argument
38650 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 63 72 65 6d . The.** increm
38660 65 6e 74 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72 ental blob I/O r
38670 6f 75 74 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79 outines can only
38680 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 read or overwri
38690 74 69 6e 67 20 65 78 69 73 74 69 6e 67 0a 2a 2a ting existing.**
386a0 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74 blob content; t
386b0 68 65 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 hey cannot chang
386c0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 e the size of a
386d0 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 blob..**.** This
386e0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f routine only wo
386f0 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 rks on a [BLOB h
38700 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 andle] which has
38710 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a been created.**
38720 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 by a prior succ
38730 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b essful call to [
38740 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
38750 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 n()] and which h
38760 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 as not.** been c
38770 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 losed by [sqlite
38780 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 3_blob_close()].
38790 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 Passing any ot
387a0 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a her pointer in.*
387b0 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e * to this routin
387c0 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 e results in und
387d0 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 efined and proba
387e0 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 bly undesirable
387f0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
38800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
38810 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c [H17843].*/.SQL
38820 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
38830 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 te3_blob_bytes(s
38840 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a qlite3_blob *);.
38850 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
38860 20 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 Read Data From
38870 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 A BLOB Increment
38880 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53 ally {H17850} <S
38890 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 30230>.**.** Thi
388a0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
388b0 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 ed to read data
388c0 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c from an open [BL
388d0 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 OB handle] into
388e0 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 a.** caller-supp
388f0 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 lied buffer. N b
38900 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 ytes of data are
38910 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 copied into buf
38920 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 fer Z.** from th
38930 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 e open BLOB, sta
38940 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 rting at offset
38950 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 iOffset..**.** I
38960 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 f offset iOffset
38970 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 is less than N
38980 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 bytes from the e
38990 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a nd of the BLOB,.
389a0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ** [SQLITE_ERROR
389b0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
389c0 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61 d no data is rea
389d0 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66 d. If N or iOff
389e0 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 set is.** less t
389f0 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 han zero, [SQLIT
38a00 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 E_ERROR] is retu
38a10 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
38a20 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20 54 68 65 is read..** The
38a30 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f size of the blo
38a40 62 20 28 61 6e 64 20 68 65 6e 63 65 20 74 68 65 b (and hence the
38a50 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f maximum value o
38a60 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a 2a 2a 20 f N+iOffset).**
38a70 63 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 can be determine
38a80 64 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c d using the [sql
38a90 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 ite3_blob_bytes(
38aa0 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a )] interface..**
38ab0 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 .** An attempt t
38ac0 6f 20 72 65 61 64 20 66 72 6f 6d 20 61 6e 20 65 o read from an e
38ad0 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e xpired [BLOB han
38ae0 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20 dle] fails with
38af0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 an.** error code
38b00 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 of [SQLITE_ABOR
38b10 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 T]..**.** On suc
38b20 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 cess, SQLITE_OK
38b30 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
38b40 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65 Otherwise, an [e
38b50 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e rror code] or an
38b60 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 [extended error
38b70 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
38b80 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
38b90 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
38ba0 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 6e s on a [BLOB han
38bb0 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 20 62 dle] which has b
38bc0 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 62 een created.** b
38bd0 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 65 73 y a prior succes
38be0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 sful call to [sq
38bf0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
38c00 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 61 73 )] and which has
38c10 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f not.** been clo
38c20 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f sed by [sqlite3_
38c30 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 20 blob_close()].
38c40 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 Passing any othe
38c50 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a 20 r pointer in.**
38c60 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
38c70 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 results in undef
38c80 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c ined and probabl
38c90 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62 65 y undesirable be
38ca0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 65 havior..**.** Se
38cb0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
38cc0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 2e 0a _blob_write()]..
38cd0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
38ce0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 5d 20 ts:.** [H17853]
38cf0 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 38 35 39 [H17856] [H17859
38d00 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 31 37 38 ] [H17862] [H178
38d10 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 5b 48 31 63] [H17865] [H1
38d20 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 7868].*/.SQLITE_
38d30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
38d40 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 blob_read(sqlite
38d50 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 3_blob *, void *
38d60 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f Z, int N, int iO
38d70 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ffset);../*.** C
38d80 41 50 49 33 52 45 46 3a 20 57 72 69 74 65 20 44 API3REF: Write D
38d90 61 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 ata Into A BLOB
38da0 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 Incrementally {H
38db0 31 37 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a 17870} <S30230>.
38dc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
38dd0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77 ion is used to w
38de0 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61 rite data into a
38df0 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e n open [BLOB han
38e00 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 dle] from a.** c
38e10 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 aller-supplied b
38e20 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f uffer. N bytes o
38e30 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 f data are copie
38e40 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 d from the buffe
38e50 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 r Z.** into the
38e60 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 open BLOB, start
38e70 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f ing at offset iO
38e80 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ffset..**.** If
38e90 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 the [BLOB handle
38ea0 5d 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 ] passed as the
38eb0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 first argument w
38ec0 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f as not opened fo
38ed0 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68 r.** writing (th
38ee0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
38ef0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c r to [sqlite3_bl
38f00 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a ob_open()] was z
38f10 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75 ero),.** this fu
38f20 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b nction returns [
38f30 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
38f40 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
38f50 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d ction may only m
38f60 6f 64 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e odify the conten
38f70 74 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20 ts of the BLOB;
38f80 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 it is.** not pos
38f90 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73 sible to increas
38fa0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 e the size of a
38fb0 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20 BLOB using this
38fc0 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65 API..** If offse
38fd0 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73 t iOffset is les
38fe0 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66 s than N bytes f
38ff0 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 rom the end of t
39000 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c he BLOB,.** [SQL
39010 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 ITE_ERROR] is re
39020 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 turned and no da
39030 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 ta is written.
39040 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20 If N is.** less
39050 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54 than zero [SQLIT
39060 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 E_ERROR] is retu
39070 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
39080 20 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 is written..**
39090 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 The size of the
390a0 42 4c 4f 42 20 28 61 6e 64 20 68 65 6e 63 65 20 BLOB (and hence
390b0 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 the maximum valu
390c0 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a e of N+iOffset).
390d0 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d ** can be determ
390e0 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b ined using the [
390f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
39100 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e es()] interface.
39110 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 .**.** An attemp
39120 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e t to write to an
39130 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 expired [BLOB h
39140 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 andle] fails wit
39150 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f h an.** error co
39160 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 de of [SQLITE_AB
39170 4f 52 54 5d 2e 20 20 57 72 69 74 65 73 20 74 6f ORT]. Writes to
39180 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 20 6f the BLOB that o
39190 63 63 75 72 72 65 64 0a 2a 2a 20 62 65 66 6f 72 ccurred.** befor
391a0 65 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 e the [BLOB hand
391b0 6c 65 5d 20 65 78 70 69 72 65 64 20 61 72 65 20 le] expired are
391c0 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 not rolled back
391d0 62 79 20 74 68 65 0a 2a 2a 20 65 78 70 69 72 61 by the.** expira
391e0 74 69 6f 6e 20 6f 66 20 74 68 65 20 68 61 6e 64 tion of the hand
391f0 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f le, though of co
39200 75 72 73 65 20 74 68 6f 73 65 20 63 68 61 6e 67 urse those chang
39210 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65 es might.** have
39220 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 been overwritte
39230 6e 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 n by the stateme
39240 6e 74 20 74 68 61 74 20 65 78 70 69 72 65 64 20 nt that expired
39250 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 0a the BLOB handle.
39260 2a 2a 20 6f 72 20 62 79 20 6f 74 68 65 72 20 69 ** or by other i
39270 6e 64 65 70 65 6e 64 65 6e 74 20 73 74 61 74 65 ndependent state
39280 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 ments..**.** On
39290 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f success, SQLITE_
392a0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
392b0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e ** Otherwise, an
392c0 20 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f [error code] o
392d0 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 r an [extended e
392e0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
392f0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
39300 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
39310 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 works on a [BLOB
39320 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 handle] which h
39330 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a as been created.
39340 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 ** by a prior su
39350 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
39360 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
39370 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 pen()] and which
39380 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e has not.** been
39390 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 closed by [sqli
393a0 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 te3_blob_close()
393b0 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 ]. Passing any
393c0 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e other pointer in
393d0 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 .** to this rout
393e0 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ine results in u
393f0 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f ndefined and pro
39400 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c bably undesirabl
39410 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a e behavior..**.*
39420 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
39430 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 ite3_blob_read()
39440 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
39450 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37 ments:.** [H1787
39460 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b 48 31 37 3] [H17874] [H17
39470 38 37 35 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48 875] [H17876] [H
39480 31 37 38 37 37 5d 20 5b 48 31 37 38 37 39 5d 20 17877] [H17879]
39490 5b 48 31 37 38 38 32 5d 20 5b 48 31 37 38 38 35 [H17882] [H17885
394a0 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f ].** [H17888].*/
394b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
394c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 sqlite3_blob_wri
394d0 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 te(sqlite3_blob
394e0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a *, const void *z
394f0 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 , int n, int iOf
39500 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 fset);../*.** CA
39510 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 PI3REF: Virtual
39520 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 File System Obje
39530 63 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 cts {H11200} <S2
39540 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 0100>.**.** A vi
39550 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d rtual filesystem
39560 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 (VFS) is an [sq
39570 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
39580 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 t.** that SQLite
39590 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 uses to interac
395a0 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e t.** with the un
395b0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
395c0 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 ng system. Most
395d0 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 SQLite builds c
395e0 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 ome with a.** si
395f0 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 ngle default VFS
39600 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
39610 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 iate for the hos
39620 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e t computer..** N
39630 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 ew VFSes can be
39640 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 registered and e
39650 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 xisting VFSes ca
39660 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 n be unregistere
39670 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 d..** The follow
39680 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 ing interfaces a
39690 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a re provided..**.
396a0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 ** The sqlite3_v
396b0 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 fs_find() interf
396c0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
396d0 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 inter to a VFS g
396e0 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a iven its name..*
396f0 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 * Names are case
39700 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e sensitive..** N
39710 61 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 ames are zero-te
39720 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 rminated UTF-8 s
39730 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 trings..** If th
39740 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c ere is no match,
39750 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
39760 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
39770 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e If zVfsName is N
39780 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 ULL then the def
39790 61 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 ault VFS is retu
397a0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 rned..**.** New
397b0 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 VFSes are regist
397c0 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 ered with sqlite
397d0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 3_vfs_register()
397e0 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 ..** Each new VF
397f0 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 S becomes the de
39800 66 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 fault VFS if the
39810 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 makeDflt flag i
39820 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 s set..** The sa
39830 6d 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 me VFS can be re
39840 67 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c gistered multipl
39850 65 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 e times without
39860 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 injury..** To ma
39870 6b 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 ke an existing V
39880 46 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 FS into the defa
39890 75 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 ult VFS, registe
398a0 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 r it again.** wi
398b0 74 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 th the makeDflt
398c0 66 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 flag set. If tw
398d0 6f 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 o different VFSe
398e0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 s with the.** sa
398f0 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 me name are regi
39900 73 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 stered, the beha
39910 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
39920 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 d. If a.** VFS
39930 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 is registered wi
39940 74 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 th a name that i
39950 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 s NULL or an emp
39960 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 ty string,.** th
39970 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 en the behavior
39980 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a is undefined..**
39990 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 .** Unregister a
399a0 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 VFS with the sq
399b0 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
399c0 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 ster() interface
399d0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 ..** If the defa
399e0 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 ult VFS is unreg
399f0 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 istered, another
39a00 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 VFS is chosen a
39a10 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 s.** the default
39a20 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f . The choice fo
39a30 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 r the new VFS is
39a40 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a arbitrary..**.*
39a50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
39a60 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b 48 31 31 ** [H11203] [H11
39a70 32 30 36 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48 206] [H11209] [H
39a80 31 31 32 31 32 5d 20 5b 48 31 31 32 31 35 5d 20 11212] [H11215]
39a90 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49 [H11218].*/.SQLI
39aa0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 TE_API sqlite3_v
39ab0 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f fs *sqlite3_vfs_
39ac0 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 find(const char
39ad0 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 *zVfsName);.SQLI
39ae0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
39af0 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 e3_vfs_register(
39b00 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
39b10 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c t makeDflt);.SQL
39b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
39b30 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 te3_vfs_unregist
39b40 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 er(sqlite3_vfs*)
39b50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
39b60 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 37 30 F: Mutexes {H170
39b70 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 00} <S20000>.**.
39b80 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f ** The SQLite co
39b90 72 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f re uses these ro
39ba0 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61 utines for threa
39bb0 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 d.** synchroniza
39bc0 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 tion. Though the
39bd0 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 y are intended f
39be0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
39bf0 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f se by SQLite, co
39c00 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 de that links ag
39c10 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a ainst SQLite is.
39c20 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 ** permitted to
39c30 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 use any of these
39c40 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a routines..**.**
39c50 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72 The SQLite sour
39c60 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 ce code contains
39c70 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d multiple implem
39c80 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 entations.** of
39c90 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74 these mutex rout
39ca0 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 ines. An approp
39cb0 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 riate implementa
39cc0 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 tion.** is selec
39cd0 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ted automaticall
39ce0 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d y at compile-tim
39cf0 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e e. The followin
39d00 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 g.** implementat
39d10 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62 ions are availab
39d20 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 le in the SQLite
39d30 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c core:.**.** <ul
39d40 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 >.** <li> SQLI
39d50 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 TE_MUTEX_OS2.**
39d60 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
39d70 54 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c TEX_PTHREAD.** <
39d80 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 li> SQLITE_MUT
39d90 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 EX_W32.** <li>
39da0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f SQLITE_MUTEX_NO
39db0 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a OP.** </ul>.**.*
39dc0 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 * The SQLITE_MUT
39dd0 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e EX_NOOP implemen
39de0 74 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20 tation is a set
39df0 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74 of routines.** t
39e00 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c hat does no real
39e10 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 locking and is
39e20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 appropriate for
39e30 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67 use in.** a sing
39e40 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c le-threaded appl
39e50 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 ication. The SQ
39e60 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a LITE_MUTEX_OS2,.
39e70 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
39e80 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c PTHREAD, and SQL
39e90 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d ITE_MUTEX_W32 im
39ea0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a plementations.**
39eb0 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 are appropriate
39ec0 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32 for use on OS/2
39ed0 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64 , Unix, and Wind
39ee0 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 ows..**.** If SQ
39ef0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
39f00 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 with the SQLITE
39f10 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72 _MUTEX_APPDEF pr
39f20 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61 eprocessor.** ma
39f30 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74 cro defined (wit
39f40 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45 h "-DSQLITE_MUTE
39f50 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68 X_APPDEF=1"), th
39f60 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69 en no mutex.** i
39f70 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
39f80 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74 included with t
39f90 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20 74 he library. In t
39fa0 68 69 73 20 63 61 73 65 20 74 68 65 0a 2a 2a 20 his case the.**
39fb0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
39fc0 20 73 75 70 70 6c 79 20 61 20 63 75 73 74 6f 6d supply a custom
39fd0 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
39fe0 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a ation using the.
39ff0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
3a000 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20 G_MUTEX] option
3a010 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 of the sqlite3_c
3a020 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 69 6f 6e onfig() function
3a030 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 .** before calli
3a040 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ng sqlite3_initi
3a050 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f alize() or any o
3a060 74 68 65 72 20 70 75 62 6c 69 63 20 73 71 6c 69 ther public sqli
3a070 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e te3_.** function
3a080 20 74 68 61 74 20 63 61 6c 6c 73 20 73 71 6c 69 that calls sqli
3a090 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
3a0a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d ..**.** {H17011}
3a0b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
3a0c0 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 ex_alloc() routi
3a0d0 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e ne allocates a n
3a0e0 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 ew.** mutex and
3a0f0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
3a100 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 31 32 r to it. {H17012
3a110 7d 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 } If it returns
3a120 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 NULL.** that mea
3a130 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 ns that a mutex
3a140 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c could not be all
3a150 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 31 33 7d ocated. {H17013}
3a160 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 SQLite.** will
3a170 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b unwind its stack
3a180 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 and return an e
3a190 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d 20 54 rror. {H17014} T
3a1a0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 he argument.** t
3a1b0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
3a1c0 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f alloc() is one o
3a1d0 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 f these integer
3a1e0 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a constants:.**.**
3a1f0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 <ul>.** <li> S
3a200 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
3a210 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
3a220 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
3a230 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
3a240 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
3a250 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 STER.** <li> SQ
3a260 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3a270 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 C_MEM.** <li> S
3a280 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
3a290 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 IC_MEM2.** <li>
3a2a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
3a2b0 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 ATIC_PRNG.** <li
3a2c0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
3a2d0 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c STATIC_LRU.** <l
3a2e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
3a2f0 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 _STATIC_LRU2.**
3a300 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 </ul>.**.** {H17
3a310 30 31 35 7d 20 54 68 65 20 66 69 72 73 74 20 74 015} The first t
3a320 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 wo constants cau
3a330 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 se sqlite3_mutex
3a340 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 _alloc() to crea
3a350 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 te.** a new mute
3a360 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 x. The new mute
3a370 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 x is recursive w
3a380 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
3a390 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 _RECURSIVE.** is
3a3a0 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 used but not ne
3a3b0 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 cessarily so whe
3a3c0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 n SQLITE_MUTEX_F
3a3d0 41 53 54 20 69 73 20 75 73 65 64 2e 20 7b 45 4e AST is used. {EN
3a3e0 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 D}.** The mutex
3a3f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 implementation d
3a400 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
3a410 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 make a distincti
3a420 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 on.** between SQ
3a430 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
3a440 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f SIVE and SQLITE_
3a450 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 MUTEX_FAST if it
3a460 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e does.** not wan
3a470 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20 t to. {H17016}
3a480 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 But SQLite will
3a490 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 only request a r
3a4a0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 ecursive mutex i
3a4b0 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 n.** cases where
3a4c0 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 it really needs
3a4d0 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20 one. {END} If
3a4e0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 a faster non-rec
3a4f0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 ursive mutex.**
3a500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
3a510 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 s available on t
3a520 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d he host platform
3a530 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 , the mutex subs
3a540 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 ystem.** might r
3a550 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 eturn such a mut
3a560 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ex in response t
3a570 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 o SQLITE_MUTEX_F
3a580 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 AST..**.** {H170
3a590 31 37 7d 20 54 68 65 20 6f 74 68 65 72 20 61 6c 17} The other al
3a5a0 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
3a5b0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
3a5c0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
3a5d0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
3a5e0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
3a5f0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
3a600 2e 20 7b 45 4e 44 7d 20 20 53 69 78 20 73 74 61 . {END} Six sta
3a610 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a tic mutexes are.
3a620 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ** used by the c
3a630 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f urrent version o
3a640 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 f SQLite. Futur
3a650 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
3a660 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 Lite.** may add
3a670 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
3a680 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 c mutexes. Stat
3a690 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 ic mutexes are f
3a6a0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
3a6b0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c se by SQLite onl
3a6c0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 y. Applications
3a6d0 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
3a6e0 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a mutexes should.
3a6f0 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 ** use only the
3a700 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 dynamic mutexes
3a710 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 returned by SQLI
3a720 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 TE_MUTEX_FAST or
3a730 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 .** SQLITE_MUTEX
3a740 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a _RECURSIVE..**.*
3a750 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20 * {H17018} Note
3a760 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 that if one of t
3a770 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
3a780 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c parameters (SQL
3a790 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
3a7a0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 * or SQLITE_MUTE
3a7b0 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 X_RECURSIVE) is
3a7c0 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 used then sqlite
3a7d0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 3_mutex_alloc().
3a7e0 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 ** returns a dif
3a7f0 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 ferent mutex on
3a800 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31 every call. {H1
3a810 37 30 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68 7034} But for th
3a820 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 e static.** mute
3a830 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d x types, the sam
3a840 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 e mutex is retur
3a850 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c ned on every cal
3a860 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 l that has.** th
3a870 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 e same type numb
3a880 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 er..**.** {H1701
3a890 39 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 9} The sqlite3_m
3a8a0 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 utex_free() rout
3a8b0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
3a8c0 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 a previously.**
3a8d0 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 allocated dynami
3a8e0 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30 c mutex. {H17020
3a8f0 7d 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 } SQLite is care
3a900 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ful to deallocat
3a910 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d e every.** dynam
3a920 69 63 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 ic mutex that it
3a930 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37 allocates. {A17
3a940 30 32 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63 021} The dynamic
3a950 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f mutexes must no
3a960 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77 t be in.** use w
3a970 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61 hen they are dea
3a980 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32 llocated. {A1702
3a990 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f 2} Attempting to
3a9a0 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74 deallocate a st
3a9b0 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65 atic.** mutex re
3a9c0 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e sults in undefin
3a9d0 65 64 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31 ed behavior. {H1
3a9e0 37 30 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76 7023} SQLite nev
3a9f0 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a er deallocates.*
3aa00 2a 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 * a static mutex
3aa10 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 . {END}.**.** Th
3aa20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
3aa30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
3aa40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
3aa50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
3aa60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
3aa70 75 74 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49 utex. {H17024} I
3aa80 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 f another thread
3aa90 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 is already with
3aaa0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a in the mutex,.**
3aab0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
3aac0 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 nter() will bloc
3aad0 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 k and sqlite3_mu
3aae0 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 tex_try() will r
3aaf0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
3ab00 42 55 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20 BUSY. {H17025}
3ab10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
3ab20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 x_try() interfac
3ab30 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 e returns [SQLIT
3ab40 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 E_OK].** upon su
3ab50 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
3ab60 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65 {H17026} Mutexe
3ab70 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a s created using.
3ab80 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
3ab90 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62 65 RECURSIVE can be
3aba0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
3abb0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
3abc0 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b ame thread..** {
3abd0 48 31 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20 H17027} In such
3abe0 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 cases the,.** mu
3abf0 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 tex must be exit
3ac00 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 ed an equal numb
3ac10 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f er of times befo
3ac20 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 re another threa
3ac30 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 d.** can enter.
3ac40 20 7b 41 31 37 30 32 38 7d 20 49 66 20 74 68 65 {A17028} If the
3ac50 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
3ac60 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
3ac70 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 other.** kind of
3ac80 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e mutex more than
3ac90 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
3aca0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
3acb0 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51 ..** {H17029} SQ
3acc0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
3acd0 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20 exhibit.** such
3ace0 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20 behavior in its
3acf0 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78 own use of mutex
3ad00 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73 es..**.** Some s
3ad10 79 73 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d ystems (for exam
3ad20 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29 ple, Windows 95)
3ad30 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
3ad40 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a the operation.**
3ad50 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
3ad60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
3ad70 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 y(). On those s
3ad80 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f ystems, sqlite3_
3ad90 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77 mutex_try().** w
3ada0 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 ill always retur
3adb0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 n SQLITE_BUSY.
3adc0 7b 48 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c {H17030} The SQL
3add0 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76 ite core only ev
3ade0 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74 er uses.** sqlit
3adf0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61 e3_mutex_try() a
3ae00 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
3ae10 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63 n so this is acc
3ae20 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72 eptable behavior
3ae30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d ..**.** {H17031}
3ae40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
3ae50 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
3ae60 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
3ae70 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
3ae80 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
3ae90 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
3aea0 61 64 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68 ad. {A17032} Th
3aeb0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
3aec0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
3aed0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
3aee0 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
3aef0 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 by the.** calli
3af00 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20 ng thread or is
3af10 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c not currently al
3af20 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33 located. {H1703
3af30 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 3} SQLite will.*
3af40 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 * never do eithe
3af50 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 r. {END}.**.** I
3af60 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 f the argument t
3af70 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
3af80 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33 enter(), sqlite3
3af90 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72 _mutex_try(), or
3afa0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
3afb0 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e x_leave() is a N
3afc0 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 ULL pointer, the
3afd0 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74 n all three rout
3afe0 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61 ines.** behave a
3aff0 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 s no-ops..**.**
3b000 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
3b010 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d e3_mutex_held()]
3b020 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 and [sqlite3_mu
3b030 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a tex_notheld()]..
3b040 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
3b050 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c lite3_mutex *sql
3b060 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
3b070 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 (int);.SQLITE_AP
3b080 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d I void sqlite3_m
3b090 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 utex_free(sqlite
3b0a0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 3_mutex*);.SQLIT
3b0b0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
3b0c0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 e3_mutex_enter(s
3b0d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
3b0e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3b0f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
3b100 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 (sqlite3_mutex*)
3b110 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
3b120 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
3b130 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 leave(sqlite3_mu
3b140 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tex*);../*.** CA
3b150 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65 PI3REF: Mutex Me
3b160 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 thods Object {H1
3b170 37 31 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 7120} <S20130>.*
3b180 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3b190 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
3b1a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
3b1b0 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c re defines the l
3b1c0 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 ow-level routine
3b1d0 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c s.** used to all
3b1e0 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75 ocate and use mu
3b1f0 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75 texes..**.** Usu
3b200 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c ally, the defaul
3b210 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e t mutex implemen
3b220 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64 tations provided
3b230 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a by SQLite are.*
3b240 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f * sufficient, ho
3b250 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20 68 wever the user h
3b260 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66 as the option of
3b270 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 substituting a
3b280 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d custom.** implem
3b290 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65 entation for spe
3b2a0 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d cialized deploym
3b2b0 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20 ents or systems
3b2c0 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 74 65 for which SQLite
3b2d0 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f .** does not pro
3b2e0 76 69 64 65 20 61 20 73 75 69 74 61 62 6c 65 20 vide a suitable
3b2f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 implementation.
3b300 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 In this case, th
3b310 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65 e user.** create
3b320 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20 s and populates
3b330 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
3b340 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f his structure to
3b350 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 pass.** to sqli
3b360 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f te3_config() alo
3b370 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ng with the [SQL
3b380 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
3b390 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64 ] option..** Add
3b3a0 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e itionally, an in
3b3b0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
3b3c0 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 tructure can be
3b3d0 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75 used as an.** ou
3b3e0 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 77 68 tput variable wh
3b3f0 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 65 20 en querying the
3b400 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63 system for the c
3b410 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20 urrent mutex.**
3b420 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
3b430 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 using the [SQLIT
3b440 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 E_CONFIG_GETMUTE
3b450 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a X] option..**.**
3b460 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 The xMutexInit
3b470 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 method defined b
3b480 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
3b490 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a is invoked as.*
3b4a0 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d * part of system
3b4b0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
3b4c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 by the sqlite3_i
3b4d0 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63 nitialize() func
3b4e0 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31 tion..** {H17001
3b4f0 7d 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 } The xMutexInit
3b500 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62 routine shall b
3b510 65 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 e called by SQLi
3b520 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 te once for each
3b530 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 20 63 61 .** effective ca
3b540 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 ll to [sqlite3_i
3b550 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a nitialize()]..**
3b560 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e .** The xMutexEn
3b570 64 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 d method defined
3b580 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 by this structu
3b590 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 re is invoked as
3b5a0 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 .** part of syst
3b5b0 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 em shutdown by t
3b5c0 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 he sqlite3_shutd
3b5d0 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 own() function.
3b5e0 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 The.** implement
3b5f0 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 ation of this me
3b600 74 68 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 thod is expected
3b610 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 to release all
3b620 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 outstanding.** r
3b630 65 73 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 esources obtaine
3b640 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d d by the mutex m
3b650 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 ethods implement
3b660 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c ation, especiall
3b670 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 y.** those obtai
3b680 6e 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 ned by the xMute
3b690 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48 xInit method. {H
3b6a0 31 37 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65 17003} The xMute
3b6b0 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 xEnd().** interf
3b6c0 61 63 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 ace shall be inv
3b6d0 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 oked once for ea
3b6e0 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ch call to [sqli
3b6f0 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e te3_shutdown()].
3b700 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 .**.** The remai
3b710 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f ning seven metho
3b720 64 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 ds defined by th
3b730 69 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d is structure (xM
3b740 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d utexAlloc,.** xM
3b750 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78 utexFree, xMutex
3b760 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79 Enter, xMutexTry
3b770 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78 , xMutexLeave, x
3b780 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a MutexHeld and.**
3b790 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20 xMutexNotheld)
3b7a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f implement the fo
3b7b0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 llowing interfac
3b7c0 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c 79 es (respectively
3b7d0 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a ):.**.** <ul>.**
3b7e0 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 <li> [sqlite
3b7f0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 3_mutex_alloc()]
3b800 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e </li>.** <li>
3b810 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 [sqlite3_mutex
3b820 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a _free()] </li>.*
3b830 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 * <li> [sqlit
3b840 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
3b850 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
3b860 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
3b870 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a x_try()] </li>.*
3b880 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 * <li> [sqlit
3b890 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
3b8a0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
3b8b0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
3b8c0 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a x_held()] </li>.
3b8d0 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 ** <li> [sqli
3b8e0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
3b8f0 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f d()] </li>.** </
3b900 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e ul>.**.** The on
3b910 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 ly difference is
3b920 20 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63 that the public
3b930 20 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e sqlite3_XXX fun
3b940 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 ctions enumerate
3b950 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e d.** above silen
3b960 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 tly ignore any i
3b970 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 nvocations that
3b980 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e pass a NULL poin
3b990 74 65 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f ter instead.** o
3b9a0 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 f a valid mutex
3b9b0 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c handle. The impl
3b9c0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 ementations of t
3b9d0 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e he methods defin
3b9e0 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74 ed.** by this st
3b9f0 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20 ructure are not
3ba00 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 required to hand
3ba10 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 le this case, th
3ba20 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 e results.** of
3ba30 70 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 passing a NULL p
3ba40 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f ointer instead o
3ba50 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 f a valid mutex
3ba60 68 61 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66 handle are undef
3ba70 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 ined.** (i.e. it
3ba80 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 74 is acceptable t
3ba90 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 o provide an imp
3baa0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
3bab0 20 73 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a segfaults if.**
3bac0 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20 it is passed a
3bad0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a NULL pointer)..*
3bae0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 *.** The xMutexI
3baf0 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 6d 75 73 nit() method mus
3bb00 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e t be threadsafe.
3bb10 20 20 49 74 20 6d 75 73 74 20 62 65 20 68 61 72 It must be har
3bb20 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 69 6e 76 6f mless to.** invo
3bb30 6b 65 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 ke xMutexInit()
3bb40 6d 75 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 mutiple times wi
3bb50 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 thin the same pr
3bb60 6f 63 65 73 73 20 61 6e 64 20 77 69 74 68 6f 75 ocess and withou
3bb70 74 0a 2a 2a 20 69 6e 74 65 72 76 65 6e 69 6e 67 t.** intervening
3bb80 20 63 61 6c 6c 73 20 74 6f 20 78 4d 75 74 65 78 calls to xMutex
3bb90 45 6e 64 28 29 2e 20 20 53 65 63 6f 6e 64 20 61 End(). Second a
3bba0 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 nd subsequent ca
3bbb0 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 74 65 78 lls to.** xMutex
3bbc0 49 6e 69 74 28 29 20 6d 75 73 74 20 62 65 20 6e Init() must be n
3bbd0 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 78 4d 75 o-ops..**.** xMu
3bbe0 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e texInit() must n
3bbf0 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 ot use SQLite me
3bc00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
3bc10 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 ([sqlite3_malloc
3bc20 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61 ()].** and its a
3bc30 73 73 6f 63 69 61 74 65 73 29 2e 20 20 53 69 6d ssociates). Sim
3bc40 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41 6c ilarly, xMutexAl
3bc50 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20 75 loc() must not u
3bc60 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 79 se SQLite memory
3bc70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 .** allocation f
3bc80 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 or a static mute
3bc90 78 2e 20 20 48 6f 77 65 76 65 72 20 78 4d 75 74 x. However xMut
3bca0 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 20 75 73 exAlloc() may us
3bcb0 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 65 6d 6f e SQLite.** memo
3bcc0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f ry allocation fo
3bcd0 72 20 61 20 66 61 73 74 20 6f 72 20 72 65 63 75 r a fast or recu
3bce0 72 73 69 76 65 20 6d 75 74 65 78 2e 0a 2a 2a 0a rsive mutex..**.
3bcf0 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 ** SQLite will i
3bd00 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75 74 65 78 nvoke the xMutex
3bd10 45 6e 64 28 29 20 6d 65 74 68 6f 64 20 77 68 65 End() method whe
3bd20 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 n [sqlite3_shutd
3bd30 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20 63 61 6c own()] is.** cal
3bd40 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79 20 69 66 led, but only if
3bd50 20 74 68 65 20 70 72 69 6f 72 20 63 61 6c 6c 20 the prior call
3bd60 74 6f 20 78 4d 75 74 65 78 49 6e 69 74 20 72 65 to xMutexInit re
3bd70 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b turned SQLITE_OK
3bd80 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65 78 49 6e ..** If xMutexIn
3bd90 69 74 20 66 61 69 6c 73 20 69 6e 20 61 6e 79 20 it fails in any
3bda0 77 61 79 2c 20 69 74 20 69 73 20 65 78 70 65 63 way, it is expec
3bdb0 74 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 ted to clean up
3bdc0 61 66 74 65 72 20 69 74 73 65 6c 66 0a 2a 2a 20 after itself.**
3bdd0 70 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e 69 prior to returni
3bde0 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ng..*/.typedef s
3bdf0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 truct sqlite3_mu
3be00 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 tex_methods sqli
3be10 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
3be20 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 s;.struct sqlite
3be30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
3be40 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 {. int (*xMutex
3be50 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 Init)(void);. i
3be60 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 nt (*xMutexEnd)(
3be70 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 void);. sqlite3
3be80 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 _mutex *(*xMutex
3be90 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 Alloc)(int);. v
3bea0 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 oid (*xMutexFree
3beb0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 )(sqlite3_mutex
3bec0 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 *);. void (*xMu
3bed0 74 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 texEnter)(sqlite
3bee0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 3_mutex *);. in
3bef0 74 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 t (*xMutexTry)(s
3bf00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b qlite3_mutex *);
3bf10 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 . void (*xMutex
3bf20 4c 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d Leave)(sqlite3_m
3bf30 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 utex *);. int (
3bf40 2a 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c *xMutexHeld)(sql
3bf50 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
3bf60 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 int (*xMutexNot
3bf70 68 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 held)(sqlite3_mu
3bf80 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a tex *);.};../*.*
3bf90 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
3bfa0 78 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 x Verification R
3bfb0 6f 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d outines {H17080}
3bfc0 20 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 <S20130> <S3080
3bfd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
3bfe0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
3bff0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
3c000 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
3c010 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e utines.** are in
3c020 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
3c030 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
3c040 74 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 tatements. {H170
3c050 38 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 81} The SQLite c
3c060 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 ore.** never use
3c070 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 s these routines
3c080 20 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 except inside a
3c090 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 n assert() and a
3c0a0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 pplications.** a
3c0b0 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f re advised to fo
3c0c0 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 llow the lead of
3c0d0 20 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 the core. {H17
3c0e0 30 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 082} The core on
3c0f0 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 ly.** provides i
3c100 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 mplementations f
3c110 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 or these routine
3c120 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d s when it is com
3c130 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 piled.** with th
3c140 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 e SQLITE_DEBUG f
3c150 6c 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 lag. {A17087} E
3c160 78 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d xternal mutex im
3c170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a plementations.**
3c180 20 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 are only requir
3c190 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 ed to provide th
3c1a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 ese routines if
3c1b0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a SQLITE_DEBUG is.
3c1c0 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 ** defined and i
3c1d0 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 f NDEBUG is not
3c1e0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b defined..**.** {
3c1f0 48 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f H17083} These ro
3c200 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 utines should re
3c210 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
3c220 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 mutex in their
3c230 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 argument.** is h
3c240 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c eld or not held,
3c250 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 respectively, b
3c260 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 y the calling th
3c270 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 read..**.** {X17
3c280 30 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 084} The impleme
3c290 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 ntation is not r
3c2a0 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 equired to provi
3c2b0 64 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ded versions of
3c2c0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 these.** routine
3c2d0 73 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 s that actually
3c2e0 77 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 work. If the imp
3c2f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
3c300 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 not provide wor
3c310 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 king.** versions
3c320 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
3c330 65 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 es, it should at
3c340 20 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 least provide s
3c350 74 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 tubs that always
3c360 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 .** return true
3c370 73 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 so that one does
3c380 20 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 not get spuriou
3c390 73 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c s assertion fail
3c3a0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ures..**.** {H17
3c3b0 30 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 085} If the argu
3c3c0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f ment to sqlite3_
3c3d0 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 mutex_held() is
3c3e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 a NULL pointer t
3c3f0 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 hen.** the routi
3c400 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e ne should return
3c410 20 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 1. {END} This
3c420 73 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e seems counter-in
3c430 74 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a tuitive since.**
3c440 20 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 clearly the mut
3c450 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c ex cannot be hel
3c460 64 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 d if it does not
3c470 20 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 exist. But the
3c480 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 .** the reason t
3c490 68 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f he mutex does no
3c4a0 74 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75 t exist is becau
3c4b0 73 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 se the build is
3c4c0 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 not.** using mut
3c4d0 65 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f exes. And we do
3c4e0 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 not want the as
3c4f0 73 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e sert() containin
3c500 67 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f g the.** call to
3c510 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
3c520 65 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 eld() to fail, s
3c530 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 o a non-zero ret
3c540 75 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 urn is.** the ap
3c550 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 propriate thing
3c560 74 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d to do. {H17086}
3c570 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
3c580 65 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 ex_notheld().**
3c590 69 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 interface should
3c5a0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 also return 1 w
3c5b0 68 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c hen given a NULL
3c5c0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c pointer..*/.SQL
3c5d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
3c5e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 te3_mutex_held(s
3c5f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
3c600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3c610 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
3c620 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
3c630 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ex*);../*.** CAP
3c640 49 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 I3REF: Mutex Typ
3c650 65 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 es {H17001} <H17
3c660 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 000>.**.** The [
3c670 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
3c680 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 loc()] interface
3c690 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
3c6a0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 argument.** whic
3c6b0 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 h is one of thes
3c6c0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 e integer consta
3c6d0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nts..**.** The s
3c6e0 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 et of static mut
3c6f0 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 exes may change
3c700 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 from one SQLite
3c710 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a release to the.*
3c720 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 * next. Applica
3c730 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 tions that overr
3c740 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e ide the built-in
3c750 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 mutex logic mus
3c760 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 t be.** prepared
3c770 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 to accommodate
3c780 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
3c790 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 c mutexes..*/.#d
3c7a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
3c7b0 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 EX_FAST
3c7c0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
3c7d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
3c7e0 53 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 SIVE 1.#d
3c7f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
3c800 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
3c810 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
3c820 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3c830 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f C_MEM 3 /
3c840 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
3c850 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 () */.#define SQ
3c860 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3c870 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f C_MEM2 4 /
3c880 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64 * NOT USED */.#d
3c890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
3c8a0 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 20 20 EX_STATIC_OPEN
3c8b0 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 4 /* sqlite
3c8c0 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f 0a 3BtreeOpen() */.
3c8d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c8e0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 UTEX_STATIC_PRNG
3c8f0 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 5 /* sqli
3c900 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a te3_random() */.
3c910 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c920 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 UTEX_STATIC_LRU
3c930 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 75 20 6 /* lru
3c940 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65 page list */.#de
3c950 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
3c960 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 20 20 X_STATIC_LRU2
3c970 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 61 67 7 /* lru pag
3c980 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a e list */../*.**
3c990 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 CAPI3REF: Retri
3c9a0 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f eve the mutex fo
3c9b0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e r a database con
3c9c0 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d nection {H17002}
3c9d0 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H17000>.**.**
3c9e0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 This interface r
3c9f0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
3ca00 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 the [sqlite3_mu
3ca10 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 tex] object that
3ca20 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 .** serializes
3ca30 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64 access to the [d
3ca40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3ca50 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 on] given in the
3ca60 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 argument.** whe
3ca70 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 n the [threading
3ca80 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c mode] is Serial
3ca90 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ized..** If the
3caa0 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
3cab0 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 is Single-threa
3cac0 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 d or Multi-threa
3cad0 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 d then this.** r
3cae0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
3caf0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
3cb00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
3cb10 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
3cb20 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c te3_db_mutex(sql
3cb30 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
3cb40 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 API3REF: Low-Lev
3cb50 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 el Control Of Da
3cb60 74 61 62 61 73 65 20 46 69 6c 65 73 20 7b 48 31 tabase Files {H1
3cb70 31 33 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 1300} <S30800>.*
3cb80 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 54 68 *.** {H11301} Th
3cb90 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f e [sqlite3_file_
3cba0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 control()] inter
3cbb0 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72 face makes a dir
3cbc0 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a ect call to the.
3cbd0 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 ** xFileControl
3cbe0 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b method for the [
3cbf0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
3cc00 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 ds] object assoc
3cc10 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 iated.** with a
3cc20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 particular datab
3cc30 61 73 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 ase identified b
3cc40 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 y the second arg
3cc50 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20 ument. {H11302}
3cc60 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 The.** name of t
3cc70 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 74 he database is t
3cc80 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 he name assigned
3cc90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
3cca0 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 by the.** <a hr
3ccb0 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e ef="lang_attach.
3ccc0 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e html">ATTACH</a>
3ccd0 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 SQL command tha
3cce0 74 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 t opened the.**
3ccf0 64 61 74 61 62 61 73 65 2e 20 7b 48 31 31 33 30 database. {H1130
3cd00 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 3} To control th
3cd10 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
3cd20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61 file, use the na
3cd30 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 me "main".** or
3cd40 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 a NULL pointer.
3cd50 7b 48 31 31 33 30 34 7d 20 54 68 65 20 74 68 69 {H11304} The thi
3cd60 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 rd and fourth pa
3cd70 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 rameters to this
3cd80 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 routine.** are
3cd90 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 passed directly
3cda0 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 through to the s
3cdb0 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 econd and third
3cdc0 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a parameters of.**
3cdd0 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f the xFileContro
3cde0 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 31 33 l method. {H113
3cdf0 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76 05} The return v
3ce00 61 6c 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c alue of the xFil
3ce10 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 eControl.** meth
3ce20 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 od becomes the r
3ce30 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 eturn value of t
3ce40 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a his routine..**.
3ce50 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 20 74 ** {H11306} If t
3ce60 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
3ce70 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f ter (zDbName) do
3ce80 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 es not match the
3ce90 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 name of any.**
3cea0 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 open database fi
3ceb0 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f le, then SQLITE_
3cec0 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 ERROR is returne
3ced0 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 69 73 d. {H11307} This
3cee0 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 error.** code i
3cef0 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 s not remembered
3cf00 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 and will not be
3cf10 20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 recalled by [sq
3cf20 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d lite3_errcode()]
3cf30 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f .** or [sqlite3_
3cf40 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33 errmsg()]. {A113
3cf50 30 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69 08} The underlyi
3cf60 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 ng xFileControl
3cf70 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 method might.**
3cf80 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 also return SQLI
3cf90 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 31 33 TE_ERROR. {A113
3cfa0 30 39 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20 09} There is no
3cfb0 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 way to distingui
3cfc0 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e sh between.** an
3cfd0 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 incorrect zDbNa
3cfe0 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 me and an SQLITE
3cff0 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 _ERROR return fr
3d000 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e om the underlyin
3d010 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f g.** xFileContro
3d020 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a l method. {END}.
3d030 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
3d040 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f [SQLITE_FCNTL_LO
3d050 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 CKSTATE].*/.SQLI
3d060 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
3d070 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
3d080 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 sqlite3*, const
3d090 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 char *zDbName, i
3d0a0 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a nt op, void*);..
3d0b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3d0c0 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 Testing Interfac
3d0d0 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 30 38 e {H11400} <S308
3d0e0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
3d0f0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 lite3_test_contr
3d100 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 ol() interface i
3d110 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f s used to read o
3d120 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 ut internal.** s
3d130 74 61 74 65 20 6f 66 20 53 51 4c 69 74 65 20 61 tate of SQLite a
3d140 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 61 75 nd to inject fau
3d150 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 lts into SQLite
3d160 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 for testing.** p
3d170 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 66 69 urposes. The fi
3d180 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 rst parameter is
3d190 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f an operation co
3d1a0 64 65 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e de that determin
3d1b0 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 es.** the number
3d1c0 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f , meaning, and o
3d1d0 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 peration of all
3d1e0 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d subsequent param
3d1f0 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 eters..**.** Thi
3d200 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e s interface is n
3d210 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20 61 70 ot for use by ap
3d220 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74 20 plications. It
3d230 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a exists solely.**
3d240 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20 74 for verifying t
3d250 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 he correct opera
3d260 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 tion of the SQLi
3d270 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65 70 te library. Dep
3d280 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 ending.** on how
3d290 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
3d2a0 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c ary is compiled,
3d2b0 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
3d2c0 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 2e might not exist.
3d2d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 .**.** The detai
3d2e0 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 ls of the operat
3d2f0 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72 ion codes, their
3d300 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 meanings, the p
3d310 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65 arameters.** the
3d320 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 y take, and what
3d330 20 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c they do are all
3d340 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
3d350 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 ge without notic
3d360 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 e..** Unlike mos
3d370 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 t of the SQLite
3d380 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69 API, this functi
3d390 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e on is not guaran
3d3a0 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 teed to.** opera
3d3b0 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 te consistently
3d3c0 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65 from one release
3d3d0 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f to the next..*/
3d3e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3d3f0 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e sqlite3_test_con
3d400 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e trol(int op, ...
3d410 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3d420 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 EF: Testing Inte
3d430 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e 20 rface Operation
3d440 43 6f 64 65 73 20 7b 48 31 31 34 31 30 7d 20 3c Codes {H11410} <
3d450 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H11400>.**.** Th
3d460 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
3d470 65 20 74 68 65 20 76 61 6c 69 64 20 6f 70 65 72 e the valid oper
3d480 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d ation code param
3d490 65 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 61 73 eters used.** as
3d4a0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
3d4b0 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ent to [sqlite3_
3d4c0 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e test_control()].
3d4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 61 72 .**.** These par
3d4e0 61 6d 65 74 65 72 73 20 61 6e 64 20 74 68 65 69 ameters and thei
3d4f0 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 20 73 r meanings are s
3d500 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
3d510 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 .** without noti
3d520 63 65 2e 20 20 54 68 65 73 65 20 76 61 6c 75 65 ce. These value
3d530 73 20 61 72 65 20 66 6f 72 20 74 65 73 74 69 6e s are for testin
3d540 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e g purposes only.
3d550 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 .** Applications
3d560 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 should not use
3d570 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70 61 72 any of these par
3d580 61 6d 65 74 65 72 73 20 6f 72 20 74 68 65 0a 2a ameters or the.*
3d590 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f * [sqlite3_test_
3d5a0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 control()] inter
3d5b0 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 face..*/.#define
3d5c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
3d5d0 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20 20 20 _PRNG_SAVE
3d5e0 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 5.#def
3d5f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
3d600 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 TRL_PRNG_RESTORE
3d610 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 6.#
3d620 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d630 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 STCTRL_PRNG_RESE
3d640 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
3d650 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 7.#define SQLITE
3d660 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 _TESTCTRL_BITVEC
3d670 5f 54 45 53 54 20 20 20 20 20 20 20 20 20 20 20 _TEST
3d680 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 8.#define SQL
3d690 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 ITE_TESTCTRL_FAU
3d6a0 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 20 20 LT_INSTALL
3d6b0 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 9.#define
3d6c0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d6d0 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f BENIGN_MALLOC_HO
3d6e0 4f 4b 53 20 20 20 20 20 31 30 0a 23 64 65 66 69 OKS 10.#defi
3d6f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
3d700 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 RL_PENDING_BYTE
3d710 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 11.#d
3d720 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3d730 54 43 54 52 4c 5f 41 53 53 45 52 54 20 20 20 20 TCTRL_ASSERT
3d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
3d750 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3d760 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 20 TESTCTRL_ALWAYS
3d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d780 20 31 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 13.#define SQLI
3d790 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45 TE_TESTCTRL_RESE
3d7a0 52 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20 RVE
3d7b0 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 14../*.** CA
3d7c0 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52 PI3REF: SQLite R
3d7d0 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48 untime Status {H
3d7e0 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17200} <S60200>.
3d7f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
3d800 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
3d810 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
3d820 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 retrieve runtime
3d830 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
3d840 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 ion.** about the
3d850 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20 preformance of
3d860 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69 SQLite, and opti
3d870 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20 onally to reset
3d880 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77 various.** highw
3d890 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65 ater marks. The
3d8a0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
3d8b0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
3d8c0 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 de for.** the sp
3d8d0 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72 ecific parameter
3d8e0 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65 to measure. Re
3d8f0 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 cognized integer
3d900 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66 codes.** are of
3d910 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54 the form [SQLIT
3d920 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
3d930 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54 USED | SQLITE_ST
3d940 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68 ATUS_...]..** Th
3d950 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
3d960 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
3d970 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74 is returned int
3d980 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20 o *pCurrent..**
3d990 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f The highest reco
3d9a0 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65 rded value is re
3d9b0 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68 turned in *pHigh
3d9c0 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a water. If the.*
3d9d0 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74 * resetFlag is t
3d9e0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 rue, then the hi
3d9f0 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c ghest record val
3da00 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65 ue is reset afte
3da10 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 r.** *pHighwater
3da20 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d is written. Som
3da30 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20 e parameters do
3da40 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68 not record the h
3da50 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e ighest.** value.
3da60 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61 For those para
3da70 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e meters.** nothin
3da80 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
3da90 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e o *pHighwater an
3daa0 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20 d the resetFlag
3dab0 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f is ignored..** O
3dac0 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 ther parameters
3dad0 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20 record only the
3dae0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 highwater mark a
3daf0 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65 nd not the curre
3db00 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f nt.** value. Fo
3db10 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70 r these latter p
3db20 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e arameters nothin
3db30 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 g is written int
3db40 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a o *pCurrent..**.
3db50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
3db60 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
3db70 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 K on success and
3db80 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b a non-zero.** [
3db90 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 error code] on f
3dba0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ailure..**.** Th
3dbb0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
3dbc0 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20 readsafe but is
3dbd0 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69 not atomic. Thi
3dbe0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
3dbf0 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 .** called while
3dc00 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 other threads a
3dc10 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 re running the s
3dc20 61 6d 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74 ame or different
3dc30 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 SQLite.** inter
3dc40 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20 faces. However
3dc50 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 the values retur
3dc60 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 ned in *pCurrent
3dc70 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 and.** *pHighwa
3dc80 74 65 72 20 72 65 66 6c 65 63 74 20 74 68 65 20 ter reflect the
3dc90 73 74 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65 status of SQLite
3dca0 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f at different po
3dcb0 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 ints in time.**
3dcc0 61 6e 64 20 69 74 20 69 73 20 70 6f 73 73 69 62 and it is possib
3dcd0 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 le that another
3dce0 74 68 72 65 61 64 20 6d 69 67 68 74 20 63 68 61 thread might cha
3dcf0 6e 67 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 nge the paramete
3dd00 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 r.** in between
3dd10 74 68 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a the times when *
3dd20 70 43 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 pCurrent and *pH
3dd30 69 67 68 77 61 74 65 72 20 61 72 65 20 77 72 69 ighwater are wri
3dd40 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 tten..**.** See
3dd50 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 also: [sqlite3_d
3dd60 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 b_status()].*/.S
3dd70 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
3dd80 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
3dd90 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 t sqlite3_status
3dda0 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 (int op, int *pC
3ddb0 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 urrent, int *pHi
3ddc0 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 ghwater, int res
3ddd0 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a etFlag);.../*.**
3dde0 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 CAPI3REF: Statu
3ddf0 73 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31 s Parameters {H1
3de00 37 32 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 7250} <H17200>.*
3de10 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3de20 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 *.** These integ
3de30 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 er constants des
3de40 69 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 ignate various r
3de50 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 un-time status p
3de60 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 arameters.** tha
3de70 74 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 t can be returne
3de80 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 d by [sqlite3_st
3de90 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c atus()]..**.** <
3dea0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 dl>.** <dt>SQLIT
3deb0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
3dec0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 USED</dt>.** <dd
3ded0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3dee0 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 is the current a
3def0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
3df00 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 checked out.** u
3df10 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 sing [sqlite3_ma
3df20 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 lloc()], either
3df30 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 directly or indi
3df40 72 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 rectly. The.**
3df50 66 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 figure includes
3df60 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 calls made to [s
3df70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
3df80 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 by the applicat
3df90 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 ion.** and inter
3dfa0 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 nal memory usage
3dfb0 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c by the SQLite l
3dfc0 69 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 ibrary. Scratch
3dfd0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 memory.** contr
3dfe0 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 olled by [SQLITE
3dff0 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
3e000 20 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 and auxiliary p
3e010 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d age-cache.** mem
3e020 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 ory controlled b
3e030 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 y [SQLITE_CONFIG
3e040 5f 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e _PAGECACHE] is n
3e050 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a ot included in.*
3e060 2a 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 * this parameter
3e070 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 . The amount re
3e080 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 turned is the su
3e090 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 m of the allocat
3e0a0 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 ion.** sizes as
3e0b0 72 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 reported by the
3e0c0 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 xSize method in
3e0d0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 [sqlite3_mem_met
3e0e0 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a hods].</dd>.**.*
3e0f0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3e100 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c TUS_MALLOC_SIZE<
3e110 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3e120 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 parameter recor
3e130 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d ds the largest m
3e140 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
3e150 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 request.** hand
3e160 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d ed to [sqlite3_m
3e170 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c alloc()] or [sql
3e180 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 ite3_realloc()]
3e190 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 (or their.** int
3e1a0 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 ernal equivalent
3e1b0 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 s). Only the va
3e1c0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 lue returned in
3e1d0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 the.** *pHighwat
3e1e0 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 er parameter to
3e1f0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 [sqlite3_status(
3e200 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 )] is of interes
3e210 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 t. .** The valu
3e220 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
3e230 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 he *pCurrent par
3e240 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 ameter is undefi
3e250 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
3e260 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
3e270 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 S_PAGECACHE_USED
3e280 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3e290 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 s parameter retu
3e2a0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
3e2b0 66 20 70 61 67 65 73 20 75 73 65 64 20 6f 75 74 f pages used out
3e2c0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 of the.** [page
3e2d0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c cache memory all
3e2e0 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73 ocator] that was
3e2f0 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e configured usin
3e300 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f g .** [SQLITE_CO
3e310 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e NFIG_PAGECACHE].
3e320 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 The.** value r
3e330 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61 eturned is in pa
3e340 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 ges, not in byte
3e350 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 s.</dd>.**.** <d
3e360 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t>SQLITE_STATUS_
3e370 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c PAGECACHE_OVERFL
3e380 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 OW</dt>.** <dd>T
3e390 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3e3a0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
3e3b0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 67 of bytes of pag
3e3c0 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 e cache.** alloc
3e3d0 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c ation which coul
3e3e0 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 d not be statisf
3e3f0 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 ied by the [SQLI
3e400 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
3e410 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 CHE].** buffer a
3e420 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 nd where forced
3e430 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b to overflow to [
3e440 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
3e450 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 ]. The.** retur
3e460 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 ned value includ
3e470 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 es allocations t
3e480 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 hat overflowed b
3e490 65 63 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 ecause they.** w
3e4a0 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 here too large (
3e4b0 74 68 65 79 20 77 65 72 65 20 6c 61 72 67 65 72 they were larger
3e4c0 20 74 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70 than the "sz" p
3e4d0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b arameter to.** [
3e4e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
3e4f0 47 45 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c GECACHE]) and al
3e500 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f locations that o
3e510 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 verflowed becaus
3e520 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 e.** no space wa
3e530 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61 s left in the pa
3e540 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a ge cache.</dd>.*
3e550 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3e560 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
3e570 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _SIZE</dt>.** <d
3e580 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3e590 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 records the lar
3e5a0 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f gest memory allo
3e5b0 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a cation request.*
3e5c0 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 * handed to [pag
3e5d0 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c ecache memory al
3e5e0 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 locator]. Only
3e5f0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
3e600 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 ed in the.** *pH
3e610 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 ighwater paramet
3e620 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 er to [sqlite3_s
3e630 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 tatus()] is of i
3e640 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 nterest. .** Th
3e650 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 e value written
3e660 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 into the *pCurre
3e670 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nt parameter is
3e680 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a undefined.</dd>.
3e690 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3e6a0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
3e6b0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 USED</dt>.** <dd
3e6c0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3e6d0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
3e6e0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e er of allocation
3e6f0 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 s used out of th
3e700 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 e.** [scratch me
3e710 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 mory allocator]
3e720 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 configured using
3e730 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
3e740 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 IG_SCRATCH]. Th
3e750 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
3e760 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f is in allocatio
3e770 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 ns, not.** in by
3e780 74 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 tes. Since a si
3e790 6e 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 ngle thread may
3e7a0 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 only have one sc
3e7b0 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e ratch allocation
3e7c0 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 .** outstanding
3e7d0 61 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 at time, this pa
3e7e0 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 rameter also rep
3e7f0 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 orts the number
3e800 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 of threads.** us
3e810 69 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f ing scratch memo
3e820 72 79 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ry at the same t
3e830 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ime.</dd>.**.**
3e840 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
3e850 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c S_SCRATCH_OVERFL
3e860 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 OW</dt>.** <dd>T
3e870 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3e880 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
3e890 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 63 72 of bytes of scr
3e8a0 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 atch memory.** a
3e8b0 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 llocation which
3e8c0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 could not be sta
3e8d0 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b tisfied by the [
3e8e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 SQLITE_CONFIG_SC
3e8f0 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 RATCH].** buffer
3e900 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 and where force
3e910 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f d to overflow to
3e920 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
3e930 28 29 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73 ()]. The values
3e940 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63 .** returned inc
3e950 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 lude overflows b
3e960 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 ecause the reque
3e970 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 sted allocation
3e980 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 was too.** large
3e990 72 20 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 r (that is, beca
3e9a0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 use the requeste
3e9b0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 d allocation was
3e9c0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
3e9d0 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 .** "sz" paramet
3e9e0 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f er to [SQLITE_CO
3e9f0 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 NFIG_SCRATCH]) a
3ea00 6e 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 nd because no sc
3ea10 72 61 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 ratch buffer.**
3ea20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c slots were avail
3ea30 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a able..** </dd>.*
3ea40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3ea50 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 STATUS_SCRATCH_S
3ea60 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e IZE</dt>.** <dd>
3ea70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3ea80 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 ecords the large
3ea90 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 st memory alloca
3eaa0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 tion request.**
3eab0 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74 handed to [scrat
3eac0 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ch memory alloca
3ead0 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 tor]. Only the
3eae0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
3eaf0 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 n the.** *pHighw
3eb00 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 ater parameter t
3eb10 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 o [sqlite3_statu
3eb20 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 s()] is of inter
3eb30 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 est. .** The va
3eb40 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f lue written into
3eb50 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 the *pCurrent p
3eb60 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 arameter is unde
3eb70 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a fined.</dd>.**.*
3eb80 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3eb90 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b TUS_PARSER_STACK
3eba0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3ebb0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f s parameter reco
3ebc0 72 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20 rds the deepest
3ebd0 70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49 parser stack. I
3ebe0 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 t is only.** mea
3ebf0 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74 ningful if SQLit
3ec00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 e is compiled wi
3ec10 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 th [YYTRACKMAXST
3ec20 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a ACKDEPTH].</dd>.
3ec30 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e ** </dl>.**.** N
3ec40 65 77 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 ew status parame
3ec50 74 65 72 73 20 6d 61 79 20 62 65 20 61 64 64 65 ters may be adde
3ec60 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 d from time to t
3ec70 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ime..*/.#define
3ec80 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
3ec90 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 20 20 20 MORY_USED
3eca0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
3ecb0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
3ecc0 41 43 48 45 5f 55 53 45 44 20 20 20 20 20 20 20 ACHE_USED
3ecd0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3ece0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
3ecf0 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 E_OVERFLOW 2.#
3ed00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3ed10 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 ATUS_SCRATCH_USE
3ed20 44 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 D 3.#def
3ed30 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3ed40 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c S_SCRATCH_OVERFL
3ed50 4f 57 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 OW 4.#define
3ed60 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d SQLITE_STATUS_M
3ed70 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 ALLOC_SIZE
3ed80 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 5.#define SQ
3ed90 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 LITE_STATUS_PARS
3eda0 45 52 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 ER_STACK
3edb0 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 6.#define SQLIT
3edc0 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
3edd0 48 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a HE_SIZE 7.
3ede0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3edf0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 TATUS_SCRATCH_SI
3ee00 5a 45 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a ZE 8../*
3ee10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 .** CAPI3REF: Da
3ee20 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
3ee30 6e 20 53 74 61 74 75 73 20 7b 48 31 37 35 30 30 n Status {H17500
3ee40 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 } <S60200>.** EX
3ee50 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
3ee60 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
3ee70 69 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 is used to retri
3ee80 65 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 eve runtime stat
3ee90 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a us information .
3eea0 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c ** about a singl
3eeb0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3eec0 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 ection]. The fi
3eed0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
3eee0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
3eef0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 connection objec
3ef00 74 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 t to be interrog
3ef10 61 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e ated. The secon
3ef20 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 d argument.** is
3ef30 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 the parameter t
3ef40 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 o interrogate.
3ef50 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f Currently, the o
3ef60 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 nly allowed valu
3ef70 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63 e.** for the sec
3ef80 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ond parameter is
3ef90 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 [SQLITE_DBSTATU
3efa0 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 S_LOOKASIDE_USED
3efb0 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c ]..** Additional
3efc0 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 options will li
3efd0 6b 65 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66 kely appear in f
3efe0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f uture releases o
3eff0 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 f SQLite..**.**
3f000 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 The current valu
3f010 65 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 e of the request
3f020 65 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 ed parameter is
3f030 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 written into *pC
3f040 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 ur.** and the hi
3f050 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 ghest instantane
3f060 6f 75 73 20 76 61 6c 75 65 20 69 73 20 77 72 69 ous value is wri
3f070 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 tten into *pHiwt
3f080 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 r. If.** the re
3f090 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 setFlg is true,
3f0a0 74 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 then the highest
3f0b0 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 instantaneous v
3f0c0 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 alue is.** reset
3f0d0 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 back down to th
3f0e0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e e current value.
3f0f0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
3f100 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
3f110 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
3f120 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e _stmt_status()].
3f130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
3f140 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
3f150 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 AL int sqlite3_d
3f160 62 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 b_status(sqlite3
3f170 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a *, int op, int *
3f180 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 pCur, int *pHiwt
3f190 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29 r, int resetFlg)
3f1a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3f1b0 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 F: Status Parame
3f1c0 74 65 72 73 20 66 6f 72 20 64 61 74 61 62 61 73 ters for databas
3f1d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 e connections {H
3f1e0 31 37 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 17520} <H17500>.
3f1f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
3f200 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 **.** These cons
3f210 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 tants are the av
3f220 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 ailable integer
3f230 22 76 65 72 62 73 22 20 74 68 61 74 20 63 61 6e "verbs" that can
3f240 20 62 65 20 70 61 73 73 65 64 20 61 73 0a 2a 2a be passed as.**
3f250 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
3f260 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c ment to the [sql
3f270 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 ite3_db_status()
3f280 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a ] interface..**.
3f290 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 6d 61 79 ** New verbs may
3f2a0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
3f2b0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
3f2c0 53 51 4c 69 74 65 2e 20 45 78 69 73 74 69 6e 67 SQLite. Existing
3f2d0 20 76 65 72 62 73 0a 2a 2a 20 6d 69 67 68 74 20 verbs.** might
3f2e0 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e be discontinued.
3f2f0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 Applications sh
3f300 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 ould check the r
3f310 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 0a eturn code from.
3f320 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 ** [sqlite3_db_s
3f330 74 61 74 75 73 28 29 5d 20 74 6f 20 6d 61 6b 65 tatus()] to make
3f340 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63 sure that the c
3f350 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a 2a 2a 20 54 all worked..** T
3f360 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 he [sqlite3_db_s
3f370 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 tatus()] interfa
3f380 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 ce will return a
3f390 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72 6f 72 20 non-zero error
3f3a0 63 6f 64 65 0a 2a 2a 20 69 66 20 61 20 64 69 73 code.** if a dis
3f3b0 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 continued or uns
3f3c0 75 70 70 6f 72 74 65 64 20 76 65 72 62 20 69 73 upported verb is
3f3d0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 invoked..**.**
3f3e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 <dl>.** <dt>SQLI
3f3f0 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b TE_DBSTATUS_LOOK
3f400 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e 0a ASIDE_USED</dt>.
3f410 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3f420 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 meter returns th
3f430 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b e number of look
3f440 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f aside memory slo
3f450 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 ts currently.**
3f460 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 checked out.</dd
3f470 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 >.** </dl>.*/.#d
3f480 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 efine SQLITE_DBS
3f490 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f TATUS_LOOKASIDE_
3f4a0 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a 0a USED 0.../*.
3f4b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72 65 ** CAPI3REF: Pre
3f4c0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
3f4d0 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d 20 Status {H17550}
3f4e0 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 <S60200>.** EXPE
3f4f0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 RIMENTAL.**.** E
3f500 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74 61 ach prepared sta
3f510 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e 73 tement maintains
3f520 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51 4c various.** [SQL
3f530 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 ITE_STMTSTATUS_S
3f540 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d 20 ORT | counters]
3f550 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 65 that measure the
3f560 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 69 number.** of ti
3f570 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 6f mes it has perfo
3f580 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f 70 rmed specific op
3f590 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73 65 erations. These
3f5a0 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a 2a counters can.**
3f5b0 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e 69 be used to moni
3f5c0 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d 61 tor the performa
3f5d0 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 nce characterist
3f5e0 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70 61 ics of the prepa
3f5f0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 red.** statement
3f600 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c s. For example,
3f610 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f if the number o
3f620 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67 72 f table steps gr
3f630 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a 2a eatly exceeds.**
3f640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
3f650 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f 72 able searches or
3f660 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74 68 result rows, th
3f670 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74 6f at would tend to
3f680 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 61 indicate.** tha
3f690 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 t the prepared s
3f6a0 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69 6e tatement is usin
3f6b0 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 g a full table s
3f6c0 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a can rather than.
3f6d0 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a 2a ** an index. .*
3f6e0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
3f6f0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 ace is used to r
3f700 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73 65 etrieve and rese
3f710 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 t counter values
3f720 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65 70 from.** a [prep
3f730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
3f740 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 The first argu
3f750 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 70 ment is the prep
3f760 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ared statement.*
3f770 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 * object to be i
3f780 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68 nterrogated. Th
3f790 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
3f7a0 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 t.** is an integ
3f7b0 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73 70 er code for a sp
3f7c0 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f 53 ecific [SQLITE_S
3f7d0 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 7c TMTSTATUS_SORT |
3f7e0 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f 20 counter].** to
3f7f0 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e be interrogated.
3f800 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 .** The current
3f810 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 value of the re
3f820 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20 quested counter
3f830 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
3f840 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 20 If the resetFlg
3f850 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
3f860 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 73 e counter is res
3f870 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 et to zero after
3f880 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 this.** interfa
3f890 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e ce call returns.
3f8a0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
3f8b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
3f8c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
3f8d0 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a _db_status()]..*
3f8e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
3f8f0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
3f900 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d int sqlite3_stm
3f910 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 t_status(sqlite3
3f920 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 69 _stmt*, int op,i
3f930 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f nt resetFlg);../
3f940 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
3f950 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 tatus Parameters
3f960 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73 74 for prepared st
3f970 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37 30 atements {H17570
3f980 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45 58 } <H17550>.** EX
3f990 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
3f9a0 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 73 These preproces
3f9b0 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e sor macros defin
3f9c0 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 e integer codes
3f9d0 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 65 that name counte
3f9e0 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 6f r.** values asso
3f9f0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
3fa00 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 [sqlite3_stmt_st
3fa10 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 atus()] interfac
3fa20 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e e..** The meanin
3fa30 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 gs of the variou
3fa40 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 61 s counters are a
3fa50 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
3fa60 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c <dl>.** <dt>SQL
3fa70 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 ITE_STMTSTATUS_F
3fa80 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64 74 ULLSCAN_STEP</dt
3fa90 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 >.** <dd>This is
3faa0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
3fab0 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74 65 imes that SQLite
3fac0 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f 72 has stepped for
3fad0 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61 62 ward in.** a tab
3fae0 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 le as part of a
3faf0 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e full table scan.
3fb00 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73 20 Large numbers
3fb10 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65 72 for this counter
3fb20 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74 65 .** may indicate
3fb30 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20 66 opportunities f
3fb40 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 or performance i
3fb50 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f 75 mprovement throu
3fb60 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20 75 gh .** careful u
3fb70 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f se of indices.</
3fb80 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3fb90 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
3fba0 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SORT</dt>.** <dd
3fbb0 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d >This is the num
3fbc0 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65 72 ber of sort oper
3fbd0 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 ations that have
3fbe0 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41 20 occurred..** A
3fbf0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69 non-zero value i
3fc00 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6d n this counter m
3fc10 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20 6f ay indicate an o
3fc20 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a 2a pportunity to.**
3fc30 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65 72 improvement per
3fc40 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67 68 formance through
3fc50 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20 careful use of
3fc60 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a indices.</dd>.**
3fc70 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 .** </dl>.*/.#de
3fc80 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 fine SQLITE_STMT
3fc90 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f STATUS_FULLSCAN_
3fca0 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66 69 STEP 1.#defi
3fcb0 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 ne SQLITE_STMTST
3fcc0 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20 20 ATUS_SORT
3fcd0 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 2../*.**
3fce0 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f 6d CAPI3REF: Custom
3fcf0 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a 65 Page Cache Obje
3fd00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 ct.** EXPERIMENT
3fd10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c AL.**.** The sql
3fd20 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70 65 ite3_pcache type
3fd30 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74 20 is opaque. It
3fd40 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 is implemented b
3fd50 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 61 62 y.** the pluggab
3fd60 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 le module. The
3fd70 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 73 20 SQLite core has
3fd80 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 0a no knowledge of.
3fd90 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 20 69 ** its size or i
3fda0 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72 nternal structur
3fdb0 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 61 6c e and never deal
3fdc0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 71 s with the.** sq
3fdd0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f 62 6a lite3_pcache obj
3fde0 65 63 74 20 65 78 63 65 70 74 20 62 79 20 68 6f ect except by ho
3fdf0 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 69 6e lding and passin
3fe00 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 74 6f g pointers.** to
3fe10 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a the object..**.
3fe20 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 33 5f ** See [sqlite3_
3fe30 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 pcache_methods]
3fe40 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
3fe50 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
3fe60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
3fe70 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 71 6c lite3_pcache sql
3fe80 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a 2f 2a ite3_pcache;../*
3fe90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 70 .** CAPI3REF: Ap
3fea0 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e 65 plication Define
3feb0 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a 2a 2a d Page Cache..**
3fec0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 61 67 65 KEYWORDS: {page
3fed0 20 63 61 63 68 65 7d 0a 2a 2a 20 45 58 50 45 52 cache}.** EXPER
3fee0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
3fef0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 e [sqlite3_confi
3ff00 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 g]([SQLITE_CONFI
3ff10 47 5f 50 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20 G_PCACHE], ...)
3ff20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a interface can.**
3ff30 20 72 65 67 69 73 74 65 72 20 61 6e 20 61 6c 74 register an alt
3ff40 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61 ernative page ca
3ff50 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
3ff60 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 20 69 6e on by passing in
3ff70 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 an .** instance
3ff80 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
3ff90 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 pcache_methods s
3ffa0 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 6d 61 tructure. The ma
3ffb0 6a 6f 72 69 74 79 20 6f 66 20 74 68 65 20 0a 2a jority of the .*
3ffc0 2a 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75 73 * heap memory us
3ffd0 65 64 20 62 79 20 53 51 4c 69 74 65 20 69 73 20 ed by SQLite is
3ffe0 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65 used by the page
3fff0 20 63 61 63 68 65 20 74 6f 20 63 61 63 68 65 20 cache to cache
40000 64 61 74 61 20 72 65 61 64 20 0a 2a 2a 20 66 72 data read .** fr
40010 6f 6d 2c 20 6f 72 20 72 65 61 64 79 20 74 6f 20 om, or ready to
40020 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 74 be written to, t
40030 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
40040 2e 20 42 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e . By implementin
40050 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 g a .** custom p
40060 61 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 20 age cache using
40070 74 68 69 73 20 41 50 49 2c 20 61 6e 20 61 70 70 this API, an app
40080 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e lication can con
40090 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72 trol more .** pr
400a0 65 63 69 73 65 6c 79 20 74 68 65 20 61 6d 6f 75 ecisely the amou
400b0 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e nt of memory con
400c0 73 75 6d 65 64 20 62 79 20 53 51 4c 69 74 65 2c sumed by SQLite,
400d0 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 63 the way in whic
400e0 68 20 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72 h .** that memor
400f0 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 y is allocated a
40100 6e 64 20 72 65 6c 65 61 73 65 64 2c 20 61 6e 64 nd released, and
40110 20 74 68 65 20 70 6f 6c 69 63 69 65 73 20 75 73 the policies us
40120 65 64 20 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d ed to .** determ
40130 69 6e 65 20 65 78 61 63 74 6c 79 20 77 68 69 63 ine exactly whic
40140 68 20 70 61 72 74 73 20 6f 66 20 61 20 64 61 74 h parts of a dat
40150 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 63 abase file are c
40160 61 63 68 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a ached and for .*
40170 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a * how long..**.*
40180 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f * The contents o
40190 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
401a0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72 ache_methods str
401b0 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 65 ucture are copie
401c0 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 74 65 72 d to an.** inter
401d0 6e 61 6c 20 62 75 66 66 65 72 20 62 79 20 53 51 nal buffer by SQ
401e0 4c 69 74 65 20 77 69 74 68 69 6e 20 74 68 65 20 Lite within the
401f0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
40200 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 6e 63 65 _config]. Hence
40210 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 .** the applicat
40220 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 72 64 20 ion may discard
40230 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 61 66 the parameter af
40240 74 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a ter the call to.
40250 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ** [sqlite3_conf
40260 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e 0a 2a ig()] returns..*
40270 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28 29 *.** The xInit()
40280 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 method is calle
40290 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 d once for each
402a0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
402b0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a 2a _initialize()].*
402c0 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 * (usually only
402d0 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 once during the
402e0 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 lifetime of the
402f0 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73 20 process). It is
40300 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70 79 passed.** a copy
40310 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
40320 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e 70 pcache_methods.p
40330 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63 61 Arg value. It ca
40340 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 n be used to set
40350 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73 74 .** up global st
40360 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75 74 ructures and mut
40370 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62 79 exes required by
40380 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67 65 the custom page
40390 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 cache .** imple
403a0 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a mentation. .**.*
403b0 2a 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 28 * The xShutdown(
403c0 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c ) method is call
403d0 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 5b ed from within [
403e0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
403f0 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 ()], .** if the
40400 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f application invo
40410 6b 65 73 20 74 68 69 73 20 41 50 49 2e 20 49 74 kes this API. It
40420 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
40430 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 61 6e 79 clean up .** any
40440 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 73 outstanding res
40450 6f 75 72 63 65 73 20 62 65 66 6f 72 65 20 70 72 ources before pr
40460 6f 63 65 73 73 20 73 68 75 74 64 6f 77 6e 2c 20 ocess shutdown,
40470 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a if required..**.
40480 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64 73 20 ** SQLite holds
40490 61 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f a [SQLITE_MUTEX_
404a0 52 45 43 55 52 53 49 56 45 5d 20 6d 75 74 65 78 RECURSIVE] mutex
404b0 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 when it invokes
404c0 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65 .** the xInit me
404d0 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e thod, so the xIn
404e0 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e it method need n
404f0 6f 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 ot be threadsafe
40500 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 . The.** xShutd
40510 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e own method is on
40520 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b ly called from [
40530 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
40540 28 29 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a ()] so it does.*
40550 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 * not need to be
40560 20 74 68 72 65 61 64 73 61 66 65 20 65 69 74 68 threadsafe eith
40570 65 72 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 6d er. All other m
40580 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 ethods must be t
40590 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 69 6e 20 hreadsafe.** in
405a0 6d 75 6c 74 69 74 68 72 65 61 64 65 64 20 61 70 multithreaded ap
405b0 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a plications..**.*
405c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 * SQLite will ne
405d0 76 65 72 20 69 6e 76 6f 6b 65 20 78 49 6e 69 74 ver invoke xInit
405e0 28 29 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 () more than onc
405f0 65 20 77 69 74 68 6f 75 74 20 61 6e 20 69 6e 74 e without an int
40600 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 61 6c 6c ervening.** call
40610 20 74 6f 20 78 53 68 75 74 64 6f 77 6e 28 29 2e to xShutdown().
40620 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 .**.** The xCrea
40630 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 te() method is u
40640 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 sed to construct
40650 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73 a new cache ins
40660 74 61 6e 63 65 2e 20 20 53 51 4c 69 74 65 0a 2a tance. SQLite.*
40670 2a 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 * will typically
40680 20 63 72 65 61 74 65 20 6f 6e 65 20 63 61 63 68 create one cach
40690 65 20 69 6e 73 74 61 6e 63 65 20 66 6f 72 20 65 e instance for e
406a0 61 63 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73 ach open databas
406b0 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 68 6f 75 67 e file,.** thoug
406c0 68 20 74 68 69 73 20 69 73 20 6e 6f 74 20 67 75 h this is not gu
406d0 61 72 61 6e 74 65 65 64 2e 20 54 68 65 0a 2a 2a aranteed. The.**
406e0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
406f0 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74 68 65 , szPage, is the
40700 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
40710 66 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74 f the pages that
40720 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f must.** be allo
40730 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61 63 cated by the cac
40740 68 65 2e 20 20 73 7a 50 61 67 65 20 77 69 6c 6c he. szPage will
40750 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72 20 not be a power
40760 6f 66 20 74 77 6f 2e 20 20 73 7a 50 61 67 65 0a of two. szPage.
40770 2a 2a 20 77 69 6c 6c 20 74 68 65 20 70 61 67 65 ** will the page
40780 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
40790 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
407a0 69 73 20 74 6f 20 62 65 20 63 61 63 68 65 64 20 is to be cached
407b0 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 6e 63 72 65 plus an.** incre
407c0 6d 65 6e 74 20 28 68 65 72 65 20 63 61 6c 6c 65 ment (here calle
407d0 64 20 22 52 22 29 20 6f 66 20 61 62 6f 75 74 20 d "R") of about
407e0 31 30 30 20 6f 72 20 32 30 30 2e 20 20 53 51 4c 100 or 200. SQL
407f0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
40800 0a 2a 2a 20 65 78 74 72 61 20 52 20 62 79 74 65 .** extra R byte
40810 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 74 s on each page t
40820 6f 20 73 74 6f 72 65 20 6d 65 74 61 64 61 74 61 o store metadata
40830 20 61 62 6f 75 74 20 74 68 65 20 75 6e 64 65 72 about the under
40840 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73 lying.** databas
40850 65 20 70 61 67 65 20 6f 6e 20 64 69 73 6b 2e 20 e page on disk.
40860 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 52 20 The value of R
40870 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 depends.** on th
40880 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e e SQLite version
40890 2c 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 , the target pla
408a0 74 66 6f 72 6d 2c 20 61 6e 64 20 68 6f 77 20 53 tform, and how S
408b0 51 4c 69 74 65 20 77 61 73 20 63 6f 6d 70 69 6c QLite was compil
408c0 65 64 2e 0a 2a 2a 20 52 20 69 73 20 63 6f 6e 73 ed..** R is cons
408d0 74 61 6e 74 20 66 6f 72 20 61 20 70 61 72 74 69 tant for a parti
408e0 63 75 6c 61 72 20 62 75 69 6c 64 20 6f 66 20 53 cular build of S
408f0 51 4c 69 74 65 2e 20 20 54 68 65 20 73 65 63 6f QLite. The seco
40900 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a nd argument to.*
40910 2a 20 78 43 72 65 61 74 65 28 29 2c 20 62 50 75 * xCreate(), bPu
40920 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 rgeable, is true
40930 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65 if the cache be
40940 69 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c ing created will
40950 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 .** be used to c
40960 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61 ache database pa
40970 67 65 73 20 6f 66 20 61 20 66 69 6c 65 20 73 74 ges of a file st
40980 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c 20 6f 72 ored on disk, or
40990 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 .** false if it
409a0 69 73 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 is used for an i
409b0 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
409c0 65 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 e. The cache imp
409d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 64 lementation.** d
409e0 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 oes not have to
409f0 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 do anything spec
40a00 69 61 6c 20 62 61 73 65 64 20 77 69 74 68 20 74 ial based with t
40a10 68 65 20 76 61 6c 75 65 20 6f 66 20 62 50 75 72 he value of bPur
40a20 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 20 69 73 geable;.** it is
40a30 20 70 75 72 65 6c 79 20 61 64 76 69 73 6f 72 79 purely advisory
40a40 2e 20 20 4f 6e 20 61 20 63 61 63 68 65 20 77 68 . On a cache wh
40a50 65 72 65 20 62 50 75 72 67 65 61 62 6c 65 20 69 ere bPurgeable i
40a60 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 74 65 20 s false, SQLite
40a70 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 69 6e will.** never in
40a80 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 20 65 78 voke xUnpin() ex
40a90 63 65 70 74 20 74 6f 20 64 65 6c 69 62 65 72 61 cept to delibera
40aa0 74 65 6c 79 20 64 65 6c 65 74 65 20 61 20 70 61 tely delete a pa
40ab0 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 ge..** In other
40ac0 77 6f 72 64 73 2c 20 61 20 63 61 63 68 65 20 63 words, a cache c
40ad0 72 65 61 74 65 64 20 77 69 74 68 20 62 50 75 72 reated with bPur
40ae0 67 65 61 62 6c 65 20 73 65 74 20 74 6f 20 66 61 geable set to fa
40af0 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 lse will.** neve
40b00 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79 20 75 6e r contain any un
40b10 70 69 6e 6e 65 64 20 70 61 67 65 73 2e 0a 2a 2a pinned pages..**
40b20 0a 2a 2a 20 54 68 65 20 78 43 61 63 68 65 73 69 .** The xCachesi
40b30 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 ze() method may
40b40 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79 be called at any
40b50 20 74 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20 time by SQLite
40b60 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75 to set the.** su
40b70 67 67 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20 ggested maximum
40b80 63 61 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62 cache-size (numb
40b90 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 er of pages stor
40ba0 65 64 20 62 79 29 20 74 68 65 20 63 61 63 68 65 ed by) the cache
40bb0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73 .** instance pas
40bc0 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
40bd0 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 argument. This
40be0 69 73 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e is the value con
40bf0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a figured using.**
40c00 20 74 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52 the SQLite "[PR
40c10 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d AGMA cache_size]
40c20 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69 " command. As wi
40c30 74 68 20 74 68 65 20 62 50 75 72 67 65 61 62 6c th the bPurgeabl
40c40 65 20 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20 e parameter,.**
40c50 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
40c60 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 on is not requir
40c70 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e ed to do anythin
40c80 67 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 76 g with this.** v
40c90 61 6c 75 65 3b 20 69 74 20 69 73 20 61 64 76 69 alue; it is advi
40ca0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a sory only..**.**
40cb0 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 28 The xPagecount(
40cc0 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 ) method should
40cd0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
40ce0 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65 r of pages curre
40cf0 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69 ntly.** stored i
40d00 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 20 n the cache..**
40d10 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 .** The xFetch()
40d20 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 method is used
40d30 74 6f 20 66 65 74 63 68 20 61 20 70 61 67 65 20 to fetch a page
40d40 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 and return a poi
40d50 6e 74 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20 nter to it. .**
40d60 41 20 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69 A 'page', in thi
40d70 73 20 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20 s context, is a
40d80 62 75 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65 buffer of szPage
40d90 20 62 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61 bytes aligned a
40da0 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62 t an.** 8-byte b
40db0 6f 75 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67 oundary. The pag
40dc0 65 20 74 6f 20 62 65 20 66 65 74 63 68 65 64 20 e to be fetched
40dd0 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
40de0 20 74 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a the key. The.**
40df0 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c mimimum key val
40e00 75 65 20 69 73 20 31 2e 20 41 66 74 65 72 20 69 ue is 1. After i
40e10 74 20 68 61 73 20 62 65 65 6e 20 72 65 74 72 69 t has been retri
40e20 65 76 65 64 20 75 73 69 6e 67 20 78 46 65 74 63 eved using xFetc
40e30 68 2c 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 h, the page .**
40e40 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f is considered to
40e50 20 62 65 20 22 70 69 6e 6e 65 64 22 2e 0a 2a 2a be "pinned"..**
40e60 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 .** If the reque
40e70 73 74 65 64 20 70 61 67 65 20 69 73 20 61 6c 72 sted page is alr
40e80 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65 eady in the page
40e90 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 cache, then the
40ea0 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 69 page cache.** i
40eb0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 mplementation mu
40ec0 73 74 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e st return a poin
40ed0 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 ter to the page
40ee0 62 75 66 66 65 72 20 77 69 74 68 20 69 74 73 20 buffer with its
40ef0 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e 74 61 63 content.** intac
40f00 74 2e 20 20 49 66 20 74 68 65 20 72 65 71 75 65 t. If the reque
40f10 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 sted page is not
40f20 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
40f30 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 0a cache, then the.
40f40 2a 2a 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 ** behavior of t
40f50 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 he cache impleme
40f60 6e 74 61 74 69 6f 6e 20 69 73 20 64 65 74 65 72 ntation is deter
40f70 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76 61 6c mined by the val
40f80 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 72 65 ue of the.** cre
40f90 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 ateFlag paramete
40fa0 72 20 70 61 73 73 65 64 20 74 6f 20 78 46 65 74 r passed to xFet
40fb0 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ch, according to
40fc0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
40fd0 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 74 61 62 able:.**.** <tab
40fe0 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 69 64 74 le border=1 widt
40ff0 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 65 6e 74 h=85% align=cent
41000 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 er>.** <tr><th>
41010 63 72 65 61 74 65 46 6c 61 67 20 3c 74 68 3e 20 createFlag <th>
41020 42 65 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70 Behaviour when p
41030 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
41040 64 79 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 3c dy in cache.** <
41050 74 72 3e 3c 74 64 3e 20 30 20 3c 74 64 3e 20 44 tr><td> 0 <td> D
41060 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61 o not allocate a
41070 20 6e 65 77 20 70 61 67 65 2e 20 20 52 65 74 75 new page. Retu
41080 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e rn NULL..** <tr>
41090 3c 74 64 3e 20 31 20 3c 74 64 3e 20 41 6c 6c 6f <td> 1 <td> Allo
410a0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
410b0 69 66 20 69 74 20 65 61 73 79 20 61 6e 64 20 63 if it easy and c
410c0 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20 onvenient to do
410d0 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 so..**
410e0 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 Otherwise
410f0 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a return NULL..**
41100 20 3c 74 72 3e 3c 74 64 3e 20 32 20 3c 74 64 3e <tr><td> 2 <td>
41110 20 4d 61 6b 65 20 65 76 65 72 79 20 65 66 66 6f Make every effo
41120 72 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 rt to allocate a
41130 20 6e 65 77 20 70 61 67 65 2e 20 20 4f 6e 6c 79 new page. Only
41140 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 return.**
41150 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 NULL
41160 69 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 if allocating a
41170 6e 65 77 20 70 61 67 65 20 69 73 20 65 66 66 65 new page is effe
41180 63 74 69 76 65 6c 79 20 69 6d 70 6f 73 73 69 62 ctively impossib
41190 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a le..** </table>.
411a0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
411b0 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 76 6f 6b l normally invok
411c0 65 20 78 46 65 74 63 68 28 29 20 77 69 74 68 20 e xFetch() with
411d0 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 20 a createFlag of
411e0 30 20 6f 72 20 31 2e 20 20 49 66 0a 2a 2a 20 61 0 or 1. If.** a
411f0 20 63 61 6c 6c 20 74 6f 20 78 46 65 74 63 68 28 call to xFetch(
41200 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 ) with createFla
41210 67 3d 3d 31 20 72 65 74 75 72 6e 73 20 4e 55 4c g==1 returns NUL
41220 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 77 L, then SQLite w
41230 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 ill.** attempt t
41240 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f 72 20 6d o unpin one or m
41250 6f 72 65 20 63 61 63 68 65 20 70 61 67 65 73 20 ore cache pages
41260 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 68 65 20 by spilling the
41270 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 70 69 content of.** pi
41280 6e 6e 65 64 20 70 61 67 65 73 20 74 6f 20 64 69 nned pages to di
41290 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 6e 67 20 sk and synching
412a0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
412b0 73 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 2e stem disk cache.
412c0 20 41 66 74 65 72 0a 2a 2a 20 61 74 74 65 6d 70 After.** attemp
412d0 74 69 6e 67 20 74 6f 20 75 6e 70 69 6e 20 70 61 ting to unpin pa
412e0 67 65 73 2c 20 74 68 65 20 78 46 65 74 63 68 28 ges, the xFetch(
412f0 29 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 62 65 ) method will be
41300 20 69 6e 76 6f 6b 65 64 20 61 67 61 69 6e 20 77 invoked again w
41310 69 74 68 0a 2a 2a 20 61 20 63 72 65 61 74 65 46 ith.** a createF
41320 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 lag of 2..**.**
41330 78 55 6e 70 69 6e 28 29 20 69 73 20 63 61 6c 6c xUnpin() is call
41340 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 74 ed by SQLite wit
41350 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 h a pointer to a
41360 20 63 75 72 72 65 6e 74 6c 79 20 70 69 6e 6e 65 currently pinne
41370 64 20 70 61 67 65 0a 2a 2a 20 61 73 20 69 74 73 d page.** as its
41380 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
41390 2e 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 . If the third p
413a0 61 72 61 6d 65 74 65 72 2c 20 64 69 73 63 61 72 arameter, discar
413b0 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a d, is non-zero,.
413c0 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 ** then the page
413d0 20 73 68 6f 75 6c 64 20 62 65 20 65 76 69 63 74 should be evict
413e0 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ed from the cach
413f0 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
41400 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73 73 75 6d SQLite .** assum
41410 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 es that the next
41420 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20 69 time the page i
41430 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f 6d s retrieved from
41440 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e 67 the cache using
41450 0a 2a 2a 20 74 68 65 20 78 46 65 74 63 68 28 29 .** the xFetch()
41460 20 6d 65 74 68 6f 64 2c 20 69 74 20 77 69 6c 6c method, it will
41470 20 62 65 20 7a 65 72 6f 65 64 2e 20 49 66 20 74 be zeroed. If t
41480 68 65 20 64 69 73 63 61 72 64 20 70 61 72 61 6d he discard param
41490 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 2c eter is.** zero,
414a0 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 then the page i
414b0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 s considered to
414c0 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 be unpinned. The
414d0 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
414e0 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 63 68 6f ation.** may cho
414f0 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75 6e 70 ose to evict unp
41500 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74 20 61 inned pages at a
41510 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 ny time..**.** T
41520 68 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 he cache is not
41530 72 65 71 75 69 72 65 64 20 74 6f 20 70 65 72 66 required to perf
41540 6f 72 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63 orm any referenc
41550 65 20 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69 e counting. A si
41560 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f ngle .** call to
41570 20 78 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73 xUnpin() unpins
41580 20 74 68 65 20 70 61 67 65 20 72 65 67 61 72 64 the page regard
41590 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 less of the numb
415a0 65 72 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c er of prior call
415b0 73 20 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28 s .** to xFetch(
415c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65 )..**.** The xRe
415d0 6b 65 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 key() method is
415e0 75 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 used to change t
415f0 68 65 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73 he key value ass
41600 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
41610 0a 2a 2a 20 70 61 67 65 20 70 61 73 73 65 64 20 .** page passed
41620 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
41630 67 75 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b gument from oldK
41640 65 79 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66 ey to newKey. If
41650 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72 the cache.** pr
41660 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e eviously contain
41670 73 20 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63 s an entry assoc
41680 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 iated with newKe
41690 79 2c 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a y, it should be.
416a0 2a 2a 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e ** discarded. An
416b0 79 20 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e y prior cache en
416c0 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 try associated w
416d0 69 74 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75 ith newKey is gu
416e0 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20 aranteed not.**
416f0 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a to be pinned..**
41700 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 .** When SQLite
41710 63 61 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63 calls the xTrunc
41720 61 74 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68 ate() method, th
41730 65 20 63 61 63 68 65 20 6d 75 73 74 20 64 69 73 e cache must dis
41740 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73 card all.** exis
41750 74 69 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69 ting cache entri
41760 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d es with page num
41770 62 65 72 73 20 28 6b 65 79 73 29 20 67 72 65 61 bers (keys) grea
41780 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
41790 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75 l.** to the valu
417a0 65 20 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20 e of the iLimit
417b0 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 parameter passed
417c0 20 74 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e to xTruncate().
417d0 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 If any.** of th
417e0 65 73 65 20 70 61 67 65 73 20 61 72 65 20 70 69 ese pages are pi
417f0 6e 6e 65 64 2c 20 74 68 65 79 20 61 72 65 20 69 nned, they are i
41800 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e mplicitly unpinn
41810 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 ed, meaning that
41820 0a 2a 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20 .** they can be
41830 73 61 66 65 6c 79 20 64 69 73 63 61 72 64 65 64 safely discarded
41840 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73 ..**.** The xDes
41850 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 troy() method is
41860 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 used to delete
41870 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 a cache allocate
41880 64 20 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a d by xCreate()..
41890 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 ** All resources
418a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
418b0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 the specified c
418c0 61 63 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 ache should be f
418d0 72 65 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63 reed. After.** c
418e0 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 alling the xDest
418f0 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 roy() method, SQ
41900 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 Lite considers t
41910 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 he [sqlite3_pcac
41920 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 he*].** handle i
41930 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c nvalid, and will
41940 20 6e 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 not use it with
41950 20 61 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 any other sqlit
41960 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
41970 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a s.** functions..
41980 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
41990 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
419a0 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 _methods sqlite3
419b0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b _pcache_methods;
419c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
419d0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b pcache_methods {
419e0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 . void *pArg;.
419f0 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f int (*xInit)(vo
41a00 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 id*);. void (*x
41a10 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 Shutdown)(void*)
41a20 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 ;. sqlite3_pcac
41a30 68 65 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 he *(*xCreate)(i
41a40 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 nt szPage, int b
41a50 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f Purgeable);. vo
41a60 69 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 id (*xCachesize)
41a70 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
41a80 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 , int nCachesize
41a90 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 );. int (*xPage
41aa0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 count)(sqlite3_p
41ab0 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 cache*);. void
41ac0 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 *(*xFetch)(sqlit
41ad0 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 e3_pcache*, unsi
41ae0 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 gned key, int cr
41af0 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 eateFlag);. voi
41b00 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 d (*xUnpin)(sqli
41b10 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 te3_pcache*, voi
41b20 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 d*, int discard)
41b30 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 ;. void (*xReke
41b40 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 y)(sqlite3_pcach
41b50 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 e*, void*, unsig
41b60 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 ned oldKey, unsi
41b70 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 gned newKey);.
41b80 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 void (*xTruncate
41b90 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 )(sqlite3_pcache
41ba0 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d *, unsigned iLim
41bb0 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 it);. void (*xD
41bc0 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f estroy)(sqlite3_
41bd0 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a pcache*);.};../*
41be0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e .** CAPI3REF: On
41bf0 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a 65 line Backup Obje
41c00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 ct.** EXPERIMENT
41c10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c AL.**.** The sql
41c20 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 ite3_backup obje
41c30 63 74 20 72 65 63 6f 72 64 73 20 73 74 61 74 65 ct records state
41c40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
41c50 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a 2a ut an ongoing.**
41c60 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f online backup o
41c70 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 peration. The s
41c80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 qlite3_backup ob
41c90 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 20 ject is created
41ca0 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 by.** a call to
41cb0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f [sqlite3_backup_
41cc0 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64 init()] and is d
41cd0 65 73 74 72 6f 79 65 64 20 62 79 20 61 20 63 61 estroyed by a ca
41ce0 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 ll to.** [sqlite
41cf0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
41d00 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c )]..**.** See Al
41d10 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 so: [Using the S
41d20 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 QLite Online Bac
41d30 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 70 65 kup API].*/.type
41d40 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
41d50 65 33 5f 62 61 63 6b 75 70 20 73 71 6c 69 74 65 e3_backup sqlite
41d60 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a 2a 2a 3_backup;../*.**
41d70 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e CAPI3REF: Onlin
41d80 65 20 42 61 63 6b 75 70 20 41 50 49 2e 0a 2a 2a e Backup API..**
41d90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
41da0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 .** This API is
41db0 75 73 65 64 20 74 6f 20 6f 76 65 72 77 72 69 74 used to overwrit
41dc0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
41dd0 66 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 77 f one database w
41de0 69 74 68 20 74 68 61 74 0a 2a 2a 20 6f 66 20 61 ith that.** of a
41df0 6e 6f 74 68 65 72 2e 20 49 74 20 69 73 20 75 73 nother. It is us
41e00 65 66 75 6c 20 65 69 74 68 65 72 20 66 6f 72 20 eful either for
41e10 63 72 65 61 74 69 6e 67 20 62 61 63 6b 75 70 73 creating backups
41e20 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 6f 72 of databases or
41e30 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 20 .** for copying
41e40 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
41e50 73 65 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 70 ses to or from p
41e60 65 72 73 69 73 74 65 6e 74 20 66 69 6c 65 73 2e ersistent files.
41e70 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f .**.** See Also
41e80 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c : [Using the SQL
41e90 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 ite Online Backu
41ea0 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 45 78 63 p API].**.** Exc
41eb0 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 69 73 lusive access is
41ec0 20 72 65 71 75 69 72 65 64 20 74 6f 20 74 68 65 required to the
41ed0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
41ee0 61 62 61 73 65 20 66 6f 72 20 74 68 65 20 0a 2a abase for the .*
41ef0 2a 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 * duration of th
41f00 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 48 6f 77 e operation. How
41f10 65 76 65 72 20 74 68 65 20 73 6f 75 72 63 65 20 ever the source
41f20 64 61 74 61 62 61 73 65 20 69 73 20 6f 6e 6c 79 database is only
41f30 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 .** read-locked
41f40 77 68 69 6c 65 20 69 74 20 69 73 20 61 63 74 75 while it is actu
41f50 61 6c 6c 79 20 62 65 69 6e 67 20 72 65 61 64 2c ally being read,
41f60 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 it is not locke
41f70 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 6f 75 73 6c d.** continuousl
41f80 79 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 y for the entire
41f90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 75 73 operation. Thus
41fa0 2c 20 74 68 65 20 62 61 63 6b 75 70 20 6d 61 79 , the backup may
41fb0 20 62 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 be.** performed
41fc0 20 6f 6e 20 61 20 6c 69 76 65 20 64 61 74 61 62 on a live datab
41fd0 61 73 65 20 77 69 74 68 6f 75 74 20 70 72 65 76 ase without prev
41fe0 65 6e 74 69 6e 67 20 6f 74 68 65 72 20 75 73 65 enting other use
41ff0 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 72 69 74 69 rs from.** writi
42000 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
42010 73 65 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64 se for an extend
42020 65 64 20 70 65 72 69 6f 64 20 6f 66 20 74 69 6d ed period of tim
42030 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 70 65 72 e..** .** To per
42040 66 6f 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70 form a backup op
42050 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a 20 20 20 3c eration: .** <
42060 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c ol>.** <li><
42070 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 b>sqlite3_backup
42080 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20 63 _init()</b> is c
42090 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69 6e alled once to in
420a0 69 74 69 61 6c 69 7a 65 20 74 68 65 0a 2a 2a 20 itialize the.**
420b0 20 20 20 20 20 20 20 20 62 61 63 6b 75 70 2c 20 backup,
420c0 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 .** <li><b>s
420d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
420e0 65 70 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c ep()</b> is call
420f0 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 ed one or more t
42100 69 6d 65 73 20 74 6f 20 74 72 61 6e 73 66 65 72 imes to transfer
42110 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 .** the
42120 20 64 61 74 61 20 62 65 74 77 65 65 6e 20 74 68 data between th
42130 65 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 2c e two databases,
42140 20 61 6e 64 20 66 69 6e 61 6c 6c 79 0a 2a 2a 20 and finally.**
42150 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 <li><b>sqlit
42160 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
42170 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 ()</b> is called
42180 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 to release all
42190 72 65 73 6f 75 72 63 65 73 20 0a 2a 2a 20 20 20 resources .**
421a0 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 associated
421b0 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 with the backup
421c0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 0a 2a 2a 20 operation. .**
421d0 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 65 72 65 </ol>.** There
421e0 20 73 68 6f 75 6c 64 20 62 65 20 65 78 61 63 74 should be exact
421f0 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 ly one call to s
42200 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
42210 6e 69 73 68 28 29 20 66 6f 72 20 65 61 63 68 0a nish() for each.
42220 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 ** successful ca
42230 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
42240 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a ckup_init()..**.
42250 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 ** <b>sqlite3_ba
42260 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 0a ckup_init()</b>.
42270 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
42280 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 70 61 two arguments pa
42290 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 ssed to [sqlite3
422a0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 _backup_init()]
422b0 61 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 are the database
422c0 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 .** handle assoc
422d0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 iated with the d
422e0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
422f0 61 73 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 ase and the data
42300 62 61 73 65 20 6e 61 6d 65 20 0a 2a 2a 20 75 73 base name .** us
42310 65 64 20 74 6f 20 61 74 74 61 63 68 20 74 68 65 ed to attach the
42320 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
42330 61 62 61 73 65 20 74 6f 20 74 68 65 20 68 61 6e abase to the han
42340 64 6c 65 2e 20 54 68 65 20 64 61 74 61 62 61 73 dle. The databas
42350 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 22 6d 61 e name.** is "ma
42360 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e in" for the main
42370 20 64 61 74 61 62 61 73 65 2c 20 22 74 65 6d 70 database, "temp
42380 22 20 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 " for the tempor
42390 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72 ary database, or
423a0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 73 70 65 .** the name spe
423b0 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f cified as part o
423c0 66 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 73 f the [ATTACH] s
423d0 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 tatement if the
423e0 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 0a 2a destination is.*
423f0 2a 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 * an attached da
42400 74 61 62 61 73 65 2e 20 54 68 65 20 74 68 69 72 tabase. The thir
42410 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 d and fourth arg
42420 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f uments passed to
42430 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 .** sqlite3_bac
42440 6b 75 70 5f 69 6e 69 74 28 29 20 69 64 65 6e 74 kup_init() ident
42450 69 66 79 20 74 68 65 20 5b 64 61 74 61 62 61 73 ify the [databas
42460 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a e connection].**
42470 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 and database na
42480 6d 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 61 63 me used.** to ac
42490 63 65 73 73 20 74 68 65 20 73 6f 75 72 63 65 20 cess the source
424a0 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 76 61 database. The va
424b0 6c 75 65 73 20 70 61 73 73 65 64 20 66 6f 72 20 lues passed for
424c0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 0a the source and .
424d0 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b ** destination [
424e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
424f0 69 6f 6e 5d 20 70 61 72 61 6d 65 74 65 72 73 20 ion] parameters
42500 6d 75 73 74 20 6e 6f 74 20 62 65 20 74 68 65 20 must not be the
42510 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 same..**.** If a
42520 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
42530 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 62 61 ithin sqlite3_ba
42540 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 74 68 65 ckup_init(), the
42550 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e n NULL is return
42560 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 ed.** and an err
42570 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f or code and erro
42580 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 r message writte
42590 6e 20 69 6e 74 6f 20 74 68 65 20 5b 64 61 74 61 n into the [data
425a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
425b0 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 .** passed as t
425c0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
425d0 74 2e 20 54 68 65 79 20 6d 61 79 20 62 65 20 72 t. They may be r
425e0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 74 etrieved using t
425f0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 he.** [sqlite3_e
42600 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 rrcode()], [sqli
42610 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 te3_errmsg()], a
42620 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d nd [sqlite3_errm
42630 73 67 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e sg16()] function
42640 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c s..** Otherwise,
42650 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 if successful,
42660 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
42670 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d [sqlite3_backup]
42680 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 72 65 object is.** re
42690 74 75 72 6e 65 64 2e 20 54 68 69 73 20 70 6f 69 turned. This poi
426a0 6e 74 65 72 20 6d 61 79 20 62 65 20 75 73 65 64 nter may be used
426b0 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 with the sqlite
426c0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
426d0 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 and.** sqlite3_b
426e0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 ackup_finish() f
426f0 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70 65 72 66 unctions to perf
42700 6f 72 6d 20 74 68 65 20 73 70 65 63 69 66 69 65 orm the specifie
42710 64 20 62 61 63 6b 75 70 20 0a 2a 2a 20 6f 70 65 d backup .** ope
42720 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 3c 62 ration..**.** <b
42730 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f >sqlite3_backup_
42740 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a step()</b>.**.**
42750 20 46 75 6e 63 74 69 6f 6e 20 5b 73 71 6c 69 74 Function [sqlit
42760 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
42770 5d 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 ] is used to cop
42780 79 20 75 70 20 74 6f 20 6e 50 61 67 65 20 70 61 y up to nPage pa
42790 67 65 73 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20 ges between .**
427a0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 the source and d
427b0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
427c0 61 73 65 73 2c 20 77 68 65 72 65 20 6e 50 61 67 ases, where nPag
427d0 65 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f e is the value o
427e0 66 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 f the .** second
427f0 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 parameter passe
42800 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 d to sqlite3_bac
42810 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 6e kup_step(). If n
42820 50 61 67 65 20 69 73 20 61 20 6e 65 67 61 74 69 Page is a negati
42830 76 65 0a 2a 2a 20 76 61 6c 75 65 2c 20 61 6c 6c ve.** value, all
42840 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 63 remaining sourc
42850 65 20 70 61 67 65 73 20 61 72 65 20 63 6f 70 69 e pages are copi
42860 65 64 2e 20 49 66 20 74 68 65 20 72 65 71 75 69 ed. If the requi
42870 72 65 64 20 70 61 67 65 73 20 61 72 65 20 0a 2a red pages are .*
42880 2a 20 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f * succesfully co
42890 70 69 65 64 2c 20 62 75 74 20 74 68 65 72 65 20 pied, but there
428a0 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70 are still more p
428b0 61 67 65 73 20 74 6f 20 63 6f 70 79 20 62 65 66 ages to copy bef
428c0 6f 72 65 20 74 68 65 20 0a 2a 2a 20 62 61 63 6b ore the .** back
428d0 75 70 20 69 73 20 63 6f 6d 70 6c 65 74 65 2c 20 up is complete,
428e0 69 74 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 it returns [SQLI
428f0 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e 6f 20 65 72 TE_OK]. If no er
42900 72 6f 72 20 6f 63 63 75 72 65 64 20 61 6e 64 20 ror occured and
42910 74 68 65 72 65 20 0a 2a 2a 20 61 72 65 20 6e 6f there .** are no
42920 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 more pages to c
42930 6f 70 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 opy, then [SQLIT
42940 45 5f 44 4f 4e 45 5d 20 69 73 20 72 65 74 75 72 E_DONE] is retur
42950 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ned. If an error
42960 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 74 68 65 .** occurs, the
42970 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f n an SQLite erro
42980 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
42990 65 64 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 5b ed. As well as [
429a0 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a SQLITE_OK] and.*
429b0 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c * [SQLITE_DONE],
429c0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
429d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
429e0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c may return [SQL
429f0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a ITE_READONLY],.*
42a00 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d * [SQLITE_NOMEM]
42a10 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c , [SQLITE_BUSY],
42a20 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d [SQLITE_LOCKED]
42a30 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 , or an.** [SQLI
42a40 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 TE_IOERR_ACCESS
42a50 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 | SQLITE_IOERR_X
42a60 58 58 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72 XX] extended err
42a70 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 or code..**.** A
42a80 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 63 61 s well as the ca
42a90 73 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73 se where the des
42aa0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
42ab0 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 e file was opene
42ac0 64 20 66 6f 72 0a 2a 2a 20 72 65 61 64 2d 6f 6e d for.** read-on
42ad0 6c 79 20 61 63 63 65 73 73 2c 20 73 71 6c 69 74 ly access, sqlit
42ae0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
42af0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c may return [SQL
42b00 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 66 ITE_READONLY] if
42b10 0a 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 .** the destinat
42b20 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d ion is an in-mem
42b30 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 74 ory database wit
42b40 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 61 h a different pa
42b50 67 65 20 73 69 7a 65 0a 2a 2a 20 66 72 6f 6d 20 ge size.** from
42b60 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
42b70 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 ase..**.** If sq
42b80 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42b90 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 p() cannot obtai
42ba0 6e 20 61 20 72 65 71 75 69 72 65 64 20 66 69 6c n a required fil
42bb0 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b 2c 20 74 e-system lock, t
42bc0 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 hen.** the [sqli
42bd0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 te3_busy_handler
42be0 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 | busy-handler
42bf0 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 function].** is
42c00 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f 6e 65 20 invoked (if one
42c10 69 73 20 73 70 65 63 69 66 69 65 64 29 2e 20 49 is specified). I
42c20 66 20 74 68 65 20 0a 2a 2a 20 62 75 73 79 2d 68 f the .** busy-h
42c30 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 6e andler returns n
42c40 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72 65 20 74 on-zero before t
42c50 68 65 20 6c 6f 63 6b 20 69 73 20 61 76 61 69 6c he lock is avail
42c60 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b able, then .** [
42c70 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 69 73 20 SQLITE_BUSY] is
42c80 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
42c90 63 61 6c 6c 65 72 2e 20 49 6e 20 74 68 69 73 20 caller. In this
42ca0 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f case the call to
42cb0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
42cc0 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 up_step() can be
42cd0 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20 retried later.
42ce0 49 66 20 74 68 65 20 73 6f 75 72 63 65 0a 2a 2a If the source.**
42cf0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
42d00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 62 65 69 ction].** is bei
42d10 6e 67 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 ng used to write
42d20 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 to the source d
42d30 61 74 61 62 61 73 65 20 77 68 65 6e 20 73 71 6c atabase when sql
42d40 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42d50 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c ().** is called,
42d60 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f then [SQLITE_LO
42d70 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 CKED] is returne
42d80 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 41 d immediately. A
42d90 67 61 69 6e 2c 20 69 6e 20 74 68 69 73 0a 2a 2a gain, in this.**
42da0 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 case the call t
42db0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
42dc0 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72 _step() can be r
42dd0 65 74 72 69 65 64 20 6c 61 74 65 72 20 6f 6e 2e etried later on.
42de0 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 If.** [SQLITE_I
42df0 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 OERR_ACCESS | SQ
42e00 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 2c LITE_IOERR_XXX],
42e10 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c [SQLITE_NOMEM],
42e20 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52 or.** [SQLITE_R
42e30 45 41 44 4f 4e 4c 59 5d 20 69 73 20 72 65 74 75 EADONLY] is retu
42e40 72 6e 65 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 rned, then .** t
42e50 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 here is no point
42e60 20 69 6e 20 72 65 74 72 79 69 6e 67 20 74 68 65 in retrying the
42e70 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
42e80 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
42e90 54 68 65 73 65 20 0a 2a 2a 20 65 72 72 6f 72 73 These .** errors
42ea0 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 are considered
42eb0 66 61 74 61 6c 2e 20 41 74 20 74 68 69 73 20 70 fatal. At this p
42ec0 6f 69 6e 74 20 74 68 65 20 61 70 70 6c 69 63 61 oint the applica
42ed0 74 69 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74 tion must accept
42ee0 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61 .** that the ba
42ef0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 ckup operation h
42f00 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61 as failed and pa
42f10 73 73 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 ss the backup op
42f20 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a eration handle .
42f30 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 ** to the sqlite
42f40 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
42f50 29 20 74 6f 20 72 65 6c 65 61 73 65 20 61 73 73 ) to release ass
42f60 6f 63 69 61 74 65 64 20 72 65 73 6f 75 72 63 65 ociated resource
42f70 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c 6f 77 69 s..**.** Followi
42f80 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c ng the first cal
42f90 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
42fa0 6b 75 70 5f 73 74 65 70 28 29 2c 20 61 6e 20 65 kup_step(), an e
42fb0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 xclusive lock is
42fc0 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 .** obtained on
42fd0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
42fe0 66 69 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20 file. It is not
42ff0 72 65 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65 released until e
43000 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 ither .** sqlite
43010 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
43020 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74 ) is called or t
43030 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
43040 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 ion is complete
43050 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f .** and sqlite3_
43060 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65 backup_step() re
43070 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f turns [SQLITE_DO
43080 4e 45 5d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c NE]. Additionall
43090 79 2c 20 65 61 63 68 20 74 69 6d 65 20 0a 2a 2a y, each time .**
430a0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
430b0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
430c0 20 69 73 20 6d 61 64 65 20 61 20 5b 73 68 61 72 is made a [shar
430d0 65 64 20 6c 6f 63 6b 5d 20 69 73 20 6f 62 74 61 ed lock] is obta
430e0 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 73 ined on.** the s
430f0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 ource database f
43100 69 6c 65 2e 20 54 68 69 73 20 6c 6f 63 6b 20 69 ile. This lock i
43110 73 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72 s released befor
43120 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 e the.** sqlite3
43130 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 _backup_step() c
43140 61 6c 6c 20 72 65 74 75 72 6e 73 2e 20 42 65 63 all returns. Bec
43150 61 75 73 65 20 74 68 65 20 73 6f 75 72 63 65 20 ause the source
43160 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a database is not.
43170 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65 ** locked betwee
43180 6e 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 n calls to sqlit
43190 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
431a0 2c 20 69 74 20 6d 61 79 20 62 65 20 6d 6f 64 69 , it may be modi
431b0 66 69 65 64 20 6d 69 64 2d 77 61 79 0a 2a 2a 20 fied mid-way.**
431c0 74 68 72 6f 75 67 68 20 74 68 65 20 62 61 63 6b through the back
431d0 75 70 20 70 72 6f 63 65 64 75 72 65 2e 20 49 66 up procedure. If
431e0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
431f0 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 base is modified
43200 20 62 79 20 61 6e 0a 2a 2a 20 65 78 74 65 72 6e by an.** extern
43210 61 6c 20 70 72 6f 63 65 73 73 20 6f 72 20 76 69 al process or vi
43220 61 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e a a database con
43230 6e 65 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 nection other th
43240 61 6e 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67 an the one being
43250 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 .** used by the
43260 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
43270 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 , then the backu
43280 70 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 70 p will be transp
43290 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 65 73 74 61 arently.** resta
432a0 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 78 74 rted by the next
432b0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
432c0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
432d0 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 0a 2a If the source .*
432e0 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f * database is mo
432f0 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 73 dified by the us
43300 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 ing the same dat
43310 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
43320 20 61 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 62 as is used.** b
43330 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 y the backup ope
43340 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ration, then the
43350 20 62 61 63 6b 75 70 20 64 61 74 61 62 61 73 65 backup database
43360 20 69 73 20 74 72 61 6e 73 70 61 72 65 6e 74 6c is transparentl
43370 79 20 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 74 y .** updated at
43380 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a the same time..
43390 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 **.** <b>sqlite3
433a0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
433b0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 </b>.**.** Once
433c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
433d0 74 65 70 28 29 20 68 61 73 20 72 65 74 75 72 6e tep() has return
433e0 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d ed [SQLITE_DONE]
433f0 2c 20 6f 72 20 77 68 65 6e 20 74 68 65 20 0a 2a , or when the .*
43400 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 * application wi
43410 73 68 65 73 20 74 6f 20 61 62 61 6e 64 6f 6e 20 shes to abandon
43420 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
43430 74 69 6f 6e 2c 20 74 68 65 20 5b 73 71 6c 69 74 tion, the [sqlit
43440 65 33 5f 62 61 63 6b 75 70 5d 0a 2a 2a 20 6f 62 e3_backup].** ob
43450 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 70 ject should be p
43460 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
43470 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
43480 2e 20 54 68 69 73 20 72 65 6c 65 61 73 65 73 20 . This releases
43490 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 all.** resources
434a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
434b0 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
434c0 61 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 ation. If sqlite
434d0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 3_backup_step().
434e0 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65 74 20 72 ** has not yet r
434f0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f eturned [SQLITE_
43500 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 61 6e 79 20 DONE], then any
43510 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 active write-tra
43520 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 0a nsaction on the.
43530 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 ** destination d
43540 61 74 61 62 61 73 65 20 69 73 20 72 6f 6c 6c 65 atabase is rolle
43550 64 20 62 61 63 6b 2e 20 54 68 65 20 5b 73 71 6c d back. The [sql
43560 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a ite3_backup] obj
43570 65 63 74 20 69 73 20 69 6e 76 61 6c 69 64 0a 2a ect is invalid.*
43580 2a 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 * and may not be
43590 20 75 73 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 used following
435a0 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
435b0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
435c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c )..**.** The val
435d0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
435e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
435f0 6e 69 73 68 20 69 73 20 5b 53 51 4c 49 54 45 5f nish is [SQLITE_
43600 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 72 6f 72 0a OK] if no error.
43610 2a 2a 20 6f 63 63 75 72 72 65 64 2c 20 72 65 67 ** occurred, reg
43620 61 72 64 6c 65 73 73 20 6f 72 20 77 68 65 74 68 ardless or wheth
43630 65 72 20 6f 72 20 6e 6f 74 20 73 71 6c 69 74 65 er or not sqlite
43640 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
43650 77 61 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 was called.** a
43660 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 sufficient numbe
43670 72 20 6f 66 20 74 69 6d 65 73 20 74 6f 20 63 6f r of times to co
43680 6d 70 6c 65 74 65 20 74 68 65 20 62 61 63 6b 75 mplete the backu
43690 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4f 72 2c p operation. Or,
436a0 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66 if.** an out-of
436b0 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f -memory conditio
436c0 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 n or IO error oc
436d0 63 75 72 65 64 20 64 75 72 69 6e 67 20 61 20 63 cured during a c
436e0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
436f0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
43700 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d then [SQLITE_NOM
43710 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 EM] or an.** [SQ
43720 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
43730 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 S | SQLITE_IOERR
43740 5f 58 58 58 5d 20 65 72 72 6f 72 20 63 6f 64 65 _XXX] error code
43750 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e .** is returned.
43760 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
43770 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 e error code and
43780 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
43790 65 20 61 72 65 0a 2a 2a 20 77 72 69 74 74 65 6e e are.** written
437a0 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 to the destinat
437b0 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f ion [database co
437c0 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a nnection]..**.**
437d0 20 41 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51 A return of [SQ
437e0 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 LITE_BUSY] or [S
437f0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 66 72 QLITE_LOCKED] fr
43800 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 om sqlite3_backu
43810 70 5f 73 74 65 70 28 29 20 69 73 0a 2a 2a 20 6e p_step() is.** n
43820 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 65 ot a permanent e
43830 72 72 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f rror and does no
43840 74 20 61 66 66 65 63 74 20 74 68 65 20 72 65 74 t affect the ret
43850 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 urn value of.**
43860 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
43870 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c inish()..**.** <
43880 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 b>sqlite3_backup
43890 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 73 71 _remaining(), sq
438a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 lite3_backup_pag
438b0 65 63 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a ecount()</b>.**.
438c0 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 ** Each call to
438d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
438e0 74 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 tep() sets two v
438f0 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 74 alues stored int
43900 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 61 6e ernally.** by an
43910 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 [sqlite3_backup
43920 5d 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 75 ] object. The nu
43930 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 mber of pages st
43940 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 ill to be backed
43950 0a 2a 2a 20 75 70 2c 20 77 68 69 63 68 20 6d 61 .** up, which ma
43960 79 20 62 65 20 71 75 65 72 69 65 64 20 62 79 20 y be queried by
43970 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 sqlite3_backup_r
43980 65 6d 61 69 6e 69 6e 67 28 29 2c 20 61 6e 64 20 emaining(), and
43990 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 6e 75 6d the total.** num
439a0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
439b0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
439c0 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 ase file, which
439d0 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 20 62 may be queried b
439e0 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 y.** sqlite3_bac
439f0 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e kup_pagecount().
43a00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
43a10 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 s returned by th
43a20 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 ese functions ar
43a30 65 20 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 62 e only updated b
43a40 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 y.** sqlite3_bac
43a50 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 kup_step(). If t
43a60 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
43a70 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 64 se is modified d
43a80 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a uring a backup.*
43a90 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 * operation, the
43aa0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 n the values are
43ab0 20 6e 6f 74 20 75 70 64 61 74 65 64 20 74 6f 20 not updated to
43ac0 61 63 63 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20 account for any
43ad0 65 78 74 72 61 0a 2a 2a 20 70 61 67 65 73 20 74 extra.** pages t
43ae0 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 75 hat need to be u
43af0 70 64 61 74 65 64 20 6f 72 20 74 68 65 20 73 69 pdated or the si
43b00 7a 65 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 ze of the source
43b10 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
43b20 2a 20 63 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a * changing..**.*
43b30 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20 * <b>Concurrent
43b40 55 73 61 67 65 20 6f 66 20 44 61 74 61 62 61 73 Usage of Databas
43b50 65 20 48 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a e Handles</b>.**
43b60 0a 2a 2a 20 54 68 65 20 73 6f 75 72 63 65 20 5b .** The source [
43b70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
43b80 69 6f 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64 ion] may be used
43b90 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 by the applicat
43ba0 69 6f 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a ion for other.**
43bb0 20 70 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20 purposes while
43bc0 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 a backup operati
43bd0 6f 6e 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f on is underway o
43be0 72 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 r being initiali
43bf0 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 69 74 zed..** If SQLit
43c00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e e is compiled an
43c10 64 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 d configured to
43c20 73 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 support threadsa
43c30 66 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 fe database.** c
43c40 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e onnections, then
43c50 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
43c60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
43c70 6d 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63 may be used conc
43c80 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d urrently.** from
43c90 20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68 within other th
43ca0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 reads..**.** How
43cb0 65 76 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63 ever, the applic
43cc0 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 ation must guara
43cd0 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 64 65 ntee that the de
43ce0 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
43cf0 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
43d00 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 70 handle is not p
43d10 61 73 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68 assed to any oth
43d20 65 72 20 41 50 49 20 28 62 79 20 61 6e 79 20 74 er API (by any t
43d30 68 72 65 61 64 29 20 61 66 74 65 72 20 0a 2a 2a hread) after .**
43d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
43d50 69 6e 69 74 28 29 20 69 73 20 63 61 6c 6c 65 64 init() is called
43d60 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20 and before the
43d70 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 61 corresponding ca
43d80 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
43d90 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
43da0 2e 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79 20 . Unfortunately
43db0 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 SQLite does not
43dc0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 0a currently check.
43dd0 2a 2a 20 66 6f 72 20 74 68 69 73 2c 20 69 66 20 ** for this, if
43de0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
43df0 64 6f 65 73 20 75 73 65 20 74 68 65 20 64 65 73 does use the des
43e00 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 tination [databa
43e10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a se connection].*
43e20 2a 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 * for some other
43e30 20 70 75 72 70 6f 73 65 20 64 75 72 69 6e 67 20 purpose during
43e40 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 a backup operati
43e50 6f 6e 2c 20 74 68 69 6e 67 73 20 6d 61 79 20 61 on, things may a
43e60 70 70 65 61 72 20 74 6f 0a 2a 2a 20 77 6f 72 6b ppear to.** work
43e70 20 63 6f 72 72 65 63 74 6c 79 20 62 75 74 20 69 correctly but i
43e80 6e 20 66 61 63 74 20 62 65 20 73 75 62 74 6c 79 n fact be subtly
43e90 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 69 6e 67 2e malfunctioning.
43ea0 20 20 55 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 Use of the.**
43eb0 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
43ec0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
43ed0 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 while a backup i
43ee0 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69 s in progress mi
43ef0 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 63 61 75 73 ght.** also caus
43f00 65 20 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f e a mutex deadlo
43f10 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65 ck..**.** Furthe
43f20 72 6d 6f 72 65 2c 20 69 66 20 72 75 6e 6e 69 6e rmore, if runnin
43f30 67 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63 g in [shared cac
43f40 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70 he mode], the ap
43f50 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 0a 2a plication must.*
43f60 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 * guarantee that
43f70 20 74 68 65 20 73 68 61 72 65 64 20 63 61 63 68 the shared cach
43f80 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64 65 e used by the de
43f90 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
43fa0 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 63 63 se.** is not acc
43fb0 65 73 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 essed while the
43fc0 62 61 63 6b 75 70 20 69 73 20 72 75 6e 6e 69 6e backup is runnin
43fd0 67 2e 20 49 6e 20 70 72 61 63 74 69 63 65 20 74 g. In practice t
43fe0 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 his means.** tha
43ff0 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f t the applicatio
44000 6e 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 n must guarantee
44010 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 that the file-s
44020 79 73 74 65 6d 20 66 69 6c 65 20 62 65 69 6e 67 ystem file being
44030 20 0a 2a 2a 20 62 61 63 6b 65 64 20 75 70 20 74 .** backed up t
44040 6f 20 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 o is not accesse
44050 64 20 62 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74 d by any connect
44060 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20 70 ion within the p
44070 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a rocess,.** not j
44080 75 73 74 20 74 68 65 20 73 70 65 63 69 66 69 63 ust the specific
44090 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
440a0 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 73 was passed to s
440b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
440c0 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 it()..**.** The
440d0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d [sqlite3_backup]
440e0 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69 object itself i
440f0 73 20 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65 s partially thre
44100 61 64 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65 adsafe. Multiple
44110 20 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 61 79 .** threads may
44120 20 73 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75 6c safely make mul
44130 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e 74 tiple concurrent
44140 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
44150 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 3_backup_step().
44160 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
44170 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
44180 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 remaining() and
44190 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 sqlite3_backup_p
441a0 61 67 65 63 6f 75 6e 74 28 29 0a 2a 2a 20 41 50 agecount().** AP
441b0 49 73 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 Is are not stric
441c0 74 6c 79 20 73 70 65 61 6b 69 6e 67 20 74 68 72 tly speaking thr
441d0 65 61 64 73 61 66 65 2e 20 49 66 20 74 68 65 79 eadsafe. If they
441e0 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 are invoked at
441f0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 the.** same time
44200 20 61 73 20 61 6e 6f 74 68 65 72 20 74 68 72 65 as another thre
44210 61 64 20 69 73 20 69 6e 76 6f 6b 69 6e 67 20 73 ad is invoking s
44220 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
44230 65 70 28 29 20 69 74 20 69 73 0a 2a 2a 20 70 6f ep() it is.** po
44240 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 79 ssible that they
44250 20 72 65 74 75 72 6e 20 69 6e 76 61 6c 69 64 20 return invalid
44260 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 values..*/.SQLIT
44270 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61 E_API sqlite3_ba
44280 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 ckup *sqlite3_ba
44290 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c ckup_init(. sql
442a0 69 74 65 33 20 2a 70 44 65 73 74 2c 20 20 20 20 ite3 *pDest,
442b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
442c0 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 /* Destinati
442d0 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 on database hand
442e0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 le */. const ch
442f0 61 72 20 2a 7a 44 65 73 74 4e 61 6d 65 2c 20 20 ar *zDestName,
44300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
44310 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 * Destination da
44320 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 tabase name */.
44330 20 73 71 6c 69 74 65 33 20 2a 70 53 6f 75 72 63 sqlite3 *pSourc
44340 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
44350 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
44360 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
44370 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
44380 72 20 2a 7a 53 6f 75 72 63 65 4e 61 6d 65 20 20 r *zSourceName
44390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
443a0 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 Source database
443b0 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 name */.);.SQLI
443c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
443d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 e3_backup_step(s
443e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
443f0 2c 20 69 6e 74 20 6e 50 61 67 65 29 3b 0a 53 51 , int nPage);.SQ
44400 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
44410 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
44420 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 sh(sqlite3_backu
44430 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 p *p);.SQLITE_AP
44440 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
44450 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 ckup_remaining(s
44460 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
44470 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
44480 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
44490 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 _pagecount(sqlit
444a0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 0a e3_backup *p);..
444b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
444c0 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 Unlock Notificat
444d0 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e ion.** EXPERIMEN
444e0 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 72 TAL.**.** When r
444f0 75 6e 6e 69 6e 67 20 69 6e 20 73 68 61 72 65 64 unning in shared
44500 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 64 -cache mode, a d
44510 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f atabase operatio
44520 6e 20 6d 61 79 20 66 61 69 6c 20 77 69 74 68 0a n may fail with.
44530 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f ** an [SQLITE_LO
44540 43 4b 45 44 5d 20 65 72 72 6f 72 20 69 66 20 74 CKED] error if t
44550 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b he required lock
44560 73 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 2d s on the shared-
44570 63 61 63 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69 cache or.** indi
44580 76 69 64 75 61 6c 20 74 61 62 6c 65 73 20 77 69 vidual tables wi
44590 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 2d thin the shared-
445a0 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 20 cache cannot be
445b0 6f 62 74 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a obtained. See.**
445c0 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d [SQLite Shared-
445d0 43 61 63 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20 Cache Mode] for
445e0 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 a description of
445f0 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f shared-cache lo
44600 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 68 69 73 20 cking. .** This
44610 41 50 49 20 6d 61 79 20 62 65 20 75 73 65 64 20 API may be used
44620 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 63 61 to register a ca
44630 6c 6c 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69 llback that SQLi
44640 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a te will invoke .
44650 2a 2a 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e ** when the conn
44660 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 ection currently
44670 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 holding the req
44680 75 69 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e uired lock relin
44690 71 75 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 54 quishes it..** T
446a0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 his API is only
446b0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 available if the
446c0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d library was com
446d0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
446e0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 * [SQLITE_ENABLE
446f0 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 _UNLOCK_NOTIFY]
44700 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 C-preprocessor s
44710 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a ymbol defined..*
44720 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b *.** See Also: [
44730 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 Using the SQLite
44740 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 Unlock Notifica
44750 74 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a tion Feature]..*
44760 2a 0a 2a 2a 20 53 68 61 72 65 64 2d 63 61 63 68 *.** Shared-cach
44770 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 e locks are rele
44780 61 73 65 64 20 77 68 65 6e 20 61 20 64 61 74 61 ased when a data
44790 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
447a0 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73 concludes.** its
447b0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
447c0 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20 tion, either by
447d0 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72 committing it or
447e0 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b rolling it back
447f0 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 . .**.** When a
44800 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f 77 connection (know
44810 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 64 n as the blocked
44820 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 69 connection) fai
44830 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a ls to obtain a.*
44840 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c * shared-cache l
44850 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c ock and SQLITE_L
44860 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 OCKED is returne
44870 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2c d to the caller,
44880 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 79 the.** identity
44890 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
448a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 65 connection (the
448b0 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 blocking connec
448c0 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 61 tion) that.** ha
448d0 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 71 s locked the req
448e0 75 69 72 65 64 20 72 65 73 6f 75 72 63 65 20 69 uired resource i
448f0 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 s stored interna
44900 6c 6c 79 2e 20 41 66 74 65 72 20 61 6e 20 0a 2a lly. After an .*
44910 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 72 65 * application re
44920 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45 ceives an SQLITE
44930 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 2c 20 69 _LOCKED error, i
44940 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 65 0a 2a t may call the.*
44950 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b * sqlite3_unlock
44960 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64 _notify() method
44970 20 77 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65 with the blocke
44980 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e d connection han
44990 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 dle as .** the f
449a0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
449b0 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 61 20 register for a
449c0 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 callback that wi
449d0 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a ll be invoked.**
449e0 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69 when the blocki
449f0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 ng connections c
44a00 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
44a10 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e on is concluded.
44a20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b The.** callback
44a30 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d is invoked from
44a40 20 77 69 74 68 69 6e 20 74 68 65 20 5b 73 71 6c within the [sql
44a50 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 20 5b 73 ite3_step] or [s
44a60 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d 0a 2a 2a qlite3_close].**
44a70 20 63 61 6c 6c 20 74 68 61 74 20 63 6f 6e 63 6c call that concl
44a80 75 64 65 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e udes the blockin
44a90 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 g connections tr
44aa0 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
44ab0 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f If sqlite3_unlo
44ac0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 ck_notify() is c
44ad0 61 6c 6c 65 64 20 69 6e 20 61 20 6d 75 6c 74 69 alled in a multi
44ae0 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 -threaded applic
44af0 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 72 65 20 ation,.** there
44b00 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 is a chance that
44b10 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f the blocking co
44b20 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61 nnection will ha
44b30 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 6f ve already.** co
44b40 6e 63 6c 75 64 65 64 20 69 74 73 20 74 72 61 6e ncluded its tran
44b50 73 61 63 74 69 6f 6e 20 62 79 20 74 68 65 20 74 saction by the t
44b60 69 6d 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f ime sqlite3_unlo
44b70 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 69 ck_notify() is i
44b80 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 nvoked..** If th
44b90 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e is happens, then
44ba0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 the specified c
44bb0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
44bc0 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a ed immediately,.
44bd0 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 ** from within t
44be0 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
44bf0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 e3_unlock_notify
44c00 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ()..**.** If the
44c10 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
44c20 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e ion is attemptin
44c30 67 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 g to obtain a wr
44c40 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0a 2a 2a ite-lock on a.**
44c50 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 shared-cache ta
44c60 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74 68 ble, and more th
44c70 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f 6e an one other con
44c80 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c nection currentl
44c90 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 72 65 61 y holds.** a rea
44ca0 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 61 d-lock on the sa
44cb0 6d 65 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 53 me table, then S
44cc0 51 4c 69 74 65 20 61 72 62 69 74 72 61 72 69 6c QLite arbitraril
44cd0 79 20 73 65 6c 65 63 74 73 20 6f 6e 65 20 6f 66 y selects one of
44ce0 20 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 63 .** the other c
44cf0 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 75 73 onnections to us
44d00 65 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e e as the blockin
44d10 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a g connection..**
44d20 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 62 65 .** There may be
44d30 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 75 6e 6c at most one unl
44d40 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
44d50 61 63 6b 20 72 65 67 69 73 74 65 72 65 64 20 62 ack registered b
44d60 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 y a .** blocked
44d70 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 73 connection. If s
44d80 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
44d90 74 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 tify() is called
44da0 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 62 6c 6f when the.** blo
44db0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 cked connection
44dc0 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 already has a re
44dd0 67 69 73 74 65 72 65 64 20 75 6e 6c 6f 63 6b 2d gistered unlock-
44de0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c notify callback,
44df0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 65 77 .** then the new
44e00 20 63 61 6c 6c 62 61 63 6b 20 72 65 70 6c 61 63 callback replac
44e10 65 73 20 74 68 65 20 6f 6c 64 2e 20 49 66 20 73 es the old. If s
44e20 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
44e30 74 69 66 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c tify() is.** cal
44e40 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 led with a NULL
44e50 70 6f 69 6e 74 65 72 20 61 73 20 69 74 73 20 73 pointer as its s
44e60 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 econd argument,
44e70 74 68 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e then any existin
44e80 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 g.** unlock-noti
44e90 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 fy callback is c
44ea0 61 6e 63 65 6c 6c 65 64 2e 20 54 68 65 20 62 6c ancelled. The bl
44eb0 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
44ec0 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 s .** unlock-not
44ed0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 ify callback may
44ee0 20 61 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65 also be cancele
44ef0 64 20 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65 d by closing the
44f00 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e blocked.** conn
44f10 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 ection using [sq
44f20 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a lite3_close()]..
44f30 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b **.** The unlock
44f40 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
44f50 20 69 73 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e is not reentran
44f60 74 2e 20 49 66 20 61 6e 20 61 70 70 6c 69 63 61 t. If an applica
44f70 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 tion invokes.**
44f80 61 6e 79 20 73 71 6c 69 74 65 33 5f 78 78 78 20 any sqlite3_xxx
44f90 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 API functions fr
44fa0 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c om within an unl
44fb0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
44fc0 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20 ack, a.** crash
44fd0 6f 72 20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20 or deadlock may
44fe0 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a be the result..*
44ff0 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 64 65 61 64 *.** Unless dead
45000 6c 6f 63 6b 20 69 73 20 64 65 74 65 63 74 65 64 lock is detected
45010 20 28 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71 (see below), sq
45020 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45030 69 66 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20 ify() always.**
45040 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
45050 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c K..**.** <b>Call
45060 62 61 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20 back Invocation
45070 44 65 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a Details</b>.**.*
45080 2a 20 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b * When an unlock
45090 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
450a0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c 20 is registered,
450b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
450c0 70 72 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73 provides a .** s
450d0 69 6e 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e ingle void* poin
450e0 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73 ter that is pass
450f0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 ed to the callba
45100 63 6b 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e ck when it is in
45110 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 voked..** Howeve
45120 72 2c 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 r, the signature
45130 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b of the callback
45140 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 function allows
45150 20 53 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a SQLite to pass.
45160 2a 2a 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f ** it an array o
45170 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 f void* context
45180 70 6f 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69 pointers. The fi
45190 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 rst argument pas
451a0 73 65 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c sed to.** an unl
451b0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
451c0 61 63 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ack is a pointer
451d0 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 to an array of
451e0 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a void* pointers,.
451f0 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e ** and the secon
45200 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 d is the number
45210 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
45220 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 e array..**.** W
45230 68 65 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 hen a blocking c
45240 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 onnections trans
45250 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 action is conclu
45260 64 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 ded, there may b
45270 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f e.** more than o
45280 6e 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 ne blocked conne
45290 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72 ction that has r
452a0 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e egistered for an
452b0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a unlock-notify.*
452c0 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 49 66 20 74 * callback. If t
452d0 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20 wo or more such
452e0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
452f0 6f 6e 73 20 68 61 76 65 20 73 70 65 63 69 66 69 ons have specifi
45300 65 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 ed the.** same c
45310 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
45320 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f , then instead o
45330 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 f invoking the c
45340 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
45350 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d .** multiple tim
45360 65 73 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 es, it is invoke
45370 64 20 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20 d once with the
45380 73 65 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e set of void* con
45390 74 65 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a text pointers.**
453a0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
453b0 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 e blocked connec
453c0 74 69 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f tions bundled to
453d0 67 65 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61 gether into an a
453e0 72 72 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69 rray..** This gi
453f0 76 65 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 ves the applicat
45400 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 ion an opportuni
45410 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65 ty to prioritize
45420 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a any actions .**
45430 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20 related to the
45440 73 65 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 set of unblocked
45450 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
45460 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e tions..**.** <b>
45470 44 65 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 Deadlock Detecti
45480 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73 on</b>.**.** Ass
45490 75 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72 uming that after
454a0 20 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72 registering for
454b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
454c0 79 20 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a y callback a .**
454d0 20 64 61 74 61 62 61 73 65 20 77 61 69 74 73 20 database waits
454e0 66 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b for the callback
454f0 20 74 6f 20 62 65 20 69 73 73 75 65 64 20 62 65 to be issued be
45500 66 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20 fore taking any
45510 66 75 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f further.** actio
45520 6e 20 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 n (a reasonable
45530 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65 assumption), the
45540 6e 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 n using this API
45550 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a may cause the.*
45560 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f * application to
45570 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65 deadlock. For e
45580 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65 xample, if conne
45590 63 74 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69 ction X is waiti
455a0 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 ng for.** connec
455b0 74 69 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63 tion Y's transac
455c0 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c tion to be concl
455d0 75 64 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61 uded, and simila
455e0 72 6c 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a rly connection.*
455f0 2a 20 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f * Y is waiting o
45600 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73 n connection X's
45610 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
45620 65 6e 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65 en neither conne
45630 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72 ction.** will pr
45640 6f 63 65 65 64 20 61 6e 64 20 74 68 65 20 73 79 oceed and the sy
45650 73 74 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20 stem may remain
45660 64 65 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66 deadlocked indef
45670 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 initely..**.** T
45680 6f 20 61 76 6f 69 64 20 74 68 69 73 20 73 63 65 o avoid this sce
45690 6e 61 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74 nario, the sqlit
456a0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 e3_unlock_notify
456b0 28 29 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64 () performs dead
456c0 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f lock.** detectio
456d0 6e 2e 20 49 66 20 61 20 67 69 76 65 6e 20 63 61 n. If a given ca
456e0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e ll to sqlite3_un
456f0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f lock_notify() wo
45700 75 6c 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73 uld put the.** s
45710 79 73 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c ystem in a deadl
45720 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65 ocked state, the
45730 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 n SQLITE_LOCKED
45740 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
45750 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 no.** unlock-not
45760 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ify callback is
45770 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20 registered. The
45780 73 79 73 74 65 6d 20 69 73 20 73 61 69 64 20 74 system is said t
45790 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61 o be in.** a dea
457a0 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66 dlocked state if
457b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61 connection A ha
457c0 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 s registered for
457d0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
457e0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e y.** callback on
457f0 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
45800 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27 of connection B'
45810 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 s transaction, a
45820 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a nd connection.**
45830 20 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65 B has itself re
45840 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 gistered for an
45850 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
45860 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e llback when conn
45870 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72 ection.** A's tr
45880 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e ansaction is con
45890 63 6c 75 64 65 64 2e 20 49 6e 64 69 72 65 63 74 cluded. Indirect
458a0 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c 73 deadlock is als
458b0 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0a 2a o detected, so.*
458c0 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 * the system is
458d0 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 also considered
458e0 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 64 to be deadlocked
458f0 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 if connection B
45900 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 has.** register
45910 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b ed for an unlock
45920 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
45930 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 on the conclusi
45940 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e on of connection
45950 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 61 63 74 .** C's transact
45960 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e 65 ion, where conne
45970 63 74 69 6f 6e 20 43 20 69 73 20 77 61 69 74 69 ction C is waiti
45980 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e ng on connection
45990 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e 75 6d 62 65 A. Any.** numbe
459a0 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 69 r of levels of i
459b0 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 65 20 61 ndirection are a
459c0 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 llowed..**.** <b
459d0 3e 54 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 >The "DROP TABLE
459e0 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f 62 3e 0a " Exception</b>.
459f0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 6c **.** When a cal
45a00 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
45a10 65 70 28 29 5d 20 72 65 74 75 72 6e 73 20 53 51 ep()] returns SQ
45a20 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 74 20 LITE_LOCKED, it
45a30 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 61 6c is almost .** al
45a40 77 61 79 73 20 61 70 70 72 6f 70 72 69 61 74 65 ways appropriate
45a50 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 to call sqlite3
45a60 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
45a70 2e 20 54 68 65 72 65 20 69 73 20 68 6f 77 65 76 . There is howev
45a80 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 65 70 er,.** one excep
45a90 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 65 63 75 tion. When execu
45aa0 74 69 6e 67 20 61 20 22 44 52 4f 50 20 54 41 42 ting a "DROP TAB
45ab0 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 LE" or "DROP IND
45ac0 45 58 22 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a EX" statement,.*
45ad0 2a 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20 * SQLite checks
45ae0 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 if there are any
45af0 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 currently execu
45b00 74 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 ting SELECT stat
45b10 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 62 ements.** that b
45b20 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d elong to the sam
45b30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 e connection. If
45b40 20 74 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49 there are, SQLI
45b50 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a 2a 20 TE_LOCKED is.**
45b60 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 returned. In thi
45b70 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 s case there is
45b80 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e no "blocking con
45b90 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76 nection", so inv
45ba0 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 oking.** sqlite3
45bb0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
45bc0 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 results in the
45bd0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
45be0 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a 2a 2a 20 llback being.**
45bf0 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 invoked immediat
45c00 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70 70 6c ely. If the appl
45c10 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72 65 2d ication then re-
45c20 61 74 74 65 6d 70 74 73 20 74 68 65 20 22 44 52 attempts the "DR
45c30 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 6f 72 20 OP TABLE".** or
45c40 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71 75 65 "DROP INDEX" que
45c50 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 ry, an infinite
45c60 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20 74 68 loop might be th
45c70 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 e result..**.**
45c80 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e 64 20 74 One way around t
45c90 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 his problem is t
45ca0 6f 20 63 68 65 63 6b 20 74 68 65 20 65 78 74 65 o check the exte
45cb0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 nded error code
45cc0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 61 returned.** by a
45cd0 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 n sqlite3_step()
45ce0 20 63 61 6c 6c 2e 20 49 66 20 74 68 65 72 65 20 call. If there
45cf0 69 73 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f is a blocking co
45d00 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 nnection, then t
45d10 68 65 0a 2a 2a 20 65 78 74 65 6e 64 65 64 20 65 he.** extended e
45d20 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 rror code is set
45d30 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 to SQLITE_LOCKE
45d40 44 5f 53 48 41 52 45 44 43 41 43 48 45 2e 20 4f D_SHAREDCACHE. O
45d50 74 68 65 72 77 69 73 65 2c 20 69 6e 0a 2a 2a 20 therwise, in.**
45d60 74 68 65 20 73 70 65 63 69 61 6c 20 22 44 52 4f the special "DRO
45d70 50 20 54 41 42 4c 45 2f 49 4e 44 45 58 22 20 63 P TABLE/INDEX" c
45d80 61 73 65 2c 20 74 68 65 20 65 78 74 65 6e 64 65 ase, the extende
45d90 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 d error code is
45da0 6a 75 73 74 20 0a 2a 2a 20 53 51 4c 49 54 45 5f just .** SQLITE_
45db0 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 LOCKED..*/.SQLIT
45dc0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
45dd0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
45de0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f . sqlite3 *pBlo
45df0 63 6b 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 cked,
45e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
45e10 2a 20 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63 * Waiting connec
45e20 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
45e30 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a *xNotify)(void *
45e40 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67 *apArg, int nArg
45e50 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 ), /* Callbac
45e60 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e k function to in
45e70 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a voke */. void *
45e80 70 4e 6f 74 69 66 79 41 72 67 20 20 20 20 20 20 pNotifyArg
45e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45ea0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
45eb0 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 4e 6f t to pass to xNo
45ec0 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a 0a 2f 2a 0a tify */.);.../*.
45ed0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 72 ** CAPI3REF: Str
45ee0 69 6e 67 20 43 6f 6d 70 61 72 69 73 6f 6e 0a 2a ing Comparison.*
45ef0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
45f00 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 *.** The [sqlite
45f10 33 5f 73 74 72 6e 69 63 6d 70 28 29 5d 20 41 50 3_strnicmp()] AP
45f20 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61 I allows applica
45f30 74 69 6f 6e 73 20 61 6e 64 20 65 78 74 65 6e 73 tions and extens
45f40 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 6f 6d 70 61 ions to.** compa
45f50 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 re the contents
45f60 6f 66 20 74 77 6f 20 62 75 66 66 65 72 73 20 63 of two buffers c
45f70 6f 6e 74 61 69 6e 69 6e 67 20 55 54 46 2d 38 20 ontaining UTF-8
45f80 73 74 72 69 6e 67 73 20 69 6e 20 61 0a 2a 2a 20 strings in a.**
45f90 63 61 73 65 2d 69 6e 64 65 6e 64 65 6e 74 20 66 case-indendent f
45fa0 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 ashion, using th
45fb0 65 20 73 61 6d 65 20 64 65 66 69 6e 69 74 69 6f e same definitio
45fc0 6e 20 6f 66 20 63 61 73 65 20 69 6e 64 65 70 65 n of case indepe
45fd0 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 68 61 74 20 ndence .** that
45fe0 53 51 4c 69 74 65 20 75 73 65 73 20 69 6e 74 65 SQLite uses inte
45ff0 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 6f 6d 70 rnally when comp
46000 61 72 69 6e 67 20 69 64 65 6e 74 69 66 69 65 72 aring identifier
46010 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
46020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 int sqlite3_str
46030 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 nicmp(const char
46040 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
46050 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55 , int);../*.** U
46060 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61 ndo the hack tha
46070 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 t converts float
46080 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20 ing point types
46090 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a to integer for.*
460a0 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 * builds on proc
460b0 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66 essors without f
460c0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 loating point su
460d0 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 pport..*/.#ifdef
460e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
460f0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e ATING_POINT.# un
46100 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 def double.#endi
46110 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 f..#if 0.} /* E
46120 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 65 72 nd of the 'exter
46130 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a n "C"' block */.
46140 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 0a #endif.#endif...
46150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46160 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 68 End of sqlite3.h
46170 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
461a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
461b0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
461c0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
461d0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
461e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
461f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46200 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 69 Include hash.h i
46210 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
46220 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46240 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
46250 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e Begin file hash.
46260 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
46270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
46290 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
462a0 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 mber 22.**.** Th
462b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
462c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
462d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
462e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
462f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
46300 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
46310 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
46320 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
46330 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
46340 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
46350 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
46360 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
46370 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
46380 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
46390 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
463a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
463b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
463c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46400 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
46410 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c s the header fil
46420 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69 e for the generi
46430 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 c hash-table imp
46440 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 lemenation.** us
46450 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f ed in SQLite..*/
46460 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 .#ifndef _SQLITE
46470 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65 _HASH_H_.#define
46480 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f _SQLITE_HASH_H_
46490 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 ../* Forward dec
464a0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 larations of str
464b0 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 uctures. */.type
464c0 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 20 def struct Hash
464d0 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 Hash;.typedef st
464e0 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 61 ruct HashElem Ha
464f0 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f shElem;../* A co
46500 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c mplete hash tabl
46510 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 e is an instance
46520 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
46530 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 g structure..**
46540 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 The internals of
46550 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
46560 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 are intended to
46570 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 be opaque -- cli
46580 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 ent.** code shou
46590 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 ld not attempt t
465a0 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 o access or modi
465b0 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 fy the fields of
465c0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a this structure.
465d0 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 ** directly. Ch
465e0 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74 ange this struct
465f0 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e ure only by usin
46600 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 g the routines b
46610 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 elow..** However
46620 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22 70 , some of the "p
46630 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 rocedures" and "
46640 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d functions" for m
46650 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 odifying and.**
46660 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73 accessing this s
46670 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61 tructure are rea
46680 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 lly macros, so w
46690 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d e can't really m
466a0 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 ake.** this stru
466b0 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2a cture opaque..**
466c0 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 .** All elements
466d0 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 of the hash tab
466e0 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 69 6e 67 le are on a sing
466f0 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 le doubly-linked
46700 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 68 2e 66 list..** Hash.f
46710 69 72 73 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 irst points to t
46720 68 65 20 68 65 61 64 20 6f 66 20 74 68 69 73 20 he head of this
46730 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 list..**.** Ther
46740 65 20 61 72 65 20 48 61 73 68 2e 68 74 73 69 7a e are Hash.htsiz
46750 65 20 62 75 63 6b 65 74 73 2e 20 20 45 61 63 68 e buckets. Each
46760 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 73 20 74 bucket points t
46770 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 74 o a spot in.** t
46780 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 62 6c 79 he global doubly
46790 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 54 -linked list. T
467a0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
467b0 68 65 20 62 75 63 6b 65 74 20 61 72 65 20 74 68 he bucket are th
467c0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 70 6f 69 e.** element poi
467d0 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 74 68 65 nted to plus the
467e0 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 6e 74 2d next _ht.count-
467f0 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 1 elements in th
46800 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 e list..**.** Ha
46810 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 20 48 61 sh.htsize and Ha
46820 73 68 2e 68 74 20 6d 61 79 20 62 65 20 7a 65 72 sh.ht may be zer
46830 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 o. In that case
46840 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f 6e 65 0a lookup is done.
46850 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 72 20 73 ** by a linear s
46860 65 61 72 63 68 20 6f 66 20 74 68 65 20 67 6c 6f earch of the glo
46870 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f 72 20 73 bal list. For s
46880 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 74 68 65 mall tables, the
46890 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 74 61 62 .** Hash.ht tab
468a0 6c 65 20 69 73 20 6e 65 76 65 72 20 61 6c 6c 6f le is never allo
468b0 63 61 74 65 64 20 62 65 63 61 75 73 65 20 69 66 cated because if
468c0 20 74 68 65 72 65 20 61 72 65 20 66 65 77 20 65 there are few e
468d0 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 lements.** in th
468e0 65 20 74 61 62 6c 65 2c 20 69 74 20 69 73 20 66 e table, it is f
468f0 61 73 74 65 72 20 74 6f 20 64 6f 20 61 20 6c 69 aster to do a li
46900 6e 65 61 72 20 73 65 61 72 63 68 20 74 68 61 6e near search than
46910 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 68 to manage.** th
46920 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f e hash table..*/
46930 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a 20 .struct Hash {.
46940 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 74 unsigned int ht
46950 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 size; /* Nu
46960 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20 mber of buckets
46970 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c in the hash tabl
46980 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
46990 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 int count;
469a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
469b0 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74 61 tries in this ta
469c0 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ble */. HashEle
469d0 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 20 20 20 m *first;
469e0 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 /* The first
469f0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 element of the a
46a00 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 rray */. struct
46a10 20 5f 68 74 20 7b 20 20 20 20 20 20 20 20 20 20 _ht {
46a20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 /* the hash
46a30 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 table */. int
46a40 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 count;
46a50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
46a60 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 r of entries wit
46a70 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 h this hash */.
46a80 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 HashElem *cha
46a90 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a in; /*
46aa0 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 Pointer to firs
46ab0 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 t entry with thi
46ac0 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 s hash */. } *h
46ad0 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 t;.};../* Each e
46ae0 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 lement in the ha
46af0 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 sh table is an i
46b00 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
46b10 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 ollowing .** str
46b20 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 ucture. All ele
46b30 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 ments are stored
46b40 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 on a single dou
46b50 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e bly-linked list.
46b60 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 .**.** Again, th
46b70 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
46b80 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f intended to be o
46b90 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 paque, but it ca
46ba0 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 n't really.** be
46bb0 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20 opaque because
46bc0 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 it is used by ma
46bd0 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 cros..*/.struct
46be0 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 HashElem {. Has
46bf0 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 hElem *next, *pr
46c00 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 ev; /* Nex
46c10 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65 t and previous e
46c20 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74 lements in the t
46c30 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a able */. void *
46c40 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 data;
46c50 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 /* Data a
46c60 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
46c70 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 his element */.
46c80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
46c90 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 2f 2a y; int nKey; /*
46ca0 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 Key associated
46cb0 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e with this elemen
46cc0 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 t */.};../*.** A
46cd0 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 ccess routines.
46ce0 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 To delete, inse
46cf0 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 rt a NULL pointe
46d00 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
46d10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
46d20 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a 29 3HashInit(Hash*)
46d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
46d40 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 void *sqlite3Ha
46d50 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 shInsert(Hash*,
46d60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 const char *pKey
46d70 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 , int nKey, void
46d80 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 *pData);.SQLITE
46d90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
46da0 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 qlite3HashFind(c
46db0 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 onst Hash*, cons
46dc0 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e t char *pKey, in
46dd0 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f t nKey);.SQLITE_
46de0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
46df0 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 61 ite3HashClear(Ha
46e00 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 sh*);../*.** Mac
46e10 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 ros for looping
46e20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 over all element
46e30 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c s of a hash tabl
46e40 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 e. The idiom is
46e50 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a .** like this:.*
46e60 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a *.** Hash h;.*
46e70 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b * HashElem *p;
46e80 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 .** ....** f
46e90 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 or(p=sqliteHashF
46ea0 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 73 irst(&h); p; p=s
46eb0 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 qliteHashNext(p)
46ec0 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 ){.** SomeSt
46ed0 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d ructure *pData =
46ee0 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 sqliteHashData(
46ef0 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f p);.** // do
46f00 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 something with
46f10 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a pData.** }.*/.
46f20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 #define sqliteHa
46f30 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29 shFirst(H) ((H)
46f40 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65 ->first).#define
46f50 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 sqliteHashNext(
46f60 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 E) ((E)->next)
46f70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 .#define sqliteH
46f80 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 45 ashData(E) ((E
46f90 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 64 65 66 )->data)./* #def
46fa0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 ine sqliteHashKe
46fb0 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b y(E) ((E)->pK
46fc0 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 ey) // NOT USED
46fd0 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 */./* #define sq
46fe0 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28 liteHashKeysize(
46ff0 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20 E) ((E)->nKey)
47000 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a // NOT USED */..
47010 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /*.** Number of
47020 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73 entries in a has
47030 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 h table.*/./* #d
47040 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
47050 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e Count(H) ((H)->
47060 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 53 count) // NOT US
47070 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a ED */..#endif /*
47080 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f _SQLITE_HASH_H_
47090 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
470a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 **** End of hash
470b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
470c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470e0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
470f0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
47100 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
47110 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
47120 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
47130 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
47140 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 **** Include par
47150 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 se.h in the midd
47160 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
47170 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
47180 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
47190 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
471a0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a parse.h ********
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
471c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
471d0 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f ***/.#define TK_
471e0 53 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 SEMI
471f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47200 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 1.#define TK_EXP
47210 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 LAIN
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 2.#
47230 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 define TK_QUERY
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47250 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 3.#def
47260 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 ine TK_PLAN
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47280 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 4.#define
47290 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 TK_BEGIN
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472b0 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5.#define TK
472c0 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 _TRANSACTION
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472e0 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 6.#define TK_DE
472f0 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20 FERRED
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 7.
47310 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 #define TK_IMMED
47320 49 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 IATE
47330 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 8.#de
47340 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 fine TK_EXCLUSIV
47350 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47360 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 9.#defin
47370 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 e TK_COMMIT
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47390 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 10.#define T
473a0 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 K_END
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473c0 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 11.#define TK_R
473d0 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 OLLBACK
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
473f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 45 .#define TK_SAVE
47400 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 POINT
47410 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 13.#d
47420 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45 efine TK_RELEASE
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47440 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 14.#defi
47450 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 ne TK_TO
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47470 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 15.#define
47480 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 TK_TABLE
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474a0 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 16.#define TK_
474b0 43 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 CREATE
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
474d0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 20 7.#define TK_IF
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474f0 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 18.#
47500 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 define TK_NOT
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47520 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 19.#def
47530 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20 ine TK_EXISTS
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47550 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20.#define
47560 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20 TK_TEMP
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47580 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 21.#define TK
47590 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20 _LP
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475b0 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 50 22.#define TK_RP
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23.
475e0 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 20 #define TK_AS
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47600 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 24.#de
47610 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20 fine TK_COMMA
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47630 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 25.#defin
47640 65 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 20 e TK_ID
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47660 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 26.#define T
47670 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 20 K_INDEXED
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47690 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 27.#define TK_A
476a0 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20 BORT
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 28
476c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 43 54 49 .#define TK_ACTI
476d0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
476e0 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 29.#d
476f0 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 efine TK_AFTER
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47710 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 30.#defi
47720 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 ne TK_ANALYZE
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47740 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 31.#define
47750 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 20 TK_ASC
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47770 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 32.#define TK_
47780 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 ATTACH
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
477a0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 3.#define TK_BEF
477b0 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ORE
477c0 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 34.#
477d0 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 define TK_BY
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477f0 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 35.#def
47800 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 ine TK_CASCADE
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47820 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 36.#define
47830 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 TK_CAST
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47850 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 37.#define TK
47860 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20 _COLUMNKW
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47880 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 38.#define TK_CO
47890 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 NFLICT
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 39.
478b0 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 #define TK_DATAB
478c0 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ASE
478d0 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 40.#de
478e0 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 fine TK_DESC
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47900 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 41.#defin
47910 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 e TK_DETACH
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47930 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 42.#define T
47940 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20 K_EACH
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47960 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 43.#define TK_F
47970 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 AIL
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34 44
47990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 .#define TK_FOR
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479b0 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 45.#d
479c0 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 efine TK_IGNORE
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479e0 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 46.#defi
479f0 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 ne TK_INITIALLY
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a10 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 47.#define
47a20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 TK_INSTEAD
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a40 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48.#define TK_
47a50 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 LIKE_KW
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47a70 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 9.#define TK_MAT
47a80 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CH
47a90 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 50.#
47aa0 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 20 20 20 20 define TK_NO
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ac0 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 51.#def
47ad0 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 ine TK_KEY
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47af0 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 52.#define
47b00 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20 TK_OF
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 53.#define TK
47b30 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 20 _OFFSET
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b50 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 54.#define TK_PR
47b60 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 AGMA
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 55.
47b80 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 #define TK_RAISE
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ba0 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 56.#de
47bb0 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 fine TK_REPLACE
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bd0 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 57.#defin
47be0 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20 20 e TK_RESTRICT
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c00 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 58.#define T
47c10 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20 20 K_ROW
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c30 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 59.#define TK_T
47c40 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 20 RIGGER
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 60
47c60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 .#define TK_VACU
47c70 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 UM
47c80 20 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 61.#d
47c90 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 efine TK_VIEW
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cb0 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 62.#defi
47cc0 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 ne TK_VIRTUAL
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ce0 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 63.#define
47cf0 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 TK_REINDEX
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d10 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 64.#define TK_
47d20 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20 RENAME
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
47d40 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 5.#define TK_CTI
47d50 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 ME_KW
47d60 20 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 66.#
47d70 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 define TK_ANY
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d90 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 67.#def
47da0 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 ine TK_OR
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47dc0 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 68.#define
47dd0 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20 TK_AND
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47df0 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 69.#define TK
47e00 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 20 _IS
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 70.#define TK_BE
47e30 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20 20 TWEEN
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 71.
47e50 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 #define TK_IN
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e70 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 72.#de
47e80 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 fine TK_ISNULL
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ea0 20 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 73.#defin
47eb0 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 e TK_NOTNULL
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ed0 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 74.#define T
47ee0 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 K_NE
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f00 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 75.#define TK_E
47f10 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Q
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 76
47f30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 .#define TK_GT
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f50 20 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 77.#d
47f60 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 efine TK_LE
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f80 20 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 78.#defi
47f90 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 ne TK_LT
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fb0 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 79.#define
47fc0 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 TK_GE
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fe0 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 80.#define TK_
47ff0 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20 20 ESCAPE
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
48010 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 1.#define TK_BIT
48020 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 AND
48030 20 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 82.#
48040 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 define TK_BITOR
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48060 20 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 83.#def
48070 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 ine TK_LSHIFT
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48090 20 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 84.#define
480a0 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20 TK_RSHIFT
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480c0 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 85.#define TK
480d0 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 _PLUS
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480f0 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 86.#define TK_MI
48100 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 NUS
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 87.
48120 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 #define TK_STAR
48130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48140 20 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 88.#de
48150 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 fine TK_SLASH
48160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48170 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 89.#defin
48180 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 e TK_REM
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481a0 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 90.#define T
481b0 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20 20 K_CONCAT
481c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481d0 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 91.#define TK_C
481e0 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20 20 OLLATE
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32 92
48200 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e .#define TK_BITN
48210 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OT
48220 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 93.#d
48230 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20 efine TK_STRING
48240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48250 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 94.#defi
48260 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 ne TK_JOIN_KW
48270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48280 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 95.#define
48290 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20 TK_CONSTRAINT
482a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482b0 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 96.#define TK_
482c0 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 DEFAULT
482d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
482e0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 7.#define TK_NUL
482f0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
48300 20 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 98.#
48310 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52 define TK_PRIMAR
48320 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Y
48330 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 99.#def
48340 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20 ine TK_UNIQUE
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48360 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 100.#defin
48370 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 e TK_CHECK
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48390 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 101.#define
483a0 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20 TK_REFERENCES
483b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483c0 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 102.#define TK
483d0 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20 20 _AUTOINCR
483e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483f0 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 103.#define TK_O
48400 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
48410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
48420 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c 4.#define TK_DEL
48430 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ETE
48440 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 105.
48450 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 44 41 54 #define TK_UPDAT
48460 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48470 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 106.#d
48480 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20 20 efine TK_SET
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484a0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 107.#def
484b0 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42 4c ine TK_DEFERRABL
484c0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
484d0 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 108.#defin
484e0 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20 20 e TK_FOREIGN
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48500 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 109.#define
48510 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20 20 TK_DROP
48520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48530 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 110.#define TK
48540 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20 _UNION
48550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48560 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 111.#define TK_A
48570 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LL
48580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
48590 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 2.#define TK_EXC
485a0 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20 20 EPT
485b0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 113.
485c0 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 52 #define TK_INTER
485d0 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 SECT
485e0 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 114.#d
485f0 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54 20 efine TK_SELECT
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48610 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 115.#def
48620 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54 20 ine TK_DISTINCT
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48640 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 116.#defin
48650 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 20 e TK_DOT
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48670 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 117.#define
48680 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 20 TK_FROM
48690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486a0 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 118.#define TK
486b0 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 20 _JOIN
486c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486d0 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 119.#define TK_U
486e0 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 SING
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
48700 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44 0.#define TK_ORD
48710 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ER
48720 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 121.
48730 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50 #define TK_GROUP
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48750 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 122.#d
48760 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20 efine TK_HAVING
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48780 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 123.#def
48790 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20 ine TK_LIMIT
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487b0 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 124.#defin
487c0 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20 e TK_WHERE
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487e0 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 125.#define
487f0 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20 TK_INTO
48800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48810 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 126.#define TK
48820 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20 _VALUES
48830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48840 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 127.#define TK_I
48850 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 NSERT
48860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
48870 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 8.#define TK_INT
48880 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 EGER
48890 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 129.
488a0 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 54 #define TK_FLOAT
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488c0 20 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 130.#d
488d0 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20 efine TK_BLOB
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488f0 20 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 131.#def
48900 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 ine TK_REGISTER
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48920 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 132.#defin
48930 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 20 e TK_VARIABLE
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48950 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 133.#define
48960 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 20 TK_CASE
48970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48980 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 134.#define TK
48990 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 _WHEN
489a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489b0 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 135.#define TK_T
489c0 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 HEN
489d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
489e0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c 53 6.#define TK_ELS
489f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48a00 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 137.
48a10 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 #define TK_INDEX
48a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a30 20 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 138.#d
48a40 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20 20 efine TK_ALTER
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a60 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 139.#def
48a70 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 20 ine TK_ADD
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a90 20 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 140.#defin
48aa0 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 e TK_TO_TEXT
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48ac0 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 141.#define
48ad0 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 TK_TO_BLOB
48ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48af0 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 142.#define TK
48b00 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 _TO_NUMERIC
48b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 143.#define TK_T
48b30 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 O_INT
48b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
48b50 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4.#define TK_TO_
48b60 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 REAL
48b70 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 145.
48b80 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 4f 54 #define TK_ISNOT
48b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48ba0 20 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 146.#d
48bb0 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f efine TK_END_OF_
48bc0 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 FILE
48bd0 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 147.#def
48be0 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 ine TK_ILLEGAL
48bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c00 20 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 148.#defin
48c10 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20 e TK_SPACE
48c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c30 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 149.#define
48c40 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 TK_UNCLOSED_STRI
48c50 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NG
48c60 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 150.#define TK
48c70 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 _FUNCTION
48c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c90 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 151.#define TK_C
48ca0 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 OLUMN
48cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 15
48cc0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 2.#define TK_AGG
48cd0 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 _FUNCTION
48ce0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 0a 153.
48cf0 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 #define TK_AGG_C
48d00 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 OLUMN
48d10 20 20 20 20 20 20 20 20 20 20 31 35 34 0a 23 64 154.#d
48d20 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 efine TK_CONST_F
48d30 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 UNC
48d40 20 20 20 20 20 20 20 20 31 35 35 0a 23 64 65 66 155.#def
48d50 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20 ine TK_UMINUS
48d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d70 20 20 20 20 20 20 31 35 36 0a 23 64 65 66 69 6e 156.#defin
48d80 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 20 e TK_UPLUS
48d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48da0 20 20 20 20 31 35 37 0a 0a 2f 2a 2a 2a 2a 2a 2a 157../******
48db0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
48dc0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a parse.h ********
48dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48df0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
48e00 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
48e10 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
48e20 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 t off in sqliteI
48e30 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
48e40 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 *******/.#includ
48e50 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 e <stdio.h>.#inc
48e60 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a lude <stdlib.h>.
48e70 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 #include <string
48e80 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 .h>.#include <as
48e90 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 sert.h>.#include
48ea0 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a <stddef.h>../*.
48eb0 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 ** If compiling
48ec0 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 for a processor
48ed0 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 that lacks float
48ee0 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 ing point suppor
48ef0 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 t,.** substitute
48f00 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f integer for flo
48f10 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 ating-point.*/.#
48f20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
48f30 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
48f40 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 .# define double
48f50 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 sqlite_int64.#
48f60 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c define LONGDOUBL
48f70 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e E_TYPE sqlite_in
48f80 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 4c t64.# ifndef SQL
48f90 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 ITE_BIG_DBL.#
48fa0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 define SQLITE_BI
48fb0 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 33 G_DBL (((sqlite3
48fc0 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 23 _int64)1)<<50).#
48fd0 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 endif.# define
48fe0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
48ff0 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 64 TIME_FUNCS 1.# d
49000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 efine SQLITE_OMI
49010 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 65 T_TRACE 1.# unde
49020 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 f SQLITE_MIXED_E
49030 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 NDIAN_64BIT_FLOA
49040 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 T.# undef SQLITE
49050 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e 64 _HAVE_ISNAN.#end
49060 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
49070 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 E_BIG_DBL.# defi
49080 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 ne SQLITE_BIG_DB
49090 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a L (1e99).#endif.
490a0 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 ./*.** OMIT_TEMP
490b0 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 DB is set to 1 i
490c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 f SQLITE_OMIT_TE
490d0 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c MPDB is defined,
490e0 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 or 0.** afterwa
490f0 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 rd. Having this
49100 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 macro allows us
49110 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63 to cause the C c
49120 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f ompiler .** to o
49130 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 mit code used by
49140 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 TEMP tables wit
49150 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 hout messy #ifnd
49160 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a ef statements..*
49170 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
49180 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 OMIT_TEMPDB.#def
49190 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 ine OMIT_TEMPDB
491a0 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 1.#else.#define
491b0 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 OMIT_TEMPDB 0.#e
491c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ndif../*.** If t
491d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
491e0 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 ro is set to 1,
491f0 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 then NULL values
49200 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a are considered.
49210 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e ** distinct when
49220 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 determining whe
49230 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 ther or not two
49240 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 entries are the
49250 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 same.** in a UNI
49260 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 QUE index. This
49270 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 is the way Post
49280 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 greSQL, Oracle,
49290 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f DB2, MySQL,.** O
492a0 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 CELOT, and Fireb
492b0 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 ird all work. T
492c0 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 he SQL92 spec ex
492d0 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 plicitly says th
492e0 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 is.** is the way
492f0 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 things are supp
49300 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a ose to work..**.
49310 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** If the follow
49320 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 ing macro is set
49330 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 to 0, the NULLs
49340 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 are indistinct
49350 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 for.** a UNIQUE
49360 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 index. In this
49370 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e mode, you can on
49380 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 ly have a single
49390 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 NULL entry.** f
493a0 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c or a column decl
493b0 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 ared UNIQUE. Th
493c0 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e is is the way In
493d0 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 formix and SQL S
493e0 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a erver.** work..*
493f0 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 /.#define NULL_D
49400 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 ISTINCT_FOR_UNIQ
49410 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 UE 1../*.** The
49420 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 "file format" nu
49430 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 mber is an integ
49440 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 er that is incre
49450 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a mented whenever.
49460 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 ** the VDBE-leve
49470 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 l file format ch
49480 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c anges. The foll
49490 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 owing macros def
494a0 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 ine the.** the d
494b0 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d efault file form
494c0 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 at for new datab
494d0 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 ases and the max
494e0 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 imum file format
494f0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 .** that the lib
49500 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a rary can read..*
49510 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
49520 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 _MAX_FILE_FORMAT
49530 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 4.#ifndef SQLIT
49540 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 E_DEFAULT_FILE_F
49550 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 ORMAT.# define S
49560 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 QLITE_DEFAULT_FI
49570 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 LE_FORMAT 1.#end
49580 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
49590 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 TE_DEFAULT_RECUR
495a0 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23 20 SIVE_TRIGGERS.#
495b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
495c0 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f FAULT_RECURSIVE_
495d0 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64 69 TRIGGERS 0.#endi
495e0 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 f../*.** Provide
495f0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 a default value
49600 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50 for SQLITE_TEMP
49610 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69 _STORE in case i
49620 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 t is not specifi
49630 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d ed.** on the com
49640 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66 mand-line.*/.#if
49650 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
49660 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20 _STORE.# define
49670 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
49680 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a E 1.#endif../*.*
49690 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64 * GCC does not d
496a0 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74 efine the offset
496b0 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65 of() macro so we
496c0 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69 'll have to do i
496d0 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a t.** ourselves..
496e0 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65 */.#ifndef offse
496f0 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73 tof.#define offs
49700 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46 etof(STRUCTURE,F
49710 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68 IELD) ((int)((ch
49720 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45 ar*)&((STRUCTURE
49730 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65 *)0)->FIELD)).#e
49740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 ndif../*.** Chec
49750 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 k to see if this
49760 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42 machine uses EB
49770 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c CDIC. (Yes, bel
49780 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f ieve it or.** no
49790 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69 t, there are sti
497a0 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20 ll machines out
497b0 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45 there that use E
497c0 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 BCDIC.).*/.#if '
497d0 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64 A' == '\301'.# d
497e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43 efine SQLITE_EBC
497f0 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 DIC 1.#else.# de
49800 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49 fine SQLITE_ASCI
49810 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a I 1.#endif../*.*
49820 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e * Integers of kn
49830 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73 own sizes. Thes
49840 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74 e typedefs might
49850 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68 change for arch
49860 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65 itectures.** whe
49870 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72 re the sizes ver
49880 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72 y. Preprocessor
49890 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69 macros are avai
498a0 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68 lable so that th
498b0 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62 e.** types can b
498c0 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72 e conveniently r
498d0 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 edefined at comp
498e0 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20 ile-type. Like
498f0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 this:.**.**
49900 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54 cc '-DUINTPT
49910 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 R_TYPE=long long
49920 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 int' ....*/.#if
49930 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 ndef UINT32_TYPE
49940 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 .# ifdef HAVE_UI
49950 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65 NT32_T.# define
49960 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e UINT32_TYPE uin
49970 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 t32_t.# else.#
49980 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 define UINT32_TY
49990 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a PE unsigned int.
499a0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
499b0 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59 ifndef UINT16_TY
499c0 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f PE.# ifdef HAVE_
499d0 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69 UINT16_T.# defi
499e0 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ne UINT16_TYPE u
499f0 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 int16_t.# else.#
49a00 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f define UINT16_
49a10 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68 TYPE unsigned sh
49a20 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a ort int.# endif.
49a30 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 #endif.#ifndef I
49a40 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 NT16_TYPE.# ifde
49a50 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23 f HAVE_INT16_T.#
49a60 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 define INT16_T
49a70 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c YPE int16_t.# el
49a80 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 se.# define INT
49a90 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e 16_TYPE short in
49aa0 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 t.# endif.#endif
49ab0 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54 .#ifndef UINT8_T
49ac0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
49ad0 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 _UINT8_T.# defi
49ae0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69 ne UINT8_TYPE ui
49af0 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 nt8_t.# else.#
49b00 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 define UINT8_TYP
49b10 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a E unsigned char.
49b20 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 # endif.#endif.#
49b30 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 ifndef INT8_TYPE
49b40 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e .# ifdef HAVE_IN
49b50 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49 T8_T.# define I
49b60 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a NT8_TYPE int8_t.
49b70 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
49b80 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65 INT8_TYPE signe
49b90 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 d char.# endif.#
49ba0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f endif.#ifndef LO
49bb0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20 NGDOUBLE_TYPE.#
49bc0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c define LONGDOUBL
49bd0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62 E_TYPE long doub
49be0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 le.#endif.typede
49bf0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 f sqlite_int64 i
49c00 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64; /*
49c10 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 8-byte signed in
49c20 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 teger */.typedef
49c30 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75 sqlite_uint64 u
49c40 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38 64; /* 8
49c50 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 -byte unsigned i
49c60 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 nteger */.typede
49c70 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33 f UINT32_TYPE u3
49c80 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2; /*
49c90 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 4-byte unsigned
49ca0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
49cb0 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75 ef UINT16_TYPE u
49cc0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 16; /*
49cd0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 2-byte unsigned
49ce0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49cf0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69 def INT16_TYPE i
49d00 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 16; /
49d10 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 2-byte signed
49d20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
49d30 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38 ef UINT8_TYPE u8
49d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
49d50 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 1-byte unsigned
49d60 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49d70 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38 def INT8_TYPE i8
49d80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
49d90 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 1-byte signed
49da0 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a integer */../*.*
49db0 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 * SQLITE_MAX_U32
49dc0 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74 61 is a u64 consta
49dd0 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20 6d nt that is the m
49de0 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75 65 aximum u64 value
49df0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 .** that can be
49e00 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32 20 stored in a u32
49e10 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 without loss of
49e20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75 65 data. The value
49e30 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30 30 .** is 0x0000000
49e40 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74 20 0ffffffff. But
49e50 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72 6b because of quirk
49e60 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69 6c s of some compil
49e70 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65 20 ers, we.** have
49e80 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20 76 to specify the v
49e90 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73 73 alue in the less
49ea0 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e 65 intuitive manne
49eb0 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65 66 r shown:.*/.#def
49ec0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 ine SQLITE_MAX_U
49ed0 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c 3c 32 ((((u64)1)<<
49ee0 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 32)-1)../*.** Ma
49ef0 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e cros to determin
49f00 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61 e whether the ma
49f10 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20 chine is big or
49f20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a little endian,.*
49f30 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72 * evaluated at r
49f40 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 untime..*/.#ifde
49f50 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d f SQLITE_AMALGAM
49f60 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 ATION.SQLITE_PRI
49f70 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 VATE const int s
49f80 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 qlite3one = 1;.#
49f90 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 else.SQLITE_PRIV
49fa0 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 ATE const int sq
49fb0 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66 lite3one;.#endif
49fc0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38 .#if defined(i38
49fd0 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 6) || defined(__
49fe0 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e i386__) || defin
49ff0 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 20 ed(_M_IX86)\.
4a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a010 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66 || def
4a020 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c ined(__x86_64) |
4a030 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f | defined(__x86_
4a040 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 64__).# define S
4a050 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 QLITE_BIGENDIAN
4a060 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51 0.# define SQ
4a070 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 LITE_LITTLEENDIA
4a080 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c N 1.# define SQL
4a090 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 ITE_UTF16NATIVE
4a0a0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a SQLITE_UTF16LE.
4a0b0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
4a0c0 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 QLITE_BIGENDIAN
4a0d0 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 (*(char *)(&s
4a0e0 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23 qlite3one)==0).#
4a0f0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c define SQLITE_L
4a100 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63 ITTLEENDIAN (*(c
4a110 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f har *)(&sqlite3o
4a120 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65 ne)==1).# define
4a130 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
4a140 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45 IVE (SQLITE_BIGE
4a150 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46 NDIAN?SQLITE_UTF
4a160 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31 16BE:SQLITE_UTF1
4a170 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 6LE).#endif../*.
4a180 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72 ** Constants for
4a190 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64 the largest and
4a1a0 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 smallest possib
4a1b0 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 le 64-bit signed
4a1c0 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 integers..** Th
4a1d0 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64 ese macros are d
4a1e0 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20 esigned to work
4a1f0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74 correctly on bot
4a200 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d h 32-bit and 64-
4a210 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73 bit.** compilers
4a220 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52 ..*/.#define LAR
4a230 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66 GEST_INT64 (0xf
4a240 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30 fffffff|(((i64)0
4a250 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29 x7fffffff)<<32))
4a260 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53 .#define SMALLES
4a270 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d T_INT64 (((i64)-
4a280 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54 1) - LARGEST_INT
4a290 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 6e 64)../* .** Roun
4a2a0 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f d up a number to
4a2b0 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 the next larger
4a2c0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20 multiple of 8.
4a2d0 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a This is used.**
4a2e0 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65 to force 8-byte
4a2f0 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34 alignment on 64
4a300 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72 -bit architectur
4a310 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 es..*/.#define R
4a320 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 28 OUND8(x) (((
4a330 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a x)+7)&~7)../*.**
4a340 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74 Round down to t
4a350 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 69 he nearest multi
4a360 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 66 ple of 8.*/.#def
4a370 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 78 ine ROUNDDOWN8(x
4a380 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a 2a ) ((x)&~7)../*.*
4a390 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 * Assert that th
4a3a0 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 61 e pointer X is a
4a3b0 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 ligned to an 8-b
4a3c0 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20 54 yte boundary. T
4a3d0 68 69 73 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 his.** macro is
4a3e0 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e used only within
4a3f0 20 61 73 73 65 72 74 28 29 20 74 6f 20 76 65 72 assert() to ver
4a400 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 64 ify that the cod
4a410 65 20 67 65 74 73 0a 2a 2a 20 61 6c 6c 20 61 6c e gets.** all al
4a420 69 67 6e 6d 65 6e 74 20 72 65 73 74 72 69 63 74 ignment restrict
4a430 69 6f 6e 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a ions correct..**
4a440 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 53 .** Except, if S
4a450 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49 QLITE_4_BYTE_ALI
4a460 47 4e 45 44 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 GNED_MALLOC is d
4a470 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 efined, then the
4a480 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d .** underlying m
4a490 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e alloc() implemen
4a4a0 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 74 75 72 tion might retur
4a4b0 6e 20 75 73 20 34 2d 62 79 74 65 20 61 6c 69 67 n us 4-byte alig
4a4c0 6e 65 64 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 2e ned.** pointers.
4a4d0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 In that case,
4a4e0 6f 6e 6c 79 20 76 65 72 69 66 79 20 34 2d 62 79 only verify 4-by
4a4f0 74 65 20 61 6c 69 67 6e 6d 65 6e 74 2e 0a 2a 2f te alignment..*/
4a500 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 34 .#ifdef SQLITE_4
4a510 5f 42 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d 41 _BYTE_ALIGNED_MA
4a520 4c 4c 4f 43 0a 23 20 64 65 66 69 6e 65 20 45 49 LLOC.# define EI
4a530 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
4a540 4e 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72 NT(X) ((((char
4a550 2a 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 *)(X) - (char*)0
4a560 29 26 33 29 3d 3d 30 29 0a 23 65 6c 73 65 0a 23 )&3)==0).#else.#
4a570 20 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42 59 define EIGHT_BY
4a580 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29 20 TE_ALIGNMENT(X)
4a590 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29 20 ((((char*)(X)
4a5a0 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d 3d - (char*)0)&7)==
4a5b0 30 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0).#endif.../*.*
4a5c0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
4a5d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
4a5e0 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 tructure is used
4a5f0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75 to store the bu
4a600 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 sy-handler.** ca
4a610 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76 llback for a giv
4a620 65 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 en sqlite handle
4a630 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c . .**.** The sql
4a640 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 ite.busyHandler
4a650 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71 member of the sq
4a660 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74 lite struct cont
4a670 61 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a ains the busy.**
4a680 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 callback for th
4a690 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
4a6a0 65 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70 e. Each pager op
4a6b0 65 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c ened via the sql
4a6c0 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 ite.** handle is
4a6d0 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65 passed a pointe
4a6e0 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 r to sqlite.busy
4a6f0 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73 Handler. The bus
4a700 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c y-handler.** cal
4a710 6c 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74 lback is current
4a720 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 ly invoked only
4a730 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65 from within page
4a740 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 r.c..*/.typedef
4a750 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c struct BusyHandl
4a760 65 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a er BusyHandler;.
4a770 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c struct BusyHandl
4a780 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 er {. int (*xFu
4a790 6e 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b nc)(void *,int);
4a7a0 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61 /* The busy ca
4a7b0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 llback */. void
4a7c0 20 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20 *pArg;
4a7d0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
4a7e0 61 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c arg to busy call
4a7f0 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 back */. int nB
4a800 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 usy;
4a810 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e /* Incremen
4a820 74 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75 ted with each bu
4a830 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f sy call */.};../
4a840 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 *.** Name of the
4a850 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
4a860 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 table. The mas
4a870 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 ter database tab
4a880 6c 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69 le.** is a speci
4a890 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f al table that ho
4a8a0 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e lds the names an
4a8b0 64 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20 d attributes of
4a8c0 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c all.** user tabl
4a8d0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a es and indices..
4a8e0 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 */.#define MASTE
4a8f0 52 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71 R_NAME "sq
4a900 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 lite_master".#de
4a910 66 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 fine TEMP_MASTER
4a920 5f 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 _NAME "sqlite_t
4a930 65 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a emp_master"../*.
4a940 2a 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 ** The root-page
4a950 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 of the master d
4a960 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a atabase table..*
4a970 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 /.#define MASTER
4a980 5f 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f _ROOT 1../
4a990 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 *.** The name of
4a9a0 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c the schema tabl
4a9b0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 e..*/.#define SC
4a9c0 48 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28 HEMA_TABLE(x) (
4a9d0 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 (!OMIT_TEMPDB)&&
4a9e0 28 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 (x==1)?TEMP_MAST
4a9f0 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e ER_NAME:MASTER_N
4aa00 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f AME)../*.** A co
4aa10 6e 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 nvenience macro
4aa20 74 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65 that returns the
4aa30 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
4aa40 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 nts in.** an arr
4aa50 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 ay..*/.#define A
4aa60 72 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28 rraySize(X) (
4aa70 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f (int)(sizeof(X)/
4aa80 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a sizeof(X[0])))..
4aa90 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
4aaa0 69 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64 ing value as a d
4aab0 65 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 estructor means
4aac0 74 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62 to use sqlite3Db
4aad0 46 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 Free()..** This
4aae0 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 is an internal e
4aaf0 78 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 xtension to SQLI
4ab00 54 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51 TE_STATIC and SQ
4ab10 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a LITE_TRANSIENT..
4ab20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
4ab30 45 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71 E_DYNAMIC ((sq
4ab40 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 lite3_destructor
4ab50 5f 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46 _type)sqlite3DbF
4ab60 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ree)../*.** When
4ab70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
4ab80 20 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20 is defined, it
4ab90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 means that the t
4aba0 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 arget platform d
4abb0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f oes.** not suppo
4abc0 72 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74 rt Writable Stat
4abd0 69 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75 ic Data (WSD) su
4abe0 63 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 ch as global and
4abf0 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 static variable
4ac00 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 s..** All variab
4ac10 6c 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20 les must either
4ac20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 be on the stack
4ac30 6f 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 or dynamically a
4ac40 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a llocated from.**
4ac50 20 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e the heap. When
4ac60 20 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72 WSD is unsuppor
4ac70 74 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c ted, the variabl
4ac80 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 e declarations s
4ac90 63 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f cattered.** thro
4aca0 75 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74 ughout the SQLit
4acb0 65 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f e code must beco
4acc0 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 me constants ins
4acd0 74 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54 tead. The SQLIT
4ace0 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 E_WSD.** macro i
4acf0 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 s used for this
4ad00 70 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e purpose. And in
4ad10 73 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e stead of referen
4ad20 63 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c cing the variabl
4ad30 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 e.** directly, w
4ad40 65 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61 e use its consta
4ad50 6e 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c nt as a key to l
4ad60 6f 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69 ookup the run-ti
4ad70 6d 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 me allocated.**
4ad80 62 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64 buffer that hold
4ad90 73 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e s real variable.
4ada0 20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 The constant i
4adb0 73 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 s also the initi
4adc0 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 alizer.** for th
4add0 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 e run-time alloc
4ade0 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a ated buffer..**.
4adf0 2a 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20 ** In the usual
4ae00 63 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 case where WSD i
4ae10 73 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 s supported, the
4ae20 20 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 SQLITE_WSD and
4ae30 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 GLOBAL.** macros
4ae40 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 become no-ops a
4ae50 6e 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 nd have zero per
4ae60 66 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e formance impact.
4ae70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
4ae80 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 E_OMIT_WSD. #de
4ae90 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 fine SQLITE_WSD
4aea0 63 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 const. #define
4aeb0 47 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 GLOBAL(t,v) (*(t
4aec0 2a 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 *)sqlite3_wsd_fi
4aed0 6e 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 nd((void*)&(v),
4aee0 73 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 sizeof(v))). #d
4aef0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f efine sqlite3Glo
4af00 62 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c balConfig GLOBAL
4af10 28 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 (struct Sqlite3C
4af20 6f 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f onfig, sqlite3Co
4af30 6e 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 nfig).SQLITE_API
4af40 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 int sqlite3_w
4af50 73 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 sd_init(int N, i
4af60 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 nt J);.SQLITE_AP
4af70 49 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 I void *sqlite
4af80 33 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 3_wsd_find(void
4af90 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 *K, int L);.#els
4afa0 65 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 e. #define SQLI
4afb0 54 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e TE_WSD . #defin
4afc0 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a e GLOBAL(t,v) v.
4afd0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
4afe0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 3GlobalConfig sq
4aff0 6c 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 lite3Config.#end
4b000 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
4b010 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 llowing macros a
4b020 72 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 re used to suppr
4b030 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 ess compiler war
4b040 6e 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 nings and to.**
4b050 6d 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f make it clear to
4b060 20 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77 human readers w
4b070 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 hen a function p
4b080 61 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69 arameter is deli
4b090 62 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 berately .** lef
4b0a0 74 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20 t unused within
4b0b0 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 the body of a fu
4b0c0 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 nction. This usu
4b0d0 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 ally happens whe
4b0e0 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 n.** a function
4b0f0 69 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20 is called via a
4b100 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
4b110 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 . For example th
4b120 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 e .** implementa
4b130 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 tion of an SQL a
4b140 67 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61 ggregate step ca
4b150 6c 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 llback may not u
4b160 73 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 se the.** parame
4b170 74 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 ter indicating t
4b180 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 he number of arg
4b190 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f uments passed to
4b1a0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a the aggregate,.
4b1b0 2a 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 ** if it knows t
4b1c0 68 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f hat this is enfo
4b1d0 72 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a rced elsewhere..
4b1e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e **.** When a fun
4b1f0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
4b200 69 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61 is not used at a
4b210 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f ll within the bo
4b220 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e dy of a function
4b230 2c 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72 ,.** it is gener
4b240 61 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 ally named "NotU
4b250 73 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 sed" or "NotUsed
4b260 32 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 2" to make thing
4b270 73 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a s even clearer..
4b280 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73 ** However, thes
4b290 65 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 e macros may als
4b2a0 6f 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70 o be used to sup
4b2b0 70 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72 press warnings r
4b2c0 65 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 elated to.** par
4b2d0 61 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79 ameters that may
4b2e0 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 or may not be u
4b2f0 73 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e sed depending on
4b300 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 compilation opt
4b310 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 ions..** For exa
4b320 6d 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d mple those param
4b330 65 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 eters only used
4b340 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
4b350 65 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 ements. In these
4b360 0a 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61 .** cases the pa
4b370 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d rameters are nam
4b380 65 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73 ed as per the us
4b390 75 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e ual conventions.
4b3a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 .*/.#define UNUS
4b3b0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20 ED_PARAMETER(x)
4b3c0 28 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e (void)(x).#defin
4b3d0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 e UNUSED_PARAMET
4b3e0 45 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f ER2(x,y) UNUSED_
4b3f0 50 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 PARAMETER(x),UNU
4b400 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29 SED_PARAMETER(y)
4b410 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 ../*.** Forward
4b420 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74 references to st
4b430 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 ructures.*/.type
4b440 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e def struct AggIn
4b450 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 fo AggInfo;.type
4b460 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43 def struct AuthC
4b470 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65 ontext AuthConte
4b480 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 xt;.typedef stru
4b490 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41 ct AutoincInfo A
4b4a0 75 74 6f 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65 utoincInfo;.type
4b4b0 64 65 66 20 73 74 72 75 63 74 20 42 69 74 76 65 def struct Bitve
4b4c0 63 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64 65 c Bitvec;.typede
4b4d0 66 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 20 f struct RowSet
4b4e0 52 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66 20 RowSet;.typedef
4b4f0 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 struct CollSeq C
4b500 6f 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66 20 ollSeq;.typedef
4b510 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f struct Column Co
4b520 6c 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 lumn;.typedef st
4b530 72 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70 65 ruct Db Db;.type
4b540 64 65 66 20 73 74 72 75 63 74 20 53 63 68 65 6d def struct Schem
4b550 61 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 a Schema;.typede
4b560 66 20 73 74 72 75 63 74 20 45 78 70 72 20 45 78 f struct Expr Ex
4b570 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 pr;.typedef stru
4b580 63 74 20 45 78 70 72 4c 69 73 74 20 45 78 70 72 ct ExprList Expr
4b590 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 List;.typedef st
4b5a0 72 75 63 74 20 45 78 70 72 53 70 61 6e 20 45 78 ruct ExprSpan Ex
4b5b0 70 72 53 70 61 6e 3b 0a 74 79 70 65 64 65 66 20 prSpan;.typedef
4b5c0 73 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79 struct FKey FKey
4b5d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b5e0 20 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66 FuncDef FuncDef
4b5f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b600 20 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e FuncDefHash Fun
4b610 63 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65 cDefHash;.typede
4b620 66 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 f struct IdList
4b630 49 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 IdList;.typedef
4b640 73 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 struct Index Ind
4b650 65 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ex;.typedef stru
4b660 63 74 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 49 ct IndexSample I
4b670 6e 64 65 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65 ndexSample;.type
4b680 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c def struct KeyCl
4b690 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 ass KeyClass;.ty
4b6a0 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 pedef struct Key
4b6b0 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 Info KeyInfo;.ty
4b6c0 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f pedef struct Loo
4b6d0 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 kaside Lookaside
4b6e0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b6f0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c LookasideSlot L
4b700 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 ookasideSlot;.ty
4b710 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64 pedef struct Mod
4b720 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 ule Module;.type
4b730 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43 def struct NameC
4b740 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 ontext NameConte
4b750 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 xt;.typedef stru
4b760 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a ct Parse Parse;.
4b770 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
4b780 61 76 65 70 6f 69 6e 74 20 53 61 76 65 70 6f 69 avepoint Savepoi
4b790 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 nt;.typedef stru
4b7a0 63 74 20 53 65 6c 65 63 74 20 53 65 6c 65 63 74 ct Select Select
4b7b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b7c0 20 53 72 63 4c 69 73 74 20 53 72 63 4c 69 73 74 SrcList SrcList
4b7d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b7e0 20 53 74 72 41 63 63 75 6d 20 53 74 72 41 63 63 StrAccum StrAcc
4b7f0 75 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 um;.typedef stru
4b800 63 74 20 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a ct Table Table;.
4b810 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 typedef struct T
4b820 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f ableLock TableLo
4b830 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ck;.typedef stru
4b840 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a ct Token Token;.
4b850 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 typedef struct T
4b860 72 69 67 67 65 72 50 72 67 20 54 72 69 67 67 65 riggerPrg Trigge
4b870 72 50 72 67 3b 0a 74 79 70 65 64 65 66 20 73 74 rPrg;.typedef st
4b880 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 ruct TriggerStep
4b890 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 TriggerStep;.ty
4b8a0 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 pedef struct Tri
4b8b0 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 gger Trigger;.ty
4b8c0 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 pedef struct Unp
4b8d0 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 ackedRecord Unpa
4b8e0 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 ckedRecord;.type
4b8f0 64 65 66 20 73 74 72 75 63 74 20 56 54 61 62 6c def struct VTabl
4b900 65 20 56 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 e VTable;.typede
4b910 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 f struct Walker
4b920 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 Walker;.typedef
4b930 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e struct WherePlan
4b940 20 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 WherePlan;.type
4b950 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 def struct Where
4b960 49 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a Info WhereInfo;.
4b970 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 typedef struct W
4b980 68 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c hereLevel WhereL
4b990 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 evel;../*.** Def
4b9a0 65 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 er sourcing vdbe
4b9b0 2e 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 .h and btree.h u
4b9c0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 ntil after the "
4b9d0 75 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 u8" and .** "Bus
4b9e0 79 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 yHandler" typede
4b9f0 66 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 fs. vdbe.h also
4ba00 72 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f requires a few o
4ba10 66 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 f the opaque.**
4ba20 70 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 pointer types (i
4ba30 2e 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 .e. FuncDef) def
4ba40 69 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f ined above..*/./
4ba50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
4ba60 6e 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 nclude btree.h i
4ba70 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
4ba80 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
4bab0 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e egin file btree.
4bac0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4baf0 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d *.** 2001 Septem
4bb00 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 15.**.** The
4bb10 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
4bb20 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
4bb30 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
4bb40 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
4bb50 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
4bb60 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
4bb70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
4bb80 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
4bb90 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
4bba0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
4bbb0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
4bbc0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
4bbd0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
4bbe0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
4bbf0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
4bc00 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
4bc10 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
4bc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bc60 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 *****.** This he
4bc70 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 ader file define
4bc80 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 s the interface
4bc90 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 that the sqlite
4bca0 42 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 B-Tree file.** s
4bcb0 75 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 ubsystem. See c
4bcc0 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 omments in the s
4bcd0 6f 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 ource code for a
4bce0 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 detailed descri
4bcf0 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 ption.** of what
4bd00 20 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 each interface
4bd10 72 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2f routine does..*/
4bd20 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f .#ifndef _BTREE_
4bd30 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 H_.#define _BTRE
4bd40 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 E_H_../* TODO: T
4bd50 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 his definition i
4bd60 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 s just included
4bd70 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 so other modules
4bd80 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 compile. It.**
4bd90 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 needs to be revi
4bda0 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e sited..*/.#defin
4bdb0 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 e SQLITE_N_BTREE
4bdc0 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 _META 10../*.**
4bdd0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f If defined as no
4bde0 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 n-zero, auto-vac
4bdf0 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 uum is enabled b
4be00 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 y default. Other
4be10 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 wise.** it must
4be20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 be turned on for
4be30 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 each database u
4be40 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 sing "PRAGMA aut
4be50 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a o_vacuum = 1"..*
4be60 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
4be70 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
4be80 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 UUM. #define SQ
4be90 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
4bea0 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 OVACUUM 0.#endif
4beb0 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f ..#define BTREE_
4bec0 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 AUTOVACUUM_NONE
4bed0 30 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 0 /* Do n
4bee0 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 ot do auto-vacuu
4bef0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 m */.#define BTR
4bf00 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 EE_AUTOVACUUM_FU
4bf10 4c 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 LL 1 /* D
4bf20 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 o full auto-vacu
4bf30 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 um */.#define BT
4bf40 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 REE_AUTOVACUUM_I
4bf50 4e 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 NCR 2 /*
4bf60 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 Incremental vacu
4bf70 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 um */../*.** For
4bf80 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
4bf90 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a s of structure.*
4bfa0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
4bfb0 20 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 Btree Btree;.ty
4bfc0 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43 pedef struct BtC
4bfd0 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a ursor BtCursor;.
4bfe0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
4bff0 74 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64 tShared BtShared
4c000 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4c010 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 BtreeMutexArray
4c020 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 BtreeMutexArray
4c030 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 ;../*.** This st
4c040 72 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 ructure records
4c050 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 all of the Btree
4c060 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 s that need to h
4c070 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 old.** a mutex b
4c080 65 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 efore we enter s
4c090 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 qlite3VdbeExec()
4c0a0 2e 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72 . The Btrees ar
4c0b0 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 e.** are placed
4c0c0 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f in aBtree[] in o
4c0d0 72 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d rder of aBtree[]
4c0e0 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 ->pBt. That way
4c0f0 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 ,.** we can alwa
4c100 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f ys lock and unlo
4c110 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 ck them all quic
4c120 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 kly..*/.struct B
4c130 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b treeMutexArray {
4c140 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 . int nMutex;.
4c150 20 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 Btree *aBtree[S
4c160 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
4c170 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 ED+1];.};...SQLI
4c180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c190 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 qlite3BtreeOpen(
4c1a0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
4c1b0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e Filename, /* N
4c1c0 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
4c1d0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
4c1e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
4c1f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 /* As
4c200 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 sociated databas
4c210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
4c220 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 Btree **ppBtre
4c230 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 e, /* Re
4c240 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a turn open Btree*
4c250 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
4c260 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
4c270 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a /* Flags */.
4c280 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 int vfsFlags
4c290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
4c2a0 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
4c2b0 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a gh to VFS open *
4c2c0 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 /.);../* The fla
4c2d0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
4c2e0 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e sqlite3BtreeOpen
4c2f0 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 can be the bitw
4c300 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a ise or of the.**
4c310 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 following value
4c320 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 s..**.** NOTE:
4c330 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 These values mus
4c340 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 t match the corr
4c350 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f esponding PAGER_
4c360 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 values in.** pa
4c370 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ger.h..*/.#defin
4c380 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 e BTREE_OMIT_JOU
4c390 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e RNAL 1 /* Do n
4c3a0 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 ot use journal.
4c3b0 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a No argument */.
4c3c0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f #define BTREE_NO
4c3d0 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f _READLOCK 2 /
4c3e0 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 * Omit readlocks
4c3f0 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c on readonly fil
4c400 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 es */.#define BT
4c410 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 REE_MEMORY
4c420 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 4 /* In-memor
4c430 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 y DB. No argume
4c440 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 nt */.#define BT
4c450 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 REE_READONLY
4c460 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 8 /* Open the
4c470 20 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 database in rea
4c480 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 d-only mode */.#
4c490 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 define BTREE_REA
4c4a0 44 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a DWRITE 16 /*
4c4b0 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 Open for both r
4c4c0 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 eading and writi
4c4d0 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 ng */.#define BT
4c4e0 52 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 REE_CREATE
4c4f0 20 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 32 /* Create t
4c500 68 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69 he database if i
4c510 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 t does not exist
4c520 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 */..SQLITE_PRIV
4c530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c540 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a treeClose(Btree*
4c550 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c560 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c570 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 eeSetCacheSize(B
4c580 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 tree*,int);.SQLI
4c590 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c5a0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 qlite3BtreeSetSa
4c5b0 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a fetyLevel(Btree*
4c5c0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
4c5d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c5e0 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 lite3BtreeSyncDi
4c5f0 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a sabled(Btree*);.
4c600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c610 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
4c620 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
4c630 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 73 69 *p, int nPagesi
4c640 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ze, int nReserve
4c650 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a 53 51 4c , int eFix);.SQL
4c660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c670 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
4c680 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b ageSize(Btree*);
4c690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c6a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c6b0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 MaxPageCount(Btr
4c6c0 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
4c6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c6e0 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 ite3BtreeGetRese
4c6f0 72 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c rve(Btree*);.SQL
4c700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c710 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 sqlite3BtreeSetA
4c720 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 utoVacuum(Btree
4c730 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
4c740 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c750 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 te3BtreeGetAutoV
4c760 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a acuum(Btree *);.
4c770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c780 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 nt sqlite3BtreeB
4c790 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a eginTrans(Btree*
4c7a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
4c7b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c7c0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
4c7d0 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e eOne(Btree*, con
4c7e0 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
4c7f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c800 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c810 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
4c820 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c830 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c840 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 ite3BtreeCommit(
4c850 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c860 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c870 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
4c880 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c890 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c8a0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 ite3BtreeBeginSt
4c8b0 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a mt(Btree*,int);.
4c8c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c8d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4c8e0 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 reateTable(Btree
4c8f0 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61 *, int*, int fla
4c900 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 gs);.SQLITE_PRIV
4c910 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c920 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 treeIsInTrans(Bt
4c930 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4c940 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c950 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 3BtreeIsInReadTr
4c960 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ans(Btree*);.SQL
4c970 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c980 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
4c990 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a Backup(Btree*);.
4c9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4c9b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 oid *sqlite3Btre
4c9c0 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c eSchema(Btree *,
4c9d0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f int, void(*)(vo
4c9e0 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 id *));.SQLITE_P
4c9f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4ca00 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 e3BtreeSchemaLoc
4ca10 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 65 ked(Btree *pBtre
4ca20 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
4ca30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4ca40 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 reeLockTable(Btr
4ca50 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 ee *pBtree, int
4ca60 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 iTab, u8 isWrite
4ca70 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Lock);.SQLITE_PR
4ca80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4ca90 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
4caa0 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e Btree *, int, in
4cab0 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
4cac0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
4cad0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
4cae0 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 ilename(Btree *)
4caf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4cb00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
4cb10 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 ite3BtreeGetJour
4cb20 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 nalname(Btree *)
4cb30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4cb40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4cb50 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 eCopyFile(Btree
4cb60 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 *, Btree *);..SQ
4cb70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4cb80 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
4cb90 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 rVacuum(Btree *)
4cba0 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 ;../* The flags
4cbb0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
4cbc0 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
4cbd0 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 able can be the
4cbe0 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 bitwise OR.** of
4cbf0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
4cc00 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lags:.*/.#define
4cc10 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 BTREE_INTKEY
4cc20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 1 /* Table
4cc30 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 has only 64-bit
4cc40 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b signed integer k
4cc50 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 eys */.#define B
4cc60 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 TREE_ZERODATA
4cc70 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 2 /* Table ha
4cc80 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f s keys only - no
4cc90 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 data */.#define
4cca0 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 BTREE_LEAFDATA
4ccb0 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 4 /* Data s
4ccc0 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 tored in leaves
4ccd0 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 only. Implies I
4cce0 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 NTKEY */..SQLITE
4ccf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4cd00 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
4cd10 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 le(Btree*, int,
4cd20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
4cd30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4cd40 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
4cd50 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e (Btree*, int, in
4cd60 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
4cd70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4cd80 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
4cd90 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 ors(Btree*, int)
4cda0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
4cdb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
4cdc0 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 reeGetMeta(Btree
4cdd0 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64 *pBtree, int id
4cde0 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b x, u32 *pValue);
4cdf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4ce00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4ce10 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 UpdateMeta(Btree
4ce20 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 *, int idx, u32
4ce30 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 value);../*.** T
4ce40 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
4ce50 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 ter to sqlite3Bt
4ce60 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73 71 reeGetMeta or sq
4ce70 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
4ce80 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 Meta.** should b
4ce90 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c e one of the fol
4cea0 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 54 lowing values. T
4ceb0 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
4cec0 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 0a s are assigned .
4ced0 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 20 ** to constants
4cee0 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 73 so that the offs
4cef0 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 et of the corres
4cf00 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 6e ponding field in
4cf10 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 an.** SQLite da
4cf20 74 61 62 61 73 65 20 68 65 61 64 65 72 20 6d 61 tabase header ma
4cf30 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e 67 y be found using
4cf40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
4cf50 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 ormula:.**.**
4cf60 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28 69 offset = 36 + (i
4cf70 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46 6f dx * 4).**.** Fo
4cf80 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 r example, the f
4cf90 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20 66 ree-page-count f
4cfa0 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 20 ield is located
4cfb0 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 33 at byte offset 3
4cfc0 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 6 of.** the data
4cfd0 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 base file header
4cfe0 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75 75 . The incr-vacuu
4cff0 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73 20 m-flag field is
4d000 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62 79 located at.** by
4d010 74 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d 3d te offset 64 (==
4d020 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64 65 36+4*7)..*/.#de
4d030 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45 5f fine BTREE_FREE_
4d040 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20 30 PAGE_COUNT 0
4d050 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 53 .#define BTREE_S
4d060 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20 20 CHEMA_VERSION
4d070 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54 52 1.#define BTR
4d080 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 20 EE_FILE_FORMAT
4d090 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
4d0a0 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 BTREE_DEFAULT_C
4d0b0 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64 65 ACHE_SIZE 3.#de
4d0c0 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47 45 fine BTREE_LARGE
4d0d0 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20 34 ST_ROOT_PAGE 4
4d0e0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 54 .#define BTREE_T
4d0f0 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 20 EXT_ENCODING
4d100 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54 52 5.#define BTR
4d110 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 20 EE_USER_VERSION
4d120 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 6.#define
4d130 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 BTREE_INCR_VACU
4d140 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 53 51 UM 7..SQ
4d150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d160 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
4d170 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 sor(. Btree*,
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d190 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
4d1a0 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Tree containing
4d1b0 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f table to open */
4d1c0 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
4d1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1e0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
4d1f0 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f of root page */
4d200 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 . int wrFlag,
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d220 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 /* 1 for
4d230 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 writing. 0 for
4d240 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 read-only */.
4d250 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c struct KeyInfo*,
4d260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d270 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 /* First ar
4d280 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 gument to compar
4d290 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
4d2a0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f BtCursor *pCurso
4d2b0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
4d2c0 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f /* Space to
4d2d0 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74 write cursor st
4d2e0 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 ructure */.);.SQ
4d2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d300 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
4d310 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 sorSize(void);.S
4d320 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4d330 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
4d340 75 72 73 6f 72 5a 65 72 6f 28 42 74 43 75 72 73 ursorZero(BtCurs
4d350 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 or*);..SQLITE_PR
4d360 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d370 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 3BtreeCloseCurso
4d380 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 r(BtCursor*);.SQ
4d390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d3a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
4d3b0 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 etoUnpacked(. B
4d3c0 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70 61 tCursor*,. Unpa
4d3d0 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 4b ckedRecord *pUnK
4d3e0 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 ey,. i64 intKey
4d3f0 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20 20 ,. int bias,.
4d400 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51 4c int *pRes.);.SQL
4d410 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d420 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
4d430 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 orHasMoved(BtCur
4d440 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c sor*, int*);.SQL
4d450 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d460 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 sqlite3BtreeDele
4d470 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 te(BtCursor*);.S
4d480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d490 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e t sqlite3BtreeIn
4d4a0 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 sert(BtCursor*,
4d4b0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
4d4c0 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20 20 , i64 nKey,.
4d4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f co
4d4f0 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
4d500 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 int nData,.
4d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
4d530 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69 61 t nZero, int bia
4d540 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c s, int seekResul
4d550 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4d560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4d570 72 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f reeFirst(BtCurso
4d580 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a r*, int *pRes);.
4d590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d5a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c nt sqlite3BtreeL
4d5b0 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 ast(BtCursor*, i
4d5c0 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 nt *pRes);.SQLIT
4d5d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d5e0 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 lite3BtreeNext(B
4d5f0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
4d600 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Res);.SQLITE_PRI
4d610 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d620 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f BtreeEof(BtCurso
4d630 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
4d640 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d650 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 treePrevious(BtC
4d660 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 ursor*, int *pRe
4d670 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 s);.SQLITE_PRIVA
4d680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4d690 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 reeKeySize(BtCur
4d6a0 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 sor*, i64 *pSize
4d6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d6d0 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 2a 2c eeKey(BtCursor*,
4d6e0 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
4d6f0 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 amt, void*);.SQ
4d700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
4d710 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
4d720 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 BtreeKeyFetch(Bt
4d730 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 Cursor*, int *pA
4d740 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 mt);.SQLITE_PRIV
4d750 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
4d760 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
4d770 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c Fetch(BtCursor*,
4d780 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c int *pAmt);.SQL
4d790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d7a0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
4d7b0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 Size(BtCursor*,
4d7c0 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c u32 *pSize);.SQL
4d7d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d7e0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
4d7f0 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 (BtCursor*, u32
4d800 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c offset, u32 amt,
4d810 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
4d820 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4d830 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 ite3BtreeSetCach
4d840 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 edRowid(BtCursor
4d850 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
4d860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d870 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 E sqlite3_int64
4d880 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 sqlite3BtreeGetC
4d890 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
4d8a0 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 sor*);..SQLITE_P
4d8b0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
4d8c0 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 ite3BtreeIntegri
4d8d0 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a 2c 20 tyCheck(Btree*,
4d8e0 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 int *aRoot, int
4d8f0 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e 74 2a nRoot, int, int*
4d900 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d910 45 20 73 74 72 75 63 74 20 50 61 67 65 72 20 2a E struct Pager *
4d920 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
4d930 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 r(Btree*);..SQLI
4d940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d950 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 qlite3BtreePutDa
4d960 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 ta(BtCursor*, u3
4d970 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
4d980 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 t, void*);.SQLIT
4d990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4d9a0 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 qlite3BtreeCache
4d9b0 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f Overflow(BtCurso
4d9c0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r *);.SQLITE_PRI
4d9d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4d9e0 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 3BtreeClearCurso
4d9f0 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a r(BtCursor *);..
4da00 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
4da10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4da20 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
4da30 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 rsorIsValid(BtCu
4da40 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a rsor*);.#endif..
4da50 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
4da60 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 53 MIT_BTREECOUNT.S
4da70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4da80 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
4da90 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 unt(BtCursor *,
4daa0 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a i64 *);.#endif..
4dab0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
4dac0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
4dad0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4dae0 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43 eeCursorInfo(BtC
4daf0 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e ursor*, int*, in
4db00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4db10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
4db20 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42 treeCursorList(B
4db30 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a tree*);.#endif..
4db40 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 /*.** If we are
4db50 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72 65 64 not using shared
4db60 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 cache, then the
4db70 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
4db80 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 73 20 .** use mutexes
4db90 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 42 74 to access the Bt
4dba0 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
4dbb0 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 65 0a s. So make the.
4dbc0 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c 65 61 ** Enter and Lea
4dbd0 76 65 20 70 72 6f 63 65 64 75 72 65 73 20 6e 6f ve procedures no
4dbe0 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 -ops..*/.#ifndef
4dbf0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
4dc00 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 RED_CACHE.SQLITE
4dc10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4dc20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
4dc30 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 r(Btree*);.SQLIT
4dc40 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4dc50 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
4dc60 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b erAll(sqlite3*);
4dc70 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
4dc80 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
4dc90 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65 20 73 r(X) .# define s
4dca0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
4dcb0 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 All(X).#endif..#
4dcc0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
4dcd0 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
4dce0 41 43 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f ACHE) && SQLITE_
4dcf0 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 THREADSAFE.SQLIT
4dd00 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4dd10 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
4dd20 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 ve(Btree*);.SQLI
4dd30 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4dd40 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
4dd50 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 terCursor(BtCurs
4dd60 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 or*);.SQLITE_PRI
4dd70 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4dd80 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
4dd90 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a sor(BtCursor*);.
4dda0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4ddb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4ddc0 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 eeLeaveAll(sqlit
4ddd0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
4dde0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4ddf0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
4de00 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 ayEnter(BtreeMut
4de10 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 exArray*);.SQLIT
4de20 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4de30 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
4de40 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 exArrayLeave(Btr
4de50 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a eeMutexArray*);.
4de60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4de70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4de80 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 eeMutexArrayInse
4de90 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 rt(BtreeMutexArr
4dea0 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 69 ay*, Btree*);.#i
4deb0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f fndef NDEBUG. /
4dec0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
4ded0 20 61 72 65 20 75 73 65 64 20 69 6e 73 69 64 65 are used inside
4dee0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
4def0 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 ents only. */.SQ
4df00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
4df10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 nt sqlite3BtreeH
4df20 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a oldsMutex(Btree*
4df30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4df40 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 E int sqlite3B
4df50 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
4df60 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 xes(sqlite3*);.#
4df70 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 20 64 endif.#else..# d
4df80 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
4df90 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66 eeLeave(X).# def
4dfa0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4dfb0 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a 23 EnterCursor(X).#
4dfc0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4dfd0 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 treeLeaveCursor(
4dfe0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 X).# define sqli
4dff0 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
4e000 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (X).# define sql
4e010 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
4e020 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64 65 rayEnter(X).# de
4e030 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 fine sqlite3Btre
4e040 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
4e050 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (X).# define sql
4e060 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
4e070 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a 0a rayInsert(X,Y)..
4e080 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4e090 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
4e0a0 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20 73 71 X) 1.# define sq
4e0b0 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
4e0c0 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 llMutexes(X) 1.#
4e0d0 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20 2f endif...#endif /
4e0e0 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a * _BTREE_H_ */..
4e0f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
4e100 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20 2a End of btree.h *
4e110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
4e140 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
4e150 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
4e160 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
4e170 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
4e180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
4e190 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
4e1a0 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 69 Include vdbe.h i
4e1b0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
4e1c0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
4e1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
4e1e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
4e1f0 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e Begin file vdbe.
4e200 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4e210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
4e230 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
4e240 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
4e250 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
4e260 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
4e270 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
4e280 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
4e290 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
4e2a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
4e2b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
4e2c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
4e2d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
4e2e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
4e2f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
4e300 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
4e310 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
4e320 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
4e330 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
4e340 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
4e350 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
4e360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e3a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 ******.** Header
4e3b0 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 69 file for the Vi
4e3c0 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 45 rtual DataBase E
4e3d0 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a 0a ngine (VDBE).**.
4e3e0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 64 ** This header d
4e3f0 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
4e400 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 face to the virt
4e410 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 ual database eng
4e420 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e 20 ine.** or VDBE.
4e430 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d The VDBE implem
4e440 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 74 ents an abstract
4e450 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 75 machine that ru
4e460 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 ns a.** simple p
4e470 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 73 rogram to access
4e480 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 and modify the
4e490 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 underlying datab
4e4a0 61 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ase..*/.#ifndef
4e4b0 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a _SQLITE_VDBE_H_.
4e4c0 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f #define _SQLITE_
4e4d0 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 VDBE_H_../*.** A
4e4e0 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20 single VDBE is
4e4f0 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 an opaque struct
4e500 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22 ure named "Vdbe"
4e510 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 . Only routines
4e520 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63 .** in the sourc
4e530 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62 e file sqliteVdb
4e540 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 e.c are allowed
4e550 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64 to see the insid
4e560 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 es.** of this st
4e570 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 ructure..*/.type
4e580 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20 def struct Vdbe
4e590 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 Vdbe;../*.** The
4e5a0 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f names of the fo
4e5b0 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65 llowing types de
4e5c0 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e clared in vdbeIn
4e5d0 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64 t.h are required
4e5e0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 .** for the Vdbe
4e5f0 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a Op definition..*
4e600 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
4e610 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75 VdbeFunc VdbeFu
4e620 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 nc;.typedef stru
4e630 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 70 65 ct Mem Mem;.type
4e640 64 65 66 20 73 74 72 75 63 74 20 53 75 62 50 72 def struct SubPr
4e650 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72 61 6d ogram SubProgram
4e660 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c ;../*.** A singl
4e670 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 e instruction of
4e680 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 the virtual mac
4e690 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 63 6f hine has an opco
4e6a0 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d 61 6e de.** and as man
4e6b0 79 20 61 73 20 74 68 72 65 65 20 6f 70 65 72 61 y as three opera
4e6c0 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74 72 75 nds. The instru
4e6d0 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 64 65 ction is recorde
4e6e0 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 74 61 d.** as an insta
4e6f0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
4e700 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 3a 0a wing structure:.
4e710 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 */.struct VdbeOp
4e720 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 {. u8 opcode;
4e730 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 /* What
4e740 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 operation to pe
4e750 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 rform */. signe
4e760 64 20 63 68 61 72 20 70 34 74 79 70 65 3b 20 2f d char p4type; /
4e770 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 34 5f * One of the P4_
4e780 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f xxx constants fo
4e790 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f 70 66 r p4 */. u8 opf
4e7a0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a lags; /*
4e7b0 20 4d 61 73 6b 20 6f 66 20 74 68 65 20 4f 50 46 Mask of the OPF
4e7c0 4c 47 5f 2a 20 66 6c 61 67 73 20 69 6e 20 6f 70 LG_* flags in op
4e7d0 63 6f 64 65 73 2e 68 20 2a 2f 0a 20 20 75 38 20 codes.h */. u8
4e7e0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p5;
4e7f0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 /* Fifth parame
4e800 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e ter is an unsign
4e810 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a ed character */.
4e820 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
4e830 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f /* First o
4e840 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
4e850 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p2;
4e860 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 /* Second parame
4e870 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a ter (often the j
4e880 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 ump destination)
4e890 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 */. int p3;
4e8a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
4e8b0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
4e8c0 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 */. union {
4e8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 /* fou
4e8e0 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f rth parameter */
4e8f0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 . int i;
4e900 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
4e910 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 nteger value if
4e920 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
4e930 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b */. void *p;
4e940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e950 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 * Generic pointe
4e960 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a r */. char *z
4e970 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4e980 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
4e990 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 ta for string (c
4e9a0 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 har array) types
4e9b0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 */. i64 *pI6
4e9c0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 4; /
4e9d0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4e9e0 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a pe is P4_INT64 *
4e9f0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 /. double *pR
4ea00 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eal; /*
4ea10 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4ea20 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 is P4_REAL */.
4ea30 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e FuncDef *pFun
4ea40 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 c; /* Use
4ea50 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4ea60 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 P4_FUNCDEF */.
4ea70 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 VdbeFunc *pVd
4ea80 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 beFunc; /* Use
4ea90 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4eaa0 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a P4_VDBEFUNC */.
4eab0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
4eac0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ll; /* Us
4ead0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4eae0 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a s P4_COLLSEQ */.
4eaf0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 Mem *pMem;
4eb00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
4eb10 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4eb20 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 s P4_MEM */.
4eb30 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 VTable *pVtab;
4eb40 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 /* Used w
4eb50 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
4eb60 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 _VTAB */. Key
4eb70 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
4eb80 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4eb90 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 p4type is P4_KE
4eba0 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 YINFO */. int
4ebb0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 *ai;
4ebc0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4ebd0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e p4type is P4_IN
4ebe0 54 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 TARRAY */. Su
4ebf0 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 bProgram *pProgr
4ec00 61 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 am; /* Used whe
4ec10 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 n p4type is P4_S
4ec20 55 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d UBPROGRAM */. }
4ec30 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 p4;.#ifdef SQLI
4ec40 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 TE_DEBUG. char
4ec50 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 *zComment;
4ec60 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 /* Comment t
4ec70 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 o improve readab
4ec80 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a ility */.#endif.
4ec90 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
4eca0 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 ILE. int cnt;
4ecb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4ecc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 * Number of time
4ecd0 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 s this instructi
4ece0 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20 on was executed
4ecf0 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b */. u64 cycles;
4ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4ed10 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e Total time spen
4ed20 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 t executing this
4ed30 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a instruction */.
4ed40 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 #endif.};.typede
4ed50 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 f struct VdbeOp
4ed60 56 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 VdbeOp;.../*.**
4ed70 41 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 A sub-routine us
4ed80 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
4ed90 61 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 a trigger progra
4eda0 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 m..*/.struct Sub
4edb0 50 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 Program {. Vdbe
4edc0 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 Op *aOp;
4edd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
4ede0 61 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f ay of opcodes fo
4edf0 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
4ee00 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 . int nOp;
4ee10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ee20 20 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 /* Elements in
4ee30 61 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e aOp[] */. int n
4ee40 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
4ee50 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
4ee60 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c er of memory cel
4ee70 6c 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 ls required */.
4ee80 20 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 int nCsr;
4ee90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4eea0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 * Number of curs
4eeb0 6f 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a ors required */.
4eec0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
4eed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4eee0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
4eef0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
4ef00 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 ructure */. voi
4ef10 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 d *token;
4ef20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 /* id
4ef30 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 that may be use
4ef40 64 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 d to recursive t
4ef50 72 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f riggers */.};../
4ef60 2a 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 *.** A smaller v
4ef70 65 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 ersion of VdbeOp
4ef80 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 used for the Vd
4ef90 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 beAddOpList() fu
4efa0 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a nction because.*
4efb0 2a 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 * it takes up le
4efc0 73 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 ss space..*/.str
4efd0 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b uct VdbeOpList {
4efe0 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 . u8 opcode;
4eff0 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f /* What o
4f000 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 peration to perf
4f010 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 orm */. signed
4f020 63 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 char p1; /*
4f030 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f First operand */
4f040 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 . signed char p
4f050 32 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 2; /* Second
4f060 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 parameter (ofte
4f070 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 n the jump desti
4f080 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 nation) */. sig
4f090 6e 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 ned char p3;
4f0a0 20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 /* Third parame
4f0b0 74 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 ter */.};.typede
4f0c0 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c f struct VdbeOpL
4f0d0 69 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a ist VdbeOpList;.
4f0e0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
4f0f0 61 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e alues of VdbeOp.
4f100 70 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e p4type.*/.#defin
4f110 65 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 e P4_NOTUSED
4f120 30 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 0 /* The P4 pa
4f130 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 rameter is not u
4f140 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 sed */.#define P
4f150 34 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 4_DYNAMIC (-1)
4f160 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
4f170 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 string obtained
4f180 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
4f190 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 oc() */.#define
4f1a0 50 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 P4_STATIC (-2)
4f1b0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
4f1c0 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 a static string
4f1d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f */.#define P4_CO
4f1e0 4c 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 LLSEQ (-4) /*
4f1f0 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
4f200 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 to a CollSeq str
4f210 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
4f220 65 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d e P4_FUNCDEF (-
4f230 35 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 5) /* P4 is a p
4f240 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 ointer to a Func
4f250 44 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f Def structure */
4f260 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 .#define P4_KEYI
4f270 4e 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 NFO (-6) /* P4
4f280 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4f290 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
4f2a0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4f2b0 50 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 P4_VDBEFUNC (-7)
4f2c0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4f2d0 6e 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 nter to a VdbeFu
4f2e0 6e 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a nc structure */.
4f2f0 23 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 #define P4_MEM
4f300 20 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 (-8) /* P4
4f310 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
4f320 61 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 a Mem* struct
4f330 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4f340 34 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 4_TRANSIENT (-9)
4f350 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4f360 74 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 ter to a transie
4f370 6e 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 nt string */.#de
4f380 66 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 fine P4_VTAB
4f390 20 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 (-10) /* P4 is
4f3a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
4f3b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 sqlite3_vtab str
4f3c0 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
4f3d0 65 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d e P4_MPRINTF (-
4f3e0 31 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 11) /* P4 is a s
4f3f0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
4f400 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 rom sqlite3_mpri
4f410 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 ntf() */.#define
4f420 20 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 P4_REAL (-1
4f430 32 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2) /* P4 is a 64
4f440 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f -bit floating po
4f450 69 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 int value */.#de
4f460 66 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 fine P4_INT64
4f470 20 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 (-13) /* P4 is
4f480 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 a 64-bit signed
4f490 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 integer */.#defi
4f4a0 6e 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 ne P4_INT32 (
4f4b0 2d 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -14) /* P4 is a
4f4c0 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 32-bit signed in
4f4d0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
4f4e0 20 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 P4_INTARRAY (-1
4f4f0 35 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 5) /* P4 is a ve
4f500 63 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 ctor of 32-bit i
4f510 6e 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 ntegers */.#defi
4f520 6e 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d ne P4_SUBPROGRAM
4f530 20 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 (-18) /* P4 is
4f540 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4f550 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 SubProgram struc
4f560 74 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e ture */../* When
4f570 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 adding a P4 arg
4f580 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b ument using P4_K
4f590 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f EYINFO, a copy o
4f5a0 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 f the KeyInfo st
4f5b0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 ructure.** is ma
4f5c0 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 de. That copy i
4f5d0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4f5e0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4f5f0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a ed. But if the.
4f600 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 ** argument is P
4f610 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
4f620 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e F, the passed in
4f630 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 pointer is used
4f640 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 . It still.** g
4f650 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 ets freed when t
4f660 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c he Vdbe is final
4f670 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c ized so it still
4f680 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 should be obtai
4f690 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 ned.** from a si
4f6a0 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f ngle sqliteMallo
4f6b0 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 c(). But no cop
4f6c0 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 y is made and th
4f6d0 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e e calling.** fun
4f6e0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f ction should *no
4f6f0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 t* try to free t
4f700 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 he KeyInfo..*/.#
4f710 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4f720 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a O_HANDOFF (-16).
4f730 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e #define P4_KEYIN
4f740 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 FO_STATIC (-17)
4f750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 ../*.** The Vdbe
4f760 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 .aColName array
4f770 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 contains 5n Mem
4f780 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 structures, wher
4f790 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e e n is the .** n
4f7a0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
4f7b0 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 of data returne
4f7c0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 d by the stateme
4f7d0 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 nt..*/.#define C
4f7e0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 OLNAME_NAME
4f7f0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 0.#define COLNAM
4f800 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 E_DECLTYPE 1.#de
4f810 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 fine COLNAME_DAT
4f820 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 ABASE 2.#define
4f830 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 COLNAME_TABLE
4f840 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 3.#define COLNA
4f850 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 ME_COLUMN 4.#i
4f860 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
4f870 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
4f880 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e TA.# define COLN
4f890 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 AME_N 5
4f8a0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
4f8b0 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d COLNAME_xxx sym
4f8c0 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 bols */.#else.#
4f8d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
4f8e0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 T_DECLTYPE.# d
4f8f0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4f900 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 1 /* S
4f910 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 tore only the na
4f920 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 me */.# else.#
4f930 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f define COLNAME_
4f940 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a N 2 /*
4f950 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 Store the name
4f960 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a and decltype */.
4f970 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a # endif.#endif..
4f980 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
4f990 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 ing macro conver
4f9a0 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 ts a relative ad
4f9b0 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 dress in the p2
4f9c0 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 field.** of a Vd
4f9d0 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 beOp structure i
4f9e0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e nto a negative n
4f9f0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a umber so that .*
4fa00 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 * sqlite3VdbeAdd
4fa10 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 OpList() knows t
4fa20 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20 hat the address
4fa30 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 is relative. Ca
4fa40 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 lling.** the mac
4fa50 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 ro again restore
4fa60 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a s the address..*
4fa70 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 /.#define ADDR(X
4fa80 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a ) (-1-(X))../*.
4fa90 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 ** The makefile
4faa0 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 scans the vdbe.c
4fab0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 source file and
4fac0 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 creates the "op
4fad0 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 codes.h".** head
4fae0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 er file that def
4faf0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f ines a number fo
4fb00 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 r each opcode us
4fb10 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a ed by the VDBE..
4fb20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4fb30 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 ** Include opcod
4fb40 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 es.h in the midd
4fb50 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a le of vdbe.h ***
4fb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fb70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4fb80 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 ** Begin file op
4fb90 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a codes.h ********
4fba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fbc0 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 */./* Automatica
4fbd0 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 lly generated.
4fbe0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f Do not edit */./
4fbf0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f * See the mkopco
4fc00 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 deh.awk script f
4fc10 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 or details */.#d
4fc20 65 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 efine OP_Goto
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 1.
4fc50 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 #define OP_Gosub
4fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc80 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 2.#define OP_Ret
4fc90 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 urn
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcb0 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 3.#define OP_Y
4fcc0 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 ield
4fcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fce0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 4.#define OP
4fcf0 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 20 20 20 20 _HaltIfNull
4fd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd10 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 5.#define
4fd20 4f 50 5f 48 61 6c 74 20 20 20 20 20 20 20 20 20 OP_Halt
4fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd40 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 6.#defin
4fd50 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 e OP_Integer
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd70 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 7.#def
4fd80 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 ine OP_Int64
4fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fda0 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 8.#d
4fdb0 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20 efine OP_Real
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdd0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 20 130
4fde0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fdf0 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 66 FLOAT */.#def
4fe00 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 ine OP_String8
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe20 20 20 20 20 20 20 20 20 20 20 20 39 34 20 20 20 94
4fe30 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 /* same as TK_ST
4fe40 52 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e RING */.#defin
4fe50 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20 e OP_String
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe70 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 9.#def
4fe80 69 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 ine OP_Null
4fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fea0 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 10.#d
4feb0 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 efine OP_Blob
4fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 11.
4fee0 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 #define OP_Varia
4fef0 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ble
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4ff10 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 2.#define OP_Mov
4ff20 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
4ff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff40 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 13.#define OP_C
4ff50 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 opy
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff70 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 14.#define OP
4ff80 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 _SCopy
4ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffa0 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 15.#define
4ffb0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 OP_ResultRow
4ffc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffd0 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 16.#defin
4ffe0 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 e OP_Concat
4fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50000 20 20 20 20 20 20 20 20 20 39 31 20 20 20 2f 2a 91 /*
50010 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 same as TK_CONC
50020 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 AT */.#define
50030 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 OP_Add
50040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50050 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 86 /* s
50060 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 ame as TK_PLUS
50070 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
50080 5f 53 75 62 74 72 61 63 74 20 20 20 20 20 20 20 _Subtract
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500a0 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d 87 /* sam
500b0 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 e as TK_MINUS
500c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d */.#define OP_M
500d0 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20 20 ultiply
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500f0 20 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 88 /* same
50100 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20 2a as TK_STAR *
50110 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 /.#define OP_Div
50120 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ide
50130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50140 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 89 /* same as
50150 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a TK_SLASH */.
50160 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 #define OP_Remai
50170 6e 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20 nder
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
50190 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 0 /* same as T
501a0 4b 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 K_REM */.#d
501b0 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 efine OP_CollSeq
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 17.
501e0 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74 #define OP_Funct
501f0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ion
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
50210 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 8.#define OP_Bit
50220 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 And
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50240 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 82 /* same as
50250 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a TK_BITAND */.
50260 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 #define OP_BitOr
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
50290 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 3 /* same as T
502a0 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 K_BITOR */.#d
502b0 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 efine OP_ShiftLe
502c0 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ft
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20 84
502e0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
502f0 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 LSHIFT */.#def
50300 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 ine OP_ShiftRigh
50310 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
50320 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20 85
50330 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 /* same as TK_RS
50340 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e HIFT */.#defin
50350 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 e OP_AddImm
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50370 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 20.#def
50380 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 ine OP_MustBeInt
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503a0 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 21.#d
503b0 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 efine OP_RealAff
503c0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 inity
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 22.
503e0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 #define OP_ToTex
503f0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
50410 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 1 /* same as T
50420 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 K_TO_TEXT */.#d
50430 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20 efine OP_ToBlob
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50450 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20 142
50460 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
50470 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66 TO_BLOB */.#def
50480 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 ine OP_ToNumeric
50490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504a0 20 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20 143
504b0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
504c0 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 _NUMERIC*/.#defi
504d0 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 ne OP_ToInt
504e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504f0 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 144 /
50500 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
50510 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 INT */.#define
50520 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 OP_ToReal
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50540 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20 145 /*
50550 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 same as TK_TO_RE
50560 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f AL */.#define O
50570 50 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20 P_Eq
50580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50590 20 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61 76 /* sa
505a0 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 me as TK_EQ
505b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
505c0 4e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Ne
505d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505e0 20 20 20 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 75 /* same
505f0 20 61 73 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 as TK_NE
50600 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 */.#define OP_Lt
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50630 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 79 /* same a
50640 73 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f s TK_LT */
50650 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 .#define OP_Le
50660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50680 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 78 /* same as
50690 54 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 TK_LE */.#
506a0 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 define OP_Gt
506b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 77
506d0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
506e0 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _GT */.#de
506f0 66 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20 fine OP_Ge
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50710 20 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20 80
50720 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 /* same as TK_G
50730 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
50740 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f ne OP_Permutatio
50750 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
50760 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 23.#de
50770 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 fine OP_Compare
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50790 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 24.#
507a0 64 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 define OP_Jump
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 25
507d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 .#define OP_And
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50800 36 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 69 /* same as
50810 54 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 TK_AND */.#
50820 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 define OP_Or
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 68
50850 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50860 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _OR */.#de
50870 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 fine OP_Not
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50890 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20 19
508a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
508b0 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 OT */.#defi
508c0 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 ne OP_BitNot
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508e0 20 20 20 20 20 20 20 20 20 20 39 33 20 20 20 2f 93 /
508f0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
50900 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 NOT */.#define
50910 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 OP_If
50920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50930 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 26.#defi
50940 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 ne OP_IfNot
50950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50960 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 27.#de
50970 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 fine OP_IsNull
50980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50990 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 73
509a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 /* same as TK_I
509b0 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 SNULL */.#defi
509c0 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 ne OP_NotNull
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509e0 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 74 /
509f0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
50a00 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
50a10 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 OP_Column
50a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a30 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 28.#defi
50a40 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 ne OP_Affinity
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a60 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 29.#de
50a70 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f fine OP_MakeReco
50a80 72 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rd
50a90 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 30.#
50aa0 64 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 define OP_Count
50ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 31
50ad0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 .#define OP_Save
50ae0 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 point
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b00 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 32.#define OP_Au
50b10 74 6f 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 toCommit
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b30 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 33.#define OP_
50b40 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 Transaction
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b60 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 34.#define O
50b70 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 P_ReadCookie
50b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b90 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 35.#define
50ba0 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 OP_SetCookie
50bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bc0 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 36.#defi
50bd0 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b ne OP_VerifyCook
50be0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ie
50bf0 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 37.#de
50c00 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 fine OP_OpenRead
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 38.#
50c30 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 define OP_OpenWr
50c40 69 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ite
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 39
50c60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e .#define OP_Open
50c70 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 Ephemeral
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c90 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 40.#define OP_Op
50ca0 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 enPseudo
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cc0 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41.#define OP_
50cd0 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 Close
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cf0 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 42.#define O
50d00 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20 20 P_SeekLt
50d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 43.#define
50d30 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 OP_SeekLe
50d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d50 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 44.#defi
50d60 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20 20 ne OP_SeekGe
50d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d80 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 45.#de
50d90 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 fine OP_SeekGt
50da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50db0 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 46.#
50dc0 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20 define OP_Seek
50dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 47
50df0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 .#define OP_NotF
50e00 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 ound
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 48.#define OP_Fo
50e30 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 und
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e50 20 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49.#define OP_
50e60 49 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 IsUnique
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e80 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50.#define O
50e90 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20 P_NotExists
50ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50eb0 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 51.#define
50ec0 20 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 OP_Sequence
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ee0 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 52.#defi
50ef0 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 ne OP_NewRowid
50f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f10 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 53.#de
50f20 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 fine OP_Insert
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f40 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 54.#
50f50 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 define OP_Insert
50f60 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Int
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 55
50f80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65 .#define OP_Dele
50f90 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fb0 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 56.#define OP_Re
50fc0 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20 20 setCount
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fe0 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 57.#define OP_
50ff0 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 RowKey
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51010 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 58.#define O
51020 50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 P_RowData
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 59.#define
51050 20 4f 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20 OP_Rowid
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51070 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 60.#defi
51080 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 ne OP_NullRow
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510a0 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 61.#de
510b0 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 fine OP_Last
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510d0 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 62.#
510e0 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 define OP_Sort
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 63
51110 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 .#define OP_Rewi
51120 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nd
51130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51140 36 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 64.#define OP_Pr
51150 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ev
51160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51170 20 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 65.#define OP_
51180 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 Next
51190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511a0 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 4f 66.#define O
511b0 50 5f 49 64 78 49 6e 73 65 72 74 20 20 20 20 20 P_IdxInsert
511c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511d0 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 67.#define
511e0 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 OP_IdxDelete
511f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51200 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 70.#defi
51210 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 ne OP_IdxRowid
51220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51230 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 71.#de
51240 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20 fine OP_IdxLT
51250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51260 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 72.#
51270 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20 define OP_IdxGE
51280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 81
512a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74 .#define OP_Dest
512b0 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 roy
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512d0 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 92.#define OP_Cl
512e0 65 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ear
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51300 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 95.#define OP_
51310 43 72 65 61 74 65 49 6e 64 65 78 20 20 20 20 20 CreateIndex
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51330 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 96.#define O
51340 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 P_CreateTable
51350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51360 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
51370 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 OP_ParseSchema
51380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51390 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 98.#defi
513a0 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 ne OP_LoadAnalys
513b0 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 is
513c0 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 99.#de
513d0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c fine OP_DropTabl
513e0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
513f0 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 100.#
51400 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e define OP_DropIn
51410 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 dex
51420 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31 101
51430 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 .#define OP_Drop
51440 54 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20 Trigger
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51460 30 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 02.#define OP_In
51470 74 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 tegrityCk
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51490 20 31 30 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 103.#define OP_
514a0 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20 RowSetAdd
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514c0 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 4f 104.#define O
514d0 50 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 P_RowSetRead
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514f0 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 105.#define
51500 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 OP_RowSetTest
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51520 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 106.#defi
51530 6e 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 ne OP_Program
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51550 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 107.#de
51560 66 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20 fine OP_Param
51570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51580 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 108.#
51590 64 65 66 69 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e define OP_FkCoun
515a0 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ter
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 39 109
515c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 .#define OP_FkIf
515d0 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 Zero
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
515f0 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 10.#define OP_Me
51600 6d 4d 61 78 20 20 20 20 20 20 20 20 20 20 20 20 mMax
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51620 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 111.#define OP_
51630 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 IfPos
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51650 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 112.#define O
51660 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 20 P_IfNeg
51670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51680 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 113.#define
51690 20 4f 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 OP_IfZero
516a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516b0 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 114.#defi
516c0 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 ne OP_AggStep
516d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516e0 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 115.#de
516f0 66 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c fine OP_AggFinal
51700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51710 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 116.#
51720 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d define OP_Vacuum
51730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37 117
51750 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 .#define OP_Incr
51760 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 Vacuum
51770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51780 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 18.#define OP_Ex
51790 70 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20 pire
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517b0 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 119.#define OP_
517c0 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 TableLock
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517e0 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 120.#define O
517f0 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20 P_VBegin
51800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51810 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 121.#define
51820 20 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 OP_VCreate
51830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51840 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 122.#defi
51850 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 ne OP_VDestroy
51860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51870 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 123.#de
51880 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 fine OP_VOpen
51890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518a0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
518b0 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 define OP_VFilte
518c0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
518d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 125
518e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c .#define OP_VCol
518f0 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 umn
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51910 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e 26.#define OP_VN
51920 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ext
51930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51940 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 127.#define OP_
51950 56 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 VRename
51960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51970 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 128.#define O
51980 50 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20 P_VUpdate
51990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
519a0 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 129.#define
519b0 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 OP_Pagecount
519c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
519d0 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 131.#defi
519e0 6e 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 ne OP_Trace
519f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a00 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 132.#de
51a10 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 fine OP_Noop
51a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a30 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 133.#
51a40 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 define OP_Explai
51a50 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
51a60 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
51a70 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ../* The followi
51a80 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 ng opcode values
51a90 20 61 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 are never used
51aa0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f */.#define OP_No
51ab0 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 tUsed_135
51ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51ad0 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 135.#define OP_
51ae0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 NotUsed_136
51af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b00 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 136.#define O
51b10 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 P_NotUsed_137
51b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b30 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 137.#define
51b40 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 OP_NotUsed_138
51b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b60 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 138.#defi
51b70 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
51b80 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 9
51b90 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 139.#de
51ba0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
51bb0 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20 140
51bc0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 140..
51bd0 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 ./* Properties s
51be0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 uch as "out2" or
51bf0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 "jump" that are
51c00 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a specified in.**
51c10 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 comments follow
51c20 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66 ing the "case" f
51c30 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 or each opcode i
51c40 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 n the vdbe.c.**
51c50 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f are encoded into
51c60 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 bitvectors as f
51c70 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 ollows:.*/.#defi
51c80 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 ne OPFLG_JUMP
51c90 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 0x0001
51ca0 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f /* jump: P2 ho
51cb0 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a lds jmp target *
51cc0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
51cd0 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 OUT2_PRERELEASE
51ce0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 0x0002 /* out2-
51cf0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 prerelease: */.#
51d00 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 define OPFLG_IN1
51d10 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
51d20 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 004 /* in1: P
51d30 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 1 is an input */
51d40 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
51d50 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N2 0
51d60 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 x0008 /* in2:
51d70 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P2 is an input
51d80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
51d90 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 _IN3
51da0 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 0x0010 /* in3:
51db0 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 P3 is an inpu
51dc0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 t */.#define OPF
51dd0 4c 47 5f 4f 55 54 32 20 20 20 20 20 20 20 20 20 LG_OUT2
51de0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 0x0020 /* ou
51df0 74 32 3a 20 20 50 32 20 69 73 20 61 6e 20 6f 75 t2: P2 is an ou
51e00 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tput */.#define
51e10 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 OPFLG_OUT3
51e20 20 20 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a 0x0040 /*
51e30 20 6f 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e out3: P3 is an
51e40 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 output */.#defi
51e50 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c ne OPFLG_INITIAL
51e60 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a IZER {\./* 0 *
51e70 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 / 0x00, 0x01, 0x
51e80 30 35 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 05, 0x04, 0x04,
51e90 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 32 0x10, 0x00, 0x02
51ea0 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 ,\./* 8 */ 0x0
51eb0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
51ec0 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x02, 0x00, 0x00,
51ed0 20 30 78 32 34 2c 20 30 78 32 34 2c 5c 0a 2f 2a 0x24, 0x24,\./*
51ee0 20 20 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 16 */ 0x00, 0x
51ef0 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 20 00, 0x00, 0x24,
51f00 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 34 0x04, 0x05, 0x04
51f10 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 , 0x00,\./* 24
51f20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 */ 0x00, 0x01, 0
51f30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 2c x05, 0x05, 0x00,
51f40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51f50 32 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 2,\./* 32 */ 0x
51f60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
51f70 30 78 30 32 2c 20 30 78 31 30 2c 20 30 78 30 30 0x02, 0x10, 0x00
51f80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f , 0x00, 0x00,\./
51f90 2a 20 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 * 40 */ 0x00, 0
51fa0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 31 2c x00, 0x00, 0x11,
51fb0 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 0x11, 0x11, 0x1
51fc0 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 20 34 38 1, 0x08,\./* 48
51fd0 20 2a 2f 20 30 78 31 31 2c 20 30 78 31 31 2c 20 */ 0x11, 0x11,
51fe0 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 32 0x11, 0x11, 0x02
51ff0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
52000 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 00,\./* 56 */ 0
52010 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
52020 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 0x00, 0x02, 0x0
52030 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a 0, 0x01, 0x01,\.
52040 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 31 2c 20 /* 64 */ 0x01,
52050 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 38 0x01, 0x01, 0x08
52060 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 , 0x4c, 0x4c, 0x
52070 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 37 00, 0x02,\./* 7
52080 32 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 35 2c 2 */ 0x01, 0x05,
52090 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 0x05, 0x15, 0x1
520a0 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 5, 0x15, 0x15, 0
520b0 78 31 35 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 x15,\./* 80 */
520c0 30 78 31 35 2c 20 30 78 30 31 2c 20 30 78 34 63 0x15, 0x01, 0x4c
520d0 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 , 0x4c, 0x4c, 0x
520e0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 5c 4c, 0x4c, 0x4c,\
520f0 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 34 63 2c ./* 88 */ 0x4c,
52100 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 0x4c, 0x4c, 0x4
52110 63 2c 20 30 78 30 32 2c 20 30 78 32 34 2c 20 30 c, 0x02, 0x24, 0
52120 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 x02, 0x00,\./*
52130 39 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32 96 */ 0x02, 0x02
52140 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
52150 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
52160 30 78 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 0x00,\./* 104 */
52170 20 30 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31 0x0c, 0x45, 0x1
52180 35 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 5, 0x01, 0x02, 0
52190 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c x00, 0x01, 0x08,
521a0 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 35 \./* 112 */ 0x05
521b0 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 , 0x05, 0x05, 0x
521c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
521d0 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 0x01, 0x00,\./*
521e0 31 32 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 120 */ 0x00, 0x0
521f0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
52200 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
52210 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 32 38 20 2a 0x01,\./* 128 *
52220 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 / 0x00, 0x00, 0x
52230 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 02, 0x02, 0x00,
52240 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
52250 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 ,\./* 136 */ 0x0
52260 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
52270 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c x00, 0x00, 0x04,
52280 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 0x04, 0x04,\./*
52290 20 31 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 144 */ 0x04, 0x
522a0 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 04,}../*********
522b0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 ***** End of opc
522c0 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.h *********
522d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
522e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
522f0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
52300 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
52310 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
52320 66 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a ff in vdbe.h ***
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52340 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f ****/../*.** Pro
52350 74 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 totypes for the
52360 56 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 VDBE interface.
52370 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e See comments on
52380 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
52390 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 ion.** for a des
523a0 63 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 cription of what
523b0 20 65 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 each of these r
523c0 6f 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f outines does..*/
523d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
523e0 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 Vdbe *sqlite3Vdb
523f0 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a eCreate(sqlite3*
52400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52420 65 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e eAddOp0(Vdbe*,in
52430 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52450 62 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 beAddOp1(Vdbe*,i
52460 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
52470 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
52480 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 te3VdbeAddOp2(Vd
52490 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 be*,int,int,int)
524a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
524b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
524c0 41 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 AddOp3(Vdbe*,int
524d0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
524e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
524f0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
52500 4f 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op4(Vdbe*,int,in
52510 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 t,int,int,const
52520 63 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a char *zP4,int);.
52530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
52540 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
52550 64 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69 6e dOp4Int(Vdbe*,in
52560 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e t,int,int,int,in
52570 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52580 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52590 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 beAddOpList(Vdbe
525a0 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 *, int nOp, Vdbe
525b0 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f OpList const *aO
525c0 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 p);.SQLITE_PRIVA
525d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
525e0 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 dbeChangeP1(Vdbe
525f0 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 *, int addr, int
52600 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 P1);.SQLITE_PRI
52610 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
52620 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64 3VdbeChangeP2(Vd
52630 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 be*, int addr, i
52640 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50 nt P2);.SQLITE_P
52650 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
52660 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 te3VdbeChangeP3(
52670 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c Vdbe*, int addr,
52680 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45 int P3);.SQLITE
52690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
526a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
526b0 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b 5(Vdbe*, u8 P5);
526c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
526d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
526e0 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20 JumpHere(Vdbe*,
526f0 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54 int addr);.SQLIT
52700 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52710 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
52720 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e ToNoop(Vdbe*, in
52730 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a t addr, int N);.
52740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52750 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
52760 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69 hangeP4(Vdbe*, i
52770 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 nt addr, const c
52780 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 har *zP4, int N)
52790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
527a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
527b0 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a eUsesBtree(Vdbe*
527c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
527d0 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 RIVATE VdbeOp *s
527e0 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 qlite3VdbeGetOp(
527f0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Vdbe*, int);.SQL
52800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52810 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
52820 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c abel(Vdbe*);.SQL
52830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
52840 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
52850 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 te(Vdbe*);.SQLIT
52860 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52870 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
52880 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e ady(Vdbe*,int,in
52890 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e t,int,int,int,in
528a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
528b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
528c0 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a beFinalize(Vdbe*
528d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
528e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
528f0 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 beResolveLabel(V
52900 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 dbe*, int);.SQLI
52910 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52920 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
52930 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 tAddr(Vdbe*);.#i
52940 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
52950 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
52960 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 int sqlite3Vd
52970 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 beAssertMayAbort
52980 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 (Vdbe *, int);.S
52990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
529a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
529b0 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 Trace(Vdbe*,FILE
529c0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
529d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
529e0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 qlite3VdbeResetS
529f0 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 tepResult(Vdbe*)
52a00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52a10 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
52a20 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 Reset(Vdbe*);.SQ
52a30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52a40 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
52a50 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e NumCols(Vdbe*,in
52a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52a80 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 beSetColName(Vdb
52a90 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f e*, int, int, co
52aa0 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 nst char *, void
52ab0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
52ac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
52ad0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e sqlite3VdbeCoun
52ae0 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b tChanges(Vdbe*);
52af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52b00 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 sqlite3 *sqlite3
52b10 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 VdbeDb(Vdbe*);.S
52b20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52b30 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
52b40 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 tSql(Vdbe*, cons
52b50 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e t char *z, int n
52b60 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
52b70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
52b80 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 te3VdbeSwap(Vdbe
52b90 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 *,Vdbe*);.SQLITE
52ba0 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 _PRIVATE VdbeOp
52bb0 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 *sqlite3VdbeTake
52bc0 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69 OpArray(Vdbe*, i
52bd0 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 nt*, int*);.SQLI
52be0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52bf0 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72 sqlite3VdbeProgr
52c00 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 amDelete(sqlite3
52c10 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a *, SubProgram *
52c20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
52c30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 RIVATE sqlite3_v
52c40 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62 alue *sqlite3Vdb
52c50 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c eGetValue(Vdbe*,
52c60 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 int, u8);.SQLIT
52c70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52c80 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72 qlite3VdbeSetVar
52c90 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29 mask(Vdbe*, int)
52ca0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
52cb0 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 53 51 4c 49 _OMIT_TRACE.SQLI
52cc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 63 68 61 TE_PRIVATE cha
52cd0 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65 45 78 r *sqlite3VdbeEx
52ce0 70 61 6e 64 53 71 6c 28 56 64 62 65 2a 2c 20 63 pandSql(Vdbe*, c
52cf0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e onst char*);.#en
52d00 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
52d10 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f ATE UnpackedReco
52d20 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 rd *sqlite3VdbeR
52d30 65 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 ecordUnpack(KeyI
52d40 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 nfo*,int,const v
52d50 6f 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b oid*,char*,int);
52d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52d70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
52d80 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 DeleteUnpackedRe
52d90 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 cord(UnpackedRec
52da0 6f 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ord*);.SQLITE_PR
52db0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
52dc0 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
52dd0 72 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 re(int,const voi
52de0 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 d*,UnpackedRecor
52df0 64 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e d*);...#ifndef N
52e00 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
52e10 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
52e20 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 te3VdbeComment(V
52e30 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 dbe*, const char
52e40 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e *, ...);.# defin
52e50 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 e VdbeComment(X)
52e60 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d sqlite3VdbeCom
52e70 6d 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 ment X.SQLITE_PR
52e80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
52e90 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ite3VdbeNoopComm
52ea0 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 ent(Vdbe*, const
52eb0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 char*, ...);.#
52ec0 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 define VdbeNoopC
52ed0 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 omment(X) sqlit
52ee0 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e e3VdbeNoopCommen
52ef0 74 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 t X.#else.# defi
52f00 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 ne VdbeComment(X
52f10 29 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e ).# define VdbeN
52f20 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 oopComment(X).#e
52f30 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a ndif..#endif../*
52f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
52f50 64 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a d of vdbe.h ****
52f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
52f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
52fa0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
52fb0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
52fc0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
52fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
52fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
52ff0 63 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e clude pager.h in
53000 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
53010 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
53020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
53030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
53040 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 gin file pager.h
53050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
53060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
53080 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
53090 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
530a0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
530b0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
530c0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
530d0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
530e0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
530f0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
53100 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
53110 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
53120 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
53130 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
53140 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
53150 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
53160 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
53170 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
53180 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
53190 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
531a0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
531b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
531c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
531d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
531e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
531f0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 ****.** This hea
53200 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 der file defines
53210 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
53220 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 hat the sqlite p
53230 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 age cache.** sub
53240 73 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 system. The pag
53250 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
53260 6d 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 m reads and writ
53270 65 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 es a file a page
53280 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e .** at a time an
53290 64 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 d provides a jou
532a0 72 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 rnal for rollbac
532b0 6b 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f k..*/..#ifndef _
532c0 50 41 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 PAGER_H_.#define
532d0 20 5f 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a _PAGER_H_../*.*
532e0 2a 20 44 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 * Default maximu
532f0 6d 20 73 69 7a 65 20 66 6f 72 20 70 65 72 73 69 m size for persi
53300 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
53310 6c 65 73 2e 20 41 20 6e 65 67 61 74 69 76 65 20 les. A negative
53320 0a 2a 2a 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 .** value means
53330 6e 6f 20 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 no limit. This v
53340 61 6c 75 65 20 6d 61 79 20 62 65 20 6f 76 65 72 alue may be over
53350 72 69 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65 ridden using the
53360 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 .** sqlite3Page
53370 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 rJournalSizeLimi
53380 74 28 29 20 41 50 49 2e 20 53 65 65 20 61 6c 73 t() API. See als
53390 6f 20 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 o "PRAGMA journa
533a0 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a l_size_limit"..*
533b0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
533c0 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c _DEFAULT_JOURNAL
533d0 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 _SIZE_LIMIT. #d
533e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
533f0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a AULT_JOURNAL_SIZ
53400 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 E_LIMIT -1.#endi
53410 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 f../*.** The typ
53420 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 e used to repres
53430 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 ent a page numbe
53440 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 r. The first pa
53450 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 ge in a file.**
53460 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 is called page 1
53470 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 . 0 is used to
53480 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 represent "not a
53490 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 page"..*/.typed
534a0 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a ef u32 Pgno;../*
534b0 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 .** Each open fi
534c0 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 le is managed by
534d0 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 a separate inst
534e0 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 ance of the "Pag
534f0 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a er" structure..*
53500 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
53510 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f Pager Pager;../
53520 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 *.** Handle type
53530 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 for pages..*/.t
53540 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 ypedef struct Pg
53550 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a Hdr DbPage;../*.
53560 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50 ** Page number P
53570 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 AGER_MJ_PGNO is
53580 6e 65 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e never used in an
53590 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
535a0 20 28 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 (it is.** reser
535b0 76 65 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 ved for working
535c0 61 72 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 around a windows
535d0 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 /posix incompati
535e0 62 69 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a bility). It is.*
535f0 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f * used in the jo
53600 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 urnal to signify
53610 20 74 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e that the remain
53620 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e der of the journ
53630 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 al file .** is d
53640 65 76 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e evoted to storin
53650 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e g a master journ
53660 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 al name - there
53670 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 are no more page
53680 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 s to.** roll bac
53690 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 k. See comments
536a0 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 for function wri
536b0 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
536c0 29 20 69 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a ) in pager.c .**
536d0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f for details..*/
536e0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d .#define PAGER_M
536f0 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f J_PGNO(x) ((Pgno
53700 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f )((PENDING_BYTE/
53710 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 ((x)->pageSize))
53720 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f +1))../*.** Allo
53730 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 wed values for t
53740 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
53750 65 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 er to sqlite3Pag
53760 65 72 4f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 erOpen()..**.**
53770 4e 4f 54 45 3a 20 54 68 65 73 65 20 76 61 6c 75 NOTE: These valu
53780 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 es must match th
53790 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
537a0 42 54 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e BTREE_ values in
537b0 20 62 74 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 btree.h..*/.#de
537c0 66 69 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f fine PAGER_OMIT_
537d0 4a 4f 55 52 4e 41 4c 20 20 30 78 30 30 30 31 20 JOURNAL 0x0001
537e0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 /* Do not use
537f0 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
53800 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 nal */.#define P
53810 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b AGER_NO_READLOCK
53820 20 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 0x0002 /*
53830 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f Omit readlocks o
53840 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 n readonly files
53850 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 */../*.** Valid
53860 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
53870 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
53880 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c to sqlite3PagerL
53890 6f 63 6b 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f ockingMode()..*/
538a0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c .#define PAGER_L
538b0 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 OCKINGMODE_QUERY
538c0 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 -1.#define
538d0 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f PAGER_LOCKINGMO
538e0 44 45 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 DE_NORMAL 0
538f0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c .#define PAGER_L
53900 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
53910 53 49 56 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 SIVE 1../*.**
53920 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 Valid values for
53930 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
53940 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 ment to sqlite3P
53950 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 agerJournalMode(
53960 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 )..*/.#define PA
53970 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
53980 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 QUERY -1.#d
53990 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
539a0 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 NALMODE_DELETE
539b0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 0 /* Commi
539c0 74 20 62 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f t by deleting jo
539d0 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 urnal file */.#d
539e0 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
539f0 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 NALMODE_PERSIST
53a00 20 20 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 1 /* Commi
53a10 74 20 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 t by zeroing jou
53a20 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 rnal header */.#
53a30 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
53a40 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 RNALMODE_OFF
53a50 20 20 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 2 /* Jour
53a60 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f nal omitted. */
53a70 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a .#define PAGER_J
53a80 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
53a90 41 54 45 20 20 20 20 33 20 20 20 2f 2a 20 43 6f ATE 3 /* Co
53aa0 6d 6d 69 74 20 62 79 20 74 72 75 6e 63 61 74 69 mmit by truncati
53ab0 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 ng journal */.#d
53ac0 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
53ad0 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 NALMODE_MEMORY
53ae0 20 20 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 4 /* In-me
53af0 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
53b00 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 e */../*.** The
53b10 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 remainder of thi
53b20 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
53b30 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 the declarations
53b40 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
53b50 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 s.** that make u
53b60 70 20 74 68 65 20 50 61 67 65 72 20 73 75 62 2d p the Pager sub-
53b70 73 79 73 74 65 6d 20 41 50 49 2e 20 53 65 65 20 system API. See
53b80 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d source code comm
53b90 65 6e 74 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 ents for .** a d
53ba0 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 etailed descript
53bb0 69 6f 6e 20 6f 66 20 65 61 63 68 20 72 6f 75 74 ion of each rout
53bc0 69 6e 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e ine..*/../* Open
53bd0 20 61 6e 64 20 63 6c 6f 73 65 20 61 20 50 61 67 and close a Pag
53be0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a er connection. *
53bf0 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 / .SQLITE_PRIVAT
53c00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53c10 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 erOpen(. sqlite
53c20 33 5f 76 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 3_vfs*,. Pager
53c30 2a 2a 70 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e **ppPager,. con
53c40 73 74 20 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c st char*,. int,
53c50 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 . int,. int,.
53c60 20 76 6f 69 64 28 2a 29 28 44 62 50 61 67 65 2a void(*)(DbPage*
53c70 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ).);.SQLITE_PRIV
53c80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
53c90 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 agerClose(Pager
53ca0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 *pPager);.SQLITE
53cb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53cc0 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c ite3PagerReadFil
53cd0 65 68 65 61 64 65 72 28 50 61 67 65 72 2a 2c 20 eheader(Pager*,
53ce0 69 6e 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 int, unsigned ch
53cf0 61 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 ar*);../* Functi
53d00 6f 6e 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 ons used to conf
53d10 69 67 75 72 65 20 61 20 50 61 67 65 72 20 6f 62 igure a Pager ob
53d20 6a 65 63 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f ject. */.SQLITE_
53d30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
53d40 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 ite3PagerSetBusy
53d50 68 61 6e 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 handler(Pager*,
53d60 69 6e 74 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 int(*)(void *),
53d70 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f void *);.SQLITE_
53d80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
53d90 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
53da0 69 7a 65 28 50 61 67 65 72 2a 2c 20 75 31 36 2a ize(Pager*, u16*
53db0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
53dc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53dd0 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f e3PagerMaxPageCo
53de0 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 unt(Pager*, int)
53df0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53e00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
53e10 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 erSetCachesize(P
53e20 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ager*, int);.SQL
53e30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
53e40 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
53e50 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 SafetyLevel(Page
53e60 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c r*,int,int);.SQL
53e70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53e80 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b sqlite3PagerLock
53e90 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c ingMode(Pager *,
53ea0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
53eb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53ec0 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 3PagerJournalMod
53ed0 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b e(Pager *, int);
53ee0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53ef0 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 i64 sqlite3Pager
53f00 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 JournalSizeLimit
53f10 28 50 61 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a (Pager *, i64);.
53f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
53f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a qlite3_backup **
53f40 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b sqlite3PagerBack
53f50 75 70 50 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a upPtr(Pager*);..
53f60 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 /* Functions use
53f70 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 d to obtain and
53f80 72 65 6c 65 61 73 65 20 70 61 67 65 20 72 65 66 release page ref
53f90 65 72 65 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c erences. */ .SQL
53fa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53fb0 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
53fc0 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ire(Pager *pPage
53fd0 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 r, Pgno pgno, Db
53fe0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 Page **ppPage, i
53ff0 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 nt clrFlag);.#de
54000 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65 fine sqlite3Page
54010 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 rGet(A,B,C) sqli
54020 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 te3PagerAcquire(
54030 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f A,B,C,0).SQLITE_
54040 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a PRIVATE DbPage *
54050 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
54060 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 up(Pager *pPager
54070 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 , Pgno pgno);.SQ
54080 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
54090 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 d sqlite3PagerRe
540a0 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 f(DbPage*);.SQLI
540b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
540c0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
540d0 66 28 44 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 f(DbPage*);../*
540e0 4f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 Operations on pa
540f0 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a ge references. *
54100 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
54110 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54120 72 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b rWrite(DbPage*);
54130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54140 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
54150 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61 67 rDontWrite(DbPag
54160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
54170 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
54180 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 agerMovepage(Pag
54190 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f er*,DbPage*,Pgno
541a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
541b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
541c0 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
541d0 6e 74 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c nt(DbPage*);.SQL
541e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
541f0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
54200 74 44 61 74 61 28 44 62 50 61 67 65 20 2a 29 3b tData(DbPage *);
54210 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
54220 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
54230 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61 gerGetExtra(DbPa
54240 67 65 20 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 ge *); ../* Func
54250 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 tions used to ma
54260 6e 61 67 65 20 70 61 67 65 72 20 74 72 61 6e 73 nage pager trans
54270 61 63 74 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 actions and save
54280 70 6f 69 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 points. */.SQLIT
54290 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
542a0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
542b0 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a unt(Pager*, int*
542c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
542d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
542e0 65 72 42 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 erBegin(Pager*,
542f0 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 int exFlag, int)
54300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54310 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54320 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
54330 50 61 67 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 Pager*,const cha
54340 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 r *zMaster, int)
54350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54360 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54370 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 rSync(Pager *pPa
54380 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ger);.SQLITE_PRI
54390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
543a0 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
543b0 54 77 6f 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c Two(Pager*);.SQL
543c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
543d0 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
543e0 62 61 63 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 back(Pager*);.SQ
543f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
54400 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
54410 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 nSavepoint(Pager
54420 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 *pPager, int n)
54430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54440 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54450 72 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 rSavepoint(Pager
54460 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 *pPager, int op
54470 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 , int iSavepoint
54480 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
54490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
544a0 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 erSharedLock(Pag
544b0 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a er *pPager);../*
544c0 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 Functions used
544d0 74 6f 20 71 75 65 72 79 20 70 61 67 65 72 20 73 to query pager s
544e0 74 61 74 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 tate and configu
544f0 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 ration. */.SQLIT
54500 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c E_PRIVATE u8 sql
54510 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f ite3PagerIsreado
54520 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c nly(Pager*);.SQL
54530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54540 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
54550 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 ount(Pager*);.SQ
54560 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
54570 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
54580 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 PagerFilename(Pa
54590 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
545a0 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 IVATE const sqli
545b0 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 te3_vfs *sqlite3
545c0 50 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29 PagerVfs(Pager*)
545d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
545e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 sqlite3_file *s
545f0 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 qlite3PagerFile(
54600 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
54610 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
54620 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 ar *sqlite3Pager
54630 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 Journalname(Page
54640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
54650 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
54660 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 agerNosync(Pager
54670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54680 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
54690 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 PagerTempSpace(P
546a0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
546b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
546c0 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 e3PagerIsMemdb(P
546d0 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 ager*);../* Func
546e0 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 74 72 tions used to tr
546f0 75 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62 uncate the datab
54700 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c ase file. */.SQL
54710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
54720 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 sqlite3PagerTru
54730 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72 ncateImage(Pager
54740 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e *,Pgno);../* Fun
54750 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 ctions to suppor
54760 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 t testing and de
54770 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 bugging. */.#if
54780 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
54790 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
547a0 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 5f TE_TEST).SQLITE_
547b0 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 73 PRIVATE Pgno s
547c0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
547d0 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b 0a umber(DbPage*);.
547e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
547f0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54800 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 rIswriteable(DbP
54810 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 age*);.#endif.#i
54820 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
54830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54840 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 int *sqlite3Pa
54850 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a 29 gerStats(Pager*)
54860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54870 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 void sqlite3P
54880 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 65 agerRefdump(Page
54890 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 r*);. void disa
548a0 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
548b0 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 _errors(void);.
548c0 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d void enable_sim
548d0 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
548e0 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 (void);.#else.#
548f0 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 define disable_s
54900 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
54910 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e rs().# define en
54920 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
54930 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 o_errors().#endi
54940 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 f..#endif /* _PA
54950 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a GER_H_ */../****
54960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
54970 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a f pager.h ******
54980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
549a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
549b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
549c0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
549d0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
549e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
549f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
54a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
54a10 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 74 de pcache.h in t
54a20 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
54a30 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
54a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
54a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
54a60 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 2a file pcache.h *
54a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
54aa0 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2008 August 05.
54ab0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
54ac0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
54ad0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
54ae0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
54af0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
54b00 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
54b10 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
54b20 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
54b30 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
54b40 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
54b50 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
54b60 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
54b70 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
54b80 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
54b90 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
54ba0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
54bb0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
54bc0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
54bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
54c10 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
54c20 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 le defines the i
54c30 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 nterface that th
54c40 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 e sqlite page ca
54c50 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d che.** subsystem
54c60 2e 20 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f . .*/..#ifndef _
54c70 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64 PCACHE_H_..typed
54c80 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 ef struct PgHdr
54c90 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 73 PgHdr;.typedef s
54ca0 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43 61 truct PCache PCa
54cb0 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 che;../*.** Ever
54cc0 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 y page in the ca
54cd0 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 che is controlle
54ce0 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 d by an instance
54cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
54d00 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a g.** structure..
54d10 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 20 */.struct PgHdr
54d20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b {. void *pData;
54d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d40 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 /* Content of
54d50 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 this page */.
54d60 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 20 void *pExtra;
54d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
54d80 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 20 * Extra content
54d90 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 */. PgHdr *pDir
54da0 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ty;
54db0 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74 /* Transient
54dc0 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
54dd0 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 ages */. Pgno p
54de0 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 gno;
54df0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
54e00 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 number for this
54e10 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 72 page */. Pager
54e20 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 *pPager;
54e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
54e40 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 65 pager this page
54e50 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23 is part of */.#
54e60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 ifdef SQLITE_CHE
54e70 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 70 CK_PAGES. u32 p
54e80 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 20 ageHash;
54e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
54ea0 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 6e h of page conten
54eb0 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 t */.#endif. u1
54ec0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
54ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54ee0 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 69 PGHDR flags defi
54ef0 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 ned below */..
54f00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
54f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54f40 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 *******. ** Ele
54f50 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 20 ments above are
54f60 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61 public. All tha
54f70 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69 t follows is pri
54f80 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e 63 vate to pcache.c
54f90 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 . ** and should
54fa0 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 64 not be accessed
54fb0 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 by other module
54fc0 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52 s.. */. i16 nR
54fd0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
54fe0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
54ff0 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74 er of users of t
55000 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 43 his page */. PC
55010 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20 ache *pCache;
55020 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
55030 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 20 Cache that owns
55040 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20 this page */..
55050 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 78 PgHdr *pDirtyNex
55060 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
55070 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 * Next element i
55080 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 n list of dirty
55090 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 pages */. PgHdr
550a0 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 20 *pDirtyPrev;
550b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
550c0 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e vious element in
550d0 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
550e0 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 ages */.};../* B
550f0 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 67 it values for Pg
55100 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65 Hdr.flags */.#de
55110 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 59 fine PGHDR_DIRTY
55120 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
55130 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 20 02 /* Page has
55140 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 changed */.#defi
55150 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 ne PGHDR_NEED_SY
55160 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30 34 NC 0x004
55170 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 72 /* Fsync the r
55180 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
55190 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 20 before.
551a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
551b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
551c0 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 61 writing this pa
551d0 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ge to the databa
551e0 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 se */.#define PG
551f0 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 20 HDR_NEED_READ
55200 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 20 0x008 /*
55210 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 61 Content is unrea
55220 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 d */.#define PGH
55230 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c DR_REUSE_UNLIKEL
55240 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 41 Y 0x010 /* A
55250 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 65 hint that reuse
55260 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a is unlikely */.
55270 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 4f #define PGHDR_DO
55280 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 NT_WRITE
55290 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 0x020 /* Do not
552a0 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 74 write content t
552b0 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e o disk */../* In
552c0 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 itialize and shu
552d0 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 tdown the page c
552e0 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 2a ache subsystem *
552f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55300 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
55310 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 heInitialize(voi
55320 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
55330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55340 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f cacheShutdown(vo
55350 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61 id);../* Page ca
55360 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 67 che buffer manag
55370 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20 ement:.** These
55380 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 routines impleme
55390 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 nt SQLITE_CONFIG
553a0 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 _PAGECACHE..*/.S
553b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
553c0 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 id sqlite3PCache
553d0 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 BufferSetup(void
553e0 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 *, int sz, int
553f0 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61 n);../* Create a
55400 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 65 new pager cache
55410 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72 ..** Under memor
55420 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65 y stress, invoke
55430 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 20 xStress to try
55440 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c to make pages cl
55450 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 ean..** Only cle
55460 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20 an and unpinned
55470 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 63 pages can be rec
55480 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 laimed..*/.SQLIT
55490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
554a0 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e qlite3PcacheOpen
554b0 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 (. int szPage,
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
554d0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 /* Size of ev
554e0 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ery page */. in
554f0 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20 t szExtra,
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
55510 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f Extra space asso
55520 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 ciated with each
55530 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 page */. int b
55540 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 Purgeable,
55550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
55560 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f e if pages are o
55570 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 n backing store
55580 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 */. int (*xStre
55590 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 ss)(void*, PgHdr
555a0 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 *), /* Call to t
555b0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 ry to make pages
555c0 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 clean */. void
555d0 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20 *pStress,
555e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
555f0 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
55600 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
55610 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 20 ToInit
55620 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f /* Preallo
55630 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 cated space for
55640 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b the PCache */.);
55650 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 ../* Modify the
55660 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 20 page-size after
55670 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62 65 the cache has be
55680 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53 en created. */.S
55690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
556a0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
556b0 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 SetPageSize(PCac
556c0 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 he *, int);../*
556d0 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
556e0 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 in bytes of a PC
556f0 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 73 ache object. Us
55700 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 ed to preallocat
55710 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 61 e.** storage spa
55720 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ce..*/.SQLITE_PR
55730 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
55740 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 3PcacheSize(void
55750 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 );../* One relea
55760 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66 75 se per successfu
55770 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 69 l fetch. Page i
55780 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72 s pinned until r
55790 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65 eleased..** Refe
557a0 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a rence counted. .
557b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
557c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
557d0 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65 2a cheFetch(PCache*
557e0 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 , Pgno, int crea
557f0 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 teFlag, PgHdr**)
55800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
55810 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55820 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72 cheRelease(PgHdr
55830 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);..SQLITE_PRIV
55840 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55850 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 PcacheDrop(PgHdr
55860 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 *); /* R
55870 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d 20 emove page from
55880 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f cache */.SQLITE_
55890 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
558a0 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 ite3PcacheMakeDi
558b0 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 20 rty(PgHdr*);
558c0 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 67 /* Make sure pag
558d0 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 74 e is marked dirt
558e0 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 y */.SQLITE_PRIV
558f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55900 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 PcacheMakeClean(
55910 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d PgHdr*); /* M
55920 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 67 ark a single pag
55930 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 e as clean */.SQ
55940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55950 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
55960 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29 leanAll(PCache*)
55970 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c ; /* Mark all
55980 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 65 dirty list page
55990 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f s as clean */../
559a0 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 20 * Change a page
559b0 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 79 number. Used by
559c0 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f incr-vacuum. */
559d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
559e0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
559f0 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 50 heMove(PgHdr*, P
55a00 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 gno);../* Remove
55a10 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 20 all pages with
55a20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 74 pgno>x. Reset t
55a30 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d 30 he cache if x==0
55a40 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
55a50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55a60 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 cacheTruncate(PC
55a70 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a ache*, Pgno x);.
55a80 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 6f ./* Get a list o
55a90 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 f all dirty page
55aa0 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 s in the cache,
55ab0 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e sorted by page n
55ac0 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f umber */.SQLITE_
55ad0 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 PRIVATE PgHdr *s
55ae0 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
55af0 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b 0a yList(PCache*);.
55b00 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 6c ./* Reset and cl
55b10 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f 62 ose the cache ob
55b20 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ject */.SQLITE_P
55b30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55b40 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 te3PcacheClose(P
55b50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 Cache*);../* Cle
55b60 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 61 ar flags from pa
55b70 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 ges of the page
55b80 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f cache */.SQLITE_
55b90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55ba0 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 ite3PcacheClearS
55bb0 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20 yncFlags(PCache
55bc0 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20 *);../* Discard
55bd0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
55be0 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c the cache */.SQL
55bf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
55c00 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
55c10 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f ear(PCache*);../
55c20 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 * Return the tot
55c30 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 al number of out
55c40 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 standing page re
55c50 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 ferences */.SQLI
55c60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
55c70 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
55c80 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a ount(PCache*);..
55c90 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 /* Increment the
55ca0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
55cb0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
55cc0 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 page */.SQLITE_P
55cd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55ce0 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 te3PcacheRef(PgH
55cf0 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 dr*);..SQLITE_PR
55d00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
55d10 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 3PcachePageRefco
55d20 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a unt(PgHdr*);../*
55d30 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
55d40 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
55d50 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
55d60 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f cache */.SQLITE_
55d70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
55d80 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
55d90 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 nt(PCache*);..#i
55da0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
55db0 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c _CHECK_PAGES) ||
55dc0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
55dd0 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61 74 DEBUG)./* Iterat
55de0 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 69 e through all di
55df0 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e rty pages curren
55e00 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 tly stored in th
55e10 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a e cache. This.**
55e20 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e interface is on
55e30 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 ly available if
55e40 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
55e50 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 68 ES is defined wh
55e60 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61 en the .** libra
55e70 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a ry is built..*/.
55e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55e90 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55ea0 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 eIterateDirty(PC
55eb0 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f ache *pCache, vo
55ec0 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 id (*xIter)(PgHd
55ed0 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f r *));.#endif../
55ee0 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 68 * Set and get th
55ef0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 e suggested cach
55f00 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 73 e-size for the s
55f10 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d 63 pecified pager-c
55f20 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ache..**.** If n
55f30 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d o global maximum
55f40 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 is configured,
55f50 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20 then the system
55f60 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 attempts to limi
55f70 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e t.** the total n
55f80 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 umber of pages c
55f90 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61 62 ached by purgeab
55fa0 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 20 le pager-caches
55fb0 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 to the sum.** of
55fc0 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
55fd0 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 ache-sizes..*/.S
55fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
55ff0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
56000 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 SetCachesize(PCa
56010 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 che *, int);.#if
56020 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
56030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
56040 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
56050 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 GetCachesize(PCa
56060 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a che *);.#endif..
56070 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
56080 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
56090 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f GEMENT./* Try to
560a0 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 return memory u
560b0 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 sed by the pcach
560c0 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 e module to the
560d0 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 main memory heap
560e0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
560f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
56100 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 acheReleaseMemor
56110 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a y(int);.#endif..
56120 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
56130 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
56140 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
56150 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 acheStats(int*,i
56160 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a nt*,int*,int*);.
56170 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
56180 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
56190 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 te3PCacheSetDefa
561a0 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 ult(void);..#end
561b0 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f if /* _PCACHE_H_
561c0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
561d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 **** End of pcac
561e0 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.h ***********
561f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56210 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
56220 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
56230 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
56240 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
56250 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
56260 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/../*********
56270 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 ***** Include os
56280 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
56290 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562b0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
562c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
562d0 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.h **********
562e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56300 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
56310 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a September 16.**
56320 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
56330 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
56340 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
56350 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
56360 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
56370 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
56380 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
56390 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
563a0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
563b0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
563c0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
563d0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
563e0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
563f0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
56400 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
56410 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
56420 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
56430 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
56440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56480 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 *.**.** This hea
56490 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 68 der file (togeth
564a0 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 61 er with is compa
564b0 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f nion C source-co
564c0 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 de file.** "os.c
564d0 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 62 ") attempt to ab
564e0 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 72 stract the under
564f0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
56500 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a system so that.*
56510 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 * the SQLite lib
56520 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f rary will work o
56530 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 n both POSIX and
56540 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 windows systems
56550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 ..**.** This hea
56560 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e 63 der file is #inc
56570 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 74 lude-ed by sqlit
56580 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 20 eInt.h and thus
56590 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67 ends up.** being
565a0 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 65 included by eve
565b0 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a ry source file..
565c0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
565d0 54 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 TE_OS_H_.#define
565e0 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a _SQLITE_OS_H_..
565f0 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 /*.** Figure out
56600 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 if we are deali
56610 6e 67 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 ng with Unix, Wi
56620 6e 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f ndows, or some o
56630 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e ther.** operatin
56640 67 20 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 g system. After
56650 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 the following b
56660 6c 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 lock of preproce
56670 73 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c ss macros,.** al
56680 6c 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 l of SQLITE_OS_U
56690 4e 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 NIX, SQLITE_OS_W
566a0 49 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 IN, SQLITE_OS_OS
566b0 32 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 2, and SQLITE_OS
566c0 5f 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 _OTHER .** will
566d0 64 65 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 defined to eithe
566e0 72 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f r 1 or 0. One o
566f0 66 20 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 f the four will
56700 62 65 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 be 1. The other
56710 20 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 .** three will
56720 62 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 be 0..*/.#if def
56730 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f ined(SQLITE_OS_O
56740 54 48 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 THER).# if SQLIT
56750 45 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 E_OS_OTHER==1.#
56760 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f undef SQLITE_O
56770 53 5f 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e S_UNIX.# defin
56780 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
56790 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 0.# undef SQL
567a0 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 ITE_OS_WIN.# d
567b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
567c0 57 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 WIN 0.# undef
567d0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 SQLITE_OS_OS2.#
567e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
567f0 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a OS_OS2 0.# else.
56800 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 # undef SQLITE
56810 5f 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 _OS_OTHER.# endi
56820 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 f.#endif.#if !de
56830 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f fined(SQLITE_OS_
56840 55 4e 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 UNIX) && !define
56850 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 d(SQLITE_OS_OTHE
56860 52 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 R).# define SQLI
56870 54 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 TE_OS_OTHER 0.#
56880 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 ifndef SQLITE_OS
56890 5f 57 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 _WIN.# if defi
568a0 6e 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 ned(_WIN32) || d
568b0 65 66 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c efined(WIN32) ||
568c0 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 defined(__CYGWI
568d0 4e 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 N__) || defined(
568e0 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 __MINGW32__) ||
568f0 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e defined(__BORLAN
56900 44 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 DC__).# defi
56910 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
56920 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 1.# define
56930 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
56940 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
56950 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 LITE_OS_OS2 0.#
56960 20 20 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f elif defined(_
56970 5f 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e _EMX__) || defin
56980 65 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 ed(_OS2) || defi
56990 6e 65 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 ned(OS2) || defi
569a0 6e 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 ned(_OS2_) || de
569b0 66 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 fined(__OS2__).#
569c0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
569d0 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 TE_OS_WIN 0.#
569e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
569f0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 OS_UNIX 0.#
56a00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
56a10 5f 4f 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a _OS2 1.# else.
56a20 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
56a30 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 ITE_OS_WIN 0.#
56a40 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
56a50 5f 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 _OS_UNIX 1.#
56a60 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
56a70 53 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 S_OS2 0.# endif
56a80 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
56a90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
56aa0 20 30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 0.# define SQL
56ab0 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 ITE_OS_OS2 0.# e
56ac0 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e ndif.#else.# ifn
56ad0 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 def SQLITE_OS_WI
56ae0 4e 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 N.# define SQLI
56af0 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e TE_OS_WIN 0.# en
56b00 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a dif.#endif../*.*
56b10 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 * Determine if w
56b20 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
56b30 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 th WindowsCE - w
56b40 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a hich has a much.
56b50 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a ** reduced API..
56b60 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f */.#if defined(_
56b70 57 49 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 WIN32_WCE).# def
56b80 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
56b90 4e 43 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 NCE 1.#else.# de
56ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56bb0 49 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a INCE 0.#endif...
56bc0 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 /*.** Define the
56bd0 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 maximum size of
56be0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
56bf0 65 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c ename.*/.#if SQL
56c00 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 ITE_OS_WIN.# inc
56c10 6c 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e lude <windows.h>
56c20 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
56c30 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 _TEMPNAME_SIZE (
56c40 4d 41 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c MAX_PATH+50).#el
56c50 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 if SQLITE_OS_OS2
56c60 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 .# if (__GNUC__
56c70 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 > 3 || __GNUC__
56c80 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d == 3 && __GNUC_M
56c90 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 INOR__ >= 3) &&
56ca0 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 defined(OS2_HIGH
56cb0 5f 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c _MEMORY).# incl
56cc0 75 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 ude <os2safe.h>
56cd0 2f 2a 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 /* has to be inc
56ce0 6c 75 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 luded before os2
56cf0 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 .h for linking t
56d00 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 o work */.# endi
56d10 66 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f f.# define INCL_
56d20 44 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 DOSDATETIME.# de
56d30 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c fine INCL_DOSFIL
56d40 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e EMGR.# define IN
56d50 43 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 CL_DOSERRORS.# d
56d60 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 efine INCL_DOSMI
56d70 53 43 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c SC.# define INCL
56d80 5f 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 _DOSPROCESS.# de
56d90 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 fine INCL_DOSMOD
56da0 55 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 ULEMGR.# define
56db0 49 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 INCL_DOSSEMAPHOR
56dc0 45 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 ES.# include <os
56dd0 32 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 2.h>.# include <
56de0 75 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e uconv.h>.# defin
56df0 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d e SQLITE_TEMPNAM
56e00 45 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 E_SIZE (CCHMAXPA
56e10 54 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 THCOMP).#else.#
56e20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
56e30 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a MPNAME_SIZE 200.
56e40 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 #endif../* If th
56e50 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d e SET_FULLSYNC m
56e60 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 acro is not defi
56e70 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 ned above, then
56e80 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d make it.** a no-
56e90 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 op.*/.#ifndef SE
56ea0 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 T_FULLSYNC.# def
56eb0 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 ine SET_FULLSYNC
56ec0 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a (x,y).#endif../*
56ed0 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
56ee0 73 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 size of a disk s
56ef0 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 ector.*/.#ifndef
56f00 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
56f10 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 SECTOR_SIZE.# de
56f20 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
56f30 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 ULT_SECTOR_SIZE
56f40 35 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 512.#endif../*.*
56f50 2a 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * Temporary file
56f60 73 20 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 s are named star
56f70 74 69 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 ting with this p
56f80 72 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 refix followed b
56f90 79 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 y 16 random.** a
56fa0 6c 70 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 lphanumeric char
56fb0 61 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 acters, and no f
56fc0 69 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 ile extension. T
56fd0 68 65 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 hey are stored i
56fe0 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 n the.** OS's st
56ff0 61 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 andard temporary
57000 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c file directory,
57010 20 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 and are deleted
57020 20 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a prior to exit..
57030 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 ** If sqlite is
57040 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 being embedded i
57050 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 n another progra
57060 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 m, you may wish
57070 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a to change the.**
57080 20 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 prefix to refle
57090 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 ct your program'
570a0 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 s name, so that
570b0 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 if your program
570c0 65 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 exits.** prematu
570d0 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 rely, old tempor
570e0 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 ary files can be
570f0 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 easily identifi
57100 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 ed. This can be
57110 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 done.** using -D
57120 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
57130 5f 50 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 _PREFIX=myprefix
57140 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 _ on the compile
57150 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a r command line..
57160 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 **.** 2006-10-31
57170 3a 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 : The default p
57180 72 65 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 refix used to be
57190 20 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 "sqlite_". But
571a0 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 then.** Mcafee
571b0 73 74 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 started using SQ
571c0 4c 69 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e Lite in their an
571d0 74 69 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 ti-virus product
571e0 20 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 and it.** start
571f0 65 64 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 ed putting files
57200 20 77 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 with the "sqlit
57210 65 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 e" name in the c
57220 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a :/temp folder..*
57230 2a 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d * This annoyed m
57240 61 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 any windows user
57250 73 2e 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 s. Those users
57260 77 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 would then do a
57270 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 .** Google searc
57280 68 20 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 h for "sqlite",
57290 66 69 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f find the telepho
572a0 6e 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 ne numbers of th
572b0 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 e.** developers
572c0 61 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 and call to wake
572d0 20 74 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 them up at nigh
572e0 74 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a t and complain..
572f0 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 ** For this reas
57300 6f 6e 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 on, the default
57310 6e 61 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 name prefix is c
57320 68 61 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 hanged to be "sq
57330 6c 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 lite" .** spelle
57340 64 20 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f d backwards. So
57350 20 74 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 the temp files
57360 61 72 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 are still identi
57370 66 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 fied, but.** any
57380 62 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 body smart enoug
57390 68 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 h to figure out
573a0 74 68 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f the code is also
573b0 20 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a likely smart.**
573c0 20 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 enough to know
573d0 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 that calling the
573e0 20 64 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 developer will
573f0 6e 6f 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 not help get rid
57400 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e .** of the file.
57410 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
57420 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
57430 46 49 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c FIX.# define SQL
57440 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 ITE_TEMP_FILE_PR
57450 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 EFIX "etilqs_".#
57460 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
57470 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 following value
57480 73 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 s may be passed
57490 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
574a0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c gument to.** sql
574b0 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 ite3OsLock(). Th
574c0 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 e various locks
574d0 65 78 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c exhibit the foll
574e0 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a owing semantics:
574f0 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 .**.** SHARED:
57500 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 Any number of
57510 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f processes may ho
57520 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ld a SHARED lock
57530 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e simultaneously.
57540 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 .** RESERVED: A
57550 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 single process
57560 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 may hold a RESER
57570 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 VED lock on a fi
57580 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 le at.**
57590 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 any time. Ot
575a0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 her processes ma
575b0 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 y hold and obtai
575c0 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 n new SHARED loc
575d0 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 ks..** PENDING:
575e0 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 A single proce
575f0 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 ss may hold a PE
57600 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 NDING lock on a
57610 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 file at.**
57620 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 any one ti
57630 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 me. Existing SHA
57640 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 RED locks may pe
57650 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 rsist, but no ne
57660 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 w.**
57670 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 SHARED locks may
57680 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 be obtained by
57690 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e other processes.
576a0 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 .** EXCLUSIVE: A
576b0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
576c0 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f precludes all o
576d0 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a ther locks..**.*
576e0 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d * PENDING_LOCK m
576f0 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 ay not be passed
57700 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c directly to sql
57710 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e ite3OsLock(). In
57720 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 stead, a.** proc
57730 65 73 73 20 74 68 61 74 20 72 65 71 75 65 73 74 ess that request
57740 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c s an EXCLUSIVE l
57750 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 ock may actually
57760 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e obtain a PENDIN
57770 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 G.** lock. This
57780 63 61 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 can be upgraded
57790 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 to an EXCLUSIVE
577a0 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 lock by a subseq
577b0 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 uent call to.**
577c0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e sqlite3OsLock().
577d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c .*/.#define NO_L
577e0 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 OCK 0.#d
577f0 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 efine SHARED_LOC
57800 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 K 1.#define
57810 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 RESERVED_LOCK
57820 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 2.#define PENDIN
57830 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 G_LOCK 3.#def
57840 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f ine EXCLUSIVE_LO
57850 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c CK 4../*.** Fil
57860 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a e Locking Notes:
57870 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 (Mostly about
57880 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f windows but also
57890 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 some info for U
578a0 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 nix).**.** We ca
578b0 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c nnot use LockFil
578c0 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 eEx() or UnlockF
578d0 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 ileEx() on Win95
578e0 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a /98/ME because.*
578f0 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e * those function
57900 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 s are not availa
57910 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 ble. So we use
57920 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 only LockFile()
57930 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c and.** UnlockFil
57940 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 e()..**.** LockF
57950 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e ile() prevents n
57960 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 ot just writing
57970 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 but also reading
57980 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 by other proces
57990 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 ses..** A SHARED
579a0 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 _LOCK is obtaine
579b0 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 d by locking a s
579c0 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 ingle randomly-c
579d0 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f hosen .** byte o
579e0 75 74 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 ut of a specific
579f0 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e range of bytes.
57a00 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 The lock byte i
57a10 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a s obtained at .*
57a20 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 * random so two
57a30 73 65 70 61 72 61 74 65 20 72 65 61 64 65 72 73 separate readers
57a40 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 can probably ac
57a50 63 65 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 cess the file at
57a60 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 the .** same ti
57a70 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 me, unless they
57a80 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 are unlucky and
57a90 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 choose the same
57aa0 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e lock byte..** An
57ab0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 EXCLUSIVE_LOCK
57ac0 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c is obtained by l
57ad0 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 ocking all bytes
57ae0 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a in the range..*
57af0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 * There can only
57b00 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 be one writer.
57b10 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b A RESERVED_LOCK
57b20 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 is obtained by
57b30 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e locking.** a sin
57b40 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 gle byte of the
57b50 66 69 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 file that is des
57b60 69 67 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 ignated as the r
57b70 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 eserved lock byt
57b80 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f e..** A PENDING_
57b90 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 LOCK is obtained
57ba0 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 by locking a de
57bb0 73 69 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 signated byte di
57bc0 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 fferent from.**
57bd0 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 the RESERVED_LOC
57be0 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e K byte..**.** On
57bf0 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 WinNT/2K/XP sys
57c00 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 tems, LockFileEx
57c10 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c () and UnlockFil
57c20 65 45 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 eEx() are availa
57c30 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 ble,.** which me
57c40 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 ans we can use r
57c50 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 eader/writer loc
57c60 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 ks. When reader
57c70 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a /writer locks.**
57c80 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c are used, the l
57c90 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e ock is placed on
57ca0 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 the same range
57cb0 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73 of bytes that is
57cc0 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f used.** for pro
57cd0 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 babilistic locki
57ce0 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d ng in Win95/98/M
57cf0 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c E. Hence, the l
57d00 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a ocking scheme.**
57d10 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 will support tw
57d20 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 o or more Win95
57d30 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f readers or two o
57d40 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 r more WinNT rea
57d50 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 ders..** But a s
57d60 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 ingle Win95 read
57d70 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 er will lock out
57d80 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 all WinNT reade
57d90 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a rs and a single.
57da0 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 ** WinNT reader
57db0 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c will lock out al
57dc0 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 l other Win95 re
57dd0 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 aders..**.** The
57de0 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 following #defi
57df0 6e 65 73 20 73 70 65 63 69 66 79 20 74 68 65 20 nes specify the
57e00 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 range of bytes u
57e10 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e sed for locking.
57e20 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 .** SHARED_SIZE
57e30 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
57e40 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 bytes available
57e50 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f in the pool fro
57e60 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e m which.** a ran
57e70 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 dom byte is sele
57e80 63 74 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 cted for a share
57e90 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f d lock. The poo
57ea0 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a l of bytes for.*
57eb0 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 * shared locks b
57ec0 65 67 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f egins at SHARED_
57ed0 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 FIRST. .**.** Th
57ee0 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 e same locking s
57ef0 74 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 trategy and.** b
57f00 79 74 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 yte ranges are u
57f10 73 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 sed for Unix. T
57f20 68 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 his leaves open
57f30 74 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f the possiblity o
57f40 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 f having.** clie
57f50 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 nts on win95, wi
57f60 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c nNT, and unix al
57f70 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 l talking to the
57f80 20 73 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c same shared fil
57f90 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 e.** and all loc
57fa0 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 king correctly.
57fb0 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 To do so would
57fc0 72 65 71 75 69 72 65 20 74 68 61 74 20 73 61 6d require that sam
57fd0 62 61 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a ba (or whatever.
57fe0 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 ** tool is being
57ff0 20 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 used for file s
58000 68 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e haring) implemen
58010 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 ts locks correct
58020 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 ly between.** wi
58030 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 ndows and unix.
58040 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 I'm guessing th
58050 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 at isn't likely
58060 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 to happen, but b
58070 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 y.** using the s
58080 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 ame locking rang
58090 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 e we are at leas
580a0 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f t open to the po
580b0 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a ssibility..**.**
580c0 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 Locking in wind
580d0 6f 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 ows is manditory
580e0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 . For this reas
580f0 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 on, we cannot st
58100 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 ore.** actual da
58110 74 61 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 ta in the bytes
58120 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 used for locking
58130 2e 20 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 . The pager nev
58140 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 er allocates.**
58150 74 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 the pages involv
58160 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 ed in locking th
58170 65 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 erefore. SHARED
58180 5f 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 _SIZE is selecte
58190 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c d so.** that all
581a0 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 locks will fit
581b0 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 on a single page
581c0 20 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e even at the min
581d0 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a imum page size..
581e0 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 ** PENDING_BYTE
581f0 64 65 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 defines the begi
58200 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 nning of the loc
58210 6b 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 ks. By default
58220 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 PENDING_BYTE.**
58230 69 73 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 is set high so t
58240 68 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 hat we don't hav
58250 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e e to allocate an
58260 20 75 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 unused page exc
58270 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 ept.** for very
58280 6c 61 72 67 65 20 64 61 74 61 62 61 73 65 73 2e large databases.
58290 20 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 But one should
582a0 20 74 65 73 74 20 74 68 65 20 70 61 67 65 20 73 test the page s
582b0 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a kipping logic .*
582c0 2a 20 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e * by setting PEN
582d0 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e DING_BYTE low an
582e0 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e d running the en
582f0 74 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 tire regression
58300 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 suite..**.** Cha
58310 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 nging the value
58320 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 of PENDING_BYTE
58330 72 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 results in a sub
58340 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 tly incompatible
58350 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e .** file format.
58360 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 Depending on h
58370 6f 77 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 ow it is changed
58380 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 , you might not
58390 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e notice.** the in
583a0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 compatibility ri
583b0 67 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 ght away, even r
583c0 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 unning a full re
583d0 67 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a gression test..*
583e0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f * The default lo
583f0 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e cation of PENDIN
58400 47 5f 42 59 54 45 20 69 73 20 74 68 65 20 66 69 G_BYTE is the fi
58410 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 rst byte past th
58420 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 e.** 1GB boundar
58430 79 2e 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 y..**.*/.#define
58440 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 PENDING_BYTE
58450 20 20 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e sqlite3Pendin
58460 67 42 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45 gByte.#define RE
58470 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 SERVED_BYTE
58480 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 (PENDING_BYTE+1)
58490 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f .#define SHARED_
584a0 46 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 FIRST (PEND
584b0 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 ING_BYTE+2).#def
584c0 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 ine SHARED_SIZE
584d0 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a 510../*.**
584e0 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 Wrapper around
584f0 4f 53 20 73 70 65 63 69 66 69 63 20 73 71 6c 69 OS specific sqli
58500 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75 te3_os_init() fu
58510 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nction..*/.SQLIT
58520 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58530 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 lite3OsInit(void
58540 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 );../* .** Funct
58550 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 ions for accessi
58560 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ng sqlite3_file
58570 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 methods .*/.SQLI
58580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58590 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 qlite3OsClose(sq
585a0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51 lite3_file*);.SQ
585b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
585c0 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 sqlite3OsRead(s
585d0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f qlite3_file*, vo
585e0 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 id*, int amt, i6
585f0 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 4 offset);.SQLIT
58600 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58610 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c lite3OsWrite(sql
58620 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 ite3_file*, cons
58630 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 t void*, int amt
58640 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 , i64 offset);.S
58650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58660 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 t sqlite3OsTrunc
58670 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
58680 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51 *, i64 size);.SQ
58690 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
586a0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 sqlite3OsSync(s
586b0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
586c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
586d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
586e0 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
586f0 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69 _file*, i64 *pSi
58700 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
58710 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58720 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 sLock(sqlite3_fi
58730 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
58740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58750 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 lite3OsUnlock(sq
58760 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
58770 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58780 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
58790 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
587a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
587b0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
587c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
587d0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 int sqlite3OsFi
587e0 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
587f0 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 3_file*,int,void
58800 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 *);.#define SQLI
58810 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
58820 41 4e 47 45 44 20 30 78 63 61 30 39 33 66 61 30 ANGED 0xca093fa0
58830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58840 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 int sqlite3OsSec
58850 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f torSize(sqlite3_
58860 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 file *id);.SQLIT
58870 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58880 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
58890 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c racteristics(sql
588a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a ite3_file *id);.
588b0 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ./* .** Function
588c0 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 s for accessing
588d0 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 sqlite3_vfs meth
588e0 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ods .*/.SQLITE_P
588f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58900 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 33 e3OsOpen(sqlite3
58910 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 _vfs *, const ch
58920 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 ar *, sqlite3_fi
58930 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 le*, int, int *)
58940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58950 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 int sqlite3OsDe
58960 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 lete(sqlite3_vfs
58970 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
58980 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
58990 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
589a0 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69 74 e3OsAccess(sqlit
589b0 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 e3_vfs *, const
589c0 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 char *, int, int
589d0 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 *pResOut);.SQLI
589e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
589f0 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 qlite3OsFullPath
58a00 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 name(sqlite3_vfs
58a10 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
58a20 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a , int, char *);.
58a30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
58a40 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
58a50 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ON.SQLITE_PRIVAT
58a60 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f E void *sqlite3O
58a70 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f sDlOpen(sqlite3_
58a80 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
58a90 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r *);.SQLITE_PRI
58aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
58ab0 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 3OsDlError(sqlit
58ac0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 e3_vfs *, int, c
58ad0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
58ae0 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 RIVATE void (*sq
58af0 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c lite3OsDlSym(sql
58b00 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 ite3_vfs *, void
58b10 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
58b20 29 29 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 ))(void);.SQLITE
58b30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
58b40 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 lite3OsDlClose(s
58b50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f qlite3_vfs *, vo
58b60 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a id *);.#endif /*
58b70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
58b80 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 D_EXTENSION */.S
58b90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58ba0 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f t sqlite3OsRando
58bb0 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
58bc0 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a s *, int, char *
58bd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58be0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 E int sqlite3OsS
58bf0 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 leep(sqlite3_vfs
58c00 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
58c10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58c20 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
58c30 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c e(sqlite3_vfs *,
58c40 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a double*);../*.*
58c50 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66 75 * Convenience fu
58c60 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65 6e nctions for open
58c70 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ing and closing
58c80 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a 20 files using .**
58c90 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
58ca0 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 to obtain space
58cb0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68 61 for the file-ha
58cc0 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a ndle structure..
58cd0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
58ce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f E int sqlite3OsO
58cf0 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 penMalloc(sqlite
58d00 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
58d10 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 har *, sqlite3_f
58d20 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 2a ile **, int,int*
58d30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58d40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
58d50 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 loseFree(sqlite3
58d60 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 69 _file *);..#endi
58d70 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f f /* _SQLITE_OS_
58d80 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a H_ */../********
58d90 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
58da0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
58db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58dd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
58de0 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
58df0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
58e00 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 off in sqliteInt
58e10 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
58e20 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
58e30 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6d ****** Include m
58e40 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d 69 utex.h in the mi
58e50 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
58e60 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
58e70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
58e80 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
58e90 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a e mutex.h ******
58ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ec0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
58ed0 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 7 August 28.**.*
58ee0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
58ef0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
58f00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
58f10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
58f20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
58f30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
58f40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
58f50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
58f60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
58f70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
58f80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
58f90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
58fa0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
58fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
58fc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
58fd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
58fe0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
58ff0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
59000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
59040 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
59050 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 ins the common h
59060 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d 75 eader for all mu
59070 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
59080 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ons..** The sqli
59090 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20 23 teInt.h header #
590a0 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66 69 includes this fi
590b0 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 le so that it is
590c0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f available.** to
590d0 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 all source file
590e0 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74 20 s. We break it
590f0 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 out in an effort
59100 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f 64 to keep the cod
59110 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67 61 e.** better orga
59120 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 nized..**.** NOT
59130 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65 73 E: source files
59140 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 69 should *not* #i
59150 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61 64 nclude this head
59160 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79 er file directly
59170 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c 65 ..** Source file
59180 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64 s should #includ
59190 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74 2e e the sqliteInt.
591a0 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20 74 h file and let t
591b0 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 6c hat file.** incl
591c0 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e 64 ude this one ind
591d0 69 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 2f 2a irectly..*/.../*
591e0 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 .** Figure out w
591f0 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 hat version of t
59200 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 he code to use.
59210 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 The choices are
59220 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f .**.** SQLITE_
59230 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 MUTEX_OMIT
59240 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 No mutex logi
59250 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 c. Not even stu
59260 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 bs. The.**
59270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59280 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 mutexes
59290 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e implemention can
592a0 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 not be overridde
592b0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
592c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
592d0 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a at start-time..
592e0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
592f0 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 UTEX_NOOP
59300 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 For single-thr
59310 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f eaded applicatio
59320 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 ns. No.**
59330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59340 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 mutual ex
59350 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 clusion is provi
59360 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a ded. But this.*
59370 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
59380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d im
59390 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e plementation can
593a0 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 be overridden a
593b0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
593c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
593d0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a start-time..**.
593e0 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
593f0 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46 X_PTHREADS F
59400 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 or multi-threade
59410 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f d applications o
59420 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 n Unix..**.**
59430 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
59440 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 For mu
59450 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 lti-threaded app
59460 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e lications on Win
59470 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 32..**.** SQLI
59480 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 TE_MUTEX_OS2
59490 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
594a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
594b0 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a tions on OS/2..*
594c0 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 /.#if !SQLITE_TH
594d0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
594e0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f e SQLITE_MUTEX_O
594f0 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 MIT.#endif.#if S
59500 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
59510 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
59520 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a ITE_MUTEX_NOOP).
59530 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f # if SQLITE_OS_
59540 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 UNIX.# define
59550 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
59560 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 HREADS.# elif S
59570 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 QLITE_OS_WIN.#
59580 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
59590 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 MUTEX_W32.# eli
595a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
595b0 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 # define SQLI
595c0 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 TE_MUTEX_OS2.#
595d0 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 else.# define
595e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f SQLITE_MUTEX_NO
595f0 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 OP.# endif.#end
59600 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
59610 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a E_MUTEX_OMIT./*.
59620 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 ** If this is a
59630 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 no-op implementa
59640 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 tion, implement
59650 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 everything as ma
59660 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 cros..*/.#define
59670 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
59680 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c lloc(X) ((sql
59690 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 ite3_mutex*)8).#
596a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
596b0 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 utex_free(X).#de
596c0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
596d0 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 ex_enter(X).#def
596e0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
596f0 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 x_try(X) SQ
59700 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 LITE_OK.#define
59710 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
59720 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 ave(X).#define s
59730 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
59740 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 d(X) 1.#defi
59750 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
59760 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 _notheld(X) 1.#
59770 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 define sqlite3Mu
59780 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 texAlloc(X)
59790 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ((sqlite3_mutex
597a0 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c *)8).#define sql
597b0 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 ite3MutexInit()
597c0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b SQLITE_OK
597d0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
597e0 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 MutexEnd().#endi
597f0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
59800 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 ITE_MUTEX_OMIT)
59810 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
59820 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 *** End of mutex
59830 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
59840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59860 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
59870 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
59880 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
59890 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
598a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
598b0 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 **/.../*.** Each
598c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
598d0 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 o be accessed by
598e0 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 the system is a
598f0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
59900 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
59910 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 tructure. There
59920 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 are normally tw
59930 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 o of these struc
59940 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tures.** in the
59950 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 sqlite.aDb[] arr
59960 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 ay. aDb[0] is t
59970 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
59980 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 file and.** aDb
59990 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 [1] is the datab
599a0 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f ase file used to
599b0 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 hold temporary
599c0 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f tables. Additio
599d0 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 nal.** databases
599e0 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64 may be attached
599f0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b ..*/.struct Db {
59a00 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
59a10 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
59a20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 of this database
59a30 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
59a40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
59a50 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 e B*Tree structu
59a60 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 re for this data
59a70 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
59a80 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 8 inTrans;
59a90 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 /* 0: not wr
59aa0 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e itable. 1: Tran
59ab0 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 saction. 2: Che
59ac0 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 ckpoint */. u8
59ad0 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 safety_level;
59ae0 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 /* How aggress
59af0 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 ive at syncing d
59b00 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 ata to disk */.
59b10 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
59b20 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
59b30 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 to database sch
59b40 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 ema (possibly sh
59b50 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ared) */.};../*.
59b60 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
59b70 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
59b80 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 structure stores
59b90 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 a database sche
59ba0 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ma..**.** If the
59bb0 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 re are no virtua
59bc0 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 l tables configu
59bd0 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 red in this sche
59be0 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d ma, the.** Schem
59bf0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 a.db variable is
59c00 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 set to NULL. Af
59c10 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 ter the first vi
59c20 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 rtual table.** h
59c30 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 as been added, i
59c40 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e t is set to poin
59c50 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
59c60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a e connection .**
59c70 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
59c80 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 the connection.
59c90 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 Once a virtual t
59ca0 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a able has been.**
59cb0 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 added to the Sc
59cc0 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 hema structure a
59cd0 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 nd the Schema.db
59ce0 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 variable popula
59cf0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 ted, .** only th
59d00 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e at database conn
59d10 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 ection may use t
59d20 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 he Schema to pre
59d30 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 pare .** stateme
59d40 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 nts..*/.struct S
59d50 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 chema {. int sc
59d60 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f hema_cookie; /
59d70 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d * Database schem
59d80 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 a version number
59d90 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a for this file *
59da0 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 /. Hash tblHash
59db0 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 ; /* All
59dc0 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 tables indexed b
59dd0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
59de0 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 idxHash;
59df0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 /* All (named)
59e00 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 indices indexed
59e10 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 by name */. Has
59e20 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 h trigHash;
59e30 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 /* All trigger
59e40 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
59e50 65 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 e */. Hash fkey
59e60 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 Hash; /* A
59e70 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 ll foreign keys
59e80 62 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 by referenced ta
59e90 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 ble name */. Ta
59ea0 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 ble *pSeqTab;
59eb0 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 /* The sqlite
59ec0 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
59ed0 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 used by AUTOINCR
59ee0 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 EMENT */. u8 fi
59ef0 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 le_format;
59f00 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 /* Schema format
59f10 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 version for thi
59f20 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 s file */. u8 e
59f30 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
59f40 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e /* Text encodin
59f50 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 g used by this d
59f60 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 atabase */. u16
59f70 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
59f80 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 /* Flags assoc
59f90 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
59fa0 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 schema */. int
59fb0 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 cache_size;
59fc0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
59fd0 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 ges to use in th
59fe0 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 e cache */.#ifnd
59ff0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
5a000 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 IRTUALTABLE. sq
5a010 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
5a020 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f /* "Owner" co
5a030 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f nnection. See co
5a040 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 mment above */.#
5a050 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
5a060 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e These macros can
5a070 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 be used to test
5a080 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 , set, or clear
5a090 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 bits in the .**
5a0a0 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a Db.flags field..
5a0b0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 */.#define DbHas
5a0c0 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 Property(D,I,P)
5a0d0 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 (((D)->aDb[I
5a0e0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
5a0f0 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 &(P))==(P)).#def
5a100 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 ine DbHasAnyProp
5a110 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 erty(D,I,P) (((
5a120 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 D)->aDb[I].pSche
5a130 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d ma->flags&(P))!=
5a140 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 0).#define DbSet
5a150 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 Property(D,I,P)
5a160 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e (D)->aDb[I].
5a170 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d pSchema->flags|=
5a180 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c (P).#define DbCl
5a190 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c earProperty(D,I,
5a1a0 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d P) (D)->aDb[I]
5a1b0 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 .pSchema->flags&
5a1c0 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c =~(P)../*.** All
5a1d0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 owed values for
5a1e0 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 the DB.flags fie
5a1f0 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 ld..**.** The DB
5a200 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c _SchemaLoaded fl
5a210 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 ag is set after
5a220 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
5a230 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ema has been.**
5a240 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e read into intern
5a250 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a al hash tables..
5a260 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 **.** DB_Unreset
5a270 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 Views means that
5a280 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 one or more vie
5a290 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e ws have column n
5a2a0 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 ames that.** hav
5a2b0 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 e been filled ou
5a2c0 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d t. If the schem
5a2d0 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 a changes, these
5a2e0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 column names mi
5a2f0 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 ght.** changes a
5a300 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 nd so the view w
5a310 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 ill need to be r
5a320 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 eset..*/.#define
5a330 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 DB_SchemaLoaded
5a340 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 0x0001 /* T
5a350 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 he schema has be
5a360 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 en loaded */.#de
5a370 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 fine DB_UnresetV
5a380 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 iews 0x0002
5a390 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 /* Some views ha
5a3a0 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d ve defined colum
5a3b0 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 n names */.#defi
5a3c0 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 ne DB_Empty
5a3d0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 0x0004 /*
5a3e0 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 The file is emp
5a3f0 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 ty (length 0 byt
5a400 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 es) */../*.** Th
5a410 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 e number of diff
5a420 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 erent kinds of t
5a430 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 hings that can b
5a440 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 e limited.** usi
5a450 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c ng the sqlite3_l
5a460 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 imit() interface
5a470 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5a480 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c ITE_N_LIMIT (SQL
5a490 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 ITE_LIMIT_TRIGGE
5a4a0 52 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a R_DEPTH+1)../*.*
5a4b0 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * Lookaside mall
5a4c0 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 oc is a set of f
5a4d0 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 ixed-size buffer
5a4e0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 s that can be us
5a4f0 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 ed.** to satisfy
5a500 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 small transient
5a510 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
5a520 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 on requests for
5a530 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 objects.** assoc
5a540 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 iated with a par
5a550 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
5a560 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
5a570 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b e use of.** look
5a580 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f aside malloc pro
5a590 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 vides a signific
5a5a0 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ant performance
5a5b0 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 enhancement.** (
5a5c0 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 approx 10%) by a
5a5d0 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 voiding numerous
5a5e0 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 malloc/free req
5a5f0 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 uests while pars
5a600 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ing.** SQL state
5a610 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ments..**.** The
5a620 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 Lookaside struc
5a630 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 ture holds confi
5a640 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 guration informa
5a650 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a tion about the.*
5a660 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * lookaside mall
5a670 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 oc subsystem. E
5a680 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 ach available me
5a690 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
5a6a0 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 in.** the lookas
5a6b0 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 ide subsystem is
5a6c0 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e stored on a lin
5a6d0 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b ked list of Look
5a6e0 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a asideSlot.** obj
5a6f0 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b ects..**.** Look
5a700 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e aside allocation
5a710 73 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 s are only allow
5a720 65 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 ed for objects t
5a730 68 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 hat are associat
5a740 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 ed.** with a par
5a750 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
5a760 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 connection. He
5a770 6e 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f nce, schema info
5a780 72 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a rmation cannot.*
5a790 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c * be stored in l
5a7a0 6f 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 ookaside because
5a7b0 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 in shared cache
5a7c0 20 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 mode the schema
5a7d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
5a7e0 69 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c is shared by mul
5a7f0 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 tiple database c
5a800 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 onnections. The
5a810 72 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 refore, while pa
5a820 72 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 rsing.** schema
5a830 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 information, the
5a840 20 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 Lookaside.bEnab
5a850 6c 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 led flag is clea
5a860 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c red so that.** l
5a870 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 ookaside allocat
5a880 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 ions are not use
5a890 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 d to construct t
5a8a0 68 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 he schema object
5a8b0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f s..*/.struct Loo
5a8c0 6b 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 kaside {. u16 s
5a8d0 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 z;
5a8e0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 /* Size of ea
5a8f0 63 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 ch buffer in byt
5a900 65 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 es */. u8 bEnab
5a910 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 led;
5a920 2f 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 /* False to disa
5a930 62 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 ble new lookasid
5a940 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f e allocations */
5a950 0a 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b . u8 bMalloced;
5a960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
5a970 75 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 ue if pStart obt
5a980 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
5a990 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 e3_malloc() */.
5a9a0 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 int nOut;
5a9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5a9c0 65 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 er of buffers cu
5a9d0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
5a9e0 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f out */. int mxO
5a9f0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
5aa00 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 /* Highwater ma
5aa10 72 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 rk for nOut */.
5aa20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
5aa30 70 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 pFree; /* List
5aa40 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 of available bu
5aa50 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 ffers */. void
5aa60 2a 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 *pStart;
5aa70 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
5aa80 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 of available me
5aa90 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 mory space */.
5aaa0 76 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 void *pEnd;
5aab0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
5aac0 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f byte past end o
5aad0 66 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 f available spac
5aae0 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c e */.};.struct L
5aaf0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 ookasideSlot {.
5ab00 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
5ab10 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 pNext; /* Nex
5ab20 74 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 t buffer in the
5ab30 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 list of free buf
5ab40 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a fers */.};../*.*
5ab50 2a 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 * A hash table f
5ab60 6f 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 or function defi
5ab70 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 nitions..**.** H
5ab80 61 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 ash each FuncDef
5ab90 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 structure into
5aba0 6f 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 one of the FuncD
5abb0 65 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 efHash.a[] slots
5abc0 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 ..** Collisions
5abd0 61 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 are on the FuncD
5abe0 65 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a ef.pHash chain..
5abf0 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 */.struct FuncDe
5ac00 66 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 fHash {. FuncDe
5ac10 66 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 f *a[23];
5ac20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f /* Hash table fo
5ac30 72 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d r functions */.}
5ac40 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 ;../*.** Each da
5ac50 74 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 tabase is an ins
5ac60 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
5ac70 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
5ac80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5ac90 74 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 te.lastRowid rec
5aca0 6f 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e ords the last in
5acb0 73 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 sert rowid gener
5acc0 61 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e ated by an.** in
5acd0 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 sert statement.
5ace0 20 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 Inserts on view
5acf0 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 s do not affect
5ad00 69 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 its value. Each
5ad10 0a 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 .** trigger has
5ad20 69 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c its own context,
5ad30 20 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 so that lastRow
5ad40 69 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 id can be update
5ad50 64 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 d inside.** trig
5ad60 67 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 gers as usual.
5ad70 54 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c The previous val
5ad80 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f ue will be resto
5ad90 72 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 red once the tri
5ada0 67 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 gger.** exits.
5adb0 55 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 Upon entering a
5adc0 62 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 before or instea
5add0 64 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 d of trigger, la
5ade0 73 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a stRowid is no.**
5adf0 20 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 longer (since a
5ae00 66 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 fter version 2.8
5ae10 2e 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 .12) reset to -1
5ae20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5ae30 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 te.nChange does
5ae40 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 not count change
5ae50 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 s within trigger
5ae60 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a s and keeps no.*
5ae70 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 * context. It i
5ae80 73 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74 s reset at start
5ae90 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 of sqlite3_exec
5aea0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e ..** The sqlite.
5aeb0 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 lsChange represe
5aec0 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f nts the number o
5aed0 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 f changes made b
5aee0 79 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e y the last.** in
5aef0 73 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 sert, update, or
5af00 20 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e delete statemen
5af10 74 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 t. It remains c
5af20 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f onstant througho
5af30 75 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 ut the.** length
5af40 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 of a statement
5af50 61 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 and is then upda
5af60 74 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 ted by OP_SetCou
5af70 6e 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 nts. It keeps a
5af80 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 .** context stac
5af90 6b 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 k just like last
5afa0 52 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 Rowid so that th
5afb0 65 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 e count of chang
5afc0 65 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 es.** within a t
5afd0 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 rigger is not se
5afe0 65 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 en outside the t
5aff0 72 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 rigger. Changes
5b000 20 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 to views do not
5b010 0a 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 .** affect the v
5b020 61 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 alue of lsChange
5b030 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e ..** The sqlite.
5b040 63 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 csChange keeps t
5b050 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 rack of the numb
5b060 65 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 er of current ch
5b070 61 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 anges (since.**
5b080 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 the last stateme
5b090 6e 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 nt) and is used
5b0a0 74 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 to update sqlite
5b0b0 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a _lsChange..**.**
5b0c0 20 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 The member vari
5b0d0 61 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 ables sqlite.err
5b0e0 43 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 Code, sqlite.zEr
5b0f0 72 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e rMsg and sqlite.
5b100 7a 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f zErrMsg16.** sto
5b110 72 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 re the most rece
5b120 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e nt error code an
5b130 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 d, if applicable
5b140 2c 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a , string. The.**
5b150 20 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 internal functi
5b160 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 on sqlite3Error(
5b170 29 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 ) is used to set
5b180 20 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 these variables
5b190 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 .** consistently
5b1a0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
5b1b0 74 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f te3 {. sqlite3_
5b1c0 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 vfs *pVfs;
5b1d0 20 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 /* OS Inte
5b1e0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e rface */. int n
5b1f0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
5b200 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5b210 65 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 er of backends c
5b220 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 urrently in use
5b230 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 */. Db *aDb;
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b250 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e /* All backen
5b260 64 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 ds */. int flag
5b270 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
5b280 20 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c /* Miscell
5b290 61 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 aneous flags. Se
5b2a0 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 e below */. int
5b2b0 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 openFlags;
5b2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
5b2d0 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 ags passed to sq
5b2e0 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
5b2f0 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f ) */. int errCo
5b300 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
5b310 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5b320 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 ent error code (
5b330 53 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 SQLITE_*) */. i
5b340 6e 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 nt errMask;
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b360 26 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 & result codes w
5b370 69 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 ith this before
5b380 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 returning */. u
5b390 38 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 8 autoCommit;
5b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b3b0 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 The auto-commit
5b3c0 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 flag. */. u8 te
5b3d0 6d 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 mp_store;
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 /* 1: f
5b3f0 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a ile 2: memory 0:
5b400 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 default */. u8
5b410 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 mallocFailed;
5b420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
5b430 72 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73 rue if we have s
5b440 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 een a malloc fai
5b450 6c 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c lure */. u8 dfl
5b460 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 tLockMode;
5b470 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 /* Defau
5b480 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 lt locking-mode
5b490 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 for attached dbs
5b4a0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 */. u8 dfltJou
5b4b0 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 rnalMode;
5b4c0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a /* Default j
5b4d0 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 ournal mode for
5b4e0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a attached dbs */.
5b4f0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 signed char ne
5b500 78 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 xtAutovac;
5b510 2f 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 /* Autovac setti
5b520 6e 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 ng after VACUUM
5b530 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 if >=0 */. int
5b540 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 nextPagesize;
5b550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
5b560 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 esize after VACU
5b570 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e UM if >0 */. in
5b580 74 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 t nTable;
5b590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5b5a0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
5b5b0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
5b5c0 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 */. CollSeq *pD
5b5d0 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 fltColl;
5b5e0 20 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c /* The defaul
5b5f0 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 t collating sequ
5b600 65 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f ence (BINARY) */
5b610 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 . i64 lastRowid
5b620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b630 20 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 /* ROWID of mos
5b640 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 t recent insert
5b650 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 (see above) */.
5b660 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
5b670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b680 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 * Magic number f
5b690 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 or detect librar
5b6a0 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e y misuse */. in
5b6b0 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 t nChange;
5b6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
5b6d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
5b6e0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
5b6f0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 () */. int nTot
5b700 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 alChange;
5b710 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
5b720 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
5b730 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
5b740 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f () */. sqlite3_
5b750 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
5b760 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 /* Connect
5b770 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 ion mutex */. i
5b780 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 nt aLimit[SQLITE
5b790 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 _N_LIMIT]; /*
5b7a0 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 Limits */. stru
5b7b0 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e ct sqlite3InitIn
5b7c0 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 fo { /* Inf
5b7d0 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 ormation used du
5b7e0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
5b7f0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ion */. int i
5b800 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
5b810 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 /* When b
5b820 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 ack is being ini
5b830 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 tialized */.
5b840 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 int newTnum;
5b850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
5b860 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ootpage of table
5b870 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
5b880 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 ed */. u8 bus
5b890 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
5b8a0 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 /* TRUE if
5b8b0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 currently initi
5b8c0 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 alizing */. u
5b8d0 38 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b 8 orphanTrigger;
5b8e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
5b8f0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 st statement is
5b900 6f 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72 orphaned TEMP tr
5b910 69 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 igger */. } ini
5b920 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 t;. int nExtens
5b930 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 ion;
5b940 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5b950 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e loaded extension
5b960 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 s */. void **aE
5b970 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 xtension;
5b980 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
5b990 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 shared library
5b9a0 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 handles */. str
5b9b0 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b uct Vdbe *pVdbe;
5b9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 /* Li
5b9d0 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 st of active vir
5b9e0 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f tual machines */
5b9f0 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 . int activeVdb
5ba00 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 eCnt;
5ba10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 /* Number of VD
5ba20 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 BEs currently ex
5ba30 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ecuting */. int
5ba40 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20 writeVdbeCnt;
5ba50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5ba60 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56 mber of active V
5ba70 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72 DBEs that are wr
5ba80 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 iting */. void
5ba90 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c (*xTrace)(void*,
5baa0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 const char*);
5bab0 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 /* Trace fu
5bac0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
5bad0 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 *pTraceArg;
5bae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5baf0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
5bb00 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 t to the trace f
5bb10 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
5bb20 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f d (*xProfile)(vo
5bb30 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c id*,const char*,
5bb40 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c u64); /* Profil
5bb50 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ing function */.
5bb60 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 void *pProfile
5bb70 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
5bb80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5bb90 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 rgument to profi
5bba0 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 le function */.
5bbb0 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 void *pCommitAr
5bbc0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
5bbd0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5bbe0 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 o xCommitCallbac
5bbf0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 k() */ . int
5bc00 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 (*xCommitCallbac
5bc10 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a k)(void*); /*
5bc20 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
5bc30 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
5bc40 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 oid *pRollbackAr
5bc50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
5bc60 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
5bc70 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 xRollbackCallbac
5bc80 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 k() */ . void
5bc90 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c (*xRollbackCall
5bca0 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a back)(void*); /*
5bcb0 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
5bcc0 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
5bcd0 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b oid *pUpdateArg;
5bce0 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 . void (*xUpdat
5bcf0 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a eCallback)(void*
5bd00 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 ,int, const char
5bd10 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 *,const char*,sq
5bd20 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 lite_int64);. v
5bd30 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 oid(*xCollNeeded
5bd40 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
5bd50 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
5bd60 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f nst char*);. vo
5bd70 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 id(*xCollNeeded1
5bd80 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 6)(void*,sqlite3
5bd90 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 *,int eTextRep,c
5bda0 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 onst void*);. v
5bdb0 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 oid *pCollNeeded
5bdc0 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 Arg;. sqlite3_v
5bdd0 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20 alue *pErr;
5bde0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5bdf0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5be00 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 e */. char *zEr
5be10 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
5be20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5be30 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5be40 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e (UTF-8 encoded
5be50 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 ) */. char *zEr
5be60 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20 rMsg16;
5be70 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5be80 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5be90 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 e (UTF-16 encode
5bea0 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a d) */. union {.
5beb0 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 volatile int
5bec0 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 isInterrupted;
5bed0 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 /* True if sqlit
5bee0 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 e3_interrupt has
5bef0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a been called */.
5bf00 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 double notUs
5bf10 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 ed1;
5bf20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d /* Spacer */. }
5bf30 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 u1;. Lookaside
5bf40 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 lookaside;
5bf50 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 /* Lookasid
5bf60 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 e malloc configu
5bf70 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 ration */.#ifnde
5bf80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
5bf90 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e THORIZATION. in
5bfa0 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a t (*xAuth)(void*
5bfb0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
5bfc0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
5bfd0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
5bfe0 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 har*);.
5bff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c000 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 /* Access
5c010 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 authorization f
5c020 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
5c030 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20 d *pAuthArg;
5c040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
5c050 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
5c060 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75 e access auth fu
5c070 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 nction */.#endif
5c080 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5c090 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 OMIT_PROGRESS_CA
5c0a0 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 LLBACK. int (*x
5c0b0 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a Progress)(void *
5c0c0 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 ); /* The pr
5c0d0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
5c0e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 */. void *pProg
5c0f0 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20 ressArg;
5c100 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5c110 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 o the progress c
5c120 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 allback */. int
5c130 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 nProgressOps;
5c140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5c150 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 mber of opcodes
5c160 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c for progress cal
5c170 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a lback */.#endif.
5c180 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
5c190 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
5c1a0 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b . Hash aModule;
5c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c1c0 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 /* populated by
5c1d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
5c1e0 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 module() */. Ta
5c1f0 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20 ble *pVTab;
5c200 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 /* v
5c210 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20 tab with active
5c220 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d Connect/Create m
5c230 65 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c ethod */. VTabl
5c240 65 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 e **aVTrans;
5c250 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 /* Virt
5c260 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20 ual tables with
5c270 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
5c280 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 s */. int nVTra
5c290 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ns;
5c2a0 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 /* Allocate
5c2b0 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e d size of aVTran
5c2c0 73 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 s */. VTable *p
5c2d0 44 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f Disconnect; /
5c2e0 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 * Disconnect the
5c2f0 73 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 se in next sqlit
5c300 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a e3_prepare() */.
5c310 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 #endif. FuncDef
5c320 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 Hash aFunc;
5c330 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 /* Hash t
5c340 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 able of connecti
5c350 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a on functions */.
5c360 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b Hash aCollSeq;
5c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c380 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 /* All collating
5c390 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 sequences */.
5c3a0 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 BusyHandler busy
5c3b0 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a Handler; /*
5c3c0 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a Busy callback *
5c3d0 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 /. int busyTime
5c3e0 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 out;
5c3f0 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 /* Busy handle
5c400 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 r timeout, in ms
5c410 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 ec */. Db aDbSt
5c420 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 atic[2];
5c430 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 /* Static
5c440 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 space for the 2
5c450 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 default backends
5c460 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 */. Savepoint
5c470 2a 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 *pSavepoint;
5c480 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 /* List of a
5c490 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
5c4a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 */. int nSavep
5c4b0 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 oint;
5c4c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
5c4d0 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e non-transaction
5c4e0 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 savepoints */.
5c4f0 20 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b int nStatement;
5c500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5c510 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 * Number of nest
5c520 65 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 ed statement-tra
5c530 6e 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 nsactions */.
5c540 75 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e u8 isTransaction
5c550 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a Savepoint; /*
5c560 20 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 True if the out
5c570 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 ermost savepoint
5c580 20 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 is a TS */. i6
5c590 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 4 nDeferredCons;
5c5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5c5b0 65 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 et deferred cons
5c5c0 74 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 traints this tra
5c5d0 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 nsaction. */..#i
5c5e0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
5c5f0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 LE_UNLOCK_NOTIFY
5c600 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
5c610 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 ing variables ar
5c620 65 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 e all protected
5c630 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 by the STATIC_MA
5c640 53 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 STER . ** mutex
5c650 2c 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 , not by sqlite3
5c660 2e 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 .mutex. They are
5c670 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e used by code in
5c680 20 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a notify.c. . **
5c690 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e . ** When X.pUn
5c6a0 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d lockConnection==
5c6b0 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 Y, that means th
5c6c0 61 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 at X is waiting
5c6d0 66 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e for Y to. ** un
5c6e0 6c 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 lock so that it
5c6f0 63 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a can proceed.. *
5c700 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 *. ** When X.pB
5c710 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f lockingConnectio
5c720 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 n==Y, that means
5c730 20 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 that something
5c740 74 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a that X tried. *
5c750 2a 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 * tried to do re
5c760 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 cently failed wi
5c770 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 th an SQLITE_LOC
5c780 4b 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f KED error due to
5c790 20 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 locks. ** held
5c7a0 20 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 by Y.. */. sq
5c7b0 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 lite3 *pBlocking
5c7c0 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 Connection; /* C
5c7d0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 onnection that c
5c7e0 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 aused SQLITE_LOC
5c7f0 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 KED */. sqlite3
5c800 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 *pUnlockConnect
5c810 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f ion; /
5c820 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 * Connection to
5c830 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b watch for unlock
5c840 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c */. void *pUnl
5c850 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 ockArg;
5c860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5c870 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f rgument to xUnlo
5c880 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f ckNotify */. vo
5c890 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 id (*xUnlockNoti
5c8a0 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 fy)(void **, int
5c8b0 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f ); /* Unlock no
5c8c0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f tify callback */
5c8d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 . sqlite3 *pNex
5c8e0 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 tBlocked;
5c8f0 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 /* Next in list
5c900 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 of all blocked
5c910 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 connections */.#
5c920 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
5c930 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f A macro to disco
5c940 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 ver the encoding
5c950 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a of a database..
5c960 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 */.#define ENC(d
5c970 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d b) ((db)->aDb[0]
5c980 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a .pSchema->enc)..
5c990 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 /*.** Possible v
5c9a0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 alues for the sq
5c9b0 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f lite.flags and o
5c9c0 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 r Db.flags field
5c9d0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 s..**.** On sqli
5c9e0 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 te.flags, the SQ
5c9f0 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c LITE_InTrans val
5ca00 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 ue means that we
5ca10 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 have.** execute
5ca20 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 d a BEGIN. On D
5ca30 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f b.flags, SQLITE_
5ca40 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 InTrans means a
5ca50 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 statement.** tra
5ca60 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
5ca70 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 ve on that parti
5ca80 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 cular database f
5ca90 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ile..*/.#define
5caa0 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 SQLITE_VdbeTrace
5cab0 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0x00000001
5cac0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 /* True to tra
5cad0 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f ce VDBE executio
5cae0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
5caf0 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 ITE_InTrans
5cb00 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 0x00000008 /
5cb10 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 * True if in a t
5cb20 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
5cb30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 efine SQLITE_Int
5cb40 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 ernChanges 0x00
5cb50 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 000010 /* Uncom
5cb60 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c mitted Hash tabl
5cb70 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 e changes */.#de
5cb80 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c fine SQLITE_Full
5cb90 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 ColNames 0x000
5cba0 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 00020 /* Show f
5cbb0 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ull column names
5cbc0 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 on SELECT */.#d
5cbd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f efine SQLITE_Sho
5cbe0 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 rtColNames 0x00
5cbf0 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 000040 /* Show
5cc00 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 short columns na
5cc10 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 mes */.#define S
5cc20 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 QLITE_CountRows
5cc30 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 0x00000080
5cc40 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 /* Count rows c
5cc50 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 hanged by INSERT
5cc60 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 , */.
5cc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5cc90 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 * DELETE, or U
5cca0 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e PDATE and return
5ccb0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
5ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ccd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5cce0 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 the count usi
5ccf0 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a ng a callback. *
5cd00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5cd10 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 _NullCallback
5cd20 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 0x00000100 /* I
5cd30 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 nvoke the callba
5cd40 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a ck once if the *
5cd50 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
5cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cd70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 /*
5cd80 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 result set is e
5cd90 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mpty */.#define
5cda0 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 SQLITE_SqlTrace
5cdb0 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0x00000200
5cdc0 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 /* Debug print
5cdd0 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 SQL as it execu
5cde0 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 tes */.#define S
5cdf0 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e QLITE_VdbeListin
5ce00 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 g 0x00000400
5ce10 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e /* Debug listin
5ce20 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 gs of VDBE progr
5ce30 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ams */.#define S
5ce40 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d QLITE_WriteSchem
5ce50 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 a 0x00000800
5ce60 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 /* OK to update
5ce70 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a SQLITE_MASTER *
5ce80 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5ce90 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 _NoReadlock
5cea0 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 0x00001000 /* R
5ceb0 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 eadlocks are omi
5cec0 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 tted when .
5ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cef0 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e ** accessin
5cf00 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 g read-only data
5cf10 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 bases */.#define
5cf20 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 SQLITE_IgnoreCh
5cf30 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 ecks 0x0000200
5cf40 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 0 /* Do not enf
5cf50 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 orce check const
5cf60 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e raints */.#defin
5cf70 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 e SQLITE_ReadUnc
5cf80 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 ommitted 0x00004
5cf90 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 000 /* For share
5cfa0 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a d-cache mode */.
5cfb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
5cfc0 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 egacyFileFmt 0x
5cfd0 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 00008000 /* Cre
5cfe0 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 ate new database
5cff0 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f s in format 1 */
5d000 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5d010 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 FullFSync 0
5d020 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 x00010000 /* Us
5d030 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 e full fsync on
5d040 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 the backend */.#
5d050 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f define SQLITE_Lo
5d060 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 adExtension 0x0
5d070 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 0020000 /* Enab
5d080 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le load_extensio
5d090 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 n */..#define SQ
5d0a0 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 LITE_RecoveryMod
5d0b0 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20 e 0x00040000
5d0c0 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 /* Ignore schema
5d0d0 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 errors */.#defi
5d0e0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 ne SQLITE_Revers
5d0f0 65 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30 eOrder 0x00100
5d100 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 000 /* Reverse
5d110 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 unordered SELECT
5d120 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
5d130 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 ITE_RecTriggers
5d140 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f 0x00200000 /
5d150 2a 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 * Enable recursi
5d160 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 ve triggers */.#
5d170 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f define SQLITE_Fo
5d180 72 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 reignKeys 0x0
5d190 30 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 0400000 /* Enfo
5d1a0 72 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 rce foreign key
5d1b0 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a constraints */.
5d1c0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 ./*.** Possible
5d1d0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 values for the s
5d1e0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c qlite.magic fiel
5d1f0 64 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 d..** The number
5d200 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61 s are obtained a
5d210 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 t random and hav
5d220 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 e no special mea
5d230 6e 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 ning, other.** t
5d240 68 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e han being distin
5d250 63 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 ct from one anot
5d260 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 her..*/.#define
5d270 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
5d280 4e 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37 N 0xa029a697
5d290 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 /* Database is
5d2a0 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 open */.#define
5d2b0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c SQLITE_MAGIC_CL
5d2c0 4f 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33 OSED 0x9f3c2d3
5d2d0 33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 3 /* Database i
5d2e0 73 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 s closed */.#def
5d2f0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
5d300 5f 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37 _SICK 0x4b77
5d310 31 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 1290 /* Error a
5d320 6e 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 nd awaiting clos
5d330 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
5d340 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 ITE_MAGIC_BUSY
5d350 20 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 0xf03b7906 /
5d360 2a 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 * Database curre
5d370 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 ntly in use */.#
5d380 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
5d390 47 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 GIC_ERROR 0xb
5d3a0 35 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 5357930 /* An S
5d3b0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 QLITE_MISUSE err
5d3c0 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a or occurred */..
5d3d0 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 /*.** Each SQL f
5d3e0 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e unction is defin
5d3f0 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ed by an instanc
5d400 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5d410 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
5d420 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 A pointer to t
5d430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
5d440 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 stored in the s
5d450 71 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 qlite.aFunc.** h
5d460 61 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e ash table. When
5d470 20 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 multiple functi
5d480 6f 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d ons have the sam
5d490 65 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 e name, the hash
5d4a0 20 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 table.** points
5d4b0 20 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 to a linked lis
5d4c0 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 t of these struc
5d4d0 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 tures..*/.struct
5d4e0 20 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 FuncDef {. i16
5d4f0 20 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 nArg;
5d500 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
5d510 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 rguments. -1 me
5d520 61 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f ans unlimited */
5d530 0a 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 . u8 iPrefEnc;
5d540 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 /* Prefe
5d550 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
5d560 6e 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c ng (SQLITE_UTF8,
5d570 20 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 16LE, 16BE) */.
5d580 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20 u8 flags;
5d590 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 /* Some c
5d5a0 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 ombination of SQ
5d5b0 4c 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 LITE_FUNC_* */.
5d5c0 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61 void *pUserData
5d5d0 3b 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61 ; /* User da
5d5e0 74 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a ta parameter */.
5d5f0 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 FuncDef *pNext
5d600 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 ; /* Next f
5d610 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d unction with sam
5d620 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 e name */. void
5d630 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 (*xFunc)(sqlite
5d640 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
5d650 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b qlite3_value**);
5d660 20 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63 /* Regular func
5d670 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
5d680 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f *xStep)(sqlite3_
5d690 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
5d6a0 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f ite3_value**); /
5d6b0 2a 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70 * Aggregate step
5d6c0 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 */. void (*xFi
5d6d0 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f nalize)(sqlite3_
5d6e0 63 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20 context*);
5d6f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 /* Agg
5d700 72 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 regate finalizer
5d710 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
5d720 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 e; /* SQ
5d730 4c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 L name of the fu
5d740 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e nction. */. Fun
5d750 63 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 cDef *pHash;
5d760 20 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 /* Next with a
5d770 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 different name
5d780 62 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 but the same has
5d790 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 h */.};../*.** P
5d7a0 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 ossible values f
5d7b0 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 or FuncDef.flags
5d7c0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5d7d0 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 TE_FUNC_LIKE
5d7e0 20 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 0x01 /* Candida
5d7f0 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 te for the LIKE
5d800 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a optimization */.
5d810 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
5d820 55 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 UNC_CASE 0x0
5d830 32 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 2 /* Case-sensit
5d840 69 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 ive LIKE-type fu
5d850 6e 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e nction */.#defin
5d860 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 e SQLITE_FUNC_EP
5d870 48 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 HEM 0x04 /* E
5d880 70 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 phemeral. Delet
5d890 65 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 e with VDBE */.#
5d8a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d8b0 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 NC_NEEDCOLL 0x08
5d8c0 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 /* sqlite3GetFu
5d8d0 6e 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 ncCollSeq() migh
5d8e0 74 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 t be called */.#
5d8f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d900 4e 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30 NC_PRIVATE 0x10
5d910 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 /* Allowed for
5d920 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c internal use onl
5d930 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
5d940 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 ITE_FUNC_COUNT
5d950 20 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 0x20 /* Built-
5d960 69 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 in count(*) aggr
5d970 65 67 61 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 egate */.#define
5d980 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 SQLITE_FUNC_COA
5d990 4c 45 53 43 45 20 30 78 34 30 20 2f 2a 20 42 75 LESCE 0x40 /* Bu
5d9a0 69 6c 74 2d 69 6e 20 63 6f 61 6c 65 73 63 65 28 ilt-in coalesce(
5d9b0 29 20 6f 72 20 69 66 6e 75 6c 6c 28 29 20 66 75 ) or ifnull() fu
5d9c0 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a nction */../*.**
5d9d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
5d9e0 68 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e hree macros, FUN
5d9f0 43 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e CTION(), LIKEFUN
5da00 43 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 C() and AGGREGAT
5da10 45 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 E() are.** used
5da20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e to create the in
5da30 69 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 itializers for t
5da40 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 he FuncDef struc
5da50 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 tures..**.** F
5da60 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e UNCTION(zName, n
5da70 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 Arg, iArg, bNC,
5da80 78 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 xFunc).** Us
5da90 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 ed to create a s
5daa0 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 calar function d
5dab0 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 efinition of a f
5dac0 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a unction zName .*
5dad0 2a 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 * implemente
5dae0 64 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 d by C function
5daf0 78 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 xFunc that accep
5db00 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 ts nArg argument
5db10 73 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 s. The.** va
5db20 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 lue passed as iA
5db30 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 rg is cast to a
5db40 28 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 (void*) and made
5db50 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 available.**
5db60 20 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 as the user-da
5db70 74 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 ta (sqlite3_user
5db80 5f 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 _data()) for the
5db90 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a function. If .*
5dba0 2a 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 * argument b
5dbb0 4e 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e NC is true, then
5dbc0 20 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 the SQLITE_FUNC
5dbd0 5f 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 _NEEDCOLL flag i
5dbe0 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 s set..**.** A
5dbf0 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 GGREGATE(zName,
5dc00 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c nArg, iArg, bNC,
5dc10 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a xStep, xFinal).
5dc20 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 ** Used to c
5dc30 72 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 reate an aggrega
5dc40 74 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 te function defi
5dc50 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nition implement
5dc60 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 ed by.** the
5dc70 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 C functions xSt
5dc80 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 ep and xFinal. T
5dc90 68 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 he first four pa
5dca0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 rameters.**
5dcb0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 are interpreted
5dcc0 69 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 in the same way
5dcd0 61 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70 as the first 4 p
5dce0 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 arameters to.**
5dcf0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a FUNCTION()..
5dd00 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 **.** LIKEFUNC
5dd10 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 (zName, nArg, pA
5dd20 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 rg, flags).**
5dd30 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 Used to create
5dd40 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 a scalar functi
5dd50 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 on definition of
5dd60 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d a function zNam
5dd70 65 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 e .** that a
5dd80 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 ccepts nArg argu
5dd90 6d 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 ments and is imp
5dda0 6c 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 lemented by a ca
5ddb0 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 ll to C .**
5ddc0 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e function likeFun
5ddd0 63 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 c. Argument pArg
5dde0 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 is cast to a (v
5ddf0 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a oid *) and made.
5de00 2a 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 ** available
5de10 20 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e as the function
5de20 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 user-data (sqli
5de30 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 te3_user_data())
5de40 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e . The.** Fun
5de50 63 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 cDef.flags varia
5de60 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 ble is set to th
5de70 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 e value passed a
5de80 73 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 s the flags.**
5de90 20 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f parameter..*/
5dea0 0a 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f .#define FUNCTIO
5deb0 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 N(zName, nArg, i
5dec0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 Arg, bNC, xFunc)
5ded0 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 \. {nArg, SQLI
5dee0 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c TE_UTF8, bNC*SQL
5def0 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c ITE_FUNC_NEEDCOL
5df00 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 L, \. SQLITE_I
5df10 4e 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c NT_TO_PTR(iArg),
5df20 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 0, xFunc, 0, 0,
5df30 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 #zName, 0}.#def
5df40 69 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e ine STR_FUNCTION
5df50 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 (zName, nArg, pA
5df60 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 rg, bNC, xFunc)
5df70 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 \. {nArg, SQLIT
5df80 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 E_UTF8, bNC*SQLI
5df90 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
5dfa0 2c 20 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 , \. pArg, 0,
5dfb0 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e xFunc, 0, 0, #zN
5dfc0 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 ame, 0}.#define
5dfd0 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 LIKEFUNC(zName,
5dfe0 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 nArg, arg, flags
5dff0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c ) \. {nArg, SQL
5e000 49 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c ITE_UTF8, flags,
5e010 20 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c (void *)arg, 0,
5e020 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c likeFunc, 0, 0,
5e030 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 #zName, 0}.#def
5e040 69 6e 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e ine AGGREGATE(zN
5e050 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 ame, nArg, arg,
5e060 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 nc, xStep, xFina
5e070 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 l) \. {nArg, SQ
5e080 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 LITE_UTF8, nc*SQ
5e090 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
5e0a0 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f LL, \. SQLITE_
5e0b0 49 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c INT_TO_PTR(arg),
5e0c0 20 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 0, 0, xStep,xFi
5e0d0 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f nal,#zName,0}../
5e0e0 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 *.** All current
5e0f0 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 savepoints are
5e100 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b stored in a link
5e110 65 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 ed list starting
5e120 20 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 at.** sqlite3.p
5e130 53 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 Savepoint. The f
5e140 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 irst element in
5e150 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 the list is the
5e160 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a most recently.**
5e170 20 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e opened savepoin
5e180 74 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 t. Savepoints ar
5e190 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c e added to the l
5e1a0 69 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a ist by the vdbe.
5e1b0 2a 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 ** OP_Savepoint
5e1c0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
5e1d0 73 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 struct Savepoint
5e1e0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 {. char *zName
5e1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5e200 20 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 /* Save
5e210 70 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d point name (nul-
5e220 74 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 terminated) */.
5e230 20 69 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f i64 nDeferredCo
5e240 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ns;
5e250 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5e260 66 20 64 65 66 65 72 72 65 64 20 66 6b 20 76 69 f deferred fk vi
5e270 6f 6c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 olations */. Sa
5e280 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 vepoint *pNext;
5e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e2a0 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 /* Parent save
5e2b0 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a point (if any) *
5e2c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
5e2d0 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 following are us
5e2e0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
5e2f0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
5e300 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 lite3Savepoint()
5e310 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 ,.** and as the
5e320 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 P1 argument to t
5e330 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 he OP_Savepoint
5e340 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
5e350 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e #define SAVEPOIN
5e360 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 T_BEGIN 0.#
5e370 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 define SAVEPOINT
5e380 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 _RELEASE 1.#d
5e390 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f efine SAVEPOINT_
5e3a0 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f ROLLBACK 2.../
5e3b0 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 *.** Each SQLite
5e3c0 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c module (virtual
5e3d0 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f table definitio
5e3e0 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 n) is defined by
5e3f0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
5e400 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
5e410 20 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 structure, stor
5e420 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ed in the sqlite
5e430 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 3.aModule.** has
5e440 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 h table..*/.stru
5e450 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f ct Module {. co
5e460 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
5e470 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 le *pModule;
5e480 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 /* Callback p
5e490 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e ointers */. con
5e4a0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 st char *zName;
5e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e4c0 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 /* Name passed
5e4d0 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c to create_modul
5e4e0 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 e() */. void *p
5e4f0 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Aux;
5e500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5e510 20 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 pAux passed to
5e520 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 create_module()
5e530 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 */. void (*xDes
5e540 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 troy)(void *);
5e550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 /* Mod
5e560 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 ule destructor f
5e570 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f unction */.};../
5e580 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e *.** information
5e590 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 about each colu
5e5a0 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 mn of an SQL tab
5e5b0 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e le is held in an
5e5c0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 instance.** of
5e5d0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
5e5e0 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e */.struct Column
5e5f0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 {. char *zName
5e600 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 ; /* Name of
5e610 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a this column */.
5e620 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 Expr *pDflt;
5e630 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 /* Default va
5e640 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 lue of this colu
5e650 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 mn */. char *zD
5e660 66 6c 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 flt; /* Orig
5e670 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 inal text of the
5e680 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a default value *
5e690 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b /. char *zType;
5e6a0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 /* Data typ
5e6b0 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d e for this colum
5e6c0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f n */. char *zCo
5e6d0 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 ll; /* Colla
5e6e0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 ting sequence.
5e6f0 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 If NULL, use the
5e700 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 default */. u8
5e710 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f notNull; /
5e720 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 * True if there
5e730 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f is a NOT NULL co
5e740 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 nstraint */. u8
5e750 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f isPrimKey; /
5e760 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 * True if this c
5e770 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
5e780 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
5e790 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
5e7a0 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 ity; /* One of
5e7b0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
5e7c0 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 ... values */.#i
5e7d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
5e7e0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
5e7f0 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 u8 isHidden;
5e800 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
5e810 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 s column is 'hid
5e820 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d den' */.#endif.}
5e830 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c ;../*.** A "Coll
5e840 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 ating Sequence"
5e850 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e is defined by an
5e860 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
5e870 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
5e880 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 ructure. Concept
5e890 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 ually, a collati
5e8a0 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 ng sequence cons
5e8b0 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 ists of a name a
5e8c0 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 nd.** a comparis
5e8d0 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 on routine that
5e8e0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 defines the orde
5e8f0 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e r of that sequen
5e900 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ce..**.** There
5e910 6d 61 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 may two separate
5e920 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
5e930 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f of the collatio
5e940 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a n function, one.
5e950 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 ** that processe
5e960 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 s text in UTF-8
5e970 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 encoding (CollSe
5e980 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 q.xCmp) and anot
5e990 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 her that.** proc
5e9a0 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 esses text encod
5e9b0 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f ed in UTF-16 (Co
5e9c0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 llSeq.xCmp16), u
5e9d0 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 sing the machine
5e9e0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 .** native byte
5e9f0 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f order. When a co
5ea00 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
5ea10 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c is invoked, SQL
5ea20 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 ite selects.** t
5ea30 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 he version that
5ea40 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 will require the
5ea50 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 least expensive
5ea60 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 encoding.** tra
5ea70 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e nslations, if an
5ea80 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c y..**.** The Col
5ea90 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 lSeq.pUser membe
5eaa0 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e r variable is an
5eab0 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 extra parameter
5eac0 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a that passed in.
5ead0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
5eae0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
5eaf0 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e UTF-8 comparison
5eb00 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e function, xCmp.
5eb10 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 .** CollSeq.pUse
5eb20 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76 r16 is the equiv
5eb30 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 alent for the UT
5eb40 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 F-16 comparison
5eb50 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d function,.** xCm
5eb60 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f p16..**.** If bo
5eb70 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 th CollSeq.xCmp
5eb80 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 and CollSeq.xCmp
5eb90 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 16 are NULL, it
5eba0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
5ebb0 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * collating sequ
5ebc0 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 ence is undefine
5ebd0 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c d. Indices buil
5ebe0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 t on an undefine
5ebf0 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 d.** collating s
5ec00 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 equence may not
5ec10 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
5ec20 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f en..*/.struct Co
5ec30 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a llSeq {. char *
5ec40 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
5ec50 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
5ec60 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5ec70 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e, UTF-8 encoded
5ec80 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
5ec90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
5eca0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e ext encoding han
5ecb0 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a dled by xCmp() *
5ecc0 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 /. u8 type;
5ecd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
5ece0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 of the SQLITE_C
5ecf0 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 OLL_... values b
5ed00 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a elow */. void *
5ed10 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 pUser;
5ed20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e /* First argumen
5ed30 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 t to xCmp() */.
5ed40 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 int (*xCmp)(voi
5ed50 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f d*,int, const vo
5ed60 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 id*, int, const
5ed70 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 void*);. void (
5ed80 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 *xDel)(void*);
5ed90 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
5eda0 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f r pUser */.};../
5edb0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
5edc0 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 ues of CollSeq.t
5edd0 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ype:.*/.#define
5ede0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 SQLITE_COLL_BINA
5edf0 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 RY 1 /* The de
5ee00 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 fault memcmp() c
5ee10 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5ee20 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
5ee30 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 ITE_COLL_NOCASE
5ee40 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2 /* The built
5ee50 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 -in NOCASE colla
5ee60 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
5ee70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5ee80 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 COLL_REVERSE 3
5ee90 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 /* The built-in
5eea0 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e REVERSE collatin
5eeb0 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 g sequence */.#d
5eec0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
5eed0 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 L_USER 0 /*
5eee0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 Any other user-d
5eef0 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 efined collating
5ef00 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a sequence */../*
5ef10 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 .** A sort order
5ef20 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 can be either A
5ef30 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 SC or DESC..*/.#
5ef40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f define SQLITE_SO
5ef50 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a _ASC 0 /*
5ef60 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 Sort in ascendi
5ef70 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 ng order */.#def
5ef80 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 ine SQLITE_SO_DE
5ef90 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f SC 1 /* So
5efa0 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 rt in ascending
5efb0 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 order */../*.**
5efc0 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 Column affinity
5efd0 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 types..**.** The
5efe0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 se used to have
5eff0 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 mnemonic name li
5f000 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 ke 'i' for SQLIT
5f010 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e E_AFF_INTEGER an
5f020 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c d.** 't' for SQL
5f030 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 ITE_AFF_TEXT. B
5f040 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 ut we can save a
5f050 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e little space an
5f060 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 d improve.** the
5f070 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 speed a little
5f080 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 by numbering the
5f090 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 values consecut
5f0a0 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 ively. .**.** B
5f0b0 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 ut rather than s
5f0c0 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 tart with 0 or 1
5f0d0 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 , we begin with
5f0e0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 'a'. That way,.
5f0f0 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 ** when multiple
5f100 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 affinity types
5f110 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 are concatenated
5f120 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 into a string a
5f130 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 nd.** used as th
5f140 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 e P4 operand, th
5f150 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 ey will be more
5f160 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 readable..**.**
5f170 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 Note also that t
5f180 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 he numeric types
5f190 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 are grouped tog
5f1a0 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 ether so that te
5f1b0 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e sting.** for a n
5f1c0 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61 umeric type is a
5f1d0 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 single comparis
5f1e0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 on..*/.#define S
5f1f0 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 QLITE_AFF_TEXT
5f200 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 'a'.#define S
5f210 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 QLITE_AFF_NONE
5f220 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 'b'.#define S
5f230 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
5f240 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 C 'c'.#define S
5f250 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
5f260 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 R 'd'.#define S
5f270 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 QLITE_AFF_REAL
5f280 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 'e'..#define
5f290 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 sqlite3IsNumeric
5f2a0 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 Affinity(X) ((X
5f2b0 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 )>=SQLITE_AFF_NU
5f2c0 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 MERIC)../*.** Th
5f2d0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 e SQLITE_AFF_MAS
5f2e0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f K values masks o
5f2f0 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 ff the significa
5f300 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a nt bits of an.**
5f310 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e affinity value.
5f320 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c .*/.#define SQL
5f330 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 ITE_AFF_MASK
5f340 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 0x67../*.** Add
5f350 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 itional bit valu
5f360 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f es that can be O
5f370 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 Red with an affi
5f380 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 nity without.**
5f390 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 changing the aff
5f3a0 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e inity..*/.#defin
5f3b0 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e e SQLITE_JUMPIFN
5f3c0 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a ULL 0x08 /* j
5f3d0 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f umps if either o
5f3e0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a perand is NULL *
5f3f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5f400 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 _STOREP2 0x
5f410 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 10 /* Store res
5f420 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 ult in reg[P2] r
5f430 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 ather than jump
5f440 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5f450 45 5f 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30 E_NULLEQ 0
5f460 78 38 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c x80 /* NULL=NUL
5f470 4c 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f L */../*.** An o
5f480 62 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 bject of this ty
5f490 70 65 20 69 73 20 63 72 65 61 74 65 64 20 66 6f pe is created fo
5f4a0 72 20 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 r each virtual t
5f4b0 61 62 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a able present in.
5f4c0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
5f4d0 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 schema. .**.** I
5f4e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 f the database s
5f4f0 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c chema is shared,
5f500 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f then there is o
5f510 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ne instance of t
5f520 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 his.** structure
5f530 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 for each databa
5f540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 se connection (s
5f550 71 6c 69 74 65 33 2a 29 20 74 68 61 74 20 75 73 qlite3*) that us
5f560 65 73 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a es the shared.**
5f570 20 73 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73 schema. This is
5f580 20 62 65 63 61 75 73 65 20 65 61 63 68 20 64 61 because each da
5f590 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
5f5a0 6e 20 72 65 71 75 69 72 65 73 20 69 74 73 20 6f n requires its o
5f5b0 77 6e 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 wn unique.** ins
5f5c0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c tance of the sql
5f5d0 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c ite3_vtab* handl
5f5e0 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 e used to access
5f5f0 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
5f600 6c 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 le .** implement
5f610 61 74 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 ation. sqlite3_v
5f620 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e tab* handles can
5f630 20 6e 6f 74 20 62 65 20 73 68 61 72 65 64 20 62 not be shared b
5f640 65 74 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 etween .** datab
5f650 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c ase connections,
5f660 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72 even when the r
5f670 65 73 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 est of the in-me
5f680 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a mory database .*
5f690 2a 20 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 * schema is shar
5f6a0 65 64 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65 ed, as the imple
5f6b0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 mentation often
5f6c0 73 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 62 stores the datab
5f6d0 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
5f6e0 6e 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 n handle passed
5f6f0 74 6f 20 69 74 20 76 69 61 20 74 68 65 20 78 43 to it via the xC
5f700 6f 6e 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65 onnect() or xCre
5f710 61 74 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 ate() method.**
5f720 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
5f730 61 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 ation internally
5f740 2e 20 54 68 69 73 20 64 61 74 61 62 61 73 65 20 . This database
5f750 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
5f760 65 20 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 e may.** then us
5f770 65 64 20 62 79 20 74 68 65 20 76 69 72 74 75 61 ed by the virtua
5f780 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e l table implemen
5f790 74 61 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 tation to access
5f7a0 20 72 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a real tables .**
5f7b0 20 77 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 within the data
5f7c0 62 61 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 base. So that th
5f7d0 65 79 20 61 70 70 65 61 72 20 61 73 20 70 61 72 ey appear as par
5f7e0 74 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 t of the callers
5f7f0 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
5f800 2c 20 74 68 65 73 65 20 61 63 63 65 73 73 65 73 , these accesses
5f810 20 6e 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 need to be made
5f820 20 76 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 via the same da
5f830 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 tabase .** conne
5f840 63 74 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73 ction as that us
5f850 65 64 20 74 6f 20 65 78 65 63 75 74 65 20 53 51 ed to execute SQ
5f860 4c 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 L operations on
5f870 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
5f880 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 e..**.** All VTa
5f890 62 6c 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 ble objects that
5f8a0 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 correspond to a
5f8b0 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e single table in
5f8c0 20 61 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 a shared.** dat
5f8d0 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 72 65 abase schema are
5f8e0 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 initially store
5f8f0 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 d in a linked-li
5f900 73 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 st pointed to by
5f910 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56 .** the Table.pV
5f920 54 61 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 Table member var
5f930 69 61 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 iable of the cor
5f940 72 65 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 responding Table
5f950 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e object..** When
5f960 20 61 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 an sqlite3_prep
5f970 61 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 are() operation
5f980 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 is required to a
5f990 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 ccess the virtua
5f9a0 6c 0a 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 l.** table, it s
5f9b0 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 earches the list
5f9c0 20 66 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20 for the VTable
5f9d0 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 that corresponds
5f9e0 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 to the.** datab
5f9f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 ase connection d
5fa00 6f 69 6e 67 20 74 68 65 20 70 72 65 70 61 72 69 oing the prepari
5fa10 6e 67 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20 ng so as to use
5fa20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 the correct.** s
5fa30 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e qlite3_vtab* han
5fa40 64 6c 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 dle in the compi
5fa50 6c 65 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a led query..**.**
5fa60 20 57 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f When an in-memo
5fa70 72 79 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 ry Table object
5fa80 69 73 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20 is deleted (for
5fa90 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 example when the
5faa0 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65 .** schema is be
5fab0 69 6e 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 ing reloaded for
5fac0 20 73 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 some reason), t
5fad0 68 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 he VTable object
5fae0 73 20 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 s are not .** de
5faf0 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 leted and the sq
5fb00 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 lite3_vtab* hand
5fb10 6c 65 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73 les are not xDis
5fb20 63 6f 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 connect()ed .**
5fb30 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 immediately. Ins
5fb40 74 65 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d tead, they are m
5fb50 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 oved from the Ta
5fb60 62 6c 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 ble.pVTable list
5fb70 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c to.** another l
5fb80 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 inked list heade
5fb90 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
5fba0 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d .pDisconnect mem
5fbb0 62 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ber of the.** co
5fbc0 72 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 rresponding sqli
5fbd0 74 65 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 te3 structure. T
5fbe0 68 65 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c hey are then del
5fbf0 65 74 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 eted/xDisconnect
5fc00 65 64 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 ed .** next time
5fc10 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 a statement is
5fc20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 prepared using s
5fc30 61 69 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 aid sqlite3*. Th
5fc40 69 73 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f is is done.** to
5fc50 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 avoid deadlock
5fc60 69 73 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 issues involving
5fc70 20 6d 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 multiple sqlite
5fc80 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e 3.mutex mutexes.
5fc90 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d .** Refer to com
5fca0 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 ments above func
5fcb0 74 69 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 tion sqlite3Vtab
5fcc0 55 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 UnlockList() for
5fcd0 20 61 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 an.** explanati
5fce0 6f 6e 20 61 73 20 74 6f 20 77 68 79 20 69 74 20 on as to why it
5fcf0 69 73 20 73 61 66 65 20 74 6f 20 61 64 64 20 61 is safe to add a
5fd00 6e 20 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 n entry to an sq
5fd10 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 lite3.pDisconnec
5fd20 74 0a 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 t.** list withou
5fd30 74 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f t holding the co
5fd40 72 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 rresponding sqli
5fd50 74 65 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e te3.mutex mutex.
5fd60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 .**.** The memor
5fd70 79 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 y for objects of
5fd80 20 74 68 69 73 20 74 79 70 65 20 69 73 20 61 6c this type is al
5fd90 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 ways allocated b
5fda0 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d y .** sqlite3DbM
5fdb0 61 6c 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 alloc(), using t
5fdc0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 he connection ha
5fdd0 6e 64 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56 ndle stored in V
5fde0 54 61 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 Table.db as .**
5fdf0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
5fe00 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 nt..*/.struct VT
5fe10 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 able {. sqlite3
5fe20 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 *db;
5fe30 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 /* Database c
5fe40 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 onnection associ
5fe50 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 ated with this t
5fe60 61 62 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 able */. Module
5fe70 20 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 *pMod;
5fe80 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
5fe90 6f 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 o module impleme
5fea0 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c ntation */. sql
5feb0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
5fec0 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 ; /* Pointe
5fed0 72 20 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e r to vtab instan
5fee0 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 ce */. int nRef
5fef0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5ff00 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
5ff10 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 ointers to this
5ff20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 structure */. V
5ff30 54 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 Table *pNext;
5ff40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
5ff50 20 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 in linked list
5ff60 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d (see above) */.}
5ff70 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 ;../*.** Each SQ
5ff80 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 L table is repre
5ff90 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 sented in memory
5ffa0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
5ffb0 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 of the.** follow
5ffc0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
5ffd0 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 *.** Table.zName
5ffe0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
5fff0 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 the table. The
60000 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 case of the orig
60010 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 inal.** CREATE T
60020 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 ABLE statement i
60030 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 s stored, but ca
60040 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 se is not signif
60050 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d icant for.** com
60060 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 parisons..**.**
60070 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 Table.nCol is th
60080 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
60090 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c mns in this tabl
600a0 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 e. Table.aCol i
600b0 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 s a.** pointer t
600c0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f o an array of Co
600d0 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c lumn structures,
600e0 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f one for each co
600f0 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lumn..**.** If t
60100 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 he table has an
60110 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
60120 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e KEY, then Table.
60130 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 iPKey is the ind
60140 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c ex of.** the col
60150 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 umn that is that
60160 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 key. Otherwis
60170 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 e Table.iPKey is
60180 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 negative. Note
60190 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 .** that the dat
601a0 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 atype of the PRI
601b0 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 MARY KEY must be
601c0 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 INTEGER for thi
601d0 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 s field to.** be
601e0 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 set. An INTEGE
601f0 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 R PRIMARY KEY is
60200 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 used as the row
60210 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 id for each row
60220 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e of.** the table.
60230 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 If a table has
60240 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d no INTEGER PRIM
60250 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 ARY KEY, then a
60260 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 random rowid.**
60270 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 is generated for
60280 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 each row of the
60290 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 table. TF_HasP
602a0 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 rimaryKey is set
602b0 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 if.** the table
602c0 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 has any PRIMARY
602d0 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 KEY, INTEGER or
602e0 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a otherwise..**.*
602f0 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 * Table.tnum is
60300 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
60310 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 for the root BTr
60320 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 ee page of the t
60330 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 able in the.** d
60340 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 atabase file. I
60350 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 f Table.iDb is t
60360 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
60370 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 database table b
60380 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c ackend.** in sql
60390 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 ite.aDb[]. 0 is
603a0 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 for the main da
603b0 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 tabase and 1 is
603c0 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 for the file tha
603d0 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f t.** holds tempo
603e0 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 rary tables and
603f0 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f indices. If TF_
60400 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 Ephemeral is set
60410 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 .** then the tab
60420 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 le is stored in
60430 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 a file that is a
60440 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c utomatically del
60450 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 eted.** when the
60460 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 VDBE cursor to
60470 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f the table is clo
60480 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 sed. In this ca
60490 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a se Table.tnum .*
604a0 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75 * refers VDBE cu
604b0 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 rsor number that
604c0 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 holds the table
604d0 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 open, not to th
604e0 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e e root.** page n
604f0 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e umber. Transien
60500 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 t tables are use
60510 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 d to hold the re
60520 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 sults of a.** su
60530 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70 b-query that app
60540 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 ears instead of
60550 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d a real table nam
60560 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c e in the FROM cl
60570 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 ause .** of a SE
60580 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
60590 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 */.struct Table
605a0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d {. sqlite3 *dbM
605b0 65 6d 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63 em; /* DB c
605c0 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66 onnection used f
605d0 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c or lookaside all
605e0 6f 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 ocations. */. c
605f0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
60600 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
60610 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 he table or view
60620 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b */. int iPKey;
60630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
60640 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75 not negative, u
60650 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 se aCol[iPKey] a
60660 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 s the primary ke
60670 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b y */. int nCol;
60680 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
60690 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
606a0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a in this table *
606b0 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c /. Column *aCol
606c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f ; /* Info
606d0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 rmation about ea
606e0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 ch column */. I
606f0 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 ndex *pIndex;
60700 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 /* List of S
60710 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 QL indexes on th
60720 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 is table. */. i
60730 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 nt tnum;
60740 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 /* Root BTre
60750 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20 e node for this
60760 74 61 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 table (see note
60770 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 above) */. Sele
60780 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 ct *pSelect;
60790 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 /* NULL for tab
607a0 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 les. Points to
607b0 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 definition if a
607c0 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e view. */. u16 n
607d0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
607e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
607f0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 54 61 nters to this Ta
60800 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 ble */. u8 tabF
60810 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a lags; /*
60820 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 Mask of TF_* va
60830 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 lues */. u8 key
60840 43 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f Conf; /
60850 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 * What to do in
60860 63 61 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 case of uniquene
60870 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 ss conflict on i
60880 50 4b 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a PKey */. FKey *
60890 70 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f pFKey; /
608a0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 * Linked list of
608b0 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 all foreign key
608c0 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 s in this table
608d0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 */. char *zColA
608e0 66 66 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72 ff; /* Str
608f0 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 ing defining the
60900 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 affinity of eac
60910 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e h column */.#ifn
60920 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
60930 43 48 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 CHECK. Expr *pC
60940 68 65 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 heck; /*
60950 54 68 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 The AND of all C
60960 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 HECK constraints
60970 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 */.#endif.#ifnd
60980 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
60990 4c 54 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 LTERTABLE. int
609a0 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 addColOffset;
609b0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52 /* Offset in CR
609c0 45 41 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20 EATE TABLE stmt
609d0 74 6f 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c to add a new col
609e0 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 umn */.#endif.#i
609f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
60a00 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
60a10 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 VTable *pVTable
60a20 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ; /* List of
60a30 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e VTable objects.
60a40 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c */. int nModul
60a50 65 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 eArg; /* Nu
60a60 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
60a70 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 s to the module
60a80 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f */. char **azMo
60a90 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 duleArg; /* Tex
60aa0 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 t of all module
60ab0 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 args. [0] is mod
60ac0 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 ule name */.#end
60ad0 69 66 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 if. Trigger *pT
60ae0 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 rigger; /* Lis
60af0 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 73 74 t of triggers st
60b00 6f 72 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20 ored in pSchema
60b10 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 */. Schema *pSc
60b20 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 hema; /* Sch
60b30 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ema that contain
60b40 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a s this table */.
60b50 20 20 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f Table *pNextZo
60b60 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f mbie; /* Next o
60b70 6e 20 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d n the Parse.pZom
60b80 62 69 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d bieTab list */.}
60b90 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
60ba0 20 76 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65 values for Tabe
60bb0 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 .tabFlags..*/.#d
60bc0 65 66 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c efine TF_Readonl
60bd0 79 20 20 20 20 20 20 20 20 30 78 30 31 20 20 20 y 0x01
60be0 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 /* Read-only sy
60bf0 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 stem table */.#d
60c00 65 66 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 efine TF_Ephemer
60c10 61 6c 20 20 20 20 20 20 20 30 78 30 32 20 20 20 al 0x02
60c20 20 2f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c /* An ephemeral
60c30 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e table */.#defin
60c40 65 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b e TF_HasPrimaryK
60c50 65 79 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 ey 0x04 /*
60c60 54 61 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d Table has a prim
60c70 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 ary key */.#defi
60c80 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d ne TF_Autoincrem
60c90 65 6e 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a ent 0x08 /*
60ca0 20 49 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 Integer primary
60cb0 20 6b 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72 key is autoincr
60cc0 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 ement */.#define
60cd0 20 54 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 TF_Virtual
60ce0 20 20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 0x10 /* I
60cf0 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c s a virtual tabl
60d00 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f e */.#define TF_
60d10 4e 65 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 NeedMetadata
60d20 30 78 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 0x20 /* aCol[
60d30 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c ].zType and aCol
60d40 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 [].pColl missing
60d50 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 */..../*.** Tes
60d60 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 t to see whether
60d70 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 or not a table
60d80 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 is a virtual tab
60d90 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 le. This is.**
60da0 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20 done as a macro
60db0 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 so that it will
60dc0 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 be optimized out
60dd0 20 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a when virtual.**
60de0 20 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69 table support i
60df0 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 s omitted from t
60e00 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 he build..*/.#if
60e10 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
60e20 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 _VIRTUALTABLE.#
60e30 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 define IsVirtua
60e40 6c 28 58 29 20 20 20 20 20 20 28 28 28 58 29 2d l(X) (((X)-
60e50 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 >tabFlags & TF_V
60e60 69 72 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 irtual)!=0).# d
60e70 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f efine IsHiddenCo
60e80 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 lumn(X) ((X)->is
60e90 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 Hidden).#else.#
60ea0 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 define IsVirtua
60eb0 6c 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 l(X) 0.# d
60ec0 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f efine IsHiddenCo
60ed0 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 lumn(X) 0.#endif
60ee0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 ../*.** Each for
60ef0 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 eign key constra
60f00 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e int is an instan
60f10 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
60f20 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
60f30 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b *.** A foreign k
60f40 65 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 ey is associated
60f50 20 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 with two tables
60f60 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 . The "from" ta
60f70 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 ble is.** the ta
60f80 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ble that contain
60f90 73 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 s the REFERENCES
60fa0 20 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 clause that cre
60fb0 61 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e ates the foreign
60fc0 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 .** key. The "t
60fd0 6f 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 o" table is the
60fe0 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 table that is na
60ff0 6d 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 med in the REFER
61000 45 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a ENCES clause..**
61010 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 Consider this e
61020 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 xample:.**.**
61030 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 CREATE TABLE e
61040 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 x1(.** a I
61050 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
61060 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 EY,.** b I
61070 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e NTEGER CONSTRAIN
61080 54 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 T fk1 REFERENCES
61090 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 ex2(x).** )
610a0 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 ;.**.** For fore
610b0 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 ign key "fk1", t
610c0 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 he from-table is
610d0 20 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 "ex1" and the t
610e0 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 o-table is "ex2"
610f0 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 ..**.** Each REF
61100 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 ERENCES clause g
61110 65 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 enerates an inst
61120 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
61130 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a owing structure.
61140 2a 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61 ** which is atta
61150 63 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d ched to the from
61160 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d -table. The to-
61170 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 table need not e
61180 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 xist when.** the
61190 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 from-table is c
611a0 72 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 reated. The exi
611b0 73 74 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f stence of the to
611c0 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 -table is not ch
611d0 65 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 ecked..*/.struct
611e0 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 FKey {. Table
611f0 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 *pFrom; /* T
61200 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 able containing
61210 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 the REFERENCES c
61220 6c 61 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c lause (aka: Chil
61230 64 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e d) */. FKey *pN
61240 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 extFrom; /* Nex
61250 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e t foreign key in
61260 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 pFrom */. char
61270 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a *zTo; /*
61280 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 Name of table t
61290 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e hat the key poin
612a0 74 73 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 ts to (aka: Pare
612b0 6e 74 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 nt) */. FKey *p
612c0 4e 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 NextTo; /* Ne
612d0 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f xt foreign key o
612e0 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 n table named zT
612f0 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 o */. FKey *pPr
61300 65 76 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 evTo; /* Prev
61310 69 6f 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 ious foreign key
61320 20 6f 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 on table named
61330 7a 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f zTo */. int nCo
61340 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 l; /* Nu
61350 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
61360 69 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 in this key */.
61370 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d /* EV: R-30323-
61380 32 31 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 21917 */. u8 is
61390 44 65 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 Deferred; /*
613a0 54 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 True if constrai
613b0 6e 74 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 nt checking is d
613c0 65 66 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d eferred till COM
613d0 4d 49 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 MIT */. u8 aAct
613e0 69 6f 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 ion[2];
613f0 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e /* ON DELETE an
61400 64 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 d ON UPDATE acti
61410 6f 6e 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c ons, respectivel
61420 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a y */. Trigger *
61430 61 70 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f apTrigger[2]; /
61440 2a 20 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 * Triggers for a
61450 41 63 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 Action[] actions
61460 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f */. struct sCo
61470 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 lMap { /* Mappi
61480 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e ng of columns in
61490 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e pFrom to column
614a0 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 s in zTo */.
614b0 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 int iFrom;
614c0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
614d0 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a olumn in pFrom *
614e0 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c /. char *zCol
614f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
61500 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 of column in zT
61510 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49 o. If 0 use PRI
61520 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 MARY KEY */. }
61530 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 aCol[1];
61540 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 /* One entry for
61550 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f each of nCol co
61560 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a lumn s */.};../*
61570 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f .** SQLite suppo
61580 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 rts many differe
61590 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c nt ways to resol
615a0 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a ve a constraint.
615b0 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 ** error. ROLLB
615c0 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d ACK processing m
615d0 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 eans that a cons
615e0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
615f0 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f .** causes the o
61600 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 peration in proc
61610 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 ess to fail and
61620 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
61630 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 transaction.** t
61640 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
61650 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 . ABORT process
61660 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 ing means the op
61670 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 eration in proce
61680 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 ss.** fails and
61690 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 any prior change
616a0 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 s from that one
616b0 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 operation are ba
616c0 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 cked out,.** but
616d0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
616e0 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 is not rolled b
616f0 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 ack. FAIL proce
61700 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 ssing means that
61710 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f .** the operatio
61720 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 n in progress st
61730 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ops and returns
61740 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 an error code.
61750 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 But prior.** cha
61760 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 nges due to the
61770 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 same operation a
61780 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 re not backed ou
61790 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 t and no rollbac
617a0 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 k.** occurs. IG
617b0 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 NORE means that
617c0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 the particular r
617d0 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 ow that caused t
617e0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a he constraint.**
617f0 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e error is not in
61800 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 serted or update
61810 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 d. Processing c
61820 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 ontinues and no
61830 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 error.** is retu
61840 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d rned. REPLACE m
61850 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 eans that preexi
61860 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 sting database r
61870 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a ows that caused.
61880 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 ** a UNIQUE cons
61890 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
618a0 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 are removed so
618b0 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 that the new ins
618c0 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 ert or.** update
618d0 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 can proceed. P
618e0 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e rocessing contin
618f0 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 ues and no error
61900 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a is reported..**
61910 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 .** RESTRICT, SE
61920 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 TNULL, and CASCA
61930 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 DE actions apply
61940 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e only to foreign
61950 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 keys..** RESTRI
61960 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 CT is the same a
61970 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 s ABORT for IMME
61980 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 DIATE foreign ke
61990 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 ys and the.** sa
619a0 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 me as ROLLBACK f
619b0 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 or DEFERRED keys
619c0 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 . SETNULL means
619d0 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 that the foreig
619e0 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 n.** key is set
619f0 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 to NULL. CASCAD
61a00 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 E means that a D
61a10 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 ELETE or UPDATE
61a20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 of the.** refere
61a30 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 nced table row i
61a40 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 s propagated int
61a50 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 o the row that h
61a60 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 olds the.** fore
61a70 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 ign key..** .**
61a80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 The following sy
61a90 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 mbolic values ar
61aa0 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 e used to record
61ab0 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f which type.** o
61ac0 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 f action to take
61ad0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f ..*/.#define OE_
61ae0 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 None 0 /*
61af0 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 There is no cons
61b00 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 traint to check
61b10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f */.#define OE_Ro
61b20 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 llback 1 /* Fa
61b30 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e il the operation
61b40 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 and rollback th
61b50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f e transaction */
61b60 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 .#define OE_Abor
61b70 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b t 2 /* Back
61b80 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 out changes but
61b90 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 do no rollback
61ba0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
61bb0 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 define OE_Fail
61bc0 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 3 /* Stop t
61bd0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 he operation but
61be0 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 leave all prior
61bf0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 changes */.#def
61c00 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 ine OE_Ignore
61c10 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 4 /* Ignore th
61c20 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 e error. Do not
61c30 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 do the INSERT or
61c40 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 UPDATE */.#defi
61c50 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 ne OE_Replace 5
61c60 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 /* Delete exi
61c70 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 sting record, th
61c80 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 en do INSERT or
61c90 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 UPDATE */..#defi
61ca0 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 ne OE_Restrict 6
61cb0 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 /* OE_Abort f
61cc0 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 or IMMEDIATE, OE
61cd0 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 _Rollback for DE
61ce0 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e FERRED */.#defin
61cf0 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 e OE_SetNull 7
61d00 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 /* Set the for
61d10 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 eign key value t
61d20 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e o NULL */.#defin
61d30 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 e OE_SetDflt 8
61d40 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 /* Set the for
61d50 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 eign key value t
61d60 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f o its default */
61d70 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 .#define OE_Casc
61d80 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 ade 9 /* Casc
61d90 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 ade the changes
61da0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 */..#define OE_D
61db0 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 efault 99 /* D
61dc0 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 o whatever the d
61dd0 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 efault action is
61de0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 */.../*.** An i
61df0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
61e00 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
61e10 72 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 re is passed as
61e20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 the first.** arg
61e30 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
61e40 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 VdbeKeyCompare a
61e50 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f nd is used to co
61e60 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f ntrol the .** co
61e70 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 mparison of the
61e80 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a two index keys..
61e90 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 */.struct KeyInf
61ea0 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 o {. sqlite3 *d
61eb0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
61ec0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
61ed0 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 tion */. u8 enc
61ee0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
61ef0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d Text encoding -
61f00 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 one of the TEXT
61f10 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a _Utf* values */.
61f20 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 u16 nField;
61f30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
61f40 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 of entries in aC
61f50 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 oll[] */. u8 *a
61f60 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f SortOrder; /
61f70 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 * If defined an
61f80 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 aSortOrder[i] is
61f90 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 true, sort DESC
61fa0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 */. CollSeq *a
61fb0 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c Coll[1]; /* Col
61fc0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
61fd0 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 for each term of
61fe0 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a the key */.};..
61ff0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
62000 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
62010 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c ng structure hol
62020 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ds information a
62030 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 bout a.** single
62040 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 index record th
62050 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 at has already b
62060 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 een parsed out i
62070 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a nto individual.*
62080 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 * values..**.**
62090 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f A record is an o
620a0 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 bject that conta
620b0 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ins one or more
620c0 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a fields of data..
620d0 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 ** Records are u
620e0 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
620f0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 content of a ta
62100 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 ble row and to s
62110 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 tore.** the key
62120 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 of an index. A
62130 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 blob encoding of
62140 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 a record is cre
62150 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f ated by.** the O
62160 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 P_MakeRecord opc
62170 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 ode of the VDBE
62180 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 and is disassemb
62190 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 led by the.** OP
621a0 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a _Column opcode..
621b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 **.** This struc
621c0 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 ture holds a rec
621d0 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 ord that has alr
621e0 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 eady been disass
621f0 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 embled.** into i
62200 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 ts constituent f
62210 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 ields..*/.struct
62220 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
62230 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 {. KeyInfo *pKe
62240 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 yInfo; /* Colla
62250 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 tion and sort-or
62260 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 der information
62270 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b */. u16 nField;
62280 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
62290 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
622a0 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 apMem[] */. u1
622b0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
622c0 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 /* Boolean set
622d0 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 tings. UNPACKED
622e0 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 _... below */.
622f0 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 i64 rowid;
62300 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 /* Used by U
62310 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 NPACKED_PREFIX_S
62320 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a EARCH */. Mem *
62330 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f aMem; /
62340 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a * Values */.};..
62350 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
62360 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 lues of Unpacked
62370 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a Record.flags.*/.
62380 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
62390 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 _NEED_FREE 0
623a0 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 x0001 /* Memory
623b0 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 is from sqlite3
623c0 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 Malloc() */.#def
623d0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 ine UNPACKED_NEE
623e0 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 D_DESTROY 0x000
623f0 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 2 /* apMem[]s s
62400 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 hould all be des
62410 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e troyed */.#defin
62420 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 e UNPACKED_IGNOR
62430 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 E_ROWID 0x0004
62440 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c /* Ignore trail
62450 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 ing rowid on key
62460 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 1 */.#define UNP
62470 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 ACKED_INCRKEY
62480 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 0x0008 /* M
62490 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 ake this key an
624a0 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a epsilon larger *
624b0 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b /.#define UNPACK
624c0 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 ED_PREFIX_MATCH
624d0 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 0x0010 /* A pr
624e0 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f efix match is co
624f0 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 nsidered OK */.#
62500 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
62510 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 PREFIX_SEARCH 0x
62520 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 0020 /* A prefi
62530 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 x match is consi
62540 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a dered OK */../*.
62550 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 ** Each SQL inde
62560 78 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 x is represented
62570 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e in memory by an
62580 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
62590 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
625a0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
625b0 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 he columns of th
625c0 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 e table that are
625d0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 to be indexed a
625e0 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 re described.**
625f0 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b by the aiColumn[
62600 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 ] field of this
62610 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 structure. For
62620 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 example, suppose
62630 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 .** we have the
62640 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 following table
62650 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a and index:.**.**
62660 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
62670 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 E Ex1(c1 int, c2
62680 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a int, c3 text);.
62690 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e ** CREATE IN
626a0 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 DEX Ex2 ON Ex1(c
626b0 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 3,c1);.**.** In
626c0 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 the Table struct
626d0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 ure describing E
626e0 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 x1, nCol==3 beca
626f0 75 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a use there are.**
62700 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 three columns i
62710 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e n the table. In
62720 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 the Index struc
62730 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a ture describing.
62740 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d ** Ex2, nColumn=
62750 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 =2 since 2 of th
62760 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 e 3 columns of E
62770 78 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a x1 are indexed..
62780 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
62790 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 aiColumn is {2,
627a0 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 0}. aiColumn[0]
627b0 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20 ==2 because the
627c0 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e .** first column
627d0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 to be indexed (
627e0 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 c3) has an index
627f0 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f of 2 in Ex1.aCo
62800 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f l[]..** The seco
62810 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 nd column to be
62820 69 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73 indexed (c1) has
62830 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 an index of 0 i
62840 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c n.** Ex1.aCol[],
62850 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c hence Ex2.aiCol
62860 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a umn[1]==0..**.**
62870 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 The Index.onErr
62880 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 or field determi
62890 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
628a0 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 ot the indexed c
628b0 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 olumns.** must b
628c0 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 e unique and wha
628d0 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 t to do if they
628e0 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 are not. When I
628f0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f ndex.onError=OE_
62900 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e None,.** it mean
62910 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 s this is not a
62920 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f unique index. O
62930 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61 therwise it is a
62940 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a unique index.**
62950 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
62960 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 f Index.onError
62970 69 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69 indicate the whi
62980 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f ch conflict reso
62990 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 lution .** algor
629a0 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 ithm to employ w
629b0 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d henever an attem
629c0 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e pt is made to in
629d0 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 sert a non-uniqu
629e0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f e.** element..*/
629f0 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a .struct Index {.
62a00 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
62a10 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
62a20 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e is index */. in
62a30 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f t nColumn; /
62a40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
62a50 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 mns in the table
62a60 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e used by this in
62a70 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 dex */. int *ai
62a80 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 Column; /* Whi
62a90 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 ch columns are u
62aa0 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 sed by this inde
62ab0 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a x. 1st is 0 */.
62ac0 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f unsigned *aiRo
62ad0 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 wEst; /* Result
62ae0 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e of ANALYZE: Est.
62af0 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 rows selected b
62b00 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f y each column */
62b10 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 . Table *pTable
62b20 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 ; /* The SQL t
62b30 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 able being index
62b40 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d ed */. int tnum
62b50 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 ; /* Page
62b60 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 containing root
62b70 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 of this index i
62b80 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
62b90 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b */. u8 onError;
62ba0 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 /* OE_Abor
62bb0 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 t, OE_Ignore, OE
62bc0 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f _Replace, or OE_
62bd0 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 None */. u8 aut
62be0 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 oIndex; /* Tr
62bf0 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 ue if is automat
62c00 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 ically created (
62c10 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a ex: by UNIQUE) *
62c20 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 /. char *zColAf
62c30 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 f; /* String d
62c40 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 efining the affi
62c50 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c nity of each col
62c60 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a umn */. Index *
62c70 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 pNext; /* The
62c80 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f next index asso
62c90 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
62ca0 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 same table */.
62cb0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b Schema *pSchema;
62cc0 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 /* Schema conta
62cd0 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 ining this index
62ce0 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f */. u8 *aSortO
62cf0 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 rder; /* Array
62d00 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 of size Index.nC
62d10 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 olumn. True==DES
62d20 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f C, False==ASC */
62d30 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c . char **azColl
62d40 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 ; /* Array of
62d50 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
62d60 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 ce names for ind
62d70 65 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d ex */. IndexSam
62d80 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 ple *aSample;
62d90 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c /* Array of SQL
62da0 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 ITE_INDEX_SAMPLE
62db0 53 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a S samples */.};.
62dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 ./*.** Each samp
62dd0 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 le stored in the
62de0 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 sqlite_stat2 ta
62df0 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 ble is represent
62e00 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a ed in memory .**
62e10 20 75 73 69 6e 67 20 61 20 73 74 72 75 63 74 75 using a structu
62e20 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e re of this type.
62e30 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 .*/.struct Index
62e40 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e Sample {. union
62e50 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 {. char *z;
62e60 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
62e70 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 if eType is SQLI
62e80 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 TE_TEXT or SQLIT
62e90 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f E_BLOB */. do
62ea0 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a uble r; /*
62eb0 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 Value if eType
62ec0 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 is SQLITE_FLOAT
62ed0 6f 72 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 or SQLITE_INTEGE
62ee0 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 R */. } u;. u8
62ef0 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 eType;
62f00 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 /* SQLITE_NULL,
62f10 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e SQLITE_INTEGER .
62f20 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 .. etc. */. u8
62f30 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f nByte; /
62f40 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f * Size in byte o
62f50 66 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 f text or blob.
62f60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 */.};../*.** Eac
62f70 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f h token coming o
62f80 75 74 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20 ut of the lexer
62f90 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
62fa0 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 f.** this struct
62fb0 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 ure. Tokens are
62fc0 20 61 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61 also used as pa
62fd0 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 rt of an express
62fe0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ion..**.** Note
62ff0 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 if Token.z==0 th
63000 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 en Token.dyn and
63010 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 Token.n are und
63020 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 efined and.** ma
63030 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d y contain random
63040 20 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 values. Do not
63050 20 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 make any assump
63060 74 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 tions about Toke
63070 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b n.dyn.** and Tok
63080 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e en.n when Token.
63090 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 z==0..*/.struct
630a0 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 Token {. const
630b0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 char *z; /*
630c0 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 Text of the toke
630d0 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 n. Not NULL-ter
630e0 6d 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e minated! */. un
630f0 73 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 signed int n;
63100 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 /* Number of ch
63110 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 aracters in this
63120 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a token */.};../*
63130 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
63140 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
63150 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 e contains infor
63160 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f mation needed to
63170 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 generate.** cod
63180 65 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 e for a SELECT t
63190 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 hat contains agg
631a0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
631b0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e ..**.** If Expr.
631c0 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d op==TK_AGG_COLUM
631d0 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 N or TK_AGG_FUNC
631e0 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 TION then Expr.p
631f0 41 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 AggInfo is a.**
63200 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
63210 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
63220 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 Expr.iColumn fie
63230 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 ld is the index
63240 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 in.** AggInfo.aC
63250 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e ol[] or AggInfo.
63260 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 aFunc[] of infor
63270 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f mation needed to
63280 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 generate.** cod
63290 65 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e e for that node.
632a0 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 .**.** AggInfo.p
632b0 47 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 GroupBy and AggI
632c0 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 nfo.aFunc.pExpr
632d0 70 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 point to fields
632e0 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 within the.** or
632f0 69 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 iginal Select st
63300 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 ructure that des
63310 63 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 cribes the SELEC
63320 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 T statement. Th
63330 65 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f ese.** fields do
63340 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
63350 66 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c freed when deall
63360 6f 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 ocating the AggI
63370 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a nfo structure..*
63380 2f 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f /.struct AggInfo
63390 20 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f {. u8 directMo
633a0 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
633b0 44 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 Direct rendering
633c0 20 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 mode means take
633d0 20 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 data directly.
633e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
633f0 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d ** from
63400 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 source tables r
63410 61 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 ather than from
63420 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a accumulators */.
63430 20 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 u8 useSortingI
63440 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 dx; /* In
63450 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 direct mode, ref
63460 65 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 erence the sorti
63470 6e 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a ng index rather.
63480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63490 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 ** tha
634a0 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 n the source tab
634b0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 le */. int sort
634c0 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 ingIdx;
634d0 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 /* Cursor number
634e0 20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 of the sorting
634f0 69 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c index */. ExprL
63500 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 ist *pGroupBy;
63510 20 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 /* The group
63520 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 by clause */. i
63530 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d nt nSortingColum
63540 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
63550 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
63560 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 he sorting index
63570 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 */. struct Agg
63580 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a Info_col { /*
63590 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e For each column
635a0 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 used in source
635b0 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 tables */. Ta
635c0 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 ble *pTab;
635d0 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
635e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e table */. in
635f0 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 t iTable;
63600 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 /* Cursor
63610 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 number of the s
63620 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 ource table */.
63630 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 int iColumn;
63640 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
63650 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 olumn number wit
63660 68 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 hin the source t
63670 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
63680 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 iSorterColumn;
63690 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e /* Column n
636a0 75 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 umber in the sor
636b0 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 ting index */.
636c0 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 int iMem;
636d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
636e0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 mory location th
636f0 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d at acts as accum
63700 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 ulator */. Ex
63710 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 pr *pExpr;
63720 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 /* The or
63730 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f iginal expressio
63740 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a n */. } *aCol;.
63750 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 int nColumn;
63760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
63770 62 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 ber of used entr
63780 69 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f ies in aCol[] */
63790 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c . int nColumnAl
637a0 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 loc; /* Nu
637b0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c mber of slots al
637c0 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c located for aCol
637d0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 [] */. int nAcc
637e0 75 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 umulator;
637f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c /* Number of col
63800 75 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 umns that show t
63810 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 hrough to the ou
63820 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 tput..
63830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63840 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f ** Additional co
63850 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f lumns are used o
63860 6e 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 nly as parameter
63870 73 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 s to.
63880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
63890 2a 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 * aggregate func
638a0 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 tions */. struc
638b0 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b t AggInfo_func {
638c0 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 /* For each a
638d0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
638e0 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 n */. Expr *p
638f0 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
63900 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
63910 65 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e encoding the fun
63920 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e ction */. Fun
63930 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 cDef *pFunc;
63940 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 /* The agg
63950 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
63960 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
63970 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 /. int iMem;
63980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
63990 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f * Memory locatio
639a0 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 n that acts as a
639b0 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 ccumulator */.
639c0 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b int iDistinct;
639d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 /* Ep
639e0 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 hemeral table us
639f0 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 ed to enforce DI
63a00 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 STINCT */. } *a
63a10 46 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e Func;. int nFun
63a20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
63a30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
63a40 72 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 ries in aFunc[]
63a50 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c */. int nFuncAl
63a60 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 loc; /*
63a70 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 Number of slots
63a80 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 allocated for aF
63a90 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a unc[] */.};../*.
63aa0 2a 2a 20 54 68 65 20 64 61 74 61 74 79 70 65 20 ** The datatype
63ab0 79 6e 56 61 72 20 69 73 20 61 20 73 69 67 6e 65 ynVar is a signe
63ac0 64 20 69 6e 74 65 67 65 72 2c 20 65 69 74 68 65 d integer, eithe
63ad0 72 20 31 36 2d 62 69 74 20 6f 72 20 33 32 2d 62 r 16-bit or 32-b
63ae0 69 74 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69 it..** Usually i
63af0 74 20 69 73 20 31 36 2d 62 69 74 73 2e 20 20 42 t is 16-bits. B
63b00 75 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ut if SQLITE_MAX
63b10 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
63b20 20 69 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 is greater.** t
63b30 68 61 6e 20 33 32 37 36 37 20 77 65 20 68 61 76 han 32767 we hav
63b40 65 20 74 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d e to make it 32-
63b50 62 69 74 2e 20 20 31 36 2d 62 69 74 20 69 73 20 bit. 16-bit is
63b60 70 72 65 66 65 72 72 65 64 20 62 65 63 61 75 73 preferred becaus
63b70 65 0a 2a 2a 20 69 74 20 75 73 65 73 20 6c 65 73 e.** it uses les
63b80 73 20 6d 65 6d 6f 72 79 20 69 6e 20 74 68 65 20 s memory in the
63b90 45 78 70 72 20 6f 62 6a 65 63 74 2c 20 77 68 69 Expr object, whi
63ba0 63 68 20 69 73 20 61 20 62 69 67 20 6d 65 6d 6f ch is a big memo
63bb0 72 79 20 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79 ry user.** in sy
63bc0 73 74 65 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 stems with lots
63bd0 6f 66 20 70 72 65 70 61 72 65 64 20 73 74 61 74 of prepared stat
63be0 65 6d 65 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 ements. And few
63bf0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
63c00 20 6e 65 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 need more than
63c10 61 62 6f 75 74 20 31 30 20 6f 72 20 32 30 20 76 about 10 or 20 v
63c20 61 72 69 61 62 6c 65 73 2e 20 20 42 75 74 20 73 ariables. But s
63c30 6f 6d 65 20 65 78 74 72 65 6d 65 20 75 73 65 72 ome extreme user
63c40 73 20 77 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76 s want.** to hav
63c50 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 e prepared state
63c60 6d 65 6e 74 73 20 77 69 74 68 20 6f 76 65 72 20 ments with over
63c70 33 32 37 36 37 20 76 61 72 69 61 62 6c 65 73 2c 32767 variables,
63c80 20 61 6e 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a and for them.**
63c90 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 61 the option is a
63ca0 76 61 69 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d vailable (at com
63cb0 70 69 6c 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23 pile-time)..*/.#
63cc0 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 if SQLITE_MAX_VA
63cd0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33 RIABLE_NUMBER<=3
63ce0 32 37 36 37 0a 74 79 70 65 64 65 66 20 69 31 36 2767.typedef i16
63cf0 20 79 6e 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79 ynVar;.#else.ty
63d00 70 65 64 65 66 20 69 6e 74 20 79 6e 56 61 72 3b pedef int ynVar;
63d10 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
63d20 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 ach node of an e
63d30 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 xpression in the
63d40 20 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61 parse tree is a
63d50 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
63d60 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
63d70 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 .**.** Expr.op i
63d80 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 s the opcode. Th
63d90 65 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 e integer parser
63da0 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 token codes are
63db0 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 reused.** as op
63dc0 63 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 codes here. For
63dd0 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 example, the par
63de0 73 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 ser defines TK_G
63df0 45 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 E to be an integ
63e00 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 er.** code repre
63e10 73 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 senting the ">="
63e20 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 operator. This
63e30 73 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 same integer cod
63e40 65 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 e is reused.** t
63e50 6f 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 o represent the
63e60 67 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d greater-than-or-
63e70 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f equal-to operato
63e80 72 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 r in the express
63e90 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a ion.** tree..**.
63ea0 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
63eb0 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c sion is an SQL l
63ec0 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 iteral (TK_INTEG
63ed0 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b ER, TK_FLOAT, TK
63ee0 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b _BLOB, .** or TK
63ef0 5f 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 _STRING), then E
63f00 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 xpr.token contai
63f10 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 ns the text of t
63f20 68 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 he SQL literal.
63f30 49 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 If.** the expres
63f40 73 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 sion is a variab
63f50 6c 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 le (TK_VARIABLE)
63f60 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 , then Expr.toke
63f70 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a n contains the .
63f80 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 ** variable name
63f90 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 . Finally, if th
63fa0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
63fb0 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 an SQL function
63fc0 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a (TK_FUNCTION),.*
63fd0 2a 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 * then Expr.toke
63fe0 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e n contains the n
63ff0 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ame of the funct
64000 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e ion..**.** Expr.
64010 70 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e pRight and Expr.
64020 70 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 pLeft are the le
64030 66 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 ft and right sub
64040 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 expressions of a
64050 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 .** binary opera
64060 74 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 tor. Either or b
64070 6f 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e oth may be NULL.
64080 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c .**.** Expr.x.pL
64090 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 ist is a list of
640a0 20 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 arguments if th
640b0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
640c0 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c an SQL function,
640d0 0a 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 .** a CASE expre
640e0 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 ssion or an IN e
640f0 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 xpression of the
64100 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 form "<lhs> IN
64110 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a (<y>, <z>...)"..
64120 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 ** Expr.x.pSelec
64130 74 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65 t is used if the
64140 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
64150 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 sub-select or a
64160 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a n expression of.
64170 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 ** the form "<lh
64180 73 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e s> IN (SELECT ..
64190 2e 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 .)". If the EP_x
641a0 49 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 IsSelect bit is
641b0 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 set in the.** Ex
641c0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 pr.flags mask, t
641d0 68 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 hen Expr.x.pSele
641e0 63 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 ct is valid. Oth
641f0 65 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 erwise, Expr.x.p
64200 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 List is .** vali
64210 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 d..**.** An expr
64220 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f ession of the fo
64230 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 rm ID or ID.ID r
64240 65 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d efers to a colum
64250 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a n in a table..**
64260 20 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 For such expres
64270 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 sions, Expr.op i
64280 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 s set to TK_COLU
64290 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 MN and Expr.iTab
642a0 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 le is.** the int
642b0 65 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 eger cursor numb
642c0 65 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 er of a VDBE cur
642d0 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
642e0 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a that table and.*
642f0 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 * Expr.iColumn i
64300 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d s the column num
64310 62 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 ber for the spec
64320 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 ific column. If
64330 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 the.** expressi
64340 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 on is used as a
64350 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 result in an agg
64360 72 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 regate SELECT, t
64370 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 hen the.** value
64380 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 is also stored
64390 69 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 in the Expr.iAgg
643a0 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 column in the a
643b0 67 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 ggregate so that
643c0 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 .** it can be ac
643d0 63 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c cessed after all
643e0 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 aggregates are
643f0 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 computed..**.**
64400 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
64410 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 n is an unbound
64420 76 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 variable marker
64430 28 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b (a question mark
64440 20 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 .** character '
64450 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e ?' in the origin
64460 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 al SQL) then the
64470 20 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c Expr.iTable hol
64480 64 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a ds the index .**
64490 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 number for that
644a0 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a variable..**.**
644b0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
644c0 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 on is a subquery
644d0 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 then Expr.iColu
644e0 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 mn holds an inte
644f0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 ger.** register
64500 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e number containin
64510 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 g the result of
64520 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 the subquery. I
64530 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 f the.** subquer
64540 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 y gives a consta
64550 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 nt result, then
64560 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 iTable is -1. I
64570 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a f the subquery.*
64580 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 * gives a differ
64590 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 ent answer at di
645a0 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 fferent times du
645b0 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 ring statement p
645c0 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 rocessing.** the
645d0 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 n iTable is the
645e0 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 address of a sub
645f0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d routine that com
64600 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 putes the subque
64610 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ry..**.** If the
64620 20 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 Expr is of type
64630 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 OP_Column, and
64640 74 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 the table it is
64650 73 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a selecting from.*
64660 2a 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c * is a disk tabl
64670 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 e or the "old.*"
64680 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 pseudo-table, t
64690 68 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 hen pTab points
646a0 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 to the.** corres
646b0 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 ponding table de
646c0 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 finition..**.**
646d0 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 ALLOCATION NOTES
646e0 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a :.**.** Expr obj
646f0 65 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c ects can use a l
64700 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 ot of memory spa
64710 63 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 ce in database s
64720 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 chema. To.** he
64730 6c 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 lp reduce memory
64740 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 requirements, s
64750 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 ometimes an Expr
64760 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a object will be.
64770 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 ** truncated. A
64780 6e 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 nd to reduce the
64790 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 number of memor
647a0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 y allocations, s
647b0 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 ometimes.** two
647c0 6f 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a or more Expr obj
647d0 65 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f ects will be sto
647e0 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 red in a single
647f0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
64800 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 n,.** together w
64810 69 74 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 ith Expr.zToken
64820 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 strings..**.** I
64830 66 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 f the EP_Reduced
64840 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c and EP_TokenOnl
64850 79 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 y flags are set
64860 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 when.** an Expr
64870 6f 62 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 object is trunca
64880 74 65 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 ted. When EP_Re
64890 64 75 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 duced is set, th
648a0 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 en all.** the ch
648b0 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 ild Expr objects
648c0 20 69 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 in the Expr.pLe
648d0 66 74 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 ft and Expr.pRig
648e0 68 74 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 ht subtrees.** a
648f0 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 re contained wit
64900 68 69 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d hin the same mem
64910 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 ory allocation.
64920 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 Note, however,
64930 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 that.** the subt
64940 72 65 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 rees in Expr.x.p
64950 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 List or Expr.x.p
64960 53 65 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 Select are alway
64970 73 20 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 s separately.**
64980 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 allocated, regar
64990 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 dless of whether
649a0 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 or not EP_Reduc
649b0 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 ed is set..*/.st
649c0 72 75 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 ruct Expr {. u8
649d0 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 op;
649e0 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f /* Operatio
649f0 6e 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 n performed by t
64a00 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 his node */. ch
64a10 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 ar affinity;
64a20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 /* The affi
64a30 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 nity of the colu
64a40 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 mn or 0 if not a
64a50 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 column */. u16
64a60 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
64a70 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 /* Various f
64a80 6c 61 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 lags. EP_* See
64a90 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e below */. union
64aa0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f {. char *zTo
64ab0 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ken; /*
64ac0 20 54 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 Token value. Ze
64ad0 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e ro terminated an
64ae0 64 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 d dequoted */.
64af0 20 20 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 int iValue;
64b00 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
64b10 67 65 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f ger value if EP_
64b20 49 6e 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 IntValue */. }
64b30 75 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 u;.. /* If the
64b40 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 EP_TokenOnly fla
64b50 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 g is set in the
64b60 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c Expr.flags mask,
64b70 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 then no. ** sp
64b80 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ace is allocated
64b90 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 for the fields
64ba0 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 below this point
64bb0 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a . An attempt to.
64bc0 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d ** access them
64bd0 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
64be0 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 a segfault or ma
64bf0 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a lfunction. . **
64c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64c40 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c ***/.. Expr *pL
64c50 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f eft; /
64c60 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a * Left subnode *
64c70 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 /. Expr *pRight
64c80 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 ; /* Ri
64c90 67 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 ght subnode */.
64ca0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 union {. Exp
64cb0 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 rList *pList;
64cc0 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 /* Function ar
64cd0 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c guments or in "<
64ce0 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d expr> IN (<expr-
64cf0 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 list)" */. Se
64d00 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 lect *pSelect;
64d10 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 /* Used for s
64d20 75 62 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 ub-selects and "
64d30 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 <expr> IN (<sele
64d40 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a ct>)" */. } x;.
64d50 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
64d60 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
64d70 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f collation type o
64d80 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 f the column or
64d90 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 0 */.. /* If th
64da0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 e EP_Reduced fla
64db0 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 g is set in the
64dc0 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c Expr.flags mask,
64dd0 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 then no. ** sp
64de0 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ace is allocated
64df0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 for the fields
64e00 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 below this point
64e10 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a . An attempt to.
64e20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d ** access them
64e30 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
64e40 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 a segfault or ma
64e50 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a lfunction.. ***
64e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64ea0 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c **/.. int iTabl
64eb0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
64ec0 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 TK_COLUMN: curs
64ed0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 or number of tab
64ee0 6c 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d le holding colum
64ef0 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n.
64f00 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b ** TK
64f10 5f 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 _REGISTER: regis
64f20 74 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 ter number.
64f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64f40 20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 ** TK_TRIGGE
64f50 52 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d R: 1 -> new, 0 -
64f60 3e 20 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 > old */. ynVar
64f70 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 iColumn;
64f80 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 /* TK_COLUMN:
64f90 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d column index. -
64fa0 31 20 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 1 for rowid..
64fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64fc0 20 20 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 ** TK_VARI
64fd0 41 42 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e ABLE: variable n
64fe0 75 6d 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d umber (always >=
64ff0 20 31 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 1). */. i16 iA
65000 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gg;
65010 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 /* Which entry
65020 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f in pAggInfo->aCo
65030 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d l[] or ->aFunc[]
65040 20 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 */. i16 iRight
65050 4a 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 JoinTable; /*
65060 49 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 If EP_FromJoin,
65070 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 the right table
65080 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 of the join */.
65090 20 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 u8 flags2;
650a0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e /* Secon
650b0 64 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 d set of flags.
650c0 20 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 EP2_... */. u8
650d0 20 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 op2;
650e0 20 20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f /* If a TK_
650f0 52 45 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72 REGISTER, the or
65100 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 iginal value of
65110 45 78 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 Expr.op */. Agg
65120 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 Info *pAggInfo;
65130 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 /* Used by T
65140 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 K_AGG_COLUMN and
65150 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
65160 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
65170 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 b; /*
65180 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c Table for TK_COL
65190 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e UMN expressions.
651a0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d */.#if SQLITE_M
651b0 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a AX_EXPR_DEPTH>0.
651c0 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 int nHeight;
651d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 /* Heig
651e0 68 74 20 6f 66 20 74 68 65 20 74 72 65 65 20 68 ht of the tree h
651f0 65 61 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f eaded by this no
65200 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a de */.#endif.};.
65210 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
65220 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 wing are the mea
65230 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e nings of bits in
65240 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 the Expr.flags
65250 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e field..*/.#defin
65260 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 e EP_FromJoin
65270 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 0x0001 /* Origi
65280 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 nated in ON or U
65290 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 SING clause of a
652a0 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 join */.#define
652b0 20 45 50 5f 41 67 67 20 20 20 20 20 20 20 20 30 EP_Agg 0
652c0 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 x0002 /* Contai
652d0 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 ns one or more a
652e0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
652f0 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ns */.#define EP
65300 5f 52 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30 _Resolved 0x00
65310 30 34 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20 04 /* IDs have
65320 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f been resolved to
65330 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 COLUMNs */.#def
65340 69 6e 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20 ine EP_Error
65350 20 20 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70 0x0008 /* Exp
65360 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 ression contains
65370 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 one or more err
65380 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ors */.#define E
65390 50 5f 44 69 73 74 69 6e 63 74 20 20 20 30 78 30 P_Distinct 0x0
653a0 30 31 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74 010 /* Aggregat
653b0 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 e function with
653c0 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 DISTINCT keyword
653d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 */.#define EP_V
653e0 61 72 53 65 6c 65 63 74 20 20 30 78 30 30 32 30 arSelect 0x0020
653f0 20 20 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20 /* pSelect is
65400 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 correlated, not
65410 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 constant */.#def
65420 69 6e 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 ine EP_DblQuoted
65430 20 20 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 0x0040 /* tok
65440 65 6e 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 en.z was origina
65450 6c 6c 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a lly in "..." */.
65460 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 #define EP_Infix
65470 46 75 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a Func 0x0080 /*
65480 20 54 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 True for an inf
65490 69 78 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b ix function: LIK
654a0 45 2c 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a E, GLOB, etc */.
654b0 23 64 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f #define EP_ExpCo
654c0 6c 6c 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a llate 0x0100 /*
654d0 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
654e0 6e 63 65 20 73 70 65 63 69 66 69 65 64 20 65 78 nce specified ex
654f0 70 6c 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 plicitly */.#def
65500 69 6e 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20 ine EP_AnyAff
65510 20 20 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 0x0200 /* Can
65520 20 74 61 6b 65 20 61 20 63 61 63 68 65 64 20 63 take a cached c
65530 6f 6c 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 olumn of any aff
65540 69 6e 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 inity */.#define
65550 20 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 EP_FixedDest 0
65560 78 30 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 x0400 /* Result
65570 20 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 needed in a spe
65580 63 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a cific register *
65590 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 /.#define EP_Int
655a0 56 61 6c 75 65 20 20 20 30 78 30 38 30 30 20 20 Value 0x0800
655b0 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
655c0 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e contained in u.
655d0 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e iValue */.#defin
655e0 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 e EP_xIsSelect
655f0 30 78 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 0x1000 /* x.pSe
65600 6c 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f lect is valid (o
65610 74 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 therwise x.pList
65620 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 is) */..#define
65630 20 45 50 5f 52 65 64 75 63 65 64 20 20 20 20 30 EP_Reduced 0
65640 78 32 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 x2000 /* Expr s
65650 74 72 75 63 74 20 69 73 20 45 58 50 52 5f 52 45 truct is EXPR_RE
65660 44 55 43 45 44 53 49 5a 45 20 62 79 74 65 73 20 DUCEDSIZE bytes
65670 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
65680 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 EP_TokenOnly 0x
65690 34 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 4000 /* Expr st
656a0 72 75 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b ruct is EXPR_TOK
656b0 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 ENONLYSIZE bytes
656c0 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 only */.#define
656d0 20 45 50 5f 53 74 61 74 69 63 20 20 20 20 20 30 EP_Static 0
656e0 78 38 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 x8000 /* Held i
656f0 6e 20 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 n memory not obt
65700 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
65710 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 c() */../*.** Th
65720 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
65730 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 the meanings of
65740 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 bits in the Expr
65750 2e 66 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a .flags2 field..*
65760 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 /.#define EP2_Ma
65770 6c 6c 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 llocedToken 0x0
65780 30 30 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 001 /* Need to
65790 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 sqlite3DbFree()
657a0 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 Expr.zToken */.#
657b0 64 65 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64 define EP2_Irred
657c0 75 63 69 62 6c 65 20 20 20 20 30 78 30 30 30 32 ucible 0x0002
657d0 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 /* Cannot EXPR
657e0 44 55 50 5f 52 45 44 55 43 45 20 74 68 69 73 20 DUP_REDUCE this
657f0 45 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 Expr */../*.** T
65800 68 65 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e he pseudo-routin
65810 65 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 e sqlite3ExprSet
65820 49 72 72 65 64 75 63 69 62 6c 65 20 73 65 74 73 Irreducible sets
65830 20 74 68 65 20 45 50 32 5f 49 72 72 65 64 75 63 the EP2_Irreduc
65840 69 62 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 ible.** flag on
65850 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 an expression st
65860 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 66 ructure. This f
65870 6c 61 67 20 69 73 20 75 73 65 64 20 66 6f 72 20 lag is used for
65880 56 56 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a VV&A only. The.
65890 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d ** routine is im
658a0 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d plemented as a m
658b0 61 63 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 acro that only w
658c0 6f 72 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62 orks when in deb
658d0 75 67 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 ugging mode,.**
658e0 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 so as not to bur
658f0 64 65 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 den production c
65900 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ode..*/.#ifdef S
65910 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65 QLITE_DEBUG.# de
65920 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 fine ExprSetIrre
65930 64 75 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d ducible(X) (X)-
65940 3e 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 >flags2 |= EP2_I
65950 72 72 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 rreducible.#else
65960 0a 23 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 .# define ExprSe
65970 74 49 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a tIrreducible(X).
65980 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
65990 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 ese macros can b
659a0 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 e used to test,
659b0 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 set, or clear bi
659c0 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 ts in the .** Ex
659d0 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a pr.flags field..
659e0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 */.#define ExprH
659f0 61 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 asProperty(E,P)
65a00 20 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 (((E)->flags
65a10 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 &(P))==(P)).#def
65a20 69 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72 ine ExprHasAnyPr
65a30 6f 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28 operty(E,P) (((
65a40 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d E)->flags&(P))!=
65a50 30 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53 0).#define ExprS
65a60 65 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 etProperty(E,P)
65a70 20 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d (E)->flags|=
65a80 28 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 (P).#define Expr
65a90 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c ClearProperty(E,
65aa0 50 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 P) (E)->flags&
65ab0 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 =~(P)../*.** Mac
65ac0 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ros to determine
65ad0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
65ae0 79 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79 ytes required by
65af0 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a a normal Expr .
65b00 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78 ** struct, an Ex
65b10 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 pr struct with t
65b20 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c he EP_Reduced fl
65b30 61 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66 ag set in Expr.f
65b40 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 lags .** and an
65b50 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68 Expr struct with
65b60 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c the EP_TokenOnl
65b70 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 y flag set..*/.#
65b80 64 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c define EXPR_FULL
65b90 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73 SIZE s
65ba0 69 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20 izeof(Expr)
65bb0 20 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 /* Full si
65bc0 7a 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 ze */.#define EX
65bd0 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 PR_REDUCEDSIZE
65be0 20 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 offsetof(E
65bf0 78 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 xpr,iTable) /*
65c00 43 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 Common features
65c10 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f */.#define EXPR_
65c20 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 TOKENONLYSIZE
65c30 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 offsetof(Expr
65c40 2c 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77 ,pLeft) /* Few
65c50 65 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a er features */..
65c60 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 /*.** Flags pass
65c70 65 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 ed to the sqlite
65c80 33 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 3ExprDup() funct
65c90 69 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 ion. See the hea
65ca0 64 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 der comment .**
65cb0 61 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 above sqlite3Exp
65cc0 72 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 rDup() for detai
65cd0 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ls..*/.#define E
65ce0 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 XPRDUP_REDUCE
65cf0 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 0x0001 /*
65d00 20 55 73 65 64 20 72 65 64 75 63 65 64 2d 73 69 Used reduced-si
65d10 7a 65 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f ze Expr nodes */
65d20 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f ../*.** A list o
65d30 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 f expressions.
65d40 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 Each expression
65d50 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 may optionally h
65d60 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 ave a.** name.
65d70 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d An expr/name com
65d80 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 bination can be
65d90 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 used in several
65da0 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 ways, such.** as
65db0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 the list of "ex
65dc0 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 pr AS ID" fields
65dd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 following a "SE
65de0 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a LECT" or in the.
65df0 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d ** list of "ID =
65e00 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 expr" items in
65e10 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 an UPDATE. A li
65e20 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e st of expression
65e30 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 s can.** also be
65e40 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67 used as the arg
65e50 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 ument to a funct
65e60 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 ion, in which ca
65e70 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a se the a.zName.*
65e80 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 * field is not u
65e90 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 sed..*/.struct E
65ea0 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 xprList {. int
65eb0 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 nExpr;
65ec0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
65ed0 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 expressions on t
65ee0 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
65ef0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 nAlloc;
65f00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
65f10 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 entries allocat
65f20 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e ed below */. in
65f30 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 t iECursor;
65f40 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 /* VDBE Cur
65f50 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 sor associated w
65f60 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 ith this ExprLis
65f70 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 t */. struct Ex
65f80 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 prList_item {.
65f90 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 Expr *pExpr;
65fa0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
65fb0 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 list of expressi
65fc0 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 ons */. char
65fd0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
65fe0 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 /* Token assoc
65ff0 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
66000 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
66010 20 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 char *zSpan;
66020 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 /* Orig
66030 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 inal text of the
66040 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
66050 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b u8 sortOrder;
66060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
66070 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 or DESC or 0 for
66080 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 ASC */. u8 d
66090 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 one;
660a0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 /* A flag to
660b0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 indicate when pr
660c0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 ocessing is fini
660d0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 shed */. u16
660e0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 iCol;
660f0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 /* For ORDER
66100 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 BY, column numbe
66110 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 r in result set
66120 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 */. u16 iAlia
66130 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
66140 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 Index into Pars
66150 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a e.aAlias[] for z
66160 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 Name */. } *a;
66170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66180 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f /* One entry fo
66190 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f r each expressio
661a0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
661b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
661c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
661d0 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72 73 used by the pars
661e0 65 72 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 er to record bot
661f0 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74 h.** the parse t
66200 72 65 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65 ree for an expre
66210 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 ssion and the sp
66220 61 6e 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 an of input text
66230 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 for an.** expre
66240 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ssion..*/.struct
66250 20 45 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 ExprSpan {. Ex
66260 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 pr *pExpr;
66270 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 /* The expre
66280 73 73 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65 ssion parse tree
66290 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
662a0 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 *zStart; /* F
662b0 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f irst character o
662c0 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a f input text */.
662d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 const char *zE
662e0 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 nd; /* One c
662f0 68 61 72 61 63 74 65 72 20 70 61 73 74 20 74 68 haracter past th
66300 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 e end of input t
66310 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ext */.};../*.**
66320 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
66330 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
66340 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 an hold a simple
66350 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 list of identif
66360 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 iers,.** such as
66370 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 the list "a,b,c
66380 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 " in the followi
66390 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a ng statements:.*
663a0 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 *.** INSERT
663b0 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 INTO t(a,b,c) V
663c0 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 ALUES ...;.**
663d0 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 CREATE INDEX
663e0 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b idx ON t(a,b,c);
663f0 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 .** CREATE
66400 54 52 49 47 47 45 52 20 74 72 69 67 20 42 45 46 TRIGGER trig BEF
66410 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 ORE UPDATE ON t(
66420 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a a,b,c) ...;.**.*
66430 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 * The IdList.a.i
66440 64 78 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 dx field is used
66450 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 when the IdList
66460 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 represents the
66470 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d list of.** colum
66480 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 n names after a
66490 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e table name in an
664a0 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e INSERT statemen
664b0 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 t. In the state
664c0 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 ment.**.** I
664d0 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 NSERT INTO t(a,b
664e0 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 ,c) ....**.** If
664f0 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 "a" is the k-th
66500 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 column of table
66510 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 "t", then IdLis
66520 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a t.a[0].idx==k..*
66530 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 /.struct IdList
66540 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 {. struct IdLis
66550 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 t_item {. cha
66560 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f r *zName; /
66570 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 * Name of the id
66580 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 entifier */.
66590 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 int idx;
665a0 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f /* Index in so
665b0 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 me Table.aCol[]
665c0 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 of a column name
665d0 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a d zName */. } *
665e0 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 a;. int nId;
665f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
66600 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f of identifiers o
66610 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 n the list */.
66620 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 int nAlloc;
66630 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
66640 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 tries allocated
66650 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f for a[] below */
66660 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 .};../*.** The b
66670 69 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 itmask datatype
66680 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 defined below is
66690 20 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 used for variou
666a0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e s optimizations.
666b0 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 .**.** Changing
666c0 74 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 this from a 64-b
666d0 69 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 it to a 32-bit t
666e0 79 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e ype limits the n
666f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c umber of.** tabl
66700 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 es in a join to
66710 33 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 32 instead of 64
66720 2e 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 . But it also r
66730 65 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a educes the size.
66740 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 ** of the librar
66750 79 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f y by 738 bytes o
66760 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 n ix86..*/.typed
66770 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a ef u64 Bitmask;.
66780 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 ./*.** The numbe
66790 72 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 r of bits in a B
667a0 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d itmask. "BMS" m
667b0 65 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 eans "BitMask Si
667c0 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ze"..*/.#define
667d0 42 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 BMS ((int)(size
667e0 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a of(Bitmask)*8)).
667f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
66800 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 wing structure d
66810 65 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f escribes the FRO
66820 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 M clause of a SE
66830 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
66840 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 ** Each table or
66850 20 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 subquery in the
66860 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 FROM clause is
66870 61 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 a separate eleme
66880 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 nt of.** the Src
66890 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a List.a[] array..
668a0 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 **.** With the a
668b0 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 ddition of multi
668c0 70 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70 ple database sup
668d0 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 port, the follow
668e0 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ing structure.**
668f0 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 can also be use
66900 64 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 d to describe a
66910 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 particular table
66920 20 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62 such as the tab
66930 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f le that.** is mo
66940 64 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 dified by an INS
66950 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 ERT, DELETE, or
66960 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 UPDATE statement
66970 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 . In standard S
66980 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 QL,.** such a ta
66990 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 ble must be a si
669a0 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 mple name: ID.
669b0 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 But in SQLite, t
669c0 68 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 he table can.**
669d0 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 now be identifie
669e0 64 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20 d by a database
669f0 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 name, a dot, the
66a00 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 n the table name
66a10 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 : ID.ID..**.** T
66a20 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 he jointype star
66a30 74 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 ts out showing t
66a40 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 he join type bet
66a50 77 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 ween the current
66a60 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 table.** and th
66a70 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 e next table on
66a80 74 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 the list. The p
66a90 61 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 arser builds the
66aa0 20 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a list this way..
66ab0 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 ** But sqlite3Sr
66ac0 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 cListShiftJoinTy
66ad0 70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 pe() later shift
66ae0 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 s the jointypes
66af0 73 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 so that each.**
66b00 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 jointype express
66b10 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 es the join betw
66b20 65 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e een the table an
66b30 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 d the previous t
66b40 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 able..*/.struct
66b50 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 SrcList {. i16
66b60 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 nSrc; /*
66b70 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 Number of tables
66b80 20 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 69 or subqueries i
66b90 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
66ba0 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f e */. i16 nAllo
66bb0 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 c; /* Numbe
66bc0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c r of entries all
66bd0 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 ocated in a[] be
66be0 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 low */. struct
66bf0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 SrcList_item {.
66c00 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 char *zDataba
66c10 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 se; /* Name of
66c20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 database holding
66c30 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 this table */.
66c40 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 char *zName;
66c50 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
66c60 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 the table */.
66c70 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 char *zAlias;
66c80 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 /* The "B" pa
66c90 72 74 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 rt of a "A AS B"
66ca0 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 phrase. zName
66cb0 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 is the "A" */.
66cc0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
66cd0 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 /* An SQL ta
66ce0 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ble correspondin
66cf0 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 g to zName */.
66d00 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
66d10 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 t; /* A SELECT
66d20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 statement used i
66d30 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 n place of a tab
66d40 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 le name */. u
66d50 38 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 8 isPopulated;
66d60 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 /* Temporary ta
66d70 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ble associated w
66d80 69 74 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f ith SELECT is po
66d90 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 pulated */. u
66da0 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 8 jointype;
66db0 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e /* Type of join
66dc0 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 between this ab
66dd0 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 le and the previ
66de0 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f ous */. u8 no
66df0 74 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 tIndexed; /*
66e00 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 True if there is
66e10 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 a NOT INDEXED c
66e20 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 lause */. int
66e30 20 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f iCursor; /
66e40 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
66e50 72 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f r number used to
66e60 20 61 63 63 65 73 73 20 74 68 69 73 20 74 61 62 access this tab
66e70 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a le */. Expr *
66e80 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 pOn; /* T
66e90 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 he ON clause of
66ea0 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 a join */. Id
66eb0 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 List *pUsing;
66ec0 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 /* The USING cla
66ed0 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f use of a join */
66ee0 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c . Bitmask col
66ef0 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 Used; /* Bit N
66f00 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f (1<<N) set if co
66f10 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 lumn N of pTab i
66f20 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 s used */. ch
66f30 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 ar *zIndex;
66f40 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 /* Identifier fr
66f50 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c om "INDEXED BY <
66f60 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 zIndex>" clause
66f70 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 */. Index *pI
66f80 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 ndex; /* Inde
66f90 78 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 x structure corr
66fa0 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e esponding to zIn
66fb0 64 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 dex, if any */.
66fc0 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 } a[1];
66fd0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 /* One entr
66fe0 79 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 y for each ident
66ff0 69 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 ifier on the lis
67000 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 t */.};../*.** P
67010 65 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 ermitted values
67020 6f 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 of the SrcList.a
67030 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a .jointype field.
67040 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e */.#define JT_IN
67050 4e 45 52 20 20 20 20 20 30 78 30 30 30 31 20 20 NER 0x0001
67060 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 /* Any kind of
67070 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 inner or cross
67080 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 join */.#define
67090 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 JT_CROSS 0x0
670a0 30 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 002 /* Explic
670b0 69 74 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 it use of the CR
670c0 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 OSS keyword */.#
670d0 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 define JT_NATURA
670e0 4c 20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a L 0x0004 /*
670f0 20 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 True for a "nat
67100 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 ural" join */.#d
67110 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 efine JT_LEFT
67120 20 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 0x0008 /*
67130 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 Left outer join
67140 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 */.#define JT_RI
67150 47 48 54 20 20 20 20 20 30 78 30 30 31 30 20 20 GHT 0x0010
67160 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 /* Right outer
67170 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 join */.#define
67180 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 JT_OUTER 0x
67190 30 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 0020 /* The "
671a0 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 OUTER" keyword i
671b0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 s present */.#de
671c0 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 fine JT_ERROR
671d0 20 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 0x0040 /* u
671e0 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 nknown or unsupp
671f0 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 orted join type
67200 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 */.../*.** A Whe
67210 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f rePlan object ho
67220 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 lds information
67230 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61 that describes a
67240 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 lookup.** strat
67250 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 egy..**.** This
67260 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64 object is intend
67270 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 ed to be opaque
67280 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 77 outside of the w
67290 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a here.c module..*
672a0 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 * It is included
672b0 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 here only so th
672c0 61 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 at that compiler
672d0 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 will know how b
672e0 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f ig it.** is. No
672f0 6e 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73 ne of the fields
67300 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 in this object
67310 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f should be used o
67320 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 utside of.** the
67330 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e where.c module.
67340 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 .**.** Within th
67350 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 e union, pIdx is
67360 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 only used when
67370 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e wsFlags&WHERE_IN
67380 44 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a DEXED is true..*
67390 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 * pTerm is only
673a0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 used when wsFlag
673b0 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 s&WHERE_MULTI_OR
673c0 20 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70 is true. And p
673d0 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e VtabIdx.** is on
673e0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
673f0 6c 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 lags&WHERE_VIRTU
67400 41 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e ALTABLE is true.
67410 20 20 49 74 20 69 73 20 6e 65 76 65 72 20 74 68 It is never th
67420 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d e.** case that m
67430 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 ore than one of
67440 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 these conditions
67450 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 is true..*/.str
67460 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a uct WherePlan {.
67470 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20 u32 wsFlags;
67480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67490 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 /* WHERE_* flag
674a0 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 s that describe
674b0 74 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a the strategy */.
674c0 20 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20 u32 nEq;
674d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
674e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d /* Number of ==
674f0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
67500 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e union {. In
67510 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 dex *pIdx;
67520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67530 49 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 45 Index when WHERE
67540 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 _INDEXED is true
67550 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57 */. struct W
67560 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b hereTerm *pTerm;
67570 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 /* WHERE
67580 63 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20 clause term for
67590 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 OR-search */.
675a0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 sqlite3_index_i
675b0 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20 nfo *pVtabIdx;
675c0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
675d0 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f index to use */
675e0 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a . } u;.};../*.*
675f0 2a 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65 * For each neste
67600 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 d loop in a WHER
67610 45 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 E clause impleme
67620 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 ntation, the Whe
67630 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 reInfo.** struct
67640 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 ure contains a s
67650 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f ingle instance o
67660 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
67670 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
67680 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 e.** is intended
67690 20 74 6f 20 62 65 20 70 72 69 76 61 74 65 20 74 to be private t
676a0 68 65 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d he the where.c m
676b0 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 odule and should
676c0 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 not be.** acces
676d0 73 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79 s or modified by
676e0 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a other modules..
676f0 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e **.** The pIdxIn
67700 66 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 fo field is used
67710 20 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68 to help pick th
67720 65 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 e best index on
67730 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 a.** virtual tab
67740 6c 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66 le. The pIdxInf
67750 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 o pointer contai
67760 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 ns indexing.** i
67770 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 nformation for t
67780 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e he i-th table in
67790 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
677a0 20 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69 before reorderi
677b0 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 ng..** All the p
677c0 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 IdxInfo pointers
677d0 20 61 72 65 20 66 72 65 65 64 20 62 79 20 77 68 are freed by wh
677e0 65 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e ereInfoFree() in
677f0 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c where.c..** All
67800 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 other informati
67810 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57 on in the i-th W
67820 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 hereLevel object
67830 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 for the i-th ta
67840 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f ble.** after FRO
67850 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e M clause orderin
67860 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 g..*/.struct Whe
67870 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 reLevel {. Wher
67880 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 ePlan plan;
67890 20 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 /* query plan
678a0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 for this element
678b0 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 of the FROM cla
678c0 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 use */. int iLe
678d0 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f ftJoin; /
678e0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 * Memory cell us
678f0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
67900 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 LEFT OUTER JOIN
67910 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72 */. int iTabCur
67920 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
67930 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 e VDBE cursor us
67940 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ed to access the
67950 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
67960 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20 iIdxCur;
67970 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 /* The VDBE cu
67980 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 rsor used to acc
67990 65 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e ess pIdx */. in
679a0 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20 t addrBrk;
679b0 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
679c0 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 to break out of
679d0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 the loop */. i
679e0 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 nt addrNxt;
679f0 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 /* Jump her
67a00 65 20 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e e to start the n
67a10 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 ext IN combinati
67a20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 on */. int addr
67a30 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Cont; /*
67a40 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f Jump here to co
67a50 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 ntinue with the
67a60 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 next loop cycle
67a70 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72 */. int addrFir
67a80 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 st; /* Fi
67a90 72 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 rst instruction
67aa0 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 of interior of t
67ab0 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 he loop */. u8
67ac0 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 iFrom;
67ad0 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 /* Which entr
67ae0 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c y in the FROM cl
67af0 61 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c ause */. u8 op,
67b00 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 p5;
67b10 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 /* Opcode and P5
67b20 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74 of the opcode t
67b30 68 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f hat ends the loo
67b40 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 p */. int p1, p
67b50 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2; /*
67b60 4f 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20 Operands of the
67b70 6f 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65 opcode used to e
67b80 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a nds the loop */.
67b90 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 union {
67ba0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 /* Infor
67bb0 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65 mation that depe
67bc0 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c nds on plan.wsFl
67bd0 61 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 ags */. struc
67be0 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 t {. int nI
67bf0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
67c00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
67c10 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b ries in aInLoop[
67c20 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 ] */. struc
67c30 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 t InLoop {.
67c40 20 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 int iCur;
67c50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
67c60 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 VDBE cursor use
67c70 64 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65 d by this IN ope
67c80 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 rator */.
67c90 20 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 int addrInTop;
67ca0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f /* Top o
67cb0 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f f the IN loop */
67cc0 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f . } *aInLoo
67cd0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p; /*
67ce0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
67cf0 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e t each nested IN
67d00 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 operator */.
67d10 20 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20 } in;
67d20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 /* Used w
67d30 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 hen plan.wsFlags
67d40 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a &WHERE_IN_ABLE *
67d50 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 /. } u;.. /* T
67d60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 he following fie
67d70 6c 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 ld is really not
67d80 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 75 72 part of the cur
67d90 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 rent level. But
67da0 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 . ** we need a
67db0 70 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20 76 place to cache v
67dc0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 irtual table ind
67dd0 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ex information f
67de0 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 or each. ** vir
67df0 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 tual table in th
67e00 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e e FROM clause an
67e10 64 20 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c d the WhereLevel
67e20 20 73 74 72 75 63 74 75 72 65 20 69 73 0a 20 20 structure is.
67e30 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 ** a convenient
67e40 70 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65 72 place since ther
67e50 65 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 e is one WhereLe
67e60 76 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f vel for each FRO
67e70 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c M clause. ** el
67e80 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 ement.. */. sq
67e90 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
67ea0 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 *pIdxInfo; /*
67eb0 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e Index info for n
67ec0 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 -th source table
67ed0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c */.};../*.** Fl
67ee0 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 ags appropriate
67ef0 66 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61 for the wctrlFla
67f00 67 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 gs parameter of
67f10 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 sqlite3WhereBegi
67f20 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 n().** and the W
67f30 68 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c hereInfo.wctrlFl
67f40 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 ags member..*/.#
67f50 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 define WHERE_ORD
67f60 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 ERBY_NORMAL 0x
67f70 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0000 /* No-op */
67f80 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f .#define WHERE_O
67f90 52 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 RDERBY_MIN
67fa0 30 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 0x0001 /* ORDER
67fb0 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f BY processing fo
67fc0 72 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a r min() func */.
67fd0 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 #define WHERE_OR
67fe0 44 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30 DERBY_MAX 0
67ff0 78 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 x0002 /* ORDER B
68000 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 Y processing for
68010 20 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 max() func */.#
68020 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 define WHERE_ONE
68030 50 41 53 53 5f 44 45 53 49 52 45 44 20 20 30 78 PASS_DESIRED 0x
68040 30 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 0004 /* Want to
68050 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 do one-pass UPDA
68060 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 TE/DELETE */.#de
68070 66 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49 fine WHERE_DUPLI
68080 43 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 CATES_OK 0x00
68090 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 08 /* Ok to retu
680a0 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 rn a row more th
680b0 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 an once */.#defi
680c0 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 ne WHERE_OMIT_OP
680d0 45 4e 20 20 20 20 20 20 20 20 30 78 30 30 31 30 EN 0x0010
680e0 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 /* Table cursor
680f0 20 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 are already ope
68100 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 n */.#define WHE
68110 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 RE_OMIT_CLOSE
68120 20 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 0x0020 /* Om
68130 69 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c it close of tabl
68140 65 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 e & index cursor
68150 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 s */.#define WHE
68160 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 RE_FORCE_TABLE
68170 20 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 0x0040 /* Do
68180 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 not use an inde
68190 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f x-only search */
681a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 ../*.** The WHER
681b0 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 E clause process
681c0 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 ing routine has
681d0 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 two halves. The
681e0 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 .** first part d
681f0 6f 65 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 oes the start of
68200 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 the WHERE loop
68210 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a and the second.*
68220 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 * half does the
68230 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 tail of the WHER
68240 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 E loop. An inst
68250 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 ance of.** this
68260 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 structure is ret
68270 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 urned by the fir
68280 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 st half and pass
68290 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 ed.** into the s
682a0 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 econd half to gi
682b0 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 ve some continui
682c0 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 ty..*/.struct Wh
682d0 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 ereInfo {. Pars
682e0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 e *pParse;
682f0 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 /* Parsing and
68300 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 code generating
68310 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 context */. u16
68320 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 wctrlFlags;
68330 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 /* Flags origi
68340 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 nally passed to
68350 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 sqlite3WhereBegi
68360 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e n() */. u8 okOn
68370 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a ePass; /*
68380 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 Ok to use one-p
68390 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f ass algorithm fo
683a0 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 r UPDATE or DELE
683b0 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 TE */. SrcList
683c0 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 *pTabList;
683d0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
683e0 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 f tables in the
683f0 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 join */. int iT
68400 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
68410 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
68420 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f very beginning o
68430 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 f the WHERE loop
68440 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 */. int iConti
68450 6e 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 nue;
68460 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 /* Jump her
68470 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 e to continue wi
68480 74 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a th next record *
68490 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 /. int iBreak;
684a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
684b0 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
684c0 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 to break out of
684d0 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e the loop */. in
684e0 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 t nLevel;
684f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68500 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 Number of nested
68510 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 loop */. struc
68520 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 t WhereClause *p
68530 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63 WC; /* Dec
68540 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 omposition of th
68550 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a e WHERE clause *
68560 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 /. WhereLevel a
68570 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 [1];
68580 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
68590 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 n about each nes
685a0 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 t loop in WHERE
685b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e */.};../*.** A N
685c0 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e ameContext defin
685d0 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 es a context in
685e0 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 which to resolve
685f0 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d table and colum
68600 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 n.** names. The
68610 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 context consist
68620 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74 s of a list of t
68630 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c ables (the pSrcL
68640 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a ist) field and.*
68650 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 * a list of name
68660 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45 d expression (pE
68670 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 List). The name
68680 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 d expression lis
68690 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c t may.** be NULL
686a0 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f 72 72 . The pSrc corr
686b0 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 esponds to the F
686c0 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 ROM clause of a
686d0 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 SELECT or.** to
686e0 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
686f0 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49 operated on by I
68700 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f NSERT, UPDATE, o
68710 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a r DELETE. The.*
68720 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70 * pEList corresp
68730 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 75 onds to the resu
68740 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 lt set of a SELE
68750 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 CT and is NULL f
68760 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 or.** other stat
68770 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 ements..**.** Na
68780 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 meContexts can b
68790 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20 e nested. When
687a0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c resolving names,
687b0 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 the inner-most
687c0 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73 .** context is s
687d0 65 61 72 63 68 65 64 20 66 69 72 73 74 2e 20 20 earched first.
687e0 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 If no match is f
687f0 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f ound, the next o
68800 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 uter.** context
68810 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 is checked. If
68820 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e there is still n
68830 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78 o match, the nex
68840 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 t context.** is
68850 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70 checked. This p
68860 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 rocess continues
68870 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20 until either a
68880 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a match is found.*
68890 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 * or all context
688a0 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68 s are check. Wh
688b0 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f en a match is fo
688c0 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 und, the nRef me
688d0 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 mber of.** the c
688e0 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e ontext containin
688f0 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 20 69 g the match is i
68900 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a ncremented. .**.
68910 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 72 79 ** Each subquery
68920 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 gets a new Name
68930 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e Context. The pN
68940 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 ext field points
68950 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 to the.** NameC
68960 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70 61 ontext in the pa
68970 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75 rent query. Thu
68980 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 s the process of
68990 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a scanning the.**
689a0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 NameContext lis
689b0 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f t corresponds to
689c0 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75 searching throu
689d0 67 68 20 73 75 63 63 65 73 73 69 76 65 6c 79 20 gh successively
689e0 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 outer.** subquer
689f0 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ies looking for
68a00 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 a match..*/.stru
68a10 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b ct NameContext {
68a20 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
68a30 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 ; /* The p
68a40 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 arser */. SrcLi
68a50 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 st *pSrcList;
68a60 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 /* One or more t
68a70 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65 ables used to re
68a80 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 solve names */.
68a90 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
68aa0 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 t; /* Optiona
68ab0 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 l list of named
68ac0 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 expressions */.
68ad0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
68ae0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
68af0 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 of names resolve
68b00 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78 d by this contex
68b10 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b t */. int nErr;
68b20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
68b30 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
68b40 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c encountered whil
68b50 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 e resolving name
68b60 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 s */. u8 allowA
68b70 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 gg; /* A
68b80 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
68b90 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 ns allowed here
68ba0 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20 */. u8 hasAgg;
68bb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
68bc0 65 20 69 66 20 61 67 67 72 65 67 61 74 65 73 20 e if aggregates
68bd0 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 are seen */. u8
68be0 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20 isCheck;
68bf0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 /* True if re
68c00 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e solving names in
68c10 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 a CHECK constra
68c20 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 int */. int nDe
68c30 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a pth; /*
68c40 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 75 65 Depth of subque
68c50 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 ry recursion. 1
68c60 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e for no recursion
68c70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 */. AggInfo *p
68c80 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e AggInfo; /* In
68c90 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
68ca0 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68 aggregates at th
68cb0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 is level */. Na
68cc0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 meContext *pNext
68cd0 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 ; /* Next outer
68ce0 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 name context.
68cf0 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f NULL for outermo
68d00 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
68d10 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
68d20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
68d30 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
68d40 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a all information.
68d50 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e ** needed to gen
68d60 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 erate code for a
68d70 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 single SELECT s
68d80 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 tatement..**.**
68d90 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f nLimit is set to
68da0 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20 -1 if there is
68db0 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e no LIMIT clause.
68dc0 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74 nOffset is set
68dd0 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 to 0..** If the
68de0 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c re is a LIMIT cl
68df0 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 65 72 ause, the parser
68e00 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 sets nLimit to
68e10 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
68e20 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f .** limit and nO
68e30 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c ffset to the val
68e40 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 ue of the offset
68e50 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 65 20 (or 0 if there
68e60 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 is not.** offset
68e70 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e ). But later on
68e80 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 , nLimit and nOf
68e90 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20 fset become the
68ea0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 memory locations
68eb0 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20 .** in the VDBE
68ec0 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 that record the
68ed0 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 limit and offset
68ee0 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a counters..**.**
68ef0 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 addrOpenEphm[]
68f00 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 entries contain
68f10 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f the address of O
68f20 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 P_OpenEphemeral
68f30 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 opcodes..** Thes
68f40 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 73 74 e addresses must
68f50 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68 be stored so th
68f60 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63 at we can go bac
68f70 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a k and fill in.**
68f80 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 the P4_KEYINFO
68f90 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65 72 and P2 parameter
68fa0 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65 s later. Neithe
68fb0 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f r the KeyInfo no
68fc0 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 r.** the number
68fd0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 of columns in P2
68fe0 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 can be computed
68ff0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
69000 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f e.** as the OP_O
69010 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74 penEphm instruct
69020 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63 ion is coded bec
69030 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 ause not.** enou
69040 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 gh information a
69050 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e bout the compoun
69060 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e d query is known
69070 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a at that point..
69080 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 ** The KeyInfo f
69090 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b or addrOpenTran[
690a0 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 0] and [1] conta
690b0 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 ins collating se
690c0 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 quences.** for t
690d0 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
690e0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 The KeyInfo for
690f0 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 addrOpenTran[2]
69100 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 contains collati
69110 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 ng.** sequences
69120 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42 59 for the ORDER BY
69130 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 clause..*/.stru
69140 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 ct Select {. Ex
69150 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
69160 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c /* The fiel
69170 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ds of the result
69180 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 */. u8 op;
69190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
691a0 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e One of: TK_UNION
691b0 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 TK_ALL TK_INTER
691c0 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a SECT TK_EXCEPT *
691d0 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 /. char affinit
691e0 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 y; /* Ma
691f0 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 keRecord with th
69200 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 is affinity for
69210 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 SRT_Set */. u16
69220 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 selFlags;
69230 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 /* Various S
69240 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 F_* values */.
69250 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 SrcList *pSrc;
69260 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 /* The FR
69270 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 OM clause */. E
69280 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 xpr *pWhere;
69290 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 /* The WHE
692a0 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 RE clause */. E
692b0 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 xprList *pGroupB
692c0 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f y; /* The GRO
692d0 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a UP BY clause */.
692e0 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b Expr *pHaving;
692f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
69300 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f HAVING clause */
69310 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 . ExprList *pOr
69320 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 derBy; /* The
69330 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
69340 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 */. Select *pP
69350 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 rior; /*
69360 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 Prior select in
69370 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 a compound selec
69380 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 t statement */.
69390 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 Select *pNext;
693a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
693b0 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65 select to the le
693c0 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ft in a compound
693d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 */. Select *pR
693e0 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 ightmost; /*
693f0 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 Right-most selec
69400 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 t in a compound
69410 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 select statement
69420 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d */. Expr *pLim
69430 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 it; /*
69440 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e LIMIT expression
69450 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 . NULL means not
69460 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 used. */. Expr
69470 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20 *pOffset;
69480 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 /* OFFSET exp
69490 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 ression. NULL me
694a0 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f ans not used. */
694b0 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 . int iLimit, i
694c0 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d Offset; /* Mem
694d0 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 68 6f ory registers ho
694e0 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 lding LIMIT & OF
694f0 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f FSET counters */
69500 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 . int addrOpenE
69510 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f phm[3]; /* OP_
69520 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 OpenEphem opcode
69530 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69 s related to thi
69540 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a s select */.};..
69550 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
69560 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e lues for Select.
69570 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22 selFlags. The "
69580 53 46 22 20 70 72 65 66 69 78 20 73 74 61 6e 64 SF" prefix stand
69590 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 s for.** "Select
695a0 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 Flag"..*/.#defi
695b0 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20 ne SF_Distinct
695c0 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 0x0001 /*
695d0 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 Output should b
695e0 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 e DISTINCT */.#d
695f0 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 efine SF_Resolve
69600 64 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 d 0x0002
69610 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20 /* Identifiers
69620 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 have been resolv
69630 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 ed */.#define SF
69640 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 20 20 _Aggregate
69650 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 0x0004 /* Cont
69660 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 ains aggregate f
69670 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 unctions */.#def
69680 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d ine SF_UsesEphem
69690 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f eral 0x0008 /
696a0 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45 * Uses the OpenE
696b0 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 phemeral opcode
696c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78 */.#define SF_Ex
696d0 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30 78 panded 0x
696e0 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 0010 /* sqlite3
696f0 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63 SelectExpand() c
69700 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f alled on this */
69710 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54 .#define SF_HasT
69720 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30 ypeInfo 0x00
69730 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 20 /* FROM subq
69740 75 65 72 69 65 73 20 68 61 76 65 20 54 61 62 6c ueries have Tabl
69750 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a e metadata */...
69760 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 /*.** The result
69770 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61 s of a select ca
69780 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65 64 n be distributed
69790 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 in several ways
697a0 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 . The.** "SRT"
697b0 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45 prefix means "SE
697c0 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 70 65 LECT Result Type
697d0 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 "..*/.#define SR
697e0 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 T_Union 1
697f0 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c /* Store resul
69800 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 t as keys in an
69810 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
69820 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 SRT_Except
69830 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 2 /* Remove r
69840 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 esult from a UNI
69850 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 ON index */.#def
69860 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 ine SRT_Exists
69870 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 3 /* Store
69880 20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 1 if the result
69890 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f is not empty */
698a0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73 .#define SRT_Dis
698b0 63 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20 card 4 /*
698c0 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20 Do not save the
698d0 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65 results anywhere
698e0 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 */../* The ORDE
698f0 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 69 R BY clause is i
69900 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f gnored for all o
69910 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 f the above */.#
69920 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 define Ignorable
69930 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e Orderby(X) ((X->
69940 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 eDest)<=SRT_Disc
69950 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 ard)..#define SR
69960 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20 35 T_Output 5
69970 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68 /* Output each
69980 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a row of result *
69990 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 /.#define SRT_Me
699a0 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a m 6 /*
699b0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e Store result in
699c0 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a a memory cell *
699d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65 /.#define SRT_Se
699e0 74 20 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a t 7 /*
699f0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 Store results a
69a00 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 s keys in an ind
69a10 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 ex */.#define SR
69a20 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 38 T_Table 8
69a30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c /* Store resul
69a40 74 20 61 73 20 64 61 74 61 20 77 69 74 68 20 61 t as data with a
69a50 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 n automatic rowi
69a60 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 d */.#define SRT
69a70 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 20 _EphemTab 9
69a80 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73 /* Create trans
69a90 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f ient tab and sto
69aa0 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c re like SRT_Tabl
69ab0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 e */.#define SRT
69ac0 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20 _Coroutine 10
69ad0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 /* Generate a s
69ae0 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 ingle row of res
69af0 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 ult */../*.** A
69b00 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 74 structure used t
69b10 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 o customize the
69b20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 behavior of sqli
69b30 74 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65 te3Select(). See
69b40 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f .** comments abo
69b50 76 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ve sqlite3Select
69b60 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a () for details..
69b70 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
69b80 74 20 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c t SelectDest Sel
69b90 65 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20 ectDest;.struct
69ba0 53 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75 SelectDest {. u
69bb0 38 20 65 44 65 73 74 3b 20 20 20 20 20 20 20 20 8 eDest;
69bc0 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f /* How to dispo
69bd0 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 se of the result
69be0 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 s */. u8 affini
69bf0 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69 ty; /* Affi
69c00 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 nity used when e
69c10 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f Dest==SRT_Set */
69c20 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 . int iParm;
69c30 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 /* A parame
69c40 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20 ter used by the
69c50 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d eDest disposal m
69c60 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ethod */. int i
69c70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Mem; /*
69c80 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68 Base register wh
69c90 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 ere results are
69ca0 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 written */. int
69cb0 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f nMem; /
69cc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 * Number of regi
69cd0 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 sters allocated
69ce0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 */.};../*.** Dur
69cf0 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 ing code generat
69d00 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 ion of statement
69d10 73 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 s that do insert
69d20 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 s into AUTOINCRE
69d30 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c MENT .** tables,
69d40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
69d50 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 nformation is at
69d60 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 tached to the Ta
69d70 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a ble.u.autoInc.p.
69d80 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 ** pointer of ea
69d90 63 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 ch autoincrement
69da0 20 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 table to record
69db0 20 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 some side infor
69dc0 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 mation that.** t
69dd0 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f he code generato
69de0 72 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 r needs. We hav
69df0 65 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 e to keep per-ta
69e00 62 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e ble autoincremen
69e10 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e t.** information
69e20 20 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 in case inserts
69e30 20 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e are down within
69e40 20 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 triggers. Trig
69e50 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e gers do not.** n
69e60 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 ormally coordina
69e70 74 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74 te their activit
69e80 69 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e ies, but we do n
69e90 65 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 eed to coordinat
69ea0 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 e the.** loading
69eb0 20 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 and saving of a
69ec0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 utoincrement inf
69ed0 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 ormation..*/.str
69ee0 75 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 uct AutoincInfo
69ef0 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 {. AutoincInfo
69f00 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 *pNext; /* Nex
69f10 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 t info block in
69f20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 a list of them a
69f30 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 ll */. Table *p
69f40 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Tab; /*
69f50 20 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f Table this info
69f60 20 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f block refers to
69f70 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 */. int iDb;
69f80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
69f90 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e ndex in sqlite3.
69fa0 61 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 aDb[] of databas
69fb0 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a e holding pTab *
69fc0 2f 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 /. int regCtr;
69fd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
69fe0 6f 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c ory register hol
69ff0 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 ding the rowid c
6a000 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ounter */.};../*
6a010 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 .** Size of the
6a020 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a column cache.*/.
6a030 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e #ifndef SQLITE_N
6a040 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 _COLCACHE.# defi
6a050 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 ne SQLITE_N_COLC
6a060 41 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a ACHE 10.#endif..
6a070 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f /*.** At least o
6a080 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ne instance of t
6a090 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
6a0a0 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 ucture is create
6a0b0 64 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 d for each .** t
6a0c0 72 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 rigger that may
6a0d0 62 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 be fired while p
6a0e0 61 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 arsing an INSERT
6a0f0 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 , UPDATE or DELE
6a100 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e TE.** statement.
6a110 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 All such object
6a120 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
6a130 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
6a140 68 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 headed at.** Par
6a150 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 se.pTriggerPrg a
6a160 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 nd deleted once
6a170 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c statement compil
6a180 61 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a ation has been.*
6a190 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a * completed..**.
6a1a0 2a 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 ** A Vdbe sub-pr
6a1b0 6f 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 ogram that imple
6a1c0 6d 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 ments the body a
6a1d0 6e 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f nd WHEN clause o
6a1e0 66 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 f trigger.** Tri
6a1f0 67 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 ggerPrg.pTrigger
6a200 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 , assuming a def
6a210 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 ault ON CONFLICT
6a220 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 clause of.** Tr
6a230 69 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c iggerPrg.orconf,
6a240 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 is stored in th
6a250 65 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 e TriggerPrg.pPr
6a260 6f 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a ogram variable..
6a270 2a 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 ** The Parse.pTr
6a280 69 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 iggerPrg list ne
6a290 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f ver contains two
6a2a0 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 entries with th
6a2b0 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 e same.** values
6a2c0 20 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 for both pTrigg
6a2d0 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a er and orconf..*
6a2e0 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 *.** The Trigger
6a2f0 50 72 67 2e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20 Prg.aColmask[0]
6a300 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 variable is set
6a310 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64 to a mask of old
6a320 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63 .* columns.** ac
6a330 63 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74 cessed (or set t
6a340 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73 o 0 for triggers
6a350 20 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75 fired as a resu
6a360 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a lt of INSERT .**
6a370 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 20 53 69 statements). Si
6a380 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 54 72 69 milarly, the Tri
6a390 67 67 65 72 50 72 67 2e 61 43 6f 6c 6d 61 73 6b ggerPrg.aColmask
6a3a0 5b 31 5d 20 76 61 72 69 61 62 6c 65 20 69 73 20 [1] variable is
6a3b0 73 65 74 20 74 6f 0a 2a 2a 20 61 20 6d 61 73 6b set to.** a mask
6a3c0 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e of new.* column
6a3d0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72 s used by the pr
6a3e0 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 ogram..*/.struct
6a3f0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20 TriggerPrg {.
6a400 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
6a410 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 r; /* Trigg
6a420 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20 er this program
6a430 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a was coded from *
6a440 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 /. int orconf;
6a450 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
6a460 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 efault ON CONFLI
6a470 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53 CT policy */. S
6a480 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 ubProgram *pProg
6a490 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61 ram; /* Progra
6a4a0 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70 m implementing p
6a4b0 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a Trigger/orconf *
6a4c0 2f 0a 20 20 75 33 32 20 61 43 6f 6c 6d 61 73 6b /. u32 aColmask
6a4d0 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d [2]; /* M
6a4e0 61 73 6b 73 20 6f 66 20 6f 6c 64 2e 2a 2c 20 6e asks of old.*, n
6a4f0 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63 ew.* columns acc
6a500 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 essed */. Trigg
6a510 65 72 50 72 67 20 2a 70 4e 65 78 74 3b 20 20 20 erPrg *pNext;
6a520 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 /* Next entry
6a530 20 69 6e 20 50 61 72 73 65 2e 70 54 72 69 67 67 in Parse.pTrigg
6a540 65 72 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d 3b erPrg list */.};
6a550 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 70 ../*.** An SQL p
6a560 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 arser context.
6a570 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73 A copy of this s
6a580 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 tructure is pass
6a590 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 ed through.** th
6a5a0 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77 e parser and dow
6a5b0 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70 n into all the p
6a5c0 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 arser action rou
6a5d0 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f tine in order to
6a5e0 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64 .** carry around
6a5f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
6a600 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74 t is global to t
6a610 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e he entire parse.
6a620 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 .**.** The struc
6a630 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64 20 ture is divided
6a640 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e 20 into two parts.
6a650 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 When the parser
6a660 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 6e and code.** gen
6a670 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d 73 erate call thems
6a680 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65 6c elves recursivel
6a690 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 72 y, the first par
6a6a0 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 t of the structu
6a6b0 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 6e re.** is constan
6a6c0 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e 64 t but the second
6a6d0 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20 61 part is reset a
6a6e0 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
6a6f0 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 61 and end of.** ea
6a700 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a 2a ch recursion..**
6a710 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c 6f .** The nTableLo
6a720 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f 63 ck and aTableLoc
6a730 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 k variables are
6a740 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 only used if the
6a750 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a 2a shared-cache .*
6a760 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 * feature is ena
6a770 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65 33 bled (if sqlite3
6a780 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65 64 Tsd()->useShared
6a790 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20 54 Data is true). T
6a7a0 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 hey are.** used
6a7b0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 to store the set
6a7c0 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 of table-locks
6a7d0 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
6a7e0 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 0a statement being.
6a7f0 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 6e ** compiled. Fun
6a800 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 62 ction sqlite3Tab
6a810 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 64 leLock() is used
6a820 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73 20 to add entries
6a830 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e 0a to the.** list..
6a840 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65 20 */.struct Parse
6a850 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
6a860 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6a870 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 main database st
6a880 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 ructure */. int
6a890 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
6a8a0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
6a8b0 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20 from execution
6a8c0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d */. char *zErrM
6a8d0 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 sg; /* An
6a8e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f error message */
6a8f0 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 . Vdbe *pVdbe;
6a900 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6e /* An en
6a910 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 gine for executi
6a920 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74 65 ng database byte
6a930 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f 6c code */. u8 col
6a940 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20 2f NamesSet; /
6a950 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50 5f * TRUE after OP_
6a960 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 62 ColumnName has b
6a970 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70 56 een issued to pV
6a980 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d 65 dbe */. u8 name
6a990 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a Clash; /*
6a9a0 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 A permanent tab
6a9b0 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 20 le name clashes
6a9c0 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65 20 with temp table
6a9d0 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 65 name */. u8 che
6a9e0 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20 2f ckSchema; /
6a9f0 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61 20 * Causes schema
6aa00 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 74 cookie check aft
6aa10 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 er an error */.
6aa20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20 20 u8 nested;
6aa30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6aa40 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 of nested calls
6aa50 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63 6f to the parser/co
6aa60 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f 0a de generator */.
6aa70 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72 3b u8 parseError;
6aa80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 /* True a
6aa90 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20 65 fter a parsing e
6aaa0 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 rror. Ticket #1
6aab0 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 794 */. u8 nTem
6aac0 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a pReg; /*
6aad0 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 6f Number of tempo
6aae0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 69 rary registers i
6aaf0 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a n aTempReg[] */.
6ab00 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 3b u8 nTempInUse;
6ab10 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6ab20 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 63 of aTempReg[] c
6ab30 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 urrently checked
6ab40 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 54 out */. int aT
6ab50 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 2f empReg[8]; /
6ab60 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 66 * Holding area f
6ab70 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 or temporary reg
6ab80 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 isters */. int
6ab90 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20 nRangeReg;
6aba0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
6abb0 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
6abc0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
6abd0 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20 20 t iRangeReg;
6abe0 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 /* First regi
6abf0 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 72 ster in temporar
6ac00 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
6ac10 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 */. int nErr;
6ac20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
6ac30 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 mber of errors s
6ac40 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 een */. int nTa
6ac50 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b; /*
6ac60 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 69 Number of previ
6ac70 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
6ac80 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f 0a VDBE cursors */.
6ac90 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 int nMem;
6aca0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6acb0 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 of memory cells
6acc0 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
6acd0 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 20 int nSet;
6ace0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6acf0 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73 6f of sets used so
6ad00 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6b far */. int ck
6ad10 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f Base; /
6ad20 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 * Base register
6ad30 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20 63 of data during c
6ad40 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 heck constraints
6ad50 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65 */. int iCache
6ad60 4c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f Level; /* Co
6ad70 6c 43 61 63 68 65 20 76 61 6c 69 64 20 77 68 65 lCache valid whe
6ad80 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69 4c n aColCache[].iL
6ad90 65 76 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76 65 evel<=iCacheLeve
6ada0 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 l */. int iCach
6adb0 65 43 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 43 eCnt; /* C
6adc0 6f 75 6e 74 65 72 20 75 73 65 64 20 74 6f 20 67 ounter used to g
6add0 65 6e 65 72 61 74 65 20 61 43 6f 6c 43 61 63 68 enerate aColCach
6ade0 65 5b 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20 2a e[].lru values *
6adf0 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63 68 65 /. u8 nColCache
6ae00 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
6ae10 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
6ae20 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 the column cach
6ae30 65 20 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61 e */. u8 iColCa
6ae40 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e che; /* N
6ae50 65 78 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 ext entry of the
6ae60 20 63 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 cache to replac
6ae70 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 e */. struct yC
6ae80 6f 6c 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e olCache {. in
6ae90 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 t iTable;
6aea0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 /* Table cur
6aeb0 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 sor number */.
6aec0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
6aed0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
6aee0 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a column number *
6aef0 2f 0a 20 20 20 20 75 38 20 61 66 66 43 68 61 6e /. u8 affChan
6af00 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ge; /* T
6af10 72 75 65 20 69 66 20 74 68 69 73 20 72 65 67 69 rue if this regi
6af20 73 74 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 ster has had an
6af30 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 affinity change
6af40 2a 2f 0a 20 20 20 20 75 38 20 74 65 6d 70 52 65 */. u8 tempRe
6af50 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
6af60 69 52 65 67 20 69 73 20 61 20 74 65 6d 70 20 72 iReg is a temp r
6af70 65 67 69 73 74 65 72 20 74 68 61 74 20 6e 65 65 egister that nee
6af80 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a ds to be freed *
6af90 2f 0a 20 20 20 20 69 6e 74 20 69 4c 65 76 65 6c /. int iLevel
6afa0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
6afb0 65 73 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f 0a esting level */.
6afc0 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 int iReg;
6afd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
6afe0 20 77 69 74 68 20 76 61 6c 75 65 20 6f 66 20 74 with value of t
6aff0 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d 65 his column. 0 me
6b000 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20 20 ans none. */.
6b010 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 int lru;
6b020 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 73 74 20 /* Least
6b030 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 65 6e recently used en
6b040 74 72 79 20 68 61 73 20 74 68 65 20 73 6d 61 6c try has the smal
6b050 6c 65 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 lest value */.
6b060 7d 20 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c 49 } aColCache[SQLI
6b070 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b 20 TE_N_COLCACHE];
6b080 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 /* One for each
6b090 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e column cache en
6b0a0 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 77 72 69 try */. u32 wri
6b0b0 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a teMask; /*
6b0c0 20 53 74 61 72 74 20 61 20 77 72 69 74 65 20 74 Start a write t
6b0d0 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
6b0e0 65 73 65 20 64 61 74 61 62 61 73 65 73 20 2a 2f ese databases */
6b0f0 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 73 . u32 cookieMas
6b100 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 k; /* Bitma
6b110 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65 72 sk of schema ver
6b120 69 66 69 65 64 20 64 61 74 61 62 61 73 65 73 20 ified databases
6b130 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 57 */. u8 isMultiW
6b140 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 rite; /* Tru
6b150 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d e if statement m
6b160 61 79 20 61 66 66 65 63 74 2f 69 6e 73 65 72 74 ay affect/insert
6b170 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 2a multiple rows *
6b180 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74 3b /. u8 mayAbort;
6b190 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
6b1a0 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 if statement ma
6b1b0 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 y throw an ABORT
6b1c0 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 exception */.
6b1d0 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 int cookieGoto;
6b1e0 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
6b1f0 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f of OP_Goto to co
6b200 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73 75 okie verifier su
6b210 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e broutine */. in
6b220 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 t cookieValue[SQ
6b230 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 LITE_MAX_ATTACHE
6b240 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 D+2]; /* Values
6b250 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 of cookies to v
6b260 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 erify */.#ifndef
6b270 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
6b280 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 RED_CACHE. int
6b290 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 nTableLock;
6b2a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6b2b0 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c locks in aTableL
6b2c0 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f ock */. TableLo
6b2d0 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 ck *aTableLock;
6b2e0 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62 6c /* Required tabl
6b2f0 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 e locks for shar
6b300 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f ed-cache mode */
6b310 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 .#endif. int re
6b320 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f gRowid; /
6b330 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
6b340 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45 41 ng rowid of CREA
6b350 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20 2a TE TABLE entry *
6b360 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b /. int regRoot;
6b370 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 /* Regi
6b380 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f ster holding roo
6b390 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f t page number fo
6b3a0 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f r new objects */
6b3b0 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a . AutoincInfo *
6b3c0 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f 72 pAinc; /* Infor
6b3d0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 54 mation about AUT
6b3e0 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 OINCREMENT count
6b3f0 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 ers */. int nMa
6b400 78 41 72 67 3b 20 20 20 20 20 20 20 20 20 2f 2a xArg; /*
6b410 20 4d 61 78 20 61 72 67 73 20 70 61 73 73 65 64 Max args passed
6b420 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f to user functio
6b430 6e 20 62 79 20 73 75 62 2d 70 72 6f 67 72 61 6d n by sub-program
6b440 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72 6d */.. /* Inform
6b450 61 74 69 6f 6e 20 75 73 65 64 20 77 68 69 6c 65 ation used while
6b460 20 63 6f 64 69 6e 67 20 74 72 69 67 67 65 72 20 coding trigger
6b470 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 50 programs. */. P
6b480 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 3b arse *pToplevel;
6b490 20 20 20 20 2f 2a 20 50 61 72 73 65 20 73 74 72 /* Parse str
6b4a0 75 63 74 75 72 65 20 66 6f 72 20 6d 61 69 6e 20 ucture for main
6b4b0 70 72 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c 4c program (or NULL
6b4c0 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 ) */. Table *pT
6b4d0 72 69 67 67 65 72 54 61 62 3b 20 20 2f 2a 20 54 riggerTab; /* T
6b4e0 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 61 72 able triggers ar
6b4f0 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 66 6f e being coded fo
6b500 72 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 r */. u32 oldma
6b510 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d sk; /* M
6b520 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c ask of old.* col
6b530 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20 umns referenced
6b540 2a 2f 0a 20 20 75 33 32 20 6e 65 77 6d 61 73 6b */. u32 newmask
6b550 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 ; /* Mas
6b560 6b 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d k of new.* colum
6b570 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f ns referenced */
6b580 0a 20 20 75 38 20 65 54 72 69 67 67 65 72 4f 70 . u8 eTriggerOp
6b590 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50 ; /* TK_UP
6b5a0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 DATE, TK_INSERT
6b5b0 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a or TK_DELETE */.
6b5c0 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20 u8 eOrconf;
6b5d0 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c /* Defaul
6b5e0 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f t ON CONFLICT po
6b5f0 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72 licy for trigger
6b600 20 73 74 65 70 73 20 2a 2f 0a 20 20 75 38 20 64 steps */. u8 d
6b610 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 20 isableTriggers;
6b620 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 73 61 /* True to disa
6b630 62 6c 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a ble triggers */.
6b640 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 . /* Above is c
6b650 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20 onstant between
6b660 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c recursions. Bel
6b670 6f 77 20 69 73 20 72 65 73 65 74 20 62 65 66 6f ow is reset befo
6b680 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a re and after. *
6b690 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e * each recursion
6b6a0 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b */.. int nVar;
6b6b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6b6c0 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72 umber of '?' var
6b6d0 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74 iables seen in t
6b6e0 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f he SQL so far */
6b6f0 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b . int nVarExpr;
6b700 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6b710 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20 r of used slots
6b720 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a in apVarExpr[] *
6b730 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 /. int nVarExpr
6b740 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62 Alloc; /* Numb
6b750 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 er of allocated
6b760 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 slots in apVarEx
6b770 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a pr[] */. Expr *
6b780 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f *apVarExpr; /
6b790 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61 * Pointers to :a
6b7a0 61 61 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c aa and $aaaa wil
6b7b0 64 63 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e dcard expression
6b7c0 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 52 65 s */. Vdbe *pRe
6b7d0 70 72 65 70 61 72 65 3b 20 20 20 20 2f 2a 20 56 prepare; /* V
6b7e0 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61 72 M being reprepar
6b7f0 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72 65 ed (sqlite3Repre
6b800 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e 74 pare()) */. int
6b810 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 nAlias;
6b820 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
6b830 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65 liased result se
6b840 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 t columns */. i
6b850 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 nt nAliasAlloc;
6b860 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6b870 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 allocated slots
6b880 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f for aAlias[] */
6b890 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 . int *aAlias;
6b8a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
6b8b0 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ter used to hold
6b8c0 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 aliased result
6b8d0 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b */. u8 explain;
6b8e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6b8f0 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e e if the EXPLAIN
6b900 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f flag is found o
6b910 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 n the query */.
6b920 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 Token sNameToke
6b930 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 n; /* Token w
6b940 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 ith unqualified
6b950 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 schema object na
6b960 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c me */. Token sL
6b970 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 astToken; /*
6b980 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 The last token p
6b990 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 arsed */. const
6b9a0 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 char *zTail;
6b9b0 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 /* All SQL text
6b9c0 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 past the last se
6b9d0 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a micolon parsed *
6b9e0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 /. Table *pNewT
6b9f0 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 able; /* A ta
6ba00 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 ble being constr
6ba10 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20 ucted by CREATE
6ba20 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 TABLE */. Trigg
6ba30 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b er *pNewTrigger;
6ba40 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 /* Trigger
6ba50 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 under construct
6ba60 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47 by a CREATE TRIG
6ba70 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 GER */. const c
6ba80 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 har *zAuthContex
6ba90 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 t; /* The 6th pa
6baa0 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 rameter to db->x
6bab0 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a Auth callbacks *
6bac0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
6bad0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
6bae0 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b LE. Token sArg;
6baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bb00 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6bb10 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 of a module arg
6bb20 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 ument */. u8 de
6bb30 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 clareVtab;
6bb40 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6bb50 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f inside sqlite3_
6bb60 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a declare_vtab() *
6bb70 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 /. int nVtabLoc
6bb80 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
6bb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 * Number of virt
6bba0 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f ual tables to lo
6bbb0 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a ck */. Table **
6bbc0 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 apVtabLock;
6bbd0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
6bbe0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
6bbf0 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 needing locking
6bc00 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 */.#endif. int
6bc10 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 nHeight;
6bc20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
6bc30 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 n tree height of
6bc40 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c current sub-sel
6bc50 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ect */. Table *
6bc60 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 pZombieTab;
6bc70 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c /* List of Tabl
6bc80 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c e objects to del
6bc90 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 ete after code g
6bca0 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 en */. TriggerP
6bcb0 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b rg *pTriggerPrg;
6bcc0 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 /* Linked li
6bcd0 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67 st of coded trig
6bce0 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 gers */.};..#ifd
6bcf0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
6bd00 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 IRTUALTABLE. #d
6bd10 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 efine IN_DECLARE
6bd20 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 _VTAB 0.#else.
6bd30 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 #define IN_DECLA
6bd40 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d RE_VTAB (pParse-
6bd50 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 >declareVtab).#e
6bd60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 ndif../*.** An i
6bd70 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
6bd80 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
6bd90 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 re can be declar
6bda0 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e ed on a stack an
6bdb0 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 d used.** to sav
6bdc0 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 e the Parse.zAut
6bdd0 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 hContext value s
6bde0 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
6bdf0 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e restored later.
6be00 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 .*/.struct AuthC
6be10 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 ontext {. const
6be20 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 char *zAuthCont
6be30 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 ext; /* Put sa
6be40 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 ved Parse.zAuthC
6be50 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 ontext here */.
6be60 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
6be70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6be80 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74 The Parse struct
6be90 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ure */.};../*.**
6bea0 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 Bitfield flags
6beb0 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20 for P5 value in
6bec0 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 OP_Insert and OP
6bed0 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 _Delete.*/.#defi
6bee0 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 ne OPFLAG_NCHANG
6bef0 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 E 0x01
6bf00 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 /* Set to update
6bf10 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a db->nChange */.
6bf20 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c #define OPFLAG_L
6bf30 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 ASTROWID 0x0
6bf40 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 2 /* Set to u
6bf50 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f pdate db->lastRo
6bf60 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f wid */.#define O
6bf70 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 PFLAG_ISUPDATE
6bf80 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 0x04 /* T
6bf90 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 his OP_Insert is
6bfa0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a an sql UPDATE *
6bfb0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 /.#define OPFLAG
6bfc0 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 _APPEND 0
6bfd0 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 x08 /* This i
6bfe0 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 s likely to be a
6bff0 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 n append */.#def
6c000 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 ine OPFLAG_USESE
6c010 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 EKRESULT 0x10
6c020 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 /* Try to avoid
6c030 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 a seek in Btree
6c040 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 Insert() */.#def
6c050 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 ine OPFLAG_CLEAR
6c060 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20 CACHE 0x20
6c070 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f /* Clear pseudo
6c080 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 -table cache in
6c090 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a OP_Column */../*
6c0a0 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 . * Each trigger
6c0b0 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 present in the
6c0c0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
6c0d0 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 is stored as an
6c0e0 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 instance of. * s
6c0f0 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a truct Trigger. .
6c100 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 *. * Pointers t
6c110 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 o instances of s
6c120 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72 truct Trigger ar
6c130 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 e stored in two
6c140 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 ways.. * 1. In t
6c150 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61 he "trigHash" ha
6c160 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f sh table (part o
6c170 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 f the sqlite3* t
6c180 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 hat represents t
6c190 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 he . * databa
6c1a0 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 se). This allows
6c1b0 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75 Trigger structu
6c1c0 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 res to be retrie
6c1d0 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 ved by name.. *
6c1e0 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 2. All triggers
6c1f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
6c200 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 a single table f
6c210 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 orm a linked lis
6c220 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 t, using the. *
6c230 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 pNext member
6c240 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 of struct Trigge
6c250 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 r. A pointer to
6c260 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e the first elemen
6c270 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c t of the. * l
6c280 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 inked list is st
6c290 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72 ored as the "pTr
6c2a0 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 igger" member of
6c2b0 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a the associated.
6c2c0 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62 * struct Tab
6c2d0 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 le.. *. * The "s
6c2e0 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 tep_list" member
6c2f0 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
6c300 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6c310 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a a linked list. *
6c320 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
6c330 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 SQL statements s
6c340 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 pecified as the
6c350 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
6c360 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 . */.struct Trig
6c370 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e ger {. char *zN
6c380 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
6c390 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 /* The name of t
6c3a0 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20 he trigger
6c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c3c0 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 */. char *tab
6c3d0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f le; /
6c3e0 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 * The table or v
6c3f0 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 iew to which the
6c400 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 trigger applies
6c410 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 */. u8 op;
6c420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6c430 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
6c440 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
6c450 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 _INSERT
6c460 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 */. u8 tr_tm;
6c470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6c480 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 One of TRIGGER_B
6c490 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 EFORE, TRIGGER_A
6c4a0 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a FTER */. Expr *
6c4b0 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 pWhen;
6c4c0 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c /* The WHEN cl
6c4d0 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 ause of the expr
6c4e0 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e ession (may be N
6c4f0 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 ULL) */. IdList
6c500 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 *pColumns;
6c510 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
6c520 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f an UPDATE OF <co
6c530 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 lumn-list> trigg
6c540 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 er,.
6c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c560 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 the <column-lis
6c570 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 t> is stored her
6c580 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 e */. Schema *p
6c590 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f Schema; /
6c5a0 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e * Schema contain
6c5b0 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 ing the trigger
6c5c0 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 */. Schema *pTa
6c5d0 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 bSchema; /*
6c5e0 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e Schema containin
6c5f0 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 g the table */.
6c600 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 TriggerStep *st
6c610 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b ep_list; /* Link
6c620 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 list of trigger
6c630 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 program steps
6c640 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
6c650 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 Trigger *pNext;
6c660 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
6c670 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 trigger associat
6c680 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c ed with the tabl
6c690 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
6c6a0 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68 trigger is eith
6c6b0 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 er a BEFORE or a
6c6c0 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e n AFTER trigger.
6c6d0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
6c6e0 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 constants.** det
6c6f0 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a ermine which. .*
6c700 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 *.** If there ar
6c710 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 e multiple trigg
6c720 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f ers, you might o
6c730 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e f some BEFORE an
6c740 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a d some AFTER..**
6c750 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 In that cases,
6c760 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 the constants be
6c770 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 low can be ORed
6c780 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 together..*/.#de
6c790 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 fine TRIGGER_BEF
6c7a0 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 ORE 1.#define T
6c7b0 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 RIGGER_AFTER 2
6c7c0 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 ../*. * An insta
6c7d0 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 nce of struct Tr
6c7e0 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65 iggerStep is use
6c7f0 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e d to store a sin
6c800 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e gle SQL statemen
6c810 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 t. * that is a p
6c820 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 art of a trigger
6c830 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a -program. . *. *
6c840 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 Instances of st
6c850 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 ruct TriggerStep
6c860 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 are stored in a
6c870 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c singly linked l
6c880 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 ist (linked. * u
6c890 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 sing the "pNext"
6c8a0 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e member) referen
6c8b0 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70 ced by the "step
6c8c0 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 _list" member of
6c8d0 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 the . * associa
6c8e0 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67 ted struct Trigg
6c8f0 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 er instance. The
6c900 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
6c910 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 f the linked lis
6c920 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 t is. * the firs
6c930 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 t step of the tr
6c940 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 igger-program..
6c950 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d * . * The "op" m
6c960 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 ember indicates
6c970 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 whether this is
6c980 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 a "DELETE", "INS
6c990 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f ERT", "UPDATE" o
6c9a0 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 r. * "SELECT" st
6c9b0 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 atement. The mea
6c9c0 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 nings of the oth
6c9d0 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 er members is de
6c9e0 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
6c9f0 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 . * value of "op
6ca00 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a " as follows:. *
6ca10 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e . * (op == TK_IN
6ca20 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 SERT). * orconf
6ca30 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 -> stores the
6ca40 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 ON CONFLICT alg
6ca50 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 orithm. * pSelec
6ca60 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 t -> If this i
6ca70 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
6ca80 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 ... SELECT ...
6ca90 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a statement, then.
6caa0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6cab0 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f this stores a po
6cac0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c inter to the SEL
6cad0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f ECT statement. O
6cae0 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6caf0 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 * target -> A
6cb00 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 token holding t
6cb10 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f he quoted name o
6cb20 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 f the table to i
6cb30 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 nsert into.. * p
6cb40 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 ExprList -> If t
6cb50 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 his is an INSERT
6cb60 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 INTO ... VALUES
6cb70 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 ... statement,
6cb80 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 then. *
6cb90 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 this stores
6cba0 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e values to be in
6cbb0 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 serted. Otherwis
6cbc0 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 e NULL.. * pIdLi
6cbd0 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 st -> If this
6cbe0 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 is an INSERT INT
6cbf0 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e O ... (<column-n
6cc00 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e ames>) VALUES ..
6cc10 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 . . *
6cc20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 statement, th
6cc30 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 en this stores t
6cc40 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 he column-names
6cc50 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 to be. *
6cc60 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 inserted i
6cc70 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d nto.. *. * (op =
6cc80 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 = TK_DELETE). *
6cc90 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
6cca0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
6ccb0 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 quoted name of
6ccc0 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c the table to del
6ccd0 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 ete from.. * pWh
6cce0 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 ere -> The WH
6ccf0 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 ERE clause of th
6cd00 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 e DELETE stateme
6cd10 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 nt if one is spe
6cd20 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 cified.. *
6cd30 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 Otherwis
6cd40 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 e NULL.. * . * (
6cd50 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 op == TK_UPDATE)
6cd60 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e . * target ->
6cd70 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 A token holding
6cd80 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 the quoted name
6cd90 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
6cda0 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e update rows of.
6cdb0 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e . * pWhere ->
6cdc0 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
6cdd0 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 e of the UPDATE
6cde0 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 statement if one
6cdf0 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20 is specified..
6ce00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f * O
6ce10 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6ce20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 * pExprList -> A
6ce30 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c list of the col
6ce40 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61 umns to update a
6ce50 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f nd the expressio
6ce60 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 ns to update. *
6ce70 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
6ce80 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 m to. See sqlite
6ce90 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65 3Update() docume
6cea0 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 ntation of "pCha
6ceb0 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20 nges". *
6cec0 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a argument..
6ced0 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 * . */.struct T
6cee0 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75 riggerStep {. u
6cef0 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 8 op;
6cf00 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b /* One of TK
6cf10 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 _DELETE, TK_UPDA
6cf20 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 TE, TK_INSERT, T
6cf30 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 K_SELECT */. u8
6cf40 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 orconf;
6cf50 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 /* OE_Rollbac
6cf60 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 k etc. */. Trig
6cf70 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20 ger *pTrig;
6cf80 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 /* The trigger
6cf90 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69 that this step i
6cfa0 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 s a part of */.
6cfb0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
6cfc0 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 ; /* SELECT
6cfd0 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 statment or RHS
6cfe0 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e of INSERT INTO .
6cff0 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a . SELECT ... */.
6d000 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 Token target;
6d010 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 /* Target
6d020 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 table for DELET
6d030 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 E, UPDATE, INSER
6d040 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 T */. Expr *pWh
6d050 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ere; /* T
6d060 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
6d070 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 for DELETE or UP
6d080 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 DATE steps */.
6d090 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c ExprList *pExprL
6d0a0 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 ist; /* SET clau
6d0b0 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20 se for UPDATE.
6d0c0 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f VALUES clause fo
6d0d0 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 r INSERT */. Id
6d0e0 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 List *pIdList;
6d0f0 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d /* Column nam
6d100 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f es for INSERT */
6d110 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
6d120 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
6d130 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 in the link-list
6d140 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 */. TriggerSte
6d150 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 p *pLast; /* La
6d160 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 st element in li
6d170 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 nk-list. Valid f
6d180 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 or 1st elem only
6d190 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
6d1a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
6d1b0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 cture contains i
6d1c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 nformation used
6d1d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 by the sqliteFix
6d1e0 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ....** routines
6d1f0 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 as they walk the
6d200 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d parse tree to m
6d210 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66 ake database ref
6d220 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 erences.** expli
6d230 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 cit. .*/.typede
6d240 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72 f struct DbFixer
6d250 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 DbFixer;.struct
6d260 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 DbFixer {. Par
6d270 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
6d280 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 /* The parsing
6d290 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 context. Error
6d2a0 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e messages written
6d2b0 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 here */. const
6d2c0 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f char *zDb; /
6d2d0 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 * Make sure all
6d2e0 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 objects are cont
6d2f0 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 ained in this da
6d300 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 tabase */. cons
6d310 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 t char *zType;
6d320 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 /* Type of the c
6d330 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 ontainer - used
6d340 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 for error messag
6d350 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f es */. const To
6d360 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e ken *pName; /* N
6d370 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 ame of the conta
6d380 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 iner - used for
6d390 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
6d3a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f /.};../*.** An o
6d3b0 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 bjected used to
6d3c0 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 accumulate the t
6d3d0 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 ext of a string
6d3e0 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e where we.** do n
6d3f0 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b ot necessarily k
6d400 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 now how big the
6d410 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 string will be i
6d420 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 n the end..*/.st
6d430 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a ruct StrAccum {.
6d440 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6d450 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
6d460 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 al database for
6d470 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 lookaside. Can
6d480 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 be NULL */. cha
6d490 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 r *zBase;
6d4a0 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f /* A base allo
6d4b0 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f cation. Not fro
6d4c0 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 m malloc. */. c
6d4d0 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 har *zText;
6d4e0 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e /* The strin
6d4f0 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 g collected so f
6d500 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 ar */. int nCh
6d510 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ar; /*
6d520 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 Length of the st
6d530 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ring so far */.
6d540 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 int nAlloc;
6d550 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 /* Amount
6d560 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 of space allocat
6d570 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 ed in zText */.
6d580 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 int mxAlloc;
6d590 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
6d5a0 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 allowed string
6d5b0 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 length */. u8
6d5c0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 mallocFailed;
6d5d0 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 /* Becomes true
6d5e0 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 if any memory a
6d5f0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 llocation fails
6d600 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c */. u8 useMal
6d610 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 loc; /* Tru
6d620 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e e if zText is en
6d630 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 largeable using
6d640 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 realloc */. u8
6d650 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 tooBig;
6d660 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 /* Becomes tru
6d670 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 e if string size
6d680 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 exceeds limits
6d690 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 */.};../*.** A p
6d6a0 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
6d6b0 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 tructure is used
6d6c0 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 to communicate
6d6d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 information.** f
6d6e0 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 rom sqlite3Init
6d6f0 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 and OP_ParseSche
6d700 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 ma into the sqli
6d710 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e te3InitCallback.
6d720 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
6d730 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a ct {. sqlite3 *
6d740 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 db; /* Th
6d750 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 e database being
6d760 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6d770 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
6d780 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d /* 0 for m
6d790 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 ain database. 1
6d7a0 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 for TEMP, 2.. f
6d7b0 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 or ATTACHed */.
6d7c0 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 char **pzErrMsg
6d7d0 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 ; /* Error me
6d7e0 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 ssage stored her
6d7f0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
6d800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6d810 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 sult code stored
6d820 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 here */.} InitD
6d830 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 ata;../*.** Stru
6d840 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cture containing
6d850 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 global configur
6d860 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 ation data for t
6d870 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
6d880 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 y..**.** This st
6d890 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e ructure also con
6d8a0 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 tains some state
6d8b0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
6d8c0 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 .struct Sqlite3C
6d8d0 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d onfig {. int bM
6d8e0 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 emstat;
6d8f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6d900 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 rue to enable me
6d910 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 mory status */.
6d920 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b int bCoreMutex;
6d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d940 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e /* True to en
6d950 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 able core mutexi
6d960 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c ng */. int bFul
6d970 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 lMutex;
6d980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6d990 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c e to enable full
6d9a0 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 mutexing */. i
6d9b0 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 nt mxStrlen;
6d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 /* Maximum stri
6d9e0 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 ng length */. i
6d9f0 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 nt szLookaside;
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da10 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b /* Default look
6da20 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a aside buffer siz
6da30 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b e */. int nLook
6da40 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 aside;
6da50 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 /* Defa
6da60 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ult lookaside bu
6da70 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 ffer count */.
6da80 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6da90 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 ods m;
6daa0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6dab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
6dac0 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 interface */.
6dad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
6dae0 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 thods mutex;
6daf0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6db00 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a utex interface *
6db10 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /. sqlite3_pcac
6db20 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 he_methods pcach
6db30 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 e; /* Low-lev
6db40 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e el page-cache in
6db50 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 terface */. voi
6db60 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 d *pHeap;
6db70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6db80 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 * Heap storage s
6db90 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 pace */. int nH
6dba0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
6dbb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
6dbc0 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a ize of pHeap[] *
6dbd0 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d /. int mnReq, m
6dbe0 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 xReq;
6dbf0 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 /* Min and
6dc00 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 max heap reques
6dc10 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f ts sizes */. vo
6dc20 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 id *pScratch;
6dc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dc40 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 /* Scratch memor
6dc50 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 y */. int szScr
6dc60 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
6dc70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6dc80 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 of each scratch
6dc90 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 buffer */. int
6dca0 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 nScratch;
6dcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6dcc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 * Number of scra
6dcd0 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 tch buffers */.
6dce0 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 void *pPage;
6dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd00 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 /* Page cache
6dd10 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
6dd20 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 szPage;
6dd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6dd40 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 * Size of each p
6dd50 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a age in pPage[] *
6dd60 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 /. int nPage;
6dd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6dd90 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 of pages in pPag
6dda0 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 e[] */. int mxP
6ddb0 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 arserStack;
6ddc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 /* ma
6ddd0 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 ximum depth of t
6dde0 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 he parser stack
6ddf0 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 */. int sharedC
6de00 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 acheEnabled;
6de10 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 /* true i
6de20 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d f shared-cache m
6de30 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 ode enabled */.
6de40 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 /* The above mi
6de50 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a ght be initializ
6de60 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 ed to non-zero.
6de70 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e The following n
6de80 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 eed to always.
6de90 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 ** initially be
6dea0 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a zero, however. *
6deb0 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 /. int isInit;
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ded0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 /* True af
6dee0 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ter initializati
6def0 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 on has finished
6df00 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 */. int inProgr
6df10 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ess;
6df20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 /* True w
6df30 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 hile initializat
6df40 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 ion in progress
6df50 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78 */. int isMutex
6df60 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
6df70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 /* True a
6df80 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65 fter mutexes are
6df90 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6dfa0 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e int isMallocIn
6dfb0 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
6dfc0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
6dfd0 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 r malloc is init
6dfe0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
6dff0 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20 isPCacheInit;
6e000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6e010 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
6e020 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
6e030 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ed */. sqlite3_
6e040 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 mutex *pInitMute
6e050 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 x; /* Mut
6e060 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 ex used by sqlit
6e070 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
6e080 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 */. int nRefIni
6e090 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 tMutex;
6e0a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6e0b0 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e of users of pIn
6e0c0 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f itMutex */.};../
6e0d0 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 *.** Context poi
6e0e0 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e nter passed down
6e0f0 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 through the tre
6e100 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 e-walk..*/.struc
6e110 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 t Walker {. int
6e120 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b (*xExprCallback
6e130 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a )(Walker*, Expr*
6e140 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 ); /* Callba
6e150 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ck for expressio
6e160 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 ns */. int (*xS
6e170 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 electCallback)(W
6e180 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b alker*,Select*);
6e190 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f /* Callback fo
6e1a0 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 r SELECTs */. P
6e1b0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
6e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e1d0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
6e1e0 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a er context. */.
6e1f0 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 union {
6e200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
6e220 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 xtra data for ca
6e230 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 llback */. Na
6e240 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 meContext *pNC;
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e260 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 /* Nami
6e270 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
6e280 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
6e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6e2b0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f Integer value */
6e2c0 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 . } u;.};../* F
6e2d0 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
6e2e0 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ons */.SQLITE_PR
6e2f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6e300 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 3WalkExpr(Walker
6e310 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6e320 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6e330 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
6e340 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c t(Walker*, ExprL
6e350 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6e360 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6e370 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 3WalkSelect(Walk
6e380 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 er*, Select*);.S
6e390 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6e3a0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
6e3b0 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c ectExpr(Walker*,
6e3c0 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 Select*);.SQLIT
6e3d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6e3e0 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
6e3f0 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c rom(Walker*, Sel
6e400 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 ect*);../*.** Re
6e410 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 turn code from t
6e420 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 he parse-tree wa
6e430 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 lking primitives
6e440 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 and their.** ca
6e450 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 llbacks..*/.#def
6e460 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 ine WRC_Continue
6e470 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 0 /* Conti
6e480 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 nue down into ch
6e490 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e ildren */.#defin
6e4a0 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 e WRC_Prune
6e4b0 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 1 /* Omit ch
6e4c0 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 ildren but conti
6e4d0 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c nue walking sibl
6e4e0 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ings */.#define
6e4f0 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
6e500 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 2 /* Abandon t
6e510 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a he tree walk */.
6e520 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ./*.** Assuming
6e530 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 zIn points to th
6e540 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6e550 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 a UTF-8 characte
6e560 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 r,.** advance zI
6e570 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 n to point to th
6e580 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6e590 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 the next UTF-8 c
6e5a0 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 haracter..*/.#de
6e5b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 fine SQLITE_SKIP
6e5c0 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 _UTF8(zIn) {
6e5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e5e0 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a \. if( (*(z
6e5f0 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 In++))>=0xc0 ){
6e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e610 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
6e620 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 while( (*zIn
6e630 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
6e640 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 zIn++; }
6e650 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
6e660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
6e690 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 .}../*.** The SQ
6e6a0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6e6b0 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 T macro can be e
6e6c0 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 ither a constant
6e6d0 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e (for production
6e6e0 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 .** builds) or a
6e6f0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 function call (
6e700 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 for debugging).
6e710 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 If it is a func
6e720 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 tion call,.** it
6e730 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 allows the oper
6e740 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 ator to set a br
6e750 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 eakpoint at the
6e760 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 spot where datab
6e770 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f ase.** corruptio
6e780 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63 n is first detec
6e790 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ted..*/.#ifdef S
6e7a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
6e7b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
6e7c0 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
6e7d0 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 void);.# define
6e7e0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
6e7f0 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 KPT sqlite3Corru
6e800 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 pt().#else.# def
6e810 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ine SQLITE_CORRU
6e820 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 PT_BKPT SQLITE_C
6e830 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f ORRUPT.#endif../
6e840 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 *.** The ctype.h
6e850 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 header is neede
6e860 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 d for non-ASCII
6e870 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 systems. It is
6e880 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 also.** needed b
6e890 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33 y FTS3 when FTS3
6e8a0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 is included in
6e8b0 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
6e8c0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
6e8d0 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20 d(SQLITE_ASCII)
6e8e0 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 || \. (define
6e8f0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
6e900 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64 FTS3) && defined
6e910 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 (SQLITE_AMALGAMA
6e920 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 TION)).# include
6e930 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 <ctype.h>.#endi
6e940 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
6e950 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 lowing macros mi
6e960 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64 mic the standard
6e970 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
6e980 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a ns toupper(),.**
6e990 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c isspace(), isal
6e9a0 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29 num(), isdigit()
6e9b0 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c and isxdigit(),
6e9c0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
6e9d0 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 he.** sqlite ver
6e9e0 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 sions only work
6e9f0 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63 for ASCII charac
6ea00 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73 ters, regardless
6ea10 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 of locale..*/.#
6ea20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 ifdef SQLITE_ASC
6ea30 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 II.# define sqli
6ea40 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28 te3Toupper(x) (
6ea50 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79 (x)&~(sqlite3Cty
6ea60 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 peMap[(unsigned
6ea70 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29 char)(x)]&0x20))
6ea80 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6ea90 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73 3Isspace(x) (s
6eaa0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6eab0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6eac0 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e )]&0x01).# defin
6ead0 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d e sqlite3Isalnum
6eae0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6eaf0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6eb00 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 char)(x)]&0x06)
6eb10 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6eb20 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73 3Isalpha(x) (s
6eb30 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6eb40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6eb50 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e )]&0x02).# defin
6eb60 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 e sqlite3Isdigit
6eb70 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6eb80 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6eb90 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29 char)(x)]&0x04)
6eba0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6ebb0 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73 3Isxdigit(x) (s
6ebc0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6ebd0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6ebe0 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e )]&0x08).# defin
6ebf0 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 e sqlite3Tolower
6ec00 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70 (x) (sqlite3Up
6ec10 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 perToLower[(unsi
6ec20 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a gned char)(x)]).
6ec30 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
6ec40 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 qlite3Toupper(x)
6ec50 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 toupper((unsi
6ec60 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
6ec70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6ec80 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70 sspace(x) issp
6ec90 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 ace((unsigned ch
6eca0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
6ecb0 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 sqlite3Isalnum(
6ecc0 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e x) isalnum((un
6ecd0 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
6ece0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6ecf0 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 3Isalpha(x) is
6ed00 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 alpha((unsigned
6ed10 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
6ed20 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 ne sqlite3Isdigi
6ed30 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28 t(x) isdigit((
6ed40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6ed50 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )).# define sqli
6ed60 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 te3Isxdigit(x)
6ed70 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e isxdigit((unsign
6ed80 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6ed90 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c efine sqlite3Tol
6eda0 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 ower(x) tolowe
6edb0 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 r((unsigned char
6edc0 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a )(x)).#endif../*
6edd0 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e .** Internal fun
6ede0 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 ction prototypes
6edf0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
6ee00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
6ee10 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 rICmp(const char
6ee20 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
6ee30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ee40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e E int sqlite3IsN
6ee50 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 umber(const char
6ee60 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 *, int*, u8);.SQ
6ee70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ee80 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
6ee90 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 (const char*);.#
6eea0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6eeb0 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 rNICmp sqlite3_s
6eec0 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f trnicmp..SQLITE_
6eed0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6eee0 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f te3MallocInit(vo
6eef0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
6ef00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6ef10 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b MallocEnd(void);
6ef20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ef30 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c void *sqlite3Mal
6ef40 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
6ef50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6ef60 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
6ef70 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
6ef80 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6ef90 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
6efa0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a sqlite3*, int);.
6efb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6efc0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 oid *sqlite3DbMa
6efd0 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a llocRaw(sqlite3*
6efe0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6eff0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6f000 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c ite3DbStrDup(sql
6f010 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
6f020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f030 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
6f040 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 DbStrNDup(sqlite
6f050 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
6f060 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f070 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6f080 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c e3Realloc(void*,
6f090 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6f0a0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6f0b0 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
6f0c0 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f ee(sqlite3 *, vo
6f0d0 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 id *, int);.SQLI
6f0e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f0f0 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f *sqlite3DbReallo
6f100 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 c(sqlite3 *, voi
6f110 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 d *, int);.SQLIT
6f120 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f130 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c qlite3DbFree(sql
6f140 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
6f150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6f160 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 t sqlite3MallocS
6f170 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 ize(void*);.SQLI
6f180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f190 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
6f1a0 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 ze(sqlite3*, voi
6f1b0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
6f1c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6f1d0 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 3ScratchMalloc(i
6f1e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f1f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f200 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 ScratchFree(void
6f210 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f220 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6f230 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b PageMalloc(int);
6f240 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f250 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
6f260 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c Free(void*);.SQL
6f270 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f280 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
6f290 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c fault(void);.SQL
6f2a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f2b0 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 sqlite3BenignMa
6f2c0 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 llocHooks(void (
6f2d0 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 *)(void), void (
6f2e0 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 *)(void));.SQLIT
6f2f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6f300 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
6f310 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c (void (*)(void*,
6f320 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 sqlite3_int64,
6f330 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c int), void*, sql
6f340 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a ite3_int64);../*
6f350 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 .** On systems w
6f360 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 ith ample stack
6f370 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73 space and that s
6f380 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c upport alloca(),
6f390 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 make.** use of
6f3a0 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 alloca() to obta
6f3b0 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72 in space for lar
6f3c0 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a ge automatic obj
6f3d0 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c ects. By defaul
6f3e0 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 t,.** obtain spa
6f3f0 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ce from malloc()
6f400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f ..**.** The allo
6f410 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 ca() routine nev
6f420 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e er returns NULL.
6f430 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 This will caus
6f440 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 e code paths.**
6f450 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 that deal with s
6f460 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 qlite3StackAlloc
6f470 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 () failures to b
6f480 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a e unreachable..*
6f490 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
6f4a0 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 USE_ALLOCA.# def
6f4b0 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b ine sqlite3Stack
6f4c0 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 AllocRaw(D,N)
6f4d0 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 alloca(N).# defi
6f4e0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 ne sqlite3StackA
6f4f0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d llocZero(D,N) m
6f500 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c emset(alloca(N),
6f510 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 0, N).# define
6f520 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
6f530 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c (D,P) .#el
6f540 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
6f550 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
6f560 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44 (D,N) sqlite3D
6f570 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a bMallocRaw(D,N).
6f580 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6f590 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 StackAllocZero(D
6f5a0 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61 ,N) sqlite3DbMa
6f5b0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 llocZero(D,N).#
6f5c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6f5d0 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20 ackFree(D,P)
6f5e0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
6f5f0 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 (D,P).#endif..#i
6f600 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
6f610 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 LE_MEMSYS3.SQLIT
6f620 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
6f630 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6f640 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 ods *sqlite3MemG
6f650 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b etMemsys3(void);
6f660 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
6f670 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
6f680 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS5.SQLITE_PRIV
6f690 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
6f6a0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
6f6b0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
6f6c0 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys5(void);.#endi
6f6d0 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 f...#ifndef SQLI
6f6e0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 TE_MUTEX_OMIT.SQ
6f6f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
6f700 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
6f710 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 hods *sqlite3Def
6f720 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b aultMutex(void);
6f730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f740 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
6f750 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c *sqlite3MutexAll
6f760 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f oc(int);.SQLITE_
6f770 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6f780 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 lite3MutexInit(v
6f790 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
6f7a0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6f7b0 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 e3MutexEnd(void)
6f7c0 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ;.#endif..SQLITE
6f7d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6f7e0 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
6f7f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f800 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f810 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20 3StatusAdd(int,
6f820 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f830 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f840 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20 3StatusSet(int,
6f850 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 int);..SQLITE_PR
6f860 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6f870 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 3IsNaN(double);.
6f880 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f890 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 void sqlite3VXPr
6f8a0 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 intf(StrAccum*,
6f8b0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
6f8c0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 23 69 66 6e , va_list);.#ifn
6f8d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6f8e0 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50 52 49 TRACE.SQLITE_PRI
6f8f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f900 33 58 50 72 69 6e 74 66 28 53 74 72 41 63 63 75 3XPrintf(StrAccu
6f910 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c m*, const char*,
6f920 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 53 51 ...);.#endif.SQ
6f930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6f940 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 r *sqlite3MPrint
6f950 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 f(sqlite3*,const
6f960 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
6f970 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6f980 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e r *sqlite3VMPrin
6f990 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 tf(sqlite3*,cons
6f9a0 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 t char*, va_list
6f9b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f9c0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d E char *sqlite3M
6f9d0 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a Appendf(sqlite3*
6f9e0 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 ,char*,const cha
6f9f0 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66 r*,...);.#if def
6fa00 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
6fa10 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
6fa20 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 ITE_DEBUG).SQLIT
6fa30 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6fa40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
6fa50 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c ntf(const char*,
6fa60 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 ...);.#endif.#i
6fa70 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
6fa80 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 _TEST).SQLITE_PR
6fa90 49 56 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 IVATE void *sq
6faa0 6c 69 74 65 33 54 65 73 74 54 65 78 74 54 6f 50 lite3TestTextToP
6fab0 74 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b tr(const char*);
6fac0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
6fad0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6fae0 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 te3SetString(cha
6faf0 72 20 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 r **, sqlite3*,
6fb00 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e const char*, ...
6fb10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fb20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
6fb30 72 6f 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63 rorMsg(Parse*, c
6fb40 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
6fb50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fb60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
6fb70 6f 72 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b orClear(Parse*);
6fb80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fb90 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f int sqlite3Dequo
6fba0 74 65 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 te(char*);.SQLIT
6fbb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6fbc0 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 lite3KeywordCode
6fbd0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
6fbe0 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c char*, int);.SQL
6fbf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6fc00 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 sqlite3RunParser
6fc10 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 (Parse*, const c
6fc20 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a har*, char **);.
6fc30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6fc40 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 oid sqlite3Finis
6fc50 68 43 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b hCoding(Parse*);
6fc60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fc70 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 int sqlite3GetTe
6fc80 6d 70 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 mpReg(Parse*);.S
6fc90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fca0 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 id sqlite3Releas
6fcb0 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c eTempReg(Parse*,
6fcc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6fcd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6fce0 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 GetTempRange(Par
6fcf0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 se*,int);.SQLITE
6fd00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fd10 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
6fd20 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 Range(Parse*,int
6fd30 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6fd40 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
6fd50 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c te3ExprAlloc(sql
6fd60 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 ite3*,int,const
6fd70 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c Token*,int);.SQL
6fd80 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
6fd90 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 *sqlite3Expr(sq
6fda0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
6fdb0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6fdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fdd0 69 74 65 33 45 78 70 72 41 74 74 61 63 68 53 75 ite3ExprAttachSu
6fde0 62 74 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c btrees(sqlite3*,
6fdf0 45 78 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72 Expr*,Expr*,Expr
6fe00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6fe10 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
6fe20 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e PExpr(Parse*, in
6fe30 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c t, Expr*, Expr*,
6fe40 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a const Token*);.
6fe50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
6fe60 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 xpr *sqlite3Expr
6fe70 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 And(sqlite3*,Exp
6fe80 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 r*, Expr*);.SQLI
6fe90 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
6fea0 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 *sqlite3ExprFunc
6feb0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 tion(Parse*,Expr
6fec0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a List*, Token*);.
6fed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6fee0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
6fef0 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 ssignVarNumber(P
6ff00 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 arse*, Expr*);.S
6ff10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ff20 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 id sqlite3ExprDe
6ff30 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 lete(sqlite3*, E
6ff40 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6ff50 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
6ff60 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 sqlite3ExprListA
6ff70 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 ppend(Parse*,Exp
6ff80 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53 rList*,Expr*);.S
6ff90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ffa0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 id sqlite3ExprLi
6ffb0 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a stSetName(Parse*
6ffc0 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e ,ExprList*,Token
6ffd0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6ffe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6fff0 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 te3ExprListSetSp
70000 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 an(Parse*,ExprLi
70010 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a st*,ExprSpan*);.
70020 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70030 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c oid sqlite3ExprL
70040 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
70050 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 3*, ExprList*);.
70060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70070 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 nt sqlite3Init(s
70080 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 qlite3*, char**)
70090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
700a0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 int sqlite3Init
700b0 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 Callback(void*,
700c0 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 int, char**, cha
700d0 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r**);.SQLITE_PRI
700e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
700f0 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 3Pragma(Parse*,T
70100 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
70110 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 en*,int);.SQLITE
70120 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70130 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e lite3ResetIntern
70140 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 alSchema(sqlite3
70150 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
70160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70170 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 ite3BeginParse(P
70180 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 arse*,int);.SQLI
70190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
701a0 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 sqlite3CommitInt
701b0 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c ernalChanges(sql
701c0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
701d0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
701e0 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 lite3ResultSetOf
701f0 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65 Select(Parse*,Se
70200 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
70210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70220 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 te3OpenMasterTab
70230 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 le(Parse *, int)
70240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
70260 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 rtTable(Parse*,T
70270 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
70280 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
70290 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
702a0 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c id sqlite3AddCol
702b0 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e umn(Parse*,Token
702c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
702d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
702e0 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a ddNotNull(Parse*
702f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70300 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70310 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 te3AddPrimaryKey
70320 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 (Parse*, ExprLis
70330 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e t*, int, int, in
70340 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
70350 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
70360 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e ddCheckConstrain
70370 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 t(Parse*, Expr*)
70380 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70390 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
703a0 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 ColumnType(Parse
703b0 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 *,Token*);.SQLIT
703c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
703d0 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 qlite3AddDefault
703e0 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70 Value(Parse*,Exp
703f0 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f rSpan*);.SQLITE_
70400 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70410 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 ite3AddCollateTy
70420 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e pe(Parse*, Token
70430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70440 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
70450 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 ndTable(Parse*,T
70460 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c oken*,Token*,Sel
70470 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ect*);..SQLITE_P
70480 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 RIVATE Bitvec *s
70490 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
704a0 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f te(u32);.SQLITE_
704b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
704c0 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 te3BitvecTest(Bi
704d0 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c tvec*, u32);.SQL
704e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
704f0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
70500 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a (Bitvec*, u32);.
70510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70520 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
70530 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 cClear(Bitvec*,
70540 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c u32, void*);.SQL
70550 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70560 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
70570 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a stroy(Bitvec*);.
70580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
70590 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 32 sqlite3Bitvec
705a0 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 Size(Bitvec*);.S
705b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
705c0 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 t sqlite3BitvecB
705d0 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 uiltinTest(int,i
705e0 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 nt*);..SQLITE_PR
705f0 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 IVATE RowSet *sq
70600 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 lite3RowSetInit(
70610 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c sqlite3*, void*,
70620 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a unsigned int);.
70630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70640 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 oid sqlite3RowSe
70650 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b tClear(RowSet*);
70660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70670 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 void sqlite3RowS
70680 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a etInsert(RowSet*
70690 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 , i64);.SQLITE_P
706a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
706b0 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 e3RowSetTest(Row
706c0 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c Set*, u8 iBatch,
706d0 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 i64);.SQLITE_PR
706e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
706f0 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 3RowSetNext(RowS
70700 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c et*, i64*);..SQL
70710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70720 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 sqlite3CreateVi
70730 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a ew(Parse*,Token*
70740 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
70750 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b elect*,int,int);
70760 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
70770 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
70780 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
70790 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
707a0 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 TABLE).SQLITE_PR
707b0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
707c0 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e te3ViewGetColumn
707d0 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62 Names(Parse*,Tab
707e0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 le*);.#else.# de
707f0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77 fine sqlite3View
70800 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 GetColumnNames(A
70810 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 ,B) 0.#endif..SQ
70820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70830 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 d sqlite3DropTab
70840 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 le(Parse*, SrcLi
70850 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
70860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70870 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
70880 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a eTable(Table*);.
70890 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
708a0 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
708b0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
708c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 void sqlite3A
708d0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 utoincrementBegi
708e0 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 n(Parse *pParse)
708f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70900 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 void sqlite3A
70910 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 utoincrementEnd(
70920 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a Parse *pParse);.
70930 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
70940 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d qlite3Autoincrem
70950 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65 entBegin(X).# de
70960 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f fine sqlite3Auto
70970 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a incrementEnd(X).
70980 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
70990 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
709a0 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c e3Insert(Parse*,
709b0 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c SrcList*, ExprL
709c0 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 ist*, Select*, I
709d0 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 dList*, int);.SQ
709e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
709f0 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 d *sqlite3ArrayA
70a00 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a llocate(sqlite3*
70a10 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 ,void*,int,int,i
70a20 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a nt*,int*,int*);.
70a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
70a40 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 dList *sqlite3Id
70a50 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 ListAppend(sqlit
70a60 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f e3*, IdList*, To
70a70 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
70a80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70a90 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 3IdListIndex(IdL
70aa0 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a ist*,const char*
70ab0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70ac0 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
70ad0 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 e3SrcListEnlarge
70ae0 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 (sqlite3*, SrcLi
70af0 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
70b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
70b10 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
70b20 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c rcListAppend(sql
70b30 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c ite3*, SrcList*,
70b40 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
70b50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70b60 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
70b70 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 3SrcListAppendFr
70b80 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 omTerm(Parse*, S
70b90 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c rcList*, Token*,
70ba0 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 Token*,.
70bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
70bd0 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 oken*, Select*,
70be0 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b Expr*, IdList*);
70bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70c00 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c void sqlite3SrcL
70c10 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 istIndexedBy(Par
70c20 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c se *, SrcList *,
70c30 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 Token *);.SQLIT
70c40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
70c50 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f lite3IndexedByLo
70c60 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 okup(Parse *, st
70c70 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
70c80 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 m *);.SQLITE_PRI
70c90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70ca0 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 3SrcListShiftJoi
70cb0 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b nType(SrcList*);
70cc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70cd0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c void sqlite3SrcL
70ce0 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 istAssignCursors
70cf0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
70d00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70d10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 TE void sqlite3I
70d20 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 dListDelete(sqli
70d30 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a te3*, IdList*);.
70d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70d50 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
70d60 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 stDelete(sqlite3
70d70 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
70d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 LITE_PRIVATE Ind
70d90 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 ex *sqlite3Creat
70da0 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f eIndex(Parse*,To
70db0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c ken*,Token*,SrcL
70dc0 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 ist*,ExprList*,i
70dd0 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 nt,Token*,.
70de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70df0 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 Token*, int,
70e00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70e20 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 3DropIndex(Parse
70e30 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
70e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c E int sqlite3Sel
70e60 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 ect(Parse*, Sele
70e70 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a ct*, SelectDest*
70e80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70e90 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 E Select *sqlite
70ea0 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 3SelectNew(Parse
70eb0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c *,ExprList*,SrcL
70ec0 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c ist*,Expr*,ExprL
70ed0 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 ist*,.
70ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
70ef0 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 xpr*,ExprList*,i
70f00 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b nt,Expr*,Expr*);
70f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70f20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
70f30 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 ctDelete(sqlite3
70f40 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
70f50 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
70f60 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 e *sqlite3SrcLis
70f70 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 tLookup(Parse*,
70f80 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
70f90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
70fa0 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 lite3IsReadOnly(
70fb0 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
70fc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
70fd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70fe0 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 3OpenTable(Parse
70ff0 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 *, int iCur, int
71000 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e iDb, Table*, in
71010 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t);.#if defined(
71020 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
71030 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
71040 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
71050 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
71060 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ERY).SQLITE_PRIV
71070 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
71080 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 3LimitWhere(Pars
71090 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 e *, SrcList *,
710a0 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 Expr *, ExprList
710b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
710c0 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e *, char *);.#en
710d0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
710e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
710f0 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a eleteFrom(Parse*
71100 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 , SrcList*, Expr
71110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71120 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
71130 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 pdate(Parse*, Sr
71140 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 cList*, ExprList
71150 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
71160 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 SQLITE_PRIVATE W
71170 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 hereInfo *sqlite
71180 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 3WhereBegin(Pars
71190 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
711a0 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c pr*, ExprList**,
711b0 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u16);.SQLITE_PR
711c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
711d0 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 e3WhereEnd(Where
711e0 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Info*);.SQLITE_P
711f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
71200 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c e3ExprCodeGetCol
71210 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c umn(Parse*, Tabl
71220 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
71230 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
71240 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71250 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
71260 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
71270 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
71280 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71290 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 ite3ExprCodeCopy
712a0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
712b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
712c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
712d0 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f ite3ExprCacheSto
712e0 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 re(Parse*, int,
712f0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
71300 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71310 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
71320 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ush(Parse*);.SQL
71330 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71340 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
71350 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 ePop(Parse*, int
71360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71370 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
71380 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 prCacheRemove(Pa
71390 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
713a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
713b0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
713c0 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 Clear(Parse*);.S
713d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
713e0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
713f0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
71400 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 e(Parse*, int, i
71410 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71420 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71430 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 ExprHardCopy(Par
71440 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 se*,int,int);.SQ
71450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71460 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
71470 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
71480 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71490 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
714a0 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 ExprCodeTemp(Par
714b0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a se*, Expr*, int*
714c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
714d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
714e0 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 rCodeTarget(Pars
714f0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
71500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71510 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
71520 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 odeAndCache(Pars
71530 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
71540 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71550 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
71560 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 CodeConstants(Pa
71570 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 rse*, Expr*);.SQ
71580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71590 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
715a0 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c ExprList(Parse*,
715b0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c ExprList*, int,
715c0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
715d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
715e0 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 e3ExprIfTrue(Par
715f0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c se*, Expr*, int,
71600 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71610 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71620 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 e3ExprIfFalse(Pa
71630 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
71640 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
71650 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
71660 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 lite3FindTable(s
71670 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
71680 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ar*, const char*
71690 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
716a0 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
716b0 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 LocateTable(Pars
716c0 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f e*,int isView,co
716d0 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
716e0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
716f0 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 PRIVATE Index *s
71700 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
71710 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
71720 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
71730 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71740 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
71750 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
71760 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 ble(sqlite3*,int
71770 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
71780 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71790 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
717a0 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 AndDeleteIndex(s
717b0 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 qlite3*,int,cons
717c0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
717d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
717e0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 lite3Vacuum(Pars
717f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
71800 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
71810 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c unVacuum(char**,
71820 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
71830 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
71840 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d *sqlite3NameFrom
71850 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 Token(sqlite3*,
71860 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
71870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
71880 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 te3ExprCompare(E
71890 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
718a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
718b0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 d sqlite3ExprAna
718c0 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e lyzeAggregates(N
718d0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 ameContext*, Exp
718e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
718f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71900 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 ExprAnalyzeAggLi
71910 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c st(NameContext*,
71920 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 ExprList*);.SQLI
71930 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
71940 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 *sqlite3GetVdbe(
71950 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
71960 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71970 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 ite3PrngSaveStat
71980 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f e(void);.SQLITE_
71990 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
719a0 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 ite3PrngRestoreS
719b0 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
719c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
719d0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 sqlite3PrngReset
719e0 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
719f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71a00 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
71a10 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 All(sqlite3*);.S
71a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71a30 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 id sqlite3CodeVe
71a40 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 rifySchema(Parse
71a50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
71a60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71a70 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 ite3BeginTransac
71a80 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 tion(Parse*, int
71a90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71aa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
71ab0 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 mmitTransaction(
71ac0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
71ad0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71ae0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e ite3RollbackTran
71af0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b saction(Parse*);
71b00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71b10 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 void sqlite3Save
71b20 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e point(Parse*, in
71b30 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 t, Token*);.SQLI
71b40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71b50 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
71b60 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a points(sqlite3 *
71b70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71b80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
71b90 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 rIsConstant(Expr
71ba0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71bb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
71bc0 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a prIsConstantNotJ
71bd0 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 oin(Expr*);.SQLI
71be0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71bf0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
71c00 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 tantOrFunction(E
71c10 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
71c20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71c30 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 3ExprIsInteger(E
71c40 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c xpr*, int*);.SQL
71c50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
71c60 73 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 sqlite3ExprCanBe
71c70 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a Null(const Expr*
71c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
71ca0 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 prCodeIsNullJump
71cb0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78 (Vdbe*, const Ex
71cc0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a pr*, int, int);.
71cd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
71ce0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 nt sqlite3ExprNe
71cf0 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 edsNoAffinityCha
71d00 6e 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c nge(const Expr*,
71d10 20 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 char);.SQLITE_P
71d20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
71d30 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 e3IsRowid(const
71d40 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
71d50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71d60 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 te3GenerateRowDe
71d70 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 lete(Parse*, Tab
71d80 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 le*, int, int, i
71d90 6e 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 nt, Trigger *, i
71da0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71dc0 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 GenerateRowIndex
71dd0 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 Delete(Parse*, T
71de0 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a able*, int, int*
71df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71e00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e E int sqlite3Gen
71e10 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 erateIndexKey(Pa
71e20 72 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e rse*, Index*, in
71e30 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
71e40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71e50 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 d sqlite3Generat
71e60 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b eConstraintCheck
71e70 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c s(Parse*,Table*,
71e80 69 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 int,int,.
71e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
71eb0 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 t*,int,int,int,i
71ec0 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 nt,int*);.SQLITE
71ed0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71ee0 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 lite3CompleteIns
71ef0 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 ertion(Parse*, T
71f00 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c able*, int, int,
71f10 20 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c int*, int, int,
71f20 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71f40 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 3OpenTableAndInd
71f50 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 ices(Parse*, Tab
71f60 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a le*, int, int);.
71f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71f80 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e oid sqlite3Begin
71f90 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 WriteOperation(P
71fa0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 arse*, int, int)
71fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71fc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c void sqlite3Mul
71fd0 74 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b tiWrite(Parse*);
71fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71ff0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 void sqlite3MayA
72000 62 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 bort(Parse*);.SQ
72010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
72020 64 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e d sqlite3HaltCon
72030 73 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 straint(Parse*,
72040 69 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 int, char*, int)
72050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72060 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
72070 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 prDup(sqlite3*,E
72080 78 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 xpr*,int);.SQLIT
72090 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 E_PRIVATE ExprLi
720a0 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c st *sqlite3ExprL
720b0 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c istDup(sqlite3*,
720c0 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a ExprList*,int);.
720d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
720e0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
720f0 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 rcListDup(sqlite
72100 33 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3*,SrcList*,int)
72110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72120 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
72130 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 IdListDup(sqlite
72140 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 3*,IdList*);.SQL
72150 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
72160 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
72170 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 tDup(sqlite3*,Se
72180 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 lect*,int);.SQLI
72190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
721a0 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e sqlite3FuncDefIn
721b0 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 sert(FuncDefHash
721c0 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 *, FuncDef*);.SQ
721d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e LITE_PRIVATE Fun
721e0 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e cDef *sqlite3Fin
721f0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 dFunction(sqlite
72200 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 3*,const char*,i
72210 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a nt,int,u8,int);.
72220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
72230 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
72240 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 terBuiltinFuncti
72250 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 ons(sqlite3*);.S
72260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72270 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
72280 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
72290 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ons(void);.SQLIT
722a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
722b0 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c qlite3RegisterGl
722c0 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f obalFunctions(vo
722d0 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 id);.#ifdef SQLI
722e0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
722f0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
72300 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 lite3SafetyOn(sq
72310 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
72320 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
72330 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 lite3SafetyOff(s
72340 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
72350 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72360 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 SafetyOn(A) 0.#
72370 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 define sqlite3Sa
72380 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e fetyOff(A) 0.#en
72390 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
723a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
723b0 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 fetyCheckOk(sqli
723c0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
723d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
723e0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 3SafetyCheckSick
723f0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a OrOk(sqlite3*);.
72400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
72410 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 oid sqlite3Chang
72420 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 eCookie(Parse*,
72430 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
72440 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
72450 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 VIEW) && !define
72460 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
72470 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 IGGER).SQLITE_PR
72480 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72490 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 e3MaterializeVie
724a0 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a w(Parse*, Table*
724b0 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 , Expr*, int);.#
724c0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
724d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
724e0 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ER.SQLITE_PRIVAT
724f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
72500 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 BeginTrigger(Par
72510 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 se*, Token*,Toke
72520 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 n*,int,int,IdLis
72530 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 t*,SrcList*,.
72540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72550 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e Expr*,in
72560 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
72570 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
72580 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 qlite3FinishTrig
72590 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 ger(Parse*, Trig
725a0 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a gerStep*, Token*
725b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
725c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
725d0 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 DropTrigger(Pars
725e0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
725f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
72600 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72610 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 3DropTriggerPtr(
72620 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a Parse*, Trigger*
72630 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72640 45 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c E Trigger *sql
72650 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 ite3TriggersExis
72660 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 t(Parse *, Table
72670 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 *, int, ExprList
72680 2a 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a *, int *pMask);.
72690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
726a0 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 Trigger *sqlite
726b0 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 3TriggerList(Par
726c0 73 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a se *, Table *);.
726d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
726e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
726f0 65 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 eRowTrigger(Pars
72700 65 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 e*, Trigger *, i
72710 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 nt, ExprList*, i
72720 6e 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 nt, Table *,.
72730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72740 20 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e int, in
72750 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
72760 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
72770 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 qlite3CodeRowTri
72780 67 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65 ggerDirect(Parse
72790 20 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54 *, Trigger *, T
727a0 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 able *, int, int
727b0 2c 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 , int);. void s
727c0 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 qliteViewTrigger
727d0 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a s(Parse*, Table*
727e0 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 , Expr*, int, Ex
727f0 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
72800 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
72810 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
72820 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 ggerStep(sqlite3
72830 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 *, TriggerStep*)
72840 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72850 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a TriggerStep *
72860 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 sqlite3TriggerSe
72870 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 lectStep(sqlite3
72880 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 *,Select*);.SQLI
72890 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
728a0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
728b0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 3TriggerInsertSt
728c0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
728d0 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 n*, IdList*,.
728e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
728f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72900 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 ExprList*,S
72910 65 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 elect*,u8);.SQLI
72920 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
72930 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
72940 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 3TriggerUpdateSt
72950 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 ep(sqlite3*,Toke
72960 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 n*,ExprList*, Ex
72970 70 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 pr*, u8);.SQLITE
72980 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
72990 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
729a0 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 riggerDeleteStep
729b0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
729c0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
729d0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
729e0 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
729f0 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 gger(sqlite3*, T
72a00 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 rigger*);.SQLITE
72a10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
72a20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 sqlite3UnlinkAnd
72a30 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 DeleteTrigger(sq
72a40 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
72a50 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
72a60 50 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 PRIVATE u32 sq
72a70 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d lite3TriggerColm
72a80 61 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 ask(Parse*,Trigg
72a90 65 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e er*,ExprList*,in
72aa0 74 2c 69 6e 74 2c 54 61 62 6c 65 2a 2c 69 6e 74 t,int,Table*,int
72ab0 29 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 );.# define sqli
72ac0 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c te3ParseToplevel
72ad0 28 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 (p) ((p)->pTople
72ae0 76 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c vel ? (p)->pTopl
72af0 65 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 evel : (p)).#els
72b00 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
72b10 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 e3TriggersExist(
72b20 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 B,C,D,E,F) 0.# d
72b30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c efine sqlite3Del
72b40 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a eteTrigger(A,B).
72b50 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72b60 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 DropTriggerPtr(A
72b70 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,B).# define sql
72b80 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c ite3UnlinkAndDel
72b90 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 eteTrigger(A,B,C
72ba0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
72bb0 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 e3CodeRowTrigger
72bc0 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 (A,B,C,D,E,F,G,H
72bd0 2c 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,I).# define sql
72be0 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 ite3CodeRowTrigg
72bf0 65 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 erDirect(A,B,C,D
72c00 2c 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73 ,E,F).# define s
72c10 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 qlite3TriggerLis
72c20 74 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69 t(X, Y) 0.# defi
72c30 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 ne sqlite3ParseT
72c40 6f 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64 oplevel(p) p.# d
72c50 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 efine sqlite3Tri
72c60 67 67 65 72 43 6f 6c 6d 61 73 6b 28 41 2c 42 2c ggerColmask(A,B,
72c70 43 2c 44 2c 45 2c 46 2c 47 29 20 30 0a 23 65 6e C,D,E,F,G) 0.#en
72c80 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
72c90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a ATE int sqlite3J
72ca0 6f 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 oinType(Parse*,
72cb0 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 Token*, Token*,
72cc0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
72cd0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
72ce0 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 ite3CreateForeig
72cf0 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 nKey(Parse*, Exp
72d00 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 rList*, Token*,
72d10 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b ExprList*, int);
72d20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72d30 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 void sqlite3Defe
72d40 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 rForeignKey(Pars
72d50 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 e*, int);.#ifnde
72d60 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
72d70 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 THORIZATION.SQLI
72d80 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
72d90 64 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 d sqlite3AuthRea
72da0 64 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 d(Parse*,Expr*,S
72db0 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 chema*,SrcList*)
72dc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72dd0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 int sqlite3Au
72de0 74 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 thCheck(Parse*,i
72df0 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c nt, const char*,
72e00 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f const char*, co
72e10 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
72e20 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
72e30 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e d sqlite3AuthCon
72e40 74 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c textPush(Parse*,
72e50 20 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 AuthContext*, c
72e60 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
72e70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
72e80 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f id sqlite3AuthCo
72e90 6e 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e ntextPop(AuthCon
72ea0 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 text*);.SQLITE_P
72eb0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
72ec0 69 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 ite3AuthReadCol(
72ed0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 Parse*, const ch
72ee0 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ar *, const char
72ef0 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a *, int);.#else.
72f00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72f10 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 AuthRead(a,b,c,d
72f20 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
72f30 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c e3AuthCheck(a,b,
72f40 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 c,d,e) SQLITE
72f50 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c _OK.# define sql
72f60 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
72f70 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 ush(a,b,c).# def
72f80 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 ine sqlite3AuthC
72f90 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 28 ontextPop(a) ((
72fa0 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66 void)(a)).#endif
72fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72fc0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 void sqlite3Atta
72fd0 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a ch(Parse*, Expr*
72fe0 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b , Expr*, Expr*);
72ff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73000 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 void sqlite3Deta
73010 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a ch(Parse*, Expr*
73020 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
73040 65 65 46 61 63 74 6f 72 79 28 73 71 6c 69 74 65 eeFactory(sqlite
73050 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
73060 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 r *zFilename,.
73070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73080 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 int omitJou
73090 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65 rnal, int nCache
730a0 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72 , int flags, Btr
730b0 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53 ee **ppBtree);.S
730c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
730d0 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 t sqlite3FixInit
730e0 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65 (DbFixer*, Parse
730f0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
73100 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e ar*, const Token
73110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
73130 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65 72 xSrcList(DbFixer
73140 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
73150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73160 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 sqlite3FixSelec
73170 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65 t(DbFixer*, Sele
73180 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
73190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
731a0 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72 2a FixExpr(DbFixer*
731b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
731c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
731d0 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 ite3FixExprList(
731e0 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69 DbFixer*, ExprLi
731f0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
73200 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73210 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 44 FixTriggerStep(D
73220 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72 bFixer*, Trigger
73230 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Step*);.SQLITE_P
73240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73250 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 e3AtoF(const cha
73260 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a r *z, double*);.
73270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73280 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 nt sqlite3GetInt
73290 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 32(const char *,
732a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
732b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
732c0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 e3FitsIn64Bits(c
732d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
732e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
732f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
73300 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 16ByteLen(const
73310 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 void *pData, int
73320 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f nChar);.SQLITE_
73330 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73340 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 te3Utf8CharLen(c
73350 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 onst char *pData
73360 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 , int nByte);.SQ
73370 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73380 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
73390 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 (const u8*, cons
733a0 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t u8**);../*.**
733b0 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61 64 Routines to read
733c0 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 69 61 and write varia
733d0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
733e0 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 65 64 ers. These used
733f0 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65 to.** be define
73400 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e d locally, but n
73410 6f 77 20 77 65 20 75 73 65 20 74 68 65 20 76 61 ow we use the va
73420 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20 69 6e rint routines in
73430 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 the util.c.** f
73440 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c ile. Code shoul
73450 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 d use the MACRO
73460 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20 forms below, as
73470 74 68 65 20 56 61 72 69 6e 74 33 32 20 76 65 72 the Varint32 ver
73480 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64 sions.** are cod
73490 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74 68 65 ed to assume the
734a0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 73 single byte cas
734b0 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 61 6e e is already han
734c0 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a 2a 20 dled (which .**
734d0 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64 the MACRO form d
734e0 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oes)..*/.SQLITE_
734f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73500 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 te3PutVarint(uns
73510 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 36 34 igned char*, u64
73520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 E int sqlite3Put
73540 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 Varint32(unsigne
73550 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53 d char*, u32);.S
73560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 QLITE_PRIVATE u8
73570 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
73580 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 t(const unsigned
73590 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b char *, u64 *);
735a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
735b0 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 u8 sqlite3GetVar
735c0 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 int32(const unsi
735d0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 gned char *, u32
735e0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
735f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
73600 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b arintLen(u64 v);
73610 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 ../*.** The head
73620 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 er of a record c
73630 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 onsists of a seq
73640 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c uence variable-l
73650 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a ength integers..
73660 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 ** These integer
73670 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 s are almost alw
73680 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 ays small and ar
73690 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 e encoded as a s
736a0 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 ingle byte..** T
736b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
736c0 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 ros take advanta
736d0 67 65 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 ge this fact to
736e0 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 provide a fast e
736f0 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 ncode.** and dec
73700 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ode of the integ
73710 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 ers in a record
73720 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 header. It is f
73730 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f aster for the co
73740 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 mmon.** case whe
73750 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 re the integer i
73760 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e s a single byte.
73770 20 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 It is a little
73780 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 slower when the
73790 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 .** integer is t
737a0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 wo or more bytes
737b0 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 . But overall i
737c0 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a t is faster..**.
737d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
737e0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 expressions are
737f0 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a equivalent:.**.
73800 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 ** x = sqlit
73810 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 e3GetVarint32( A
73820 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 , &B );.** x
73830 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 = sqlite3PutVar
73840 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a int32( A, B );.*
73850 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 *.** x = get
73860 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 Varint32( A, B )
73870 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 ;.** x = put
73880 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 Varint32( A, B )
73890 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ;.**.*/.#define
738a0 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 getVarint32(A,B)
738b0 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28 75 38 (u8)((*(A)<(u8
738c0 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20 )0x80) ? ((B) =
738d0 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73 (u32)*(A)),1 : s
738e0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 qlite3GetVarint3
738f0 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28 2((A), (u32 *)&(
73900 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 B))).#define put
73910 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 Varint32(A,B) (
73920 75 38 29 28 28 28 75 33 32 29 28 42 29 3c 28 75 u8)(((u32)(B)<(u
73930 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28 41 29 32)0x80) ? (*(A)
73940 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
73950 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 74 r)(B)),1 : sqlit
73960 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 28 41 e3PutVarint32((A
73970 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69 6e 65 ), (B))).#define
73980 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 73 71 getVarint sq
73990 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 0a 23 lite3GetVarint.#
739a0 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 define putVarint
739b0 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 56 61 sqlite3PutVa
739c0 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 rint...SQLITE_PR
739d0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
739e0 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 *sqlite3IndexAf
739f0 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a finityStr(Vdbe *
73a00 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 , Index *);.SQLI
73a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73a20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 sqlite3TableAffi
73a30 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 nityStr(Vdbe *,
73a40 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
73a50 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
73a60 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
73a70 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
73a80 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 , char aff2);.SQ
73a90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73aa0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 sqlite3IndexAff
73ab0 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 inityOk(Expr *pE
73ac0 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 xpr, char idx_af
73ad0 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f finity);.SQLITE_
73ae0 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
73af0 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
73b00 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 (Expr *pExpr);.S
73b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73b20 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 t sqlite3Atoi64(
73b30 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 const char*, i64
73b40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73b50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
73b60 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 rror(sqlite3*, i
73b70 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c nt, const char*,
73b80 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
73b90 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
73ba0 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 e3HexToBlob(sqli
73bb0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 te3*, const char
73bc0 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c *z, int n);.SQL
73bd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
73be0 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 sqlite3TwoPartNa
73bf0 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 me(Parse *, Toke
73c00 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f n *, Token *, To
73c10 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f ken **);.SQLITE_
73c20 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
73c30 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 ar *sqlite3ErrSt
73c40 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 r(int);.SQLITE_P
73c50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73c60 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 e3ReadSchema(Par
73c70 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c se *pParse);.SQL
73c80 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c ITE_PRIVATE Coll
73c90 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 Seq *sqlite3Find
73ca0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a CollSeq(sqlite3*
73cb0 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 ,u8 enc, const c
73cc0 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 har*,int);.SQLIT
73cd0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
73ce0 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 q *sqlite3Locate
73cf0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 CollSeq(Parse *p
73d00 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
73d10 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 r*zName);.SQLITE
73d20 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 _PRIVATE CollSeq
73d30 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c *sqlite3ExprCol
73d40 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 lSeq(Parse *pPar
73d50 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 se, Expr *pExpr)
73d60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73d70 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
73d80 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 prSetColl(Parse
73d90 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c *pParse, Expr *,
73da0 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 Token *);.SQLIT
73db0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73dc0 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 lite3CheckCollSe
73dd0 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 q(Parse *, CollS
73de0 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 eq *);.SQLITE_PR
73df0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73e00 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 3CheckObjectName
73e10 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 (Parse *, const
73e20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f char *);.SQLITE_
73e30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73e40 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
73e50 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e es(sqlite3 *, in
73e60 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
73e70 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
73e80 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
73e90 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c (sqlite3_value*,
73ea0 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u8);.SQLITE_PRI
73eb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73ec0 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 ValueBytes(sqlit
73ed0 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a e3_value*, u8);.
73ee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73ef0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
73f00 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76 SetStr(sqlite3_v
73f10 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 alue*, int, cons
73f20 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 t void *,u8, .
73f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73f40 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f void(*)(vo
73f50 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id*));.SQLITE_PR
73f60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73f70 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 e3ValueFree(sqli
73f80 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
73f90 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
73fa0 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 te3_value *sqlit
73fb0 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 e3ValueNew(sqlit
73fc0 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e3 *);.SQLITE_PR
73fd0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
73fe0 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 te3Utf16to8(sqli
73ff0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 te3 *, const voi
74000 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 d*, int);.#ifdef
74010 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 SQLITE_ENABLE_S
74020 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52 49 56 TAT2.SQLITE_PRIV
74030 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
74040 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74 65 3Utf8to16(sqlite
74050 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c 3 *, u8, char *,
74060 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 int, int *);.#e
74070 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
74080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
74090 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c alueFromExpr(sql
740a0 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20 ite3 *, Expr *,
740b0 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 5f u8, u8, sqlite3_
740c0 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 value **);.SQLIT
740d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
740e0 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 qlite3ValueApply
740f0 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65 33 Affinity(sqlite3
74100 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38 _value *, u8, u8
74110 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
74120 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 E_AMALGAMATION.S
74130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
74140 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
74150 72 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 r sqlite3OpcodeP
74160 72 6f 70 65 72 74 79 5b 5d 3b 0a 53 51 4c 49 54 roperty[];.SQLIT
74170 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
74180 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
74190 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
741a0 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r[];.SQLITE_PRIV
741b0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ATE const unsign
741c0 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 ed char sqlite3C
741d0 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 typeMap[];.SQLIT
741e0 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 E_PRIVATE SQLITE
741f0 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 _WSD struct Sqli
74200 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 te3Config sqlite
74210 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 3Config;.SQLITE_
74220 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 PRIVATE SQLITE_W
74230 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 SD FuncDefHash s
74240 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 qlite3GlobalFunc
74250 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 tions;.SQLITE_PR
74260 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
74270 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 3PendingByte;.#e
74280 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
74290 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
742a0 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 RootPageMoved(Db
742b0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
742c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
742d0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 d sqlite3Reindex
742e0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
742f0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
74300 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74310 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 lite3AlterFuncti
74320 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 ons(sqlite3*);.S
74330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74340 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 id sqlite3AlterR
74350 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65 enameTable(Parse
74360 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b *, SrcList*, Tok
74370 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
74380 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74390 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 GetToken(const u
743a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
743b0 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int *);.SQLITE_P
743c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
743d0 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50 te3NestedParse(P
743e0 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
743f0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 r*, ...);.SQLITE
74400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74410 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
74420 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 redStatements(sq
74430 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
74440 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74450 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 te3CodeSubselect
74460 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a (Parse *, Expr *
74470 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
74480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74490 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 sqlite3SelectPr
744a0 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 ep(Parse*, Selec
744b0 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a t*, NameContext*
744c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
744d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
744e0 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 olveExprNames(Na
744f0 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 meContext*, Expr
74500 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74510 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
74520 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 esolveSelectName
74530 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 s(Parse*, Select
74540 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 *, NameContext*)
74550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74560 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f int sqlite3Reso
74570 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
74580 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c Parse*, Select*,
74590 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 ExprList*, cons
745a0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
745b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
745c0 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 lite3ColumnDefau
745d0 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 lt(Vdbe *, Table
745e0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 *, int, int);.S
745f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74600 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 id sqlite3AlterF
74610 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 inishAddColumn(P
74620 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 arse *, Token *)
74630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
74650 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e erBeginAddColumn
74660 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 (Parse *, SrcLis
74670 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
74680 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
74690 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 lite3GetCollSeq(
746a0 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c 20 43 6f sqlite3*, u8, Co
746b0 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 llSeq *, const c
746c0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
746d0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 IVATE char sqlit
746e0 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 e3AffinityType(c
746f0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
74700 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74710 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 sqlite3Analyze(
74720 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 Parse*, Token*,
74730 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
74740 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74750 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e te3InvokeBusyHan
74760 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 dler(BusyHandler
74770 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
74790 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 ndDb(sqlite3*, T
747a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
747b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
747c0 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c e3FindDbName(sql
747d0 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 ite3 *, const ch
747e0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar *);.SQLITE_PR
747f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
74800 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 3AnalysisLoad(sq
74810 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b lite3*,int iDB);
74820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74830 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 void sqlite3Dele
74840 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 49 teIndexSamples(I
74850 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ndex*);.SQLITE_P
74860 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74870 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 te3DefaultRowEst
74880 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 (Index*);.SQLITE
74890 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
748a0 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b lite3RegisterLik
748b0 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 eFunctions(sqlit
748c0 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e3*, int);.SQLIT
748d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
748e0 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 lite3IsLikeFunct
748f0 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 ion(sqlite3*,Exp
74900 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a r*,int*,char*);.
74910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74920 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d oid sqlite3Minim
74930 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 umFileFormat(Par
74940 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a se*, int, int);.
74950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74960 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d oid sqlite3Schem
74970 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 aFree(void *);.S
74980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 63 QLITE_PRIVATE Sc
74990 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 68 hema *sqlite3Sch
749a0 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20 2a emaGet(sqlite3 *
749b0 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 , Btree *);.SQLI
749c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
749d0 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
749e0 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c dex(sqlite3 *db,
749f0 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49 Schema *);.SQLI
74a00 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e TE_PRIVATE KeyIn
74a10 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 fo *sqlite3Index
74a20 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c Keyinfo(Parse *,
74a30 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 Index *);.SQLIT
74a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
74a50 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 lite3CreateFunc(
74a60 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 sqlite3 *, const
74a70 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e char *, int, in
74a80 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f t, void *, . vo
74a90 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 id (*)(sqlite3_c
74aa0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
74ab0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 te3_value **),.
74ac0 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 void (*)(sqlite
74ad0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
74ae0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 qlite3_value **)
74af0 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 , void (*)(sqlit
74b00 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53 e3_context*));.S
74b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74b20 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 t sqlite3ApiExit
74b30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
74b40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
74b50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 TE int sqlite3Op
74b60 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 enTempDatabase(P
74b70 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 arse *);..SQLITE
74b80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74b90 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
74ba0 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 61 t(StrAccum*, cha
74bb0 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 r*, int, int);.S
74bc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74bd0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 id sqlite3StrAcc
74be0 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 umAppend(StrAccu
74bf0 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 m*,const char*,i
74c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
74c10 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
74c20 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
74c30 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 StrAccum*);.SQLI
74c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74c50 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
74c60 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b eset(StrAccum*);
74c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74c80 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
74c90 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 ctDestInit(Selec
74ca0 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b tDest*,int,int);
74cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74cc0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 Expr *sqlite3Cre
74cd0 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 ateColumnExpr(sq
74ce0 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c 69 73 74 lite3 *, SrcList
74cf0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a *, int, int);..
74d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74d10 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
74d20 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 pRestart(sqlite3
74d30 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49 _backup *);.SQLI
74d40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74d50 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
74d60 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ate(sqlite3_back
74d70 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73 up *, Pgno, cons
74d80 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t u8 *);../*.**
74d90 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
74da0 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 the LEMON-gener
74db0 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 ated parser.*/.S
74dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74dd0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 id *sqlite3Parse
74de0 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 rAlloc(void*(*)(
74df0 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 size_t));.SQLITE
74e00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74e10 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 lite3ParserFree(
74e20 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 void*, void(*)(v
74e30 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
74e40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74e50 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c te3Parser(void*,
74e60 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 int, Token, Par
74e70 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54 se*);.#ifdef YYT
74e80 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 RACKMAXSTACKDEPT
74e90 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 H.SQLITE_PRIVATE
74ea0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 int sqlite3Pa
74eb0 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f rserStackPeak(vo
74ec0 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 id*);.#endif..SQ
74ed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74ee0 64 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 d sqlite3AutoLoa
74ef0 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 dExtensions(sqli
74f00 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 te3*);.#ifndef S
74f10 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
74f20 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 EXTENSION.SQLITE
74f30 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
74f40 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 sqlite3CloseExte
74f50 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 nsions(sqlite3*)
74f60 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
74f70 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 sqlite3CloseExt
74f80 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 ensions(X).#endi
74f90 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
74fa0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
74fb0 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 CHE.SQLITE_PRIVA
74fc0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
74fd0 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 3TableLock(Parse
74fe0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 *, int, int, u8
74ff0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
75000 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
75010 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
75020 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e k(v,w,x,y,z).#en
75030 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
75040 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 TE_TEST.SQLITE_P
75050 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
75060 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 ite3Utf8To8(unsi
75070 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e gned char*);.#en
75080 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
75090 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
750a0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73 ABLE.# define s
750b0 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 qlite3VtabClear(
750c0 59 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c Y).# define sql
750d0 69 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59 ite3VtabSync(X,Y
750e0 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 ) SQLITE_OK.# d
750f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
75100 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 bRollback(X).#
75110 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
75120 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 abCommit(X).# d
75130 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
75140 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 20 bInSync(db) 0.#
75150 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
75160 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64 tabLock(X) .# d
75170 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
75180 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65 bUnlock(X).# de
75190 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
751a0 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 0a 23 65 UnlockList(X).#e
751b0 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 lse.SQLITE_PRIVA
751c0 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 TE void sqlit
751d0 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c e3VtabClear(Tabl
751e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
751f0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 ATE int sqlit
75200 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74 e3VtabSync(sqlit
75210 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29 e3 *db, char **)
75220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75230 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 int sqlite3V
75240 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 tabRollback(sqli
75250 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 te3 *db);.SQLITE
75260 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 _PRIVATE int
75270 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 sqlite3VtabCommi
75280 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a t(sqlite3 *db);.
75290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
752a0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 void sqlite3Vt
752b0 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 abLock(VTable *)
752c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
752d0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 void sqlite3
752e0 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c VtabUnlock(VTabl
752f0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
75300 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c VATE void sql
75310 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 ite3VtabUnlockLi
75320 73 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 20 st(sqlite3*);.#
75330 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
75340 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28 28 tabInSync(db) ((
75350 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 26 db)->nVTrans>0 &
75360 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73 3d & (db)->aVTrans=
75370 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 =0).#endif.SQLIT
75380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
75390 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 qlite3VtabMakeWr
753a0 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 61 itable(Parse*,Ta
753b0 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
753c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
753d0 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 e3VtabBeginParse
753e0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
753f0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
75400 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75410 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 void sqlite3Vta
75420 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72 bFinishParse(Par
75430 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 se*, Token*);.SQ
75440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75450 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 d sqlite3VtabArg
75460 49 6e 69 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 Init(Parse*);.SQ
75470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75480 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 d sqlite3VtabArg
75490 45 78 74 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 Extend(Parse*, T
754a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
754b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
754c0 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 e3VtabCallCreate
754d0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 (sqlite3*, int,
754e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 68 const char *, ch
754f0 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ar **);.SQLITE_P
75500 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
75510 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 e3VtabCallConnec
75520 74 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a t(Parse*, Table*
75530 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75540 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 E int sqlite3Vta
75550 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c bCallDestroy(sql
75560 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 ite3*, int, cons
75570 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
75580 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
75590 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 73 lite3VtabBegin(s
755a0 71 6c 69 74 65 33 20 2a 2c 20 56 54 61 62 6c 65 qlite3 *, VTable
755b0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
755c0 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c ATE FuncDef *sql
755d0 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 ite3VtabOverload
755e0 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
755f0 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 *,FuncDef*, int
75600 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 nArg, Expr*);.S
75610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
75620 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 id sqlite3Invali
75630 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 dFunction(sqlite
75640 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
75650 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b qlite3_value**);
75660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75670 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 50 int sqlite3VdbeP
75680 61 72 61 6d 65 74 65 72 49 6e 64 65 78 28 56 64 arameterIndex(Vd
75690 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a be*, const char*
756a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
756b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
756c0 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e e3TransferBindin
756d0 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
756e0 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 *, sqlite3_stmt
756f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
75700 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
75710 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b 0a prepare(Vdbe*);.
75720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
75730 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c oid sqlite3ExprL
75740 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 50 istCheckLength(P
75750 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a arse*, ExprList*
75760 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
75770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
75780 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42 ollSeq *sqlite3B
75790 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
757a0 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78 70 Seq(Parse *, Exp
757b0 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 51 r *, Expr *);.SQ
757c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
757d0 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 sqlite3TempInMe
757e0 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 mory(const sqlit
757f0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
75800 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c VATE VTable *sql
75810 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 ite3GetVTable(sq
75820 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b lite3*, Table*);
75830 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f 6e ../* Declaration
75840 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 s for functions
75850 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20 6f in fkey.c. All o
75860 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70 6c f these are repl
75870 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f 70 aced by.** no-op
75880 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54 5f macros if OMIT_
75890 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 64 FOREIGN_KEY is d
758a0 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 efined. In this
758b0 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e 0a case no foreign.
758c0 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e 61 ** key functiona
758d0 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62 6c lity is availabl
758e0 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47 47 e. If OMIT_TRIGG
758f0 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62 75 ER is defined bu
75900 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49 47 t.** OMIT_FOREIG
75910 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f 6e N_KEY is not, on
75920 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 66 ly some of the f
75930 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 2d unctions are no-
75940 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69 73 oped. In.** this
75950 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b 65 case foreign ke
75960 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20 62 ys are parsed, b
75970 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e 63 ut no other func
75980 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a 2a tionality is .**
75990 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f 72 provided (enfor
759a0 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f 6e cement of FK con
759b0 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72 65 straints require
759c0 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 73 s the triggers s
759d0 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a 23 ub-system)..*/.#
759e0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
759f0 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
75a00 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 64 KEY) && !defined
75a10 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 (SQLITE_OMIT_TRI
75a20 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 GGER).SQLITE_PRI
75a30 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
75a40 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73 65 te3FkCheck(Parse
75a50 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
75a60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
75a70 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
75a80 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 50 te3FkDropTable(P
75a90 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20 2a arse*, SrcList *
75aa0 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 , Table*);.SQLIT
75ab0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
75ac0 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e sqlite3FkAction
75ad0 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a s(Parse*, Table*
75ae0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
75af0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75b00 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 46 E int sqlite3F
75b10 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65 2a kRequired(Parse*
75b20 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c 20 , Table*, int*,
75b30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
75b40 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69 74 VATE u32 sqlit
75b50 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72 73 e3FkOldmask(Pars
75b60 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c e*, Table*);.SQL
75b70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46 4b ITE_PRIVATE FK
75b80 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65 66 ey *sqlite3FkRef
75b90 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a 29 erences(Table *)
75ba0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e ;.#else. #defin
75bb0 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f e sqlite3FkActio
75bc0 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 64 ns(a,b,c,d). #d
75bd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 43 efine sqlite3FkC
75be0 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20 20 heck(a,b,c,d).
75bf0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 #define sqlite3F
75c00 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c 63 kDropTable(a,b,c
75c10 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
75c20 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c 62 te3FkOldmask(a,b
75c30 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66 69 ) 0. #defi
75c40 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 ne sqlite3FkRequ
75c50 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30 0a ired(a,b,c,d) 0.
75c60 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
75c70 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
75c80 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 GN_KEY.SQLITE_PR
75c90 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75ca0 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61 62 ite3FkDelete(Tab
75cb0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 le*);.#else. #d
75cc0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 44 efine sqlite3FkD
75cd0 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66 0a elete(a).#endif.
75ce0 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c ../*.** Availabl
75cf0 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 e fault injector
75d00 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 s. Should be nu
75d10 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 mbered beginning
75d20 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 with 0..*/.#def
75d30 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 ine SQLITE_FAULT
75d40 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 INJECTOR_MALLOC
75d50 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
75d60 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 LITE_FAULTINJECT
75d70 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a OR_COUNT 1.
75d80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 ./*.** The inter
75d90 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 face to the code
75da0 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 in fault.c used
75db0 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 for identifying
75dc0 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c "benign".** mal
75dd0 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 loc failures. Th
75de0 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 is is only prese
75df0 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 nt if SQLITE_OMI
75e00 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a T_BUILTIN_TEST.*
75e10 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 * is not defined
75e20 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
75e30 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
75e40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
75e50 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
75e60 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
75e70 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 lloc(void);.SQLI
75e80 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75e90 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 d sqlite3EndBeni
75ea0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a gnMalloc(void);.
75eb0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
75ec0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
75ed0 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 gnMalloc(). #de
75ee0 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 fine sqlite3EndB
75ef0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 enignMalloc().#e
75f00 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e ndif..#define IN
75f10 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 _INDEX_ROWID
75f20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
75f30 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 IN_INDEX_EPH
75f40 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
75f50 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 ine IN_INDEX_IND
75f60 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 EX 3.S
75f70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
75f80 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 t sqlite3FindInI
75f90 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 ndex(Parse *, Ex
75fa0 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 pr *, int*);..#i
75fb0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
75fc0 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
75fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75fe0 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
75ff0 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f nalOpen(sqlite3_
76000 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
76010 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c r *, sqlite3_fil
76020 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a e *, int, int);.
76030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
76040 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
76050 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f nalSize(sqlite3_
76060 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 vfs *);.SQLITE_P
76070 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
76080 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 ite3JournalCreat
76090 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
760a0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
760b0 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 ne sqlite3Journa
760c0 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 lSize(pVfs) ((pV
760d0 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 fs)->szOsFile).#
760e0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
760f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
76100 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
76110 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
76120 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76130 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a int sqlite3MemJ
76140 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 ournalSize(void)
76150 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76160 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 int sqlite3IsMe
76170 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 mJournal(sqlite3
76180 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 _file *);..#if S
76190 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
761a0 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 EPTH>0.SQLITE_PR
761b0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
761c0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
761d0 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
761e0 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 Expr *p);.SQLIT
761f0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
76200 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
76210 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a rHeight(Select *
76220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
76230 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 E int sqlite3E
76240 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 xprCheckHeight(P
76250 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c arse*, int);.#el
76260 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
76270 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
76280 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 t(x,y). #define
76290 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
762a0 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 prHeight(x) 0.
762b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 #define sqlite3E
762c0 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 xprCheckHeight(x
762d0 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 ,y).#endif..SQLI
762e0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
762f0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 qlite3Get4byte(c
76300 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 onst u8*);.SQLIT
76310 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
76320 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 qlite3Put4byte(u
76330 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 8*, u32);..#ifde
76340 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
76350 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 UNLOCK_NOTIFY.SQ
76360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
76370 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 oid sqlite3Conne
76380 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c ctionBlocked(sql
76390 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 ite3 *, sqlite3
763a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
763b0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
763c0 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 3ConnectionUnloc
763d0 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ked(sqlite3 *db)
763e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
763f0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
76400 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 onnectionClosed(
76410 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 sqlite3 *db);.#e
76420 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
76430 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
76440 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 locked(x,y). #d
76450 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e efine sqlite3Con
76460 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
76470 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c x). #define sql
76480 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c ite3ConnectionCl
76490 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a osed(x).#endif..
764a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
764b0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
764c0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
764d0 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 3ParserTrace(FIL
764e0 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e E*, char *);.#en
764f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 dif../*.** If th
76500 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 e SQLITE_ENABLE
76510 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74 IOTRACE exists t
76520 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 hen the global v
76530 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 ariable.** sqlit
76540 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 e3IoTrace is a p
76550 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e ointer to a prin
76560 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 tf-like routine
76570 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 used to.** print
76580 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 I/O tracing mes
76590 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 sages. .*/.#ifde
765a0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
765b0 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 IOTRACE.# define
765c0 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 IOTRACE(A) if(
765d0 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 sqlite3IoTrace
765e0 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 ){ sqlite3IoTrac
765f0 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 e A; }.SQLITE_PR
76600 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
76610 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 ite3VdbeIOTraceS
76620 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 ql(Vdbe*);.SQLIT
76630 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 E_PRIVATE void (
76640 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 *sqlite3IoTrace)
76650 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e (const char*,...
76660 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
76670 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 e IOTRACE(A).# d
76680 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 efine sqlite3Vdb
76690 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 eIOTraceSql(X).#
766a0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f endif..#endif../
766b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
766c0 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e nd of sqliteInt.
766d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
766e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
766f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
76700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
76710 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c egin file global
76720 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
76730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
76750 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 *.** 2008 June 1
76760 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
76770 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
76780 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
76790 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
767a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
767b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
767c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
767d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
767e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
767f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
76800 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
76810 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
76820 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
76830 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
76840 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
76850 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
76860 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
76870 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
76880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
768a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
768b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
768c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
768d0 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 contains defini
768e0 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 tions of global
768f0 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f variables and co
76900 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 20 41 ntants..*/../* A
76910 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 61 n array to map a
76920 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 68 ll upper-case ch
76930 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 aracters into th
76940 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e eir correspondin
76950 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 g.** lower-case
76960 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a character. .**.*
76970 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f * SQLite only co
76980 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 49 nsiders US-ASCII
76990 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 61 (or EBCDIC) cha
769a0 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f 20 racters. We do
769b0 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 not.** handle ca
769c0 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 se conversions f
769d0 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 72 61 or the UTF chara
769e0 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 20 74 cter set since t
769f0 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 he tables.** inv
76a00 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c 79 olved are nearly
76a10 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 67 65 as big or bigge
76a20 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69 74 r than SQLite it
76a30 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f self..*/.SQLITE_
76a40 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e PRIVATE const un
76a50 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 signed char sqli
76a60 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
76a70 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51 4c ] = {.#ifdef SQL
76a80 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20 20 ITE_ASCII.
76a90 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 0, 1, 2, 3,
76aa0 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 4, 5, 6, 7,
76ab0 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 8, 9, 10, 11, 1
76ac0 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 2, 13, 14, 15, 1
76ad0 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 6, 17,. 18,
76ae0 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 19, 20, 21, 22,
76af0 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 23, 24, 25, 26,
76b00 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 27, 28, 29, 30,
76b10 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 31, 32, 33, 34,
76b20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 35,. 36, 37,
76b30 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 38, 39, 40, 41,
76b40 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 42, 43, 44, 45,
76b50 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 46, 47, 48, 49,
76b60 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 50, 51, 52, 53,
76b70 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35 36 . 54, 55, 56
76b80 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 , 57, 58, 59, 60
76b90 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 , 61, 62, 63, 64
76ba0 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 , 97, 98, 99,100
76bb0 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 ,101,102,103,.
76bc0 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 104,105,106,10
76bd0 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 7,108,109,110,11
76be0 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 1,112,113,114,11
76bf0 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 5,116,117,118,11
76c00 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 9,120,121,. 1
76c10 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 22, 91, 92, 93,
76c20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 94, 95, 96, 97,
76c30 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 98, 99,100,101,1
76c40 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 02,103,104,105,1
76c50 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 06,107,. 108,
76c60 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 109,110,111,112,
76c70 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 113,114,115,116,
76c80 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 117,118,119,120,
76c90 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 121,122,123,124,
76ca0 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 125,. 126,127
76cb0 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 ,128,129,130,131
76cc0 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 ,132,133,134,135
76cd0 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 ,136,137,138,139
76ce0 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 ,140,141,142,143
76cf0 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 ,. 144,145,14
76d00 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 6,147,148,149,15
76d10 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 0,151,152,153,15
76d20 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 4,155,156,157,15
76d30 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 8,159,160,161,.
76d40 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 162,163,164,1
76d50 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 65,166,167,168,1
76d60 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 69,170,171,172,1
76d70 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 73,174,175,176,1
76d80 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 77,178,179,.
76d90 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 180,181,182,183,
76da0 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 184,185,186,187,
76db0 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 188,189,190,191,
76dc0 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 192,193,194,195,
76dd0 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 196,197,. 198
76de0 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 ,199,200,201,202
76df0 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 ,203,204,205,206
76e00 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 ,207,208,209,210
76e10 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 ,211,212,213,214
76e20 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 ,215,. 216,21
76e30 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 7,218,219,220,22
76e40 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 1,222,223,224,22
76e50 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 5,226,227,228,22
76e60 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 9,230,231,232,23
76e70 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c 32 3,. 234,235,2
76e80 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 36,237,238,239,2
76e90 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 40,241,242,243,2
76ea0 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 44,245,246,247,2
76eb0 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 48,249,250,251,.
76ec0 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 252,253,254,
76ed0 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 255.#endif.#ifde
76ee0 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a f SQLITE_EBCDIC.
76ef0 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 0, 1, 2,
76f00 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 3, 4, 5, 6,
76f10 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 7, 8, 9, 10,
76f20 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 11, 12, 13, 14,
76f30 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 15, /* 0x */.
76f40 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 16, 17, 18, 1
76f50 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 9, 20, 21, 22, 2
76f60 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 3, 24, 25, 26, 2
76f70 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 7, 28, 29, 30, 3
76f80 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 1, /* 1x */.
76f90 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 32, 33, 34, 35,
76fa0 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 36, 37, 38, 39,
76fb0 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 40, 41, 42, 43,
76fc0 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 44, 45, 46, 47,
76fd0 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 /* 2x */. 4
76fe0 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 8, 49, 50, 51, 5
76ff0 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 2, 53, 54, 55, 5
77000 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 6, 57, 58, 59, 6
77010 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 0, 61, 62, 63, /
77020 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c * 3x */. 64,
77030 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 65, 66, 67, 68,
77040 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 69, 70, 71, 72,
77050 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 73, 74, 75, 76,
77060 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 77, 78, 79, /*
77070 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 4x */. 80, 8
77080 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 1, 82, 83, 84, 8
77090 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 5, 86, 87, 88, 8
770a0 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 9, 90, 91, 92, 9
770b0 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 3, 94, 95, /* 5x
770c0 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37 2c */. 96, 97,
770d0 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 66, 67, 68, 69,
770e0 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 70, 71, 72, 73,
770f0 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 106,107,108,109,
77100 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 110,111, /* 6x *
77110 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20 38 /. 112, 81, 8
77120 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 2, 83, 84, 85, 8
77130 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 6, 87, 88, 89,12
77140 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 2,123,124,125,12
77150 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 6,127, /* 7x */.
77160 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 128,129,130,
77170 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 131,132,133,134,
77180 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 135,136,137,138,
77190 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 139,140,141,142,
771a0 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 143, /* 8x */.
771b0 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 144,145,146,14
771c0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 7,148,149,150,15
771d0 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 1,152,153,154,15
771e0 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 5,156,157,156,15
771f0 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 9, /* 9x */.
77200 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 160,161,162,163,
77210 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 164,165,166,167,
77220 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 168,169,170,171,
77230 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 140,141,142,175,
77240 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 /* Ax */. 17
77250 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 6,177,178,179,18
77260 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 0,181,182,183,18
77270 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 4,185,186,187,18
77280 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 8,189,190,191, /
77290 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c * Bx */. 192,
772a0 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 129,130,131,132,
772b0 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 133,134,135,136,
772c0 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 137,202,203,204,
772d0 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 205,206,207, /*
772e0 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 Cx */. 208,14
772f0 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 5,146,147,148,14
77300 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 9,150,151,152,15
77310 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 3,218,219,220,22
77320 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 1,222,223, /* Dx
77330 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35 2c */. 224,225,
77340 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 162,163,164,165,
77350 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 166,167,168,169,
77360 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 232,203,204,205,
77370 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 206,207, /* Ex *
77380 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 34 /. 239,240,24
77390 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 1,242,243,244,24
773a0 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 5,246,247,248,24
773b0 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 9,219,220,221,22
773c0 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 2,255, /* Fx */.
773d0 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
773e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 The following 2
773f0 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20 74 56 byte lookup t
77400 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
77410 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73 20 support SQLites
77420 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75 69 built-in.** equi
77430 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 66 valents to the f
77440 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 72 ollowing standar
77450 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 d library functi
77460 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73 ons:.**.** iss
77470 70 61 63 65 28 29 20 20 20 20 20 20 20 20 20 20 pace()
77480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
77490 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61 28 01.** isalpha(
774a0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
774b0 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a 2a 0x02.**
774c0 20 20 20 69 73 64 69 67 69 74 28 29 20 20 20 20 isdigit()
774d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
774e0 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69 73 0x04.** is
774f0 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20 20 alnum()
77500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
77510 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67 69 x06.** isxdigi
77520 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 t()
77530 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0a 2a 0x08.*
77540 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 20 20 * toupper()
77550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77560 20 20 20 20 20 30 78 32 30 0a 2a 2a 20 20 20 53 0x20.** S
77570 51 4c 69 74 65 20 69 64 65 6e 74 69 66 69 65 72 QLite identifier
77580 20 63 68 61 72 61 63 74 65 72 20 20 20 20 20 20 character
77590 30 78 34 30 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 0x40.**.** Bit 0
775a0 78 32 30 20 69 73 20 73 65 74 20 69 66 20 74 68 x20 is set if th
775b0 65 20 6d 61 70 70 65 64 20 63 68 61 72 61 63 74 e mapped charact
775c0 65 72 20 72 65 71 75 69 72 65 73 20 74 72 61 6e er requires tran
775d0 73 6c 61 74 69 6f 6e 20 74 6f 20 75 70 70 65 72 slation to upper
775e0 0a 2a 2a 20 63 61 73 65 2e 20 69 2e 65 2e 20 69 .** case. i.e. i
775f0 66 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 f the character
77600 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 is a lower-case
77610 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2e ASCII character.
77620 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 6c 6f .** If x is a lo
77630 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 wer-case ASCII c
77640 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 69 haracter, then i
77650 74 73 20 75 70 70 65 72 2d 63 61 73 65 20 65 71 ts upper-case eq
77660 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 uivalent.** is (
77670 78 20 2d 20 30 78 32 30 29 2e 20 54 68 65 72 65 x - 0x20). There
77680 66 6f 72 65 20 74 6f 75 70 70 65 72 28 29 20 63 fore toupper() c
77690 61 6e 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 an be implemente
776a0 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 d as:.**.** (x
776b0 20 26 20 7e 28 6d 61 70 5b 78 5d 26 30 78 32 30 & ~(map[x]&0x20
776c0 29 29 0a 2a 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 )).**.** Standar
776d0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 d function tolow
776e0 65 72 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e er() is implemen
776f0 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 71 ted using the sq
77700 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
77710 72 5b 5d 0a 2a 2a 20 61 72 72 61 79 2e 20 74 6f r[].** array. to
77720 6c 6f 77 65 72 28 29 20 69 73 20 75 73 65 64 20 lower() is used
77730 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 more often than
77740 74 6f 75 70 70 65 72 28 29 20 62 79 20 53 51 4c toupper() by SQL
77750 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 ite..**.** Bit 0
77760 78 34 30 20 69 73 20 73 65 74 20 69 66 20 74 68 x40 is set if th
77770 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f 6e 2d e character non-
77780 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 61 6e 64 alphanumeric and
77790 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 can be used in
777a0 61 6e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 69 64 an .** SQLite id
777b0 65 6e 74 69 66 69 65 72 2e 20 20 49 64 65 6e 74 entifier. Ident
777c0 69 66 69 65 72 73 20 61 72 65 20 61 6c 70 68 61 ifiers are alpha
777d0 6e 75 6d 65 72 69 63 73 2c 20 22 5f 22 2c 20 22 numerics, "_", "
777e0 24 22 2c 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 6e $", and any.** n
777f0 6f 6e 2d 41 53 43 49 49 20 55 54 46 20 63 68 61 on-ASCII UTF cha
77800 72 61 63 74 65 72 2e 20 48 65 6e 63 65 20 74 68 racter. Hence th
77810 65 20 74 65 73 74 20 66 6f 72 20 77 68 65 74 68 e test for wheth
77820 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 68 61 72 er or not a char
77830 61 63 74 65 72 20 69 73 0a 2a 2a 20 70 61 72 74 acter is.** part
77840 20 6f 66 20 61 6e 20 69 64 65 6e 74 69 66 69 65 of an identifie
77850 72 20 69 73 20 30 78 34 36 2e 0a 2a 2a 0a 2a 2a r is 0x46..**.**
77860 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f SQLite's versio
77870 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c ns are identical
77880 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 to the standard
77890 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69 versions assumi
778a0 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f ng a.** locale o
778b0 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 20 f "C". They are
778c0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d implemented as m
778d0 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49 acros in sqliteI
778e0 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 nt.h..*/.#ifdef
778f0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c SQLITE_ASCII.SQL
77900 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
77910 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
77920 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
77930 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 256] = {. 0x00,
77940 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77950 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77960 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 x00, 0x00, /* 0
77970 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..07 .......
77980 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
77990 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 1, 0x01, 0x01, 0
779a0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x01, 0x01, 0x00,
779b0 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 0x00, /* 08..0
779c0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
779d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
779e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
779f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a00 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20 0, /* 10..17
77a10 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77a20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77a30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a40 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77a50 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e /* 18..1f ...
77a60 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c ..... */. 0x01,
77a70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a80 30 2c 20 30 78 34 30 2c 20 30 78 30 30 2c 20 30 0, 0x40, 0x00, 0
77a90 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 x00, 0x00, /* 2
77aa0 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 26 0..27 !"#$%&
77ab0 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 ' */. 0x00, 0x0
77ac0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77ad0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77ae0 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 0x00, /* 28..2
77af0 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f f ()*+,-./ */
77b00 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 . 0x0c, 0x0c, 0
77b10 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
77b20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
77b30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20 c, /* 30..37
77b40 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30 01234567 */. 0
77b50 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c x0c, 0x0c, 0x00,
77b60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77b70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77b80 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a /* 38..3f 89:
77b90 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 ;<=>? */.. 0x00
77ba0 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 , 0x0a, 0x0a, 0x
77bb0 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 0a, 0x0a, 0x0a,
77bc0 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20 0x0a, 0x02, /*
77bd0 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45 40..47 @ABCDE
77be0 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 FG */. 0x02, 0x
77bf0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
77c00 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77c10 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e , 0x02, /* 48..
77c20 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 4f HIJKLMNO *
77c30 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 /. 0x02, 0x02,
77c40 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77c50 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
77c60 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20 02, /* 50..57
77c70 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20 PQRSTUVW */.
77c80 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77c90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77ca0 30 30 2c 20 30 78 30 30 2c 20 30 78 34 30 2c 20 00, 0x00, 0x40,
77cb0 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59 /* 58..5f XY
77cc0 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 Z[\]^_ */. 0x00
77cd0 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 , 0x2a, 0x2a, 0x
77ce0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 2a, 0x2a, 0x2a,
77cf0 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20 0x2a, 0x22, /*
77d00 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65 60..67 `abcde
77d10 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 fg */. 0x22, 0x
77d20 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
77d30 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77d40 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e , 0x22, /* 68..
77d50 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 6f hijklmno *
77d60 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 /. 0x22, 0x22,
77d70 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77d80 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
77d90 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20 22, /* 70..77
77da0 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20 pqrstuvw */.
77db0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77dc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77dd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77de0 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79 /* 78..7f xy
77df0 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 34 z{|}~. */.. 0x4
77e00 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77e10 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77e20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 0x40, 0x40, /*
77e30 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 80..87 .....
77e40 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 ... */. 0x40, 0
77e50 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77e60 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77e70 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38 38 2e 0, 0x40, /* 88.
77e80 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .8f ........
77e90 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c */. 0x40, 0x40,
77ea0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77eb0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77ec0 78 34 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20 x40, /* 90..97
77ed0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
77ee0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77ef0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77f00 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77f10 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e /* 98..9f .
77f20 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 ....... */. 0x4
77f30 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77f40 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77f50 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 0x40, 0x40, /*
77f60 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e a0..a7 .....
77f70 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 ... */. 0x40, 0
77f80 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77f90 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77fa0 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 61 38 2e 0, 0x40, /* a8.
77fb0 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .af ........
77fc0 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c */. 0x40, 0x40,
77fd0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77fe0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77ff0 78 34 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20 x40, /* b0..b7
78000 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
78010 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
78020 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
78030 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
78040 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e /* b8..bf .
78050 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 ....... */.. 0x
78060 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78070 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
78080 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f , 0x40, 0x40, /
78090 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e * c0..c7 ....
780a0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 .... */. 0x40,
780b0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
780c0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
780d0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 38 40, 0x40, /* c8
780e0 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..cf ........
780f0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
78100 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
78110 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78120 30 78 34 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 0x40, /* d0..d7
78130 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
78140 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
78150 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78160 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
78170 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20 , /* d8..df
78180 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
78190 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
781a0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
781b0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f , 0x40, 0x40, /
781c0 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e * e0..e7 ....
781d0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 .... */. 0x40,
781e0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
781f0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
78200 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 38 40, 0x40, /* e8
78210 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..ef ........
78220 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
78230 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
78240 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78250 30 78 34 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 0x40, /* f0..f7
78260 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
78270 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
78280 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78290 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
782a0 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20 /* f8..ff
782b0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 ........ */.};.#
782c0 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 endif..../*.** T
782d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e he following sin
782e0 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 gleton contains
782f0 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 the global confi
78300 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 guration for.**
78310 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
78320 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ry..*/.SQLITE_PR
78330 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 IVATE SQLITE_WSD
78340 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 struct Sqlite3C
78350 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e onfig sqlite3Con
78360 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 fig = {. SQLIT
78370 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 E_DEFAULT_MEMSTA
78380 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 TUS, /* bMemsta
78390 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 t */. 1,
783a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
783b0 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 /* bCoreMutex
783c0 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 */. SQLITE_TH
783d0 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 READSAFE==1,
783e0 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 /* bFullMutex
783f0 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 */. 0x7ffffffe
78400 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
78410 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a /* mxStrlen */.
78420 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 100,
78430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78440 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a szLookaside */.
78450 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 500,
78460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78470 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 nLookaside */.
78480 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0
78490 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ,0}, /*
784a0 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 m */. {0,0,0,0
784b0 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 ,0,0,0,0,0},
784c0 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 /* mutex */.
784d0 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0
784e0 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 ,0,0,0,0}, /*
784f0 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f pcache */. (vo
78500 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 id*)0,
78510 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 /* pHeap
78520 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78540 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 /* nHeap */.
78550 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 0, 0,
78560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d /* m
78570 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f nHeap, mxHeap */
78580 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 . (void*)0,
78590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
785a0 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 * pScratch */.
785b0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
785c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
785d0 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 zScratch */. 0
785e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
785f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 /* nSc
78600 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 ratch */. (voi
78610 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 d*)0,
78620 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
78630 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
78640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78650 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 /* szPage */.
78660 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78670 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
78680 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Page */. 0,
78690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
786a0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 /* mxParse
786b0 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 rStack */. 0,
786c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
786d0 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 /* share
786e0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f dCacheEnabled */
786f0 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 . /* All the r
78700 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 est should alway
78710 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 s be initialized
78720 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 to zero */. 0
78730 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
78740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 /* isI
78750 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
78760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78770 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 /* inProgre
78780 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ss */. 0,
78790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
787a0 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e /* isMutexIn
787b0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 it */. 0,
787c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
787d0 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 /* isMallocI
787e0 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
787f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78800 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 /* isPCache
78810 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
78820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78830 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 /* pInitMu
78840 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 tex */. 0,
78850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78860 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 /* nRefInit
78870 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a Mutex */.};.../*
78880 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 .** Hash table f
78890 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 or global functi
788a0 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 ons - functions
788b0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
788c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
788d0 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e tions. After in
788e0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 itialization, th
788f0 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 is table is.** r
78900 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c ead-only..*/.SQL
78910 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 ITE_PRIVATE SQLI
78920 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 TE_WSD FuncDefHa
78930 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sh sqlite3Global
78940 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a Functions;../*.*
78950 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * The value of t
78960 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74 he "pending" byt
78970 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 30 e must be 0x4000
78980 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 73 0000 (1 byte pas
78990 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62 t the.** 1-gibab
789a0 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e yte boundary) in
789b0 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 a compatible da
789c0 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20 tabase. SQLite
789d0 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68 never uses.** th
789e0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
789f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
78a00 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20 e pending byte.
78a10 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70 It never attemp
78a20 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72 ts.** to read or
78a30 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 65 write that page
78a40 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62 . The pending b
78a50 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 20 yte page is set
78a60 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 assign.** for us
78a70 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 79 e by the VFS lay
78a80 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f 72 ers as space for
78a90 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c managing file l
78aa0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 ocks..**.** Duri
78ab0 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69 ng testing, it i
78ac0 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c s often desirabl
78ad0 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65 e to move the pe
78ae0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a nding byte to.**
78af0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73 a different pos
78b00 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c ition in the fil
78b10 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 e. This allows
78b20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 6f code that has to
78b30 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68 .** deal with th
78b40 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 e pending byte t
78b50 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74 o run on files t
78b60 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61 hat are much sma
78b70 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47 ller.** than 1 G
78b80 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 iB. The sqlite3
78b90 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 _test_control()
78ba0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
78bb0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 used to.** move
78bc0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 the pending byt
78bd0 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 e..**.** IMPORTA
78be0 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 NT: Changing th
78bf0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 e pending byte t
78c00 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 o any value othe
78c10 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30 r than.** 0x4000
78c20 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20 0000 results in
78c30 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 an incompatible
78c40 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f database file fo
78c50 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e rmat!.** Changin
78c60 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 g the pending by
78c70 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74 te during operat
78c80 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ing results in u
78c90 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 ndefined.** and
78ca0 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 dileterious beha
78cb0 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f vior..*/.SQLITE_
78cc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
78cd0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d te3PendingByte =
78ce0 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 0x40000000;../*
78cf0 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f .** Properties o
78d00 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20 f opcodes. The
78d10 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
78d20 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72 R macro is.** cr
78d30 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64 eated by mkopcod
78d40 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f eh.awk during co
78d50 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61 mpilation. Data
78d60 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 is obtained.**
78d70 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 from the comment
78d80 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 s following the
78d90 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20 "case OP_xxxx:"
78da0 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a statements in.**
78db0 20 74 68 65 20 76 64 62 65 2e 63 20 66 69 6c 65 the vdbe.c file
78dc0 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 . .*/.SQLITE_PR
78dd0 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 IVATE const unsi
78de0 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 gned char sqlite
78df0 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 3OpcodeProperty[
78e00 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 ] = OPFLG_INITIA
78e10 4c 49 5a 45 52 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a LIZER;../*******
78e20 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 ******* End of g
78e30 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a lobal.c ********
78e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
78e70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
78e80 6c 65 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a le status.c ****
78e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78eb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
78ec0 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 08 June 18.**.**
78ed0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
78ee0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
78ef0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
78f00 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
78f10 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
78f20 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
78f30 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
78f40 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
78f50 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
78f60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
78f70 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
78f80 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
78f90 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
78fa0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
78fb0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
78fc0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
78fd0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
78fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79020 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
79030 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c This module impl
79040 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 ements the sqlit
79050 65 33 5f 73 74 61 74 75 73 28 29 20 69 6e 74 65 e3_status() inte
79060 72 66 61 63 65 20 61 6e 64 20 72 65 6c 61 74 65 rface and relate
79070 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c 69 d.** functionali
79080 74 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 ty..*/../*.** Va
79090 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 riables in which
790a0 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 to record statu
790b0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a s information..*
790c0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
790d0 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 sqlite3StatType
790e0 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 sqlite3StatType
790f0 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f ;.static SQLITE_
79100 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 WSD struct sqlit
79110 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 e3StatType {. i
79120 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 nt nowValue[9];
79130 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
79140 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e nt value */. in
79150 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 t mxValue[9];
79160 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
79170 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c m value */.} sql
79180 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c ite3Stat = { {0,
79190 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 }, {0,} };.../*
791a0 54 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 The "wsdStat" ma
791b0 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 cro will resolve
791c0 20 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 to the status i
791d0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 nformation.** st
791e0 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 ate vector. If
791f0 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 writable static
79200 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 data is unsuppor
79210 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 ted on the targe
79220 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f t,.** we have to
79230 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 locate the stat
79240 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d e vector at run-
79250 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f time. In the mo
79260 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 re common.** cas
79270 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 e where writable
79280 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 static data is
79290 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 supported, wsdSt
792a0 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 at can refer dir
792b0 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 ectly.** to the
792c0 22 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 "sqlite3Stat" st
792d0 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 ate vector decla
792e0 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 red above..*/.#i
792f0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
79300 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 _WSD.# define ws
79310 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 dStatInit sqlit
79320 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 e3StatType *x =
79330 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 &GLOBAL(sqlite3S
79340 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 tatType,sqlite3S
79350 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 tat).# define ws
79360 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 dStat x[0].#else
79370 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
79380 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 tInit.# define w
79390 73 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 sdStat sqlite3St
793a0 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a at.#endif../*.**
793b0 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
793c0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 ent value of a s
793d0 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e tatus parameter.
793e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
793f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
79400 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 atusValue(int op
79410 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 ){. wsdStatInit
79420 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d ;. assert( op>=
79430 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 0 && op<ArraySiz
79440 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
79450 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ue) );. return
79460 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
79470 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 [op];.}../*.** A
79480 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 dd N to the valu
79490 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 e of a status re
794a0 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 cord. It is ass
794b0 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a umed that the.**
794c0 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 caller holds ap
794d0 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e propriate locks.
794e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
794f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
79500 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c tatusAdd(int op,
79510 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 int N){. wsdSt
79520 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 atInit;. assert
79530 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 ( op>=0 && op<Ar
79540 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e raySize(wsdStat.
79550 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 nowValue) );. w
79560 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
79570 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 op] += N;. if(
79580 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
79590 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 [op]>wsdStat.mxV
795a0 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 alue[op] ){.
795b0 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b wsdStat.mxValue[
795c0 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f op] = wsdStat.no
795d0 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a wValue[op];. }.
795e0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
795f0 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 value of a stat
79600 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 us to X..*/.SQLI
79610 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
79620 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 sqlite3StatusSet
79630 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b (int op, int X){
79640 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a . wsdStatInit;.
79650 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 assert( op>=0
79660 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 && op<ArraySize(
79670 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
79680 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e ) );. wsdStat.n
79690 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b owValue[op] = X;
796a0 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e . if( wsdStat.n
796b0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 owValue[op]>wsdS
796c0 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 tat.mxValue[op]
796d0 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d ){. wsdStat.m
796e0 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 xValue[op] = wsd
796f0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 Stat.nowValue[op
79700 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ];. }.}../*.**
79710 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 Query status inf
79720 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 ormation..**.**
79730 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
79740 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
79750 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 reading or writ
79760 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a ing an aligned.*
79770 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 * 32-bit integer
79780 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 is an atomic op
79790 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 eration. If tha
797a0 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 t assumption is
797b0 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 not true,.** the
797c0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
797d0 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 s not threadsafe
797e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
797f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 int sqlite3_stat
79800 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a us(int op, int *
79810 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 pCurrent, int *p
79820 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 Highwater, int r
79830 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 esetFlag){. wsd
79840 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 StatInit;. if(
79850 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 op<0 || op>=Arra
79860 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f ySize(wsdStat.no
79870 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 wValue) ){. r
79880 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
79890 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 USE;. }. *pCur
798a0 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e rent = wsdStat.n
798b0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a owValue[op];. *
798c0 70 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 pHighwater = wsd
798d0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d Stat.mxValue[op]
798e0 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 ;. if( resetFla
798f0 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 g ){. wsdStat
79900 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 .mxValue[op] = w
79910 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
79920 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 op];. }. retur
79930 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
79940 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 /*.** Query stat
79950 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 us information f
79960 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 or a single data
79970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
79980 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
79990 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 t sqlite3_db_sta
799a0 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a tus(. sqlite3 *
799b0 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 db, /*
799c0 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
799d0 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 nection whose st
799e0 61 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 atus is desired
799f0 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
79a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
79a10 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 atus verb */. i
79a20 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 nt *pCurrent,
79a30 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 /* Write cu
79a40 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 rrent value here
79a50 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 */. int *pHigh
79a60 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 water, /* W
79a70 72 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 rite high-water
79a80 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 mark here */. i
79a90 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 nt resetFlag
79aa0 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 /* Reset hi
79ab0 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 gh-water mark if
79ac0 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 true */.){. sw
79ad0 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
79ae0 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 case SQLITE_DBST
79af0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 ATUS_LOOKASIDE_U
79b00 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 SED: {. *pC
79b10 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f urrent = db->loo
79b20 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 kaside.nOut;.
79b30 20 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d *pHighwater =
79b40 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d db->lookaside.m
79b50 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 xOut;. if(
79b60 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 resetFlag ){.
79b70 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
79b80 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c de.mxOut = db->l
79b90 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 ookaside.nOut;.
79ba0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
79bb0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
79bc0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 fault: {. r
79bd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
79be0 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 OR;. }. }.
79bf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
79c00 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
79c10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 **** End of stat
79c20 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a us.c ***********
79c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79c50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
79c60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
79c70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a date.c *********
79c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79ca0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 ***/./*.** 2003
79cb0 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a October 31.**.**
79cc0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
79cd0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
79ce0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
79cf0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
79d00 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
79d10 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
79d20 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
79d30 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
79d40 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
79d50 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
79d60 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
79d70 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
79d80 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
79d90 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
79da0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
79db0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
79dc0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
79dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
79e20 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
79e30 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
79e40 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 that implement d
79e50 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 ate and time.**
79e60 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 functions for SQ
79e70 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 Lite. .**.** Th
79e80 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 ere is only one
79e90 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 exported symbol
79ea0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 in this file - t
79eb0 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 he function.** s
79ec0 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 qlite3RegisterDa
79ed0 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 teTimeFunctions(
79ee0 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 ) found at the b
79ef0 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c ottom of the fil
79f00 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 e..** All other
79f10 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 code has file sc
79f20 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ope..**.** SQLit
79f30 65 20 70 72 6f 63 65 73 73 65 73 20 61 6c 6c 20 e processes all
79f40 74 69 6d 65 73 20 61 6e 64 20 64 61 74 65 73 20 times and dates
79f50 61 73 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 as Julian Day nu
79f60 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a 2a 20 64 mbers. The.** d
79f70 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73 20 61 ates and times a
79f80 72 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 re stored as the
79f90 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 number of days
79fa0 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e since noon.** in
79fb0 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e 6f Greenwich on No
79fc0 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31 34 20 vember 24, 4714
79fd0 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e 67 20 74 B.C. according t
79fe0 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 0a o the Gregorian.
79ff0 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74 ** calendar syst
7a000 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d em. .**.** 1970-
7a010 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 01-01 00:00:00 i
7a020 73 20 4a 44 20 32 34 34 30 35 38 37 2e 35 0a 2a s JD 2440587.5.*
7a030 2a 20 32 30 30 30 2d 30 31 2d 30 31 20 30 30 3a * 2000-01-01 00:
7a040 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 35 31 00:00 is JD 2451
7a050 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 544.5.**.** This
7a060 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 implemention re
7a070 71 75 69 72 65 73 20 79 65 61 72 73 20 74 6f 20 quires years to
7a080 62 65 20 65 78 70 72 65 73 73 65 64 20 61 73 20 be expressed as
7a090 61 20 34 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 a 4-digit number
7a0a0 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 .** which means
7a0b0 74 68 61 74 20 6f 6e 6c 79 20 64 61 74 65 73 20 that only dates
7a0c0 62 65 74 77 65 65 6e 20 30 30 30 30 2d 30 31 2d between 0000-01-
7a0d0 30 31 20 61 6e 64 20 39 39 39 39 2d 31 32 2d 33 01 and 9999-12-3
7a0e0 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72 65 70 72 1 can.** be repr
7a0f0 65 73 65 6e 74 65 64 2c 20 65 76 65 6e 20 74 68 esented, even th
7a100 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 61 79 20 ough julian day
7a110 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 20 61 20 numbers allow a
7a120 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a 20 72 61 much wider.** ra
7a130 6e 67 65 20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a nge of dates..**
7a140 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f 72 69 61 .** The Gregoria
7a150 6e 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65 n calendar syste
7a160 6d 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c m is used for al
7a170 6c 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 l dates and time
7a180 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 6f 73 65 s,.** even those
7a190 20 74 68 61 74 20 70 72 65 64 61 74 65 20 74 68 that predate th
7a1a0 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 e Gregorian cale
7a1b0 6e 64 61 72 2e 20 20 48 69 73 74 6f 72 69 61 6e ndar. Historian
7a1c0 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 s usually.** use
7a1d0 20 74 68 65 20 4a 75 6c 69 61 6e 20 63 61 6c 65 the Julian cale
7a1e0 6e 64 61 72 20 66 6f 72 20 64 61 74 65 73 20 70 ndar for dates p
7a1f0 72 69 6f 72 20 74 6f 20 31 35 38 32 2d 31 30 2d rior to 1582-10-
7a200 31 35 20 61 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 15 and for some.
7a210 2a 2a 20 64 61 74 65 73 20 61 66 74 65 72 77 61 ** dates afterwa
7a220 72 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f rds, depending o
7a230 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65 77 61 72 n locale. Bewar
7a240 65 20 6f 66 20 74 68 69 73 20 64 69 66 66 65 72 e of this differ
7a250 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ence..**.** The
7a260 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c 67 6f 72 conversion algor
7a270 69 74 68 6d 73 20 61 72 65 20 69 6d 70 6c 65 6d ithms are implem
7a280 65 6e 74 65 64 20 62 61 73 65 64 20 6f 6e 20 64 ented based on d
7a290 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 escriptions.** i
7a2a0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
7a2b0 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 text:.**.**
7a2c0 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 Jean Meeus.**
7a2d0 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69 63 61 6c Astronomical
7a2e0 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20 32 6e 64 Algorithms, 2nd
7a2f0 20 45 64 69 74 69 6f 6e 2c 20 31 39 39 38 0a 2a Edition, 1998.*
7a300 2a 20 20 20 20 20 20 49 53 42 4d 20 30 2d 39 34 * ISBM 0-94
7a310 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 3396-61-1.**
7a320 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c Willmann-Bell,
7a330 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20 52 69 63 Inc.** Ric
7a340 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e 69 61 20 hmond, Virginia
7a350 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 (USA).*/.#includ
7a360 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e e <time.h>..#ifn
7a370 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7a380 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 0a DATETIME_FUNCS..
7a390 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63 65 6e 74 20 /*.** On recent
7a3a0 57 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d Windows platform
7a3b0 73 2c 20 74 68 65 20 6c 6f 63 61 6c 74 69 6d 65 s, the localtime
7a3c0 5f 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 _s() function is
7a3d0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 available.** as
7a3e0 20 70 61 72 74 20 6f 66 20 74 68 65 20 22 53 65 part of the "Se
7a3f0 63 75 72 65 20 43 52 54 22 2e 20 49 74 20 69 73 cure CRT". It is
7a400 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 65 71 75 essentially equ
7a410 69 76 61 6c 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c ivalent to .** l
7a420 6f 63 61 6c 74 69 6d 65 5f 72 28 29 20 61 76 61 ocaltime_r() ava
7a430 69 6c 61 62 6c 65 20 75 6e 64 65 72 20 6d 6f 73 ilable under mos
7a440 74 20 50 4f 53 49 58 20 70 6c 61 74 66 6f 72 6d t POSIX platform
7a450 73 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 74 s, except that t
7a460 68 65 20 0a 2a 2a 20 6f 72 64 65 72 20 6f 66 20 he .** order of
7a470 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 the parameters i
7a480 73 20 72 65 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a s reversed..**.*
7a490 2a 20 53 65 65 20 68 74 74 70 3a 2f 2f 6d 73 64 * See http://msd
7a4a0 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f n.microsoft.com/
7a4b0 65 6e 2d 75 73 2f 6c 69 62 72 61 72 79 2f 61 34 en-us/library/a4
7a4c0 34 32 78 33 79 65 28 56 53 2e 38 30 29 2e 61 73 42x3ye(VS.80).as
7a4d0 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 px..**.** If the
7a4e0 20 75 73 65 72 20 68 61 73 20 6e 6f 74 20 69 6e user has not in
7a4f0 64 69 63 61 74 65 64 20 74 6f 20 75 73 65 20 6c dicated to use l
7a500 6f 63 61 6c 74 69 6d 65 5f 72 28 29 20 6f 72 20 ocaltime_r() or
7a510 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a localtime_s().**
7a520 20 61 6c 72 65 61 64 79 2c 20 63 68 65 63 6b 20 already, check
7a530 66 6f 72 20 61 6e 20 4d 53 56 43 20 62 75 69 6c for an MSVC buil
7a540 64 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 68 d environment th
7a550 61 74 20 70 72 6f 76 69 64 65 73 20 0a 2a 2a 20 at provides .**
7a560 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a localtime_s()..*
7a570 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 48 /.#if !defined(H
7a580 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 AVE_LOCALTIME_R)
7a590 20 26 26 20 21 64 65 66 69 6e 65 64 28 48 41 56 && !defined(HAV
7a5a0 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 E_LOCALTIME_S) &
7a5b0 26 20 5c 0a 20 20 20 20 20 64 65 66 69 6e 65 64 & \. defined
7a5c0 28 5f 4d 53 43 5f 56 45 52 29 20 26 26 20 64 65 (_MSC_VER) && de
7a5d0 66 69 6e 65 64 28 5f 43 52 54 5f 49 4e 53 45 43 fined(_CRT_INSEC
7a5e0 55 52 45 5f 44 45 50 52 45 43 41 54 45 29 0a 23 URE_DEPRECATE).#
7a5f0 64 65 66 69 6e 65 20 48 41 56 45 5f 4c 4f 43 41 define HAVE_LOCA
7a600 4c 54 49 4d 45 5f 53 20 31 0a 23 65 6e 64 69 66 LTIME_S 1.#endif
7a610 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 ../*.** A struct
7a620 75 72 65 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 ure for holding
7a630 61 20 73 69 6e 67 6c 65 20 64 61 74 65 20 61 6e a single date an
7a640 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 d time..*/.typed
7a650 65 66 20 73 74 72 75 63 74 20 44 61 74 65 54 69 ef struct DateTi
7a660 6d 65 20 44 61 74 65 54 69 6d 65 3b 0a 73 74 72 me DateTime;.str
7a670 75 63 74 20 44 61 74 65 54 69 6d 65 20 7b 0a 20 uct DateTime {.
7a680 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 sqlite3_int64 i
7a690 4a 44 3b 20 2f 2a 20 54 68 65 20 6a 75 6c 69 61 JD; /* The julia
7a6a0 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 74 69 6d n day number tim
7a6b0 65 73 20 38 36 34 30 30 30 30 30 20 2a 2f 0a 20 es 86400000 */.
7a6c0 20 69 6e 74 20 59 2c 20 4d 2c 20 44 3b 20 20 20 int Y, M, D;
7a6d0 20 20 20 20 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e /* Year, mon
7a6e0 74 68 2c 20 61 6e 64 20 64 61 79 20 2a 2f 0a 20 th, and day */.
7a6f0 20 69 6e 74 20 68 2c 20 6d 3b 20 20 20 20 20 20 int h, m;
7a700 20 20 20 20 2f 2a 20 48 6f 75 72 20 61 6e 64 20 /* Hour and
7a710 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 minutes */. int
7a720 20 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 tz;
7a730 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 /* Timezone offs
7a740 65 74 20 69 6e 20 6d 69 6e 75 74 65 73 20 2a 2f et in minutes */
7a750 0a 20 20 64 6f 75 62 6c 65 20 73 3b 20 20 20 20 . double s;
7a760 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73 /* Seconds
7a770 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 */. char valid
7a780 59 4d 44 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 YMD; /* True
7a790 20 28 31 29 20 69 66 20 59 2c 4d 2c 44 20 61 72 (1) if Y,M,D ar
7a7a0 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 e valid */. cha
7a7b0 72 20 76 61 6c 69 64 48 4d 53 3b 20 20 20 20 20 r validHMS;
7a7c0 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 68 /* True (1) if h
7a7d0 2c 6d 2c 73 20 61 72 65 20 76 61 6c 69 64 20 2a ,m,s are valid *
7a7e0 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 4a 44 /. char validJD
7a7f0 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 ; /* True (
7a800 31 29 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c 1) if iJD is val
7a810 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c id */. char val
7a820 69 64 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72 idTZ; /* Tr
7a830 75 65 20 28 31 29 20 69 66 20 74 7a 20 69 73 20 ue (1) if tz is
7a840 76 61 6c 69 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a valid */.};.../*
7a850 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 7a 44 61 74 .** Convert zDat
7a860 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f e into one or mo
7a870 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 41 64 re integers. Ad
7a880 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e ditional argumen
7a890 74 73 0a 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72 ts.** come in gr
7a8a0 6f 75 70 73 20 6f 66 20 35 20 61 73 20 66 6f 6c oups of 5 as fol
7a8b0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lows:.**.**
7a8c0 20 20 4e 20 20 20 20 20 20 20 6e 75 6d 62 65 72 N number
7a8d0 20 6f 66 20 64 69 67 69 74 73 20 69 6e 20 74 68 of digits in th
7a8e0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 e integer.**
7a8f0 20 20 20 6d 69 6e 20 20 20 20 20 6d 69 6e 69 6d min minim
7a900 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 um allowed value
7a910 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
7a920 2a 2a 20 20 20 20 20 20 20 6d 61 78 20 20 20 20 ** max
7a930 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 maximum allowed
7a940 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
7a950 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6e teger.** n
7a960 65 78 74 43 20 20 20 66 69 72 73 74 20 63 68 61 extC first cha
7a970 72 61 63 74 65 72 20 61 66 74 65 72 20 74 68 65 racter after the
7a980 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
7a990 20 20 70 56 61 6c 20 20 20 20 77 68 65 72 65 20 pVal where
7a9a0 74 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 74 to write the int
7a9b0 65 67 65 72 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a egers value..**.
7a9c0 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 63 ** Conversions c
7a9d0 6f 6e 74 69 6e 75 65 20 75 6e 74 69 6c 20 6f 6e ontinue until on
7a9e0 65 20 77 69 74 68 20 6e 65 78 74 43 3d 3d 30 20 e with nextC==0
7a9f0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
7aa00 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 ** The function
7aa10 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
7aa20 65 72 20 6f 66 20 73 75 63 63 65 73 73 66 75 6c er of successful
7aa30 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f conversions..*/
7aa40 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 44 .static int getD
7aa50 69 67 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 igits(const char
7aa60 20 2a 7a 44 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 *zDate, ...){.
7aa70 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 va_list ap;. i
7aa80 6e 74 20 76 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b nt val;. int N;
7aa90 0a 20 20 69 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e . int min;. in
7aaa0 74 20 6d 61 78 3b 0a 20 20 69 6e 74 20 6e 65 78 t max;. int nex
7aab0 74 43 3b 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b tC;. int *pVal;
7aac0 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a . int cnt = 0;.
7aad0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
7aae0 44 61 74 65 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 Date);. do{.
7aaf0 20 4e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 N = va_arg(ap,
7ab00 69 6e 74 29 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 int);. min =
7ab10 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b va_arg(ap, int);
7ab20 0a 20 20 20 20 6d 61 78 20 3d 20 76 61 5f 61 72 . max = va_ar
7ab30 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 g(ap, int);.
7ab40 6e 65 78 74 43 20 3d 20 76 61 5f 61 72 67 28 61 nextC = va_arg(a
7ab50 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 70 56 61 p, int);. pVa
7ab60 6c 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 l = va_arg(ap, i
7ab70 6e 74 2a 29 3b 0a 20 20 20 20 76 61 6c 20 3d 20 nt*);. val =
7ab80 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 0;. while( N-
7ab90 2d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 - ){. if( !
7aba0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
7abb0 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 zDate) ){.
7abc0 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 goto end_getDi
7abd0 67 69 74 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 gits;. }.
7abe0 20 20 20 20 76 61 6c 20 3d 20 76 61 6c 2a 31 30 val = val*10
7abf0 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b + *zDate - '0';
7ac00 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a . zDate++;.
7ac10 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 61 }. if( va
7ac20 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 l<min || val>max
7ac30 20 7c 7c 20 28 6e 65 78 74 43 21 3d 30 20 26 26 || (nextC!=0 &&
7ac40 20 6e 65 78 74 43 21 3d 2a 7a 44 61 74 65 29 20 nextC!=*zDate)
7ac50 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e ){. goto en
7ac60 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 d_getDigits;.
7ac70 20 7d 0a 20 20 20 20 2a 70 56 61 6c 20 3d 20 76 }. *pVal = v
7ac80 61 6c 3b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b al;. zDate++;
7ac90 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 . cnt++;. }w
7aca0 68 69 6c 65 28 20 6e 65 78 74 43 20 29 3b 0a 65 hile( nextC );.e
7acb0 6e 64 5f 67 65 74 44 69 67 69 74 73 3a 0a 20 20 nd_getDigits:.
7acc0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 va_end(ap);. re
7acd0 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a turn cnt;.}../*.
7ace0 2a 2a 20 52 65 61 64 20 74 65 78 74 20 66 72 6f ** Read text fro
7acf0 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f 6e 76 65 72 m z[] and conver
7ad00 74 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e t into a floatin
7ad10 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20 g point number.
7ad20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e Return.** the n
7ad30 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 umber of digits
7ad40 63 6f 6e 76 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 converted..*/.#d
7ad50 65 66 69 6e 65 20 67 65 74 56 61 6c 75 65 20 73 efine getValue s
7ad60 71 6c 69 74 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a qlite3AtoF../*.*
7ad70 2a 20 50 61 72 73 65 20 61 20 74 69 6d 65 7a 6f * Parse a timezo
7ad80 6e 65 20 65 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 ne extension on
7ad90 74 68 65 20 65 6e 64 20 6f 66 20 61 20 64 61 74 the end of a dat
7ada0 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 e-time..** The e
7adb0 78 74 65 6e 73 69 6f 6e 20 69 73 20 6f 66 20 74 xtension is of t
7adc0 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 he form:.**.**
7add0 20 20 20 20 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d (+/-)HH:MM
7ade0 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 65 20 22 7a .**.** Or the "z
7adf0 75 6c 75 22 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a ulu" notation:.*
7ae00 2a 0a 2a 2a 20 20 20 20 20 20 20 20 5a 0a 2a 2a *.** Z.**
7ae10 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 73 65 .** If the parse
7ae20 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
7ae30 77 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 write the number
7ae40 20 6f 66 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f of minutes.** o
7ae50 66 20 63 68 61 6e 67 65 20 69 6e 20 70 2d 3e 74 f change in p->t
7ae60 7a 20 61 6e 64 20 72 65 74 75 72 6e 20 30 2e 20 z and return 0.
7ae70 20 49 66 20 61 20 70 61 72 73 65 72 20 65 72 72 If a parser err
7ae80 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 72 65 or occurs,.** re
7ae90 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a turn non-zero..*
7aea0 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 6e 67 20 73 *.** A missing s
7aeb0 70 65 63 69 66 69 65 72 20 69 73 20 6e 6f 74 20 pecifier is not
7aec0 63 6f 6e 73 69 64 65 72 65 64 20 61 6e 20 65 72 considered an er
7aed0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ror..*/.static i
7aee0 6e 74 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 nt parseTimezone
7aef0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 (const char *zDa
7af00 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 te, DateTime *p)
7af10 7b 0a 20 20 69 6e 74 20 73 67 6e 20 3d 20 30 3b {. int sgn = 0;
7af20 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b . int nHr, nMn;
7af30 0a 20 20 69 6e 74 20 63 3b 0a 20 20 77 68 69 6c . int c;. whil
7af40 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 e( sqlite3Isspac
7af50 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 e(*zDate) ){ zDa
7af60 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 te++; }. p->tz
7af70 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61 74 = 0;. c = *zDat
7af80 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27 20 e;. if( c=='-'
7af90 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31 3b ){. sgn = -1;
7afa0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d . }else if( c==
7afb0 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d '+' ){. sgn =
7afc0 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 +1;. }else if(
7afd0 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a c=='Z' || c=='z
7afe0 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b ' ){. zDate++
7aff0 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75 5f ;. goto zulu_
7b000 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 time;. }else{.
7b010 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30 3b 0a return c!=0;.
7b020 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 }. zDate++;.
7b030 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a if( getDigits(z
7b040 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20 Date, 2, 0, 14,
7b050 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c ':', &nHr, 2, 0,
7b060 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 59, 0, &nMn)!=2
7b070 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
7b080 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d ;. }. zDate +=
7b090 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 67 5;. p->tz = sg
7b0a0 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 n*(nMn + nHr*60)
7b0b0 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 ;.zulu_time:. w
7b0c0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
7b0d0 70 61 63 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 pace(*zDate) ){
7b0e0 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 zDate++; }. ret
7b0f0 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d urn *zDate!=0;.}
7b100 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 ../*.** Parse ti
7b110 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 mes of the form
7b120 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 HH:MM or HH:MM:S
7b130 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 S or HH:MM:SS.FF
7b140 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d FF..** The HH, M
7b150 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 M, and SS must e
7b160 61 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 ach be exactly 2
7b170 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a digits. The.**
7b180 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f fractional seco
7b190 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 nds FFFF can be
7b1a0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 one or more digi
7b1b0 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ts..**.** Return
7b1c0 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 1 if there is a
7b1d0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 parsing error a
7b1e0 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e nd 0 on success.
7b1f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
7b200 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 arseHhMmSs(const
7b210 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 char *zDate, Da
7b220 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
7b230 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 t h, m, s;. dou
7b240 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 ble ms = 0.0;.
7b250 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 if( getDigits(zD
7b260 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 ate, 2, 0, 24, '
7b270 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 :', &h, 2, 0, 59
7b280 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 , 0, &m)!=2 ){.
7b290 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
7b2a0 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 . zDate += 5;.
7b2b0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 if( *zDate==':'
7b2c0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b ){. zDate++;
7b2d0 0a 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 . if( getDigi
7b2e0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 ts(zDate, 2, 0,
7b2f0 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 59, 0, &s)!=1 ){
7b300 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
7b310 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 . }. zDate
7b320 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a += 2;. if( *
7b330 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 73 71 zDate=='.' && sq
7b340 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 44 61 lite3Isdigit(zDa
7b350 74 65 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 te[1]) ){.
7b360 64 6f 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 double rScale =
7b370 31 2e 30 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 1.0;. zDate
7b380 2b 2b 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 ++;. while(
7b390 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
7b3a0 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 *zDate) ){.
7b3b0 20 20 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20 ms = ms*10.0
7b3c0 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a + *zDate - '0';.
7b3d0 20 20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a rScale *
7b3e0 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 = 10.0;.
7b3f0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d zDate++;. }
7b400 0a 20 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63 . ms /= rSc
7b410 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ale;. }. }el
7b420 73 65 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20 se{. s = 0;.
7b430 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 }. p->validJD
7b440 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 = 0;. p->validH
7b450 4d 53 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d MS = 1;. p->h =
7b460 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a h;. p->m = m;.
7b470 20 20 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b p->s = s + ms;
7b480 0a 20 20 69 66 28 20 70 61 72 73 65 54 69 6d 65 . if( parseTime
7b490 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29 zone(zDate, p) )
7b4a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e return 1;. p->
7b4b0 76 61 6c 69 64 54 5a 20 3d 20 28 70 2d 3e 74 7a validTZ = (p->tz
7b4c0 21 3d 30 29 3f 31 3a 30 3b 0a 20 20 72 65 74 75 !=0)?1:0;. retu
7b4d0 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn 0;.}../*.** C
7b4e0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 59 59 59 59 onvert from YYYY
7b4f0 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 20 -MM-DD HH:MM:SS
7b500 74 6f 20 6a 75 6c 69 61 6e 20 64 61 79 2e 20 20 to julian day.
7b510 57 65 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 We always assume
7b520 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 59 59 59 .** that the YYY
7b530 59 2d 4d 4d 2d 44 44 20 69 73 20 61 63 63 6f 72 Y-MM-DD is accor
7b540 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 ding to the Greg
7b550 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a orian calendar..
7b560 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 3a **.** Reference:
7b570 20 20 4d 65 65 75 73 20 70 61 67 65 20 36 31 0a Meeus page 61.
7b580 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
7b590 6f 6d 70 75 74 65 4a 44 28 44 61 74 65 54 69 6d omputeJD(DateTim
7b5a0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 e *p){. int Y,
7b5b0 4d 2c 20 44 2c 20 41 2c 20 42 2c 20 58 31 2c 20 M, D, A, B, X1,
7b5c0 58 32 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 76 61 X2;.. if( p->va
7b5d0 6c 69 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0a lidJD ) return;.
7b5e0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d if( p->validYM
7b5f0 44 20 29 7b 0a 20 20 20 20 59 20 3d 20 70 2d 3e D ){. Y = p->
7b600 59 3b 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b Y;. M = p->M;
7b610 0a 20 20 20 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 . D = p->D;.
7b620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 59 20 3d 20 }else{. Y =
7b630 32 30 30 30 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 2000; /* If no
7b640 59 4d 44 20 73 70 65 63 69 66 69 65 64 2c 20 61 YMD specified, a
7b650 73 73 75 6d 65 20 32 30 30 30 2d 4a 61 6e 2d 30 ssume 2000-Jan-0
7b660 31 20 2a 2f 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 1 */. M = 1;.
7b670 20 20 20 20 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 D = 1;. }.
7b680 20 69 66 28 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 if( M<=2 ){.
7b690 20 59 2d 2d 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 Y--;. M += 1
7b6a0 32 3b 0a 20 20 7d 0a 20 20 41 20 3d 20 59 2f 31 2;. }. A = Y/1
7b6b0 30 30 3b 0a 20 20 42 20 3d 20 32 20 2d 20 41 20 00;. B = 2 - A
7b6c0 2b 20 28 41 2f 34 29 3b 0a 20 20 58 31 20 3d 20 + (A/4);. X1 =
7b6d0 33 36 35 32 35 2a 28 59 2b 34 37 31 36 29 2f 31 36525*(Y+4716)/1
7b6e0 30 30 3b 0a 20 20 58 32 20 3d 20 33 30 36 30 30 00;. X2 = 30600
7b6f0 31 2a 28 4d 2b 31 29 2f 31 30 30 30 30 3b 0a 20 1*(M+1)/10000;.
7b700 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
7b710 65 33 5f 69 6e 74 36 34 29 28 28 58 31 20 2b 20 e3_int64)((X1 +
7b720 58 32 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32 X2 + D + B - 152
7b730 34 2e 35 20 29 20 2a 20 38 36 34 30 30 30 30 30 4.5 ) * 86400000
7b740 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 );. p->validJD
7b750 3d 20 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 = 1;. if( p->va
7b760 6c 69 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d lidHMS ){. p-
7b770 3e 69 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 >iJD += p->h*360
7b780 30 30 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 0000 + p->m*6000
7b790 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 0 + (sqlite3_int
7b7a0 36 34 29 28 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 64)(p->s*1000);.
7b7b0 20 20 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 if( p->valid
7b7c0 54 5a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 TZ ){. p->i
7b7d0 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 JD -= p->tz*6000
7b7e0 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
7b7f0 64 59 4d 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 dYMD = 0;.
7b800 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b p->validHMS = 0;
7b810 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 . p->validT
7b820 5a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d Z = 0;. }. }
7b830 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 .}../*.** Parse
7b840 64 61 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72 dates of the for
7b850 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 m.**.** YYYY
7b860 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e -MM-DD HH:MM:SS.
7b870 46 46 46 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d FFF.** YYYY-
7b880 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a MM-DD HH:MM:SS.*
7b890 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 * YYYY-MM-DD
7b8a0 20 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 HH:MM.** YY
7b8b0 59 59 2d 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 YY-MM-DD.**.** W
7b8c0 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 rite the result
7b8d0 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d into the DateTim
7b8e0 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 e structure and
7b8f0 72 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 return 0.** on s
7b900 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 uccess and 1 if
7b910 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
7b920 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 is not a well-f
7b930 6f 72 6d 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a ormed.** date..*
7b940 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 /.static int par
7b950 73 65 59 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 seYyyyMmDd(const
7b960 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 char *zDate, Da
7b970 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
7b980 74 20 59 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a t Y, M, D, neg;.
7b990 0a 20 20 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d . if( zDate[0]=
7b9a0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 ='-' ){. zDat
7b9b0 65 2b 2b 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 e++;. neg = 1
7b9c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
7b9d0 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 eg = 0;. }. if
7b9e0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 ( getDigits(zDat
7b9f0 65 2c 34 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 e,4,0,9999,'-',&
7ba00 59 2c 32 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c Y,2,1,12,'-',&M,
7ba10 32 2c 31 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 2,1,31,0,&D)!=3
7ba20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
7ba30 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 . }. zDate +=
7ba40 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 10;. while( sql
7ba50 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 ite3Isspace(*zDa
7ba60 74 65 29 20 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 te) || 'T'==*(u8
7ba70 2a 29 7a 44 61 74 65 20 29 7b 20 7a 44 61 74 65 *)zDate ){ zDate
7ba80 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 70 61 72 73 ++; }. if( pars
7ba90 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 eHhMmSs(zDate, p
7baa0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 )==0 ){. /* W
7bab0 65 20 67 6f 74 20 74 68 65 20 74 69 6d 65 20 2a e got the time *
7bac0 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a /. }else if( *z
7bad0 44 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 Date==0 ){. p
7bae0 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a ->validHMS = 0;.
7baf0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
7bb00 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e urn 1;. }. p->
7bb10 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 validJD = 0;. p
7bb20 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a ->validYMD = 1;.
7bb30 20 20 70 2d 3e 59 20 3d 20 6e 65 67 20 3f 20 2d p->Y = neg ? -
7bb40 59 20 3a 20 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 Y : Y;. p->M =
7bb50 4d 3b 0a 20 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 M;. p->D = D;.
7bb60 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 if( p->validTZ
7bb70 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 ){. computeJD
7bb80 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (p);. }. retur
7bb90 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 n 0;.}../*.** Se
7bba0 74 20 74 68 65 20 74 69 6d 65 20 74 6f 20 74 68 t the time to th
7bbb0 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 72 e current time r
7bbc0 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 56 eported by the V
7bbd0 46 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 FS.*/.static voi
7bbe0 64 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 d setDateTimeToC
7bbf0 75 72 72 65 6e 74 28 73 71 6c 69 74 65 33 5f 63 urrent(sqlite3_c
7bc00 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
7bc10 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 DateTime *p){.
7bc20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 73 71 6c double r;. sql
7bc30 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
7bc40 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
7bc50 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
7bc60 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
7bc70 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 tTime(db->pVfs,
7bc80 26 72 29 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 &r);. p->iJD =
7bc90 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7bca0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 r*86400000.0 + 0
7bcb0 2e 35 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a .5);. p->validJ
7bcc0 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D = 1;.}../*.**
7bcd0 41 74 74 65 6d 70 74 20 74 6f 20 70 61 72 73 65 Attempt to parse
7bce0 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e the given strin
7bcf0 67 20 69 6e 74 6f 20 61 20 4a 75 6c 69 61 6e 20 g into a Julian
7bd00 44 61 79 20 4e 75 6d 62 65 72 2e 20 20 52 65 74 Day Number. Ret
7bd10 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 urn.** the numbe
7bd20 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a r of errors..**.
7bd30 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
7bd40 20 61 72 65 20 61 63 63 65 70 74 61 62 6c 65 20 are acceptable
7bd50 66 6f 72 6d 73 20 66 6f 72 20 74 68 65 20 69 6e forms for the in
7bd60 70 75 74 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a put string:.**.*
7bd70 2a 20 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 * YYYY-MM-D
7bd80 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 D HH:MM:SS.FFF
7bd90 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 +/-HH:MM.**
7bda0 20 44 44 44 44 2e 44 44 20 0a 2a 2a 20 20 20 20 DDDD.DD .**
7bdb0 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 now.**.** In t
7bdc0 68 65 20 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 he first form, t
7bdd0 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 he +/-HH:MM is a
7bde0 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 lways optional.
7bdf0 20 54 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 0a The fractional.
7be00 2a 2a 20 73 65 63 6f 6e 64 73 20 65 78 74 65 6e ** seconds exten
7be10 73 69 6f 6e 20 28 74 68 65 20 22 2e 46 46 46 22 sion (the ".FFF"
7be20 29 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 ) is optional.
7be30 54 68 65 20 73 65 63 6f 6e 64 73 20 70 6f 72 74 The seconds port
7be40 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53 2e 46 46 46 ion.** (":SS.FFF
7be50 22 29 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 ") is option. T
7be60 68 65 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 he year and date
7be70 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 can be omitted
7be80 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 as long.** as th
7be90 65 72 65 20 69 73 20 61 20 74 69 6d 65 20 73 74 ere is a time st
7bea0 72 69 6e 67 2e 20 20 54 68 65 20 74 69 6d 65 20 ring. The time
7beb0 73 74 72 69 6e 67 20 63 61 6e 20 62 65 20 6f 6d string can be om
7bec0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a itted as long.**
7bed0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 79 as there is a y
7bee0 65 61 72 20 61 6e 64 20 64 61 74 65 2e 0a 2a 2f ear and date..*/
7bef0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 .static int pars
7bf00 65 44 61 74 65 4f 72 54 69 6d 65 28 0a 20 20 73 eDateOrTime(. s
7bf10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7bf20 63 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 context, . cons
7bf30 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 0a t char *zDate, .
7bf40 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 7b DateTime *p.){
7bf50 0a 20 20 69 6e 74 20 69 73 52 65 61 6c 4e 75 6d . int isRealNum
7bf60 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 ; /* Return f
7bf70 72 6f 6d 20 73 71 6c 69 74 65 33 49 73 4e 75 6d rom sqlite3IsNum
7bf80 62 65 72 28 29 2e 20 20 4e 6f 74 20 75 73 65 64 ber(). Not used
7bf90 20 2a 2f 0a 20 20 69 66 28 20 70 61 72 73 65 59 */. if( parseY
7bfa0 79 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 yyyMmDd(zDate,p)
7bfb0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
7bfc0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 0;. }else if(
7bfd0 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 parseHhMmSs(zDa
7bfe0 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 te, p)==0 ){.
7bff0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
7c000 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 se if( sqlite3St
7c010 72 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 rICmp(zDate,"now
7c020 22 29 3d 3d 30 29 7b 0a 20 20 20 20 73 65 74 44 ")==0){. setD
7c030 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
7c040 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 (context, p);.
7c050 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
7c060 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 49 lse if( sqlite3I
7c070 73 4e 75 6d 62 65 72 28 7a 44 61 74 65 2c 20 26 sNumber(zDate, &
7c080 69 73 52 65 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 isRealNum, SQLIT
7c090 45 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 E_UTF8) ){. d
7c0a0 6f 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 ouble r;. get
7c0b0 56 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 Value(zDate, &r)
7c0c0 3b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 ;. p->iJD = (
7c0d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
7c0e0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e *86400000.0 + 0.
7c0f0 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 5);. p->valid
7c100 4a 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 JD = 1;. retu
7c110 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn 0;. }. retu
7c120 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn 1;.}../*.** C
7c130 6f 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c ompute the Year,
7c140 20 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 Month, and Day
7c150 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 from the julian
7c160 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 day number..*/.s
7c170 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 tatic void compu
7c180 74 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a teYMD(DateTime *
7c190 70 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 p){. int Z, A,
7c1a0 42 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a B, C, D, E, X1;.
7c1b0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d if( p->validYM
7c1c0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 D ) return;. if
7c1d0 28 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b ( !p->validJD ){
7c1e0 0a 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 . p->Y = 2000
7c1f0 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a ;. p->M = 1;.
7c200 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 p->D = 1;.
7c210 7d 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 }else{. Z = (
7c220 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 int)((p->iJD + 4
7c230 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 3200000)/8640000
7c240 30 29 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 0);. A = (int
7c250 29 28 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 )((Z - 1867216.2
7c260 35 29 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 5)/36524.25);.
7c270 20 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 A = Z + 1 + A
7c280 2d 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d - (A/4);. B =
7c290 20 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 A + 1524;. C
7c2a0 20 3d 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 = (int)((B - 12
7c2b0 32 2e 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 2.1)/365.25);.
7c2c0 20 20 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f D = (36525*C)/
7c2d0 31 30 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 100;. E = (in
7c2e0 74 29 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 t)((B-D)/30.6001
7c2f0 29 3b 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74 );. X1 = (int
7c300 29 28 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 )(30.6001*E);.
7c310 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d p->D = B - D -
7c320 20 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 X1;. p->M =
7c330 45 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 E<14 ? E-1 : E-1
7c340 33 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3;. p->Y = p-
7c350 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20 >M>2 ? C - 4716
7c360 3a 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a : C - 4715;. }.
7c370 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 p->validYMD =
7c380 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 1;.}../*.** Comp
7c390 75 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 ute the Hour, Mi
7c3a0 6e 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 nute, and Second
7c3b0 73 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 s from the julia
7c3c0 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f n day number..*/
7c3d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d .static void com
7c3e0 70 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65 puteHMS(DateTime
7c3f0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 *p){. int s;.
7c400 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 if( p->validHMS
7c410 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d ) return;. com
7c420 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d puteJD(p);. s =
7c430 20 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b (int)((p->iJD +
7c440 20 34 33 32 30 30 30 30 30 29 20 25 20 38 36 34 43200000) % 864
7c450 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 00000);. p->s =
7c460 20 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d s/1000.0;. s =
7c470 20 28 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d (int)p->s;. p-
7c480 3e 73 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 >s -= s;. p->h
7c490 3d 20 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d = s/3600;. s -=
7c4a0 20 70 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d p->h*3600;. p-
7c4b0 3e 6d 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e >m = s/60;. p->
7c4c0 73 20 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 s += s - p->m*60
7c4d0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 ;. p->validHMS
7c4e0 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f = 1;.}../*.** Co
7c4f0 6d 70 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61 mpute both YMD a
7c500 6e 64 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 nd HMS.*/.static
7c510 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 void computeYMD
7c520 5f 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 _HMS(DateTime *p
7c530 29 7b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 ){. computeYMD(
7c540 70 29 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 p);. computeHMS
7c550 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c (p);.}../*.** Cl
7c560 65 61 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20 ear the YMD and
7c570 48 4d 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a HMS and the TZ.*
7c580 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c /.static void cl
7c590 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 earYMD_HMS_TZ(Da
7c5a0 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d teTime *p){. p-
7c5b0 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 >validYMD = 0;.
7c5c0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 p->validHMS = 0
7c5d0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d ;. p->validTZ =
7c5e0 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 0;.}..#ifndef S
7c5f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c QLITE_OMIT_LOCAL
7c600 54 49 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 TIME./*.** Compu
7c610 74 65 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 te the differenc
7c620 65 20 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e e (in millisecon
7c630 64 73 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c ds).** between l
7c640 6f 63 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 ocaltime and UTC
7c650 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a (a.k.a. GMT).**
7c660 20 66 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61 for the time va
7c670 6c 75 65 20 70 20 77 68 65 72 65 20 70 20 69 73 lue p where p is
7c680 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 in UTC..*/.stat
7c690 69 63 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 ic sqlite3_int64
7c6a0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
7c6b0 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 (DateTime *p){.
7c6c0 20 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a DateTime x, y;.
7c6d0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 time_t t;. x
7c6e0 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 = *p;. computeY
7c6f0 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 MD_HMS(&x);. if
7c700 28 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e ( x.Y<1971 || x.
7c710 59 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 Y>=2038 ){. x
7c720 2e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 .Y = 2000;. x
7c730 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 .M = 1;. x.D
7c740 3d 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 = 1;. x.h = 0
7c750 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 ;. x.m = 0;.
7c760 20 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 x.s = 0.0;.
7c770 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 } else {. int
7c780 20 73 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b s = (int)(x.s +
7c790 20 30 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 0.5);. x.s =
7c7a0 20 73 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d s;. }. x.tz =
7c7b0 20 30 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 0;. x.validJD
7c7c0 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 = 0;. computeJD
7c7d0 28 26 78 29 3b 0a 20 20 74 20 3d 20 28 74 69 6d (&x);. t = (tim
7c7e0 65 5f 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 e_t)(x.iJD/1000
7c7f0 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 - 21086676*(i64)
7c800 31 30 30 30 30 29 3b 0a 23 69 66 64 65 66 20 48 10000);.#ifdef H
7c810 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a AVE_LOCALTIME_R.
7c820 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
7c830 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f m sLocal;. lo
7c840 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 caltime_r(&t, &s
7c850 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20 Local);. y.Y
7c860 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 = sLocal.tm_year
7c870 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
7c880 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e = sLocal.tm_mon
7c890 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 + 1;. y.D =
7c8a0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a sLocal.tm_mday;.
7c8b0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c y.h = sLocal
7c8c0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e .tm_hour;. y.
7c8d0 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 m = sLocal.tm_mi
7c8e0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f n;. y.s = sLo
7c8f0 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a cal.tm_sec;. }.
7c900 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
7c910 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 VE_LOCALTIME_S)
7c920 26 26 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d && HAVE_LOCALTIM
7c930 45 5f 53 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 E_S. {. stru
7c940 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 ct tm sLocal;.
7c950 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73 localtime_s(&s
7c960 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20 Local, &t);.
7c970 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.Y = sLocal.tm_
7c980 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 year + 1900;.
7c990 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.M = sLocal.tm
7c9a0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e _mon + 1;. y.
7c9b0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 D = sLocal.tm_md
7c9c0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c ay;. y.h = sL
7c9d0 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 ocal.tm_hour;.
7c9e0 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.m = sLocal.t
7c9f0 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d m_min;. y.s =
7ca00 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a sLocal.tm_sec;.
7ca10 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 }.#else. {.
7ca20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d struct tm *pTm
7ca30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
7ca40 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
7ca50 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7ca60 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7ca70 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 MASTER));. pT
7ca80 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 m = localtime(&t
7ca90 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d );. y.Y = pTm
7caa0 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 ->tm_year + 1900
7cab0 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d ;. y.M = pTm-
7cac0 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 >tm_mon + 1;.
7cad0 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.D = pTm->tm_m
7cae0 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 day;. y.h = p
7caf0 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 Tm->tm_hour;.
7cb00 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.m = pTm->tm_m
7cb10 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 in;. y.s = pT
7cb20 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 m->tm_sec;. s
7cb30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
7cb40 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
7cb50 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
7cb60 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
7cb70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
7cb80 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a y.validYMD = 1;.
7cb90 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31 y.validHMS = 1
7cba0 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 ;. y.validJD =
7cbb0 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d 0;. y.validTZ =
7cbc0 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 0;. computeJD(
7cbd0 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e &y);. return y.
7cbe0 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 iJD - x.iJD;.}.#
7cbf0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
7cc00 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a OMIT_LOCALTIME *
7cc10 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 /../*.** Process
7cc20 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 a modifier to a
7cc30 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 date-time stamp
7cc40 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 . The modifiers
7cc50 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f are.** as follo
7cc60 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e ws:.**.** NN
7cc70 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e N days.** NN
7cc80 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e N hours.** N
7cc90 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 NN minutes.**
7cca0 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e NNN.NNNN secon
7ccb0 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f ds.** NNN mo
7ccc0 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 nths.** NNN
7ccd0 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 years.** sta
7cce0 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 rt of month.**
7ccf0 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 start of year
7cd00 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 .** start of
7cd10 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 week.** sta
7cd20 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 rt of day.**
7cd30 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 weekday N.**
7cd40 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 unixepoch.**
7cd50 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 localtime.**
7cd60 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 utc.**.** Re
7cd70 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 turn 0 on succes
7cd80 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
7cd90 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 is any kind of
7cda0 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 error..*/.static
7cdb0 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69 int parseModifi
7cdc0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a er(const char *z
7cdd0 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 Mod, DateTime *p
7cde0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b ){. int rc = 1;
7cdf0 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 . int n;. doub
7ce00 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c le r;. char *z,
7ce10 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d zBuf[30];. z =
7ce20 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 zBuf;. for(n=0
7ce30 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 ; n<ArraySize(zB
7ce40 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d uf)-1 && zMod[n]
7ce50 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d ; n++){. z[n]
7ce60 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
7ce70 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 UpperToLower[(u8
7ce80 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 )zMod[n]];. }.
7ce90 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 z[n] = 0;. swi
7cea0 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 tch( z[0] ){.#if
7ceb0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
7cec0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 _LOCALTIME. c
7ced0 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 ase 'l': {.
7cee0 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 /* localtime
7cef0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
7cf00 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 ** Assuming the
7cf10 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c current time val
7cf20 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 ue is UTC (a.k.a
7cf30 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74 . GMT), shift it
7cf40 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f to. ** sho
7cf50 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 w local time..
7cf60 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
7cf70 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 strcmp(z, "loca
7cf80 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 ltime")==0 ){.
7cf90 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7cfa0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
7cfb0 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f JD += localtimeO
7cfc0 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 ffset(p);.
7cfd0 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
7cfe0 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 Z(p);. rc
7cff0 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
7d000 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
7d010 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
7d020 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 'u': {. /*
7d030 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 . ** uni
7d040 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a xepoch. **.
7d050 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 ** Treat t
7d060 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
7d070 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 of p->iJD as th
7d080 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 e number of.
7d090 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e ** seconds sin
7d0a0 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 ce 1970. Conver
7d0b0 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 t to a real juli
7d0c0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 an day number..
7d0d0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
7d0e0 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 ( strcmp(z, "uni
7d0f0 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 xepoch")==0 && p
7d100 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 ->validJD ){.
7d110 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70 p->iJD = (p
7d120 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38 ->iJD + 43200)/8
7d130 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a 6400 + 21086676*
7d140 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20 (i64)10000000;.
7d150 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f clearYMD_
7d160 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 HMS_TZ(p);.
7d170 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7d180 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
7d190 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 E_OMIT_LOCALTIME
7d1a0 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 . else if(
7d1b0 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29 strcmp(z, "utc")
7d1c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
7d1d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b qlite3_int64 c1;
7d1e0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7d1f0 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 JD(p);. c
7d200 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 1 = localtimeOff
7d210 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
7d220 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 p->iJD -= c1;.
7d230 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 clearYMD_H
7d240 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 MS_TZ(p);.
7d250 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d p->iJD += c1 -
7d260 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
7d270 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
7d280 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e = 0;. }.#en
7d290 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b dif. break;
7d2a0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
7d2b0 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 'w': {. /*.
7d2c0 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b ** week
7d2d0 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 day N. **.
7d2e0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 ** Move the
7d2f0 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d date to the sam
7d300 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65 e time on the ne
7d310 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 xt occurrence of
7d320 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61 . ** weekda
7d330 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e y N where 0==Sun
7d340 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 day, 1==Monday,
7d350 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 and so forth. I
7d360 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 f the. ** d
7d370 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ate is already o
7d380 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
7d390 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20 e weekday, this
7d3a0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
7d3b0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 */. if( s
7d3c0 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 trncmp(z, "weekd
7d3d0 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67 ay ", 8)==0 && g
7d3e0 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 etValue(&z[8],&r
7d3f0 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 )>0.
7d400 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29 && (n=(int)
7d410 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 r)==r && n>=0 &&
7d420 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 r<7 ){.
7d430 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b sqlite3_int64 Z;
7d440 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7d450 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
7d460 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d p->validTZ =
7d470 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 0;. p->v
7d480 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
7d490 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7d4a0 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28 ;. Z = ((
7d4b0 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30 p->iJD + 1296000
7d4c0 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 00)/86400000) %
7d4d0 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a 7;. if( Z
7d4e0 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 >n ) Z -= 7;.
7d4f0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7d500 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b n - Z)*86400000;
7d510 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
7d520 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7d530 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7d540 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
7d550 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
7d560 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 's': {. /*
7d570 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61 . ** sta
7d580 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20 rt of TTTTT.
7d590 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f **. ** Mo
7d5a0 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b ve the date back
7d5b0 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67 wards to the beg
7d5c0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75 inning of the cu
7d5d0 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 rrent day,.
7d5e0 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 ** or month or
7d5f0 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 year.. */.
7d600 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 if( strncmp
7d610 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c (z, "start of ",
7d620 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 9)!=0 ) break;.
7d630 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 z += 9;.
7d640 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 computeYMD(p
7d650 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 );. p->vali
7d660 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 dHMS = 1;.
7d670 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b p->h = p->m = 0;
7d680 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e . p->s = 0.
7d690 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
7d6a0 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 dTZ = 0;. p
7d6b0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
7d6c0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
7d6d0 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b z,"month")==0 ){
7d6e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 . p->D =
7d6f0 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 1;. rc =
7d700 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 0;. }else i
7d710 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 f( strcmp(z,"yea
7d720 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
7d730 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b computeYMD(p);
7d740 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 . p->M =
7d750 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 1;. p->D
7d760 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 = 1;. rc
7d770 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
7d780 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 if( strcmp(z,"d
7d790 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ay")==0 ){.
7d7a0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7d7b0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
7d7c0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 }. case '
7d7d0 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27 +':. case '-'
7d7e0 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a :. case '0':.
7d7f0 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20 case '1':.
7d800 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20 case '2':.
7d810 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61 case '3':. ca
7d820 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 se '4':. case
7d830 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '5':. case '
7d840 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27 6':. case '7'
7d850 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a :. case '8':.
7d860 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a case '9': {.
7d870 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f double rRo
7d880 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d under;. n =
7d890 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 getValue(z, &r)
7d8a0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
7d8b0 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 n>=1 );. if
7d8c0 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 ( z[n]==':' ){.
7d8d0 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 /* A modi
7d8e0 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d fier of the form
7d8f0 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 (+|-)HH:MM:SS.F
7d900 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 FF adds (or subt
7d910 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 racts) the.
7d920 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 ** specified
7d930 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c number of hours,
7d940 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 minutes, second
7d950 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 s, and fractiona
7d960 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 l seconds.
7d970 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 ** to the time
7d980 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 . The ".FFF" ma
7d990 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 y be omitted. T
7d9a0 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 he ":SS.FFF" may
7d9b0 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f be. ** o
7d9c0 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 mitted..
7d9d0 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 */. const
7d9e0 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
7d9f0 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 DateTime
7da00 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 tx;. sqli
7da10 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20 te3_int64 day;.
7da20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 if( !sqli
7da30 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20 te3Isdigit(*z2)
7da40 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ) z2++;.
7da50 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73 memset(&tx, 0, s
7da60 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20 izeof(tx));.
7da70 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d if( parseHhM
7da80 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62 mSs(z2, &tx) ) b
7da90 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f reak;. co
7daa0 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20 mputeJD(&tx);.
7dab0 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20 tx.iJD -=
7dac0 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20 43200000;.
7dad0 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38 day = tx.iJD/8
7dae0 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
7daf0 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38 tx.iJD -= day*8
7db00 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
7db10 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 if( z[0]=='-' )
7db20 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a tx.iJD = -tx.iJ
7db30 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 D;. compu
7db40 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
7db50 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
7db60 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
7db70 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 iJD += tx.iJD;.
7db80 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7db90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7dba0 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d }. z +=
7dbb0 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 n;. while(
7dbc0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
7dbd0 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 *z) ) z++;.
7dbe0 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
7dbf0 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69 en30(z);. i
7dc00 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 f( n>10 || n<3 )
7dc10 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 break;. if
7dc20 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b ( z[n-1]=='s' ){
7dc30 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d z[n-1] = 0; n--
7dc40 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 ; }. comput
7dc50 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63 eJD(p);. rc
7dc60 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75 = 0;. rRou
7dc70 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e nder = r<0 ? -0.
7dc80 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20 5 : +0.5;.
7dc90 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63 if( n==3 && strc
7dca0 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
7dcb0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 {. p->iJD
7dcc0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
7dcd0 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 64)(r*86400000.0
7dce0 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 + rRounder);.
7dcf0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d }else if( n=
7dd00 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =4 && strcmp(z,"
7dd10 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 hour")==0 ){.
7dd20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7dd30 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
7dd40 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e *(86400000.0/24.
7dd50 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 0) + rRounder);.
7dd60 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
7dd70 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a n==6 && strcmp(z
7dd80 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b ,"minute")==0 ){
7dd90 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7dda0 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 += (sqlite3_int6
7ddb0 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30 4)(r*(86400000.0
7ddc0 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 /(24.0*60.0)) +
7ddd0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7dde0 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 }else if( n==6
7ddf0 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63 && strcmp(z,"sec
7de00 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ond")==0 ){.
7de10 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7de20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
7de30 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e (86400000.0/(24.
7de40 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20 0*60.0*60.0)) +
7de50 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7de60 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20 }else if( n==5
7de70 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e && strcmp(z,"mon
7de80 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 th")==0 ){.
7de90 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20 int x, y;.
7dea0 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
7deb0 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
7dec0 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a p->M += (int)r;.
7ded0 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d x = p->M
7dee0 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 >0 ? (p->M-1)/12
7def0 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b : (p->M-12)/12;
7df00 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
7df10 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d x;. p->M
7df20 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20 -= x*12;.
7df30 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 p->validJD = 0
7df40 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
7df50 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 eJD(p);.
7df60 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 y = (int)r;.
7df70 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a if( y!=r ){.
7df80 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 p->iJD
7df90 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
7dfa0 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30 64)((r - y)*30.0
7dfb0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 *86400000.0 + rR
7dfc0 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20 ounder);.
7dfd0 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }. }else i
7dfe0 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d f( n==4 && strcm
7dff0 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 p(z,"year")==0 )
7e000 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20 {. int y
7e010 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 = (int)r;.
7e020 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 computeYMD_HMS
7e030 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
7e040 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20 Y += y;.
7e050 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a p->validJD = 0;.
7e060 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a computeJ
7e070 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 D(p);. if
7e080 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 ( y!=r ){.
7e090 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7e0a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 qlite3_int64)((r
7e0b0 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30 - y)*365.0*8640
7e0c0 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 0000.0 + rRounde
7e0d0 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 r);. }.
7e0e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
7e0f0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 rc = 1;.
7e100 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d }. clearYM
7e110 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7e120 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7e130 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
7e140 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7e150 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
7e160 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 c;.}../*.** Proc
7e170 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f ess time functio
7e180 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72 n arguments. ar
7e190 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d gv[0] is a date-
7e1a0 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 time stamp..** a
7e1b0 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f rgv[1] and follo
7e1c0 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65 wing are modifie
7e1d0 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20 rs. Parse them
7e1e0 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a all and write.**
7e1f0 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74 the resulting t
7e200 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74 ime into the Dat
7e210 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20 eTime structure
7e220 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20 p. Return 0.**
7e230 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 on success and 1
7e240 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e if there are an
7e250 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 y errors..**.**
7e260 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72 If there are zer
7e270 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66 o parameters (if
7e280 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73 even argv[0] is
7e290 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74 undefined).** t
7e2a0 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66 hen assume a def
7e2b0 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e ault value of "n
7e2c0 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e ow" for argv[0].
7e2d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
7e2e0 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 sDate(. sqlite3
7e2f0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7e300 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 t, . int argc,
7e310 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
7e320 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65 **argv, . Date
7e330 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 Time *p.){. int
7e340 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 i;. const unsi
7e350 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 gned char *z;.
7e360 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d int eType;. mem
7e370 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
7e380 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67 (*p));. if( arg
7e390 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44 c==0 ){. setD
7e3a0 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
7e3b0 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 (context, p);.
7e3c0 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65 }else if( (eType
7e3d0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
7e3e0 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d _type(argv[0]))=
7e3f0 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20 =SQLITE_FLOAT.
7e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e410 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
7e420 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 E_INTEGER ){.
7e430 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
7e440 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 e3_int64)(sqlite
7e450 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 3_value_double(a
7e460 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 rgv[0])*86400000
7e470 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 .0 + 0.5);. p
7e480 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
7e490 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 }else{. z =
7e4a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
7e4b0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 xt(argv[0]);.
7e4c0 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65 if( !z || parse
7e4d0 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65 DateOrTime(conte
7e4e0 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29 xt, (char*)z, p)
7e4f0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
7e500 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
7e510 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b for(i=1; i<argc;
7e520 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 i++){. if( (
7e530 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
7e540 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29 e_text(argv[i]))
7e550 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69 ==0 || parseModi
7e560 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70 fier((char*)z, p
7e570 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
7e580 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
7e590 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f return 0;.}.../
7e5a0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
7e5b0 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c ng routines impl
7e5c0 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 ement the variou
7e5d0 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 s date and time
7e5e0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 functions.** of
7e5f0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a SQLite..*/../*.*
7e600 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 * julianday(
7e610 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
7e620 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
7e630 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 Return the juli
7e640 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 an day number of
7e650 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66 the date specif
7e660 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d ied in the argum
7e670 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ents.*/.static v
7e680 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e oid juliandayFun
7e690 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
7e6a0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
7e6b0 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
7e6c0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7e6d0 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 v.){. DateTime
7e6e0 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 x;. if( isDate(
7e6f0 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 context, argc, a
7e700 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 rgv, &x)==0 ){.
7e710 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 computeJD(&x)
7e720 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
7e730 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 sult_double(cont
7e740 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30 ext, x.iJD/86400
7e750 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 000.0);. }.}../
7e760 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65 *.** datetime
7e770 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f ( TIMESTRING, MO
7e780 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a D, MOD, ...).**.
7e790 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d ** Return YYYY-M
7e7a0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f M-DD HH:MM:SS.*/
7e7b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 .static void dat
7e7c0 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c etimeFunc(. sql
7e7d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7e7e0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7e7f0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7e800 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7e810 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 ateTime x;. if(
7e820 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c isDate(context,
7e830 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 argc, argv, &x)
7e840 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ==0 ){. char
7e850 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 zBuf[100];. c
7e860 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
7e870 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
7e880 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
7e890 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 Buf), zBuf, "%04
7e8a0 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64 d-%02d-%02d %02d
7e8b0 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20 :%02d:%02d",.
7e8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e8d0 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c x.Y, x.M, x.D,
7e8e0 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
7e8f0 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 (x.s));. sqli
7e900 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
7e910 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d context, zBuf, -
7e920 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 1, SQLITE_TRANSI
7e930 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
7e940 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45 ** time( TIME
7e950 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
7e960 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
7e970 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a urn HH:MM:SS.*/.
7e980 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 static void time
7e990 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7e9a0 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7e9b0 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7e9c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7e9d0 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
7e9e0 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
7e9f0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
7ea00 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
7ea10 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
7ea20 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
7ea30 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 eHMS(&x);. sq
7ea40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7ea50 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7ea60 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30 f, "%02d:%02d:%0
7ea70 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 2d", x.h, x.m, (
7ea80 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71 int)x.s);. sq
7ea90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
7eaa0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
7eab0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
7eac0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f SIENT);. }.}../
7ead0 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 *.** date( TI
7eae0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
7eaf0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
7eb00 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
7eb10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7eb20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 dateFunc(. sqli
7eb30 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7eb40 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7eb50 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
7eb60 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
7eb70 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
7eb80 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
7eb90 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
7eba0 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a =0 ){. char z
7ebb0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f Buf[100];. co
7ebc0 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20 mputeYMD(&x);.
7ebd0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7ebe0 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c tf(sizeof(zBuf),
7ebf0 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 zBuf, "%04d-%02
7ec00 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e d-%02d", x.Y, x.
7ec10 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c M, x.D);. sql
7ec20 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
7ec30 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 (context, zBuf,
7ec40 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 -1, SQLITE_TRANS
7ec50 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a IENT);. }.}../*
7ec60 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28 .** strftime(
7ec70 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52 FORMAT, TIMESTR
7ec80 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
7ec90 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
7eca0 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69 a string descri
7ecb0 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20 bed by FORMAT.
7ecc0 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66 Conversions as f
7ecd0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
7ece0 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68 %d day of month
7ecf0 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61 .** %f ** fra
7ed00 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 ctional seconds
7ed10 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20 SS.SSS.** %H
7ed20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20 hour 00-24.**
7ed30 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72 %j day of year
7ed40 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 000-366.** %J
7ed50 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20 ** Julian day
7ed60 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20 number.** %m
7ed70 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20 month 01-12.**
7ed80 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35 %M minute 00-5
7ed90 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 9.** %s secon
7eda0 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31 ds since 1970-01
7edb0 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 -01.** %S sec
7edc0 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20 onds 00-59.**
7edd0 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 %w day of week
7ede0 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 0-6 sunday==0.*
7edf0 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20 * %W week of
7ee00 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20 year 00-53.**
7ee10 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39 %Y year 0000-99
7ee20 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f 99.** %% %.*/
7ee30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 .static void str
7ee40 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c ftimeFunc(. sql
7ee50 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7ee60 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7ee70 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7ee80 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7ee90 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34 ateTime x;. u64
7eea0 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a n;. size_t i,j
7eeb0 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 ;. char *z;. s
7eec0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f qlite3 *db;. co
7eed0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d nst char *zFmt =
7eee0 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
7eef0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
7ef00 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 (argv[0]);. cha
7ef10 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 r zBuf[100];. i
7ef20 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 f( zFmt==0 || is
7ef30 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 Date(context, ar
7ef40 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 gc-1, argv+1, &x
7ef50 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 ) ) return;. db
7ef60 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
7ef70 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
7ef80 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 text);. for(i=0
7ef90 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 , n=1; zFmt[i];
7efa0 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 i++, n++){. i
7efb0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 f( zFmt[i]=='%'
7efc0 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 ){. switch(
7efd0 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 zFmt[i+1] ){.
7efe0 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a case 'd':.
7eff0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 case 'H'
7f000 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
7f010 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 m':. case
7f020 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 'M':. ca
7f030 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 se 'S':.
7f040 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 case 'W':.
7f050 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 n++;.
7f060 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 /* fall thru
7f070 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 */. case
7f080 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 'w':. cas
7f090 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 e '%':.
7f0a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7f0b0 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 case 'f':.
7f0c0 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 n += 8;.
7f0d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7f0e0 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 case 'j':.
7f0f0 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b n += 3;
7f100 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
7f110 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7f120 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 Y':. n
7f130 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 += 8;.
7f140 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7f150 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 ase 's':.
7f160 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 case 'J':.
7f170 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 n += 50;.
7f180 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7f190 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a default:.
7f1a0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
7f1b0 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 ; /* ERROR. re
7f1c0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 turn a NULL */.
7f1d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b }. i++
7f1e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65 ;. }. }. te
7f1f0 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f stcase( n==sizeo
7f200 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74 f(zBuf)-1 );. t
7f210 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 estcase( n==size
7f220 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65 of(zBuf) );. te
7f230 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
7f240 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
7f250 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b E_LIMIT_LENGTH]+
7f260 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 1 );. testcase(
7f270 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 n==(u64)db->aLi
7f280 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
7f290 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66 _LENGTH] );. if
7f2a0 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 ( n<sizeof(zBuf)
7f2b0 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 ){. z = zBuf
7f2c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e ;. }else if( n>
7f2d0 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
7f2e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
7f2f0 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 GTH] ){. sqli
7f300 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
7f310 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 _toobig(context)
7f320 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
7f330 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 }else{. z = s
7f340 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
7f350 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 w(db, (int)n);.
7f360 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 if( z==0 ){.
7f370 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
7f380 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 ult_error_nomem(
7f390 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 context);.
7f3a0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
7f3b0 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 }. computeJD(&x
7f3c0 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f );. computeYMD_
7f3d0 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 HMS(&x);. for(i
7f3e0 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 =j=0; zFmt[i]; i
7f3f0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d ++){. if( zFm
7f400 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 t[i]!='%' ){.
7f410 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 z[j++] = zFmt
7f420 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a [i];. }else{.
7f430 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
7f440 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d switch( zFmt[i]
7f450 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ){. case
7f460 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'd': sqlite3_s
7f470 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d nprintf(3, &z[j]
7f480 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b ,"%02d",x.D); j+
7f490 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
7f4a0 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 case 'f': {.
7f4b0 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 double
7f4c0 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 s = x.s;.
7f4d0 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 if( s>59.999
7f4e0 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 ) s = 59.999;.
7f4f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7f500 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a snprintf(7, &z[j
7f510 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a ],"%06.3f", s);.
7f520 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 j += s
7f530 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
7f540 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
7f550 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7f560 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
7f570 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 H': sqlite3_snp
7f580 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
7f590 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 %02d",x.h); j+=2
7f5a0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
7f5b0 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 case 'W': /* Fa
7f5c0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 ll thru */.
7f5d0 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 case 'j': {.
7f5e0 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61 int nDa
7f5f0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
7f600 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 * Number of days
7f610 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f since 1st day o
7f620 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 f year */.
7f630 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d DateTime y =
7f640 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e x;. y.
7f650 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 validJD = 0;.
7f660 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a y.M = 1;.
7f670 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20 y.D =
7f680 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 1;. com
7f690 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 puteJD(&y);.
7f6a0 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e nDay = (in
7f6b0 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b t)((x.iJD-y.iJD+
7f6c0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
7f6d0 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 00);. i
7f6e0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 f( zFmt[i]=='W'
7f6f0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
7f700 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f nt wd; /* 0=Mo
7f710 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c nday, 1=Tuesday,
7f720 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f ... 6=Sunday */
7f730 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20 . wd
7f740 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b = (int)(((x.iJD+
7f750 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
7f760 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20 00)%7);.
7f770 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
7f780 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(3, &z[j],"%
7f790 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 02d",(nDay+7-wd)
7f7a0 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 /7);.
7f7b0 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 j += 2;.
7f7c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
7f7d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
7f7e0 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c printf(4, &z[j],
7f7f0 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a "%03d",nDay+1);.
7f800 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d j +=
7f810 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 3;. }.
7f820 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7f830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7f840 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 case 'J': {.
7f850 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
7f860 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a _snprintf(20, &z
7f870 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a [j],"%.16g",x.iJ
7f880 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
7f890 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69 j+=sqli
7f8a0 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a te3Strlen30(&z[j
7f8b0 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 ]);. br
7f8c0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
7f8d0 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a case 'm':
7f8e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7f8f0 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 tf(3, &z[j],"%02
7f900 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 d",x.M); j+=2; b
7f910 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
7f920 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 se 'M': sqlite3
7f930 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
7f940 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 j],"%02d",x.m);
7f950 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
7f960 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b case 's': {
7f970 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
7f980 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 e3_snprintf(30,&
7f990 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 z[j],"%lld",.
7f9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f9b0 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e (i64)(x.
7f9c0 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 iJD/1000 - 21086
7f9d0 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 676*(i64)10000))
7f9e0 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d ;. j +=
7f9f0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
7fa00 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 (&z[j]);.
7fa10 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
7fa20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 }. case
7fa30 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'S': sqlite3_s
7fa40 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c nprintf(3,&z[j],
7fa50 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 "%02d",(int)x.s)
7fa60 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 ; j+=2; break;.
7fa70 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
7fa80 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a {. z[j
7fa90 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78 ++] = (char)(((x
7faa0 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f .iJD+129600000)/
7fab0 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b 86400000) % 7) +
7fac0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
7fad0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
7fae0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 . case 'Y
7faf0 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ': {. s
7fb00 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7fb10 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 5,&z[j],"%04d",x
7fb20 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 .Y); j+=sqlite3S
7fb30 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7fb40 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7fb50 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7fb60 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b default: z[
7fb70 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 j++] = '%'; brea
7fb80 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
7fb90 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b . }. z[j] = 0;
7fba0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
7fbb0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
7fbc0 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 z, -1,.
7fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d z==
7fbe0 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 zBuf ? SQLITE_TR
7fbf0 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 ANSIENT : SQLITE
7fc00 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a _DYNAMIC);.}../*
7fc10 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 .** current_time
7fc20 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ().**.** This fu
7fc30 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
7fc40 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 he same value as
7fc50 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f time('now')..*/
7fc60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 .static void cti
7fc70 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 meFunc(. sqlite
7fc80 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7fc90 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7fca0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7fcb0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7fcc0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7fcd0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7fce0 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 tUsed2);. timeF
7fcf0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7fd00 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 0);.}../*.** cur
7fd10 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a rent_date().**.*
7fd20 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
7fd30 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 returns the same
7fd40 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27 value as date('
7fd50 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 now')..*/.static
7fd60 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28 void cdateFunc(
7fd70 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7fd80 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
7fd90 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
7fda0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
7fdb0 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 tUsed2.){. UNUS
7fdc0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
7fdd0 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
7fde0 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e ;. dateFunc(con
7fdf0 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a text, 0, 0);.}..
7fe00 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 /*.** current_ti
7fe10 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 mestamp().**.**
7fe20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
7fe30 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
7fe40 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 alue as datetime
7fe50 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 ('now')..*/.stat
7fe60 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 ic void ctimesta
7fe70 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 mpFunc(. sqlite
7fe80 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7fe90 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7fea0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7feb0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7fec0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7fed0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7fee0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74 tUsed2);. datet
7fef0 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c imeFunc(context,
7ff00 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0, 0);.}.#endif
7ff10 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c /* !defined(SQL
7ff20 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d ITE_OMIT_DATETIM
7ff30 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 E_FUNCS) */..#if
7ff40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7ff50 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f DATETIME_FUNCS./
7ff60 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 *.** If the libr
7ff70 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ary is compiled
7ff80 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c to omit the full
7ff90 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 -scale date and
7ffa0 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 time.** handling
7ffb0 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c (to get a small
7ffc0 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 er binary), the
7ffd0 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 following minima
7ffe0 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 l version.** of
7fff0 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 the functions cu
80000 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 rrent_time(), cu
80010 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 rrent_date() and
80020 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 current_timesta
80030 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c mp().** are incl
80040 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 uded instead. Th
80050 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 is is to support
80060 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 column declarat
80070 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 ions that.** inc
80080 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 lude "DEFAULT CU
80090 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e RRENT_TIME" etc.
800a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
800b0 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d tion uses the C-
800c0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e library function
800d0 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 s time(), gmtime
800e0 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 ().** and strfti
800f0 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 me(). The format
80100 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 string to pass
80110 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 to strftime() is
80120 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 supplied.** as
80130 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f the user-data fo
80140 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a r the function..
80150 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
80160 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a urrentTimeFunc(.
80170 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
80180 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
80190 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
801a0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
801b0 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 {. time_t t;.
801c0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 char *zFormat =
801d0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
801e0 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 user_data(contex
801f0 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 t);. sqlite3 *d
80200 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a b;. double rT;.
80210 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b char zBuf[20];
80220 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
80230 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e ETER(argc);. UN
80240 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
80250 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71 rgv);.. db = sq
80260 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
80270 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
80280 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 ;. sqlite3OsCur
80290 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
802a0 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66 s, &rT);.#ifndef
802b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
802c0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20 ATING_POINT. t
802d0 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20 = 86400.0*(rT -
802e0 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35 2440587.5) + 0.5
802f0 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74 ;.#else. /* wit
80300 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f hout floating po
80310 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20 int support, rT
80320 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 will have. ** a
80330 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63 lready lost frac
80340 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69 tional day preci
80350 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d sion.. */. t =
80360 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32 86400 * (rT - 2
80370 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b 440587) - 43200;
80380 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 .#endif.#ifdef H
80390 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b AVE_GMTIME_R. {
803a0 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 . struct tm s
803b0 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f Now;. gmtime_
803c0 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 r(&t, &sNow);.
803d0 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c strftime(zBuf,
803e0 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 20, zFormat, &s
803f0 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a Now);. }.#else.
80400 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
80410 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 m *pTm;. sqli
80420 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
80430 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
80440 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
80450 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
80460 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 pTm = gmtime
80470 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 (&t);. strfti
80480 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f me(zBuf, 20, zFo
80490 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 rmat, pTm);.
804a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
804b0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
804c0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
804d0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
804e0 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ));. }.#endif..
804f0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
80500 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a _text(context, z
80510 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f Buf, -1, SQLITE_
80520 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 TRANSIENT);.}.#e
80530 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
80540 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 function regist
80550 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 ered all of the
80560 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e above C function
80570 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 s as SQL.** func
80580 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f tions. This sho
80590 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 uld be the only
805a0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
805b0 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 file with.** ext
805c0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a ernal linkage..*
805d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
805e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
805f0 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e isterDateTimeFun
80600 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 ctions(void){.
80610 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
80620 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54 D FuncDef aDateT
80630 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 imeFuncs[] = {.#
80640 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
80650 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 IT_DATETIME_FUNC
80660 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a S. FUNCTION(j
80670 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20 ulianday,
80680 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61 -1, 0, 0, julia
80690 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20 ndayFunc ),.
806a0 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20 FUNCTION(date,
806b0 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 -1, 0
806c0 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20 , 0, dateFunc
806d0 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
806e0 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20 ON(time,
806f0 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74 -1, 0, 0, t
80700 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a imeFunc ),.
80710 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 FUNCTION(dat
80720 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d etime, -
80730 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d 1, 0, 0, datetim
80740 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 eFunc ),. FU
80750 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c NCTION(strftime,
80760 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 -1, 0,
80770 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20 0, strftimeFunc
80780 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
80790 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 (current_time,
807a0 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69 0, 0, 0, cti
807b0 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 meFunc ),.
807c0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 FUNCTION(curre
807d0 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c nt_timestamp, 0,
807e0 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 0, 0, ctimestam
807f0 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 pFunc),. FUNC
80800 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 TION(current_dat
80810 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c e, 0, 0, 0,
80820 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 cdateFunc )
80830 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f ,.#else. STR_
80840 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 FUNCTION(current
80850 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22 _time, 0, "
80860 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20 %H:%M:%S",
80870 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 0, currentTi
80880 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 meFunc),. STR
80890 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e _FUNCTION(curren
808a0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 t_timestamp, 0,
808b0 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20 "%Y-%m-%d",
808c0 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 0, currentT
808d0 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 imeFunc),. ST
808e0 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 R_FUNCTION(curre
808f0 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c nt_date, 0,
80900 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d "%Y-%m-%d %H:%M
80910 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74 :%S", 0, current
80920 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69 TimeFunc),.#endi
80930 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a f. };. int i;.
80940 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 FuncDefHash *p
80950 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 Hash = &GLOBAL(F
80960 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 uncDefHash, sqli
80970 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f te3GlobalFunctio
80980 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a ns);. FuncDef *
80990 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 aFunc = (FuncDef
809a0 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 *)&GLOBAL(FuncDe
809b0 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 f, aDateTimeFunc
809c0 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 s);.. for(i=0;
809d0 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74 i<ArraySize(aDat
809e0 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b eTimeFuncs); i++
809f0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 ){. sqlite3Fu
80a00 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 ncDefInsert(pHas
80a10 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 h, &aFunc[i]);.
80a20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
80a30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 ***** End of dat
80a40 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
80a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80a70 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
80a80 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
80a90 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c **********
80aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80ac0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 ****/./*.** 2005
80ad0 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a November 29.**.
80ae0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
80af0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
80b00 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
80b10 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
80b20 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
80b30 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
80b40 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
80b50 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
80b60 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
80b70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
80b80 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
80b90 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
80ba0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
80bb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
80bc0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
80bd0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
80be0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
80bf0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
80c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80c40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
80c50 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 contains OS int
80c60 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 erface code that
80c70 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
80c80 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 l.** architectur
80c90 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 5f es..*/.#define _
80ca0 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 SQLITE_OS_C_ 1.#
80cb0 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 undef _SQLITE_OS
80cc0 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 _C_../*.** The d
80cd0 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20 73 71 efault SQLite sq
80ce0 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c 65 6d lite3_vfs implem
80cf0 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e 6f 74 entations do not
80d00 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d allocate.** mem
80d10 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c 20 6f ory (actually, o
80d20 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 61 74 s_unix.c allocat
80d30 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e es a small amoun
80d40 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 t of memory.** f
80d50 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f 70 65 rom within OsOpe
80d60 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65 20 74 n()), but some t
80d70 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 hird-party imple
80d80 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 2e 0a mentations may..
80d90 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20 74 68 ** So we test th
80da0 65 20 65 66 66 65 63 74 73 20 6f 66 20 61 20 6d e effects of a m
80db0 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e 67 20 alloc() failing
80dc0 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 4f and the sqlite3O
80dd0 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63 74 69 sXXX().** functi
80de0 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c on returning SQL
80df0 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 ITE_IOERR_NOMEM
80e00 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f 53 5f using the DO_OS_
80e10 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 63 72 MALLOC_TEST macr
80e20 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c o..**.** The fol
80e30 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 lowing functions
80e40 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e 74 65 are instrumente
80e50 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 66 d for malloc() f
80e60 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73 74 69 ailure .** testi
80e70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 ng:.**.** sq
80e80 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a lite3OsOpen().**
80e90 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 65 sqlite3OsRe
80ea0 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 ad().** sqli
80eb0 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a 2a 20 te3OsWrite().**
80ec0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e sqlite3OsSyn
80ed0 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 c().** sqlit
80ee0 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f e3OsLock().**.*/
80ef0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
80f00 49 54 45 5f 54 45 53 54 29 20 26 26 20 28 53 51 ITE_TEST) && (SQ
80f10 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a LITE_OS_WIN==0).
80f20 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f #define DO_OS_
80f30 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 69 MALLOC_TEST(x) i
80f40 66 20 28 21 78 20 7c 7c 20 21 73 71 6c 69 74 65 f (!x || !sqlite
80f50 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 3IsMemJournal(x)
80f60 29 20 7b 20 20 20 20 20 5c 0a 20 20 20 20 76 6f ) { \. vo
80f70 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 id *pTstAlloc =
80f80 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 sqlite3Malloc(10
80f90 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
80fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
80fb0 0a 20 20 20 20 69 66 20 28 21 70 54 73 74 41 6c . if (!pTstAl
80fc0 6c 6f 63 29 20 72 65 74 75 72 6e 20 53 51 4c 49 loc) return SQLI
80fd0 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 TE_IOERR_NOMEM;
80fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80ff0 20 20 20 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 \. sqli
81000 74 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c te3_free(pTstAll
81010 6f 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 oc);
81020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
81030 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
81040 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 }.#else. #defi
81050 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f ne DO_OS_MALLOC_
81060 54 45 53 54 28 78 29 0a 23 65 6e 64 69 66 0a 0a TEST(x).#endif..
81070 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
81080 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ing routines are
81090 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 convenience wra
810a0 70 70 65 72 73 20 61 72 6f 75 6e 64 20 6d 65 74 ppers around met
810b0 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 hods.** of the s
810c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 qlite3_file obje
810d0 63 74 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 73 ct. This is mos
810e0 74 6c 79 20 6a 75 73 74 20 73 79 6e 74 61 63 74 tly just syntact
810f0 69 63 20 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a ic sugar. All.**
81100 20 6f 66 20 74 68 69 73 20 77 6f 75 6c 64 20 62 of this would b
81110 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 e completely aut
81120 6f 6d 61 74 69 63 20 69 66 20 53 51 4c 69 74 65 omatic if SQLite
81130 20 77 65 72 65 20 63 6f 64 65 64 20 75 73 69 6e were coded usin
81140 67 0a 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 g.** C++ instead
81150 20 6f 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e of plain old C.
81160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
81170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81180 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
81190 6c 65 20 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 le *pId){. int
811a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
811b0 20 20 69 66 28 20 70 49 64 2d 3e 70 4d 65 74 68 if( pId->pMeth
811c0 6f 64 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ods ){. rc =
811d0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 pId->pMethods->x
811e0 43 6c 6f 73 65 28 70 49 64 29 3b 0a 20 20 20 20 Close(pId);.
811f0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 pId->pMethods =
81200 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
81210 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 rc;.}.SQLITE_PRI
81220 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
81230 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 OsRead(sqlite3_f
81240 69 6c 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 ile *id, void *p
81250 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 Buf, int amt, i6
81260 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4 offset){. DO_
81270 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 OS_MALLOC_TEST(i
81280 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d d);. return id-
81290 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52 65 61 64 >pMethods->xRead
812a0 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 (id, pBuf, amt,
812b0 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 offset);.}.SQLIT
812c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
812d0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c lite3OsWrite(sql
812e0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 63 ite3_file *id, c
812f0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
81300 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 int amt, i64 of
81310 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d fset){. DO_OS_M
81320 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a ALLOC_TEST(id);.
81330 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
81340 74 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 thods->xWrite(id
81350 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 , pBuf, amt, off
81360 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 set);.}.SQLITE_P
81370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
81380 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c e3OsTruncate(sql
81390 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
813a0 36 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 64 size){. retu
813b0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
813c0 3e 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 >xTruncate(id, s
813d0 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ize);.}.SQLITE_P
813e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
813f0 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 e3OsSync(sqlite3
81400 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 _file *id, int f
81410 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d lags){. DO_OS_M
81420 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a ALLOC_TEST(id);.
81430 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
81440 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c thods->xSync(id,
81450 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 flags);.}.SQLIT
81460 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81470 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
81480 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
81490 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 , i64 *pSize){.
814a0 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
814b0 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
814c0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
814d0 46 69 6c 65 53 69 7a 65 28 69 64 2c 20 70 53 69 FileSize(id, pSi
814e0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ze);.}.SQLITE_PR
814f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
81500 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 3OsLock(sqlite3_
81510 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
81520 63 6b 54 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 ckType){. DO_OS
81530 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 _MALLOC_TEST(id)
81540 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
81550 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 Methods->xLock(i
81560 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a d, lockType);.}.
81570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81580 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f nt sqlite3OsUnlo
81590 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
815a0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 *id, int lockTyp
815b0 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d e){. return id-
815c0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f >pMethods->xUnlo
815d0 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 ck(id, lockType)
815e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
815f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81600 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
81610 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
81620 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
81630 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
81640 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
81650 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
81660 73 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 s->xCheckReserve
81670 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 dLock(id, pResOu
81680 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 t);.}.SQLITE_PRI
81690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
816a0 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 OsFileControl(sq
816b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
816c0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 int op, void *pA
816d0 72 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 rg){. return id
816e0 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c ->pMethods->xFil
816f0 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c eControl(id, op,
81700 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 pArg);.}.SQLITE
81710 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
81720 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
81730 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
81740 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 d){. int (*xSec
81750 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 torSize)(sqlite3
81760 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d _file*) = id->pM
81770 65 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 ethods->xSectorS
81780 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 ize;. return (x
81790 53 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 SectorSize ? xSe
817a0 63 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 ctorSize(id) : S
817b0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
817c0 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 CTOR_SIZE);.}.SQ
817d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
817e0 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 sqlite3OsDevice
817f0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
81800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
81810 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e ){. return id->
81820 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 pMethods->xDevic
81830 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
81840 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 (id);.}../*.** T
81850 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 he next group of
81860 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f routines are co
81870 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 nvenience wrappe
81880 72 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a rs around the.**
81890 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f VFS methods..*/
818a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
818b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
818c0 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
818d0 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 *pVfs, . const
818e0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 char *zPath, .
818f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
81900 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 File, . int fla
81910 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 gs, . int *pFla
81920 67 73 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 gsOut.){. int r
81930 63 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f c;. DO_OS_MALLO
81940 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 2f 2a 20 C_TEST(0);. /*
81950 30 78 37 66 31 66 20 69 73 20 61 20 6d 61 73 6b 0x7f1f is a mask
81960 20 6f 66 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f of SQLITE_OPEN_
81970 20 66 6c 61 67 73 20 74 68 61 74 20 61 72 65 20 flags that are
81980 76 61 6c 69 64 20 74 6f 20 62 65 20 70 61 73 73 valid to be pass
81990 65 64 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 ed. ** down int
819a0 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e o the VFS layer.
819b0 20 20 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 Some SQLITE_OP
819c0 45 4e 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65 EN_ flags (for e
819d0 78 61 6d 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c xample,. ** SQL
819e0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 ITE_OPEN_FULLMUT
819f0 45 58 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 EX or SQLITE_OPE
81a00 4e 5f 53 48 41 52 45 44 43 41 43 48 45 29 20 61 N_SHAREDCACHE) a
81a10 72 65 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 re blocked befor
81a20 65 0a 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 e. ** reaching
81a30 74 68 65 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 the VFS. */. rc
81a40 20 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 = pVfs->xOpen(p
81a50 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c Vfs, zPath, pFil
81a60 65 2c 20 66 6c 61 67 73 20 26 20 30 78 37 66 31 e, flags & 0x7f1
81a70 66 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 f, pFlagsOut);.
81a80 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
81a90 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d ITE_OK || pFile-
81aa0 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a >pMethods==0 );.
81ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
81ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81ad0 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 t sqlite3OsDelet
81ae0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 e(sqlite3_vfs *p
81af0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Vfs, const char
81b00 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 *zPath, int dirS
81b10 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ync){. return p
81b20 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 Vfs->xDelete(pVf
81b30 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e s, zPath, dirSyn
81b40 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 c);.}.SQLITE_PRI
81b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
81b60 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 OsAccess(. sqli
81b70 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a te3_vfs *pVfs, .
81b80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
81b90 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 ath, . int flag
81ba0 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f s, . int *pResO
81bb0 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ut.){. DO_OS_MA
81bc0 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 LLOC_TEST(0);.
81bd0 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 return pVfs->xAc
81be0 63 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 cess(pVfs, zPath
81bf0 2c 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 , flags, pResOut
81c00 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
81c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
81c20 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 sFullPathname(.
81c30 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
81c40 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
81c50 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 r *zPath, . int
81c60 20 6e 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 nPathOut, . ch
81c70 61 72 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a ar *zPathOut.){.
81c80 20 20 7a 50 61 74 68 4f 75 74 5b 30 5d 20 3d 20 zPathOut[0] =
81c90 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 0;. return pVfs
81ca0 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 ->xFullPathname(
81cb0 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61 pVfs, zPath, nPa
81cc0 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29 thOut, zPathOut)
81cd0 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
81ce0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
81cf0 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 ENSION.SQLITE_PR
81d00 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
81d10 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 te3OsDlOpen(sqli
81d20 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
81d30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
81d40 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 ){. return pVfs
81d50 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20 ->xDlOpen(pVfs,
81d60 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 zPath);.}.SQLITE
81d70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
81d80 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 lite3OsDlError(s
81d90 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
81da0 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 , int nByte, cha
81db0 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 70 r *zBufOut){. p
81dc0 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 70 56 Vfs->xDlError(pV
81dd0 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f fs, nByte, zBufO
81de0 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ut);.}.SQLITE_PR
81df0 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c IVATE void (*sql
81e00 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69 ite3OsDlSym(sqli
81e10 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 te3_vfs *pVfs, v
81e20 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 oid *pHdle, cons
81e30 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 28 76 t char *zSym))(v
81e40 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 oid){. return p
81e50 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 66 73 Vfs->xDlSym(pVfs
81e60 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a , pHdle, zSym);.
81e70 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
81e80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 void sqlite3OsD
81e90 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 lClose(sqlite3_v
81ea0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
81eb0 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73 pHandle){. pVfs
81ec0 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c ->xDlClose(pVfs,
81ed0 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e pHandle);.}.#en
81ee0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
81ef0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
81f00 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 N */.SQLITE_PRIV
81f10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
81f20 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 sRandomness(sqli
81f30 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
81f40 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a nt nByte, char *
81f50 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 zBufOut){. retu
81f60 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d rn pVfs->xRandom
81f70 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65 ness(pVfs, nByte
81f80 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 , zBufOut);.}.SQ
81f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
81fa0 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 sqlite3OsSleep(
81fb0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
81fc0 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a s, int nMicro){.
81fd0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
81fe0 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69 63 Sleep(pVfs, nMic
81ff0 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ro);.}.SQLITE_PR
82000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
82010 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 3OsCurrentTime(s
82020 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
82030 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f , double *pTimeO
82040 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 ut){. return pV
82050 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 fs->xCurrentTime
82060 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 (pVfs, pTimeOut)
82070 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
82080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
82090 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 sOpenMalloc(. s
820a0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
820b0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
820c0 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 *zFile, . sqlit
820d0 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 e3_file **ppFile
820e0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a , . int flags,.
820f0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 int *pOutFlags
82100 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
82110 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 QLITE_NOMEM;. s
82120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
82130 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 le;. pFile = (s
82140 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 qlite3_file *)sq
82150 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73 lite3Malloc(pVfs
82160 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 ->szOsFile);. i
82170 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 f( pFile ){.
82180 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
82190 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 en(pVfs, zFile,
821a0 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f pFile, flags, pO
821b0 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 utFlags);. if
821c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
821d0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
821e0 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 _free(pFile);.
821f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a }else{. *
82200 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a ppFile = pFile;.
82210 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
82220 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
82230 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
82240 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 te3OsCloseFree(s
82250 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
82260 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 le){. int rc =
82270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
82280 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
82290 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c rc = sqlite3OsCl
822a0 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 71 ose(pFile);. sq
822b0 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
822c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
822d0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
822e0 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70 nction is a wrap
822f0 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 4f per around the O
82300 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 6c 65 S specific imple
82310 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 mentation of.**
82320 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
82330 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f ). The purpose o
82340 66 20 74 68 65 20 77 72 61 70 70 65 72 20 69 73 f the wrapper is
82350 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a to provide the.
82360 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 ** ability to si
82370 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f 63 20 mulate a malloc
82380 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 61 74 failure, so that
82390 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 the handling of
823a0 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e 20 an.** error in
823b0 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
823c0 29 20 62 79 20 74 68 65 20 75 70 70 65 72 20 6c ) by the upper l
823d0 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 65 73 ayers can be tes
823e0 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
823f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
82400 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 7b 0a e3OsInit(void){.
82410 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 void *p = sqli
82420 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 3b 0a te3_malloc(10);.
82430 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
82440 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
82450 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
82460 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 (p);. return sq
82470 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 3b lite3_os_init();
82480 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 .}../*.** The li
82490 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 74 st of all regist
824a0 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d 65 ered VFS impleme
824b0 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 ntations..*/.sta
824c0 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 tic sqlite3_vfs
824d0 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 73 * SQLITE_WSD vfs
824e0 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e List = 0;.#defin
824f0 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 4c e vfsList GLOBAL
82500 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
82510 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 vfsList)../*.**
82520 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 20 Locate a VFS by
82530 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 6d name. If no nam
82540 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d 70 e is given, simp
82550 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a ly return the.**
82560 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 68 first VFS on th
82570 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 e list..*/.SQLIT
82580 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 E_API sqlite3_vf
82590 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 s *sqlite3_vfs_f
825a0 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ind(const char *
825b0 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 zVfs){. sqlite3
825c0 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a _vfs *pVfs = 0;.
825d0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
825e0 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f DSAFE. sqlite3_
825f0 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 65 mutex *mutex;.#e
82600 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
82610 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
82620 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c T. int rc = sql
82630 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
82640 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
82650 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 23 turn 0;.#endif.#
82660 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
82670 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 73 SAFE. mutex = s
82680 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
82690 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
826a0 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 ATIC_MASTER);.#e
826b0 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
826c0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
826d0 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d 20 );. for(pVfs =
826e0 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 70 vfsList; pVfs; p
826f0 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29 Vfs=pVfs->pNext)
82700 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d 3d {. if( zVfs==
82710 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 0 ) break;. i
82720 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c 20 f( strcmp(zVfs,
82730 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 pVfs->zName)==0
82740 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 ) break;. }. s
82750 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
82760 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
82770 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a urn pVfs;.}../*.
82780 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20 ** Unlink a VFS
82790 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20 from the linked
827a0 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 list.*/.static v
827b0 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71 oid vfsUnlink(sq
827c0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 lite3_vfs *pVfs)
827d0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
827e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 te3_mutex_held(s
827f0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
82800 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
82810 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b ATIC_MASTER)) );
82820 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 . if( pVfs==0 )
82830 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a {. /* No-op *
82840 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 /. }else if( vf
82850 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20 sList==pVfs ){.
82860 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 vfsList = pVf
82870 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 s->pNext;. }els
82880 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b e if( vfsList ){
82890 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
828a0 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 *p = vfsList;.
828b0 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65 while( p->pNe
828c0 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d xt && p->pNext!=
828d0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20 pVfs ){. p
828e0 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 = p->pNext;.
828f0 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 }. if( p->pNe
82900 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 xt==pVfs ){.
82910 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 p->pNext = pVf
82920 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a s->pNext;. }.
82930 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 }.}../*.** Reg
82940 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 ister a VFS with
82950 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74 the system. It
82960 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20 is harmless to
82970 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61 6d register the sam
82980 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c e.** VFS multipl
82990 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65 e times. The ne
829a0 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 w VFS becomes th
829b0 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b e default if mak
829c0 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65 eDflt is.** true
829d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
829e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f int sqlite3_vfs_
829f0 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 register(sqlite3
82a00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
82a10 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71 6c makeDflt){. sql
82a20 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
82a30 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 x = 0;.#ifndef S
82a40 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
82a50 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 NIT. int rc = s
82a60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
82a70 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 e();. if( rc )
82a80 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
82a90 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 f. mutex = sqli
82aa0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
82ab0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
82ac0 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c C_MASTER);. sql
82ad0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
82ae0 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e (mutex);. vfsUn
82af0 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 66 link(pVfs);. if
82b00 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 66 ( makeDflt || vf
82b10 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 sList==0 ){.
82b20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 pVfs->pNext = vf
82b30 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c 69 sList;. vfsLi
82b40 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 6c st = pVfs;. }el
82b50 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e se{. pVfs->pN
82b60 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70 ext = vfsList->p
82b70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 Next;. vfsLis
82b80 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b t->pNext = pVfs;
82b90 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 66 . }. assert(vf
82ba0 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 sList);. sqlite
82bb0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
82bc0 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 tex);. return S
82bd0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
82be0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 ** Unregister a
82bf0 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 69 VFS so that it i
82c00 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 65 s no longer acce
82c10 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ssible..*/.SQLIT
82c20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
82c30 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 3_vfs_unregister
82c40 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
82c50 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f fs){.#if SQLITE_
82c60 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c THREADSAFE. sql
82c70 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
82c80 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 x = sqlite3Mutex
82c90 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
82ca0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
82cb0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
82cc0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
82cd0 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c mutex);. vfsUnl
82ce0 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 6c ink(pVfs);. sql
82cf0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
82d00 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 (mutex);. retur
82d10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
82d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
82d30 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a End of os.c ****
82d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
82d70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
82d80 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c 74 Begin file fault
82d90 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
82da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
82dc0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 32 /*.** 2008 Jan 2
82dd0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
82de0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
82df0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
82e00 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
82e10 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
82e20 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
82e30 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
82e40 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
82e50 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
82e60 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
82e70 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
82e80 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
82e90 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
82ea0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
82eb0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
82ec0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
82ed0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
82ee0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
82ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
82f40 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
82f50 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f o support the co
82f60 6e 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e ncept of "benign
82f70 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 " .** malloc fai
82f80 6c 75 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 lures (when the
82f90 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 xMalloc() or xRe
82fa0 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f alloc() method o
82fb0 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 f the.** sqlite3
82fc0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 _mem_methods str
82fd0 75 63 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 ucture fails to
82fe0 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b allocate a block
82ff0 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e of memory.** an
83000 64 20 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a d returns 0). .*
83010 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 *.** Most malloc
83020 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f failures are no
83030 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 n-benign. After
83040 74 68 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 they occur, SQLi
83050 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 te.** abandons t
83060 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 he current opera
83070 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 tion and returns
83080 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 an error code (
83090 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 usually.** SQLIT
830a0 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 E_NOMEM) to the
830b0 75 73 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 user. However, s
830c0 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 ometimes a fault
830d0 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
830e0 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f ily.** fatal. Fo
830f0 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 r example, if a
83100 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 malloc fails whi
83110 6c 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 le resizing a ha
83120 73 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a sh table, this .
83130 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 ** is completely
83140 20 72 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d recoverable sim
83150 70 6c 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 ply by not carry
83160 69 6e 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 ing out the resi
83170 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 ze. The .** hash
83180 20 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 table will cont
83190 69 6e 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e inue to function
831a0 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 normally. So a
831b0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
831c0 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 .** during a has
831d0 68 20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 h table resize i
831e0 73 20 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 s a benign fault
831f0 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 ..*/...#ifndef S
83200 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
83210 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 IN_TEST../*.** G
83220 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e lobal variables.
83230 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
83240 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 ct BenignMallocH
83250 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f ooks BenignMallo
83260 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 cHooks;.static S
83270 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
83280 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
83290 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 ks {. void (*xB
832a0 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 enignBegin)(void
832b0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e );. void (*xBen
832c0 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d ignEnd)(void);.}
832d0 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 sqlite3Hooks =
832e0 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 { 0, 0 };../* Th
832f0 65 20 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 e "wsdHooks" mac
83300 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
83310 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 to the appropria
83320 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 te BenignMallocH
83330 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 ooks.** structur
83340 65 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 e. If writable
83350 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 static data is u
83360 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 nsupported on th
83370 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 e target,.** we
83380 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 have to locate t
83390 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 he state vector
833a0 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e at run-time. In
833b0 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e the more common
833c0 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 .** case where w
833d0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
833e0 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 ata is supported
833f0 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 , wsdHooks can r
83400 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a efer directly.**
83410 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 to the "sqlite3
83420 48 6f 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 Hooks" state vec
83430 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f tor declared abo
83440 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ve..*/.#ifdef SQ
83450 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 LITE_OMIT_WSD.#
83460 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 define wsdHooksI
83470 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 nit \. BenignMa
83480 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 llocHooks *x = &
83490 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c GLOBAL(BenignMal
834a0 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 locHooks,sqlite3
834b0 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 Hooks).# define
834c0 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 wsdHooks x[0].#e
834d0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 lse.# define wsd
834e0 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 HooksInit.# defi
834f0 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 ne wsdHooks sqli
83500 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a te3Hooks.#endif.
83510 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
83520 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 hooks to call w
83530 68 65 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e hen sqlite3Begin
83540 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 BenignMalloc() a
83550 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 nd.** sqlite3End
83560 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 BenignMalloc() a
83570 72 65 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 re called, respe
83580 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 ctively..*/.SQLI
83590 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
835a0 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c sqlite3BenignMal
835b0 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 locHooks(. void
835c0 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 (*xBenignBegin)
835d0 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 (void),. void (
835e0 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 *xBenignEnd)(voi
835f0 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 d).){. wsdHooks
83600 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 Init;. wsdHooks
83610 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 .xBenignBegin =
83620 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 xBenignBegin;.
83630 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
83640 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 End = xBenignEnd
83650 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
83660 28 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 (sqlite3EndBenig
83670 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 nMalloc()) is ca
83680 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 lled by SQLite c
83690 6f 64 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 ode to indicate
836a0 74 68 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 that.** subseque
836b0 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 nt malloc failur
836c0 65 73 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 es are benign. A
836d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
836e0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
836f0 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 ).** indicates t
83700 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d hat subsequent m
83710 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 alloc failures a
83720 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a re non-benign..*
83730 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
83740 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 void sqlite3Beg
83750 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 inBenignMalloc(v
83760 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 oid){. wsdHooks
83770 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 Init;. if( wsdH
83780 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 ooks.xBenignBegi
83790 6e 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b n ){. wsdHook
837a0 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 s.xBenignBegin()
837b0 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 ;. }.}.SQLITE_P
837c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
837d0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
837e0 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 oc(void){. wsdH
837f0 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 ooksInit;. if(
83800 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
83810 45 6e 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f End ){. wsdHo
83820 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 oks.xBenignEnd()
83830 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 ;. }.}..#endif
83840 20 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c /* #ifndef SQL
83850 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
83860 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a _TEST */../*****
83870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
83880 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a fault.c *******
83890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
838a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
838b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
838c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
838d0 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a file mem0.c ****
838e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
838f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
83910 32 30 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2008 October 28.
83920 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
83930 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
83940 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
83950 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
83960 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
83970 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
83980 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
83990 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
839a0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
839b0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
839c0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
839d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
839e0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
839f0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
83a00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
83a10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
83a20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
83a30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
83a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
83a80 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
83a90 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 ontains a no-op
83aa0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
83ab0 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 n drivers for us
83ac0 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 e when.** SQLITE
83ad0 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 _ZERO_MALLOC is
83ae0 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c defined. The al
83af0 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
83b00 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 implemented.**
83b10 68 65 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c here always fail
83b20 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
83b30 6f 74 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 ot operate with
83b40 74 68 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 these drivers.
83b50 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 These.** are mer
83b60 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 ely placeholders
83b70 2e 20 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 . Real drivers
83b80 6d 75 73 74 20 62 65 20 73 75 62 73 74 69 74 75 must be substitu
83b90 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c ted using.** sql
83ba0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 ite3_config() be
83bb0 66 6f 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c fore SQLite will
83bc0 20 6f 70 65 72 61 74 65 2e 0a 2a 2f 0a 0a 2f 2a operate..*/../*
83bd0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
83be0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
83bf0 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 llocator is the
83c00 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a default. It is.
83c10 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 ** used when no
83c20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c other memory all
83c30 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 ocator is specif
83c40 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c ied using compil
83c50 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 e-time.** macros
83c60 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
83c70 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a TE_ZERO_MALLOC..
83c80 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 /*.** No-op vers
83c90 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f ions of all memo
83ca0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f ry allocation ro
83cb0 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 utines.*/.static
83cc0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 void *sqlite3Me
83cd0 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 mMalloc(int nByt
83ce0 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a e){ return 0; }.
83cf0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
83d00 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 te3MemFree(void
83d10 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 6e *pPrior){ return
83d20 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
83d30 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c *sqlite3MemReall
83d40 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c oc(void *pPrior,
83d50 20 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 int nByte){ ret
83d60 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
83d70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 int sqlite3MemSi
83d80 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 ze(void *pPrior)
83d90 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 { return 0; }.st
83da0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
83db0 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e MemRoundup(int n
83dc0 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 ){ return n; }.s
83dd0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
83de0 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 3MemInit(void *N
83df0 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 20 otUsed){ return
83e00 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 SQLITE_OK; }.sta
83e10 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
83e20 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 MemShutdown(void
83e30 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 *NotUsed){ retu
83e40 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rn; }../*.** Thi
83e50 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 s routine is the
83e60 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e only routine in
83e70 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 this file with
83e80 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 external linkage
83e90 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 ..**.** Populate
83ea0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
83eb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
83ec0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 function pointe
83ed0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 rs in.** sqlite3
83ee0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 GlobalConfig.m w
83ef0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
83f00 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 the routines in
83f10 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 this file..*/.SQ
83f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
83f30 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 d sqlite3MemSetD
83f40 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 efault(void){.
83f50 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
83f60 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
83f70 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 defaultMethods
83f80 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 = {. sqlite3
83f90 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 MemMalloc,.
83fa0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a sqlite3MemFree,.
83fb0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
83fc0 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c ealloc,. sql
83fd0 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 ite3MemSize,.
83fe0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e sqlite3MemRoun
83ff0 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 dup,. sqlite
84000 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 3MemInit,. s
84010 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 qlite3MemShutdow
84020 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 n,. 0. };.
84030 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
84040 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 SQLITE_CONFIG_MA
84050 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 LLOC, &defaultMe
84060 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 thods);.}..#endi
84070 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f f /* SQLITE_ZERO
84080 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a _MALLOC */../***
84090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
840a0 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a of mem0.c ******
840b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
840c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
840d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
840e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
840f0 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a n file mem1.c **
84100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
84130 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 * 2007 August 14
84140 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
84150 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
84160 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
84170 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
84180 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
84190 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
841a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
841b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
841c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
841d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
841e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
841f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
84200 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
84210 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
84220 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
84230 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
84240 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
84250 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
84260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
842a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
842b0 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 contains low-lev
842c0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
842d0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 tion drivers for
842e0 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 when.** SQLite
842f0 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61 will use the sta
84300 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20 ndard C-library
84310 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 malloc/realloc/f
84320 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a ree interface.**
84330 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d to obtain the m
84340 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 0a emory it needs..
84350 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
84360 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 contains impleme
84370 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
84380 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
84390 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 allocation.** r
843a0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 outines specifie
843b0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 d in the sqlite3
843c0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a _mem_methods obj
843d0 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ect..*/../*.** T
843e0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
843f0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
84400 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 tor is the defau
84410 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 lt. It is.** us
84420 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 ed when no other
84430 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
84440 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 r is specified u
84450 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d sing compile-tim
84460 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a e.** macros..*/.
84470 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 59 #ifdef SQLITE_SY
84480 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a STEM_MALLOC../*.
84490 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 ** Like malloc()
844a0 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 20 74 , but remember t
844b0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 he size of the a
844c0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 llocation.** so
844d0 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 6e 64 that we can find
844e0 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e 67 20 it later using
844f0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 29 sqlite3MemSize()
84500 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ..**.** For this
84510 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 low-level routi
84520 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 72 61 ne, we are guara
84530 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 74 65 nteed that nByte
84540 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 63 61 >0 because.** ca
84550 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d 30 20 ses of nByte<=0
84560 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 65 70 will be intercep
84570 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 ted and dealt wi
84580 74 68 20 62 79 20 68 69 67 68 65 72 20 6c 65 76 th by higher lev
84590 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a el.** routines..
845a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
845b0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
845c0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 (int nByte){. s
845d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b qlite3_int64 *p;
845e0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
845f0 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 >0 );. nByte =
84600 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 ROUND8(nByte);.
84610 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 p = malloc( nBy
84620 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20 te+8 );. if( p
84630 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 ){. p[0] = nB
84640 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 yte;. p++;.
84650 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
84660 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c *)p;.}../*.** L
84670 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20 77 ike free() but w
84680 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 orks for allocat
84690 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66 72 ions obtained fr
846a0 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c om sqlite3MemMal
846b0 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 loc().** or sqli
846c0 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 2e te3MemRealloc().
846d0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 .**.** For this
846e0 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e low-level routin
846f0 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e e, we already kn
84700 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d ow that pPrior!=
84710 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65 73 0 since.** cases
84720 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d 30 where pPrior==0
84730 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 will have been
84740 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 64 intecepted and d
84750 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79 20 ealt with.** by
84760 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 higher-level rou
84770 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tines..*/.static
84780 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
84790 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
847a0 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e r){. sqlite3_in
847b0 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 t64 *p = (sqlite
847c0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
847d0 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f . assert( pPrio
847e0 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 r!=0 );. p--;.
847f0 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a free(p);.}../*.
84800 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 ** Like realloc(
84810 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61 6c ). Resize an al
84820 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 location previou
84830 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f sly obtained fro
84840 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 4d m.** sqlite3MemM
84850 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 alloc()..**.** F
84860 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 or this low-leve
84870 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 20 l interface, we
84880 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 know that pPrior
84890 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65 72 !=0. Cases wher
848a0 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 77 e.** pPrior==0 w
848b0 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20 69 hile have been i
848c0 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 ntercepted by hi
848d0 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 gher-level routi
848e0 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72 65 ne and.** redire
848f0 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e cted to xMalloc.
84900 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 20 Similarly, we
84910 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 3e know that nByte>
84920 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63 61 0 becauses.** ca
84930 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 3c ses where nByte<
84940 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 =0 will have bee
84950 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 n intercepted by
84960 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a higher-level.**
84970 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72 65 routines and re
84980 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72 65 directed to xFre
84990 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
849a0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 d *sqlite3MemRea
849b0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f lloc(void *pPrio
849c0 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 r, int nByte){.
849d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
849e0 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 p = (sqlite3_int
849f0 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 64*)pPrior;. as
84a00 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 sert( pPrior!=0
84a10 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 && nByte>0 );.
84a20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e nByte = ROUND8(n
84a30 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28 73 71 Byte);. p = (sq
84a40 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
84a50 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 ior;. p--;. p
84a60 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 = realloc(p, nBy
84a70 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20 te+8 );. if( p
84a80 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 ){. p[0] = nB
84a90 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 yte;. p++;.
84aa0 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
84ab0 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 *)p;.}../*.** Re
84ac0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 port the allocat
84ad0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69 ed size of a pri
84ae0 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78 or return from x
84af0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 Malloc().** or x
84b00 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 Realloc()..*/.st
84b10 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
84b20 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 MemSize(void *pP
84b30 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 rior){. sqlite3
84b40 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28 _int64 *p;. if(
84b50 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74 pPrior==0 ) ret
84b60 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71 urn 0;. p = (sq
84b70 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 lite3_int64*)pPr
84b80 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 ior;. p--;. re
84b90 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a turn (int)p[0];.
84ba0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 }../*.** Round u
84bb0 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 p a request size
84bc0 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c to the next val
84bd0 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 id allocation si
84be0 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ze..*/.static in
84bf0 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
84c00 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 dup(int n){. re
84c10 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a turn ROUND8(n);.
84c20 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
84c30 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e ize this module.
84c40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
84c50 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f qlite3MemInit(vo
84c60 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
84c70 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
84c80 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
84c90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
84ca0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 ../*.** Deinitia
84cb0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
84cc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
84cd0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
84ce0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 own(void *NotUse
84cf0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
84d00 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
84d10 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a . return;.}../*
84d20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
84d30 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 is the only rou
84d40 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c tine in this fil
84d50 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 e with external
84d60 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 linkage..**.** P
84d70 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d opulate the low-
84d80 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
84d90 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
84da0 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 pointers in.**
84db0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
84dc0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 fig.m with point
84dd0 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 ers to the routi
84de0 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nes in this file
84df0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
84e00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
84e10 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f MemSetDefault(vo
84e20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
84e30 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
84e40 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d methods defaultM
84e50 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
84e60 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
84e70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
84e80 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 mFree,. sqli
84e90 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 te3MemRealloc,.
84ea0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 sqlite3MemSi
84eb0 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 ze,. sqlite3
84ec0 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 MemRoundup,.
84ed0 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c sqlite3MemInit,
84ee0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
84ef0 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 Shutdown,. 0
84f00 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f . };. sqlite3_
84f10 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
84f20 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 NFIG_MALLOC, &de
84f30 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d faultMethods);.}
84f40 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
84f50 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 TE_SYSTEM_MALLOC
84f60 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
84f70 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 31 **** End of mem1
84f80 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
84f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84fb0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
84fc0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
84fd0 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem2.c *********
84fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85000 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
85010 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 August 15.**.**
85020 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
85030 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
85040 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
85050 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
85060 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
85070 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
85080 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
85090 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
850a0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
850b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
850c0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
850d0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
850e0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
850f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
85100 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
85110 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
85120 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
85130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
85170 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
85180 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
85190 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f s low-level memo
851a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
851b0 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a ivers for when.*
851c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
851d0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 e the standard C
851e0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f -library malloc/
851f0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 realloc/free int
85200 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 erface.** to obt
85210 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 ain the memory i
85220 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61 64 t needs while ad
85230 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64 64 ding lots of add
85240 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69 6e itional debuggin
85250 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e g.** information
85260 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61 74 to each allocat
85270 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ion in order to
85280 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64 20 help detect and
85290 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 fix memory.** le
852a0 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20 75 aks and memory u
852b0 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a sage errors..**.
852c0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
852d0 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 tains implementa
852e0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 tions of the low
852f0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
85300 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 location.** rout
85310 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 ines specified i
85320 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 n the sqlite3_me
85330 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 m_methods object
85340 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ..*/../*.** This
85350 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
85360 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
85370 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69 66 is used only if
85380 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d the.** SQLITE_M
85390 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 EMDEBUG macro is
853a0 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 defined.*/.#ifd
853b0 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ef SQLITE_MEMDEB
853c0 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 UG../*.** The ba
853d0 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e cktrace function
853e0 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61 76 ality is only av
853f0 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c 49 ailable with GLI
85400 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 47 BC.*/.#ifdef __G
85410 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e 20 LIBC__. extern
85420 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76 6f int backtrace(vo
85430 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78 74 id**,int);. ext
85440 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 61 ern void backtra
85450 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 6f ce_symbols_fd(vo
85460 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e id*const*,int,in
85470 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 t);.#else.# defi
85480 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c 42 ne backtrace(A,B
85490 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61 63 ) 1.# define bac
854a0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 ktrace_symbols_f
854b0 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a d(A,B,C).#endif.
854c0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d 6f ./*.** Each memo
854d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f ry allocation lo
854e0 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a oks like this:.*
854f0 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d *.** ----------
85500 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85510 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85520 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85530 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
85540 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 61 * | Title | ba
85550 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 cktrace pointers
85560 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 | MemBlockHdr
85570 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 | allocation |
85580 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 20 EndGuard |.**
85590 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
855a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
855b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
855c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
855d0 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 --------.**.** T
855e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 he application c
855f0 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 20 ode sees only a
85600 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 pointer to the a
85610 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 68 llocation. We h
85620 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 75 ave.** to back u
85630 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 p from the alloc
85640 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 6f ation pointer to
85650 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f find the MemBlo
85660 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 4d ckHdr. The.** M
85670 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c 73 emBlockHdr tells
85680 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 us the size of
85690 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 the allocation a
856a0 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
856b0 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 6f .** backtrace po
856c0 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 69 inters. There i
856d0 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 77 s also a guard w
856e0 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f ord at the end o
856f0 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 f the.** MemBloc
85700 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 kHdr..*/.struct
85710 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 MemBlockHdr {.
85720 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 20 i64 iSize;
85730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85740 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
85750 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a his allocation *
85760 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c /. struct MemBl
85770 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a ockHdr *pNext, *
85780 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 pPrev; /* Linke
85790 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e d list of all un
857a0 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a freed memory */.
857b0 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 char nBacktrac
857c0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
857d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
857e0 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f 6e of backtraces on
857f0 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 this alloc */.
85800 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65 char nBacktrace
85810 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 20 Slots;
85820 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c /* Availabl
85830 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f 74 e backtrace slot
85840 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54 69 s */. short nTi
85850 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 tle;
85860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
85870 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 6e tes of title; in
85880 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a 20 cludes '\0' */.
85890 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64 3b int iForeGuard;
858a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
858b0 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77 6f /* Guard wo
858c0 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a 2f rd for sanity */
858d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 .};../*.** Guard
858e0 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69 6e words.*/.#defin
858f0 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 38 30 e FOREGUARD 0x80
85900 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20 52 F5E153.#define R
85910 45 41 52 47 55 41 52 44 20 30 78 45 34 36 37 36 EARGUARD 0xE4676
85920 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 B53../*.** Numbe
85930 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 r of malloc size
85940 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20 74 increments to t
85950 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rack..*/.#define
85960 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a 2f NCSIZE 1000../
85970 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
85980 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
85990 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f used by this mo
859a0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 dule are collect
859b0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e ed.** into a sin
859c0 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 gle structure na
859d0 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 73 med "mem". This
859e0 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a is to keep the.
859f0 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62 ** static variab
85a00 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e les organized an
85a10 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 d to reduce name
85a20 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a space pollution.
85a30 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 ** when this mod
85a40 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 ule is combined
85a50 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 with other in th
85a60 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a e amalgamation..
85a70 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
85a80 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 {. . /*. **
85a90 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c Mutex to control
85aa0 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d access to the m
85ab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
85ac0 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f subsystem.. */
85ad0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
85ae0 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 *mutex;.. /*.
85af0 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 69 ** Head and tai
85b00 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 l of a linked li
85b10 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 61 st of all outsta
85b20 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e nding allocation
85b30 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 s. */. struct
85b40 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46 69 MemBlockHdr *pFi
85b50 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 rst;. struct Me
85b60 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73 74 mBlockHdr *pLast
85b70 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 ;. . /*. ** T
85b80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 he number of lev
85b90 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63 65 els of backtrace
85ba0 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77 20 to save in new
85bb0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a allocations.. *
85bc0 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72 61 /. int nBacktra
85bd0 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 61 ce;. void (*xBa
85be0 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e cktrace)(int, in
85bf0 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 t, void **);..
85c00 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74 65 /*. ** Title te
85c10 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 20 xt to insert in
85c20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62 6c front of each bl
85c30 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e ock. */. int n
85c40 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a Title; /*
85c50 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c 65 Bytes of zTitle
85c60 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c 75 to save. Inclu
85c70 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61 64 des '\0' and pad
85c80 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a ding */. char z
85c90 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 Title[100]; /*
85ca0 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 20 2a The title text *
85cb0 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 /.. /* . ** sq
85cc0 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c lite3MallocDisal
85cd0 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74 73 low() increments
85ce0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
85cf0 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71 6c ounter.. ** sql
85d00 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 ite3MallocAllow(
85d10 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74 2e ) decrements it.
85d20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61 . */. int disa
85d30 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 llow; /* Do not
85d40 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c 6c allow memory all
85d50 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a ocation */.. /*
85d60 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74 61 . ** Gather sta
85d70 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20 73 tistics on the s
85d80 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 izes of memory a
85d90 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a llocations.. **
85da0 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74 68 nAlloc[i] is th
85db0 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f e number of allo
85dc0 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 cation attempts
85dd0 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74 65 of i*8. ** byte
85de0 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69 73 s. i==NCSIZE is
85df0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 the number of a
85e00 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
85e10 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 ts for. ** size
85e20 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53 49 s more than NCSI
85e30 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a 2f ZE*8 bytes.. */
85e40 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 . int nAlloc[NC
85e50 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20 54 SIZE]; /* T
85e60 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 61 otal number of a
85e70 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 llocations */.
85e80 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43 53 int nCurrent[NCS
85e90 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72 72 IZE]; /* Curr
85ea0 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c ent number of al
85eb0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 locations */. i
85ec0 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53 nt mxCurrent[NCS
85ed0 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 77 IZE]; /* Highw
85ee0 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 43 ater mark for nC
85ef0 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d urrent */..} mem
85f00 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 ;.../*.** Adjust
85f10 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73 74 memory usage st
85f20 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61 74 atistics.*/.stat
85f30 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53 74 ic void adjustSt
85f40 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20 69 ats(int iSize, i
85f50 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 nt increment){.
85f60 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38 28 int i = ROUND8(
85f70 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28 20 iSize)/8;. if(
85f80 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20 i>NCSIZE-1 ){.
85f90 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31 i = NCSIZE - 1
85fa0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63 72 ;. }. if( incr
85fb0 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d ement>0 ){. m
85fc0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a em.nAlloc[i]++;.
85fd0 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 mem.nCurrent
85fe0 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d [i]++;. if( m
85ff0 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d em.nCurrent[i]>m
86000 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 em.mxCurrent[i]
86010 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 ){. mem.mxC
86020 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e urrent[i] = mem.
86030 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 nCurrent[i];.
86040 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
86050 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d mem.nCurrent[i]-
86060 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d -;. assert( m
86070 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d em.nCurrent[i]>=
86080 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 0 );. }.}../*.*
86090 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 * Given an alloc
860a0 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 ation, find the
860b0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 MemBlockHdr for
860c0 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e that allocation.
860d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
860e0 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20 67 ine checks the g
860f0 75 61 72 64 73 20 61 74 20 65 69 74 68 65 72 20 uards at either
86100 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 end of the alloc
86110 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 ation and.** if
86120 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72 65 they are incorre
86130 63 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a 2a ct it asserts..*
86140 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
86150 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c MemBlockHdr *sql
86160 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 ite3MemsysGetHea
86170 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 der(void *pAlloc
86180 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 ation){. struct
86190 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b MemBlockHdr *p;
861a0 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 . int *pInt;.
861b0 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e u8 *pU8;. int n
861c0 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20 Reserve;.. p =
861d0 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b (struct MemBlock
861e0 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e Hdr*)pAllocation
861f0 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72 ;. p--;. asser
86200 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72 64 t( p->iForeGuard
86210 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52 44 ==(int)FOREGUARD
86220 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d );. nReserve =
86230 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a 65 ROUND8(p->iSize
86240 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 );. pInt = (int
86250 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 *)pAllocation;.
86260 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c 6c pU8 = (u8*)pAll
86270 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65 72 ocation;. asser
86280 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 t( pInt[nReserve
86290 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d 28 /sizeof(int)]==(
862a0 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 29 3b int)REARGUARD );
862b0 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63 6b . /* This check
862c0 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65 78 s any of the "ex
862d0 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f 63 tra" bytes alloc
862e0 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74 6f ated due. ** to
862f0 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f 20 rounding up to
86300 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 an 8 byte bounda
86310 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20 20 ry to ensure .
86320 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74 20 ** they haven't
86330 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e been overwritten
86340 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 .. */. while(
86350 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d 3e nReserve-- > p->
86360 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 28 20 iSize ) assert(
86370 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d 30 pU8[nReserve]==0
86380 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 x65 );. return
86390 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 p;.}../*.** Retu
863a0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
863b0 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79 bytes currently
863c0 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64 allocated at ad
863d0 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 dress p..*/.stat
863e0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
863f0 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a mSize(void *p){.
86400 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
86410 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66 kHdr *pHdr;. if
86420 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 ( !p ){. retu
86430 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72 rn 0;. }. pHdr
86440 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 = sqlite3Memsys
86450 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20 GetHeader(p);.
86460 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69 return pHdr->iSi
86470 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ze;.}../*.** Ini
86480 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
86490 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
864a0 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 bsystem..*/.stat
864b0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
864c0 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
864d0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
864e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
864f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 69 );. assert( (si
86500 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d 42 zeof(struct MemB
86510 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20 30 lockHdr)&7) == 0
86520 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 );. if( !sqlit
86530 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
86540 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 2f Memstat ){. /
86550 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61 74 * If memory stat
86560 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 us is enabled, t
86570 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e 63 hen the malloc.c
86580 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61 6c wrapper will al
86590 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f 6c ready. ** hol
865a0 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d d the STATIC_MEM
865b0 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65 20 mutex when the
865c0 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61 72 routines here ar
865d0 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20 e invoked. */.
865e0 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71 mem.mutex = sq
865f0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
86600 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
86610 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 TIC_MEM);. }.
86620 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
86630 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
86640 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
86650 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
86660 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 bsystem..*/.stat
86670 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d ic void sqlite3M
86680 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 emShutdown(void
86690 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 *NotUsed){. UNU
866a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
866b0 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75 tUsed);. mem.mu
866c0 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a tex = 0;.}../*.*
866d0 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 * Round up a req
866e0 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 uest size to the
866f0 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f next valid allo
86700 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a cation size..*/.
86710 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
86720 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 e3MemRoundup(int
86730 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f n){. return RO
86740 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a UND8(n);.}../*.*
86750 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 * Allocate nByte
86760 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
86770 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
86780 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c *sqlite3MemMall
86790 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 oc(int nByte){.
867a0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
867b0 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 Hdr *pHdr;. voi
867c0 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20 d **pBt;. char
867d0 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b *z;. int *pInt;
867e0 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a . void *p = 0;.
867f0 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b int totalSize;
86800 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b . int nReserve;
86810 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
86820 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 _enter(mem.mutex
86830 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
86840 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a .disallow==0 );.
86850 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 nReserve = ROU
86860 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 6f ND8(nByte);. to
86870 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 72 talSize = nReser
86880 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 ve + sizeof(*pHd
86890 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 r) + sizeof(int)
868a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
868b0 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 mem.nBacktrace
868c0 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b *sizeof(void*) +
868d0 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 70 mem.nTitle;. p
868e0 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 = malloc(totalS
868f0 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b ize);. if( p ){
86900 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20 . z = p;.
86910 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 7a pBt = (void**)&z
86920 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20 [mem.nTitle];.
86930 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 74 pHdr = (struct
86940 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 70 MemBlockHdr*)&p
86950 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 Bt[mem.nBacktrac
86960 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e e];. pHdr->pN
86970 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 64 ext = 0;. pHd
86980 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e 70 r->pPrev = mem.p
86990 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d 65 Last;. if( me
869a0 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 m.pLast ){.
869b0 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 78 mem.pLast->pNex
869c0 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 65 t = pHdr;. }e
869d0 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 lse{. mem.p
869e0 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 First = pHdr;.
869f0 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 }. mem.pLas
86a00 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 48 t = pHdr;. pH
86a10 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 3d dr->iForeGuard =
86a20 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 20 FOREGUARD;.
86a30 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
86a40 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 63 Slots = mem.nBac
86a50 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 72 ktrace;. pHdr
86a60 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e ->nTitle = mem.n
86a70 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 6d Title;. if( m
86a80 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b em.nBacktrace ){
86a90 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 64 . void *aAd
86aa0 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 48 dr[40];. pH
86ab0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d dr->nBacktrace =
86ac0 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 72 backtrace(aAddr
86ad0 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 , mem.nBacktrace
86ae0 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 6d +1)-1;. mem
86af0 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 5b cpy(pBt, &aAddr[
86b00 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 1], pHdr->nBackt
86b10 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 race*sizeof(void
86b20 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 *));. asser
86b30 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 20 t(pBt[0]);.
86b40 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72 if( mem.xBacktr
86b50 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ace ){. m
86b60 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42 em.xBacktrace(nB
86b70 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b yte, pHdr->nBack
86b80 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b trace-1, &aAddr[
86b90 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 1]);. }.
86ba0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48 }else{. pH
86bb0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d dr->nBacktrace =
86bc0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0;. }. if
86bd0 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a ( mem.nTitle ){.
86be0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 memcpy(z,
86bf0 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e mem.zTitle, mem.
86c00 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 nTitle);. }.
86c10 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d pHdr->iSize =
86c20 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75 nByte;. adju
86c30 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b stStats(nByte, +
86c40 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28 1);. pInt = (
86c50 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 int*)&pHdr[1];.
86c60 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 pInt[nReserve
86c70 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20 /sizeof(int)] =
86c80 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d REARGUARD;. m
86c90 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 emset(pInt, 0x65
86ca0 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 , nReserve);.
86cb0 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74 p = (void*)pInt
86cc0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
86cd0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e mutex_leave(mem.
86ce0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
86cf0 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 p; .}../*.** Fr
86d00 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 ee memory..*/.st
86d10 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
86d20 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 3MemFree(void *p
86d30 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74 Prior){. struct
86d40 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 MemBlockHdr *pH
86d50 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 dr;. void **pBt
86d60 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 ;. char *z;. a
86d70 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c ssert( sqlite3Gl
86d80 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
86d90 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 tat || mem.mutex
86da0 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 !=0 );. pHdr =
86db0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 sqlite3MemsysGet
86dc0 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a Header(pPrior);.
86dd0 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 pBt = (void**)
86de0 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 pHdr;. pBt -= p
86df0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 Hdr->nBacktraceS
86e00 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f lots;. sqlite3_
86e10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e mutex_enter(mem.
86e20 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48 mutex);. if( pH
86e30 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 dr->pPrev ){.
86e40 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 assert( pHdr->p
86e50 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 Prev->pNext==pHd
86e60 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 r );. pHdr->p
86e70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48 Prev->pNext = pH
86e80 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c dr->pNext;. }el
86e90 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
86ea0 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72 mem.pFirst==pHdr
86eb0 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72 );. mem.pFir
86ec0 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 st = pHdr->pNext
86ed0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72 ;. }. if( pHdr
86ee0 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 ->pNext ){. a
86ef0 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65 ssert( pHdr->pNe
86f00 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20 xt->pPrev==pHdr
86f10 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 );. pHdr->pNe
86f20 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72 xt->pPrev = pHdr
86f30 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 ->pPrev;. }else
86f40 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 {. assert( me
86f50 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b m.pLast==pHdr );
86f60 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d . mem.pLast =
86f70 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 pHdr->pPrev;.
86f80 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 }. z = (char*)p
86f90 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d Bt;. z -= pHdr-
86fa0 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73 >nTitle;. adjus
86fb0 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69 tStats(pHdr->iSi
86fc0 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 ze, -1);. memse
86fd0 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f t(z, 0x2b, sizeo
86fe0 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e f(void*)*pHdr->n
86ff0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b BacktraceSlots +
87000 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b sizeof(*pHdr) +
87010 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
87020 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b pHdr->iSize +
87030 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 sizeof(int) + p
87040 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 Hdr->nTitle);.
87050 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74 free(z);. sqlit
87060 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
87070 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a em.mutex); .}..
87080 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
87090 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 size of an exis
870a0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ting memory allo
870b0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f cation..**.** Fo
870c0 72 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67 r this debugging
870d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c implementation,
870e0 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b we *always* mak
870f0 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a e a copy of the.
87100 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e ** allocation in
87110 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69 to a new place i
87120 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68 n memory. In th
87130 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a is way, if the .
87140 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 ** higher level
87150 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f code is using po
87160 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64 inter to the old
87170 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 allocation, it
87180 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 is .** much more
87190 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b likely to break
871a0 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68 and we are much
871b0 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20 more liking to
871c0 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f find.** the erro
871d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
871e0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 d *sqlite3MemRea
871f0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f lloc(void *pPrio
87200 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 r, int nByte){.
87210 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
87220 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 Hdr *pOldHdr;.
87230 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73 void *pNew;. as
87240 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c sert( mem.disall
87250 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48 ow==0 );. pOldH
87260 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 dr = sqlite3Mems
87270 79 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69 ysGetHeader(pPri
87280 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 or);. pNew = sq
87290 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e lite3MemMalloc(n
872a0 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 Byte);. if( pNe
872b0 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 w ){. memcpy(
872c0 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 pNew, pPrior, nB
872d0 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 yte<pOldHdr->iSi
872e0 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c ze ? nByte : pOl
872f0 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 dHdr->iSize);.
87300 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 if( nByte>pOld
87310 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 Hdr->iSize ){.
87320 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 memset(&((ch
87330 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 ar*)pNew)[pOldHd
87340 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c r->iSize], 0x2b,
87350 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 nByte - pOldHdr
87360 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a ->iSize);. }.
87370 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 sqlite3MemFr
87380 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a ee(pPrior);. }.
87390 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
873a0 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 ../*.** Populate
873b0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
873c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
873d0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 function pointe
873e0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 rs in.** sqlite3
873f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 GlobalConfig.m w
87400 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ith pointers to
87410 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 the routines in
87420 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 this file..*/.SQ
87430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
87440 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 d sqlite3MemSetD
87450 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 efault(void){.
87460 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
87470 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
87480 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 defaultMethods
87490 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 = {. sqlite3
874a0 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 MemMalloc,.
874b0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a sqlite3MemFree,.
874c0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
874d0 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c ealloc,. sql
874e0 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 ite3MemSize,.
874f0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e sqlite3MemRoun
87500 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 dup,. sqlite
87510 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 3MemInit,. s
87520 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 qlite3MemShutdow
87530 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 n,. 0. };.
87540 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
87550 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 SQLITE_CONFIG_MA
87560 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 LLOC, &defaultMe
87570 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a thods);.}../*.**
87580 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 Set the number
87590 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 of backtrace lev
875a0 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 els kept for eac
875b0 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h allocation..**
875c0 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f A value of zero
875d0 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 turns off backt
875e0 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d racing. The num
875f0 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f ber is always ro
87600 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 unded.** up to a
87610 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a multiple of 2..
87620 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
87630 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
87640 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 mdebugBacktrace(
87650 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 int depth){. if
87660 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 ( depth<0 ){ dep
87670 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 th = 0; }. if(
87680 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 depth>20 ){ dept
87690 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 h = 20; }. dept
876a0 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 h = (depth+1)&0x
876b0 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 fe;. mem.nBackt
876c0 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a race = depth;.}.
876d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
876e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 void sqlite3Memd
876f0 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c ebugBacktraceCal
87700 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 lback(void (*xBa
87710 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e cktrace)(int, in
87720 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 t, void **)){.
87730 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d mem.xBacktrace =
87740 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a xBacktrace;.}..
87750 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 /*.** Set the ti
87760 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 tle string for s
87770 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 ubsequent alloca
87780 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tions..*/.SQLITE
87790 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
877a0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 lite3MemdebugSet
877b0 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 title(const char
877c0 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73 *zTitle){. uns
877d0 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71 igned int n = sq
877e0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 lite3Strlen30(zT
877f0 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c itle) + 1;. sql
87800 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
87810 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 (mem.mutex);. i
87820 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d f( n>=sizeof(mem
87830 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 .zTitle) ) n = s
87840 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 izeof(mem.zTitle
87850 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 )-1;. memcpy(me
87860 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 m.zTitle, zTitle
87870 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 , n);. mem.zTit
87880 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d le[n] = 0;. mem
87890 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38 .nTitle = ROUND8
878a0 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d (n);. sqlite3_m
878b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d utex_leave(mem.m
878c0 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 utex);.}..SQLITE
878d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
878e0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e lite3MemdebugSyn
878f0 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 c(){. struct Me
87900 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
87910 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e . for(pHdr=mem.
87920 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 pFirst; pHdr; pH
87930 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b dr=pHdr->pNext){
87940 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 . void **pBt
87950 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a = (void**)pHdr;.
87960 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d pBt -= pHdr-
87970 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
87980 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 ;. mem.xBackt
87990 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 race(pHdr->iSize
879a0 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 , pHdr->nBacktra
879b0 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a ce-1, &pBt[1]);.
879c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 }.}../*.** Ope
879d0 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 n the file indic
879e0 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 ated and write a
879f0 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 log of all unfr
87a00 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 eed memory .** a
87a10 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 llocations into
87a20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c that log..*/.SQL
87a30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87a40 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
87a50 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 Dump(const char
87a60 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 *zFilename){. F
87a70 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 ILE *out;. stru
87a80 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
87a90 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
87aa0 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f Bt;. int i;. o
87ab0 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 ut = fopen(zFile
87ac0 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 name, "w");. if
87ad0 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( out==0 ){.
87ae0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
87af0 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 "** Unable to ou
87b00 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 tput memory debu
87b10 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 g output log: %s
87b20 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 **\n",.
87b30 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c zFil
87b40 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 ename);. retu
87b50 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 rn;. }. for(pH
87b60 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 dr=mem.pFirst; p
87b70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e Hdr; pHdr=pHdr->
87b80 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 pNext){. char
87b90 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 *z = (char*)pHd
87ba0 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 r;. z -= pHdr
87bb0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
87bc0 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 s*sizeof(void*)
87bd0 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a + pHdr->nTitle;.
87be0 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
87bf0 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 "**** %lld byte
87c00 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 s at %p from %s
87c10 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 ****\n", .
87c20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a pHdr->iSiz
87c30 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 e, &pHdr[1], pHd
87c40 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 r->nTitle ? z :
87c50 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 "???");. if(
87c60 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
87c70 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 ){. fflush
87c80 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 (out);. pBt
87c90 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
87ca0 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 . pBt -= pH
87cb0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
87cc0 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 ots;. backt
87cd0 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 race_symbols_fd(
87ce0 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b pBt, pHdr->nBack
87cf0 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 trace, fileno(ou
87d00 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e t));. fprin
87d10 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 tf(out, "\n");.
87d20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e }. }. fprin
87d30 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a tf(out, "COUNTS:
87d40 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b \n");. for(i=0;
87d50 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b i<NCSIZE-1; i++
87d60 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e ){. if( mem.n
87d70 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 Alloc[i] ){.
87d80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
87d90 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 %5d: %10d %10
87da0 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 d %10d\n", .
87db0 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d i*8, mem
87dc0 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e .nAlloc[i], mem.
87dd0 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d nCurrent[i], mem
87de0 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a .mxCurrent[i]);.
87df0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
87e00 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a mem.nAlloc[NCSIZ
87e10 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 E-1] ){. fpri
87e20 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 ntf(out, " %5d
87e30 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 : %10d %10d %10d
87e40 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
87e50 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 NCSIZE*8-8, me
87e60 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d m.nAlloc[NCSIZE-
87e70 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 1],.
87e80 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 mem.nCurrent[NC
87e90 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 SIZE-1], mem.mxC
87ea0 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d urrent[NCSIZE-1]
87eb0 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 );. }. fclose(
87ec0 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 out);.}../*.** R
87ed0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
87ee0 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 of times sqlite
87ef0 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 3MemMalloc() has
87f00 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f been called..*/
87f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87f20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 int sqlite3Memde
87f30 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 bugMallocCount()
87f40 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
87f50 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 nTotal = 0;. f
87f60 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 or(i=0; i<NCSIZE
87f70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 ; i++){. nTot
87f80 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 al += mem.nAlloc
87f90 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 [i];. }. retur
87fa0 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 n nTotal;.}...#e
87fb0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
87fc0 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a EMDEBUG */../***
87fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
87fe0 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a of mem2.c ******
87ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
88020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
88030 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a n file mem3.c **
88040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
88070 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 * 2007 October 1
88080 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
88090 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
880a0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
880b0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
880c0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
880d0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
880e0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
880f0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
88100 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
88110 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
88120 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
88130 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
88140 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
88150 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
88160 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
88170 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
88180 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
88190 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
881a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
881b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
881c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
881d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
881e0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
881f0 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
88200 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
88210 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a ement a memory.*
88220 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 * allocation sub
88230 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 system for use b
88240 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a y SQLite. .**.**
88250 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
88260 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
88270 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
88280 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 omits all.** us
88290 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 e of malloc(). T
882a0 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 he SQLite user s
882b0 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 upplies a block
882c0 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 of memory.** bef
882d0 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 ore calling sqli
882e0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
882f0 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f from which allo
88300 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d cations.** are m
88310 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 ade and returned
88320 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 by the xMalloc(
88330 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 ) and xRealloc()
88340 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 .** implementat
88350 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 ions. Once sqlit
88360 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
88370 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c has been called,
88380 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f .** the amount o
88390 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 f memory availab
883a0 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 le to SQLite is
883b0 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 fixed and cannot
883c0 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a .** be changed..
883d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 **.** This versi
883e0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
883f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
88400 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 ystem is include
88410 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c d.** in the buil
88420 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 d only if SQLITE
88430 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 _ENABLE_MEMSYS3
88440 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a is defined..*/..
88450 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
88460 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
88470 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e allocator is on
88480 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 ly built into th
88490 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c e library.** SQL
884a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
884b0 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 S3 is defined. D
884c0 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d efining this sym
884d0 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 bol does not.**
884e0 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 mean that the li
884f0 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 brary will use a
88500 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 memory-pool by
88510 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 default, just th
88520 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 at.** it is avai
88530 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f lable. The mempo
88540 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ol allocator is
88550 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c activated by cal
88560 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ling.** sqlite3_
88570 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 config()..*/.#if
88580 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
88590 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a E_MEMSYS3../*.**
885a0 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 Maximum size (i
885b0 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 n Mem3Blocks) of
885c0 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b a "small" chunk
885d0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
885e0 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a SMALL 10.../*.**
885f0 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
88600 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a ist hash slots.*
88610 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 /.#define N_HASH
88620 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 61../*.** A me
88630 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
88640 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 (also called a "
88650 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 chunk") consists
88660 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d of two or .** m
88670 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 ore blocks where
88680 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 each block is 8
88690 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 bytes. The fir
886a0 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a st 8 bytes are .
886b0 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74 ** a header that
886c0 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 is not returned
886d0 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a to the user..**
886e0 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 .** A chunk is t
886f0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b wo or more block
88700 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72 s that is either
88710 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a checked out or.
88720 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 ** free. The fi
88730 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f rst block has fo
88740 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 rmat u.hdr. u.h
88750 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 dr.size4x is 4 t
88760 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 imes the.** size
88770 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
88780 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 on in blocks if
88790 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
887a0 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 s free..** The u
887b0 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 .hdr.size4x&1 bi
887c0 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 t is true if the
887d0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
887e0 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c d out and.** fal
887f0 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 se if the chunk
88800 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 is on the freeli
88810 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 st. The u.hdr.s
88820 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 ize4x&2 bit.** i
88830 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 s true if the pr
88840 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
88850 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 checked out and
88860 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 false if the.**
88870 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
88880 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 s free. The u.h
88890 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c dr.prevSize fiel
888a0 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
888b0 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 .** the previous
888c0 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 chunk in blocks
888d0 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
888e0 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 chunk is on the
888f0 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 .** freelist. If
88900 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 the previous ch
88910 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f unk is checked o
88920 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 ut, then.** u.hd
88930 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 r.prevSize can b
88940 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 e part of the da
88950 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e ta for that chun
88960 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 k and should.**
88970 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 not be read or w
88980 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 ritten..**.** We
88990 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 often identify
889a0 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 a chunk by its i
889b0 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f ndex in mem3.aPo
889c0 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 ol[]. When.** t
889d0 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 his is done, the
889e0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 chunk index ref
889f0 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e ers to the secon
88a00 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 d block of.** th
88a10 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 e chunk. In thi
88a20 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 s way, the first
88a30 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e chunk has an in
88a40 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 dex of 1..** A c
88a50 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 hunk index of 0
88a60 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 means "no such c
88a70 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 hunk" and is the
88a80 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f equivalent.** o
88a90 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
88aa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
88ab0 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 nd block of free
88ac0 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 chunks is of th
88ad0 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 e form u.list.
88ae0 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 The.** two field
88af0 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d s form a double-
88b00 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 linked list of c
88b10 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 hunks of related
88b20 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 sizes..** Point
88b30 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 ers to the head
88b40 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 of the list are
88b50 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 stored in mem3.a
88b60 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 iSmall[] .** for
88b70 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 smaller chunks
88b80 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b and mem3.aiHash[
88b90 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 ] for larger chu
88ba0 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nks..**.** The s
88bb0 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 econd block of a
88bc0 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 chunk is user d
88bd0 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b ata if the chunk
88be0 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 is checked .**
88bf0 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b out. If a chunk
88c00 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c is checked out,
88c10 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d the user data m
88c20 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a ay extend into.*
88c30 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 * the u.hdr.prev
88c40 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 Size value of th
88c50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e e following chun
88c60 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 k..*/.typedef st
88c70 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d ruct Mem3Block M
88c80 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 em3Block;.struct
88c90 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 Mem3Block {. u
88ca0 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 nion {. struc
88cb0 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 t {. u32 pr
88cc0 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a evSize; /* Siz
88cd0 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 e of previous ch
88ce0 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
88cf0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
88d00 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 u32 size4x;
88d10 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a /* 4x the siz
88d20 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 e of current chu
88d30 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 nk in Mem3Block
88d40 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 elements */.
88d50 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 } hdr;. struc
88d60 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 t {. u32 ne
88d70 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
88d80 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
88d90 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 [] of next free
88da0 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 chunk */. u
88db0 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 32 prev; /
88dc0 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e * Index in mem3.
88dd0 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 aPool[] of previ
88de0 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ous free chunk *
88df0 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 /. } list;.
88e00 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 } u;.};../*.** A
88e10 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
88e20 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
88e30 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
88e40 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
88e50 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
88e60 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
88e70 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 em3". This is t
88e80 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
88e90 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
88ea0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
88eb0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
88ec0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
88ed0 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
88ee0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
88ef0 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
88f00 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
88f10 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
88f20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
88f30 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
88f40 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
88f50 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e or allocation. n
88f60 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 Pool is the size
88f70 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 of the array.
88f80 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b ** (in Mem3Block
88f90 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 s) pointed to by
88fa0 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 aPool less 2..
88fb0 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b */. u32 nPool;
88fc0 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 . Mem3Block *aP
88fd0 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ool;.. /*. **
88fe0 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65 True if we are e
88ff0 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 valuating an out
89000 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 -of-memory callb
89010 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ack.. */. int
89020 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 alarmBusy;. .
89030 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f /*. ** Mutex to
89040 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
89050 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
89060 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
89070 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 em.. */. sqlit
89080 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
89090 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 . . /*. ** Th
890a0 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 e minimum amount
890b0 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74 of free space t
890c0 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e hat we have seen
890d0 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d .. */. u32 mnM
890e0 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a aster;.. /*. *
890f0 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 * iMaster is the
89100 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 index of the ma
89110 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 ster chunk. Mos
89120 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e t new allocation
89130 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 s. ** occur off
89140 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 of this chunk.
89150 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 szMaster is the
89160 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c size (in Mem3Bl
89170 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 ocks). ** of th
89180 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 e current master
89190 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 . iMaster is 0
891a0 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 if there is not
891b0 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 master chunk..
891c0 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 ** The master ch
891d0 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 unk is not in ei
891e0 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b ther the aiHash[
891f0 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a ] or aiSmall[]..
89200 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 */. u32 iMast
89210 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 er;. u32 szMast
89220 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 er;.. /*. ** A
89230 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 rray of lists of
89240 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 free blocks acc
89250 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c ording to the bl
89260 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 ock size . ** f
89270 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b or smaller chunk
89280 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 s, or a hash on
89290 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 the block size f
892a0 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 or larger. ** c
892b0 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 hunks.. */. u3
892c0 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 2 aiSmall[MX_SMA
892d0 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 LL-1]; /* For
892e0 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 sizes 2 through
892f0 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 MX_SMALL, inclus
89300 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 ive */. u32 aiH
89310 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 ash[N_HASH];
89320 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 /* For sizes
89330 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 MX_SMALL+1 and
89340 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 larger */.} mem3
89350 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b = { 97535575 };
89360 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 ..#define mem3 G
89370 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d LOBAL(struct Mem
89380 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 3Global, mem3)..
89390 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 /*.** Unlink the
893a0 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 chunk at mem3.a
893b0 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 Pool[i] from lis
893c0 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c t it is currentl
893d0 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 y.** on. *pRoot
893e0 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 is the list tha
893f0 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 t i is a member
89400 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
89410 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
89420 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 FromList(u32 i,
89430 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 u32 *pRoot){. u
89440 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 32 next = mem3.a
89450 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
89460 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 ext;. u32 prev
89470 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e = mem3.aPool[i].
89480 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 u.list.prev;. a
89490 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
894a0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
894b0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
894c0 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 rev==0 ){. *p
894d0 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d Root = next;. }
894e0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 else{. mem3.a
894f0 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
89500 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 t.next = next;.
89510 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b }. if( next ){
89520 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
89530 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 next].u.list.pre
89540 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 v = prev;. }.
89550 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
89560 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 list.next = 0;.
89570 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
89580 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a .list.prev = 0;.
89590 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 }../*.** Unlink
895a0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
895b0 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 ex i from .** wh
895c0 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 atever list is c
895d0 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 urrently a membe
895e0 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 r of..*/.static
895f0 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 void memsys3Unli
89600 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 nk(u32 i){. u32
89610 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 size, hash;. a
89620 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
89630 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
89640 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
89650 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
89660 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89670 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 & 1)==0 );. as
89680 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
89690 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
896a0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
896b0 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
896c0 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f size==mem3.aPoo
896d0 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
896e0 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 r.prevSize );.
896f0 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 assert( size>=2
89700 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d );. if( size <=
89710 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
89720 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
89730 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
89740 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
89750 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
89760 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
89770 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
89780 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
89790 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
897a0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
897b0 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 .** Link the chu
897c0 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c nk at mem3.aPool
897d0 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f [i] so that is o
897e0 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 n the list roote
897f0 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a d.** at *pRoot..
89800 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
89810 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
89820 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
89830 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 Root){. assert(
89840 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89850 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
89860 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b );. mem3.aPool[
89870 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d i].u.list.next =
89880 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e *pRoot;. mem3.
89890 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
898a0 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 prev = 0;. if(
898b0 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 *pRoot ){. me
898c0 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d m3.aPool[*pRoot]
898d0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 .u.list.prev = i
898e0 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d ;. }. *pRoot =
898f0 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e i;.}../*.** Lin
89900 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 k the chunk at i
89910 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 ndex i into eith
89920 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 er the appropria
89930 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e te.** small chun
89940 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 k list, or into
89950 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
89960 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 hash table..*/.s
89970 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
89980 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 s3Link(u32 i){.
89990 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b u32 size, hash;
899a0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
899b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
899c0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
899d0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
899e0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
899f0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
89a00 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 size4x & 1)==0 )
89a10 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e ;. size = mem3.
89a20 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
89a30 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 .size4x/4;. ass
89a40 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e ert( size==mem3.
89a50 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
89a60 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 u.hdr.prevSize )
89a70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
89a80 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a >=2 );. if( siz
89a90 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b e <= MX_SMALL ){
89aa0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
89ab0 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
89ac0 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 3.aiSmall[size-2
89ad0 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
89ae0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e hash = size % N
89af0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 _HASH;. memsy
89b00 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 s3LinkIntoList(i
89b10 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
89b20 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
89b30 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 .** If the STATI
89b40 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e C_MEM mutex is n
89b50 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c ot already held,
89b60 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 obtain it now.
89b70 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c The mutex.** wil
89b80 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c l already be hel
89b90 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 d (obtained by c
89ba0 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 ode in malloc.c)
89bb0 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c if.** sqlite3Gl
89bc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 obalConfig.bMemS
89bd0 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a tat is true..*/.
89be0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
89bf0 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a ys3Enter(void){.
89c00 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
89c10 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
89c20 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 at==0 && mem3.mu
89c30 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 tex==0 ){. me
89c40 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 m3.mutex = sqlit
89c50 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
89c60 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
89c70 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c _MEM);. }. sql
89c80 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
89c90 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem3.mutex);.}.
89ca0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
89cb0 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a ys3Leave(void){.
89cc0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
89cd0 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 leave(mem3.mutex
89ce0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c );.}../*.** Call
89cf0 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 ed when we are u
89d00 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 nable to satisfy
89d10 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
89d20 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 f nBytes..*/.sta
89d30 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
89d40 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 OutOfMemory(int
89d50 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d nByte){. if( !m
89d60 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b em3.alarmBusy ){
89d70 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 . mem3.alarmB
89d80 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 usy = 1;. ass
89d90 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
89da0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
89db0 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ex) );. sqlit
89dc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
89dd0 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em3.mutex);.
89de0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
89df0 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 memory(nByte);.
89e00 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
89e10 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 _enter(mem3.mute
89e20 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 x);. mem3.ala
89e30 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a rmBusy = 0;. }.
89e40 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 }.../*.** Chunk
89e50 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e i is a free chun
89e60 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 k that has been
89e70 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 unlinked. Adjus
89e80 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 t its .** size p
89e90 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 arameters for ch
89ea0 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 eck-out and retu
89eb0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
89ec0 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 the .** user por
89ed0 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e tion of the chun
89ee0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
89ef0 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f d *memsys3Checko
89f00 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 ut(u32 i, u32 nB
89f10 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a lock){. u32 x;.
89f20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
89f30 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
89f40 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
89f50 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
89f60 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f assert( mem3.aPo
89f70 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
89f80 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 ze4x/4==nBlock )
89f90 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 ;. assert( mem3
89fa0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
89fb0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
89fc0 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 e==nBlock );. x
89fd0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
89fe0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
89ff0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d . mem3.aPool[i-
8a000 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a010 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c = nBlock*4 | 1 |
8a020 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 (x&2);. mem3.a
8a030 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
8a040 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
8a050 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 = nBlock;. mem3
8a060 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
8a070 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a080 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 |= 2;. return &
8a090 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d mem3.aPool[i];.}
8a0a0 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 ../*.** Carve a
8a0b0 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 piece off of the
8a0c0 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 end of the mem3
8a0d0 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 .iMaster free ch
8a0e0 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 unk..** Return a
8a0f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
8a100 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
8a110 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 Or, if the mast
8a120 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e er chunk.** is n
8a130 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c ot large enough,
8a140 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 return 0..*/.st
8a150 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 atic void *memsy
8a160 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 s3FromMaster(u32
8a170 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 nBlock){. asse
8a180 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
8a190 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
8a1a0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
8a1b0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e mem3.szMaster>=n
8a1c0 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e Block );. if( n
8a1d0 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 Block>=mem3.szMa
8a1e0 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a ster-1 ){. /*
8a1f0 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 Use the entire
8a200 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f master */. vo
8a210 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 id *p = memsys3C
8a220 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 heckout(mem3.iMa
8a230 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 ster, mem3.szMas
8a240 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 ter);. mem3.i
8a250 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
8a260 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 mem3.szMaster =
8a270 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 0;. mem3.mnMa
8a280 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 ster = 0;. re
8a290 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b turn p;. }else{
8a2a0 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 . /* Split th
8a2b0 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 e master block.
8a2c0 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c Return the tail
8a2d0 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 . */. u32 new
8a2e0 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d i, x;. newi =
8a2f0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 mem3.iMaster +
8a300 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 mem3.szMaster -
8a310 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 nBlock;. asse
8a320 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e rt( newi > mem3.
8a330 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 iMaster+1 );.
8a340 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8a350 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8a360 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8a370 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 prevSize = nBloc
8a380 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f k;. mem3.aPoo
8a390 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d l[mem3.iMaster+m
8a3a0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e em3.szMaster-1].
8a3b0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 u.hdr.size4x |=
8a3c0 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 2;. mem3.aPoo
8a3d0 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e l[newi-1].u.hdr.
8a3e0 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a size4x = nBlock*
8a3f0 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 4 + 1;. mem3.
8a400 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f szMaster -= nBlo
8a410 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
8a420 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
8a430 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 .prevSize = mem3
8a440 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 .szMaster;. x
8a450 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 = mem3.aPool[me
8a460 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
8a470 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a hdr.size4x & 2;.
8a480 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8a490 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8a4a0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
8a4b0 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
8a4c0 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e x;. if( mem3.
8a4d0 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e szMaster < mem3.
8a4e0 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 mnMaster ){.
8a4f0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 mem3.mnMaster
8a500 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
8a510 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
8a520 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 n (void*)&mem3.a
8a530 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a Pool[newi];. }.
8a540 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 }../*.** *pRoot
8a550 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 is the head of a
8a560 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 list of free ch
8a570 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 unks of the same
8a580 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 size.** or same
8a590 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 size hash. In
8a5a0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 other words, *pR
8a5b0 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 oot is an entry
8a5c0 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d in either.** mem
8a5d0 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 3.aiSmall[] or m
8a5e0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a em3.aiHash[]. .
8a5f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
8a600 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 ne examines all
8a610 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 entries on the g
8a620 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 iven list and tr
8a630 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 ies.** to coales
8a640 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 ce each entries
8a650 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 with adjacent fr
8a660 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a ee chunks. .**.
8a670 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20 ** If it sees a
8a680 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 chunk that is la
8a690 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 rger than mem3.i
8a6a0 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 Master, it repla
8a6b0 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 ces .** the curr
8a6c0 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ent mem3.iMaster
8a6d0 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 with the new la
8a6e0 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 rger chunk. In
8a6f0 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 order for.** thi
8a700 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 s mem3.iMaster r
8a710 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f eplacement to wo
8a720 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 rk, the master c
8a730 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 hunk must be.**
8a740 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 linked into the
8a750 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 hash tables. Th
8a760 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f at is not the no
8a770 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a rmal state of.**
8a780 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 affairs, of cou
8a790 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e rse. The callin
8a7a0 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c g routine must l
8a7b0 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ink the master.*
8a7c0 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 * chunk before i
8a7d0 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
8a7e0 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 tine, then must
8a7f0 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 unlink the (poss
8a800 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 ibly.** changed)
8a810 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e master chunk on
8a820 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ce this routine
8a830 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f has finished..*/
8a840 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8a850 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 sys3Merge(u32 *p
8a860 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 Root){. u32 iNe
8a870 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 xt, prev, size,
8a880 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 i, x;.. assert(
8a890 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8a8a0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
8a8b0 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f );. for(i=*pRoo
8a8c0 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 t; i>0; i=iNext)
8a8d0 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 {. iNext = me
8a8e0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
8a8f0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a st.next;. siz
8a900 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 e = mem3.aPool[i
8a910 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8a920 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73 ;. assert( (s
8a930 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 ize&1)==0 );.
8a940 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 if( (size&2)==0
8a950 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
8a960 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
8a970 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 i, pRoot);.
8a980 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d assert( i > mem
8a990 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8a9a0 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
8a9b0 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 prev = i -
8a9c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
8a9d0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a u.hdr.prevSize;.
8a9e0 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d if( prev==
8a9f0 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 iNext ){.
8aa00 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 iNext = mem3.aP
8aa10 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 ool[prev].u.list
8aa20 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 .next;. }.
8aa30 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8aa40 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 nk(prev);.
8aa50 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f size = i + size/
8aa60 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 4 - prev;.
8aa70 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 x = mem3.aPool[p
8aa80 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a rev-1].u.hdr.siz
8aa90 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
8aaa0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 em3.aPool[prev-1
8aab0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
8aac0 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 size*4 | x;.
8aad0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 mem3.aPool[pr
8aae0 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 ev+size-1].u.hdr
8aaf0 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 .prevSize = size
8ab00 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c ;. memsys3L
8ab10 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
8ab20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d i = prev;. }
8ab30 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 else{. size
8ab40 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 /= 4;. }.
8ab50 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 if( size>mem3.s
8ab60 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
8ab70 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
8ab80 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a i;. mem3.sz
8ab90 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 Master = size;.
8aba0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
8abb0 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b * Return a block
8abc0 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 of memory of at
8abd0 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e least nBytes in
8abe0 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e size..** Return
8abf0 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e NULL if unable.
8ac00 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
8ac10 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
8ac20 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
8ac30 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
8ac40 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
8ac50 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
8ac60 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
8ac70 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e"..*/.static vo
8ac80 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f id *memsys3Mallo
8ac90 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 cUnsafe(int nByt
8aca0 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 e){. u32 i;. u
8acb0 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 32 nBlock;. u32
8acc0 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 toFree;.. asse
8acd0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
8ace0 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
8acf0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
8ad00 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b sizeof(Mem3Block
8ad10 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 )==8 );. if( nB
8ad20 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e yte<=12 ){. n
8ad30 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c Block = 2;. }el
8ad40 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d se{. nBlock =
8ad50 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b (nByte + 11)/8;
8ad60 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
8ad70 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f Block>=2 );.. /
8ad80 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c * STEP 1:. ** L
8ad90 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 ook for an entry
8ada0 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 of the correct
8adb0 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 size in either t
8adc0 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 he small. ** ch
8add0 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 unk table or in
8ade0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
8adf0 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 hash table. Thi
8ae00 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 s is. ** succes
8ae10 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 sful most of the
8ae20 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 time (about 9 t
8ae30 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e imes out of 10).
8ae40 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f . */. if( nBlo
8ae50 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ck <= MX_SMALL )
8ae60 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 {. i = mem3.a
8ae70 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d iSmall[nBlock-2]
8ae80 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b ;. if( i>0 ){
8ae90 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
8aea0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
8aeb0 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 &mem3.aiSmall[nB
8aec0 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 lock-2]);.
8aed0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 return memsys3Ch
8aee0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b eckout(i, nBlock
8aef0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
8af00 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d {. int hash =
8af10 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 nBlock % N_HASH
8af20 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 ;. for(i=mem3
8af30 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 .aiHash[hash]; i
8af40 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; i=mem3.aPool
8af50 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [i].u.list.next)
8af60 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 {. if( mem3
8af70 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
8af80 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f r.size4x/4==nBlo
8af90 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ck ){. me
8afa0 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c msys3UnlinkFromL
8afb0 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 ist(i, &mem3.aiH
8afc0 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 ash[hash]);.
8afd0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
8afe0 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 s3Checkout(i, nB
8aff0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 lock);. }.
8b000 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
8b010 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 TEP 2:. ** Try
8b020 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 to satisfy the a
8b030 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 llocation by car
8b040 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 ving a piece off
8b050 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a of the end. **
8b060 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
8b070 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 hunk. This step
8b080 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 usually works i
8b090 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a f step 1 fails..
8b0a0 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e */. if( mem3.
8b0b0 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b szMaster>=nBlock
8b0c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
8b0d0 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 emsys3FromMaster
8b0e0 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a (nBlock);. }...
8b0f0 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 /* STEP 3: .
8b100 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 ** Loop through
8b110 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f the entire memo
8b120 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 ry pool. Coales
8b130 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 ce adjacent free
8b140 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 . ** chunks. R
8b150 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 ecompute the mas
8b160 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 ter chunk as the
8b170 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68 largest free ch
8b180 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 unk.. ** Then t
8b190 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 ry again to sati
8b1a0 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 sfy the allocati
8b1b0 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 on by carving a
8b1c0 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f piece off. ** o
8b1d0 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
8b1e0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 master chunk.
8b1f0 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e This step happen
8b200 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 s very. ** rare
8b210 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 ly (we hope!).
8b220 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d */. for(toFree=
8b230 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 nBlock*16; toFre
8b240 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 e<(mem3.nPool*16
8b250 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b ); toFree *= 2){
8b260 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f . memsys3OutO
8b270 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b fMemory(toFree);
8b280 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d . if( mem3.iM
8b290 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
8b2a0 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e emsys3Link(mem3.
8b2b0 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 iMaster);.
8b2c0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 mem3.iMaster = 0
8b2d0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
8b2e0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d aster = 0;. }
8b2f0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
8b300 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
8b310 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 memsys3Merge
8b320 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d (&mem3.aiHash[i]
8b330 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
8b340 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c (i=0; i<MX_SMALL
8b350 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 -1; i++){.
8b360 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 memsys3Merge(&me
8b370 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a m3.aiSmall[i]);.
8b380 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
8b390 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 m3.szMaster ){.
8b3a0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8b3b0 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
8b3c0 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 ;. if( mem3
8b3d0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
8b3e0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 k ){. ret
8b3f0 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d urn memsys3FromM
8b400 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 aster(nBlock);.
8b410 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
8b420 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f .. /* If none o
8b430 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b f the above work
8b440 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c ed, then we fail
8b450 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b . */. return 0;
8b460 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
8b470 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 n outstanding me
8b480 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
8b490 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
8b4a0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
8b4b0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
8b4c0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
8b4d0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
8b4e0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
8b4f0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
8b500 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 e"..*/.void mems
8b510 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f ys3FreeUnsafe(vo
8b520 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d id *pOld){. Mem
8b530 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 3Block *p = (Mem
8b540 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 3Block*)pOld;.
8b550 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a int i;. u32 siz
8b560 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 e, x;. assert(
8b570 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
8b580 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
8b590 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 ;. assert( p>me
8b5a0 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d m3.aPool && p<&m
8b5b0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e em3.aPool[mem3.n
8b5c0 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 Pool] );. i = p
8b5d0 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 - mem3.aPool;.
8b5e0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
8b5f0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8b600 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a size4x&1)==1 );.
8b610 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 size = mem3.aP
8b620 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
8b630 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 ize4x/4;. asser
8b640 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e t( i+size<=mem3.
8b650 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d nPool+1 );. mem
8b660 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8b670 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b dr.size4x &= ~1;
8b680 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b . mem3.aPool[i+
8b690 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 size-1].u.hdr.pr
8b6a0 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 evSize = size;.
8b6b0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 mem3.aPool[i+si
8b6c0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 ze-1].u.hdr.size
8b6d0 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 4x &= ~2;. mems
8b6e0 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f ys3Link(i);.. /
8b6f0 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 * Try to expand
8b700 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 the master using
8b710 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 the newly freed
8b720 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 chunk */. if(
8b730 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a mem3.iMaster ){.
8b740 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 while( (mem3
8b750 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8b760 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8b770 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 e4x&2)==0 ){.
8b780 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 size = mem3.a
8b790 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b7a0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
8b7b0 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e ize;. mem3.
8b7c0 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b iMaster -= size;
8b7d0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 . mem3.szMa
8b7e0 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 ster += size;.
8b7f0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8b800 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b k(mem3.iMaster);
8b810 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e . x = mem3.
8b820 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8b830 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8b840 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 4x & 2;. me
8b850 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8b860 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8b870 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
8b880 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
8b890 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
8b8a0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
8b8b0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
8b8c0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d r.prevSize = mem
8b8d0 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
8b8e0 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 }. x = mem3.a
8b8f0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b900 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8b910 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 x & 2;. while
8b920 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 ( (mem3.aPool[me
8b930 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
8b940 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
8b950 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 r.size4x&1)==0 )
8b960 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
8b970 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 nlink(mem3.iMast
8b980 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
8b990 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a );. mem3.sz
8b9a0 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 Master += mem3.a
8b9b0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b9c0 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
8b9d0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
8b9e0 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 4;. mem3.aP
8b9f0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8ba00 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8ba10 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
8ba20 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 *4 | x;. me
8ba30 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8ba40 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 aster+mem3.szMas
8ba50 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ter-1].u.hdr.pre
8ba60 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d vSize = mem3.szM
8ba70 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d aster;. }. }
8ba80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
8ba90 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 the size of an
8baa0 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f outstanding allo
8bab0 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 cation, in bytes
8bac0 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 . The.** size r
8bad0 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 eturned omits th
8bae0 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 8-byte header
8baf0 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 overhead. This
8bb00 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f only.** works fo
8bb10 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 r chunks that ar
8bb20 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 e currently chec
8bb30 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 ked out..*/.stat
8bb40 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 ic int memsys3Si
8bb50 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d ze(void *p){. M
8bb60 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b em3Block *pBlock
8bb70 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
8bb80 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 eturn 0;. pBloc
8bb90 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 k = (Mem3Block*)
8bba0 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 p;. assert( (pB
8bbb0 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 lock[-1].u.hdr.s
8bbc0 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 ize4x&1)!=0 );.
8bbd0 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b return (pBlock[
8bbe0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8bbf0 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f &~3)*2 - 4;.}../
8bc00 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
8bc10 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
8bc20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
8bc30 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
8bc40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
8bc50 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 msys3Roundup(int
8bc60 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 n){. if( n<=12
8bc70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
8bc80 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2;. }else{.
8bc90 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e return ((n+11)&~
8bca0 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 7) - 4;. }.}../
8bcb0 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
8bcc0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a ytes of memory..
8bcd0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
8bce0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e memsys3Malloc(in
8bcf0 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c t nBytes){. sql
8bd00 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 ite3_int64 *p;.
8bd10 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e assert( nBytes>
8bd20 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 0 ); /*
8bd30 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 malloc.c filter
8bd40 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 s out 0 byte req
8bd50 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 uests */. memsy
8bd60 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d s3Enter();. p =
8bd70 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e memsys3MallocUn
8bd80 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 safe(nBytes);.
8bd90 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
8bda0 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
8bdb0 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p; .}../*.** Fre
8bdc0 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 e memory..*/.voi
8bdd0 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f d memsys3Free(vo
8bde0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 id *pPrior){. a
8bdf0 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b ssert( pPrior );
8be00 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 . memsys3Enter(
8be10 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 );. memsys3Free
8be20 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
8be30 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 memsys3Leave()
8be40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
8be50 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
8be60 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
8be70 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 allocation.*/.v
8be80 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c oid *memsys3Real
8be90 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
8bea0 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 , int nBytes){.
8beb0 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 int nOld;. voi
8bec0 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 d *p;. if( pPri
8bed0 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 or==0 ){. ret
8bee0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c urn sqlite3_mall
8bef0 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a oc(nBytes);. }.
8bf00 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 if( nBytes<=0
8bf10 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
8bf20 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 ree(pPrior);.
8bf30 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
8bf40 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 nOld = memsys3S
8bf50 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 ize(pPrior);. i
8bf60 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 f( nBytes<=nOld
8bf70 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d && nBytes>=nOld-
8bf80 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 128 ){. retur
8bf90 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 n pPrior;. }.
8bfa0 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a memsys3Enter();.
8bfb0 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c p = memsys3Mal
8bfc0 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 locUnsafe(nBytes
8bfd0 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
8bfe0 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 if( nOld<nByte
8bff0 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 s ){. memcp
8c000 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c y(p, pPrior, nOl
8c010 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
8c020 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
8c030 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a Prior, nBytes);.
8c040 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 }. memsys
8c050 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 3FreeUnsafe(pPri
8c060 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 or);. }. memsy
8c070 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 s3Leave();. ret
8c080 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
8c090 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 Initialize this
8c0a0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
8c0b0 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 c int memsys3Ini
8c0c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
8c0d0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
8c0e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
8c0f0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
8c100 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 balConfig.pHeap
8c110 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
8c120 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
8c130 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f . /* Store a po
8c140 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d inter to the mem
8c150 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f ory block in glo
8c160 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65 bal structure me
8c170 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 m3. */. assert(
8c180 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
8c190 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e k)==8 );. mem3.
8c1a0 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f aPool = (Mem3Blo
8c1b0 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 ck *)sqlite3Glob
8c1c0 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a alConfig.pHeap;.
8c1d0 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 mem3.nPool = (
8c1e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8c1f0 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65 fig.nHeap / size
8c200 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d of(Mem3Block)) -
8c210 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 2;.. /* Initia
8c220 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20 lize the master
8c230 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 block. */. mem3
8c240 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .szMaster = mem3
8c250 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d .nPool;. mem3.m
8c260 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73 nMaster = mem3.s
8c270 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e zMaster;. mem3.
8c280 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d iMaster = 1;. m
8c290 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 em3.aPool[0].u.h
8c2a0 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d dr.size4x = (mem
8c2b0 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b 3.szMaster<<2) +
8c2c0 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 2;. mem3.aPool
8c2d0 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
8c2e0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
8c2f0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 m3.nPool;. mem3
8c300 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f .aPool[mem3.nPoo
8c310 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 l].u.hdr.size4x
8c320 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 = 1;.. return S
8c330 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
8c340 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 ** Deinitialize
8c350 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
8c360 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8c370 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 ys3Shutdown(void
8c380 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
8c390 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
8c3a0 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33 2e otUsed);. mem3.
8c3b0 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 74 mutex = 0;. ret
8c3c0 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 urn;.}..../*.**
8c3d0 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e Open the file in
8c3e0 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 dicated and writ
8c3f0 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 e a log of all u
8c400 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a nfreed memory .*
8c410 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e * allocations in
8c420 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a to that log..*/.
8c430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8c440 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 oid sqlite3Memsy
8c450 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 s3Dump(const cha
8c460 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 r *zFilename){.#
8c470 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
8c480 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a UG. FILE *out;.
8c490 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 u32 i, j;. u3
8c4a0 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 2 size;. if( zF
8c4b0 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 ilename==0 || zF
8c4c0 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b ilename[0]==0 ){
8c4d0 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 . out = stdou
8c4e0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
8c4f0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c out = fopen(zFil
8c500 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 ename, "w");.
8c510 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 if( out==0 ){.
8c520 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
8c530 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 err, "** Unable
8c540 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 to output memory
8c550 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f debug output lo
8c560 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 g: %s **\n",.
8c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8c580 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 zFilename);.
8c590 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
8c5a0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 }. }. memsys3
8c5b0 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e Enter();. fprin
8c5c0 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a tf(out, "CHUNKS:
8c5d0 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b \n");. for(i=1;
8c5e0 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 i<=mem3.nPool;
8c5f0 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 i+=size/4){.
8c600 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
8c610 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8c620 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a e4x;. if( siz
8c630 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 e/4<=1 ){.
8c640 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
8c650 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 size error\n",
8c660 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b &mem3.aPool[i]);
8c670 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 . assert( 0
8c680 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
8c690 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 . }. if( (
8c6a0 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 size&1)==0 && me
8c6b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f m3.aPool[i+size/
8c6c0 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 4-1].u.hdr.prevS
8c6d0 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 ize!=size/4 ){.
8c6e0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
8c6f0 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 , "%p tail size
8c700 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e does not match\n
8c710 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ", &mem3.aPool[i
8c720 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ]);. assert
8c730 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ( 0 );. bre
8c740 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ak;. }. if
8c750 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ( ((mem3.aPool[i
8c760 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 +size/4-1].u.hdr
8c770 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d .size4x&2)>>1)!=
8c780 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 (size&1) ){.
8c790 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8c7a0 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 %p tail checkout
8c7b0 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 bit is incorrec
8c7c0 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f t\n", &mem3.aPoo
8c7d0 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 l[i]);. ass
8c7e0 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 ert( 0 );.
8c7f0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
8c800 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 if( size&1 ){.
8c810 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
8c820 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20 , "%p %6d bytes
8c830 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 checked out\n",
8c840 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 &mem3.aPool[i],
8c850 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 (size/4)*8-8);.
8c860 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
8c870 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 fprintf(out, "%p
8c880 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 25 %6d bytes free%
8c890 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f s\n", &mem3.aPoo
8c8a0 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 l[i], (size/4)*8
8c8b0 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 -8,.
8c8c0 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d i==mem3.iM
8c8d0 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 aster ? " **mast
8c8e0 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 er**" : "");.
8c8f0 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 }. }. for(i=0
8c900 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 ; i<MX_SMALL-1;
8c910 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 i++){. if( me
8c920 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 m3.aiSmall[i]==0
8c930 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
8c940 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 fprintf(out, "s
8c950 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b mall(%2d):", i);
8c960 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d . for(j = mem
8c970 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 3.aiSmall[i]; j>
8c980 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 0; j=mem3.aPool[
8c990 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b j].u.list.next){
8c9a0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
8c9b0 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 ut, " %p(%d)", &
8c9c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 mem3.aPool[j],.
8c9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 (me
8c9e0 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e m3.aPool[j-1].u.
8c9f0 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d hdr.size4x/4)*8-
8ca00 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 8);. }. fp
8ca10 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
8ca20 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 ; . }. for(i=0
8ca30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 ; i<N_HASH; i++)
8ca40 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 {. if( mem3.a
8ca50 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f iHash[i]==0 ) co
8ca60 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 ntinue;. fpri
8ca70 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 ntf(out, "hash(%
8ca80 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 2d):", i);. f
8ca90 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 or(j = mem3.aiHa
8caa0 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 sh[i]; j>0; j=me
8cab0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 m3.aPool[j].u.li
8cac0 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 st.next){.
8cad0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 fprintf(out, " %
8cae0 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 p(%d)", &mem3.aP
8caf0 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 ool[j],.
8cb00 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f (mem3.aPoo
8cb10 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[j-1].u.hdr.siz
8cb20 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 e4x/4)*8-8);.
8cb30 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f }. fprintf(o
8cb40 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a ut, "\n"); . }.
8cb50 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8cb60 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 master=%d\n", me
8cb70 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 m3.iMaster);. f
8cb80 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 printf(out, "now
8cb90 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 Used=%d\n", mem3
8cba0 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e .nPool*8 - mem3.
8cbb0 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 szMaster*8);. f
8cbc0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 printf(out, "mxU
8cbd0 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e sed=%d\n", mem3.
8cbe0 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d nPool*8 - mem3.m
8cbf0 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 nMaster*8);. sq
8cc00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
8cc10 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 e(mem3.mutex);.
8cc20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 if( out==stdout
8cc30 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 ){. fflush(s
8cc40 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b tdout);. }else{
8cc50 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 . fclose(out)
8cc60 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e ;. }.#else. UN
8cc70 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
8cc80 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 Filename);.#endi
8cc90 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 f.}../*.** This
8cca0 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
8ccb0 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
8ccc0 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
8ccd0 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 ternal .** linka
8cce0 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 ge..**.** Popula
8ccf0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
8cd00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8cd10 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
8cd20 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
8cd30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
8cd40 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
8cd50 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
8cd60 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 n this file. The
8cd70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 .** arguments sp
8cd80 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 ecify the block
8cd90 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e of memory to man
8cda0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 age..**.** This
8cdb0 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 routine is only
8cdc0 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 called by sqlite
8cdd0 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 3_config(), and
8cde0 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 therefore.** is
8cdf0 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
8ce00 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 69 be threadsafe (i
8ce10 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 t is not)..*/.SQ
8ce20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
8ce30 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
8ce40 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d ethods *sqlite3M
8ce50 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 emGetMemsys3(voi
8ce60 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e d){. static con
8ce70 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
8ce80 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 ethods mempoolMe
8ce90 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d thods = {. m
8cea0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 emsys3Malloc,.
8ceb0 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a memsys3Free,.
8cec0 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c memsys3Real
8ced0 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 loc,. memsys
8cee0 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 3Size,. mems
8cef0 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 ys3Roundup,.
8cf00 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 memsys3Init,.
8cf10 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f memsys3Shutdo
8cf20 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
8cf30 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f return &mempoo
8cf40 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e lMethods;.}..#en
8cf50 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
8cf60 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a ABLE_MEMSYS3 */.
8cf70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8cf80 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a End of mem3.c *
8cf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8cfc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
8cfd0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 Begin file mem5
8cfe0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
8cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
8d010 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f ./*.** 2007 Octo
8d020 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 14.**.** The
8d030 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
8d040 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
8d050 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
8d060 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
8d070 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
8d080 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
8d090 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
8d0a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
8d0b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
8d0c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
8d0d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
8d0e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
8d0f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
8d100 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
8d110 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
8d120 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
8d130 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
8d140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8d180 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
8d190 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
8d1a0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
8d1b0 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d implement a mem
8d1c0 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ory.** allocatio
8d1d0 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 n subsystem for
8d1e0 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a use by SQLite. .
8d1f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 **.** This versi
8d200 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
8d210 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
8d220 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a ystem omits all.
8d230 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 ** use of malloc
8d240 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 74 (). The applicat
8d250 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 65 ion gives SQLite
8d260 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
8d270 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c ry.** before cal
8d280 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 ling sqlite3_ini
8d290 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77 tialize() from w
8d2a0 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 hich allocations
8d2b0 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64 .** are made and
8d2c0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
8d2d0 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78 xMalloc() and x
8d2e0 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d Realloc() .** im
8d2f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f plementations. O
8d300 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 nce sqlite3_init
8d310 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65 ialize() has bee
8d320 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 n called,.** the
8d330 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
8d340 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53 y available to S
8d350 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 61 QLite is fixed a
8d360 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 nd cannot.** be
8d370 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 changed..**.** T
8d380 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8d390 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8d3a0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69 tion subsystem i
8d3b0 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e s included.** in
8d3c0 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20 the build only
8d3d0 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
8d3e0 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 _MEMSYS5 is defi
8d3f0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
8d400 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
8d410 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 uses the follow
8d420 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a ing algorithm:.*
8d430 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 6d *.** 1. All m
8d440 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8d450 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 6e s sizes are roun
8d460 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 65 ded up to a powe
8d470 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 r of 2..**.**
8d480 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 63 2. If two adjac
8d490 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 ent free blocks
8d4a0 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 6f are the halves o
8d4b0 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b f a larger block
8d4c0 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e 20 ,.** then
8d4d0 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 61 the two blocks a
8d4e0 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 6f re coalesed into
8d4f0 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 67 the single larg
8d500 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 er block..**.**
8d510 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 79 3. New memory
8d520 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 is allocated fr
8d530 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 61 om the first ava
8d540 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f 63 ilable free bloc
8d550 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c k..**.** This al
8d560 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 72 gorithm is descr
8d570 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52 ibed in: J. M. R
8d580 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 66 obson. "Bounds f
8d590 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f 6e or Some Function
8d5a0 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 20 s.** Concerning
8d5b0 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 20 Dynamic Storage
8d5c0 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f 75 Allocation". Jou
8d5d0 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 6f rnal of the Asso
8d5e0 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43 ciation for.** C
8d5f0 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e 65 omputing Machine
8d600 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 4e ry, Volume 21, N
8d610 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 39 umber 8, July 19
8d620 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 39 74, pages 491-49
8d630 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20 9..** .** Let n
8d640 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 be the size of t
8d650 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 he largest alloc
8d660 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 79 ation divided by
8d670 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20 the minimum.**
8d680 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
8d690 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 20 (after rounding
8d6a0 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f 20 all sizes up to
8d6b0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 20 a power of 2.)
8d6c0 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 20 Let M.** be the
8d6d0 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f maximum amount o
8d6e0 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f 75 f memory ever ou
8d6f0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
8d700 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e time. Let.** N
8d710 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 6d be the total am
8d720 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 ount of memory a
8d730 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
8d740 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e ocation. Robson
8d750 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 20 .** proved that
8d760 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f this memory allo
8d770 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 cator will never
8d780 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 74 breakdown due t
8d790 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 74 o .** fragmentat
8d7a0 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 ion as long as t
8d7b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
8d7c0 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a straint holds:.*
8d7d0 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 20 *.** N >=
8d7e0 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f 32 M*(1 + log2(n)/2
8d7f0 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20 ) - n + 1.**.**
8d800 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 The sqlite3_stat
8d810 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 6b us() logic track
8d820 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 s the maximum va
8d830 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d 20 lues of n and M
8d840 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 70 so.** that an ap
8d850 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 61 plication can, a
8d860 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 69 t any time, veri
8d870 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 fy this constrai
8d880 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 nt..*/../*.** Th
8d890 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
8d8a0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
8d8b0 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 or is used only
8d8c0 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f when .** SQLITE_
8d8d0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 ENABLE_MEMSYS5 i
8d8e0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 s defined..*/.#i
8d8f0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
8d900 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a LE_MEMSYS5../*.*
8d910 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f * A minimum allo
8d920 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 cation is an ins
8d930 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
8d940 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
8d950 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c 6f ..** Larger allo
8d960 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 61 cations are an a
8d970 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 74 rray of these st
8d980 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 74 ructures where t
8d990 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 he.** size of th
8d9a0 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f 77 e array is a pow
8d9b0 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 er of 2..**.** T
8d9c0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 he size of this
8d9d0 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 61 object must be a
8d9e0 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 power of two.
8d9f0 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a 20 That fact is.**
8da00 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d 73 verified in mems
8da10 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79 ys5Init()..*/.ty
8da20 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
8da30 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 5Link Mem5Link;.
8da40 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 struct Mem5Link
8da50 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20 {. int next;
8da60 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
8da70 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 next free chunk
8da80 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20 */. int prev;
8da90 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8daa0 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 previous free c
8dab0 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a hunk */.};../*.*
8dac0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f * Maximum size o
8dad0 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e f any allocation
8dae0 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 is ((1<<LOGMAX)
8daf0 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 53 *mem5.szAtom). S
8db00 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41 ince.** mem5.szA
8db10 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74 tom is always at
8db20 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 2d least 8 and 32-
8db30 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 65 bit integers are
8db40 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 20 used,.** it is
8db50 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f 73 not actually pos
8db60 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 74 sible to reach t
8db70 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 his limit..*/.#d
8db80 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a efine LOGMAX 30.
8db90 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 ./*.** Masks use
8dba0 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c d for mem5.aCtrl
8dbb0 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a [] elements..*/.
8dbc0 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 #define CTRL_LOG
8dbd0 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a SIZE 0x1f /*
8dbe0 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68 Log2 Size of th
8dbf0 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66 is block */.#def
8dc00 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20 ine CTRL_FREE
8dc10 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75 0x20 /* Tru
8dc20 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64 e if not checked
8dc30 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 out */../*.** A
8dc40 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
8dc50 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
8dc60 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
8dc70 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
8dc80 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
8dc90 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
8dca0 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74 em5". This is t
8dcb0 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
8dcc0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
8dcd0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
8dce0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
8dcf0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
8dd00 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
8dd10 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
8dd20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
8dd30 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
8dd40 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
8dd50 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 struct Mem5Globa
8dd60 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
8dd70 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
8dd80 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 or allocation.
8dd90 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d 3b */. int szAtom;
8dda0 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 /* Smalles
8ddb0 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63 t possible alloc
8ddc0 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a ation in bytes *
8ddd0 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 /. int nBlock;
8dde0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
8ddf0 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 62 f szAtom sized b
8de00 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a locks in zPool *
8de10 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20 /. u8 *zPool;
8de20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 /* Memory a
8de30 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 61 vailable to be a
8de40 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a 20 llocated */. .
8de50 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 /*. ** Mutex t
8de60 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
8de70 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
8de80 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
8de90 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tem.. */. sqli
8dea0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
8deb0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 72 ;.. /*. ** Per
8dec0 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 formance statist
8ded0 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 6e ics. */. u64 n
8dee0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f Alloc; /
8def0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
8df00 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f f calls to mallo
8df10 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c c */. u64 total
8df20 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 6f Alloc; /* To
8df30 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f tal of all mallo
8df40 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 64 c calls - includ
8df50 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 es internal frag
8df60 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c 45 */. u64 totalE
8df70 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f 74 xcess; /* Tot
8df80 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 al internal frag
8df90 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
8dfa0 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 20 32 currentOut;
8dfb0 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 68 /* Current ch
8dfc0 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 6e eckout, includin
8dfd0 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d g internal fragm
8dfe0 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 entation */. u3
8dff0 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 20 2 currentCount;
8e000 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d /* Current num
8e010 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 20 ber of distinct
8e020 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 75 checkouts */. u
8e030 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 20 32 maxOut;
8e040 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e /* Maximum in
8e050 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 stantaneous curr
8e060 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 20 entOut */. u32
8e070 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 maxCount;
8e080 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 /* Maximum insta
8e090 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 ntaneous current
8e0a0 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6d Count */. u32 m
8e0b0 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 2f axRequest; /
8e0c0 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 * Largest alloca
8e0d0 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 20 tion (exclusive
8e0e0 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 of internal frag
8e0f0 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ) */. . /*. *
8e100 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 20 * Lists of free
8e110 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 6c blocks. aiFreel
8e120 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 74 ist[0] is a list
8e130 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 of free blocks
8e140 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 6d of. ** size mem
8e150 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 65 5.szAtom. aiFre
8e160 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 62 elist[1] holds b
8e170 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 7a locks of size sz
8e180 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64 Atom*2.. ** and
8e190 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a so forth.. */.
8e1a0 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74 int aiFreelist
8e1b0 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f [LOGMAX+1];.. /
8e1c0 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72 *. ** Space for
8e1d0 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20 tracking which
8e1e0 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b blocks are check
8e1f0 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73 ed out and the s
8e200 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 ize. ** of each
8e210 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74 block. One byt
8e220 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a e per block.. *
8e230 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a /. u8 *aCtrl;..
8e240 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a } mem5 = { 0 };.
8e250 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 68 ./*.** Access th
8e260 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
8e270 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 72 e through a macr
8e280 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49 o for SQLITE_OMI
8e290 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 T_WSD.*/.#define
8e2a0 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 mem5 GLOBAL(str
8e2b0 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 uct Mem5Global,
8e2c0 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 mem5)../*.** Ass
8e2d0 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c uming mem5.zPool
8e2e0 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 69 is divided up i
8e2f0 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 nto an array of
8e300 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 Mem5Link.** stru
8e310 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 61 ctures, return a
8e320 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
8e330 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e idx-th such lik.
8e340 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35 .*/.#define MEM5
8e350 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 35 LINK(idx) ((Mem5
8e360 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 Link *)(&mem5.zP
8e370 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73 ool[(idx)*mem5.s
8e380 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 zAtom]))../*.**
8e390 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
8e3a0 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 at mem5.aPool[i
8e3b0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
8e3c0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
8e3d0 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65 n. It should be
8e3e0 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 found on mem5.a
8e3f0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
8e400 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ze]..*/.static v
8e410 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e oid memsys5Unlin
8e420 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f k(int i, int iLo
8e430 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65 gsize){. int ne
8e440 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65 xt, prev;. asse
8e450 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 rt( i>=0 && i<me
8e460 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 m5.nBlock );. a
8e470 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e ssert( iLogsize>
8e480 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d =0 && iLogsize<=
8e490 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 LOGMAX );. asse
8e4a0 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b rt( (mem5.aCtrl[
8e4b0 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a i] & CTRL_LOGSIZ
8e4c0 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a E)==iLogsize );.
8e4d0 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49 . next = MEM5LI
8e4e0 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 NK(i)->next;. p
8e4f0 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 rev = MEM5LINK(i
8e500 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70 )->prev;. if( p
8e510 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d rev<0 ){. mem
8e520 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8e530 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 gsize] = next;.
8e540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35 }else{. MEM5
8e550 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74 LINK(prev)->next
8e560 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
8e570 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20 f( next>=0 ){.
8e580 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 MEM5LINK(next)
8e590 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 ->prev = prev;.
8e5a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b }.}../*.** Link
8e5b0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 the chunk at me
8e5c0 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 m5.aPool[i] so t
8e5d0 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c hat is on the iL
8e5e0 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c ogsize.** free l
8e5f0 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
8e600 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 oid memsys5Link(
8e610 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 int i, int iLogs
8e620 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 ize){. int x;.
8e630 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
8e640 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 _mutex_held(mem5
8e650 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 .mutex) );. ass
8e660 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8e670 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8e680 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8e690 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8e6a0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 =LOGMAX );. ass
8e6b0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c ert( (mem5.aCtrl
8e6c0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 [i] & CTRL_LOGSI
8e6d0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b ZE)==iLogsize );
8e6e0 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b .. x = MEM5LINK
8e6f0 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 (i)->next = mem5
8e700 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
8e710 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e size];. MEM5LIN
8e720 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b K(i)->prev = -1;
8e730 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20 . if( x>=0 ){.
8e740 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d assert( x<mem
8e750 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 5.nBlock );.
8e760 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 MEM5LINK(x)->pre
8e770 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d v = i;. }. mem
8e780 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8e790 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f gsize] = i;.}../
8e7a0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 *.** If the STAT
8e7b0 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 IC_MEM mutex is
8e7c0 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
8e7d0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e , obtain it now.
8e7e0 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 The mutex.** wi
8e7f0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 ll already be he
8e800 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 ld (obtained by
8e810 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 code in malloc.c
8e820 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 ) if.** sqlite3G
8e830 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
8e840 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f Stat is true..*/
8e850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8e860 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b sys5Enter(void){
8e870 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
8e880 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 _enter(mem5.mute
8e890 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 x);.}.static voi
8e8a0 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 d memsys5Leave(v
8e8b0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
8e8c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 mutex_leave(mem5
8e8d0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a .mutex);.}../*.*
8e8e0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
8e8f0 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 e of an outstand
8e900 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ing allocation,
8e910 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a in bytes. The.*
8e920 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 * size returned
8e930 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 omits the 8-byte
8e940 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 header overhead
8e950 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 . This only.**
8e960 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 works for chunks
8e970 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e that are curren
8e980 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e tly checked out.
8e990 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8e9a0 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 emsys5Size(void
8e9b0 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 *p){. int iSize
8e9c0 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b = 0;. if( p ){
8e9d0 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 . int i = ((u
8e9e0 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 8 *)p-mem5.zPool
8e9f0 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 )/mem5.szAtom;.
8ea00 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 assert( i>=0
8ea10 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b && i<mem5.nBlock
8ea20 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 );. iSize =
8ea30 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 mem5.szAtom * (1
8ea40 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b << (mem5.aCtrl[
8ea50 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 i]&CTRL_LOGSIZE)
8ea60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
8ea70 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iSize;.}../*.**
8ea80 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65 Find the first e
8ea90 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65 ntry on the free
8eaa0 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 list iLogsize.
8eab0 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 Unlink that.** e
8eac0 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 ntry and return
8ead0 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 its index. .*/.s
8eae0 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8eaf0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 5UnlinkFirst(int
8eb00 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e iLogsize){. in
8eb10 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 t i;. int iFirs
8eb20 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c t;.. assert( iL
8eb30 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f ogsize>=0 && iLo
8eb40 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b gsize<=LOGMAX );
8eb50 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20 . i = iFirst =
8eb60 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
8eb70 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 iLogsize];. ass
8eb80 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29 ert( iFirst>=0 )
8eb90 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 ;. while( i>0 )
8eba0 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72 {. if( i<iFir
8ebb0 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b st ) iFirst = i;
8ebc0 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e . i = MEM5LIN
8ebd0 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a K(i)->next;. }.
8ebe0 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 memsys5Unlink(
8ebf0 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 iFirst, iLogsize
8ec00 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72 );. return iFir
8ec10 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 st;.}../*.** Ret
8ec20 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d urn a block of m
8ec30 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 emory of at leas
8ec40 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 t nBytes in size
8ec50 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
8ec60 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 74 if unable. Ret
8ec70 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74 urn NULL if nByt
8ec80 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 es==0..**.** The
8ec90 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 caller guarante
8eca0 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 6f es that nByte po
8ecb0 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 sitive..**.** Th
8ecc0 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 e caller has obt
8ecd0 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 72 ained a mutex pr
8ece0 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
8ecf0 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
8ed00 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 65 so there is neve
8ed10 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 61 r any chance tha
8ed20 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a t two or more.**
8ed30 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 threads can be
8ed40 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 in this routine
8ed50 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
8ed60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8ed70 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 *memsys5MallocU
8ed80 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 nsafe(int nByte)
8ed90 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
8eda0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8edb0 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 a mem5.aPool[]
8edc0 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42 slot */. int iB
8edd0 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e in; /* In
8ede0 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 dex into mem5.ai
8edf0 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 Freelist[] */.
8ee00 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20 int iFullSz;
8ee10 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f /* Size of allo
8ee20 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75 cation rounded u
8ee30 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 20 p to power of 2
8ee40 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a */. int iLogsiz
8ee50 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 e; /* Log2 of
8ee60 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 iFullSz/POW2_MI
8ee70 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 N */.. /* nByte
8ee80 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 74 must be a posit
8ee90 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ive */. assert(
8eea0 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f nByte>0 );.. /
8eeb0 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 * Keep track of
8eec0 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f the maximum allo
8eed0 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 cation request.
8eee0 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 Even unfulfille
8eef0 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 d. ** requests
8ef00 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 are counted */.
8ef10 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e if( (u32)nByte>
8ef20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 mem5.maxRequest
8ef30 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 ){. mem5.maxR
8ef40 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a equest = nByte;.
8ef50 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20 }.. /* Abort
8ef60 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 if the requested
8ef70 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8ef80 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
8ef90 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a the largest. **
8efa0 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 68 power of two th
8efb0 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 73 at we can repres
8efc0 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 74 ent using 32-bit
8efd0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 signed integers
8efe0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79 .. */. if( nBy
8eff0 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 20 te > 0x40000000
8f000 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
8f010 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 . }.. /* Round
8f020 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 nByte up to the
8f030 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 next valid powe
8f040 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f r of two */. fo
8f050 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 r(iFullSz=mem5.s
8f060 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d zAtom, iLogsize=
8f070 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 0; iFullSz<nByte
8f080 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 ; iFullSz *= 2,
8f090 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 iLogsize++){}..
8f0a0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 /* Make sure me
8f0b0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8f0c0 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 ogsize] contains
8f0d0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 at least one fr
8f0e0 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 ee. ** block.
8f0f0 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c If not, then spl
8f100 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 it a block of th
8f110 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f e next larger po
8f120 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 wer of. ** two
8f130 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 in order to crea
8f140 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c te a new free bl
8f150 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 ock of size iLog
8f160 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 size.. */. for
8f170 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 (iBin=iLogsize;
8f180 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
8f190 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c iBin]<0 && iBin<
8f1a0 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 =LOGMAX; iBin++)
8f1b0 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f {}. if( iBin>LO
8f1c0 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b GMAX ) return 0;
8f1d0 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e . i = memsys5Un
8f1e0 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b linkFirst(iBin);
8f1f0 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 . while( iBin>i
8f200 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 Logsize ){. i
8f210 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 nt newSize;..
8f220 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 iBin--;. new
8f230 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e Size = 1 << iBin
8f240 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c ;. mem5.aCtrl
8f250 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 [i+newSize] = CT
8f260 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a RL_FREE | iBin;.
8f270 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 memsys5Link(
8f280 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 i+newSize, iBin)
8f290 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 ;. }. mem5.aCt
8f2a0 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 rl[i] = iLogsize
8f2b0 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61 ;.. /* Update a
8f2c0 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d llocator perform
8f2d0 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 2e ance statistics.
8f2e0 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f */. mem5.nAllo
8f2f0 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 c++;. mem5.tota
8f300 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 lAlloc += iFullS
8f310 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 z;. mem5.totalE
8f320 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a xcess += iFullSz
8f330 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 - nByte;. mem5
8f340 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b .currentCount++;
8f350 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f . mem5.currentO
8f360 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 ut += iFullSz;.
8f370 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 if( mem5.maxCou
8f380 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 nt<mem5.currentC
8f390 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 ount ) mem5.maxC
8f3a0 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 ount = mem5.curr
8f3b0 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 entCount;. if(
8f3c0 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 mem5.maxOut<mem5
8f3d0 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 .currentOut ) me
8f3e0 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 m5.maxOut = mem5
8f3f0 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 .currentOut;..
8f400 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e /* Return a poin
8f410 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 ter to the alloc
8f420 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a ated memory. */.
8f430 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
8f440 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 &mem5.zPool[i*me
8f450 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f m5.szAtom];.}../
8f460 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 *.** Free an out
8f470 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 standing memory
8f480 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 allocation..*/.s
8f490 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
8f4a0 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 s5FreeUnsafe(voi
8f4b0 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 d *pOld){. u32
8f4c0 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a size, iLogsize;.
8f4d0 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20 int iBlock;..
8f4e0 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 /* Set iBlock t
8f4f0 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 o the index of t
8f500 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 he block pointed
8f510 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a to by pOld in .
8f520 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f ** the array o
8f530 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 79 f mem5.szAtom by
8f540 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 te blocks pointe
8f550 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f d to by mem5.zPo
8f560 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 ol.. */. iBloc
8f570 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d k = ((u8 *)pOld-
8f580 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 mem5.zPool)/mem5
8f590 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 .szAtom;.. /* C
8f5a0 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f heck that the po
8f5b0 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 inter pOld point
8f5c0 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f s to a valid, no
8f5d0 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f n-free block. */
8f5e0 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 . assert( iBloc
8f5f0 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d k>=0 && iBlock<m
8f600 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8f610 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70 assert( ((u8 *)p
8f620 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 Old-mem5.zPool)%
8f630 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29 mem5.szAtom==0 )
8f640 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d ;. assert( (mem
8f650 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8f660 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20 & CTRL_FREE)==0
8f670 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d );.. iLogsize =
8f680 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8f690 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 ck] & CTRL_LOGSI
8f6a0 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c ZE;. size = 1<<
8f6b0 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65 iLogsize;. asse
8f6c0 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d rt( iBlock+size-
8f6d0 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 1<(u32)mem5.nBlo
8f6e0 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 ck );.. mem5.aC
8f6f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 trl[iBlock] |= C
8f700 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d 35 TRL_FREE;. mem5
8f710 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 .aCtrl[iBlock+si
8f720 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 ze-1] |= CTRL_FR
8f730 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 EE;. assert( me
8f740 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e m5.currentCount>
8f750 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 0 );. assert( m
8f760 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 3d em5.currentOut>=
8f770 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f (size*mem5.szAto
8f780 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 m) );. mem5.cur
8f790 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d rentCount--;. m
8f7a0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2d em5.currentOut -
8f7b0 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 = size*mem5.szAt
8f7c0 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 om;. assert( me
8f7d0 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 20 m5.currentOut>0
8f7e0 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 || mem5.currentC
8f7f0 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 ount==0 );. ass
8f800 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e ert( mem5.curren
8f810 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35 tCount>0 || mem5
8f820 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 29 .currentOut==0 )
8f830 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b ;.. mem5.aCtrl[
8f840 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 iBlock] = CTRL_F
8f850 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a REE | iLogsize;.
8f860 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 while( ALWAYS(
8f870 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 29 iLogsize<LOGMAX)
8f880 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64 ){. int iBud
8f890 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c dy;. if( (iBl
8f8a0 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 ock>>iLogsize) &
8f8b0 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64 1 ){. iBud
8f8c0 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 dy = iBlock - si
8f8d0 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ze;. }else{.
8f8e0 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 iBuddy = iB
8f8f0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 lock + size;.
8f900 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 }. assert( i
8f910 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 Buddy>=0 );.
8f920 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c if( (iBuddy+(1<<
8f930 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e iLogsize))>mem5.
8f940 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a nBlock ) break;.
8f950 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 if( mem5.aCt
8f960 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52 rl[iBuddy]!=(CTR
8f970 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
8f980 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 e) ) break;.
8f990 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 memsys5Unlink(iB
8f9a0 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b uddy, iLogsize);
8f9b0 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b . iLogsize++;
8f9c0 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c . if( iBuddy<
8f9d0 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 iBlock ){.
8f9e0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
8f9f0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c y] = CTRL_FREE |
8fa00 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 iLogsize;.
8fa10 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8fa20 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ck] = 0;. i
8fa30 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a Block = iBuddy;.
8fa40 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8fa50 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8fa60 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ck] = CTRL_FREE
8fa70 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
8fa80 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 mem5.aCtrl[iBu
8fa90 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ddy] = 0;. }.
8faa0 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 size *= 2;.
8fab0 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b }. memsys5Link
8fac0 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a (iBlock, iLogsiz
8fad0 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c e);.}../*.** All
8fae0 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 ocate nBytes of
8faf0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 memory.*/.static
8fb00 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 void *memsys5Ma
8fb10 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 lloc(int nBytes)
8fb20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
8fb30 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 4 *p = 0;. if(
8fb40 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 nBytes>0 ){.
8fb50 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a memsys5Enter();.
8fb60 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d p = memsys5M
8fb70 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8fb80 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 es);. memsys5
8fb90 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 Leave();. }. r
8fba0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 eturn (void*)p;
8fbb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
8fbc0 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 emory..**.** The
8fbd0 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 6d outer layer mem
8fbe0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72 ory allocator pr
8fbf0 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 74 events this rout
8fc00 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e ine from.** bein
8fc10 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 50 g called with pP
8fc20 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 rior==0..*/.stat
8fc30 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 ic void memsys5F
8fc40 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
8fc50 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 ){. assert( pPr
8fc60 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 ior!=0 );. mems
8fc70 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65 ys5Enter();. me
8fc80 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 msys5FreeUnsafe(
8fc90 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 pPrior);. memsy
8fca0 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a 0a s5Leave(); .}..
8fcb0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
8fcc0 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 size of an exis
8fcd0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ting memory allo
8fce0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
8fcf0 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 e outer layer me
8fd00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 mory allocator p
8fd10 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 revents this rou
8fd20 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 tine from.** bei
8fd30 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 ng called with p
8fd40 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a Prior==0. .**.*
8fd50 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 61 * nBytes is alwa
8fd60 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 69 ys a value obtai
8fd70 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 ned from a prior
8fd80 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73 call to.** mems
8fd90 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 6e ys5Round(). Hen
8fda0 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 ce nBytes is alw
8fdb0 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ays a non-negati
8fdc0 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 ve power.** of t
8fdd0 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d 3d wo. If nBytes==
8fde0 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 0 that means tha
8fdf0 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 6c t an oversize al
8fe00 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20 location.** (an
8fe10 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 allocation large
8fe20 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 30 r than 0x4000000
8fe30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 64 0) was requested
8fe40 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 and this.** rou
8fe50 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75 tine should retu
8fe60 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 65 rn 0 without fre
8fe70 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a eing pPrior..*/.
8fe80 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
8fe90 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 sys5Realloc(void
8fea0 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
8feb0 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c ytes){. int nOl
8fec0 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 d;. void *p;.
8fed0 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d assert( pPrior!=
8fee0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 0 );. assert( (
8fef0 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d 31 nBytes&(nBytes-1
8ff00 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ))==0 );. asser
8ff10 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b 0a t( nBytes>=0 );.
8ff20 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 20 if( nBytes==0
8ff30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
8ff40 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 . }. nOld = me
8ff50 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 msys5Size(pPrior
8ff60 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c );. if( nBytes<
8ff70 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 =nOld ){. ret
8ff80 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a urn pPrior;. }.
8ff90 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
8ffa0 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d ;. p = memsys5M
8ffb0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
8ffc0 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a es);. if( p ){.
8ffd0 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 memcpy(p, pP
8ffe0 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 rior, nOld);.
8fff0 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 memsys5FreeUnsa
90000 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a fe(pPrior);. }.
90010 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 memsys5Leave()
90020 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
90030 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
90040 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
90050 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
90060 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
90070 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c 6c . If.** the all
90080 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 6c ocation is too l
90090 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 6c arge to be handl
900a0 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 ed by this alloc
900b0 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a ation system,.**
900c0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a return 0..**.**
900d0 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 All allocations
900e0 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
900f0 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 74 of two and must
90100 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 79 be expressed by
90110 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 67 a.** 32-bit sig
90120 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 65 ned integer. He
90130 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 nce the largest
90140 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 78 allocation is 0x
90150 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 31 40000000.** or 1
90160 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 2e 073741824 bytes.
90170 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
90180 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e emsys5Roundup(in
90190 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c t n){. int iFul
901a0 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 30 lSz;. if( n > 0
901b0 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 75 x40000000 ) retu
901c0 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c rn 0;. for(iFul
901d0 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b lSz=mem5.szAtom;
901e0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c iFullSz<n; iFul
901f0 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 lSz *= 2);. ret
90200 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a urn iFullSz;.}..
90210 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
90220 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 20 ceiling of the
90230 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 32 logarithm base 2
90240 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a of iValue..**.*
90250 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d 65 * Examples: me
90260 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 30 msys5Log(1) -> 0
90270 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
90280 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d 3e memsys5Log(2) ->
90290 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 1.**
902a0 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 20 memsys5Log(4)
902b0 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 20 -> 2.**
902c0 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 35 memsys5Log(5
902d0 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20 ) -> 3.**
902e0 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 memsys5Log
902f0 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 (8) -> 3.**
90300 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c memsys5L
90310 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74 og(9) -> 4.*/.st
90320 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
90330 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b Log(int iValue){
90340 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 . int iLog;. f
90350 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 or(iLog=0; (1<<i
90360 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f Log)<iValue; iLo
90370 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 g++);. return i
90380 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e Log;.}../*.** In
90390 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
903a0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a ory allocator..*
903b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
903c0 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 e is not threads
903d0 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 72 afe. The caller
903e0 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 must be holding
903f0 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 70 a mutex.** to p
90400 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20 revent multiple
90410 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e 74 threads from ent
90420 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 6d ering at the sam
90430 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 e time..*/.stati
90440 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 c int memsys5Ini
90450 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
90460 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 {. int ii;
90470 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
90480 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
90490 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f nByte; /
904a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
904b0 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 s of memory avai
904c0 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 6c lable to this al
904d0 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 locator */. u8
904e0 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 *zByte;
904f0 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c 65 /* Memory usable
90500 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 by this allocat
90510 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e or */. int nMin
90520 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 6f Log; /* Lo
90530 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e 69 g base 2 of mini
90540 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 mum allocation s
90550 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ize in bytes */.
90560 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 int iOffset;
90570 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 65 /* An offse
90580 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 72 t into mem5.aCtr
90590 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 l[] */.. UNUSED
905a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
905b0 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 74 ed);.. /* For t
905c0 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 he purposes of t
905d0 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 73 his routine, dis
905e0 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 2a able the mutex *
905f0 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d /. mem5.mutex =
90600 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 0;.. /* The si
90610 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e 6b ze of a Mem5Link
90620 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 object must be
90630 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 a power of two.
90640 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 2a Verify that. *
90650 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e 0a * this is case..
90660 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
90670 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 sizeof(Mem5Link)
90680 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e &(sizeof(Mem5Lin
90690 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 k)-1))==0 );..
906a0 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47 nByte = sqlite3G
906b0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 lobalConfig.nHea
906c0 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 38 p;. zByte = (u8
906d0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
906e0 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 61 onfig.pHeap;. a
906f0 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 20 ssert( zByte!=0
90700 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 ); /* sqlite3_c
90710 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f 74 onfig() does not
90720 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 65 allow otherwise
90730 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d */.. nMinLog =
90740 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c 69 memsys5Log(sqli
90750 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90760 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e 73 mnReq);. mem5.s
90770 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e zAtom = (1<<nMin
90780 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 Log);. while( (
90790 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c int)sizeof(Mem5L
907a0 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d ink)>mem5.szAtom
907b0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a 41 ){. mem5.szA
907c0 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f tom = mem5.szAto
907d0 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d m << 1;. }.. m
907e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 em5.nBlock = (nB
907f0 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 74 yte / (mem5.szAt
90800 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b om+sizeof(u8)));
90810 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 . mem5.zPool =
90820 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 zByte;. mem5.aC
90830 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d trl = (u8 *)&mem
90840 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 5.zPool[mem5.nBl
90850 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d ock*mem5.szAtom]
90860 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 ;.. for(ii=0; i
90870 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 i<=LOGMAX; ii++)
90880 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 {. mem5.aiFre
90890 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a elist[ii] = -1;.
908a0 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d }.. iOffset =
908b0 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47 0;. for(ii=LOG
908c0 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d MAX; ii>=0; ii--
908d0 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f ){. int nAllo
908e0 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20 c = (1<<ii);.
908f0 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41 if( (iOffset+nA
90900 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f lloc)<=mem5.nBlo
90910 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 ck ){. mem5
90920 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20 .aCtrl[iOffset]
90930 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45 = ii | CTRL_FREE
90940 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c ;. memsys5L
90950 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29 ink(iOffset, ii)
90960 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 ;. iOffset
90970 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d += nAlloc;. }
90980 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66 . assert((iOf
90990 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d fset+nAlloc)>mem
909a0 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 5.nBlock);. }..
909b0 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 20 /* If a mutex
909c0 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 is required for
909d0 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f 6e normal operation
909e0 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 2a , allocate one *
909f0 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 /. if( sqlite3G
90a00 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
90a10 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6d stat==0 ){. m
90a20 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em5.mutex = sqli
90a30 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
90a40 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
90a50 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72 C_MEM);. }.. r
90a60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
90a70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
90a80 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
90a90 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
90aa0 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f id memsys5Shutdo
90ab0 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 wn(void *NotUsed
90ac0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
90ad0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
90ae0 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 mem5.mutex = 0
90af0 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 ;. return;.}..#
90b00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
90b10 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 T./*.** Open the
90b20 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
90b30 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
90b40 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
90b50 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
90b60 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
90b70 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
90b80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
90b90 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 te3Memsys5Dump(c
90ba0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
90bb0 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f name){. FILE *o
90bc0 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 ut;. int i, j,
90bd0 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 n;. int nMinLog
90be0 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 ;.. if( zFilena
90bf0 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 me==0 || zFilena
90c00 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 me[0]==0 ){.
90c10 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 out = stdout;.
90c20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d }else{. out =
90c30 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 fopen(zFilename
90c40 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 , "w");. if(
90c50 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 out==0 ){.
90c60 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
90c70 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 "** Unable to ou
90c80 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 tput memory debu
90c90 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 g output log: %s
90ca0 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 **\n",.
90cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 zF
90cc0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ilename);.
90cd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
90ce0 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 }. memsys5Enter
90cf0 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 ();. nMinLog =
90d00 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e memsys5Log(mem5.
90d10 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 szAtom);. for(i
90d20 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 =0; i<=LOGMAX &&
90d30 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 i+nMinLog<32; i
90d40 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 ++){. for(n=0
90d50 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c , j=mem5.aiFreel
90d60 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 ist[i]; j>=0; j
90d70 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e = MEM5LINK(j)->n
90d80 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 ext, n++){}.
90d90 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 fprintf(out, "fr
90da0 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20 eelist items of
90db0 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 size %d: %d\n",
90dc0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69 mem5.szAtom << i
90dd0 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 , n);. }. fpri
90de0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e ntf(out, "mem5.n
90df0 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c Alloc = %l
90e00 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c lu\n", mem5.nAll
90e10 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
90e20 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 ut, "mem5.totalA
90e30 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 lloc = %llu\n"
90e40 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f , mem5.totalAllo
90e50 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 c);. fprintf(ou
90e60 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 t, "mem5.totalEx
90e70 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c cess = %llu\n",
90e80 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 mem5.totalExces
90e90 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 s);. fprintf(ou
90ea0 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 t, "mem5.current
90eb0 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d Out = %u\n", m
90ec0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b em5.currentOut);
90ed0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
90ee0 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 "mem5.currentCou
90ef0 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 nt = %u\n", mem5
90f00 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a .currentCount);.
90f10 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90f20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 mem5.maxOut
90f30 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
90f40 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e maxOut);. fprin
90f50 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 tf(out, "mem5.ma
90f60 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c xCount = %u\
90f70 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e n", mem5.maxCoun
90f80 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 t);. fprintf(ou
90f90 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 t, "mem5.maxRequ
90fa0 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d est = %u\n", m
90fb0 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b em5.maxRequest);
90fc0 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
90fd0 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 );. if( out==st
90fe0 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 dout ){. fflu
90ff0 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 sh(stdout);. }e
91000 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 lse{. fclose(
91010 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 out);. }.}.#end
91020 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
91030 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e outine is the on
91040 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 ly routine in th
91050 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 is file with ext
91060 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 ernal .** linkag
91070 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 e. It returns a
91080 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
91090 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f tic sqlite3_mem_
910a0 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63 methods.** struc
910b0 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 t populated with
910c0 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74 the memsys5 met
910d0 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hods..*/.SQLITE_
910e0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 PRIVATE const sq
910f0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
91100 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 s *sqlite3MemGet
91110 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20 Memsys5(void){.
91120 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
91130 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
91140 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 s memsys5Methods
91150 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 = {. memsys
91160 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 5Malloc,. me
91170 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20 msys5Free,.
91180 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a memsys5Realloc,.
91190 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65 memsys5Size
911a0 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f ,. memsys5Ro
911b0 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 undup,. mems
911c0 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 ys5Init,. me
911d0 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20 msys5Shutdown,.
911e0 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 0. };. ret
911f0 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 urn &memsys5Meth
91200 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ods;.}..#endif /
91210 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
91220 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a MEMSYS5 */../***
91230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
91240 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a of mem5.c ******
91250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
91280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
91290 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a n file mutex.c *
912a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
912b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
912c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
912d0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 * 2007 August 14
912e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
912f0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
91300 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
91310 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
91320 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
91330 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
91340 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
91350 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
91360 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
91370 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
91380 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
91390 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
913a0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
913b0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
913c0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
913d0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
913e0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
913f0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
91400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
91440 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
91450 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
91460 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
91470 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ment mutexes..**
91480 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
91490 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 ntains code that
914a0 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 is common acros
914b0 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c s all mutex impl
914c0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a ementations..*/.
914d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
914e0 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 21 64 ITE_DEBUG) && !d
914f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 efined(SQLITE_MU
91500 54 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20 TEX_OMIT)./*.**
91510 46 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70 75 For debugging pu
91520 72 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20 77 rposes, record w
91530 68 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73 75 hen the mutex su
91540 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 bsystem is initi
91550 61 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75 6e alized.** and un
91560 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20 74 initialized so t
91570 68 61 74 20 77 65 20 63 61 6e 20 61 73 73 65 72 hat we can asser
91580 74 28 29 20 69 66 20 74 68 65 72 65 20 69 73 20 t() if there is
91590 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a an attempt to.**
915a0 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 65 allocate a mute
915b0 78 20 77 68 69 6c 65 20 74 68 65 20 73 79 73 74 x while the syst
915c0 65 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69 em is uninitiali
915d0 7a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 zed..*/.static S
915e0 51 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d 75 QLITE_WSD int mu
915f0 74 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a 23 texIsInit = 0;.#
91600 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
91610 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64 DEBUG */...#ifnd
91620 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
91630 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 OMIT./*.** Initi
91640 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
91650 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
91660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
91670 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 lite3MutexInit(v
91680 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 oid){ . int rc
91690 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
916a0 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
916b0 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 Config.bCoreMute
916c0 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 x ){. if( !sq
916d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
916e0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
916f0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 loc ){. /*
91700 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c If the xMutexAll
91710 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f oc method has no
91720 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e t been set, then
91730 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f the user did no
91740 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 t. ** insta
91750 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 ll a mutex imple
91760 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 mentation via sq
91770 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 lite3_config() p
91780 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a rior to . *
91790 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 * sqlite3_initia
917a0 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c lize() being cal
917b0 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 led. This block
917c0 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 copies pointers
917d0 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 to. ** the
917e0 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e default implemen
917f0 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 tation into the
91800 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91810 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 fig structure..
91820 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
91830 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
91840 6f 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c ods *pFrom = sql
91850 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
91860 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ();. sqlite
91870 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
91880 2a 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47 *pTo = &sqlite3G
91890 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
918a0 78 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 x;.. memcpy
918b0 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 (pTo, pFrom, off
918c0 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 setof(sqlite3_mu
918d0 74 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 tex_methods, xMu
918e0 74 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 texAlloc));.
918f0 20 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78 memcpy(&pTo->x
91900 4d 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f MutexFree, &pFro
91910 6d 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20 m->xMutexFree,.
91920 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 size
91930 6f 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65 of(*pTo) - offse
91940 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 tof(sqlite3_mute
91950 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 x_methods, xMute
91960 78 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70 xFree));. p
91970 54 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 To->xMutexAlloc
91980 3d 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 = pFrom->xMutexA
91990 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 lloc;. }.
919a0 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 rc = sqlite3Glob
919b0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
919c0 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d MutexInit();. }
919d0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
919e0 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 DEBUG. GLOBAL(i
919f0 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 nt, mutexIsInit)
91a00 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 = 1;.#endif..
91a10 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
91a20 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 .** Shutdown the
91a30 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54 mutex system. T
91a40 68 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72 his call frees r
91a50 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 esources allocat
91a60 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
91a70 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a MutexInit()..*/.
91a80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
91a90 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 nt sqlite3MutexE
91aa0 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 nd(void){. int
91ab0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
91ac0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
91ad0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
91ae0 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 xMutexEnd ){.
91af0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f rc = sqlite3Glo
91b00 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
91b10 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d xMutexEnd();. }
91b20 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
91b30 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 DEBUG. GLOBAL(i
91b40 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29 nt, mutexIsInit)
91b50 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 = 0;.#endif..
91b60 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
91b70 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
91b80 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
91b90 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f ic mutex or allo
91ba0 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 6d cate a new dynam
91bb0 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ic one..*/.SQLIT
91bc0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 E_API sqlite3_mu
91bd0 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 tex *sqlite3_mut
91be0 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 ex_alloc(int id)
91bf0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
91c00 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 _OMIT_AUTOINIT.
91c10 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 if( sqlite3_ini
91c20 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 tialize() ) retu
91c30 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 rn 0;.#endif. r
91c40 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
91c50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
91c60 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b xMutexAlloc(id);
91c70 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
91c80 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 TE sqlite3_mutex
91c90 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c *sqlite3MutexAl
91ca0 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 loc(int id){. i
91cb0 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 f( !sqlite3Globa
91cc0 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
91cd0 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ex ){. return
91ce0 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 0;. }. assert
91cf0 28 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 ( GLOBAL(int, mu
91d00 74 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20 20 texIsInit) );.
91d10 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
91d20 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
91d30 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 .xMutexAlloc(id)
91d40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
91d50 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e a dynamic mutex.
91d60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
91d70 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 oid sqlite3_mute
91d80 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d x_free(sqlite3_m
91d90 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
91da0 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
91db0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
91dc0 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29 ex.xMutexFree(p)
91dd0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
91de0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 btain the mutex
91df0 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 p. If some other
91e00 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20 thread already
91e10 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62 has the mutex, b
91e20 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 lock.** until it
91e30 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 can be obtained
91e40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
91e50 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
91e60 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
91e70 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
91e80 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
91e90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
91ea0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 utex.xMutexEnter
91eb0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
91ec0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 * Obtain the mut
91ed0 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73 ex p. If success
91ee0 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ful, return SQLI
91ef0 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 TE_OK. Otherwise
91f00 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 , if another.**
91f10 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 thread holds the
91f20 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61 mutex and it ca
91f30 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
91f40 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
91f50 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f BUSY..*/.SQLITE_
91f60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
91f70 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 mutex_try(sqlite
91f80 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
91f90 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
91fa0 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 K;. if( p ){.
91fb0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
91fc0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
91fd0 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b ex.xMutexTry(p);
91fe0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
91ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
92000 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
92010 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
92020 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
92030 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
92040 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 * entered by the
92050 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 same thread. T
92060 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
92070 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
92080 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 mutex .** is not
92090 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
920a0 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f ed. If a NULL po
920b0 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20 inter is passed
920c0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a as an argument.*
920d0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
920e0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
920f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
92100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
92110 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
92120 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
92130 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
92140 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
92150 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 MutexLeave(p);.
92160 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
92170 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
92180 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
92190 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
921a0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
921b0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
921c0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
921d0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
921e0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 statements..*/.S
921f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
92200 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
92210 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
92220 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
92230 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
92240 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
92250 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a MutexHeld(p);.}.
92260 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
92270 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
92280 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
92290 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
922a0 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 p==0 || sqlite3
922b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
922c0 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 ex.xMutexNotheld
922d0 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 (p);.}.#endif..#
922e0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
922f0 4d 55 54 45 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f MUTEX_OMIT */../
92300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
92310 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a nd of mutex.c **
92320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
92360 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
92370 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a noop.c *********
92380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
923a0 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 *.** 2008 Octobe
923b0 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 07.**.** The a
923c0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
923d0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
923e0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
923f0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
92400 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
92410 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
92420 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
92430 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
92440 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
92450 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
92460 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
92470 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
92480 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
92490 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
924a0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
924b0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
924c0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
924d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
924f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92510 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
92520 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
92530 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
92540 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
92550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 ..**.** This imp
92560 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 lementation in t
92570 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f his file does no
92580 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75 t provide any mu
92590 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f tual.** exclusio
925a0 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75 n and is thus su
925b0 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f itable for use o
925c0 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69 nly in applicati
925d0 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20 ons.** that use
925e0 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67 SQLite in a sing
925f0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
92600 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 routines defined
92610 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61 .** here are pla
92620 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70 ce-holders. App
92630 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75 lications can su
92640 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67 bstitute working
92650 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e .** mutex routin
92660 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 es at start-time
92670 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a using the.**.**
92680 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e sqlite3_con
92690 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
926a0 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a G_MUTEX,...).**.
926b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a ** interface..**
926c0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 .** If compiled
926d0 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 with SQLITE_DEBU
926e0 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e G, then addition
926f0 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65 al logic is inse
92700 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65 rted.** that doe
92710 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 s error checking
92720 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d on mutexes to m
92730 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 ake sure they ar
92740 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 e being.** calle
92750 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a d correctly..*/.
92760 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ..#if defined(SQ
92770 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 LITE_MUTEX_NOOP)
92780 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
92790 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
927a0 20 53 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66 Stub routines f
927b0 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 or all mutex met
927c0 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 hods..**.** This
927d0 20 72 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 routines provid
927e0 65 20 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c e no mutual excl
927f0 75 73 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 usion or error c
92800 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
92810 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 ic int noopMutex
92820 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 Held(sqlite3_mut
92830 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 ex *p){ return 1
92840 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e ; }.static int n
92850 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 oopMutexNotheld(
92860 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
92870 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 ){ return 1; }.s
92880 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
92890 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 texInit(void){ r
928a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
928b0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f }.static int no
928c0 6f 70 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 opMutexEnd(void)
928d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
928e0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c OK; }.static sql
928f0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 ite3_mutex *noop
92900 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 MutexAlloc(int i
92910 64 29 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69 d){ return (sqli
92920 74 65 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a te3_mutex*)8; }.
92930 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 static void noop
92940 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 MutexFree(sqlite
92950 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 3_mutex *p){ ret
92960 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f urn; }.static vo
92970 69 64 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 id noopMutexEnte
92980 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
92990 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 *p){ return; }.s
929a0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
929b0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
929c0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e utex *p){ return
929d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
929e0 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 atic void noopMu
929f0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 texLeave(sqlite3
92a00 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 _mutex *p){ retu
92a10 72 6e 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 rn; }..SQLITE_PR
92a20 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
92a30 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
92a40 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
92a50 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
92a60 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
92a70 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
92a80 7b 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 {. noopMutexI
92a90 6e 69 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 nit,. noopMut
92aa0 65 78 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d exEnd,. noopM
92ab0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e utexAlloc,. n
92ac0 6f 6f 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 oopMutexFree,.
92ad0 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 noopMutexEnter
92ae0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 ,. noopMutexT
92af0 72 79 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 ry,. noopMute
92b00 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f xLeave,.. noo
92b10 70 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 pMutexHeld,.
92b20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 noopMutexNotheld
92b30 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 . };.. return
92b40 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 &sMutex;.}.#endi
92b50 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
92b60 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 ITE_MUTEX_NOOP)
92b70 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
92b80 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 TE_DEBUG) */..#i
92b90 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
92ba0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
92bb0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
92bc0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 EBUG)./*.** In t
92bd0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
92be0 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69 on, error checki
92bf0 6e 67 20 69 73 20 70 72 6f 76 69 64 65 64 20 66 ng is provided f
92c00 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e or testing.** an
92c10 64 20 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 d debugging purp
92c20 6f 73 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78 oses. The mutex
92c30 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 es still do not
92c40 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d provide any.** m
92c50 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e utual exclusion.
92c60 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d .*/../*.** The m
92c70 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 utex object.*/.s
92c80 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 truct sqlite3_mu
92c90 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 tex {. int id;
92ca0 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 /* The mutex
92cb0 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 type */. int c
92cc0 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 nt; /* Number
92cd0 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 of entries with
92ce0 6f 75 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c out a matching l
92cf0 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a eave */.};../*.*
92d00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
92d10 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 tex_held() and s
92d20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
92d30 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 held() routine a
92d40 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 re.** intended f
92d50 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 or use inside as
92d60 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
92d70 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
92d80 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 debugMutexHeld(
92d90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
92da0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 ){. return p==0
92db0 20 7c 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a || p->cnt>0;.}.
92dc0 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 static int debug
92dd0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c MutexNotheld(sql
92de0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
92df0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
92e00 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f p->cnt==0;.}../
92e10 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
92e20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 and deinitialize
92e30 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 the mutex subsy
92e40 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 stem..*/.static
92e50 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e int debugMutexIn
92e60 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
92e70 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
92e80 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
92e90 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
92ea0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
92eb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
92ec0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
92ed0 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 () routine alloc
92ee0 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 ates a new.** mu
92ef0 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 tex and returns
92f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
92f10 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 If it returns
92f20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 NULL.** that mea
92f30 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 ns that a mutex
92f40 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c could not be all
92f50 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 ocated. .*/.stat
92f60 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
92f70 20 2a 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f *debugMutexAllo
92f80 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 c(int id){. sta
92f90 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
92fa0 78 20 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 x aStatic[6];.
92fb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
92fc0 4e 65 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 New = 0;. switc
92fd0 68 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 h( id ){. cas
92fe0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 e SQLITE_MUTEX_F
92ff0 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 AST:. case SQ
93000 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
93010 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e SIVE: {. pN
93020 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ew = sqlite3Mall
93030 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 oc(sizeof(*pNew)
93040 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 );. if( pNe
93050 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 w ){. pNe
93060 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 w->id = id;.
93070 20 20 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 pNew->cnt =
93080 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
93090 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
930a0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
930b0 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 assert( id-2
930c0 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 >= 0 );. as
930d0 73 65 72 74 28 20 69 64 2d 32 20 3c 20 28 69 6e sert( id-2 < (in
930e0 74 29 28 73 69 7a 65 6f 66 28 61 53 74 61 74 69 t)(sizeof(aStati
930f0 63 29 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69 c)/sizeof(aStati
93100 63 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 20 20 c[0])) );.
93110 70 4e 65 77 20 3d 20 26 61 53 74 61 74 69 63 5b pNew = &aStatic[
93120 69 64 2d 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 id-2];. pNe
93130 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 w->id = id;.
93140 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
93150 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
93160 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
93170 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
93180 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
93190 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 allocated mutex
931a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
931b0 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65 28 debugMutexFree(
931c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
931d0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
931e0 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 cnt==0 );. asse
931f0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
93200 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 E_MUTEX_FAST ||
93210 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
93220 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
93230 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
93240 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 p);.}../*.** The
93250 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
93260 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 nter() and sqlit
93270 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 e3_mutex_try() r
93280 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a outines attempt.
93290 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 ** to enter a mu
932a0 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 tex. If another
932b0 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 thread is alrea
932c0 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 dy within the mu
932d0 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f tex,.** sqlite3_
932e0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 mutex_enter() wi
932f0 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c ll block and sql
93300 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
93310 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 will return.**
93320 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 SQLITE_BUSY. Th
93330 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
93340 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 try() interface
93350 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
93360 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 K.** upon succes
93370 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 sful entry. Mut
93380 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 exes created usi
93390 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ng SQLITE_MUTEX_
933a0 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a RECURSIVE can.**
933b0 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 be entered mult
933c0 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 iple times by th
933d0 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
933e0 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 In such cases th
933f0 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 e,.** mutex must
93400 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 be exited an eq
93410 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 ual number of ti
93420 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 mes before anoth
93430 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e er thread.** can
93440 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 enter. If the
93450 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 same thread trie
93460 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f s to enter any o
93470 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 ther kind of mut
93480 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 ex.** more than
93490 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 once, the behavi
934a0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
934b0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
934c0 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 28 debugMutexEnter(
934d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
934e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
934f0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
93500 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65 _RECURSIVE || de
93510 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 bugMutexNotheld(
93520 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b p) );. p->cnt++
93530 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 ;.}.static int d
93540 65 62 75 67 4d 75 74 65 78 54 72 79 28 73 71 6c ebugMutexTry(sql
93550 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
93560 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
93570 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
93580 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 CURSIVE || debug
93590 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
935a0 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 );. p->cnt++;.
935b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
935c0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
935d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
935e0 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 ave() routine ex
935f0 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 its a mutex that
93600 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
93610 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 ly entered by th
93620 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
93630 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 The behavior.**
93640 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 is undefined if
93650 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 the mutex is not
93660 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
93670 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 ed or.** is not
93680 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 currently alloca
93690 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ted. SQLite wil
936a0 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 l never do eithe
936b0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
936c0 64 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 d debugMutexLeav
936d0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
936e0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 *p){. assert( d
936f0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 70 29 ebugMutexHeld(p)
93700 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a );. p->cnt--;.
93710 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
93720 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
93730 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 CURSIVE || debug
93740 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
93750 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
93760 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
93770 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
93780 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
93790 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
937a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
937b0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
937c0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 49 . debugMutexI
937d0 6e 69 74 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 nit,. debugMu
937e0 74 65 78 45 6e 64 2c 0a 20 20 20 20 64 65 62 75 texEnd,. debu
937f0 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 gMutexAlloc,.
93800 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65 2c debugMutexFree,
93810 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 . debugMutexE
93820 6e 74 65 72 2c 0a 20 20 20 20 64 65 62 75 67 4d nter,. debugM
93830 75 74 65 78 54 72 79 2c 0a 20 20 20 20 64 65 62 utexTry,. deb
93840 75 67 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 ugMutexLeave,..
93850 20 20 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c debugMutexHel
93860 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 d,. debugMute
93870 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 xNotheld. };..
93880 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b return &sMutex;
93890 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 .}.#endif /* def
938a0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
938b0 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e X_NOOP) && defin
938c0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
938d0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
938e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 **** End of mute
938f0 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a x_noop.c *******
93900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93920 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
93930 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
93940 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a mutex_os2.c ****
93950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93970 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
93980 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 August 28.**.**
93990 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
939a0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
939b0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
939c0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
939d0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
939e0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
939f0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
93a00 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
93a10 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
93a20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
93a30 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
93a40 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
93a50 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
93a60 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
93a70 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
93a80 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
93a90 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
93aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93ae0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
93af0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
93b00 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
93b10 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 hat implement mu
93b20 74 65 78 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a texes for OS/2.*
93b30 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 /../*.** The cod
93b40 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
93b50 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53 s only used if S
93b60 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 QLITE_MUTEX_OS2
93b70 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 is defined..** S
93b80 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66 ee the mutex.h f
93b90 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e ile for details.
93ba0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
93bb0 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a E_MUTEX_OS2../**
93bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93bd0 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20 **** OS/2 Mutex
93be0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a Implementation *
93bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93c00 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
93c10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
93c20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75 of mutexes is bu
93c30 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53 ilt using the OS
93c40 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a /2 API..*/../*.*
93c50 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 * The mutex obje
93c60 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 ct.** Each recur
93c70 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e sive mutex is an
93c80 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
93c90 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
93ca0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
93cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
93cc0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20 HMTX mutex;
93cd0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e /* Mutex con
93ce0 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 trolling the loc
93cf0 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20 k */. int id;
93d00 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
93d10 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
93d20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a nRef; /*
93d30 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
93d40 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20 ences */. TID
93d50 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 owner; /*
93d60 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 Thread holding t
93d70 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a his mutex */.};.
93d80 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54 .#define OS2_MUT
93d90 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20 EX_INITIALIZER
93da0 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 0,0,0,0../*.**
93db0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 Initialize and d
93dc0 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
93dd0 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e mutex subsystem.
93de0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
93df0 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 s2MutexInit(void
93e00 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
93e10 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e _OK; }.static in
93e20 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f t os2MutexEnd(vo
93e30 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
93e40 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK; }../*.**
93e50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
93e60 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
93e70 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
93e80 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
93e90 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
93ea0 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 to it. If it r
93eb0 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
93ec0 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
93ed0 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
93ee0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a be allocated. .
93ef0 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
93f00 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
93f10 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
93f20 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ror. The argume
93f30 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 nt.** to sqlite3
93f40 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 _mutex_alloc() i
93f50 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
93f60 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
93f70 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
93f80 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
93f90 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 EX_FAST
93fa0 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 0.** <li>
93fb0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
93fc0 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 CURSIVE
93fd0 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 1.** <li> SQLI
93fe0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
93ff0 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a MASTER 2.**
94000 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
94010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 TEX_STATIC_MEM
94020 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 3.** <li>
94030 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
94040 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 TATIC_PRNG
94050 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 4.** </ul>.**.
94060 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
94070 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
94080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
94090 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
940a0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
940b0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
940c0 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
940d0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
940e0 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
940f0 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
94100 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
94110 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
94120 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
94130 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
94140 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
94150 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
94160 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
94170 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
94180 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
94190 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
941a0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
941b0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
941c0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
941d0 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
941e0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
941f0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
94200 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
94210 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
94220 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
94230 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
94240 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
94250 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
94260 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
94270 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
94280 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
94290 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
942a0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
942b0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
942c0 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
942d0 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
942e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
942f0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
94300 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
94310 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
94320 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
94330 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 . Three static
94340 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 mutexes are.** u
94350 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 sed by the curre
94360 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 nt version of SQ
94370 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 Lite. Future ve
94380 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
94390 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 .** may add addi
943a0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 tional static mu
943b0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d texes. Static m
943c0 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 utexes are for i
943d0 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 nternal.** use b
943e0 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 y SQLite only.
943f0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 Applications tha
94400 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 t use SQLite mut
94410 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 exes should.** u
94420 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 se only the dyna
94430 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 mic mutexes retu
94440 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d rned by SQLITE_M
94450 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 UTEX_FAST or.**
94460 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
94470 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f URSIVE..**.** No
94480 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f te that if one o
94490 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 f the dynamic mu
944a0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 tex parameters (
944b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
944c0 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d T.** or SQLITE_M
944d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 UTEX_RECURSIVE)
944e0 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c is used then sql
944f0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
94500 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
94510 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 different mutex
94520 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 on every call.
94530 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 But for the stat
94540 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 ic.** mutex type
94550 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 s, the same mute
94560 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e x is returned on
94570 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 every call that
94580 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 has.** the same
94590 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f type number..*/
945a0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
945b0 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41 mutex *os2MutexA
945c0 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b lloc(int iType){
945d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
945e0 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 *p = NULL;. sw
945f0 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
94600 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
94610 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
94620 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
94630 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
94640 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
94650 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
94660 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
94670 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 f( p ){.
94680 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
94690 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72 if( DosCr
946a0 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c eateMutexSem( 0,
946b0 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46 &p->mutex, 0, F
946c0 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52 ALSE ) != NO_ERR
946d0 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OR ){.
946e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 sqlite3_free( p
946f0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d );. p =
94700 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d NULL;. }
94710 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
94720 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
94730 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
94740 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 static volatile
94750 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b int isInit = 0;
94760 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71 . static sq
94770 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74 lite3_mutex stat
94780 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a icMutexes[] = {.
94790 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
947a0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
947b0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
947c0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
947d0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 ZER, },.
947e0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 { OS2_MUTEX_INIT
947f0 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 IALIZER, },.
94800 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
94810 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
94820 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
94830 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
94840 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
94850 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
94860 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b ZER, },. };
94870 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49 . if ( !isI
94880 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41 nit ){. A
94890 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20 PIRET rc;.
948a0 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 PTIB ptib;.
948b0 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a PPIB ppib;.
948c0 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74 HMTX mut
948d0 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ex;. char
948e0 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20 name[32];.
948f0 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
94900 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69 cks( &ptib, &ppi
94910 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c b );. sql
94920 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73 ite3_snprintf( s
94930 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d izeof(name), nam
94940 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c e, "\\SEM32\\SQL
94950 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20 ITE%04x",.
94960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94970 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c ppib->pib_ul
94980 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77 pid );. w
94990 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b hile( !isInit ){
949a0 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 . mutex
949b0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
949c0 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75 rc = DosCreateMu
949d0 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d texSem( name, &m
949e0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b utex, 0, FALSE);
949f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
94a00 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b c == NO_ERROR ){
94a10 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 . uns
94a20 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 igned int i;.
94a30 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 if( !is
94a40 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
94a50 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30 for( i = 0
94a60 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 ; i < sizeof(sta
94a70 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 ticMutexes)/size
94a80 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 of(staticMutexes
94a90 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 [0]); i++ ){.
94aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 Dos
94ab0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 CreateMutexSem(
94ac0 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 0, &staticMutexe
94ad0 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46 s[i].mutex, 0, F
94ae0 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 ALSE );.
94af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
94b00 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
94b10 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
94b20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43 DosC
94b30 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75 loseMutexSem( mu
94b40 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20 tex );.
94b50 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d }else if( rc ==
94b60 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45 ERROR_DUPLICATE
94b70 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20 _NAME ){.
94b80 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31 DosSleep( 1
94b90 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 );. }e
94ba0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
94bb0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 return p;.
94bc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
94bd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
94be0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e ssert( iType-2 >
94bf0 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 = 0 );. ass
94c00 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 ert( iType-2 < s
94c10 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
94c20 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 xes)/sizeof(stat
94c30 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b icMutexes[0]) );
94c40 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 . p = &stat
94c50 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
94c60 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
94c70 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
94c80 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
94c90 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
94ca0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
94cb0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
94cc0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
94cd0 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 cated mutex..**
94ce0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
94cf0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
94d00 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74 every mutex that
94d10 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a it allocates..*
94d20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
94d30 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 2MutexFree(sqlit
94d40 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94d50 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
94d60 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e n;. assert( p->
94d70 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 nRef==0 );. ass
94d80 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
94d90 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c TE_MUTEX_FAST ||
94da0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
94db0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 UTEX_RECURSIVE )
94dc0 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 ;. DosCloseMute
94dd0 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29 xSem( p->mutex )
94de0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
94df0 28 20 70 20 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 ( p );.}..#ifdef
94e00 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
94e10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
94e20 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 mutex_held() and
94e30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
94e40 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 otheld() routine
94e50 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 are.** intended
94e60 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 for use inside
94e70 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
94e80 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nts..*/.static i
94e90 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 28 nt os2MutexHeld(
94ea0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
94eb0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 ){. TID tid;.
94ec0 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 PID pid;. ULONG
94ed0 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42 ulCount;. PTIB
94ee0 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d ptib;. if( p!=
94ef0 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 0 ) {. DosQue
94f00 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 ryMutexSem(p->mu
94f10 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64 2c tex, &pid, &tid,
94f20 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 &ulCount);. }
94f30 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 65 else {. DosGe
94f40 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 tInfoBlocks(&pti
94f50 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69 b, NULL);. ti
94f60 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 d = ptib->tib_pt
94f70 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b ib2->tib2_ultid;
94f80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d . }. return p=
94f90 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d =0 || (p->nRef!=
94fa0 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 0 && p->owner==t
94fb0 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e id);.}.static in
94fc0 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c t os2MutexNothel
94fd0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
94fe0 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a *p){. TID tid;.
94ff0 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f PID pid;. ULO
95000 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 NG ulCount;. PT
95010 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 IB ptib;. if( p
95020 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 != 0 ) {. Dos
95030 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
95040 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 >mutex, &pid, &t
95050 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 id, &ulCount);.
95060 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f } else {. Do
95070 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 sGetInfoBlocks(&
95080 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 ptib, NULL);.
95090 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 tid = ptib->tib
950a0 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 _ptib2->tib2_ult
950b0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e id;. }. return
950c0 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 p==0 || p->nRef
950d0 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 ==0 || p->owner!
950e0 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a =tid;.}.#endif..
950f0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
95100 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
95110 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
95120 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 x_try() routines
95130 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 attempt.** to e
95140 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 nter a mutex. I
95150 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 f another thread
95160 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 is already with
95170 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a in the mutex,.**
95180 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
95190 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 nter() will bloc
951a0 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 k and sqlite3_mu
951b0 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 tex_try() will r
951c0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
951d0 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 BUSY. The sqlit
951e0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 e3_mutex_try() i
951f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
95200 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 SQLITE_OK.** up
95210 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e on successful en
95220 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 try. Mutexes cr
95230 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 eated using SQLI
95240 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
95250 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 VE can.** be ent
95260 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 ered multiple ti
95270 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 mes by the same
95280 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 thread. In such
95290 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d cases the,.** m
952a0 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 utex must be exi
952b0 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d ted an equal num
952c0 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 ber of times bef
952d0 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 ore another thre
952e0 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e ad.** can enter.
952f0 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 If the same th
95300 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e read tries to en
95310 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 ter any other ki
95320 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d nd of mutex.** m
95330 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 ore than once, t
95340 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
95350 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 ndefined..*/.sta
95360 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 tic void os2Mute
95370 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d xEnter(sqlite3_m
95380 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
95390 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 tid;. PID holde
953a0 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 r1;. ULONG hold
953b0 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 er2;. if( p==0
953c0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
953d0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
953e0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
953f0 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74 E || os2MutexNot
95400 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 44 6f 73 held(p) );. Dos
95410 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d 28 RequestMutexSem(
95420 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4e p->mutex, SEM_IN
95430 44 45 46 49 4e 49 54 45 5f 57 41 49 54 29 3b 0a DEFINITE_WAIT);.
95440 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 DosQueryMutexS
95450 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f em(p->mutex, &ho
95460 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f lder1, &tid, &ho
95470 6c 64 65 72 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e lder2);. p->own
95480 65 72 20 3d 20 74 69 64 3b 0a 20 20 70 2d 3e 6e er = tid;. p->n
95490 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 Ref++;.}.static
954a0 69 6e 74 20 6f 73 32 4d 75 74 65 78 54 72 79 28 int os2MutexTry(
954b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
954c0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 ){. int rc;. T
954d0 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f ID tid;. PID ho
954e0 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 lder1;. ULONG h
954f0 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d older2;. if( p=
95500 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
95510 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 TE_OK;. assert(
95520 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
95530 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
95540 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c | os2MutexNothel
95550 64 28 70 29 20 29 3b 0a 20 20 69 66 28 20 44 6f d(p) );. if( Do
95560 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d sRequestMutexSem
95570 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 (p->mutex, SEM_I
95580 4d 4d 45 44 49 41 54 45 5f 52 45 54 55 52 4e 29 MMEDIATE_RETURN)
95590 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a == NO_ERROR) {.
955a0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 DosQueryMute
955b0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 xSem(p->mutex, &
955c0 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 holder1, &tid, &
955d0 68 6f 6c 64 65 72 32 29 3b 0a 20 20 20 20 70 2d holder2);. p-
955e0 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 >owner = tid;.
955f0 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 p->nRef++;.
95600 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
95610 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
95620 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
95630 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
95640 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
95650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
95660 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 eave() routine e
95670 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 xits a mutex tha
95680 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 t was.** previou
95690 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 sly entered by t
956a0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
956b0 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a The behavior.**
956c0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 is undefined if
956d0 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f the mutex is no
956e0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
956f0 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 red or.** is not
95700 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 currently alloc
95710 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 ated. SQLite wi
95720 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 ll never do eith
95730 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
95740 69 64 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 id os2MutexLeave
95750 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
95760 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
95770 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 PID holder1;.
95780 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 ULONG holder2;.
95790 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
957a0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
957b0 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 44 6f 73 >nRef>0 );. Dos
957c0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
957d0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 >mutex, &holder1
957e0 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 , &tid, &holder2
957f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
95800 6f 77 6e 65 72 3d 3d 74 69 64 20 29 3b 0a 20 20 owner==tid );.
95810 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 p->nRef--;. ass
95820 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
95830 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
95840 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
95850 20 29 3b 0a 20 20 44 6f 73 52 65 6c 65 61 73 65 );. DosRelease
95860 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
95870 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 x);.}..SQLITE_PR
95880 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
95890 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
958a0 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
958b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
958c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
958d0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
958e0 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e {. os2MutexIn
958f0 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 it,. os2Mutex
95900 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 End,. os2Mute
95910 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d xAlloc,. os2M
95920 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73 utexFree,. os
95930 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 2MutexEnter,.
95940 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20 os2MutexTry,.
95950 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c os2MutexLeave,
95960 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
95970 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65 EBUG. os2Mute
95980 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75 xHeld,. os2Mu
95990 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
959a0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
959b0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
959c0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
959d0 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_OS2 */../****
959e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
959f0 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a f mutex_os2.c **
95a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
95a40 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 file mutex_unix
95a50 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
95a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
95a80 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
95a90 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
95aa0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
95ab0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
95ac0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
95ad0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
95ae0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
95af0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
95b00 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
95b10 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
95b20 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
95b30 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
95b40 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
95b50 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
95b60 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
95b70 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
95b80 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
95b90 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
95ba0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
95bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
95bf0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
95c00 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
95c10 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
95c20 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
95c30 70 74 68 72 65 61 64 73 0a 2a 2f 0a 0a 2f 2a 0a pthreads.*/../*.
95c40 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
95c50 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
95c60 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 used if we are
95c70 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64 compiling thread
95c80 73 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e safe.** under un
95c90 69 78 20 77 69 74 68 20 70 74 68 72 65 61 64 73 ix with pthreads
95ca0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
95cb0 74 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 t this implement
95cc0 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 ation requires a
95cd0 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 version of pthr
95ce0 65 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 eads that.** sup
95cf0 70 6f 72 74 73 20 72 65 63 75 72 73 69 76 65 20 ports recursive
95d00 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 mutexes..*/.#ifd
95d10 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
95d20 50 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 PTHREADS..#inclu
95d30 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a de <pthread.h>..
95d40 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 ./*.** Each recu
95d50 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 rsive mutex is a
95d60 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
95d70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
95d80 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
95d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b sqlite3_mutex {
95da0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 . pthread_mutex
95db0 5f 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a _t mutex; /*
95dc0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 Mutex controlli
95dd0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 ng the lock */.
95de0 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 int id;
95df0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
95e00 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 utex type */. i
95e10 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
95e20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
95e30 62 65 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 ber of entrances
95e40 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20 */. pthread_t
95e50 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 owner;
95e60 20 2f 2a 20 54 68 72 65 61 64 20 74 68 61 74 20 /* Thread that
95e70 69 73 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d is within this m
95e80 75 74 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 utex */.#ifdef S
95e90 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e QLITE_DEBUG. in
95ea0 74 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 t trace;
95eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
95ec0 20 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 to trace change
95ed0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 s */.#endif.};.#
95ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
95ef0 55 47 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 UG.#define SQLIT
95f00 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
95f10 49 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d IZER { PTHREAD_M
95f20 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
95f30 2c 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64 , 0, 0, (pthread
95f40 5f 74 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a _t)0, 0 }.#else.
95f50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f #define SQLITE3_
95f60 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
95f70 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 R { PTHREAD_MUTE
95f80 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 X_INITIALIZER, 0
95f90 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 , 0, (pthread_t)
95fa0 30 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0 }.#endif../*.*
95fb0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
95fc0 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 tex_held() and s
95fd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
95fe0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 held() routine a
95ff0 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 re.** intended f
96000 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 or use only insi
96010 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
96020 65 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 ements. On some
96030 20 70 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 platforms,.** t
96040 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 61 here might be ra
96050 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 ce conditions th
96060 61 74 20 63 61 6e 20 63 61 75 73 65 20 74 68 65 at can cause the
96070 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a se routines to.*
96080 2a 20 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 * deliver incorr
96090 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e ect results. In
960a0 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20 particular, if
960b0 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 pthread_equal()
960c0 69 73 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f is.** not an ato
960d0 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 mic operation, t
960e0 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e hen these routin
960f0 65 73 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 es might deliver
96100 79 0a 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 y.** incorrect r
96110 65 73 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 esults. On most
96120 20 70 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 platforms, pthr
96130 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61 ead_equal() is a
96140 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 .** comparison
96150 6f 66 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20 of two integers
96160 61 6e 64 20 69 73 20 74 68 65 72 65 66 6f 72 65 and is therefore
96170 20 61 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 atomic. But we
96180 20 61 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 are.** told tha
96190 74 20 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75 t HPUX is not su
961a0 63 68 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 ch a platform.
961b0 49 66 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 If so, then thes
961c0 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 e routines.** wi
961d0 6c 6c 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f ll not always wo
961e0 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 rk correctly on
961f0 48 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 HPUX..**.** On t
96200 68 6f 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 hose platforms w
96210 68 65 72 65 20 70 74 68 72 65 61 64 5f 65 71 75 here pthread_equ
96220 61 6c 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d al() is not atom
96230 69 63 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 ic, SQLite.** sh
96240 6f 75 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 ould be compiled
96250 20 77 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 without -DSQLIT
96260 45 5f 44 45 42 55 47 20 61 6e 64 20 77 69 74 68 E_DEBUG and with
96270 20 2d 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 -DNDEBUG to.**
96280 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73 make sure no ass
96290 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
962a0 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 are evaluated a
962b0 6e 64 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a nd hence these.*
962c0 2a 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e * routines are n
962d0 65 76 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a ever called..*/.
962e0 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 #if !defined(NDE
962f0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 BUG) || defined(
96300 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 SQLITE_DEBUG).st
96310 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
96320 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 MutexHeld(sqlite
96330 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
96340 65 74 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d eturn (p->nRef!=
96350 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 0 && pthread_equ
96360 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 al(p->owner, pth
96370 72 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d read_self()));.}
96380 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
96390 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 eadMutexNotheld(
963a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
963b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e ){. return p->n
963c0 52 65 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 Ref==0 || pthrea
963d0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 d_equal(p->owner
963e0 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 , pthread_self()
963f0 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a )==0;.}.#endif..
96400 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
96410 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a and deinitializ
96420 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 e the mutex subs
96430 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 ystem..*/.static
96440 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 int pthreadMute
96450 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
96460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
96470 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
96480 65 61 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 eadMutexEnd(void
96490 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
964a0 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK; }../*.** Th
964b0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
964c0 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 alloc() routine
964d0 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a allocates a new.
964e0 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 ** mutex and ret
964f0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
96500 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 o it. If it ret
96510 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 urns NULL.** tha
96520 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d t means that a m
96530 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 utex could not b
96540 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 e allocated. SQ
96550 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 Lite.** will unw
96560 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e ind its stack an
96570 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f d return an erro
96580 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 r. The argument
96590 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d .** to sqlite3_m
965a0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 utex_alloc() is
965b0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 one of these int
965c0 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a eger constants:.
965d0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
965e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
965f0 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _FAST.** <li> S
96600 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
96610 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 RSIVE.** <li> S
96620 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
96630 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 IC_MASTER.** <li
96640 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
96650 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c STATIC_MEM.** <l
96660 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
96670 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 _STATIC_MEM2.**
96680 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
96690 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a EX_STATIC_PRNG.*
966a0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
966b0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a UTEX_STATIC_LRU.
966c0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
966d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
966e0 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 2.** </ul>.**.**
966f0 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 The first two c
96700 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 onstants cause s
96710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
96720 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a oc() to create.*
96730 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 * a new mutex.
96740 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 The new mutex is
96750 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 recursive when
96760 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
96770 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 URSIVE.** is use
96780 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 d but not necess
96790 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 arily so when SQ
967a0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
967b0 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 is used..** The
967c0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
967d0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 tion does not ne
967e0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 ed to make a dis
967f0 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 tinction.** betw
96800 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 een SQLITE_MUTEX
96810 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 _RECURSIVE and S
96820 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
96830 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e if it does.** n
96840 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 ot want to. But
96850 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c SQLite will onl
96860 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75 y request a recu
96870 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a rsive mutex in.*
96880 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74 * cases where it
96890 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e really needs on
968a0 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 20 e. If a faster
968b0 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 non-recursive mu
968c0 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 tex.** implement
968d0 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 ation is availab
968e0 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 le on the host p
968f0 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 latform, the mut
96900 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 ex subsystem.**
96910 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 might return suc
96920 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 h a mutex in res
96930 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f ponse to SQLITE_
96940 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a MUTEX_FAST..**.*
96950 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f * The other allo
96960 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 wed parameters t
96970 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
96980 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 alloc() each ret
96990 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
969a0 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 to a static pre
969b0 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 existing mutex.
969c0 20 53 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 Six static mute
969d0 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 xes are.** used
969e0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 by the current v
969f0 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ersion of SQLite
96a00 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f . Future versio
96a10 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 ns of SQLite.**
96a20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e may add addition
96a30 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 al static mutexe
96a40 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 s. Static mutex
96a50 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 es are for inter
96a60 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 nal.** use by SQ
96a70 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c Lite only. Appl
96a80 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 ications that us
96a90 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 e SQLite mutexes
96aa0 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f should.** use o
96ab0 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 nly the dynamic
96ac0 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 mutexes returned
96ad0 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 by SQLITE_MUTEX
96ae0 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 _FAST or.** SQLI
96af0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
96b00 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 VE..**.** Note t
96b10 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 hat if one of th
96b20 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 e dynamic mutex
96b30 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 parameters (SQLI
96b40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a TE_MUTEX_FAST.**
96b50 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 or SQLITE_MUTEX
96b60 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 _RECURSIVE) is u
96b70 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 sed then sqlite3
96b80 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a _mutex_alloc().*
96b90 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 * returns a diff
96ba0 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 erent mutex on e
96bb0 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 very call. But
96bc0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0a for the static .
96bd0 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 ** mutex types,
96be0 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 the same mutex i
96bf0 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 s returned on ev
96c00 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 ery call that ha
96c10 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 s.** the same ty
96c20 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 pe number..*/.st
96c30 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
96c40 65 78 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 ex *pthreadMutex
96c50 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 Alloc(int iType)
96c60 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
96c70 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d e3_mutex staticM
96c80 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 utexes[] = {.
96c90 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 SQLITE3_MUTEX_I
96ca0 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 NITIALIZER,.
96cb0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e SQLITE3_MUTEX_IN
96cc0 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 ITIALIZER,. S
96cd0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
96ce0 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 TIALIZER,. SQ
96cf0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
96d00 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
96d10 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
96d20 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
96d30 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
96d40 4c 49 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c LIZER. };. sql
96d50 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 ite3_mutex *p;.
96d60 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29 switch( iType )
96d70 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
96d80 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96d90 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 E: {. p = s
96da0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
96db0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a ( sizeof(*p) );.
96dc0 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 if( p ){.#
96dd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
96de0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
96df0 5f 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f _MUTEX. /
96e00 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d * If recursive m
96e10 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 utexes are not a
96e20 76 61 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c vailable, we wil
96e30 6c 20 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 l have to.
96e40 20 20 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f ** build our o
96e50 77 6e 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 wn. See below.
96e60 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 */. pthre
96e70 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 ad_mutex_init(&p
96e80 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c ->mutex, 0);.#el
96e90 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 se. /* Us
96ea0 65 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 e a recursive mu
96eb0 74 65 78 20 69 66 20 69 74 20 69 73 20 61 76 61 tex if it is ava
96ec0 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 ilable */.
96ed0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 pthread_mutexa
96ee0 74 74 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 ttr_t recursiveA
96ef0 74 74 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 ttr;. pth
96f00 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 read_mutexattr_i
96f10 6e 69 74 28 26 72 65 63 75 72 73 69 76 65 41 74 nit(&recursiveAt
96f20 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 tr);. pth
96f30 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 read_mutexattr_s
96f40 65 74 74 79 70 65 28 26 72 65 63 75 72 73 69 76 ettype(&recursiv
96f50 65 41 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d eAttr, PTHREAD_M
96f60 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b UTEX_RECURSIVE);
96f70 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96f80 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e _mutex_init(&p->
96f90 6d 75 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 mutex, &recursiv
96fa0 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 eAttr);.
96fb0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 pthread_mutexatt
96fc0 72 5f 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 r_destroy(&recur
96fd0 73 69 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 siveAttr);.#endi
96fe0 66 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 f. p->id
96ff0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d = iType;. }
97000 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
97010 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
97020 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 ITE_MUTEX_FAST:
97030 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 {. p = sqli
97040 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
97050 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 izeof(*p) );.
97060 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
97070 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
97080 65 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 e;. pthre
97090 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 ad_mutex_init(&p
970a0 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 ->mutex, 0);.
970b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
970c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
970d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
970e0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 ert( iType-2 >=
970f0 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
97100 74 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 t( iType-2 < Arr
97110 61 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 aySize(staticMut
97120 65 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 exes) );. p
97130 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 = &staticMutexe
97140 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 s[iType-2];.
97150 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
97160 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
97170 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
97180 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 p;.}.../*.** Th
97190 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c is routine deall
971a0 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 ocates a previou
971b0 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 sly.** allocated
971c0 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 mutex. SQLite
971d0 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 is careful to de
971e0 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a allocate every.*
971f0 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 * mutex that it
97200 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 allocates..*/.st
97210 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 atic void pthrea
97220 64 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 dMutexFree(sqlit
97230 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
97240 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
97250 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
97260 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
97270 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 TEX_FAST || p->i
97280 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
97290 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 RECURSIVE );. p
972a0 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 thread_mutex_des
972b0 74 72 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b troy(&p->mutex);
972c0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
972d0 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 p);.}../*.** The
972e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
972f0 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 nter() and sqlit
97300 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 e3_mutex_try() r
97310 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a outines attempt.
97320 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 ** to enter a mu
97330 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 tex. If another
97340 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 thread is alrea
97350 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 dy within the mu
97360 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f tex,.** sqlite3_
97370 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 mutex_enter() wi
97380 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c ll block and sql
97390 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
973a0 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 will return.**
973b0 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 SQLITE_BUSY. Th
973c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
973d0 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 try() interface
973e0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
973f0 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 K.** upon succes
97400 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 sful entry. Mut
97410 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 exes created usi
97420 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ng SQLITE_MUTEX_
97430 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a RECURSIVE can.**
97440 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 be entered mult
97450 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 iple times by th
97460 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
97470 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 In such cases th
97480 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 e,.** mutex must
97490 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 be exited an eq
974a0 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 ual number of ti
974b0 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 mes before anoth
974c0 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e er thread.** can
974d0 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 enter. If the
974e0 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 same thread trie
974f0 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f s to enter any o
97500 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 ther kind of mut
97510 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 ex.** more than
97520 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 once, the behavi
97530 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
97540 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
97550 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 pthreadMutexEnte
97560 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
97570 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
97580 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
97590 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 EX_RECURSIVE ||
975a0 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 pthreadMutexNoth
975b0 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 eld(p) );..#ifde
975c0 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f f SQLITE_HOMEGRO
975d0 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 WN_RECURSIVE_MUT
975e0 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 EX. /* If recur
975f0 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 sive mutexes are
97600 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 not available,
97610 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 then we have to
97620 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 grow. ** our ow
97630 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 n. This impleme
97640 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 ntation assumes
97650 74 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 that pthread_equ
97660 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f al(). ** is ato
97670 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 mic - that it ca
97680 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 nnot be deceived
97690 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 into thinking s
976a0 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e elf. ** and p->
976b0 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 owner are equal
976c0 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e if p->owner chan
976d0 67 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 ges between two
976e0 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 values. ** that
976f0 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 are not equal t
97700 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 o self while the
97710 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 comparison is t
97720 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a aking place.. *
97730 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
97740 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d ation also assum
97750 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 es a coherent ca
97760 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a che - that . **
97770 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 separate proces
97780 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 ses cannot read
97790 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 different values
977a0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 from the same.
977b0 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 ** address at t
977c0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 he same time. I
977d0 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 f either of thes
977e0 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 e two conditions
977f0 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 . ** are not me
97800 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 t, then the mute
97810 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e xes will fail an
97820 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 d problems will
97830 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b result.. */. {
97840 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 . pthread_t s
97850 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 elf = pthread_se
97860 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d lf();. if( p-
97870 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 >nRef>0 && pthre
97880 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 ad_equal(p->owne
97890 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 r, self) ){.
978a0 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 p->nRef++;.
978b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 }else{. pt
978c0 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b hread_mutex_lock
978d0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 (&p->mutex);.
978e0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 assert( p->nR
978f0 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 ef==0 );. p
97900 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a ->owner = self;.
97910 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 p->nRef =
97920 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 1;. }. }.#el
97930 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 se. /* Use the
97940 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 built-in recursi
97950 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 ve mutexes if th
97960 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ey are available
97970 2e 0a 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 .. */. pthread
97980 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e _mutex_lock(&p->
97990 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e mutex);. p->own
979a0 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c er = pthread_sel
979b0 66 28 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b f();. p->nRef++
979c0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
979d0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
979e0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a if( p->trace ){.
979f0 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 printf("ente
97a00 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 r mutex %p (%d)
97a10 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
97a20 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
97a30 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
97a40 69 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 if.}.static int
97a50 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 pthreadMutexTry(
97a60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
97a70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
97a80 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
97a90 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
97aa0 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d SIVE || pthreadM
97ab0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
97ac0 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
97ad0 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 _HOMEGROWN_RECUR
97ae0 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 SIVE_MUTEX. /*
97af0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 If recursive mut
97b00 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 exes are not ava
97b10 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 ilable, then we
97b20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a have to grow. *
97b30 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 * our own. This
97b40 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
97b50 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 assumes that pth
97b60 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a read_equal(). *
97b70 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 * is atomic - th
97b80 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 at it cannot be
97b90 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 deceived into th
97ba0 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a inking self. **
97bb0 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 and p->owner ar
97bc0 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 e equal if p->ow
97bd0 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 ner changes betw
97be0 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 een two values.
97bf0 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 ** that are not
97c00 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 equal to self w
97c10 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 hile the compari
97c20 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c son is taking pl
97c30 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 ace.. ** This i
97c40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c mplementation al
97c50 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 so assumes a coh
97c60 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 erent cache - th
97c70 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 at . ** separat
97c80 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e e processes cann
97c90 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e ot read differen
97ca0 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 t values from th
97cb0 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 e same. ** addr
97cc0 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ess at the same
97cd0 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 time. If either
97ce0 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f of these two co
97cf0 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 nditions. ** ar
97d00 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 e not met, then
97d10 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c the mutexes will
97d20 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 fail and proble
97d30 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a ms will result..
97d40 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 */. {. pth
97d50 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 read_t self = pt
97d60 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
97d70 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 if( p->nRef>0
97d80 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c && pthread_equal
97d90 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 (p->owner, self)
97da0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 ){. p->nRe
97db0 66 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 f++;. rc =
97dc0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
97dd0 65 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64 else if( pthread
97de0 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 _mutex_trylock(&
97df0 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a p->mutex)==0 ){.
97e00 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
97e10 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 >nRef==0 );.
97e20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c p->owner = sel
97e30 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 f;. p->nRef
97e40 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d = 1;. rc =
97e50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
97e60 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
97e70 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
97e80 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 }. }.#else.
97e90 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c /* Use the buil
97ea0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d t-in recursive m
97eb0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 utexes if they a
97ec0 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 re available..
97ed0 2a 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 */. if( pthread
97ee0 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 _mutex_trylock(&
97ef0 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a p->mutex)==0 ){.
97f00 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 p->owner = p
97f10 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 thread_self();.
97f20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 p->nRef++;.
97f30 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
97f40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
97f50 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
97f60 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 . }.#endif..#if
97f70 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
97f80 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
97f90 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 E_OK && p->trace
97fa0 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 ){. printf("
97fb0 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 enter mutex %p (
97fc0 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 %d) with nRef=%d
97fd0 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 \n", p, p->trace
97fe0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a , p->nRef);. }.
97ff0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
98000 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
98010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
98020 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 eave() routine e
98030 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 xits a mutex tha
98040 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 t was.** previou
98050 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 sly entered by t
98060 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
98070 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a The behavior.**
98080 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 is undefined if
98090 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f the mutex is no
980a0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
980b0 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 red or.** is not
980c0 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 currently alloc
980d0 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 ated. SQLite wi
980e0 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 ll never do eith
980f0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
98100 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c id pthreadMutexL
98110 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
98120 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
98130 28 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 ( pthreadMutexHe
98140 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 ld(p) );. p->nR
98150 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 ef--;. assert(
98160 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
98170 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
98180 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a X_RECURSIVE );..
98190 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f #ifdef SQLITE_HO
981a0 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 MEGROWN_RECURSIV
981b0 45 5f 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d E_MUTEX. if( p-
981c0 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
981d0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e pthread_mutex_un
981e0 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b lock(&p->mutex);
981f0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 . }.#else. pth
98200 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 read_mutex_unloc
98210 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 k(&p->mutex);.#e
98220 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
98230 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 ITE_DEBUG. if(
98240 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 p->trace ){.
98250 70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 printf("leave mu
98260 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 tex %p (%d) with
98270 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 nRef=%d\n", p,
98280 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 p->trace, p->nRe
98290 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d f);. }.#endif.}
982a0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
982b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
982c0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 ethods *sqlite3D
982d0 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
982e0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
982f0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
98300 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
98310 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 pthreadMutexIni
98320 74 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 t,. pthreadMu
98330 74 65 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 texEnd,. pthr
98340 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 eadMutexAlloc,.
98350 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 pthreadMutexF
98360 72 65 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64 ree,. pthread
98370 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
98380 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c pthreadMutexTry,
98390 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
983a0 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 xLeave,.#ifdef S
983b0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
983c0 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 pthreadMutexHeld
983d0 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 ,. pthreadMut
983e0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a exNotheld.#else.
983f0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 0,. 0.#en
98400 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 dif. };.. retu
98410 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 rn &sMutex;.}..#
98420 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
98430 4d 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f MUTEX_PTHREAD */
98440 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
98450 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 * End of mutex_u
98460 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nix.c **********
98470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98490 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
984a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
984b0 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a ex_w32.c *******
984c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
984d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
984e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
984f0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 14.**.** The
98500 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
98510 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
98520 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
98530 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
98540 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
98550 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
98560 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
98570 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
98580 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
98590 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
985a0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
985b0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
985c0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
985d0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
985e0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
985f0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
98600 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
98610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98650 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
98660 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
98670 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
98680 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
98690 65 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2f 0a es for win32.*/.
986a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 ./*.** The code
986b0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 in this file is
986c0 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 only used if we
986d0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 are compiling mu
986e0 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f ltithreaded.** o
986f0 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74 65 6d n a win32 system
98700 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
98710 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a TE_MUTEX_W32../*
98720 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 .** Each recursi
98730 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 ve mutex is an i
98740 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
98750 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
98760 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 re..*/.struct sq
98770 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
98780 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e CRITICAL_SECTION
98790 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75 mutex; /* Mu
987a0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 tex controlling
987b0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e the lock */. in
987c0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 t id;
987d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 /* Mute
987e0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 x type */. int
987f0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
98800 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
98810 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73 20 2a of enterances *
98820 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b /. DWORD owner;
98830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
98840 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 * Thread holding
98850 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d this mutex */.}
98860 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ;../*.** Return
98870 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 true (non-zero)
98880 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e if we are runnin
98890 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 g under WinNT, W
988a0 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 in2K, WinXP,.**
988b0 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72 or WinCE. Retur
988c0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 n false (zero) f
988d0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c or Win95, Win98,
988e0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a or WinME..**.**
988f0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 Here is an inte
98900 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 resting observat
98910 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e ion: Win95, Win
98920 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 98, and WinME la
98930 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 ck.** the LockFi
98940 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 leEx() API. But
98950 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 we can still st
98960 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 atically link ag
98970 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 ainst that.** AP
98980 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 I as long as we
98990 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69 don't call it wi
989a0 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f n running Win95/
989b0 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 98/ME. A call t
989c0 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e o.** this routin
989d0 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 e is used to det
989e0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f ermine if the ho
989f0 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d st is Win95/98/M
98a00 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b E or.** WinNT/2K
98a10 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 /XP so that we w
98a20 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 ill know whether
98a30 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 or not we can s
98a40 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 afely call.** th
98a50 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 e LockFileEx() A
98a60 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 PI..**.** mutexI
98a70 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20 75 73 sNT() is only us
98a80 65 64 20 66 6f 72 20 74 68 65 20 54 72 79 45 6e ed for the TryEn
98a90 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
98aa0 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a on() API call,.*
98ab0 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c 79 20 * which is only
98ac0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79 6f 75 available if you
98ad0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 61 r application wa
98ae0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
98af0 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 .** _WIN32_WINNT
98b00 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20 76 61 defined to a va
98b10 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e 20 20 lue >= 0x0400.
98b20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f Currently, the o
98b30 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 nly.** call to T
98b40 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 ryEnterCriticalS
98b50 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69 66 64 ection() is #ifd
98b60 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20 23 69 ef'ed out, so #i
98b70 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75 fdef .** this ou
98b80 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 t as well..*/.#i
98b90 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f f 0.#if SQLITE_O
98ba0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
98bb0 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20 28 31 mutexIsNT() (1
98bc0 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 ).#else. static
98bd0 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54 28 76 int mutexIsNT(v
98be0 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74 69 63 oid){. static
98bf0 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20 30 3b int osType = 0;
98c00 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70 65 3d . if( osType=
98c10 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 =0 ){. OSVE
98c20 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b RSIONINFO sInfo;
98c30 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f . sInfo.dwO
98c40 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 SVersionInfoSize
98c50 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 = sizeof(sInfo)
98c60 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 ;. GetVersi
98c70 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 onEx(&sInfo);.
98c80 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73 49 6e osType = sIn
98c90 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d fo.dwPlatformId=
98ca0 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 =VER_PLATFORM_WI
98cb0 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a N32_NT ? 2 : 1;.
98cc0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
98cd0 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a osType==2;. }.
98ce0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
98cf0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e _OS_WINCE */.#en
98d00 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
98d10 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
98d20 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
98d30 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
98d40 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
98d50 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
98d60 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
98d70 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 use only inside
98d80 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
98d90 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nts..*/.static i
98da0 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 28 nt winMutexHeld(
98db0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
98dc0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e ){. return p->n
98dd0 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e Ref!=0 && p->own
98de0 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 er==GetCurrentTh
98df0 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74 readId();.}.stat
98e00 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e ic int winMutexN
98e10 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
98e20 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
98e30 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c rn p->nRef==0 ||
98e40 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75 p->owner!=GetCu
98e50 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b rrentThreadId();
98e60 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
98e70 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
98e80 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
98e90 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
98ea0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c m..*/.static sql
98eb0 69 74 65 33 5f 6d 75 74 65 78 20 77 69 6e 4d 75 ite3_mutex winMu
98ec0 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 tex_staticMutexe
98ed0 73 5b 36 5d 3b 0a 73 74 61 74 69 63 20 69 6e 74 s[6];.static int
98ee0 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 winMutex_isInit
98ef0 20 3d 20 30 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d = 0;./* As winM
98f00 75 74 65 78 49 6e 69 74 28 29 20 61 6e 64 20 77 utexInit() and w
98f10 69 6e 4d 75 74 65 78 45 6e 64 28 29 20 61 72 65 inMutexEnd() are
98f20 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 0a called as part.
98f30 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ** of the sqlite
98f40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 3_initialize and
98f50 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
98f60 6e 28 29 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e n().** processin
98f70 67 2c 20 74 68 65 20 22 69 6e 74 65 72 6c 6f 63 g, the "interloc
98f80 6b 65 64 22 20 6d 61 67 69 63 20 69 73 20 70 72 ked" magic is pr
98f90 6f 62 61 62 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 obably not.** st
98fa0 72 69 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79 rictly necessary
98fb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 6c 6f 6e 67 ..*/.static long
98fc0 20 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d winMutex_lock =
98fd0 20 30 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 0;..static int
98fe0 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 winMutexInit(voi
98ff0 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20 66 69 d){ . /* The fi
99000 72 73 74 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 rst to increment
99010 20 74 6f 20 31 20 64 6f 65 73 20 61 63 74 75 61 to 1 does actua
99020 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e l initialization
99030 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c */. if( Interl
99040 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 ockedCompareExch
99050 61 6e 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c ange(&winMutex_l
99060 6f 63 6b 2c 20 31 2c 20 30 29 3d 3d 30 20 29 7b ock, 1, 0)==0 ){
99070 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
99080 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 for(i=0; i<Array
99090 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 Size(winMutex_st
990a0 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b aticMutexes); i+
990b0 2b 29 7b 0a 20 20 20 20 20 20 49 6e 69 74 69 61 +){. Initia
990c0 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 lizeCriticalSect
990d0 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 ion(&winMutex_st
990e0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d aticMutexes[i].m
990f0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 utex);. }.
99100 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 winMutex_isInit
99110 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
99120 20 20 20 2f 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c /* Someone el
99130 73 65 20 69 73 20 69 6e 20 74 68 65 20 70 72 6f se is in the pro
99140 63 65 73 73 20 6f 66 20 69 6e 69 74 69 6e 67 20 cess of initing
99150 74 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 the static mutex
99160 65 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 es */. while(
99170 20 21 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 !winMutex_isIni
99180 74 20 29 7b 0a 20 20 20 20 20 20 53 6c 65 65 70 t ){. Sleep
99190 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 (1);. }. }.
991a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
991b0 4b 3b 20 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e K; .}..static in
991c0 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f t winMutexEnd(vo
991d0 69 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20 66 id){ . /* The f
991e0 69 72 73 74 20 74 6f 20 64 65 63 72 65 6d 65 6e irst to decremen
991f0 74 20 74 6f 20 30 20 64 6f 65 73 20 61 63 74 75 t to 0 does actu
99200 61 6c 20 73 68 75 74 64 6f 77 6e 20 0a 20 20 2a al shutdown . *
99210 2a 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64 20 * (which should
99220 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f 20 73 be the last to s
99230 68 75 74 64 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69 hutdown.) */. i
99240 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 6f f( InterlockedCo
99250 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 77 mpareExchange(&w
99260 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c inMutex_lock, 0,
99270 20 31 29 3d 3d 31 20 29 7b 0a 20 20 20 20 69 66 1)==1 ){. if
99280 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 ( winMutex_isIni
99290 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e t==1 ){. in
992a0 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 t i;. for(i
992b0 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 =0; i<ArraySize(
992c0 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d winMutex_staticM
992d0 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 utexes); i++){.
992e0 20 20 20 20 20 20 20 44 65 6c 65 74 65 43 72 69 DeleteCri
992f0 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77 69 ticalSection(&wi
99300 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 nMutex_staticMut
99310 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a exes[i].mutex);.
99320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 69 }. wi
99330 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 nMutex_isInit =
99340 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
99350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
99360 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 .}../*.** The s
99370 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
99380 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c oc() routine all
99390 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 ocates a new.**
993a0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e mutex and return
993b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
993c0 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e t. If it return
993d0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d s NULL.** that m
993e0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 eans that a mute
993f0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 x could not be a
99400 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
99410 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 e.** will unwind
99420 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 its stack and r
99430 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 eturn an error.
99440 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a The argument.**
99450 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
99460 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 x_alloc() is one
99470 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
99480 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a r constants:.**.
99490 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
994a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
994b0 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ST.** <li> SQLI
994c0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
994d0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 VE.** <li> SQLI
994e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
994f0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 MASTER.** <li>
99500 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
99510 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 TIC_MEM.** <li>
99520 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
99530 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 ATIC_MEM2.** <li
99540 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
99550 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c STATIC_PRNG.** <
99560 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
99570 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 X_STATIC_LRU.**
99580 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
99590 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a EX_STATIC_LRU2.*
995a0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
995b0 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 e first two cons
995c0 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 tants cause sqli
995d0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
995e0 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 ) to create.** a
995f0 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 new mutex. The
99600 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 new mutex is re
99610 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c cursive when SQL
99620 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
99630 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 IVE.** is used b
99640 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 ut not necessari
99650 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 ly so when SQLIT
99660 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 E_MUTEX_FAST is
99670 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 used..** The mut
99680 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
99690 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
996a0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
996b0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
996c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
996d0 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
996e0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
996f0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
99700 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 want to. But SQ
99710 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 Lite will only r
99720 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 equest a recursi
99730 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 ve mutex in.** c
99740 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 ases where it re
99750 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 ally needs one.
99760 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
99770 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
99780 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
99790 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
997a0 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
997b0 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
997c0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
997d0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
997e0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
997f0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
99800 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 EX_FAST..**.** T
99810 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 he other allowed
99820 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 parameters to s
99830 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
99840 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e oc() each return
99850 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
99860 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 a static preexi
99870 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 sting mutex. Si
99880 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 x static mutexes
99890 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
998a0 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
998b0 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
998c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
998d0 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
998e0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
998f0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
99900 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
99910 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
99920 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
99930 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
99940 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
99950 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
99960 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
99970 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
99980 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
99990 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
999a0 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
999b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
999c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
999d0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 if one of the d
999e0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 ynamic mutex par
999f0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f ameters (SQLITE_
99a00 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 MUTEX_FAST.** or
99a10 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
99a20 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 CURSIVE) is used
99a30 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 then sqlite3_mu
99a40 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 tex_alloc().** r
99a50 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 eturns a differe
99a60 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 nt mutex on ever
99a70 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 y call. But for
99a80 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 the static .**
99a90 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
99aa0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
99ab0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
99ac0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
99ad0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
99ae0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
99af0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
99b00 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28 69 *winMutexAlloc(i
99b10 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c nt iType){. sql
99b20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a ite3_mutex *p;..
99b30 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20 switch( iType
99b40 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
99b50 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 TE_MUTEX_FAST:.
99b60 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
99b70 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 UTEX_RECURSIVE:
99b80 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 {. p = sqli
99b90 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
99ba0 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 izeof(*p) );.
99bb0 20 20 20 69 66 28 20 70 20 29 7b 20 20 0a 20 20 if( p ){ .
99bc0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
99bd0 79 70 65 3b 0a 20 20 20 20 20 20 20 20 49 6e 69 ype;. Ini
99be0 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 tializeCriticalS
99bf0 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 ection(&p->mutex
99c00 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
99c10 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
99c20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
99c30 20 20 20 61 73 73 65 72 74 28 20 77 69 6e 4d 75 assert( winMu
99c40 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29 3b tex_isInit==1 );
99c50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
99c60 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 Type-2 >= 0 );.
99c70 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 assert( iTy
99c80 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65 pe-2 < ArraySize
99c90 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 (winMutex_static
99ca0 4d 75 74 65 78 65 73 29 20 29 3b 0a 20 20 20 20 Mutexes) );.
99cb0 20 20 70 20 3d 20 26 77 69 6e 4d 75 74 65 78 5f p = &winMutex_
99cc0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 staticMutexes[iT
99cd0 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d ype-2];. p-
99ce0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 >id = iType;.
99cf0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
99d00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
99d10 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 }.../*.** This r
99d20 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 outine deallocat
99d30 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a es a previously.
99d40 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 ** allocated mut
99d50 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 ex. SQLite is c
99d60 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f areful to deallo
99d70 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 cate every.** mu
99d80 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f tex that it allo
99d90 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 cates..*/.static
99da0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 void winMutexFr
99db0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ee(sqlite3_mutex
99dc0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
99dd0 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p );. assert( p
99de0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
99df0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
99e00 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
99e10 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
99e20 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
99e30 20 29 3b 0a 20 20 44 65 6c 65 74 65 43 72 69 74 );. DeleteCrit
99e40 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
99e50 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
99e60 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
99e70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
99e80 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
99e90 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
99ea0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
99eb0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
99ec0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 er a mutex. If
99ed0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
99ee0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
99ef0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
99f00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
99f10 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
99f20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
99f30 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
99f40 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
99f50 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 SY. The sqlite3
99f60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
99f70 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 erface returns S
99f80 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e QLITE_OK.** upon
99f90 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
99fa0 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 y. Mutexes crea
99fb0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 ted using SQLITE
99fc0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
99fd0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 can.** be enter
99fe0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
99ff0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
9a000 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 read. In such c
9a010 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
9a020 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
9a030 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
9a040 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
9a050 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
9a060 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 .** can enter.
9a070 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
9a080 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
9a090 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
9a0a0 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 of mutex.** mor
9a0b0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
9a0c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
9a0d0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 efined..*/.stati
9a0e0 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 45 c void winMutexE
9a0f0 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
9a100 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
9a110 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
9a120 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
9a130 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 || winMutexNothe
9a140 6c 64 28 70 29 20 29 3b 0a 20 20 45 6e 74 65 72 ld(p) );. Enter
9a150 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
9a160 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d &p->mutex);. p-
9a170 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 >owner = GetCurr
9a180 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a entThreadId(); .
9a190 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 p->nRef++;.}.s
9a1a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 tatic int winMut
9a1b0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 exTry(sqlite3_mu
9a1c0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 tex *p){. int r
9a1d0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
9a1e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
9a1f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
9a200 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d ECURSIVE || winM
9a210 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
9a220 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 ;. /*. ** The
9a230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
9a240 79 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 76 y() routine is v
9a250 65 72 79 20 72 61 72 65 6c 79 20 75 73 65 64 2c ery rarely used,
9a260 20 61 6e 64 20 77 68 65 6e 20 69 74 0a 20 20 2a and when it. *
9a270 2a 20 69 73 20 75 73 65 64 20 69 74 20 69 73 20 * is used it is
9a280 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74 69 6d 69 merely an optimi
9a290 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74 20 69 zation. So it i
9a2a0 73 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f 20 61 s OK for it to a
9a2b0 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61 69 6c 2e lways. ** fail.
9a2c0 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 . **. ** The
9a2d0 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 TryEnterCritica
9a2e0 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 65 72 lSection() inter
9a2f0 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 face is only ava
9a300 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e ilable on WinNT.
9a310 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 20 77 . ** And some w
9a320 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 72 73 indows compilers
9a330 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 6f 75 complain if you
9a340 20 74 72 79 20 74 6f 20 75 73 65 20 69 74 20 77 try to use it w
9a350 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69 72 73 ithout. ** firs
9a360 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23 64 65 t doing some #de
9a370 66 69 6e 65 73 20 74 68 61 74 20 70 72 65 76 65 fines that preve
9a380 6e 74 20 53 51 4c 69 74 65 20 66 72 6f 6d 20 62 nt SQLite from b
9a390 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e 39 38 uilding on Win98
9a3a0 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61 74 20 .. ** For that
9a3b0 72 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 reason, we will
9a3c0 6f 6d 69 74 20 74 68 69 73 20 6f 70 74 69 6d 69 omit this optimi
9a3d0 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 zation for now.
9a3e0 20 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b 65 74 See. ** ticket
9a3f0 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 #2685.. */.#if
9a400 20 30 0a 20 20 69 66 28 20 6d 75 74 65 78 49 73 0. if( mutexIs
9a410 4e 54 28 29 20 26 26 20 54 72 79 45 6e 74 65 72 NT() && TryEnter
9a420 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
9a430 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 &p->mutex) ){.
9a440 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74 p->owner = Get
9a450 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 CurrentThreadId(
9a460 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b );. p->nRef++
9a470 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
9a480 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a E_OK;. }.#else.
9a490 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
9a4a0 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ER(p);.#endif.
9a4b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
9a4c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
9a4d0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
9a4e0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
9a4f0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
9a500 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
9a510 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
9a520 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 hread. The beha
9a530 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
9a540 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
9a550 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
9a560 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a ly entered or.**
9a570 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
9a580 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 y allocated. SQ
9a590 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
9a5a0 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 do either..*/.st
9a5b0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 atic void winMut
9a5c0 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f exLeave(sqlite3_
9a5d0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
9a5e0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
9a5f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f ;. assert( p->o
9a600 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 wner==GetCurrent
9a610 54 68 72 65 61 64 49 64 28 29 20 29 3b 0a 20 20 ThreadId() );.
9a620 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 p->nRef--;. ass
9a630 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
9a640 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
9a650 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
9a660 20 29 3b 0a 20 20 4c 65 61 76 65 43 72 69 74 69 );. LeaveCriti
9a670 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d calSection(&p->m
9a680 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 utex);.}..SQLITE
9a690 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
9a6a0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
9a6b0 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 sqlite3DefaultMu
9a6c0 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 tex(void){. sta
9a6d0 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
9a6e0 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 x_methods sMutex
9a6f0 20 3d 20 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 = {. winMute
9a700 78 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 xInit,. winMu
9a710 74 65 78 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d texEnd,. winM
9a720 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 utexAlloc,. w
9a730 69 6e 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 inMutexFree,.
9a740 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a winMutexEnter,.
9a750 20 20 20 20 77 69 6e 4d 75 74 65 78 54 72 79 2c winMutexTry,
9a760 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 . winMutexLea
9a770 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ve,.#ifdef SQLIT
9a780 45 5f 44 45 42 55 47 0a 20 20 20 20 77 69 6e 4d E_DEBUG. winM
9a790 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 utexHeld,. wi
9a7a0 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 nMutexNotheld.#e
9a7b0 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30 lse. 0,. 0
9a7c0 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 .#endif. };..
9a7d0 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a return &sMutex;.
9a7e0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
9a7f0 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 2a 2f 0a TE_MUTEX_W32 */.
9a800 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9a810 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 77 33 End of mutex_w3
9a820 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2.c ************
9a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9a850 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9a860 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 6c 6c Begin file mall
9a870 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a oc.c ***********
9a880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9a8a0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
9a8b0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
9a8c0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
9a8d0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
9a8e0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
9a8f0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
9a900 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
9a910 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
9a920 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
9a930 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
9a940 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
9a950 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
9a960 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
9a970 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
9a980 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
9a990 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
9a9a0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
9a9b0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
9a9c0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
9a9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9aa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9aa10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 *******.**.** Me
9aa20 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9aa30 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 functions used t
9aa40 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 hroughout sqlite
9aa50 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ..*/../*.** This
9aa60 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 routine runs wh
9aa70 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c en the memory al
9aa80 6c 6f 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 locator sees tha
9aa90 74 20 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d t the.** total m
9aaa0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
9aab0 20 69 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 is about to exc
9aac0 65 65 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 eed the soft hea
9aad0 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 p.** limit..*/.s
9aae0 74 61 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 tatic void softH
9aaf0 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 eapLimitEnforcer
9ab00 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 (. void *NotUse
9ab10 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e d, . sqlite3_in
9ab20 74 36 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 t64 NotUsed2,.
9ab30 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b int allocSize.){
9ab40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
9ab50 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
9ab60 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 tUsed2);. sqlit
9ab70 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
9ab80 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a y(allocSize);.}.
9ab90 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 ./*.** Set the s
9aba0 6f 66 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 oft heap-size li
9abb0 6d 69 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 mit for the libr
9abc0 61 72 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a ary. Passing a z
9abd0 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 ero or .** negat
9abe0 69 76 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 ive value indica
9abf0 74 65 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f tes no limit..*/
9ac00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
9ac10 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 sqlite3_soft_he
9ac20 61 70 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b ap_limit(int n){
9ac30 0a 20 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 . sqlite3_uint6
9ac40 34 20 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 4 iLimit;. int
9ac50 6f 76 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e overage;. if( n
9ac60 3c 30 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 <0 ){. iLimit
9ac70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
9ac80 20 20 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 iLimit = n;.
9ac90 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
9aca0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
9acb0 20 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 sqlite3_initia
9acc0 6c 69 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 lize();.#endif.
9acd0 20 69 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b if( iLimit>0 ){
9ace0 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f . sqlite3Memo
9acf0 72 79 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 ryAlarm(softHeap
9ad00 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 LimitEnforcer, 0
9ad10 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c , iLimit);. }el
9ad20 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d se{. sqlite3M
9ad30 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c emoryAlarm(0, 0,
9ad40 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61 0);. }. overa
9ad50 67 65 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74 ge = (int)(sqlit
9ad60 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 e3_memory_used()
9ad70 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66 - (i64)n);. if
9ad80 28 20 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20 ( overage>0 ){.
9ad90 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 sqlite3_relea
9ada0 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67 se_memory(overag
9adb0 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a e);. }.}../*.**
9adc0 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 Attempt to rele
9add0 61 73 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65 ase up to n byte
9ade0 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 s of non-essenti
9adf0 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e al memory curren
9ae00 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 tly.** held by S
9ae10 51 4c 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c QLite. An exampl
9ae20 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 e of non-essenti
9ae30 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d al memory is mem
9ae40 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 ory used to.** c
9ae50 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61 ache database pa
9ae60 67 65 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 ges that are not
9ae70 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 currently in us
9ae80 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
9ae90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c int sqlite3_rel
9aea0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 ease_memory(int
9aeb0 6e 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 n){.#ifdef SQLIT
9aec0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f E_ENABLE_MEMORY_
9aed0 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 MANAGEMENT. int
9aee0 20 6e 52 65 74 20 3d 20 30 3b 0a 20 20 6e 52 65 nRet = 0;. nRe
9aef0 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 t += sqlite3Pcac
9af00 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 heReleaseMemory(
9af10 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 n-nRet);. retur
9af20 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 n nRet;.#else.
9af30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
9af40 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (n);. return SQ
9af50 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a LITE_OK;.#endif.
9af60 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 }../*.** State i
9af70 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c nformation local
9af80 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
9af90 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
9afa0 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 tem..*/.static S
9afb0 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
9afc0 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 Mem0Global {.
9afd0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 /* Number of fre
9afe0 65 20 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 e pages for scra
9aff0 74 63 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 tch and page-cac
9b000 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 he memory */. u
9b010 33 32 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 32 nScratchFree;
9b020 0a 20 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 . u32 nPageFree
9b030 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ;.. sqlite3_mut
9b040 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
9b050 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 /* Mutex to s
9b060 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 erialize access
9b070 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 */.. /*. ** Th
9b080 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b e alarm callback
9b090 20 61 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e and its argumen
9b0a0 74 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 ts. The mem0.mu
9b0b0 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 tex lock will.
9b0c0 2a 2a 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 ** be held while
9b0d0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
9b0e0 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 running. Recur
9b0f0 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a sive calls into.
9b100 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 ** the memory
9b110 73 75 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c subsystem are al
9b120 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 lowed, but no ne
9b130 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c w callbacks will
9b140 20 62 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e be. ** issued.
9b150 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f . */. sqlite3_
9b160 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73 int64 alarmThres
9b170 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 hold;. void (*a
9b180 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f larmCallback)(vo
9b190 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 id*, sqlite3_int
9b1a0 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 64,int);. void
9b1b0 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a *alarmArg;.. /*
9b1c0 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 . ** Pointers t
9b1d0 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c o the end of sql
9b1e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b1f0 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a 20 20 .pScratch and.
9b200 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
9b210 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74 6f 20 Config.pPage to
9b220 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 a block of memor
9b230 79 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a 20 y that records.
9b240 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65 73 20 ** which pages
9b250 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 are available..
9b260 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63 72 61 */. u32 *aScra
9b270 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 2a tchFree;. u32 *
9b280 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d aPageFree;.} mem
9b290 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 0 = { 0, 0, 0, 0
9b2a0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a , 0, 0, 0, 0 };.
9b2b0 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c .#define mem0 GL
9b2c0 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 OBAL(struct Mem0
9b2d0 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f Global, mem0)../
9b2e0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
9b2f0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
9b300 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
9b310 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9b320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 TE int sqlite3Ma
9b330 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a llocInit(void){.
9b340 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9b350 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c balConfig.m.xMal
9b360 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 loc==0 ){. sq
9b370 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
9b380 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 lt();. }. mems
9b390 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a et(&mem0, 0, siz
9b3a0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 eof(mem0));. if
9b3b0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9b3c0 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 onfig.bCoreMutex
9b3d0 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 ){. mem0.mut
9b3e0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
9b3f0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
9b400 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
9b410 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
9b420 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9b430 53 63 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 Scratch && sqlit
9b440 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9b450 7a 53 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 zScratch>=100.
9b460 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c && sqlite3Gl
9b470 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
9b480 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e tch>=0 ){. in
9b490 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 t i;. sqlite3
9b4a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
9b4b0 63 72 61 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f cratch = ROUNDDO
9b4c0 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 WN8(sqlite3Globa
9b4d0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9b4e0 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 h-4);. mem0.a
9b4f0 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 28 75 ScratchFree = (u
9b500 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 32*)&((char*)sql
9b510 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b520 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20 20 20 .pScratch).
9b530 20 20 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 [sq
9b540 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b550 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71 6c 69 g.szScratch*sqli
9b560 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b570 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20 20 66 nScratch];. f
9b580 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 or(i=0; i<sqlite
9b590 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 3GlobalConfig.nS
9b5a0 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 cratch; i++){ me
9b5b0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b m0.aScratchFree[
9b5c0 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 i] = i; }. me
9b5d0 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 m0.nScratchFree
9b5e0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9b5f0 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a onfig.nScratch;.
9b600 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
9b610 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b620 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 .pScratch = 0;.
9b630 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9b640 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9b650 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
9b660 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b670 66 69 67 2e 70 50 61 67 65 20 26 26 20 73 71 6c fig.pPage && sql
9b680 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b690 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20 20 20 .szPage>=512.
9b6a0 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f && sqlite3Glo
9b6b0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e balConfig.nPage>
9b6c0 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b =1 ){. int i;
9b6d0 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68 65 61 . int overhea
9b6e0 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 d;. int sz =
9b6f0 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 ROUNDDOWN8(sqlit
9b700 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9b710 7a 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 74 20 zPage);. int
9b720 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 n = sqlite3Globa
9b730 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 lConfig.nPage;.
9b740 20 20 20 6f 76 65 72 68 65 61 64 20 3d 20 28 34 overhead = (4
9b750 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b *n + sz - 1)/sz;
9b760 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9b770 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d alConfig.nPage -
9b780 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 = overhead;.
9b790 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 20 3d mem0.aPageFree =
9b7a0 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 (u32*)&((char*)
9b7b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b7c0 66 69 67 2e 70 50 61 67 65 29 0a 20 20 20 20 20 fig.pPage).
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 [sq
9b7e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b7f0 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69 74 65 33 g.szPage*sqlite3
9b800 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 GlobalConfig.nPa
9b810 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ge];. for(i=0
9b820 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 ; i<sqlite3Globa
9b830 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69 lConfig.nPage; i
9b840 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46 ++){ mem0.aPageF
9b850 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 ree[i] = i; }.
9b860 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 mem0.nPageFree
9b870 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
9b880 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 Config.nPage;.
9b890 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
9b8a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9b8b0 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 Page = 0;. sq
9b8c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b8d0 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20 g.szPage = 0;.
9b8e0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
9b8f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9b900 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c .xInit(sqlite3Gl
9b910 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 obalConfig.m.pAp
9b920 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pData);.}../*.**
9b930 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 Deinitialize th
9b940 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9b950 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
9b960 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9b970 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c void sqlite3Mal
9b980 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 locEnd(void){.
9b990 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9b9a0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 lConfig.m.xShutd
9b9b0 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 own ){. sqlit
9b9c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9b9d0 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 .xShutdown(sqlit
9b9e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9b9f0 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a .pAppData);. }.
9ba00 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 memset(&mem0,
9ba10 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 0, sizeof(mem0))
9ba20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
9ba30 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 n the amount of
9ba40 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 memory currently
9ba50 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f checked out..*/
9ba60 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
9ba70 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
9ba80 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 3_memory_used(vo
9ba90 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 id){. int n, mx
9baa0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ;. sqlite3_int6
9bab0 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 4 res;. sqlite3
9bac0 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 _status(SQLITE_S
9bad0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9bae0 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a D, &n, &mx, 0);.
9baf0 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33 res = (sqlite3
9bb00 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f _int64)n; /* Wo
9bb10 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e rk around bug in
9bb20 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b Borland C. Tick
9bb30 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 et #3216 */. re
9bb40 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
9bb50 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 ** Return the ma
9bb60 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 ximum amount of
9bb70 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 61 73 20 memory that has
9bb80 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65 ever been.** che
9bb90 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 65 20 65 cked out since e
9bba0 69 74 68 65 72 20 74 68 65 20 62 65 67 69 6e 6e ither the beginn
9bbb0 69 6e 67 20 6f 66 20 74 68 69 73 20 70 72 6f 63 ing of this proc
9bbc0 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 ess.** or since
9bbd0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
9bbe0 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 reset..*/.SQLITE
9bbf0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 _API sqlite3_int
9bc00 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 64 sqlite3_memor
9bc10 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 y_highwater(int
9bc20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e resetFlag){. in
9bc30 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 t n, mx;. sqlit
9bc40 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 e3_int64 res;.
9bc50 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 sqlite3_status(S
9bc60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9bc70 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d ORY_USED, &n, &m
9bc80 78 2c 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 20 x, resetFlag);.
9bc90 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f res = (sqlite3_
9bca0 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f int64)mx; /* Wo
9bcb0 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e rk around bug in
9bcc0 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b Borland C. Tick
9bcd0 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 et #3216 */. re
9bce0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
9bcf0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 61 6c ** Change the al
9bd00 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a arm callback.*/.
9bd10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
9bd20 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 nt sqlite3Memory
9bd30 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 Alarm(. void(*x
9bd40 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a Callback)(void *
9bd50 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e pArg, sqlite3_in
9bd60 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c t64 used,int N),
9bd70 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 . void *pArg,.
9bd80 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 sqlite3_int64 i
9bd90 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 Threshold.){. s
9bda0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9bdb0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9bdc0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c mem0.alarmCall
9bdd0 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b back = xCallback
9bde0 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 ;. mem0.alarmAr
9bdf0 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 g = pArg;. mem0
9be00 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 .alarmThreshold
9be10 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 = iThreshold;.
9be20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
9be30 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
9be40 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
9be50 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 _OK;.}..#ifndef
9be60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 SQLITE_OMIT_DEPR
9be70 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 ECATED./*.** Dep
9be80 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c recated external
9be90 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 interface. Int
9bea0 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 ernal/core SQLit
9beb0 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 e code.** should
9bec0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d call sqlite3Mem
9bed0 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c oryAlarm..*/.SQL
9bee0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
9bef0 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d te3_memory_alarm
9bf00 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 (. void(*xCallb
9bf10 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c ack)(void *pArg,
9bf20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 sqlite3_int64 u
9bf30 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f sed,int N),. vo
9bf40 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 id *pArg,. sqli
9bf50 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 te3_int64 iThres
9bf60 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e hold.){. return
9bf70 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c sqlite3MemoryAl
9bf80 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 arm(xCallback, p
9bf90 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 Arg, iThreshold)
9bfa0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
9bfb0 2a 20 54 72 69 67 67 65 72 20 74 68 65 20 61 6c * Trigger the al
9bfc0 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 arm .*/.static v
9bfd0 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f oid sqlite3Mallo
9bfe0 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 cAlarm(int nByte
9bff0 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c ){. void (*xCal
9c000 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c lback)(void*,sql
9c010 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b ite3_int64,int);
9c020 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
9c030 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 nowUsed;. void
9c040 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 *pArg;. if( me
9c050 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9c060 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
9c070 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 xCallback = mem0
9c080 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a .alarmCallback;.
9c090 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69 nowUsed = sqli
9c0a0 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 te3StatusValue(S
9c0b0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9c0c0 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72 ORY_USED);. pAr
9c0d0 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 g = mem0.alarmAr
9c0e0 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 g;. mem0.alarmC
9c0f0 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 73 allback = 0;. s
9c100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
9c110 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
9c120 20 20 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 xCallback(pArg
9c130 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42 79 74 65 , nowUsed, nByte
9c140 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
9c150 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9c160 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 tex);. mem0.ala
9c170 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 rmCallback = xCa
9c180 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 llback;. mem0.a
9c190 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a larmArg = pArg;.
9c1a0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65 }../*.** Do a me
9c1b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9c1c0 77 69 74 68 20 73 74 61 74 69 73 74 69 63 73 20 with statistics
9c1d0 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41 73 73 and alarms. Ass
9c1e0 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 ume the.** lock
9c1f0 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e is already held.
9c200 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
9c210 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 69 allocWithAlarm(i
9c220 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 70 29 nt n, void **pp)
9c230 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20 {. int nFull;.
9c240 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 void *p;. asse
9c250 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
9c260 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 74 65 x_held(mem0.mute
9c270 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20 x) );. nFull =
9c280 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c290 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e fig.m.xRoundup(n
9c2a0 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 74 );. sqlite3Stat
9c2b0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 usSet(SQLITE_STA
9c2c0 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c TUS_MALLOC_SIZE,
9c2d0 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e n);. if( mem0.
9c2e0 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 alarmCallback!=0
9c2f0 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55 73 65 ){. int nUse
9c300 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 d = sqlite3Statu
9c310 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 sValue(SQLITE_ST
9c320 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9c330 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73 65 64 );. if( nUsed
9c340 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61 +nFull >= mem0.a
9c350 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b larmThreshold ){
9c360 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 . sqlite3Ma
9c370 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 llocAlarm(nFull)
9c380 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 ;. }. }. p
9c390 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9c3a0 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 onfig.m.xMalloc(
9c3b0 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20 70 3d nFull);. if( p=
9c3c0 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d =0 && mem0.alarm
9c3d0 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Callback ){.
9c3e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 sqlite3MallocAla
9c3f0 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 70 rm(nFull);. p
9c400 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
9c410 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 Config.m.xMalloc
9c420 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 (nFull);. }. i
9c430 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46 75 6c f( p ){. nFul
9c440 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f l = sqlite3Mallo
9c450 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 cSize(p);. sq
9c460 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9c470 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9c480 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c 6c 29 ORY_USED, nFull)
9c490 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b ;. }. *pp = p;
9c4a0 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b . return nFull;
9c4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
9c4c0 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 te memory. This
9c4d0 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69 6b 65 routine is like
9c4e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
9c4f0 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 ) except that it
9c500 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 65 20 .** assumes the
9c510 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
9c520 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
9c530 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a n initialized..*
9c540 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9c550 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 void *sqlite3Ma
9c560 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 lloc(int n){. v
9c570 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c oid *p;. if( n<
9c580 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66 66 66 66 =0 || n>=0x7ffff
9c590 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 20 f00 ){. /* A
9c5a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9c5b0 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 20 6f 66 n of a number of
9c5c0 20 62 79 74 65 73 20 77 68 69 63 68 20 69 73 20 bytes which is
9c5d0 6e 65 61 72 20 74 68 65 20 6d 61 78 69 6d 75 6d near the maximum
9c5e0 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65 64 20 69 . ** signed i
9c5f0 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6d 69 67 nteger value mig
9c600 68 74 20 63 61 75 73 65 20 61 6e 20 69 6e 74 65 ht cause an inte
9c610 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 69 6e 73 ger overflow ins
9c620 69 64 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a ide of the. *
9c630 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20 20 48 65 * xMalloc(). He
9c640 6e 63 65 20 77 65 20 6c 69 6d 69 74 20 74 68 65 nce we limit the
9c650 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 74 6f maximum size to
9c660 20 30 78 37 66 66 66 66 66 30 30 2c 20 67 69 76 0x7fffff00, giv
9c670 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35 35 20 62 ing. ** 255 b
9c680 79 74 65 73 20 6f 66 20 6f 76 65 72 68 65 61 64 ytes of overhead
9c690 2e 20 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 . SQLite itself
9c6a0 20 77 69 6c 6c 20 6e 65 76 65 72 20 75 73 65 20 will never use
9c6b0 61 6e 79 74 68 69 6e 67 20 6e 65 61 72 0a 20 20 anything near.
9c6c0 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f 75 6e 74 ** this amount
9c6d0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 . The only way
9c6e0 74 6f 20 72 65 61 63 68 20 74 68 65 20 6c 69 6d to reach the lim
9c6f0 69 74 20 69 73 20 77 69 74 68 20 73 71 6c 69 74 it is with sqlit
9c700 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 e3_malloc() */.
9c710 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 p = 0;. }els
9c720 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f e if( sqlite3Glo
9c730 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
9c740 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 at ){. sqlite
9c750 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
9c760 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d m0.mutex);. m
9c770 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e allocWithAlarm(n
9c780 2c 20 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 , &p);. sqlit
9c790 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9c7a0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 em0.mutex);. }e
9c7b0 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c lse{. p = sql
9c7c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9c7d0 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 .m.xMalloc(n);.
9c7e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
9c7f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
9c800 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
9c810 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 ry allocation is
9c820 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 for use by the
9c830 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 application..**
9c840 46 69 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20 First make sure
9c850 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 the memory subsy
9c860 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 stem is initiali
9c870 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 zed, then do the
9c880 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a .** allocation..
9c890 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
9c8a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c id *sqlite3_mall
9c8b0 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 oc(int n){.#ifnd
9c8c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
9c8d0 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 UTOINIT. if( sq
9c8e0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
9c8f0 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 () ) return 0;.#
9c900 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 endif. return s
9c910 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b qlite3Malloc(n);
9c920 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 .}../*.** Each t
9c930 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 hread may only h
9c940 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74 ave a single out
9c950 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 standing allocat
9c960 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 ion from.** xScr
9c970 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 atchMalloc(). W
9c980 65 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f e verify this co
9c990 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 nstraint in the
9c9a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a single-threaded.
9c9b0 2a 2a 20 63 61 73 65 20 62 79 20 73 65 74 74 69 ** case by setti
9c9c0 6e 67 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f ng scratchAllocO
9c9d0 75 74 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20 ut to 1 when an
9c9e0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 allocation.** is
9c9f0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65 outstanding cle
9ca00 61 72 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68 aring it when th
9ca10 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 e allocation is
9ca20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 freed..*/.#if SQ
9ca30 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
9ca40 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e =0 && !defined(N
9ca50 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e DEBUG).static in
9ca60 74 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 t scratchAllocOu
9ca70 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a t = 0;.#endif...
9ca80 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d /*.** Allocate m
9ca90 65 6d 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f emory that is to
9caa0 20 62 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c be used and rel
9cab0 65 61 73 65 64 20 72 69 67 68 74 20 61 77 61 79 eased right away
9cac0 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
9cad0 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 e is similar to
9cae0 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74 alloca() in that
9caf0 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e it is not inten
9cb00 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61 ded.** for situa
9cb10 74 69 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 tions where the
9cb20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20 memory might be
9cb30 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 held long-term.
9cb40 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 This.** routine
9cb50 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 is intended to
9cb60 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c get memory to ol
9cb70 64 20 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e d large transien
9cb80 74 20 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 t data.** struct
9cb90 75 72 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20 ures that would
9cba0 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 not normally fit
9cbb0 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66 on the stack of
9cbc0 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 an.** embedded
9cbd0 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 processor..*/.SQ
9cbe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9cbf0 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63 d *sqlite3Scratc
9cc00 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a hMalloc(int n){.
9cc10 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 void *p;. ass
9cc20 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 ert( n>0 );..#if
9cc30 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
9cc40 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 FE==0 && !define
9cc50 64 28 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 d(NDEBUG). /* V
9cc60 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f erify that no mo
9cc70 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 re than one scra
9cc80 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 tch allocation p
9cc90 65 72 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69 er thread. ** i
9cca0 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 s outstanding at
9ccb0 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 one time. (Thi
9ccc0 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 s is only checke
9ccd0 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 d in the. ** si
9cce0 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 ngle-threaded ca
9ccf0 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e se since checkin
9cd00 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 g in the multi-t
9cd10 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a hreaded case. *
9cd20 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 * would be much
9cd30 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 more complicated
9cd40 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 .) */. assert(
9cd50 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d scratchAllocOut=
9cd60 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 =0 );.#endif..
9cd70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9cd80 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9cd90 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 h<n ){. goto
9cda0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
9cdb0 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 ;. }else{ .
9cdc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9cdd0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9cde0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e ;. if( mem0.n
9cdf0 53 63 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29 ScratchFree==0 )
9ce00 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
9ce10 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9ce20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67 .mutex);. g
9ce30 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 oto scratch_over
9ce40 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b flow;. }else{
9ce50 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
9ce60 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63 i = mem0.aSc
9ce70 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 ratchFree[--mem0
9ce80 2e 6e 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a .nScratchFree];.
9ce90 20 20 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 i *= sqlit
9cea0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9ceb0 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 zScratch;.
9cec0 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
9ced0 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 (SQLITE_STATUS_S
9cee0 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b CRATCH_USED, 1);
9cef0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
9cf00 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
9cf10 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 TATUS_SCRATCH_SI
9cf20 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 ZE, n);. sq
9cf30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9cf40 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9cf50 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 p = (void*)
9cf60 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9cf70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9cf80 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20 ratch)[i];.
9cf90 20 61 73 73 65 72 74 28 20 20 28 28 28 75 38 2a assert( (((u8*
9cfa0 29 70 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 )p - (u8*)0) & 7
9cfb0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 )==0 );. }.
9cfc0 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 }.#if SQLITE_THR
9cfd0 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 EADSAFE==0 && !d
9cfe0 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 efined(NDEBUG).
9cff0 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
9d000 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a = p!=0;.#endif.
9d010 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 . return p;..sc
9d020 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a ratch_overflow:.
9d030 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9d040 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
9d050 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 at ){. sqlite
9d060 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
9d070 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 m0.mutex);. s
9d080 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
9d090 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9d0a0 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a RATCH_SIZE, n);.
9d0b0 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 n = mallocWi
9d0c0 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a thAlarm(n, &p);.
9d0d0 20 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 if( p ) sqli
9d0e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9d0f0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
9d100 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b CH_OVERFLOW, n);
9d110 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
9d120 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9d130 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 tex);. }else{.
9d140 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c p = sqlite3Gl
9d150 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
9d160 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 lloc(n);. }.#if
9d170 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
9d180 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 FE==0 && !define
9d190 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 d(NDEBUG). scra
9d1a0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 tchAllocOut = p!
9d1b0 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 =0;.#endif. ret
9d1c0 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c urn p; .}.SQL
9d1d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9d1e0 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 sqlite3ScratchF
9d1f0 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 ree(void *p){.
9d200 69 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 if( p ){..#if SQ
9d210 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
9d220 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e =0 && !defined(N
9d230 44 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 DEBUG). /* Ve
9d240 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 rify that no mor
9d250 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 e than one scrat
9d260 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 ch allocation pe
9d270 72 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 r thread. **
9d280 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 is outstanding a
9d290 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 t one time. (Th
9d2a0 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b is is only check
9d2b0 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a ed in the. **
9d2c0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 single-threaded
9d2d0 20 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 case since chec
9d2e0 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 king in the mult
9d2f0 69 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a i-threaded case.
9d300 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 ** would be
9d310 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 much more compli
9d320 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 cated.) */. a
9d330 73 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c ssert( scratchAl
9d340 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 locOut==1 );.
9d350 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
9d360 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 = 0;.#endif..
9d370 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9d380 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
9d390 63 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 ch==0.
9d3a0 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f || p<sqlite3Glo
9d3b0 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
9d3c0 63 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c ch. ||
9d3d0 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e p>=(void*)mem0.
9d3e0 61 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a aScratchFree ){.
9d3f0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
9d400 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
9d410 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 emstat ){.
9d420 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 int iSize = sq
9d430 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9d440 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 p);. sqli
9d450 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9d460 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9d470 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
9d480 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9d490 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 TUS_SCRATCH_OVER
9d4a0 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 FLOW, -iSize);.
9d4b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
9d4c0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
9d4d0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9d4e0 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 D, -iSize);.
9d4f0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9d500 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
9d510 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 p);. sqli
9d520 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9d530 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9d540 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9d550 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9d560 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
9d570 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
9d580 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 lse{. int i
9d590 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74 ;. i = (int
9d5a0 29 28 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 )((u8*)p - (u8*)
9d5b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d5c0 66 69 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20 fig.pScratch);.
9d5d0 20 20 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 i /= sqlite
9d5e0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
9d5f0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61 Scratch;. a
9d600 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
9d610 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f <sqlite3GlobalCo
9d620 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b nfig.nScratch );
9d630 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
9d640 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
9d650 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 mutex);. as
9d660 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61 sert( mem0.nScra
9d670 74 63 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c tchFree<(u32)sql
9d680 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d690 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 .nScratch );.
9d6a0 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 mem0.aScratch
9d6b0 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 Free[mem0.nScrat
9d6c0 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 chFree++] = i;.
9d6d0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
9d6e0 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9d6f0 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 TUS_SCRATCH_USED
9d700 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c , -1);. sql
9d710 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
9d720 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9d730 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
9d740 20 54 52 55 45 20 69 66 20 70 20 69 73 20 61 20 TRUE if p is a
9d750 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
9d760 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d allocation from
9d770 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 db.*/.#ifndef S
9d780 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 QLITE_OMIT_LOOKA
9d790 53 49 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20 SIDE.static int
9d7a0 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 isLookaside(sqli
9d7b0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 te3 *db, void *p
9d7c0 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26 ){. return db &
9d7d0 26 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f & p && p>=db->lo
9d7e0 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20 26 okaside.pStart &
9d7f0 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 & p<db->lookasid
9d800 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a e.pEnd;.}.#else.
9d810 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 #define isLookas
9d820 69 64 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 ide(A,B) 0.#endi
9d830 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
9d840 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 the size of a me
9d850 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9d860 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 previously obtai
9d870 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
9d880 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 te3Malloc() or s
9d890 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
9d8a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9d8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 TE int sqlite3Ma
9d8c0 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 llocSize(void *p
9d8d0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
9d8e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9d8f0 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 m.xSize(p);.}.SQ
9d900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
9d910 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9d920 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 Size(sqlite3 *db
9d930 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 , void *p){. as
9d940 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 sert( db==0 || s
9d950 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
9d960 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
9d970 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 if( isLookasid
9d980 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 e(db, p) ){.
9d990 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 return db->looka
9d9a0 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 side.sz;. }else
9d9b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c {. return sql
9d9c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d9d0 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d .m.xSize(p);. }
9d9e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
9d9f0 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79 emory previously
9da00 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
9da10 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a qlite3Malloc()..
9da20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
9da30 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 id sqlite3_free(
9da40 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 void *p){. if(
9da50 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
9da60 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9da70 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 alConfig.bMemsta
9da80 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
9da90 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9daa0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 0.mutex);. sq
9dab0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9dac0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9dad0 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 ORY_USED, -sqlit
9dae0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 e3MallocSize(p))
9daf0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f ;. sqlite3Glo
9db00 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 balConfig.m.xFre
9db10 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 e(p);. sqlite
9db20 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9db30 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c m0.mutex);. }el
9db40 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 se{. sqlite3G
9db50 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 lobalConfig.m.xF
9db60 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ree(p);. }.}../
9db70 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 *.** Free memory
9db80 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 that might be a
9db90 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
9dba0 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
9dbb0 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 base.** connecti
9dbc0 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
9dbd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
9dbe0 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 e3DbFree(sqlite3
9dbf0 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a *db, void *p){.
9dc00 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 assert( db==0
9dc10 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
9dc20 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
9dc30 20 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b );. if( isLook
9dc40 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a aside(db, p) ){.
9dc50 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f LookasideSlo
9dc60 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 t *pBuf = (Looka
9dc70 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 sideSlot*)p;.
9dc80 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 pBuf->pNext = d
9dc90 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 b->lookaside.pFr
9dca0 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b ee;. db->look
9dcb0 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 aside.pFree = pB
9dcc0 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b uf;. db->look
9dcd0 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 aside.nOut--;.
9dce0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
9dcf0 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a e3_free(p);. }.
9dd00 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
9dd10 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
9dd20 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
9dd30 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c llocation.*/.SQL
9dd40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9dd50 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 *sqlite3Realloc
9dd60 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 (void *pOld, int
9dd70 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 nBytes){. int
9dd80 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f nOld, nNew;. vo
9dd90 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 id *pNew;. if(
9dda0 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pOld==0 ){. r
9ddb0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c eturn sqlite3Mal
9ddc0 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d loc(nBytes);. }
9ddd0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 . if( nBytes<=0
9dde0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9ddf0 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 free(pOld);.
9de00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
9de10 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 78 37 66 if( nBytes>=0x7f
9de20 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a ffff00 ){. /*
9de30 20 54 68 65 20 30 78 37 66 66 66 66 30 30 20 6c The 0x7ffff00 l
9de40 69 6d 69 74 20 74 65 72 6d 20 69 73 20 65 78 70 imit term is exp
9de50 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e lained in commen
9de60 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c ts on sqlite3Mal
9de70 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 72 65 74 loc() */. ret
9de80 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c urn 0;. }. nOl
9de90 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f d = sqlite3Mallo
9dea0 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20 6e cSize(pOld);. n
9deb0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f New = sqlite3Glo
9dec0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 balConfig.m.xRou
9ded0 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20 ndup(nBytes);.
9dee0 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 if( nOld==nNew )
9def0 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c {. pNew = pOl
9df00 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 d;. }else if( s
9df10 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9df20 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
9df30 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9df40 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9df50 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 x);. sqlite3S
9df60 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f tatusSet(SQLITE_
9df70 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 STATUS_MALLOC_SI
9df80 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 ZE, nBytes);.
9df90 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 if( sqlite3Stat
9dfa0 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 usValue(SQLITE_S
9dfb0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9dfc0 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 D)+nNew-nOld >=
9dfd0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e . mem0.
9dfe0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 alarmThreshold )
9dff0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
9e000 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d allocAlarm(nNew-
9e010 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 nOld);. }.
9e020 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 pNew = sqlite3G
9e030 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
9e040 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 ealloc(pOld, nNe
9e050 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 w);. if( pNew
9e060 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 ==0 && mem0.alar
9e070 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 mCallback ){.
9e080 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 sqlite3Malloc
9e090 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 Alarm(nBytes);.
9e0a0 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 pNew = sqli
9e0b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9e0c0 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c m.xRealloc(pOld,
9e0d0 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 nNew);. }.
9e0e0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 if( pNew ){.
9e0f0 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 nNew = sqlit
9e100 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 e3MallocSize(pNe
9e110 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 w);. sqlite
9e120 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9e130 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9e140 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 USED, nNew-nOld)
9e150 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
9e160 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9e170 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
9e180 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d else{. pNew =
9e190 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9e1a0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 nfig.m.xRealloc(
9e1b0 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d pOld, nNew);. }
9e1c0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
9e1d0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 }../*.** The pub
9e1e0 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f lic interface to
9e1f0 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e sqlite3Realloc.
9e200 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 Make sure that
9e210 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 the memory.** s
9e220 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 ubsystem is init
9e230 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f ialized prior to
9e240 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 invoking sqlite
9e250 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 Realloc..*/.SQLI
9e260 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
9e270 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 ite3_realloc(voi
9e280 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b d *pOld, int n){
9e290 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9e2a0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
9e2b0 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 if( sqlite3_init
9e2c0 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 ialize() ) retur
9e2d0 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 n 0;.#endif. re
9e2e0 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c turn sqlite3Real
9e2f0 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a loc(pOld, n);.}.
9e300 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9e310 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 and zero memory
9e320 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
9e330 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9e340 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 e3MallocZero(int
9e350 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d n){. void *p =
9e360 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
9e370 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
9e380 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e memset(p, 0, n
9e390 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
9e3a0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f p;.}../*.** Allo
9e3b0 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 cate and zero me
9e3c0 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c mory. If the al
9e3d0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 location fails,
9e3e0 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c make.** the mall
9e3f0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e ocFailed flag in
9e400 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
9e410 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 pointer..*/.SQLI
9e420 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9e430 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 *sqlite3DbMalloc
9e440 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 Zero(sqlite3 *db
9e450 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
9e460 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d *p = sqlite3DbM
9e470 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b allocRaw(db, n);
9e480 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
9e490 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b memset(p, 0, n);
9e4a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
9e4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
9e4c0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f te and zero memo
9e4d0 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f ry. If the allo
9e4e0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 cation fails, ma
9e4f0 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 ke.** the malloc
9e500 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 Failed flag in t
9e510 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f he connection po
9e520 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 inter..**.** If
9e530 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 db!=0 and db->ma
9e540 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 llocFailed is tr
9e550 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 ue (indicating a
9e560 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a prior malloc.**
9e570 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 failure on the
9e580 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
9e590 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 nnection) then a
9e5a0 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a lways return 0..
9e5b0 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 ** Hence for a p
9e5c0 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
9e5d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f se connection, o
9e5e0 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 nce malloc start
9e5f0 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 s.** failing, it
9e600 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e fails consisten
9e610 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 tly until malloc
9e620 46 61 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e Failed is reset.
9e630 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 .** This is an i
9e640 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 mportant assumpt
9e650 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 ion. There are
9e660 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 many places in t
9e670 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 he.** code that
9e680 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 do things like t
9e690 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
9e6a0 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 int *a = (int
9e6b0 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
9e6c0 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a cRaw(db, 100);.*
9e6d0 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 * int *b
9e6e0 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 = (int*)sqlite3
9e6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
9e700 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 200);.**
9e710 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d if( b ) a[10] =
9e720 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 9;.**.** In oth
9e730 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 er words, if a s
9e740 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 ubsequent malloc
9e750 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 (ex: "b") worke
9e760 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 d, it is assumed
9e770 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 .** that all pri
9e780 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 or mallocs (ex:
9e790 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e "a") worked too.
9e7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9e7b0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9e7c0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 DbMallocRaw(sqli
9e7d0 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b te3 *db, int n){
9e7e0 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 . void *p;. as
9e7f0 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 sert( db==0 || s
9e800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
9e810 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
9e820 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9e830 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 MIT_LOOKASIDE.
9e840 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f if( db ){. Lo
9e850 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 okasideSlot *pBu
9e860 66 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d f;. if( db->m
9e870 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
9e880 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
9e890 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d }. if( db-
9e8a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 >lookaside.bEnab
9e8b0 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f led && n<=db->lo
9e8c0 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 okaside.sz.
9e8d0 20 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 && (pBuf = d
9e8e0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 b->lookaside.pFr
9e8f0 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ee)!=0 ){.
9e900 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 db->lookaside.pF
9e910 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 ree = pBuf->pNex
9e920 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f t;. db->loo
9e930 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 kaside.nOut++;.
9e940 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f if( db->loo
9e950 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e kaside.nOut>db->
9e960 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 lookaside.mxOut
9e970 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c ){. db->l
9e980 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d ookaside.mxOut =
9e990 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
9e9a0 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Out;. }.
9e9b0 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a return (void*
9e9c0 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d )pBuf;. }. }
9e9d0 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 .#else. if( db
9e9e0 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 && db->mallocFai
9e9f0 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 led ){. retur
9ea00 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 0;. }.#endif.
9ea10 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
9ea20 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 loc(n);. if( !p
9ea30 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 && db ){. db
9ea40 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
9ea50 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
9ea60 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 p;.}../*.** Res
9ea70 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 ize the block of
9ea80 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 memory pointed
9ea90 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 to by p to n byt
9eaa0 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 es. If the.** re
9eab0 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 size fails, set
9eac0 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 the mallocFailed
9ead0 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e flag in the con
9eae0 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a nection object..
9eaf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
9eb00 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 E void *sqlite3D
9eb10 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 bRealloc(sqlite3
9eb20 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 *db, void *p, i
9eb30 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 nt n){. void *p
9eb40 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 New = 0;. asser
9eb50 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 t( db!=0 );. as
9eb60 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
9eb70 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
9eb80 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d ex) );. if( db-
9eb90 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 >mallocFailed==0
9eba0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 ){. if( p==0
9ebb0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
9ebc0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9ebd0 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 Raw(db, n);.
9ebe0 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b }. if( isLook
9ebf0 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a aside(db, p) ){.
9ec00 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d if( n<=db-
9ec10 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b >lookaside.sz ){
9ec20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
9ec30 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 p;. }.
9ec40 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
9ec50 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
9ec60 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 );. if( pNe
9ec70 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d w ){. mem
9ec80 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d cpy(pNew, p, db-
9ec90 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a >lookaside.sz);.
9eca0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
9ecb0 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 bFree(db, p);.
9ecc0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
9ecd0 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 . pNew = sq
9ece0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c lite3_realloc(p,
9ecf0 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 n);. if( !
9ed00 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
9ed10 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9ed20 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
9ed30 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
9ed40 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
9ed50 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c Attempt to reall
9ed60 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 ocate p. If the
9ed70 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 reallocation fa
9ed80 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 ils, then free p
9ed90 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20 .** and set the
9eda0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
9edb0 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 g in the databas
9edc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f e connection..*/
9edd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9ede0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
9edf0 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c eallocOrFree(sql
9ee00 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a ite3 *db, void *
9ee10 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 p, int n){. voi
9ee20 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 d *pNew;. pNew
9ee30 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
9ee40 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 oc(db, p, n);.
9ee50 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 if( !pNew ){.
9ee60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
9ee70 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 b, p);. }. ret
9ee80 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
9ee90 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f ** Make a copy o
9eea0 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 f a string in me
9eeb0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
9eec0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
9eed0 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e ). These .** fun
9eee0 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 ctions call sqli
9eef0 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 te3MallocRaw() d
9ef00 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 irectly instead
9ef10 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 of sqliteMalloc(
9ef20 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 ). This.** is be
9ef30 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 cause when memor
9ef40 79 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 y debugging is t
9ef50 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 urned on, these
9ef60 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 two functions ar
9ef70 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 e .** called via
9ef80 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 macros that rec
9ef90 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 ord the current
9efa0 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 file and line nu
9efb0 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 mber in the.** T
9efc0 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63 74 hreadData struct
9efd0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ure..*/.SQLITE_P
9efe0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
9eff0 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c ite3DbStrDup(sql
9f000 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
9f010 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 char *z){. char
9f020 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74 *zNew;. size_t
9f030 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 n;. if( z==0 )
9f040 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
9f050 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 }. n = sqlite
9f060 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3Strlen30(z) + 1
9f070 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 ;. assert( (n&0
9f080 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b x7fffffff)==n );
9f090 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 . zNew = sqlite
9f0a0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
9f0b0 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20 (int)n);. if(
9f0c0 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 zNew ){. memc
9f0d0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a py(zNew, z, n);.
9f0e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 }. return zNe
9f0f0 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 w;.}.SQLITE_PRIV
9f100 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
9f110 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 3DbStrNDup(sqlit
9f120 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
9f130 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 ar *z, int n){.
9f140 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 char *zNew;. i
9f150 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( z==0 ){. r
9f160 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 eturn 0;. }. a
9f170 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 ssert( (n&0x7fff
9f180 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e ffff)==n );. zN
9f190 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
9f1a0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29 llocRaw(db, n+1)
9f1b0 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a ;. if( zNew ){.
9f1c0 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c memcpy(zNew,
9f1d0 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 z, n);. zNew
9f1e0 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 [n] = 0;. }. r
9f1f0 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f eturn zNew;.}../
9f200 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 *.** Create a st
9f210 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 ring from the zF
9f220 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 romat argument a
9f230 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 nd the va_list t
9f240 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 hat follows..**
9f250 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 Store the string
9f260 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 in memory obtai
9f270 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d ned from sqliteM
9f280 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 alloc() and make
9f290 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f *pz.** point to
9f2a0 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f that string..*/
9f2b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9f2c0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 void sqlite3SetS
9f2d0 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c tring(char **pz,
9f2e0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f sqlite3 *db, co
9f2f0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
9f300 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 t, ...){. va_li
9f310 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a st ap;. char *z
9f320 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 ;.. va_start(ap
9f330 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 , zFormat);. z
9f340 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
9f350 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
9f360 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
9f370 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
9f380 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 e(db, *pz);. *p
9f390 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a z = z;.}.../*.**
9f3a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
9f3b0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 ust be called be
9f3c0 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79 fore exiting any
9f3d0 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 API function (i
9f3e0 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e .e. .** returnin
9f3f0 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 g control to the
9f400 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73 20 user) that has
9f410 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d called sqlite3_m
9f420 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 alloc or.** sqli
9f430 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a te3_realloc..**.
9f440 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
9f450 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c value is normall
9f460 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 y a copy of the
9f470 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
9f480 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 to this.** funct
9f490 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 ion. However, if
9f4a0 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
9f4b0 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 ure has occurred
9f4c0 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 since the previ
9f4d0 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f ous.** invocatio
9f4e0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 n SQLITE_NOMEM i
9f4f0 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 s returned inste
9f500 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ad. .**.** If th
9f510 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
9f520 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c , db, is not NUL
9f530 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 L and a malloc()
9f540 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
9f550 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 red,.** then the
9f560 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f connection erro
9f570 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 r-code (the valu
9f580 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
9f590 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 lite3_errcode())
9f5a0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
9f5b0 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 LITE_NOMEM..*/.S
9f5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
9f5d0 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 t sqlite3ApiExit
9f5e0 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e (sqlite3* db, in
9f5f0 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 t rc){. /* If t
9f600 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 he db handle is
9f610 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 not NULL, then w
9f620 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 e must hold the
9f630 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
9f640 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 e. ** mutex her
9f650 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 e. Otherwise the
9f660 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 read (and possi
9f670 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 ble write) of db
9f680 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a ->mallocFailed .
9f690 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 ** is unsafe,
9f6a0 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 as is the call t
9f6b0 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 o sqlite3Error()
9f6c0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
9f6d0 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f !db || sqlite3_
9f6e0 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
9f6f0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 utex) );. if( d
9f700 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 b && (db->malloc
9f710 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 Failed || rc==SQ
9f720 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
9f730 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
9f740 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 Error(db, SQLITE
9f750 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 _NOMEM, 0);.
9f760 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9f770 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 = 0;. rc = S
9f780 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
9f790 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 . return rc & (
9f7a0 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b db ? db->errMask
9f7b0 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a : 0xff);.}../**
9f7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
9f7d0 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a of malloc.c ***
9f7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
9f810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
9f820 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 in file printf.c
9f830 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
9f840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
9f860 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 ** The "printf"
9f870 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 code that follow
9f880 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 s dates from the
9f890 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 1980's. It is
9f8a0 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 in.** the public
9f8b0 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 domain. The or
9f8c0 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 iginal comments
9f8d0 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 are included her
9f8e0 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 e for.** complet
9f8f0 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 eness. They are
9f900 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 very out-of-dat
9f910 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 e but might be u
9f920 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 seful as.** an h
9f930 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 istorical refere
9f940 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 nce. Most of th
9f950 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 e "enhancements"
9f960 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 have been backe
9f970 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 d.** out so that
9f980 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 the functionali
9f990 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 ty is now the sa
9f9a0 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 me as standard p
9f9b0 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a rintf()..**.****
9f9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fa00 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
9fa10 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c following modul
9fa20 65 73 20 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 es is an enhance
9fa30 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f d replacement fo
9fa40 72 20 74 68 65 20 22 70 72 69 6e 74 66 22 20 73 r the "printf" s
9fa50 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f ubroutines.** fo
9fa60 75 6e 64 20 69 6e 20 74 68 65 20 73 74 61 6e 64 und in the stand
9fa70 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 20 20 ard C library.
9fa80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e The following en
9fa90 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a hancements are.*
9faa0 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a * supported:.**.
9fab0 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 64 69 74 ** + Addit
9fac0 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e ional functions.
9fad0 20 20 54 68 65 20 73 74 61 6e 64 61 72 64 20 73 The standard s
9fae0 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 20 66 et of "printf" f
9faf0 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 unctions.**
9fb00 20 20 20 20 69 6e 63 6c 75 64 65 73 20 70 72 69 includes pri
9fb10 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 ntf, fprintf, sp
9fb20 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 rintf, vprintf,
9fb30 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a vfprintf, and.**
9fb40 20 20 20 20 20 20 20 20 20 76 73 70 72 69 6e 74 vsprint
9fb50 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 f. This module
9fb60 61 64 64 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 adds the followi
9fb70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ng:.**.**
9fb80 20 20 20 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 * snprintf
9fb90 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 -- Works like sp
9fba0 72 69 6e 74 66 2c 20 62 75 74 20 68 61 73 20 61 rintf, but has a
9fbb0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74 n extra argument
9fbc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 69 whi
9fbe0 63 68 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ch is the size o
9fbf0 66 20 74 68 65 20 62 75 66 66 65 72 20 77 72 69 f the buffer wri
9fc00 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 tten to..**.**
9fc10 20 20 20 20 20 20 20 20 20 2a 20 20 6d 70 72 69 * mpri
9fc20 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 ntf -- Similar
9fc30 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 57 72 69 to sprintf. Wri
9fc40 74 65 73 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 tes output to me
9fc50 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 mory.**
9fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fc70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
9fc80 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 alloc..**.**
9fc90 20 20 20 20 20 20 20 2a 20 20 78 70 72 69 6e 74 * xprint
9fca0 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 f -- Calls a fu
9fcb0 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 nction to dispos
9fcc0 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a e of output..**.
9fcd0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
9fce0 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f nprintf -- No o
9fcf0 75 74 70 75 74 2c 20 62 75 74 20 72 65 74 75 72 utput, but retur
9fd00 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
9fd10 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 characters.**
9fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fd30 20 20 20 20 20 20 20 20 74 68 61 74 20 77 6f 75 that wou
9fd40 6c 64 20 68 61 76 65 20 62 65 65 6e 20 6f 75 74 ld have been out
9fd50 70 75 74 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a put by printf..*
9fd60 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9fd70 20 20 41 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 A v- version (
9fd80 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 29 20 6f ex: vsnprintf) o
9fd90 66 20 65 76 65 72 79 20 66 75 6e 63 74 69 6f 6e f every function
9fda0 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 is also.**
9fdb0 20 20 20 20 20 20 20 20 20 73 75 70 70 6c 69 65 supplie
9fdc0 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 d..**.** +
9fdd0 20 41 20 66 65 77 20 65 78 74 65 6e 73 69 6f 6e A few extension
9fde0 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 s to the formatt
9fdf0 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 ing notation are
9fe00 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a supported:.**.*
9fe10 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 * * T
9fe20 68 65 20 22 3d 22 20 66 6c 61 67 20 28 73 69 6d he "=" flag (sim
9fe30 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 63 61 75 ilar to "-") cau
9fe40 73 65 73 20 74 68 65 20 6f 75 74 70 75 74 20 74 ses the output t
9fe50 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 o be.**
9fe60 20 20 20 20 20 62 65 20 63 65 6e 74 65 72 65 64 be centered
9fe70 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 in the appropri
9fe80 61 74 65 6c 79 20 73 69 7a 65 64 20 66 69 65 6c ately sized fiel
9fe90 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 d..**.**
9fea0 20 20 20 2a 20 20 54 68 65 20 25 62 20 66 69 65 * The %b fie
9feb0 6c 64 20 6f 75 74 70 75 74 73 20 61 6e 20 69 6e ld outputs an in
9fec0 74 65 67 65 72 20 69 6e 20 62 69 6e 61 72 79 20 teger in binary
9fed0 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 notation..**.**
9fee0 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 * The
9fef0 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 %c field now ac
9ff00 63 65 70 74 73 20 61 20 70 72 65 63 69 73 69 6f cepts a precisio
9ff10 6e 2e 20 20 54 68 65 20 63 68 61 72 61 63 74 65 n. The characte
9ff20 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 r output.**
9ff30 20 20 20 20 20 20 20 20 20 69 73 20 72 65 70 65 is repe
9ff40 61 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 ated by the numb
9ff50 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20 er of times the
9ff60 70 72 65 63 69 73 69 6f 6e 20 73 70 65 63 69 66 precision specif
9ff70 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ies..**.**
9ff80 20 20 20 20 20 2a 20 20 54 68 65 20 25 27 20 66 * The %' f
9ff90 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 ield works like
9ffa0 25 63 2c 20 62 75 74 20 74 61 6b 65 73 20 61 73 %c, but takes as
9ffb0 20 69 74 73 20 63 68 61 72 61 63 74 65 72 20 74 its character t
9ffc0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
9ffd0 20 20 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 next characte
9ffe0 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 r of the format
9fff0 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 string, instead
a0000 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 of the next.**
a0010 20 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75 argu
a0020 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 ment. For examp
a0030 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 25 2e 37 le, printf("%.7
a0040 38 27 2d 22 29 20 20 70 72 69 6e 74 73 20 37 38 8'-") prints 78
a0050 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 minus.**
a0060 20 20 20 20 20 20 20 73 69 67 6e 73 2c 20 74 68 signs, th
a0070 65 20 73 61 6d 65 20 61 73 20 20 70 72 69 6e 74 e same as print
a0080 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a f("%.78c",'-')..
a0090 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 **.** + Wh
a00a0 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e en compiled usin
a00b0 67 20 47 43 43 20 6f 6e 20 61 20 53 50 41 52 43 g GCC on a SPARC
a00c0 2c 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f , this version o
a00d0 66 20 70 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 f printf is.**
a00e0 20 20 20 20 20 20 20 66 61 73 74 65 72 20 74 68 faster th
a00f0 61 6e 20 74 68 65 20 6c 69 62 72 61 72 79 20 70 an the library p
a0100 72 69 6e 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 rintf for SUN OS
a0110 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4.1..**.**
a0120 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e + All function
a0130 73 20 61 72 65 20 66 75 6c 6c 79 20 72 65 65 6e s are fully reen
a0140 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a trant..**.*/../*
a0150 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 .** Conversion t
a0160 79 70 65 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 ypes fall into v
a0170 61 72 69 6f 75 73 20 63 61 74 65 67 6f 72 69 65 arious categorie
a0180 73 20 61 73 20 64 65 66 69 6e 65 64 20 62 79 20 s as defined by
a0190 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 the.** following
a01a0 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f enumeration..*/
a01b0 0a 23 64 65 66 69 6e 65 20 65 74 52 41 44 49 58 .#define etRADIX
a01c0 20 20 20 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 1 /* Inte
a01d0 67 65 72 20 74 79 70 65 73 2e 20 20 25 64 2c 20 ger types. %d,
a01e0 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 %x, %o, and so f
a01f0 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 orth */.#define
a0200 65 74 46 4c 4f 41 54 20 20 20 20 20 20 20 32 20 etFLOAT 2
a0210 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e /* Floating poin
a0220 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e t. %f */.#defin
a0230 65 20 65 74 45 58 50 20 20 20 20 20 20 20 20 20 e etEXP
a0240 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 3 /* Exponention
a0250 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 al notation. %e
a0260 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e and %E */.#defin
a0270 65 20 65 74 47 45 4e 45 52 49 43 20 20 20 20 20 e etGENERIC
a0280 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 4 /* Floating or
a0290 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 exponential, de
a02a0 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e pending on expon
a02b0 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 ent. %g */.#defi
a02c0 6e 65 20 65 74 53 49 5a 45 20 20 20 20 20 20 20 ne etSIZE
a02d0 20 35 20 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 5 /* Return num
a02e0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
a02f0 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 s processed so f
a0300 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e ar. %n */.#defin
a0310 65 20 65 74 53 54 52 49 4e 47 20 20 20 20 20 20 e etSTRING
a0320 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 6 /* Strings. %s
a0330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 44 59 */.#define etDY
a0340 4e 53 54 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 NSTRING 7 /* D
a0350 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
a0360 61 74 65 64 20 73 74 72 69 6e 67 73 2e 20 25 7a ated strings. %z
a0370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 45 */.#define etPE
a0380 52 43 45 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 RCENT 8 /* P
a0390 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 ercent symbol. %
a03a0 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 % */.#define etC
a03b0 48 41 52 58 20 20 20 20 20 20 20 39 20 2f 2a 20 HARX 9 /*
a03c0 43 68 61 72 61 63 74 65 72 73 2e 20 25 63 20 2a Characters. %c *
a03d0 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74 20 61 72 /./* The rest ar
a03e0 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f e extensions, no
a03f0 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 t normally found
a0400 20 69 6e 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a in printf() */.
a0410 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 #define etSQLESC
a0420 41 50 45 20 20 31 30 20 2f 2a 20 53 74 72 69 6e APE 10 /* Strin
a0430 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75 gs with '\'' dou
a0440 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 bled. %q */.#de
a0450 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 fine etSQLESCAPE
a0460 32 20 31 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 2 11 /* Strings
a0470 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 with '\'' double
a0480 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 d and enclosed i
a0490 6e 20 27 27 2c 0a 20 20 20 20 20 20 20 20 20 20 n '',.
a04a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a04b0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 NULL pointers re
a04c0 70 6c 61 63 65 64 20 62 79 20 53 51 4c 20 4e 55 placed by SQL NU
a04d0 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 LL. %Q */.#defi
a04e0 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 ne etTOKEN
a04f0 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 12 /* a pointer
a0500 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 to a Token struc
a0510 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
a0520 65 74 53 52 43 4c 49 53 54 20 20 20 20 31 33 20 etSRCLIST 13
a0530 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 /* a pointer to
a0540 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 a SrcList */.#de
a0550 66 69 6e 65 20 65 74 50 4f 49 4e 54 45 52 20 20 fine etPOINTER
a0560 20 20 31 34 20 2f 2a 20 54 68 65 20 25 70 20 63 14 /* The %p c
a0570 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 onversion */.#de
a0580 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 fine etSQLESCAPE
a0590 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 3 15 /* %w -> St
a05a0 72 69 6e 67 73 20 77 69 74 68 20 27 5c 22 27 20 rings with '\"'
a05b0 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 doubled */.#defi
a05c0 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 ne etORDINAL
a05d0 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 16 /* %r -> 1st,
a05e0 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 2nd, 3rd, 4th,
a05f0 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e etc. English on
a0600 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65 ly */..#define e
a0610 74 49 4e 56 41 4c 49 44 20 20 20 20 20 30 20 2f tINVALID 0 /
a0620 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a * Any unrecogniz
a0630 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 ed conversion ty
a0640 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e pe */.../*.** An
a0650 20 22 65 74 42 79 74 65 22 20 69 73 20 61 6e 20 "etByte" is an
a0660 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 76 8-bit unsigned v
a0670 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 alue..*/.typedef
a0680 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65 unsigned char e
a0690 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 tByte;../*.** Ea
a06a0 63 68 20 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 ch builtin conve
a06b0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20 rsion character
a06c0 28 65 78 3a 20 74 68 65 20 27 64 27 20 69 6e 20 (ex: the 'd' in
a06d0 22 25 64 22 29 20 69 73 20 64 65 73 63 72 69 62 "%d") is describ
a06e0 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 ed.** by an inst
a06f0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
a0700 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a owing structure.
a0710 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
a0720 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a t et_info { /*
a0730 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
a0740 75 74 20 65 61 63 68 20 66 6f 72 6d 61 74 20 66 ut each format f
a0750 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 ield */. char f
a0760 6d 74 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 mttype;
a0770 20 20 20 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 /* The format
a0780 20 66 69 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 field code lett
a0790 65 72 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 er */. etByte b
a07a0 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ase;
a07b0 20 2f 2a 20 54 68 65 20 62 61 73 65 20 66 6f 72 /* The base for
a07c0 20 72 61 64 69 78 20 63 6f 6e 76 65 72 73 69 6f radix conversio
a07d0 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c n */. etByte fl
a07e0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
a07f0 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f /* One or more o
a0800 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 f FLAG_ constant
a0810 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 s below */. etB
a0820 79 74 65 20 74 79 70 65 3b 20 20 20 20 20 20 20 yte type;
a0830 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 /* Convers
a0840 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a ion paradigm */.
a0850 20 20 65 74 42 79 74 65 20 63 68 61 72 73 65 74 etByte charset
a0860 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 ; /* Of
a0870 66 73 65 74 20 69 6e 74 6f 20 61 44 69 67 69 74 fset into aDigit
a0880 73 5b 5d 20 6f 66 20 74 68 65 20 64 69 67 69 74 s[] of the digit
a0890 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 s string */. et
a08a0 42 79 74 65 20 70 72 65 66 69 78 3b 20 20 20 20 Byte prefix;
a08b0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
a08c0 20 69 6e 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 into aPrefix[]
a08d0 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 73 74 of the prefix st
a08e0 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 ring */.} et_inf
a08f0 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 o;../*.** Allowe
a0900 64 20 76 61 6c 75 65 73 20 66 6f 72 20 65 74 5f d values for et_
a0910 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 info.flags.*/.#d
a0920 65 66 69 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 efine FLAG_SIGNE
a0930 44 20 20 31 20 20 20 20 20 2f 2a 20 54 72 75 65 D 1 /* True
a0940 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 74 6f if the value to
a0950 20 63 6f 6e 76 65 72 74 20 69 73 20 73 69 67 6e convert is sign
a0960 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c ed */.#define FL
a0970 41 47 5f 49 4e 54 45 52 4e 20 20 32 20 20 20 20 AG_INTERN 2
a0980 20 2f 2a 20 54 72 75 65 20 69 66 20 66 6f 72 20 /* True if for
a0990 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c internal use onl
a09a0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 y */.#define FLA
a09b0 47 5f 53 54 52 49 4e 47 20 20 34 20 20 20 20 20 G_STRING 4
a09c0 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 /* Allow infinit
a09d0 79 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a y precision */..
a09e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
a09f0 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 73 65 wing table is se
a0a00 61 72 63 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c arched linearly,
a0a10 20 73 6f 20 69 74 20 69 73 20 67 6f 6f 64 20 74 so it is good t
a0a20 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 o put the.** mos
a0a30 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75 73 65 t frequently use
a0a40 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 d conversion typ
a0a50 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 es first..*/.sta
a0a60 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 tic const char a
a0a70 44 69 67 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 Digits[] = "0123
a0a80 34 35 36 37 38 39 41 42 43 44 45 46 30 31 32 33 456789ABCDEF0123
a0a90 34 35 36 37 38 39 61 62 63 64 65 66 22 3b 0a 73 456789abcdef";.s
a0aa0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
a0ab0 20 61 50 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 aPrefix[] = "-x
a0ac0 30 5c 30 30 30 58 30 22 3b 0a 73 74 61 74 69 63 0\000X0";.static
a0ad0 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 const et_info f
a0ae0 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b mtinfo[] = {. {
a0af0 20 20 27 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 'd', 10, 1, et
a0b00 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a0b10 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 0 },. { 's',
a0b20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 0, 4, etSTRING,
a0b30 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
a0b40 20 20 27 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 'g', 0, 1, et
a0b50 47 45 4e 45 52 49 43 2c 20 20 20 20 33 30 2c 20 GENERIC, 30,
a0b60 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 0 },. { 'z',
a0b70 30 2c 20 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 0, 4, etDYNSTRIN
a0b80 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b G, 0, 0 },. {
a0b90 20 20 27 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 'q', 0, 4, et
a0ba0 53 51 4c 45 53 43 41 50 45 2c 20 20 30 2c 20 20 SQLESCAPE, 0,
a0bb0 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 0 },. { 'Q',
a0bc0 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 0, 4, etSQLESCAP
a0bd0 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b E2, 0, 0 },. {
a0be0 20 20 27 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 'w', 0, 4, et
a0bf0 53 51 4c 45 53 43 41 50 45 33 2c 20 30 2c 20 20 SQLESCAPE3, 0,
a0c00 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 0 },. { 'c',
a0c10 30 2c 20 30 2c 20 65 74 43 48 41 52 58 2c 20 20 0, 0, etCHARX,
a0c20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
a0c30 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 'o', 8, 0, et
a0c40 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a0c50 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 2 },. { 'u', 1
a0c60 30 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 0, 0, etRADIX,
a0c70 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
a0c80 20 20 27 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 'x', 16, 0, et
a0c90 52 41 44 49 58 2c 20 20 20 20 20 20 31 36 2c 20 RADIX, 16,
a0ca0 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 1 },. { 'X', 1
a0cb0 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 6, 0, etRADIX,
a0cc0 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 0, 4 },.#if
a0cd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a0ce0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
a0cf0 20 20 7b 20 20 27 66 27 2c 20 20 30 2c 20 31 2c { 'f', 0, 1,
a0d00 20 65 74 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 etFLOAT, 0
a0d10 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 , 0 },. { 'e'
a0d20 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 , 0, 1, etEXP,
a0d30 20 20 20 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 30, 0 },.
a0d40 20 20 7b 20 20 27 45 27 2c 20 20 30 2c 20 31 2c { 'E', 0, 1,
a0d50 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 31 etEXP, 1
a0d60 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 4, 0 },. { 'G'
a0d70 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 , 0, 1, etGENER
a0d80 49 43 2c 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a IC, 14, 0 },.
a0d90 23 65 6e 64 69 66 0a 20 20 7b 20 20 27 69 27 2c #endif. { 'i',
a0da0 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 10, 1, etRADIX,
a0db0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
a0dc0 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 { 'n', 0, 0,
a0dd0 65 74 53 49 5a 45 2c 20 20 20 20 20 20 20 30 2c etSIZE, 0,
a0de0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 0 },. { '%',
a0df0 20 20 30 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 0, 0, etPERCEN
a0e00 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 T, 0, 0 },.
a0e10 20 7b 20 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 { 'p', 16, 0,
a0e20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c etPOINTER, 0,
a0e30 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 1 },../* All t
a0e40 68 65 20 72 65 73 74 20 68 61 76 65 20 74 68 65 he rest have the
a0e50 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 62 69 74 FLAG_INTERN bit
a0e60 20 73 65 74 20 61 6e 64 20 61 72 65 20 74 68 75 set and are thu
a0e70 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a s for internal.*
a0e80 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 * use only */.
a0e90 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 { 'T', 0, 2, e
a0ea0 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 tTOKEN, 0,
a0eb0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 0 },. { 'S',
a0ec0 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 53 54 0, 2, etSRCLIST
a0ed0 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 , 0, 0 },.
a0ee0 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 { 'r', 10, 3, e
a0ef0 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 tORDINAL, 0,
a0f00 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 0 },.};../*.**
a0f10 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 If SQLITE_OMIT_F
a0f20 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73 LOATING_POINT is
a0f30 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e defined, then n
a0f40 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 one of the float
a0f50 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e ing point.** con
a0f60 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f versions will wo
a0f70 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 rk..*/.#ifndef S
a0f80 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a0f90 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 ING_POINT./*.**
a0fa0 22 2a 76 61 6c 22 20 69 73 20 61 20 64 6f 75 62 "*val" is a doub
a0fb0 6c 65 20 73 75 63 68 20 74 68 61 74 20 30 2e 31 le such that 0.1
a0fc0 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a <= *val < 10.0.
a0fd0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 73 ** Return the as
a0fe0 63 69 69 20 63 6f 64 65 20 66 6f 72 20 74 68 65 cii code for the
a0ff0 20 6c 65 61 64 69 6e 67 20 64 69 67 69 74 20 6f leading digit o
a1000 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 f *val, then.**
a1010 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 6c 22 20 multiply "*val"
a1020 62 79 20 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72 by 10.0 to renor
a1030 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 malize..**.** Ex
a1040 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e ample:.** in
a1050 70 75 74 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20 put: *val =
a1060 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f 3.14159.** o
a1070 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d utput: *val =
a1080 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e 63 74 1.4159 funct
a1090 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27 33 27 ion return = '3'
a10a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 .**.** The count
a10b0 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63 72 65 er *cnt is incre
a10c0 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69 6d 65 mented each time
a10d0 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74 65 72 . After counter
a10e0 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 36 20 28 exceeds.** 16 (
a10f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 the number of si
a1100 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 gnificant digits
a1110 20 69 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f in a 64-bit flo
a1120 61 74 29 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c at) '0' is.** al
a1130 77 61 79 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a ways returned..*
a1140 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 65 74 /.static char et
a1150 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f _getdigit(LONGDO
a1160 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20 UBLE_TYPE *val,
a1170 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 int *cnt){. int
a1180 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f digit;. LONGDO
a1190 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20 20 69 UBLE_TYPE d;. i
a11a0 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 f( (*cnt)++ >= 1
a11b0 36 20 29 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 6 ) return '0';.
a11c0 20 20 64 69 67 69 74 20 3d 20 28 69 6e 74 29 2a digit = (int)*
a11d0 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67 69 74 val;. d = digit
a11e0 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 27 30 27 ;. digit += '0'
a11f0 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c ;. *val = (*val
a1200 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65 - d)*10.0;. re
a1210 74 75 72 6e 20 28 63 68 61 72 29 64 69 67 69 74 turn (char)digit
a1220 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
a1230 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
a1240 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a NG_POINT */../*.
a1250 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 ** Append N spac
a1260 65 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 e characters to
a1270 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 the given string
a1280 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 buffer..*/.stat
a1290 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70 ic void appendSp
a12a0 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41 ace(StrAccum *pA
a12b0 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 ccum, int N){.
a12c0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
a12d0 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 r zSpaces[] = "
a12e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a12f0 20 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20 ";.
a1300 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 while( N>=(int)
a1310 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d sizeof(zSpaces)-
a1320 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 1 ){. sqlite3
a1330 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a1340 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 Accum, zSpaces,
a1350 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d sizeof(zSpaces)-
a1360 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 1);. N -= siz
a1370 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a eof(zSpaces)-1;.
a1380 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b }. if( N>0 ){
a1390 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 . sqlite3StrA
a13a0 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a13b0 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a m, zSpaces, N);.
a13c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 }.}../*.** On
a13d0 6d 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20 machines with a
a13e0 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 small stack size
a13f0 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69 , you can redefi
a1400 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 ne the.** SQLITE
a1410 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 _PRINT_BUF_SIZE
a1420 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 to be less than
a1430 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 350..*/.#ifndef
a1440 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a1450 5f 53 49 5a 45 0a 23 20 69 66 20 64 65 66 69 6e _SIZE.# if defin
a1460 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f ed(SQLITE_SMALL_
a1470 53 54 41 43 4b 29 0a 23 20 20 20 64 65 66 69 6e STACK).# defin
a1480 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 e SQLITE_PRINT_B
a1490 55 46 5f 53 49 5a 45 20 35 30 0a 23 20 65 6c 73 UF_SIZE 50.# els
a14a0 65 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c e.# define SQL
a14b0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
a14c0 5a 45 20 33 35 30 0a 23 20 65 6e 64 69 66 0a 23 ZE 350.# endif.#
a14d0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 65 74 endif.#define et
a14e0 42 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 BUFSIZE SQLITE_P
a14f0 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f RINT_BUF_SIZE /
a1500 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 * Size of the ou
a1510 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a tput buffer */..
a1520 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 /*.** The root p
a1530 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 rogram. All var
a1540 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 iations call thi
a1550 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e s core..**.** IN
a1560 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 PUTS:.** func
a1570 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e This is a poin
a1580 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ter to a functio
a1590 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 65 20 61 n taking three a
a15a0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 rguments.**
a15b0 20 20 20 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 1. A poin
a15c0 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e ter to anything.
a15d0 20 20 53 61 6d 65 20 61 73 20 74 68 65 20 22 61 Same as the "a
a15e0 72 67 22 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a rg" parameter..*
a15f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 32 2e 20 * 2.
a1600 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
a1610 20 6c 69 73 74 20 6f 66 20 63 68 61 72 61 63 74 list of charact
a1620 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 ers to be output
a1630 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
a1640 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 (Note, this li
a1650 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 st is NOT null t
a1660 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 erminated.).**
a1670 20 20 20 20 20 20 20 20 20 20 33 2e 20 41 6e 20 3. An
a1680 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f integer number o
a1690 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 f characters to
a16a0 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 be output..**
a16b0 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 (Not
a16c0 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 20 6d e: This number m
a16d0 69 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a ight be zero.).*
a16e0 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 54 68 *.** arg Th
a16f0 69 73 20 69 73 20 74 68 65 20 70 6f 69 6e 74 65 is is the pointe
a1700 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 r to anything wh
a1710 69 63 68 20 77 69 6c 6c 20 62 65 20 70 61 73 73 ich will be pass
a1720 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 ed as the.**
a1730 20 20 20 20 20 20 66 69 72 73 74 20 61 72 67 75 first argu
a1740 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 ment to "func".
a1750 20 55 73 65 20 69 74 20 66 6f 72 20 77 68 61 74 Use it for what
a1760 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a ever you like..*
a1770 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 54 68 *.** fmt Th
a1780 69 73 20 69 73 20 74 68 65 20 66 6f 72 6d 61 74 is is the format
a1790 20 73 74 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 string, as in t
a17a0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a he usual print..
a17b0 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 20 54 **.** ap T
a17c0 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 his is a pointer
a17d0 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 61 72 to a list of ar
a17e0 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 guments. Same a
a17f0 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 s in.**
a1800 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 vfprint..**.**
a1810 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 OUTPUTS:.**
a1820 20 20 20 20 20 54 68 65 20 72 65 74 75 72 6e 20 The return
a1830 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 value is the tot
a1840 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 al number of cha
a1850 72 61 63 74 65 72 73 20 73 65 6e 74 20 74 6f 0a racters sent to.
a1860 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
a1870 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e function "func".
a1880 20 20 52 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 Returns -1 on
a1890 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e a error..**.** N
a18a0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 ote that the ord
a18b0 65 72 20 69 6e 20 77 68 69 63 68 20 61 75 74 6f er in which auto
a18c0 6d 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 matic variables
a18d0 61 72 65 20 64 65 63 6c 61 72 65 64 20 62 65 6c are declared bel
a18e0 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d ow.** seems to m
a18f0 61 6b 65 20 61 20 62 69 67 20 64 69 66 66 65 72 ake a big differ
a1900 65 6e 63 65 20 69 6e 20 64 65 74 65 72 6d 69 6e ence in determin
a1910 69 6e 67 20 68 6f 77 20 66 61 73 74 20 74 68 69 ing how fast thi
a1920 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 s beast.** will
a1930 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 run..*/.SQLITE_P
a1940 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a1950 74 65 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53 te3VXPrintf(. S
a1960 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c trAccum *pAccum,
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1980 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 /* Accumulate
a1990 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
a19a0 20 20 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65 int useExtende
a19b0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
a19c0 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 /* Allow ex
a19d0 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 tended %-convers
a19e0 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ions */. const
a19f0 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 char *fmt,
a1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a1a10 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f Format string */
a1a20 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20 . va_list ap
a1a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1a40 20 20 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e /* argumen
a1a50 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 ts */.){. int c
a1a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a1a70 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
a1a80 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 66 aracter in the f
a1a90 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a ormat string */.
a1aa0 20 20 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20 char *bufpt;
a1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a1ac0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 Pointer to the c
a1ad0 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 onversion buffer
a1ae0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73 */. int precis
a1af0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 ion;
a1b00 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 /* Precision of
a1b10 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 the current fie
a1b20 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 ld */. int leng
a1b30 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 th;
a1b40 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
a1b50 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 the field */. i
a1b60 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 nt idx;
a1b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67 /* A g
a1b80 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c eneral purpose l
a1b90 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
a1ba0 20 69 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20 int width;
a1bb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
a1bc0 69 64 74 68 20 6f 66 20 74 68 65 20 63 75 72 72 idth of the curr
a1bd0 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 ent field */. e
a1be0 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a tByte flag_leftj
a1bf0 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 ustify; /* Tru
a1c00 65 20 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73 e if "-" flag is
a1c10 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
a1c20 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 Byte flag_plussi
a1c30 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 gn; /* True
a1c40 20 69 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20 if "+" flag is
a1c50 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a1c60 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 yte flag_blanksi
a1c70 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 gn; /* True
a1c80 69 66 20 22 20 22 20 66 6c 61 67 20 69 73 20 70 if " " flag is p
a1c90 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
a1ca0 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 te flag_alternat
a1cb0 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 eform; /* True i
a1cc0 66 20 22 23 22 20 66 6c 61 67 20 69 73 20 70 72 f "#" flag is pr
a1cd0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
a1ce0 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b e flag_altform2;
a1cf0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a1d00 20 22 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 "!" flag is pre
a1d10 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a1d20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 flag_zeropad;
a1d30 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1d40 66 69 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 field width cons
a1d50 74 61 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 tant starts with
a1d60 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 zero */. etByt
a1d70 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 e flag_long;
a1d80 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a1d90 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 "l" flag is pre
a1da0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a1db0 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 flag_longlong;
a1dc0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1dd0 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 the "ll" flag is
a1de0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
a1df0 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 Byte done;
a1e00 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
a1e10 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 termination fla
a1e20 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 g */. sqlite_ui
a1e30 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 nt64 longvalue;
a1e40 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 /* Value for i
a1e50 6e 74 65 67 65 72 20 74 79 70 65 73 20 2a 2f 0a nteger types */.
a1e60 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 LONGDOUBLE_TYP
a1e70 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 E realvalue; /*
a1e80 56 61 6c 75 65 20 66 6f 72 20 72 65 61 6c 20 74 Value for real t
a1e90 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ypes */. const
a1ea0 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 et_info *infop;
a1eb0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
a1ec0 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 to the appropria
a1ed0 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 75 72 te info structur
a1ee0 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 75 66 5b e */. char buf[
a1ef0 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 etBUFSIZE];
a1f00 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 /* Conversion
a1f10 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 buffer */. char
a1f20 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
a1f30 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 /* Prefix
a1f40 20 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22 character. "+"
a1f50 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f or "-" or " " o
a1f60 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 r '\0'. */. etB
a1f70 79 74 65 20 78 74 79 70 65 20 3d 20 30 3b 20 20 yte xtype = 0;
a1f80 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 /* Conve
a1f90 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a rsion paradigm *
a1fa0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 /. char *zExtra
a1fb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a1fc0 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 * Extra memory u
a1fd0 73 65 64 20 66 6f 72 20 65 74 54 43 4c 45 53 43 sed for etTCLESC
a1fe0 41 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 APE conversions
a1ff0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
a2000 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
a2010 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 POINT. int exp
a2020 2c 20 65 32 3b 20 20 20 20 20 20 20 20 20 20 20 , e2;
a2030 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f /* exponent o
a2040 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a f real numbers *
a2050 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 /. double round
a2060 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f er; /
a2070 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 * Used for round
a2080 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 ing floating poi
a2090 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 nt values */. e
a20a0 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 tByte flag_dp;
a20b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a20c0 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 e if decimal poi
a20d0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f nt should be sho
a20e0 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 wn */. etByte f
a20f0 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 lag_rtz;
a2100 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 /* True if tr
a2110 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f ailing zeros sho
a2120 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a uld be removed *
a2130 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a2140 65 78 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f exp; /
a2150 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 63 65 20 * True to force
a2160 64 69 73 70 6c 61 79 20 6f 66 20 74 68 65 20 65 display of the e
a2170 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 xponent */. int
a2180 20 6e 73 64 3b 20 20 20 20 20 20 20 20 20 20 20 nsd;
a2190 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
a21a0 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 r of significant
a21b0 20 64 69 67 69 74 73 20 72 65 74 75 72 6e 65 64 digits returned
a21c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 */.#endif.. le
a21d0 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 ngth = 0;. bufp
a21e0 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 t = 0;. for(; (
a21f0 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b c=(*fmt))!=0; ++
a2200 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 63 21 fmt){. if( c!
a2210 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 69 6e ='%' ){. in
a2220 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 t amt;. buf
a2230 70 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 pt = (char *)fmt
a2240 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b ;. amt = 1;
a2250 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 . while( (c
a2260 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 =(*++fmt))!='%'
a2270 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b && c!=0 ) amt++;
a2280 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
a2290 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
a22a0 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 cum, bufpt, amt)
a22b0 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 ;. if( c==0
a22c0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a ) break;. }.
a22d0 20 20 20 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 if( (c=(*++f
a22e0 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 mt))==0 ){.
a22f0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a2300 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 Append(pAccum, "
a2310 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 %", 1);. br
a2320 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f eak;. }. /
a2330 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20 * Find out what
a2340 66 6c 61 67 73 20 61 72 65 20 70 72 65 73 65 6e flags are presen
a2350 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 t */. flag_le
a2360 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c 61 67 ftjustify = flag
a2370 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 _plussign = flag
a2380 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 _blanksign = .
a2390 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 flag_alternat
a23a0 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 eform = flag_alt
a23b0 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 form2 = flag_zer
a23c0 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f opad = 0;. do
a23d0 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a ne = 0;. do{.
a23e0 20 20 20 20 20 20 73 77 69 74 63 68 28 20 63 20 switch( c
a23f0 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 ){. case
a2400 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 '-': flag_left
a2410 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20 justify = 1;
a2420 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a2430 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 case '+': flag
a2440 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 _plussign = 1;
a2450 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a2460 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 20 case ' ':
a2470 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 flag_blanksign
a2480 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65 61 6b = 1; break
a2490 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
a24a0 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 #': flag_alter
a24b0 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 nateform = 1;
a24c0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
a24d0 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61 67 5f ase '!': flag_
a24e0 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 altform2 = 1;
a24f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a2500 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 20 20 case '0':
a2510 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 flag_zeropad = 1
a2520 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ; break;
a2530 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 . default
a2540 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 : done = 1;
a2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
a2560 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
a2570 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 }while( !done
a2580 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 && (c=(*++fmt))!
a2590 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 =0 );. /* Get
a25a0 20 74 68 65 20 66 69 65 6c 64 20 77 69 64 74 68 the field width
a25b0 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 3d 20 */. width =
a25c0 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 0;. if( c=='*
a25d0 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 ' ){. width
a25e0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 = va_arg(ap,int
a25f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77 69 64 );. if( wid
a2600 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 th<0 ){.
a2610 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a2620 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 69 = 1;. wi
a2630 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 dth = -width;.
a2640 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 }. c =
a2650 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 *++fmt;. }els
a2660 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 e{. while(
a2670 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 c>='0' && c<='9'
a2680 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 64 74 ){. widt
a2690 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b 20 63 h = width*10 + c
a26a0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 - '0';.
a26b0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
a26c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
a26d0 28 20 77 69 64 74 68 20 3e 20 65 74 42 55 46 53 ( width > etBUFS
a26e0 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 IZE-10 ){.
a26f0 77 69 64 74 68 20 3d 20 65 74 42 55 46 53 49 5a width = etBUFSIZ
a2700 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E-10;. }.
a2710 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65 63 69 /* Get the preci
a2720 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 sion */. if(
a2730 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 c=='.' ){.
a2740 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 precision = 0;.
a2750 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b c = *++fmt;
a2760 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 2a . if( c=='*
a2770 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 65 ' ){. pre
a2780 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 cision = va_arg(
a2790 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ap,int);.
a27a0 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 if( precision<0
a27b0 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d ) precision = -
a27c0 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 precision;.
a27d0 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a27e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a27f0 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 while( c>='0
a2800 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 ' && c<='9' ){.
a2810 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 precisi
a2820 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 on = precision*1
a2830 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 0 + c - '0';.
a2840 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d c = *++fm
a2850 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
a2860 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a2870 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
a2880 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = -1;. }.
a2890 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 76 65 /* Get the conve
a28a0 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 rsion type modif
a28b0 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 ier */. if( c
a28c0 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 =='l' ){. f
a28d0 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 lag_long = 1;.
a28e0 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
a28f0 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 if( c=='l'
a2900 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 ){. flag
a2910 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 _longlong = 1;.
a2920 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d c = *++fm
a2930 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a t;. }else{.
a2940 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e flag_lon
a2950 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 glong = 0;.
a2960 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
a2970 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 flag_long =
a2980 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 flag_longlong =
a2990 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 0;. }. /*
a29a0 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f 20 65 Fetch the info e
a29b0 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 65 ntry for the fie
a29c0 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 ld */. infop
a29d0 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 = &fmtinfo[0];.
a29e0 20 20 20 78 74 79 70 65 20 3d 20 65 74 49 4e 56 xtype = etINV
a29f0 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 28 69 64 ALID;. for(id
a2a00 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79 53 69 x=0; idx<ArraySi
a2a10 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 ze(fmtinfo); idx
a2a20 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 ++){. if( c
a2a30 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 ==fmtinfo[idx].f
a2a40 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 mttype ){.
a2a50 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e infop = &fmtin
a2a60 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 fo[idx];.
a2a70 20 69 66 28 20 75 73 65 45 78 74 65 6e 64 65 64 if( useExtended
a2a80 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 || (infop->flag
a2a90 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 s & FLAG_INTERN)
a2aa0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
a2ab0 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e xtype = infop->
a2ac0 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 type;. }e
a2ad0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 lse{. r
a2ae0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d eturn;. }
a2af0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a2b00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
a2b10 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a zExtra = 0;...
a2b20 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 /* Limit the
a2b30 20 70 72 65 63 69 73 69 6f 6e 20 74 6f 20 70 72 precision to pr
a2b40 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e event overflowin
a2b50 67 20 62 75 66 5b 5d 20 64 75 72 69 6e 67 20 63 g buf[] during c
a2b60 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 onversion */.
a2b70 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 if( precision>e
a2b80 74 42 55 46 53 49 5a 45 2d 34 30 20 26 26 20 28 tBUFSIZE-40 && (
a2b90 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 infop->flags & F
a2ba0 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29 LAG_STRING)==0 )
a2bb0 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f {. precisio
a2bc0 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 34 30 n = etBUFSIZE-40
a2bd0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a ;. }.. /*.
a2be0 20 20 20 20 2a 2a 20 41 74 20 74 68 69 73 20 70 ** At this p
a2bf0 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c 65 73 20 oint, variables
a2c00 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 are initialized
a2c10 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 as follows:.
a2c20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 **. ** flag
a2c30 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 20 _alternateform
a2c40 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a2c50 61 20 27 23 27 20 69 73 20 70 72 65 73 65 6e 74 a '#' is present
a2c60 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a2c70 61 6c 74 66 6f 72 6d 32 20 20 20 20 20 20 20 20 altform2
a2c80 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 TRUE if a
a2c90 20 27 21 27 20 69 73 20 70 72 65 73 65 6e 74 2e '!' is present.
a2ca0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70 . ** flag_p
a2cb0 6c 75 73 73 69 67 6e 20 20 20 20 20 20 20 20 20 lussign
a2cc0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 TRUE if a
a2cd0 27 2b 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a '+' is present..
a2ce0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 ** flag_le
a2cf0 66 74 6a 75 73 74 69 66 79 20 20 20 20 20 20 20 ftjustify
a2d00 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 TRUE if a '
a2d10 2d 27 20 69 73 20 70 72 65 73 65 6e 74 20 6f 72 -' is present or
a2d20 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 20 if the. **
a2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d40 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 65 fie
a2d50 6c 64 20 77 69 64 74 68 20 77 61 73 20 6e 65 67 ld width was neg
a2d60 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20 ative.. **
a2d70 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 20 20 20 flag_zeropad
a2d80 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 TRUE
a2d90 20 69 66 20 74 68 65 20 77 69 64 74 68 20 62 65 if the width be
a2da0 67 61 6e 20 77 69 74 68 20 30 2e 0a 20 20 20 20 gan with 0..
a2db0 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 ** flag_long
a2dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2dd0 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65 74 TRUE if the let
a2de0 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 20 70 72 ter 'l' (ell) pr
a2df0 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 efixed. **
a2e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e10 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 the
a2e20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 conversion chara
a2e30 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 cter.. ** f
a2e40 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 lag_longlong
a2e50 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 TRUE
a2e60 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c if the letter 'l
a2e70 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65 l' (ell ell) pre
a2e80 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 fixed. **
a2e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2ea0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63 the c
a2eb0 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 onversion charac
a2ec0 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c ter.. ** fl
a2ed0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20 ag_blanksign
a2ee0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 TRUE i
a2ef0 66 20 61 20 27 20 27 20 69 73 20 70 72 65 73 65 f a ' ' is prese
a2f00 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 77 69 64 nt.. ** wid
a2f10 74 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 th
a2f20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65 The spe
a2f30 63 69 66 69 65 64 20 66 69 65 6c 64 20 77 69 64 cified field wid
a2f40 74 68 2e 20 20 54 68 69 73 20 69 73 0a 20 20 20 th. This is.
a2f50 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a2f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2f70 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 always non-neg
a2f80 61 74 69 76 65 2e 20 20 5a 65 72 6f 20 69 73 20 ative. Zero is
a2f90 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20 20 the default..
a2fa0 20 2a 2a 20 20 20 70 72 65 63 69 73 69 6f 6e 20 ** precision
a2fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2fc0 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 The specified
a2fd0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 precision. The
a2fe0 64 65 66 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20 default. **
a2ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3000 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 is
a3010 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20 78 74 79 -1.. ** xty
a3020 70 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 pe
a3030 20 20 20 20 20 20 20 20 20 54 68 65 20 63 6c 61 The cla
a3040 73 73 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 ss of the conver
a3050 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69 sion.. ** i
a3060 6e 66 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 nfop
a3070 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 Point
a3080 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 er to the approp
a3090 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 riate info struc
a30a0 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 77 t.. */. sw
a30b0 69 74 63 68 28 20 78 74 79 70 65 20 29 7b 0a 20 itch( xtype ){.
a30c0 20 20 20 20 20 63 61 73 65 20 65 74 50 4f 49 4e case etPOIN
a30d0 54 45 52 3a 0a 20 20 20 20 20 20 20 20 66 6c 61 TER:. fla
a30e0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a g_longlong = siz
a30f0 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 eof(char*)==size
a3100 6f 66 28 69 36 34 29 3b 0a 20 20 20 20 20 20 20 of(i64);.
a3110 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a flag_long = siz
a3120 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65 eof(char*)==size
a3130 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 of(long int);.
a3140 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 /* Fall th
a3150 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65 20 6e rough into the n
a3160 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 ext case */.
a3170 20 20 63 61 73 65 20 65 74 4f 52 44 49 4e 41 4c case etORDINAL
a3180 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 52 :. case etR
a3190 41 44 49 58 3a 0a 20 20 20 20 20 20 20 20 69 66 ADIX:. if
a31a0 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 ( infop->flags &
a31b0 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a FLAG_SIGNED ){.
a31c0 20 20 20 20 20 20 20 20 20 20 69 36 34 20 76 3b i64 v;
a31d0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 . if( f
a31e0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a lag_longlong ){.
a31f0 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 v =
a3200 76 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a va_arg(ap,i64);.
a3210 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 }else
a3220 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b if( flag_long ){
a3230 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d . v =
a3240 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 va_arg(ap,long
a3250 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 int);.
a3260 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3270 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61 70 v = va_arg(ap
a3280 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 ,int);.
a3290 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 }. if(
a32a0 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 v<0 ){.
a32b0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
a32c0 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 -v;.
a32d0 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 prefix = '-';.
a32e0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a32f0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 longv
a3300 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 20 20 20 alue = v;.
a3310 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 if( flag_p
a3320 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 lussign )
a3330 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 prefix = '+';.
a3340 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 else
a3350 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 if( flag_blanksi
a3360 67 6e 20 29 20 20 70 72 65 66 69 78 20 3d 20 27 gn ) prefix = '
a3370 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ';.
a3380 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 else
a3390 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 prefi
a33a0 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 x = 0;.
a33b0 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
a33c0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
a33d0 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b flag_longlong ){
a33e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
a33f0 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 gvalue = va_arg(
a3400 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20 ap,u64);.
a3410 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 }else if( fla
a3420 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 g_long ){.
a3430 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
a3440 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 = va_arg(ap,unsi
a3450 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a gned long int);.
a3460 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
a3470 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
a3480 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 gvalue = va_arg(
a3490 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 ap,unsigned int)
a34a0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a34b0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
a34c0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0;. }.
a34d0 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 76 61 if( longva
a34e0 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c lue==0 ) flag_al
a34f0 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b ternateform = 0;
a3500 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 . if( fla
a3510 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 70 72 65 g_zeropad && pre
a3520 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72 cision<width-(pr
a3530 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 20 20 20 efix!=0) ){.
a3540 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
a3550 3d 20 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 = width-(prefix!
a3560 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 =0);. }.
a3570 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 26 bufpt = &
a3580 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d buf[etBUFSIZE-1]
a3590 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 ;. if( xt
a35a0 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29 ype==etORDINAL )
a35b0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 {. stat
a35c0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f ic const char zO
a35d0 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e 64 72 64 rd[] = "thstndrd
a35e0 22 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ";. int
a35f0 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76 x = (int)(longv
a3600 61 6c 75 65 20 25 20 31 30 29 3b 0a 20 20 20 20 alue % 10);.
a3610 20 20 20 20 20 20 69 66 28 20 78 3e 3d 34 20 7c if( x>=4 |
a3620 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 | (longvalue/10)
a3630 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 %10==1 ){.
a3640 20 20 20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 x = 0;.
a3650 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3660 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 buf[etBUFSIZE
a3670 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b -3] = zOrd[x*2];
a3680 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 . buf[e
a3690 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f tBUFSIZE-2] = zO
a36a0 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 rd[x*2+1];.
a36b0 20 20 20 20 20 62 75 66 70 74 20 2d 3d 20 32 3b bufpt -= 2;
a36c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a36d0 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72 {. r
a36e0 65 67 69 73 74 65 72 20 63 6f 6e 73 74 20 63 68 egister const ch
a36f0 61 72 20 2a 63 73 65 74 3b 20 20 20 20 20 20 2f ar *cset; /
a3700 2a 20 55 73 65 20 72 65 67 69 73 74 65 72 73 20 * Use registers
a3710 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 for speed */.
a3720 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 register
a3730 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 20 20 int base;.
a3740 20 20 20 20 63 73 65 74 20 3d 20 26 61 44 69 67 cset = &aDig
a3750 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 its[infop->chars
a3760 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 et];. b
a3770 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 ase = infop->bas
a3780 65 3b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b e;. do{
a3790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a37a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a37b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
a37c0 6e 76 65 72 74 20 74 6f 20 61 73 63 69 69 20 2a nvert to ascii *
a37d0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 /. *(
a37e0 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 65 74 5b --bufpt) = cset[
a37f0 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 65 5d 3b longvalue%base];
a3800 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
a3810 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c gvalue = longval
a3820 75 65 2f 62 61 73 65 3b 0a 20 20 20 20 20 20 20 ue/base;.
a3830 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 }while( longv
a3840 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 alue>0 );.
a3850 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 }. leng
a3860 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b th = (int)(&buf[
a3870 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 etBUFSIZE-1]-buf
a3880 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 pt);. for
a3890 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c (idx=precision-l
a38a0 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69 64 ength; idx>0; id
a38b0 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 x--){.
a38c0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 30 27 *(--bufpt) = '0'
a38d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a38e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a38f0 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20 Zero pad */.
a3900 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a3910 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 2d 2d f( prefix ) *(--
a3920 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 78 3b bufpt) = prefix;
a3930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a3940 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20 * Add sign */.
a3950 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 if( flag_a
a3960 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 26 20 lternateform &&
a3970 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b infop->prefix ){
a3980 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 30 22 /* Add "0"
a3990 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 20 20 or "0x" */.
a39a0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
a39b0 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 20 20 *pre;.
a39c0 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 20 20 char x;.
a39d0 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 66 69 pre = &aPrefi
a39e0 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d x[infop->prefix]
a39f0 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
a3a00 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b ; (x=(*pre))!=0;
a3a10 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 pre++) *(--bufp
a3a20 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 t) = x;.
a3a30 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 }. length
a3a40 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 = (int)(&buf[et
a3a50 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 BUFSIZE-1]-bufpt
a3a60 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
a3a70 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46 ;. case etF
a3a80 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65 LOAT:. case
a3a90 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61 etEXP:. ca
a3aa0 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 se etGENERIC:.
a3ab0 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20 realvalue
a3ac0 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 = va_arg(ap,doub
a3ad0 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c le);.#ifndef SQL
a3ae0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a3af0 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 G_POINT.
a3b00 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 if( precision<0
a3b10 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b ) precision = 6;
a3b20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 /* Set
a3b30 64 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f default precisio
a3b40 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 n */. if(
a3b50 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 precision>etBUF
a3b60 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63 SIZE/2-10 ) prec
a3b70 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a ision = etBUFSIZ
a3b80 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 E/2-10;.
a3b90 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e if( realvalue<0.
a3ba0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
a3bb0 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c ealvalue = -real
a3bc0 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20 value;.
a3bd0 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 prefix = '-';.
a3be0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a3bf0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a3c00 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 _plussign )
a3c10 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b prefix = '+
a3c20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 ';. els
a3c30 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b e if( flag_blank
a3c40 73 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78 sign ) prefix
a3c50 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 = ' ';.
a3c60 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
a3c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
a3c80 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 refix = 0;.
a3c90 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
a3ca0 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 xtype==etGENERI
a3cb0 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 C && precision>0
a3cc0 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a ) precision--;.
a3cd0 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a #if 0. /*
a3ce0 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 Rounding works
a3cf0 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68 like BSD when th
a3d00 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 e constant 0.499
a3d10 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72 9 is used. Wier
a3d20 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f d! */. fo
a3d30 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c r(idx=precision,
a3d40 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b rounder=0.4999;
a3d50 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 idx>0; idx--, r
a3d60 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 ounder*=0.1);.#e
a3d70 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 lse. /* I
a3d80 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e t makes more sen
a3d90 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f se to use 0.5 */
a3da0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 . for(idx
a3db0 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e =precision, roun
a3dc0 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 der=0.5; idx>0;
a3dd0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d idx--, rounder*=
a3de0 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 0.1){}.#endif.
a3df0 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
a3e00 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 =etFLOAT ) realv
a3e10 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b alue += rounder;
a3e20 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d . /* Norm
a3e30 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 alize realvalue
a3e40 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e to within 10.0 >
a3e50 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e realvalue >= 1.
a3e60 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70 0 */. exp
a3e70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
a3e80 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 28 ( sqlite3IsNaN((
a3e90 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 6c 75 65 double)realvalue
a3ea0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 ) ){. b
a3eb0 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 ufpt = "NaN";.
a3ec0 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a3ed0 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 3;. br
a3ee0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
a3ef0 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 if( realv
a3f00 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20 alue>0.0 ){.
a3f10 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 while( rea
a3f20 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26 26 20 lvalue>=1e32 &&
a3f30 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c exp<=350 ){ real
a3f40 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20 value *= 1e-32;
a3f50 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20 exp+=32; }.
a3f60 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c while( real
a3f70 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20 65 78 value>=1e8 && ex
a3f80 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 p<=350 ){ realva
a3f90 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70 lue *= 1e-8; exp
a3fa0 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 +=8; }.
a3fb0 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 while( realvalu
a3fc0 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70 3c 3d e>=10.0 && exp<=
a3fd0 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 350 ){ realvalue
a3fe0 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 *= 0.1; exp++;
a3ff0 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c }. whil
a4000 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d e( realvalue<1e-
a4010 38 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 8 ){ realvalue *
a4020 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d = 1e8; exp-=8; }
a4030 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a4040 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 ( realvalue<1.0
a4050 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
a4060 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 10.0; exp--; }.
a4070 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 if( exp
a4080 3e 33 35 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >350 ){.
a4090 20 20 20 20 69 66 28 20 70 72 65 66 69 78 3d 3d if( prefix==
a40a0 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 '-' ){.
a40b0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2d 49 bufpt = "-I
a40c0 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nf";.
a40d0 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65 66 69 }else if( prefi
a40e0 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 x=='+' ){.
a40f0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a4100 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 "+Inf";.
a4110 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a4120 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d bufpt =
a4130 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 "Inf";.
a4140 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a4150 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 length = sqlit
a4160 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 e3Strlen30(bufpt
a4170 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 );. b
a4180 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
a4190 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
a41a0 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b bufpt = buf;
a41b0 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 . /*.
a41c0 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 ** If the fi
a41d0 65 6c 64 20 74 79 70 65 20 69 73 20 65 74 47 45 eld type is etGE
a41e0 4e 45 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 NERIC, then conv
a41f0 65 72 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 ert to either et
a4200 45 58 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f EXP. ** o
a4210 72 20 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 r etFLOAT, as ap
a4220 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 propriate..
a4230 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c */. fl
a4240 61 67 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d ag_exp = xtype==
a4250 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 etEXP;. i
a4260 66 28 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 f( xtype!=etFLOA
a4270 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 T ){. r
a4280 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e ealvalue += roun
a4290 64 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 der;. i
a42a0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 f( realvalue>=10
a42b0 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 .0 ){ realvalue
a42c0 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d *= 0.1; exp++; }
a42d0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a42e0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 if( xtype==et
a42f0 47 45 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 GENERIC ){.
a4300 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 flag_rtz =
a4310 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 !flag_alternatef
a4320 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 orm;. i
a4330 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 f( exp<-4 || exp
a4340 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 >precision ){.
a4350 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 xtype
a4360 3d 20 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 = etEXP;.
a4370 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4380 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
a4390 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 = precision - ex
a43a0 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 p;. x
a43b0 74 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a type = etFLOAT;.
a43c0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a43d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a43e0 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 flag_rtz =
a43f0 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
a4400 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
a4410 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 etEXP ){.
a4420 20 20 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 e2 = 0;.
a4430 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4440 20 20 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 e2 = exp;.
a4450 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4460 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 nsd = 0;.
a4470 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 flag_dp = (prec
a4480 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 ision>0 ?1:0) |
a4490 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a44a0 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72 rm | flag_altfor
a44b0 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 m2;. /* T
a44c0 68 65 20 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74 he sign in front
a44d0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a of the number *
a44e0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 /. if( pr
a44f0 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20 20 20 efix ){.
a4500 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 70 *(bufpt++) = p
a4510 72 65 66 69 78 3b 0a 20 20 20 20 20 20 20 20 7d refix;. }
a4520 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 67 69 . /* Digi
a4530 74 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ts prior to the
a4540 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f decimal point */
a4550 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 32 3c . if( e2<
a4560 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 0 ){. *
a4570 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b (bufpt++) = '0';
a4580 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
a4590 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 for(;
a45a0 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 e2>=0; e2--){.
a45b0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a45c0 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 t++) = et_getdig
a45d0 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e it(&realvalue,&n
a45e0 73 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d sd);. }
a45f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a4600 20 20 20 2f 2a 20 54 68 65 20 64 65 63 69 6d 61 /* The decima
a4610 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 l point */.
a4620 20 20 20 69 66 28 20 66 6c 61 67 5f 64 70 20 29 if( flag_dp )
a4630 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a4640 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 fpt++) = '.';.
a4650 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4660 2f 2a 20 22 30 22 20 64 69 67 69 74 73 20 61 66 /* "0" digits af
a4670 74 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 ter the decimal
a4680 70 6f 69 6e 74 20 62 75 74 20 62 65 66 6f 72 65 point but before
a4690 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 the first.
a46a0 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e ** significan
a46b0 74 20 64 69 67 69 74 20 6f 66 20 74 68 65 20 6e t digit of the n
a46c0 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 umber */.
a46d0 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b for(e2++; e2<0;
a46e0 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 precision--, e2
a46f0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 ++){. a
a4700 73 73 65 72 74 28 20 70 72 65 63 69 73 69 6f 6e ssert( precision
a4710 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 >0 );.
a4720 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 *(bufpt++) = '0'
a4730 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a4740 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69 63 61 /* Significa
a4750 6e 74 20 64 69 67 69 74 73 20 61 66 74 65 72 20 nt digits after
a4760 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e the decimal poin
a4770 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 t */. whi
a4780 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d le( (precision--
a4790 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 )>0 ){.
a47a0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 74 *(bufpt++) = et
a47b0 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c 76 _getdigit(&realv
a47c0 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20 alue,&nsd);.
a47d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
a47e0 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67 Remove trailing
a47f0 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65 20 22 zeros and the "
a4800 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74 73 20 ." if no digits
a4810 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a follow the "." *
a4820 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c /. if( fl
a4830 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67 5f 64 ag_rtz && flag_d
a4840 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 p ){. w
a4850 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d hile( bufpt[-1]=
a4860 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66 70 74 ='0' ) *(--bufpt
a4870 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 ) = 0;.
a4880 20 61 73 73 65 72 74 28 20 62 75 66 70 74 3e 62 assert( bufpt>b
a4890 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 uf );.
a48a0 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 if( bufpt[-1]=='
a48b0 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 .' ){.
a48c0 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f if( flag_altfo
a48d0 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rm2 ){.
a48e0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a48f0 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 = '0';.
a4900 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4910 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 *(--bufp
a4920 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 t) = 0;.
a4930 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a4940 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
a4950 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 22 /* Add the "
a4960 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a eNNN" suffix */.
a4970 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a4980 5f 65 78 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65 _exp || xtype==e
a4990 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 tEXP ){.
a49a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 61 *(bufpt++) = a
a49b0 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 Digits[infop->ch
a49c0 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 arset];.
a49d0 20 20 69 66 28 20 65 78 70 3c 30 20 29 7b 0a 20 if( exp<0 ){.
a49e0 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 *(buf
a49f0 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70 pt++) = '-'; exp
a4a00 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 20 20 20 = -exp;.
a4a10 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4a20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a4a30 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 = '+';.
a4a40 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 }. if
a4a50 28 20 65 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20 ( exp>=100 ){.
a4a60 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a4a70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 28 65 t++) = (char)((e
a4a80 78 70 2f 31 30 30 29 2b 27 30 27 29 3b 20 20 20 xp/100)+'0');
a4a90 20 20 20 20 20 2f 2a 20 31 30 30 27 73 20 64 69 /* 100's di
a4aa0 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 git */.
a4ab0 20 20 20 65 78 70 20 25 3d 20 31 30 30 3b 0a 20 exp %= 100;.
a4ac0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a4ad0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a4ae0 3d 20 28 63 68 61 72 29 28 65 78 70 2f 31 30 2b = (char)(exp/10+
a4af0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20 '0');
a4b00 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69 74 20 /* 10's digit
a4b10 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 */. *(b
a4b20 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
a4b30 28 65 78 70 25 31 30 2b 27 30 27 29 3b 20 20 20 (exp%10+'0');
a4b40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 27 73 /* 1's
a4b50 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 digit */.
a4b60 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 62 75 66 }. *buf
a4b70 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 pt = 0;..
a4b80 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 72 74 65 /* The converte
a4b90 64 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62 d number is in b
a4ba0 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65 uf[] and zero te
a4bb0 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 70 75 74 rminated. Output
a4bc0 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 it.. **
a4bd0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 75 Note that the nu
a4be0 6d 62 65 72 20 69 73 20 69 6e 20 74 68 65 20 75 mber is in the u
a4bf0 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20 sual order, not
a4c00 72 65 76 65 72 73 65 64 20 61 73 20 77 69 74 68 reversed as with
a4c10 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 . ** inte
a4c20 67 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e ger conversions.
a4c30 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 */. leng
a4c40 74 68 20 3d 20 28 69 6e 74 29 28 62 75 66 70 74 th = (int)(bufpt
a4c50 2d 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 62 -buf);. b
a4c60 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20 ufpt = buf;..
a4c70 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 /* Special
a4c80 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61 64 69 case: Add leadi
a4c90 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68 65 20 ng zeros if the
a4ca0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61 flag_zeropad fla
a4cb0 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 g is. **
a4cc0 73 65 74 20 61 6e 64 20 77 65 20 61 72 65 20 6e set and we are n
a4cd0 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66 69 65 ot left justifie
a4ce0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 d */. if(
a4cf0 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 flag_zeropad &&
a4d00 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 !flag_leftjusti
a4d10 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c 20 77 fy && length < w
a4d20 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20 20 20 idth){.
a4d30 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 int i;.
a4d40 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77 69 64 int nPad = wid
a4d50 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 th - length;.
a4d60 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77 69 64 for(i=wid
a4d70 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d th; i>=nPad; i--
a4d80 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 ){. b
a4d90 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b ufpt[i] = bufpt[
a4da0 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20 20 20 i-nPad];.
a4db0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
a4dc0 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a 20 20 = prefix!=0;.
a4dd0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e while( n
a4de0 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b Pad-- ) bufpt[i+
a4df0 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +] = '0';.
a4e00 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 length = wid
a4e10 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 th;. }.#e
a4e20 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62 72 65 ndif. bre
a4e30 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
a4e40 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a tSIZE:. *
a4e50 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29 (va_arg(ap,int*)
a4e60 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 ) = pAccum->nCha
a4e70 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 r;. lengt
a4e80 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 h = width = 0;.
a4e90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4ea0 20 20 20 20 63 61 73 65 20 65 74 50 45 52 43 45 case etPERCE
a4eb0 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75 66 5b NT:. buf[
a4ec0 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20 20 20 0] = '%';.
a4ed0 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 bufpt = buf;.
a4ee0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a4ef0 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 1;. break
a4f00 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 43 ;. case etC
a4f10 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20 63 20 HARX:. c
a4f20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 = va_arg(ap,int)
a4f30 3b 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d ;. buf[0]
a4f40 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20 = (char)c;.
a4f50 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a4f60 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 n>=0 ){.
a4f70 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 64 78 for(idx=1; idx
a4f80 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b <precision; idx+
a4f90 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 28 63 +) buf[idx] = (c
a4fa0 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 20 har)c;.
a4fb0 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 length = precis
a4fc0 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ion;. }el
a4fd0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 se{. le
a4fe0 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 ngth =1;.
a4ff0 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 }. bufpt
a5000 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 = buf;.
a5010 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
a5020 65 20 65 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 e etSTRING:.
a5030 20 20 63 61 73 65 20 65 74 44 59 4e 53 54 52 49 case etDYNSTRI
a5040 4e 47 3a 0a 20 20 20 20 20 20 20 20 62 75 66 70 NG:. bufp
a5050 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68 t = va_arg(ap,ch
a5060 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ar*);. if
a5070 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 ( bufpt==0 ){.
a5080 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a5090 22 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 "";. }els
a50a0 65 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 44 e if( xtype==etD
a50b0 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 YNSTRING ){.
a50c0 20 20 20 20 20 20 7a 45 78 74 72 61 20 3d 20 62 zExtra = b
a50d0 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a ufpt;. }.
a50e0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 if( prec
a50f0 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 ision>=0 ){.
a5100 20 20 20 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 for(length
a5110 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 =0; length<preci
a5120 73 69 6f 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 sion && bufpt[le
a5130 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 ngth]; length++)
a5140 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 {}. }else
a5150 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 {. leng
a5160 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c th = sqlite3Strl
a5170 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 en30(bufpt);.
a5180 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
a5190 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
a51a0 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a 20 20 etSQLESCAPE:.
a51b0 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 case etSQLES
a51c0 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63 61 73 CAPE2:. cas
a51d0 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 3a 20 e etSQLESCAPE3:
a51e0 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 2c {. int i,
a51f0 20 6a 2c 20 6b 2c 20 6e 2c 20 69 73 6e 75 6c 6c j, k, n, isnull
a5200 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65 ;. int ne
a5210 65 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 edQuote;.
a5220 20 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20 char ch;.
a5230 20 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79 char q = ((xty
a5240 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33 pe==etSQLESCAPE3
a5250 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f )?'"':'\''); /
a5260 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65 * Quote characte
a5270 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 r */. cha
a5280 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61 r *escarg = va_a
a5290 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 rg(ap,char*);.
a52a0 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65 isnull = e
a52b0 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20 scarg==0;.
a52c0 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65 if( isnull ) e
a52d0 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d scarg = (xtype==
a52e0 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22 etSQLESCAPE2 ? "
a52f0 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22 NULL" : "(NULL)"
a5300 29 3b 0a 20 20 20 20 20 20 20 20 6b 20 3d 20 70 );. k = p
a5310 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 recision;.
a5320 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 for(i=n=0; (ch
a5330 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 20 26 =escarg[i])!=0 &
a5340 26 20 6b 21 3d 30 3b 20 69 2b 2b 2c 20 6b 2d 2d & k!=0; i++, k--
a5350 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
a5360 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 ch==q ) n++;.
a5370 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a5380 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 needQuote = !is
a5390 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 null && xtype==e
a53a0 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 tSQLESCAPE2;.
a53b0 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 n += i + 1
a53c0 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 + needQuote*2;.
a53d0 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 if( n>etB
a53e0 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 UFSIZE ){.
a53f0 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 bufpt = zExt
a5400 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ra = sqlite3Mall
a5410 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 oc( n );.
a5420 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 if( bufpt==0
a5430 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
a5440 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 Accum->mallocFai
a5450 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 led = 1;.
a5460 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
a5470 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a5480 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a5490 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 bufpt = buf;.
a54a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a54b0 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 j = 0;.
a54c0 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 if( needQuote )
a54d0 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a bufpt[j++] = q;.
a54e0 20 20 20 20 20 20 20 20 6b 20 3d 20 69 3b 0a 20 k = i;.
a54f0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
a5500 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<k; i++){.
a5510 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 bufpt[j++]
a5520 3d 20 63 68 20 3d 20 65 73 63 61 72 67 5b 69 5d = ch = escarg[i]
a5530 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a5540 63 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b ch==q ) bufpt[j+
a5550 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 +] = ch;.
a5560 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e }. if( n
a5570 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 eedQuote ) bufpt
a5580 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 [j++] = q;.
a5590 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b bufpt[j] = 0;
a55a0 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a55b0 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 = j;. /*
a55c0 54 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69 6e The precision in
a55d0 20 25 71 20 61 6e 64 20 25 51 20 6d 65 61 6e 73 %q and %Q means
a55e0 20 68 6f 77 20 6d 61 6e 79 20 69 6e 70 75 74 20 how many input
a55f0 63 68 61 72 61 63 74 65 72 73 20 74 6f 0a 20 20 characters to.
a5600 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 75 6d 65 ** consume
a5610 2c 20 6e 6f 74 20 74 68 65 20 6c 65 6e 67 74 68 , not the length
a5620 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2e 2e of the output..
a5630 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 28 .. ** if(
a5640 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 precision>=0 &&
a5650 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 precision<lengt
a5660 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 h ) length = pre
a5670 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 cision; */.
a5680 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a5690 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 }. case etT
a56a0 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 OKEN: {.
a56b0 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 Token *pToken =
a56c0 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e va_arg(ap, Token
a56d0 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *);. if(
a56e0 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 pToken ){.
a56f0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a5700 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a5710 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 , (const char*)p
a5720 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e Token->z, pToken
a5730 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ->n);. }.
a5740 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a5750 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
a5760 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a5770 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 }. case et
a5780 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 SRCLIST: {.
a5790 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 SrcList *pSrc
a57a0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 = va_arg(ap, Sr
a57b0 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 cList*);.
a57c0 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 int k = va_arg(
a57d0 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ap, int);.
a57e0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
a57f0 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 _item *pItem = &
a5800 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 pSrc->a[k];.
a5810 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 assert( k>=0
a5820 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 && k<pSrc->nSrc
a5830 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
a5840 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 pItem->zDatabase
a5850 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
a5860 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a5870 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 end(pAccum, pIte
a5880 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 m->zDatabase, -1
a5890 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
a58a0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a58b0 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 nd(pAccum, ".",
a58c0 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1);. }.
a58d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a58e0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
a58f0 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 um, pItem->zName
a5900 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c , -1);. l
a5910 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 ength = width =
a5920 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 0;. break
a5930 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
a5940 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
a5950 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 assert( xtype
a5960 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 ==etINVALID );.
a5970 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a5980 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 }. }/* E
a5990 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 nd switch over t
a59a0 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a he format type *
a59b0 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 /. /*. **
a59c0 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 The text of the
a59d0 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
a59e0 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 inted to by "buf
a59f0 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a pt" and is. *
a5a00 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 * "length" chara
a5a10 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 cters long. The
a5a20 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 field width is
a5a30 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 "width". Do.
a5a40 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a ** the output..
a5a50 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
a5a60 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a5a70 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 ){. regist
a5a80 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 er int nspace;.
a5a90 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 nspace = wi
a5aa0 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 dth-length;.
a5ab0 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 if( nspace>0 )
a5ac0 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 {. append
a5ad0 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 Space(pAccum, ns
a5ae0 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pace);. }.
a5af0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e }. if( len
a5b00 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 gth>0 ){. s
a5b10 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a5b20 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 pend(pAccum, buf
a5b30 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 pt, length);.
a5b40 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f }. if( flag_
a5b50 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 leftjustify ){.
a5b60 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
a5b70 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 t nspace;.
a5b80 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c nspace = width-l
a5b90 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 ength;. if(
a5ba0 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 nspace>0 ){.
a5bb0 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 appendSpace
a5bc0 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 (pAccum, nspace)
a5bd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a5be0 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 if( zExtra )
a5bf0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
a5c00 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 free(zExtra);.
a5c10 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f }. }/* End fo
a5c20 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 r loop over the
a5c30 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f format string */
a5c40 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e .} /* End of fun
a5c50 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ction */../*.**
a5c60 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f Append N bytes o
a5c70 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f f text from z to
a5c80 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 the StrAccum ob
a5c90 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
a5ca0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a5cb0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a5cc0 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 nd(StrAccum *p,
a5cd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
a5ce0 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt N){. assert(
a5cf0 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b z!=0 || N==0 );
a5d00 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 . if( p->tooBig
a5d10 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | p->mallocFail
a5d20 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 ed ){. testca
a5d30 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 se(p->tooBig);.
a5d40 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d testcase(p->m
a5d50 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 allocFailed);.
a5d60 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
a5d70 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e if( N<0 ){. N
a5d80 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
a5d90 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 30(z);. }. if(
a5da0 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a N==0 || NEVER(z
a5db0 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ==0) ){. retu
a5dc0 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d rn;. }. if( p-
a5dd0 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e >nChar+N >= p->n
a5de0 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 Alloc ){. cha
a5df0 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 r *zNew;. if(
a5e00 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 !p->useMalloc )
a5e10 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 {. p->tooBi
a5e20 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d g = 1;. N =
a5e30 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e p->nAlloc - p->
a5e40 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 nChar - 1;.
a5e50 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 if( N<=0 ){.
a5e60 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
a5e70 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a5e80 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 i64 szNew
a5e90 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 = p->nChar;.
a5ea0 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 szNew += N + 1
a5eb0 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 ;. if( szNe
a5ec0 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 w > p->mxAlloc )
a5ed0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
a5ee0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 3StrAccumReset(p
a5ef0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f );. p->to
a5f00 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 oBig = 1;.
a5f10 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
a5f20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
a5f30 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 ->nAlloc = (int)
a5f40 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 szNew;. }.
a5f50 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 zNew = sqli
a5f60 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
a5f70 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 ->db, p->nAlloc
a5f80 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 );. if( zNe
a5f90 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d w ){. mem
a5fa0 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 cpy(zNew, p->zTe
a5fb0 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 xt, p->nChar);.
a5fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
a5fd0 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
a5fe0 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 p->zText
a5ff0 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d = zNew;. }
a6000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
a6010 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
a6020 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 1;. sqlit
a6030 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
a6040 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
a6050 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
a6060 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 }. }. memcpy(&
a6070 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 p->zText[p->nCha
a6080 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e r], z, N);. p->
a6090 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f nChar += N;.}../
a60a0 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 *.** Finish off
a60b0 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 a string by maki
a60c0 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 ng sure it is ze
a60d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a ro-terminated..*
a60e0 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
a60f0 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 er to the result
a6100 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 ing string. Ret
a6110 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f urn a NULL.** po
a6120 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e inter if any kin
a6130 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 d of error was e
a6140 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 ncountered..*/.S
a6150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a6160 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 ar *sqlite3StrAc
a6170 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 cumFinish(StrAcc
a6180 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d um *p){. if( p-
a6190 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d >zText ){. p-
a61a0 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d >zText[p->nChar]
a61b0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d = 0;. if( p-
a61c0 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d >useMalloc && p-
a61d0 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 >zText==p->zBase
a61e0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 ){. p->zTe
a61f0 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 xt = sqlite3DbMa
a6200 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 llocRaw(p->db, p
a6210 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 ->nChar+1 );.
a6220 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 if( p->zText
a6230 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
a6240 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a y(p->zText, p->z
a6250 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 Base, p->nChar+1
a6260 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
a6270 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f p->mallo
a6280 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
a6290 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
a62a0 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 return p->zText
a62b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 ;.}../*.** Reset
a62c0 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 an StrAccum str
a62d0 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c ing. Reclaim al
a62e0 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 l malloced memor
a62f0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
a6300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a6310 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 3StrAccumReset(S
a6320 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 trAccum *p){. i
a6330 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e f( p->zText!=p->
a6340 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c zBase ){. sql
a6350 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
a6360 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d , p->zText);. }
a6370 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b . p->zText = 0;
a6380 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
a6390 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 lize a string ac
a63a0 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c cumulator.*/.SQL
a63b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a63c0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a63d0 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 Init(StrAccum *p
a63e0 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 , char *zBase, i
a63f0 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 nt n, int mx){.
a6400 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a p->zText = p->z
a6410 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 Base = zBase;.
a6420 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e p->db = 0;. p->
a6430 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e nChar = 0;. p->
a6440 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d nAlloc = n;. p-
a6450 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 >mxAlloc = mx;.
a6460 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 p->useMalloc =
a6470 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 1;. p->tooBig =
a6480 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 0;. p->mallocF
a6490 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ailed = 0;.}../*
a64a0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
a64b0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a64c0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
a64d0 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 (). Use the int
a64e0 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
a64f0 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
a6500 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a6510 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
a6520 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 3VMPrintf(sqlite
a6530 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
a6540 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c r *zFormat, va_l
a6550 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 ist ap){. char
a6560 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 *z;. char zBase
a6570 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 [SQLITE_PRINT_BU
a6580 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 F_SIZE];. StrAc
a6590 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 cum acc;. asser
a65a0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 t( db!=0 );. sq
a65b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a65c0 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 t(&acc, zBase, s
a65d0 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 izeof(zBase),.
a65e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a65f0 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 db->aLimit[S
a6600 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
a6610 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d TH]);. acc.db =
a6620 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 db;. sqlite3VX
a6630 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 Printf(&acc, 1,
a6640 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
a6650 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 z = sqlite3StrAc
a6660 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
a6670 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f . if( acc.mallo
a6680 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 cFailed ){. d
a6690 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
a66a0 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 1;. }. retur
a66b0 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
a66c0 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a66d0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a66e0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 liteMalloc(). U
a66f0 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a se the internal.
a6700 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
a6710 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
a6720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a6730 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e ar *sqlite3MPrin
a6740 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 tf(sqlite3 *db,
a6750 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a6760 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
a6770 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
a6780 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 *z;. va_start(a
a6790 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a p, zFormat);. z
a67a0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
a67b0 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 tf(db, zFormat,
a67c0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a67d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
a67e0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c ../*.** Like sql
a67f0 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 ite3MPrintf(), b
a6800 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 ut call sqlite3D
a6810 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 bFree() on zStr
a6820 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 after formatting
a6830 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 .** the string a
a6840 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e nd before return
a6850 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 ning. This rout
a6860 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ine is intended
a6870 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f to be used.** to
a6880 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 modify an exist
a6890 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 ing string. For
a68a0 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 example:.**.**
a68b0 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 x = sqlite
a68c0 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 3MPrintf(db, x,
a68d0 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 "prefix %s suffi
a68e0 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 x", x);.**.*/.SQ
a68f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a6900 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
a6910 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 df(sqlite3 *db,
a6920 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 char *zStr, cons
a6930 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
a6940 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
a6950 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
a6960 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
a6970 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
a6980 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
a6990 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
a69a0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
a69b0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
a69c0 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 b, zStr);. retu
a69d0 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
a69e0 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
a69f0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
a6a00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
a6a10 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 Omit the inter
a6a20 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 nal.** %-convers
a6a30 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a ion extensions..
a6a40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 */.SQLITE_API ch
a6a50 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 ar *sqlite3_vmpr
a6a60 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
a6a70 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 *zFormat, va_lis
a6a80 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a t ap){. char *z
a6a90 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 ;. char zBase[S
a6aa0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
a6ab0 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 SIZE];. StrAccu
a6ac0 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 m acc;.#ifndef S
a6ad0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
a6ae0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
a6af0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
a6b00 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
a6b10 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 f. sqlite3StrAc
a6b20 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 cumInit(&acc, zB
a6b30 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 ase, sizeof(zBas
a6b40 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c e), SQLITE_MAX_L
a6b50 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 ENGTH);. sqlite
a6b60 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 3VXPrintf(&acc,
a6b70 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0, zFormat, ap);
a6b80 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 . z = sqlite3St
a6b90 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 rAccumFinish(&ac
a6ba0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a c);. return z;.
a6bb0 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a6bc0 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a6bd0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
a6be0 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d _malloc()(). Om
a6bf0 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a it the internal.
a6c00 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
a6c10 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
a6c20 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
a6c30 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
a6c40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a6c50 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
a6c60 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
a6c70 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 *z;.#ifndef SQLI
a6c80 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
a6c90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
a6ca0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
a6cb0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
a6cc0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
a6cd0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
a6ce0 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a lite3_vmprintf(z
a6cf0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
a6d00 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 a_end(ap);. ret
a6d10 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
a6d20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
a6d30 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e () works like sn
a6d40 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 printf() except
a6d50 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 that it ignores
a6d60 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c the.** current l
a6d70 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 ocale settings.
a6d80 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 This is importa
a6d90 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 nt for SQLite be
a6da0 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 cause we.** are
a6db0 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 not able to use
a6dc0 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 a "," as the dec
a6dd0 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c imal point in pl
a6de0 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a ace of "." as.**
a6df0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f specified by so
a6e00 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 me locales..*/.S
a6e10 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
a6e20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
a6e30 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 (int n, char *zB
a6e40 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a uf, const char *
a6e50 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a6e60 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c char *z;. va_l
a6e70 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 ist ap;. StrAcc
a6e80 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e um acc;.. if( n
a6e90 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 <=0 ){. retur
a6ea0 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 n zBuf;. }. sq
a6eb0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a6ec0 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c t(&acc, zBuf, n,
a6ed0 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 0);. acc.useMa
a6ee0 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 lloc = 0;. va_s
a6ef0 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 tart(ap,zFormat)
a6f00 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
a6f10 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
a6f20 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
a6f30 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 end(ap);. z = s
a6f40 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a6f50 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 nish(&acc);. re
a6f60 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 turn z;.}..#if d
a6f70 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
a6f80 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 BUG)./*.** A ver
a6f90 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 sion of printf()
a6fa0 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 that understand
a6fb0 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f s %lld. Used fo
a6fc0 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 r debugging..**
a6fd0 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 The printf() bui
a6fe0 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 lt into some ver
a6ff0 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 sions of windows
a7000 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 does not unders
a7010 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 tand %lld.** and
a7020 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f segfaults if yo
a7030 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 u give it a long
a7040 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 long int..*/.SQ
a7050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a7060 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 d sqlite3DebugPr
a7070 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 intf(const char
a7080 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
a7090 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
a70a0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 StrAccum acc;.
a70b0 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a char zBuf[500];.
a70c0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a70d0 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 mInit(&acc, zBuf
a70e0 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 , sizeof(zBuf),
a70f0 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 0);. acc.useMal
a7100 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 loc = 0;. va_st
a7110 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
a7120 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
a7130 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 tf(&acc, 0, zFor
a7140 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a7150 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 nd(ap);. sqlite
a7160 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
a7170 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 &acc);. fprintf
a7180 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 (stdout,"%s", zB
a7190 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 uf);. fflush(st
a71a0 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a dout);.}.#endif.
a71b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a71c0 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a OMIT_TRACE./*.**
a71d0 20 76 61 72 69 61 62 6c 65 2d 61 72 67 75 6d 65 variable-argume
a71e0 6e 74 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e nt wrapper aroun
a71f0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 d sqlite3VXPrint
a7200 66 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 f()..*/.SQLITE_P
a7210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a7220 74 65 33 58 50 72 69 6e 74 66 28 53 74 72 41 63 te3XPrintf(StrAc
a7230 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 cum *p, const ch
a7240 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
a7250 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
a7260 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a . va_start(ap,z
a7270 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 Format);. sqlit
a7280 65 33 56 58 50 72 69 6e 74 66 28 70 2c 20 31 2c e3VXPrintf(p, 1,
a7290 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
a72a0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 23 va_end(ap);.}.#
a72b0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
a72c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 ****** End of pr
a72d0 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a intf.c *********
a72e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a72f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7300 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
a7310 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
a7320 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a e random.c *****
a7330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7350 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
a7360 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
a7370 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
a7380 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
a7390 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
a73a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
a73b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
a73c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
a73d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
a73e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
a73f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
a7400 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
a7410 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
a7420 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
a7430 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
a7440 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
a7450 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
a7460 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
a7470 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
a7480 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
a7490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a74a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a74b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a74c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
a74d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
a74e0 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c ins code to impl
a74f0 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 ement a pseudo-r
a7500 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 andom number.**
a7510 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 generator (PRNG)
a7520 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a for SQLite..**.
a7530 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 ** Random number
a7540 73 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f s are used by so
a7550 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
a7560 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f se backends in o
a7570 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 rder.** to gener
a7580 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 ate random integ
a7590 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c er keys for tabl
a75a0 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c es or random fil
a75b0 65 6e 61 6d 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 enames..*/.../*
a75c0 41 6c 6c 20 74 68 72 65 61 64 73 20 73 68 61 72 All threads shar
a75d0 65 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f e a single rando
a75e0 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 m number generat
a75f0 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 or..** This stru
a7600 63 74 75 72 65 20 69 73 20 74 68 65 20 63 75 72 cture is the cur
a7610 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 rent state of th
a7620 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a e generator..*/.
a7630 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
a7640 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 D struct sqlite3
a7650 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 PrngType {. uns
a7660 69 67 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69 igned char isIni
a7670 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t; /* T
a7680 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a rue if initializ
a7690 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ed */. unsigned
a76a0 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 char i, j;
a76b0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 /* State
a76c0 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 variables */. u
a76d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32 nsigned char s[2
a76e0 35 36 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 56]; /*
a76f0 20 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 State variables
a7700 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e */.} sqlite3Prn
a7710 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 g;../*.** Get a
a7720 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e single 8-bit ran
a7730 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 dom value from t
a7740 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 he RC4 PRNG. Th
a7750 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 e Mutex.** must
a7760 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 be held while ex
a7770 65 63 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 ecuting this rou
a7780 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 tine..**.** Why
a7790 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c not just use a l
a77a0 69 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 ibrary random ge
a77b0 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 nerator like lra
a77c0 6e 64 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f nd48() for this?
a77d0 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 .** Because the
a77e0 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f OP_NewRowid opco
a77f0 64 65 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 de in the VDBE d
a7800 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 epends on having
a7810 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 a very.** good
a7820 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d source of random
a7830 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c numbers. The l
a7840 72 61 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 rand48() library
a7850 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a function may.**
a7860 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e well be good en
a7870 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 ough. But maybe
a7880 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 not. Or maybe
a7890 6c 72 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f lrand48() has so
a78a0 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f me.** subtle pro
a78b0 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 blems on some sy
a78c0 73 74 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 stems that could
a78d0 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e cause problems.
a78e0 20 20 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 It is hard.**
a78f0 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e to know. To min
a7900 69 6d 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f imize the risk o
a7910 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 f problems due t
a7920 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a o bad lrand48().
a7930 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
a7940 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 ns, SQLite uses
a7950 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 this random numb
a7960 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 er generator bas
a7970 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 ed.** on RC4, wh
a7980 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b ich we know work
a7990 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a s very well..**.
a79a0 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 ** (Later): Act
a79b0 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 ually, OP_NewRow
a79c0 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 id does not depe
a79d0 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 nd on a good sou
a79e0 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d rce of.** random
a79f0 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 ness any more.
a7a00 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 But we will leav
a7a10 65 20 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 e this code in a
a7a20 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a ll the same..*/.
a7a30 73 74 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d static u8 random
a7a40 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e Byte(void){. un
a7a50 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a signed char t;..
a7a60 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72 . /* The "wsdPr
a7a70 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 ng" macro will r
a7a80 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73 esolve to the ps
a7a90 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 eudo-random numb
a7aa0 65 72 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a er generator. *
a7ab0 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 * state vector.
a7ac0 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 If writable sta
a7ad0 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 tic data is unsu
a7ae0 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 pported on the t
a7af0 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68 arget,. ** we h
a7b00 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 ave to locate th
a7b10 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 e state vector a
a7b20 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 t run-time. In
a7b30 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a the more common.
a7b40 20 20 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 ** case where
a7b50 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 writable static
a7b60 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 data is supporte
a7b70 64 2c 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72 d, wsdPrng can r
a7b80 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20 efer directly.
a7b90 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 ** to the "sqlit
a7ba0 65 33 50 72 6e 67 22 20 73 74 61 74 65 20 76 65 e3Prng" state ve
a7bb0 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 ctor declared ab
a7bc0 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 ove.. */.#ifdef
a7bd0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
a7be0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 . struct sqlite
a7bf0 33 50 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26 3PrngType *p = &
a7c00 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 GLOBAL(struct sq
a7c10 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 lite3PrngType, s
a7c20 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64 qlite3Prng);.# d
a7c30 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b efine wsdPrng p[
a7c40 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 0].#else.# defin
a7c50 65 20 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 e wsdPrng sqlite
a7c60 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20 3Prng.#endif...
a7c70 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
a7c80 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
a7c90 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 random number ge
a7ca0 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 nerator once,.
a7cb0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d ** the first tim
a7cc0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 e this routine i
a7cd0 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 s called. The s
a7ce0 65 65 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 eed value does.
a7cf0 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 ** not need to
a7d00 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 contain a lot of
a7d10 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 randomness sinc
a7d20 65 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a e we are not. *
a7d30 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 * trying to do s
a7d40 65 63 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e ecure encryption
a7d50 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b or anything lik
a7d60 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 e that.... **.
a7d70 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 ** Nothing in t
a7d80 68 69 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 his file or anyw
a7d90 68 65 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c here else in SQL
a7da0 69 74 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e ite does any kin
a7db0 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 d of. ** encryp
a7dc0 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 tion. The RC4 a
a7dd0 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e lgorithm is bein
a7de0 67 20 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 g used as a PRNG
a7df0 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a (pseudo-random.
a7e00 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 ** number gene
a7e10 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e rator) not as an
a7e20 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 encryption devi
a7e30 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ce.. */. if( !
a7e40 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29 wsdPrng.isInit )
a7e50 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
a7e60 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20 char k[256];.
a7e70 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b wsdPrng.j = 0;
a7e80 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d . wsdPrng.i =
a7e90 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 0;. sqlite3O
a7ea0 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 sRandomness(sqli
a7eb0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c te3_vfs_find(0),
a7ec0 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 256, k);. fo
a7ed0 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b r(i=0; i<256; i+
a7ee0 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e +){. wsdPrn
a7ef0 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a g.s[i] = (u8)i;.
a7f00 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
a7f10 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 0; i<256; i++){.
a7f20 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 wsdPrng.j
a7f30 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 += wsdPrng.s[i]
a7f40 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 + k[i];. t
a7f50 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 = wsdPrng.s[wsdP
a7f60 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73 rng.j];. ws
a7f70 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a7f80 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 j] = wsdPrng.s[i
a7f90 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ];. wsdPrng
a7fa0 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d .s[i] = t;. }
a7fb0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49 . wsdPrng.isI
a7fc0 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 nit = 1;. }..
a7fd0 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 /* Generate and
a7fe0 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 return single ra
a7ff0 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 ndom byte. */.
a8000 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20 wsdPrng.i++;.
a8010 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 t = wsdPrng.s[ws
a8020 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50 dPrng.i];. wsdP
a8030 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73 rng.j += t;. ws
a8040 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a8050 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 i] = wsdPrng.s[w
a8060 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64 sdPrng.j];. wsd
a8070 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a Prng.s[wsdPrng.j
a8080 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73 ] = t;. t += ws
a8090 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a80a0 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 i];. return wsd
a80b0 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a Prng.s[t];.}../*
a80c0 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e .** Return N ran
a80d0 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51 dom bytes..*/.SQ
a80e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
a80f0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
a8100 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 (int N, void *pB
a8110 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 uf){. unsigned
a8120 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 char *zBuf = pBu
a8130 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 f;.#if SQLITE_TH
a8140 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 READSAFE. sqlit
a8150 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 e3_mutex *mutex
a8160 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
a8170 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
a8180 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23 _STATIC_PRNG);.#
a8190 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
a81a0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
a81b0 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d x);. while( N--
a81c0 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b ){. *(zBuf++
a81d0 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29 ) = randomByte()
a81e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
a81f0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
a8200 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 x);.}..#ifndef S
a8210 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
a8220 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f IN_TEST./*.** Fo
a8230 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 r testing purpos
a8240 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73 es, we sometimes
a8250 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76 want to preserv
a8260 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a e the state of.*
a8270 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f * PRNG and resto
a8280 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 re the PRNG to i
a8290 74 73 20 73 61 76 65 64 20 73 74 61 74 65 20 61 ts saved state a
a82a0 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20 t a later time,
a82b0 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74 or.** to reset t
a82c0 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69 he PRNG to its i
a82d0 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54 nitial state. T
a82e0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63 hese routines ac
a82f0 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73 complish.** thos
a8300 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 e tasks..**.** T
a8310 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f he sqlite3_test_
a8320 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 control() interf
a8330 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 ace calls these
a8340 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63 routines to.** c
a8350 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e ontrol the PRNG.
a8360 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
a8370 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c E_WSD struct sql
a8380 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c ite3PrngType sql
a8390 69 74 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 ite3SavedPrng;.S
a83a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a83b0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 id sqlite3PrngSa
a83c0 76 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 veState(void){.
a83d0 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c memcpy(. &GL
a83e0 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a83f0 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a8400 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a ite3SavedPrng),.
a8410 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 &GLOBAL(stru
a8420 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a8430 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 pe, sqlite3Prng)
a8440 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c ,. sizeof(sql
a8450 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d ite3Prng). );.}
a8460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a8470 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 void sqlite3Prng
a8480 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 RestoreState(voi
a8490 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 d){. memcpy(.
a84a0 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 &GLOBAL(struct
a84b0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a84c0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a , sqlite3Prng),.
a84d0 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 &GLOBAL(stru
a84e0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a84f0 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64 pe, sqlite3Saved
a8500 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f Prng),. sizeo
a8510 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 f(sqlite3Prng).
a8520 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 );.}.SQLITE_PRI
a8530 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a8540 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 3PrngResetState(
a8550 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 void){. GLOBAL(
a8560 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
a8570 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 ngType, sqlite3P
a8580 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b rng).isInit = 0;
a8590 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
a85a0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
a85b0 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a _TEST */../*****
a85c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
a85d0 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a random.c ******
a85e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a85f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8600 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
a8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
a8620 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a file utf.c *****
a8630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8650 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
a8660 32 30 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 2004 April 13.**
a8670 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
a8680 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
a8690 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
a86a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
a86b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
a86c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
a86d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
a86e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
a86f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
a8700 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
a8710 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
a8720 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
a8730 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
a8740 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
a8750 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
a8760 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
a8770 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
a8780 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
a8790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a87a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a87b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a87c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
a87d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
a87e0 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 ns routines used
a87f0 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65 to translate be
a8800 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a tween UTF-8, .**
a8810 20 55 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 UTF-16, UTF-16B
a8820 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e E, and UTF-16LE.
a8830 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 .**.** Notes on
a8840 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 UTF-8:.**.** B
a8850 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 yte-0 Byte-1
a8860 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 Byte-2 Byt
a8870 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 e-3 Value.**
a8880 20 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20 0xxxxxxx
a8890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a88a0 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
a88b0 30 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78 00 00000000 0xxx
a88c0 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 xxxx.** 110yyyy
a88d0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20 y 10xxxxxx
a88e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a88f0 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 00000000 00000
a8900 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 yyy yyxxxxxx.**
a8910 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 1110zzzz 10yyy
a8920 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 yyy 10xxxxxx
a8930 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
a8940 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 00 zzzzyyyy yyxx
a8950 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 xxxx.** 11110uu
a8960 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 u 10uuzzzz 10y
a8970 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 yyyyy 10xxxxxx
a8980 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 000uuuuu zzzzy
a8990 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a yyy yyxxxxxx.**.
a89a0 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 **.** Notes on U
a89b0 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 TF-16: (with ww
a89c0 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a ww+1==uuuuu).**.
a89d0 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 ** Word-0
a89e0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 Wor
a89f0 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c d-1 Val
a8a00 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 ue.** 110110ww
a8a10 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 wwzzzzyy 11011
a8a20 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 1yy yyxxxxxx
a8a30 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 000uuuuu zzzzyyy
a8a40 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a y yyxxxxxx.** z
a8a50 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
a8a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8a70 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 00000000
a8a80 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 zzzzyyyy yyxxxx
a8a90 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 xx.**.**.** BOM
a8aa0 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 or Byte Order Ma
a8ab0 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 rk:.** 0xff
a8ac0 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 0xfe little-en
a8ad0 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c dian utf-16 foll
a8ae0 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 ows.** 0xfe
a8af0 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 0xff big-endia
a8b00 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 n utf-16 follows
a8b10 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a .**.*/./********
a8b20 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 ****** Include v
a8b30 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 dbeInt.h in the
a8b40 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 middle of utf.c
a8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8b60 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
a8b70 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
a8b80 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a e vdbeInt.h ****
a8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8bb0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
a8bc0 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 3 September 6.**
a8bd0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
a8be0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
a8bf0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
a8c00 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
a8c10 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
a8c20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
a8c30 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
a8c40 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
a8c50 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
a8c60 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
a8c70 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
a8c80 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
a8c90 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
a8ca0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
a8cb0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
a8cc0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
a8cd0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
a8ce0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
a8cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
a8d30 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 This is the head
a8d40 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f er file for info
a8d50 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 rmation that is
a8d60 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a private to the.*
a8d70 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e * VDBE. This in
a8d80 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 formation used t
a8d90 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 o all be at the
a8da0 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c top of the singl
a8db0 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 e.** source code
a8dc0 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 file "vdbe.c".
a8dd0 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 When that file
a8de0 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 became too big (
a8df0 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e over.** 6000 lin
a8e00 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 es long) it was
a8e10 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 split up into se
a8e20 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 veral smaller fi
a8e30 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 les and.** this
a8e40 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 header informati
a8e50 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 on was factored
a8e60 6f 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 out..*/.#ifndef
a8e70 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 _VDBEINT_H_.#def
a8e80 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a ine _VDBEINT_H_.
a8e90 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 ./*.** SQL is tr
a8ea0 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 anslated into a
a8eb0 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 sequence of inst
a8ec0 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a ructions to be.*
a8ed0 2a 20 65 78 65 63 75 74 65 64 20 62 79 20 61 20 * executed by a
a8ee0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
a8ef0 20 20 45 61 63 68 20 69 6e 73 74 72 75 63 74 69 Each instructi
a8f00 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 on is an instanc
a8f10 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
a8f20 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
a8f30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
a8f40 63 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f ct VdbeOp Op;../
a8f50 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c *.** Boolean val
a8f60 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 ues.*/.typedef u
a8f70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f nsigned char Boo
a8f80 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 l;../*.** A curs
a8f90 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 or is a pointer
a8fa0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 into a single BT
a8fb0 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 ree within a dat
a8fc0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 abase file..** T
a8fd0 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65 he cursor can se
a8fe0 65 6b 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e ek to a BTree en
a8ff0 74 72 79 20 77 69 74 68 20 61 20 70 61 72 74 69 try with a parti
a9000 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a cular key, or.**
a9010 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 loop over all e
a9020 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 42 74 ntries of the Bt
a9030 72 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c ree. You can al
a9040 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54 so insert new BT
a9050 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f ree.** entries o
a9060 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20 6b r retrieve the k
a9070 65 79 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 ey or data from
a9080 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 the entry that t
a9090 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 he cursor.** is
a90a0 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
a90b0 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 ng to..** .** Ev
a90c0 65 72 79 20 63 75 72 73 6f 72 20 74 68 61 74 20 ery cursor that
a90d0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
a90e0 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20 ine has open is
a90f0 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 represented by a
a9100 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
a9110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
a9120 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
a9130 49 66 20 74 68 65 20 56 64 62 65 43 75 72 73 6f If the VdbeCurso
a9140 72 2e 69 73 54 72 69 67 67 65 72 52 6f 77 20 66 r.isTriggerRow f
a9150 6c 61 67 20 69 73 20 73 65 74 20 69 74 20 6d 65 lag is set it me
a9160 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 63 75 ans that this cu
a9170 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c rsor is.** reall
a9180 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 y a single row t
a9190 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 hat represents t
a91a0 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 he NEW or OLD ps
a91b0 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a eudo-table of.**
a91c0 20 61 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20 a row trigger.
a91d0 20 54 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 The data for th
a91e0 65 20 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20 e row is stored
a91f0 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44 in VdbeCursor.pD
a9200 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 ata and.** the r
a9210 6f 77 69 64 20 69 73 20 69 6e 20 56 64 62 65 43 owid is in VdbeC
a9220 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 ursor.iKey..*/.s
a9230 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 truct VdbeCursor
a9240 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 {. BtCursor *p
a9250 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 Cursor; /* Th
a9260 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 e cursor structu
a9270 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e re of the backen
a9280 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 d */. int iDb;
a9290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a92a0 49 6e 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20 Index of cursor
a92b0 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e database in db->
a92c0 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f aDb[] (or -1) */
a92d0 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 . i64 lastRowid
a92e0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 ; /* Last
a92f0 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 rowid from a Ne
a9300 78 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 xt or NextIdx op
a9310 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f eration */. Boo
a9320 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 l zeroed;
a9330 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 /* True if ze
a9340 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 roed out and rea
a9350 64 79 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a dy for reuse */.
a9360 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 Bool rowidIsVa
a9370 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 lid; /* True
a9380 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 if lastRowid is
a9390 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 valid */. Bool
a93a0 61 74 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 atFirst;
a93b0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e /* True if poin
a93c0 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e ting to first en
a93d0 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 try */. Bool us
a93e0 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f eRandomRowid; /
a93f0 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 * Generate new r
a9400 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 ecord numbers se
a9410 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 mi-randomly */.
a9420 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 Bool nullRow;
a9430 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a9440 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 f pointing to a
a9450 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 row with no data
a9460 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 */. Bool defer
a9470 72 65 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 redMoveto; /* A
a9480 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
a9490 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 BtreeMoveto() is
a94a0 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f needed */. Boo
a94b0 6c 20 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20 l isTable;
a94c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 /* True if a
a94d0 74 61 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20 table requiring
a94e0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a integer keys */.
a94f0 20 20 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 Bool isIndex;
a9500 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a9510 69 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 if an index cont
a9520 61 69 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 aining keys only
a9530 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 - no data */.
a9540 69 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74 i64 movetoTarget
a9550 3b 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e ; /* Argumen
a9560 74 20 74 6f 20 74 68 65 20 64 65 66 65 72 72 65 t to the deferre
a9570 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f d sqlite3BtreeMo
a9580 76 65 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65 veto() */. Btre
a9590 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 e *pBt;
a95a0 20 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 /* Separate fi
a95b0 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f le holding tempo
a95c0 72 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20 rary table */.
a95d0 69 6e 74 20 70 73 65 75 64 6f 54 61 62 6c 65 52 int pseudoTableR
a95e0 65 67 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 eg; /* Registe
a95f0 72 20 68 6f 6c 64 69 6e 67 20 70 73 65 75 64 6f r holding pseudo
a9600 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 2a table content. *
a9610 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 /. KeyInfo *pKe
a9620 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 yInfo; /* Inf
a9630 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 o about index ke
a9640 79 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 ys needed by ind
a9650 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 ex cursors */.
a9660 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 int nField;
a9670 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a9680 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
a9690 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 header */. i64
a96a0 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 seqCount;
a96b0 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 /* Sequence c
a96c0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 ounter */. sqli
a96d0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
a96e0 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f *pVtabCursor; /
a96f0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 * The cursor for
a9700 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
a9710 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 */. const sqli
a9720 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
a9730 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 ule; /* Modu
a9740 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 le for cursor pV
a9750 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 tabCursor */..
a9760 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 /* Result of las
a9770 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f t sqlite3BtreeMo
a9780 76 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 veto() done by a
a9790 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f n OP_NotExists o
a97a0 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 r . ** OP_IsUni
a97b0 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 que opcode on th
a97c0 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 is cursor. */.
a97d0 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a int seekResult;.
a97e0 0a 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 . /* Cached inf
a97f0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
a9800 68 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 he header for th
a9810 65 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 e data record th
a9820 61 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 at the. ** curs
a9830 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 or is currently
a9840 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e pointing to. On
a9850 6c 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 ly valid if cach
a9860 65 53 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a eStatus matches.
a9870 20 20 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43 ** Vdbe.cacheC
a9880 74 72 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43 tr. Vdbe.cacheC
a9890 74 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61 tr will never ta
a98a0 6b 65 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ke on the value
a98b0 6f 66 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 of. ** CACHE_ST
a98c0 41 4c 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69 ALE and so setti
a98d0 6e 67 20 63 61 63 68 65 53 74 61 74 75 73 3d 43 ng cacheStatus=C
a98e0 41 43 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61 ACHE_STALE guara
a98f0 6e 74 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 ntees that. **
a9900 74 68 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 the cache is out
a9910 20 6f 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20 of date.. **.
a9920 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 ** aRow might p
a9930 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 oint to (ephemer
a9940 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 al) data for the
a9950 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 current row, or
a9960 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 it might. ** b
a9970 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75 e NULL.. */. u
a9980 33 32 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 32 cacheStatus;
a9990 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 /* Cache is
a99a0 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d valid if this m
a99b0 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 atches Vdbe.cach
a99c0 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 eCtr */. int pa
a99d0 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 yloadSize;
a99e0 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
a99f0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
a9a00 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 record */. u32
a9a10 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 *aType;
a9a20 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 /* Type values
a9a30 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 for all entries
a9a40 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
a9a50 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 /. u32 *aOffset
a9a60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 ; /* Cac
a9a70 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 hed offsets to t
a9a80 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 he start of each
a9a90 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f columns data */
a9aa0 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 . u8 *aRow;
a9ab0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
a9ac0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
a9ad0 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 row, if all on
a9ae0 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 one page */.};.t
a9af0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 ypedef struct Vd
a9b00 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 beCursor VdbeCur
a9b10 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e sor;../*.** When
a9b20 20 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 a sub-program i
a9b30 73 20 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50 s executed (OP_P
a9b40 72 6f 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63 rogram), a struc
a9b50 74 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 ture of this typ
a9b60 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 e.** is allocate
a9b70 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 d to store the c
a9b80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
a9b90 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e the program coun
a9ba0 74 65 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20 ter, as.** well
a9bb0 61 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d as the current m
a9bc0 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 emory cell array
a9bd0 20 61 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 and various oth
a9be0 65 72 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 er frame specifi
a9bf0 63 0a 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 c.** values stor
a9c00 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 ed in the Vdbe s
a9c10 74 72 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 truct. When the
a9c20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 sub-program is f
a9c30 69 6e 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65 inished, .** the
a9c40 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f se values are co
a9c50 70 69 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 pied back to the
a9c60 20 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56 Vdbe from the V
a9c70 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 dbeFrame structu
a9c80 72 65 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67 re,.** restoring
a9c90 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
a9ca0 65 20 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61 e VM to as it wa
a9cb0 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 75 62 s before the sub
a9cc0 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61 -program.** bega
a9cd0 6e 20 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a n executing..**.
a9ce0 2a 2a 20 46 72 61 6d 65 73 20 61 72 65 20 73 74 ** Frames are st
a9cf0 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 ored in a linked
a9d00 20 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 20 list headed at
a9d10 56 64 62 65 2e 70 50 61 72 65 6e 74 2e 20 56 64 Vdbe.pParent. Vd
a9d20 62 65 2e 70 50 61 72 65 6e 74 0a 2a 2a 20 69 73 be.pParent.** is
a9d30 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 the parent of t
a9d40 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 he current frame
a9d50 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 , or zero if the
a9d60 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 0a 2a current frame.*
a9d70 2a 20 69 73 20 74 68 65 20 6d 61 69 6e 20 56 64 * is the main Vd
a9d80 62 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 74 be program..*/.t
a9d90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 ypedef struct Vd
a9da0 62 65 46 72 61 6d 65 20 56 64 62 65 46 72 61 6d beFrame VdbeFram
a9db0 65 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46 72 e;.struct VdbeFr
a9dc0 61 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b ame {. Vdbe *v;
a9dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a9de0 2f 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d 65 /* VM this frame
a9df0 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 20 belongs to */.
a9e00 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 int pc;
a9e10 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 /* Prog
a9e20 72 61 6d 20 43 6f 75 6e 74 65 72 20 2a 2f 0a 20 ram Counter */.
a9e30 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 Op *aOp;
a9e40 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 /* Prog
a9e50 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ram instructions
a9e60 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 */. int nOp;
a9e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a9e80 20 53 69 7a 65 20 6f 66 20 61 4f 70 20 61 72 72 Size of aOp arr
a9e90 61 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 ay */. Mem *aMe
a9ea0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
a9eb0 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f /* Array of memo
a9ec0 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e ry cells */. in
a9ed0 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t nMem;
a9ee0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a9ef0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d of entries in aM
a9f00 65 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 em */. VdbeCurs
a9f10 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 or **apCsr;
a9f20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f 66 20 56 64 /* Element of Vd
a9f30 62 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 be cursors */.
a9f40 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 u16 nCursor;
a9f50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
a9f60 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
a9f70 61 70 43 73 72 20 2a 2f 0a 20 20 76 6f 69 64 20 apCsr */. void
a9f80 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 *token;
a9f90 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 53 75 /* Copy of Su
a9fa0 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 20 2a bProgram.token *
a9fb0 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 4d 65 /. int nChildMe
a9fc0 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e m; /* N
a9fd0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
a9fe0 63 65 6c 6c 73 20 66 6f 72 20 63 68 69 6c 64 20 cells for child
a9ff0 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e frame */. int n
aa000 43 68 69 6c 64 43 73 72 3b 20 20 20 20 20 20 20 ChildCsr;
aa010 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
aa020 63 75 72 73 6f 72 73 20 66 6f 72 20 63 68 69 6c cursors for chil
aa030 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 36 34 d frame */. i64
aa040 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
aa050 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 69 6e 73 /* Last ins
aa060 65 72 74 20 72 6f 77 69 64 20 28 73 71 6c 69 74 ert rowid (sqlit
aa070 65 33 2e 6c 61 73 74 52 6f 77 69 64 29 20 2a 2f e3.lastRowid) */
aa080 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
aa090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
aa0a0 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 atement changes
aa0b0 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 (Vdbe.nChanges)
aa0c0 20 20 20 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 */. VdbeFra
aa0d0 6d 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 me *pParent;
aa0e0 20 2f 2a 20 50 61 72 65 6e 74 20 6f 66 20 74 68 /* Parent of th
aa0f0 69 73 20 66 72 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a is frame */.};..
aa100 23 64 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d #define VdbeFram
aa110 65 4d 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29 eMem(p) ((Mem *)
aa120 26 28 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44 &((u8 *)p)[ROUND
aa130 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 8(sizeof(VdbeFra
aa140 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 me))])../*.** A
aa150 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 value for VdbeCu
aa160 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 rsor.cacheValid
aa170 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 that means the c
aa180 61 63 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 ache is always i
aa190 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 nvalid..*/.#defi
aa1a0 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 ne CACHE_STALE 0
aa1b0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c ../*.** Internal
aa1c0 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e ly, the vdbe man
aa1d0 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 ipulates nearly
aa1e0 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 all SQL values a
aa1f0 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 s Mem.** structu
aa200 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 res. Each Mem st
aa210 72 75 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d ruct may cache m
aa220 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e ultiple represen
aa230 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c tations (string,
aa240 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e .** integer etc.
aa250 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 ) of the same va
aa260 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 lue. A value (a
aa270 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d nd therefore Mem
aa280 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 structure).** h
aa290 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 as the following
aa2a0 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a properties:.**.
aa2b0 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 ** Each value ha
aa2c0 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 s a manifest typ
aa2d0 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 e. The manifest
aa2e0 74 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 type of the valu
aa2f0 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 e stored.** in a
aa300 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 Mem struct is r
aa310 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d eturned by the M
aa320 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 emType(Mem*) mac
aa330 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a ro. The type is.
aa340 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 ** one of SQLITE
aa350 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e _NULL, SQLITE_IN
aa360 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 TEGER, SQLITE_RE
aa370 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 AL, SQLITE_TEXT
aa380 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f or.** SQLITE_BLO
aa390 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d B..*/.struct Mem
aa3a0 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 {. union {.
aa3b0 20 69 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 i64 i;
aa3c0 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
aa3d0 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e value. */. in
aa3e0 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 t nZero;
aa3f0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 /* Used when b
aa400 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 it MEM_Zero is s
aa410 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 et in flags */.
aa420 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 FuncDef *pDef
aa430 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f ; /* Used o
aa440 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d nly when flags==
aa450 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 MEM_Agg */. R
aa460 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 owSet *pRowSet;
aa470 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 /* Used only
aa480 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f when flags==MEM_
aa490 52 6f 77 53 65 74 20 2a 2f 0a 20 20 20 20 56 64 RowSet */. Vd
aa4a0 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
aa4b0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 66 /* Used when f
aa4c0 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 61 6d 65 20 lags==MEM_Frame
aa4d0 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 */. } u;. doub
aa4e0 6c 65 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 le r;
aa4f0 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f /* Real value */
aa500 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
aa510 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 /* The as
aa520 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 sociated databas
aa530 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
aa540 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 char *z;
aa550 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 /* String
aa560 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f or BLOB value */
aa570 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 . int n;
aa580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
aa590 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 of characters i
aa5a0 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 n string value,
aa5b0 65 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a excluding '\0' *
aa5c0 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 /. u16 flags;
aa5d0 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 /* Some
aa5e0 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d combination of M
aa5f0 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 EM_Null, MEM_Str
aa600 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 , MEM_Dyn, etc.
aa610 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20 20 */. u8 type;
aa620 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
aa630 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 of SQLITE_NULL,
aa640 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 4c SQLITE_TEXT, SQL
aa650 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 63 ITE_INTEGER, etc
aa660 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20 20 */. u8 enc;
aa670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c /* SQL
aa680 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
aa690 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 _UTF16BE, SQLITE
aa6a0 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f _UTF16LE */. vo
aa6b0 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 id (*xDel)(void
aa6c0 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e *); /* If not n
aa6d0 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 73 20 66 ull, call this f
aa6e0 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74 unction to delet
aa6f0 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61 e Mem.z */. cha
aa700 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 r *zMalloc;
aa710 20 2f 2a 20 44 79 6e 61 6d 69 63 20 62 75 66 66 /* Dynamic buff
aa720 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 er allocated by
aa730 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
aa740 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f */.};../* One o
aa750 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f r more of the fo
aa760 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 61 72 llowing flags ar
aa770 65 20 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74 e set to indicat
aa780 65 20 74 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a e the validOK.**
aa790 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
aa7a0 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 of the value st
aa7b0 6f 72 65 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 ored in the Mem
aa7c0 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 struct..**.** If
aa7d0 20 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c the MEM_Null fl
aa7e0 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
aa7f0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 the value is an
aa800 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a SQL NULL value..
aa810 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67 ** No other flag
aa820 73 20 6d 61 79 20 62 65 20 73 65 74 20 69 6e 20 s may be set in
aa830 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a this case..**.**
aa840 20 49 66 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 If the MEM_Str
aa850 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e flag is set then
aa860 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74 Mem.z points at
aa870 20 61 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 a string repres
aa880 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75 entation..** Usu
aa890 61 6c 6c 79 20 74 68 69 73 20 69 73 20 65 6e 63 ally this is enc
aa8a0 6f 64 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 oded in the same
aa8b0 20 75 6e 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e unicode encodin
aa8c0 67 20 61 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a g as the main.**
aa8d0 20 64 61 74 61 62 61 73 65 20 28 73 65 65 20 62 database (see b
aa8e0 65 6c 6f 77 20 66 6f 72 20 65 78 63 65 70 74 69 elow for excepti
aa8f0 6f 6e 73 29 2e 20 49 66 20 74 68 65 20 4d 45 4d ons). If the MEM
aa900 5f 54 65 72 6d 20 66 6c 61 67 20 69 73 20 61 6c _Term flag is al
aa910 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20 so.** set, then
aa920 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e 75 the string is nu
aa930 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 54 68 l terminated. Th
aa940 65 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 e MEM_Int and ME
aa950 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73 M_Real .** flags
aa960 20 6d 61 79 20 63 6f 65 78 69 73 74 20 77 69 74 may coexist wit
aa970 68 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c h the MEM_Str fl
aa980 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 ag..**.** Multip
aa990 6c 65 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 le of these valu
aa9a0 65 73 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e es can appear in
aa9b0 20 4d 65 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74 Mem.flags. But
aa9c0 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20 only one.** at
aa9d0 61 20 74 69 6d 65 20 63 61 6e 20 61 70 70 65 61 a time can appea
aa9e0 72 20 69 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a r in Mem.type..*
aa9f0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 /.#define MEM_Nu
aaa00 6c 6c 20 20 20 20 20 20 30 78 30 30 30 31 20 20 ll 0x0001
aaa10 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4e 55 4c /* Value is NUL
aaa20 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d L */.#define MEM
aaa30 5f 53 74 72 20 20 20 20 20 20 20 30 78 30 30 30 _Str 0x000
aaa40 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 2 /* Value is
aaa50 61 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 a string */.#def
aaa60 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20 ine MEM_Int
aaa70 20 20 30 78 30 30 30 34 20 20 20 2f 2a 20 56 61 0x0004 /* Va
aaa80 6c 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 lue is an intege
aaa90 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d r */.#define MEM
aaaa0 5f 52 65 61 6c 20 20 20 20 20 20 30 78 30 30 30 _Real 0x000
aaab0 38 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 8 /* Value is
aaac0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f a real number */
aaad0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f .#define MEM_Blo
aaae0 62 20 20 20 20 20 20 30 78 30 30 31 30 20 20 20 b 0x0010
aaaf0 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 42 4c /* Value is a BL
aab00 4f 42 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 OB */.#define ME
aab10 4d 5f 52 6f 77 53 65 74 20 20 20 20 30 78 30 30 M_RowSet 0x00
aab20 32 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 /* Value is
aab30 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
aab40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aab50 46 72 61 6d 65 20 20 20 20 20 30 78 30 30 34 30 Frame 0x0040
aab60 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 /* Value is a
aab70 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 VdbeFrame objec
aab80 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d t */.#define MEM
aab90 5f 54 79 70 65 4d 61 73 6b 20 20 30 78 30 30 66 _TypeMask 0x00f
aaba0 66 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 f /* Mask of t
aabb0 79 70 65 20 62 69 74 73 20 2a 2f 0a 0a 2f 2a 20 ype bits */../*
aabc0 57 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e Whenever Mem con
aabd0 74 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 tains a valid st
aabe0 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 ring or blob rep
aabf0 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 resentation, one
aac00 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f of.** the follo
aac10 77 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 wing flags must
aac20 62 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d be set to determ
aac30 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d ine the memory m
aac40 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c anagement.** pol
aac50 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 icy for Mem.z.
aac60 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 The MEM_Term fla
aac70 67 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 g tells us wheth
aac80 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a er or not the.**
aac90 20 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 string is \000
aaca0 6f 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e or \u0000 termin
aacb0 61 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ated.*/.#define
aacc0 4d 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 MEM_Term 0x
aacd0 30 32 30 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 0200 /* String
aace0 20 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d rep is nul term
aacf0 69 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e inated */.#defin
aad00 65 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 e MEM_Dyn
aad10 30 78 30 34 30 30 20 20 20 2f 2a 20 4e 65 65 64 0x0400 /* Need
aad20 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 to call sqliteF
aad30 72 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a ree() on Mem.z *
aad40 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 /.#define MEM_St
aad50 61 74 69 63 20 20 20 20 30 78 30 38 30 30 20 20 atic 0x0800
aad60 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 /* Mem.z points
aad70 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 to a static str
aad80 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ing */.#define M
aad90 45 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 31 EM_Ephem 0x1
aada0 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 000 /* Mem.z p
aadb0 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 oints to an ephe
aadc0 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a meral string */.
aadd0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 #define MEM_Agg
aade0 20 20 20 20 20 20 30 78 32 30 30 30 20 20 20 2f 0x2000 /
aadf0 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 * Mem.z points t
aae00 6f 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f o an agg functio
aae10 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 n context */.#de
aae20 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 fine MEM_Zero
aae30 20 20 20 30 78 34 30 30 30 20 20 20 2f 2a 20 4d 0x4000 /* M
aae40 65 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f em.i contains co
aae50 75 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 unt of 0s append
aae60 65 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 ed to blob */..#
aae70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
aae80 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e T_INCRBLOB. #un
aae90 64 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 def MEM_Zero. #
aaea0 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 define MEM_Zero
aaeb0 30 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 0x0000.#endif...
aaec0 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 /*.** Clear any
aaed0 65 78 69 73 74 69 6e 67 20 74 79 70 65 20 66 6c existing type fl
aaee0 61 67 73 20 66 72 6f 6d 20 61 20 4d 65 6d 20 61 ags from a Mem a
aaef0 6e 64 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 nd replace them
aaf00 77 69 74 68 20 66 0a 2a 2f 0a 23 64 65 66 69 6e with f.*/.#defin
aaf10 65 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 e MemSetTypeFlag
aaf20 28 70 2c 20 66 29 20 5c 0a 20 20 20 28 28 70 29 (p, f) \. ((p)
aaf30 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 29 2d 3e ->flags = ((p)->
aaf40 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 79 70 65 flags&~(MEM_Type
aaf50 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f 29 29 7c Mask|MEM_Zero))|
aaf60 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75 f).../* A VdbeFu
aaf70 6e 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e nc is just a Fun
aaf80 63 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e cDef (defined in
aaf90 20 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68 sqliteInt.h) th
aafa0 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 at contains.** a
aafb0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
aafc0 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69 ation about auxi
aafd0 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f liary informatio
aafe0 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d n bound to argum
aaff0 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ents.** of the f
ab000 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 unction. This i
ab010 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d s used to implem
ab020 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f ent the sqlite3_
ab030 67 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a get_auxdata().**
ab040 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74 and sqlite3_set
ab050 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e _auxdata() APIs.
ab060 20 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20 The "auxdata"
ab070 69 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72 is some auxiliar
ab080 79 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63 y data.** that c
ab090 61 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64 an be associated
ab0a0 20 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 with a constant
ab0b0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 argument to a f
ab0c0 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a unction. This.*
ab0d0 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f * allows functio
ab0e0 6e 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65 ns such as "rege
ab0f0 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74 xp" to compile t
ab100 68 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65 heir constant re
ab110 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73 gular.** express
ab120 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63 ion argument onc
ab130 65 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65 e and reused the
ab140 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66 compiled code f
ab150 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 or multiple.** i
ab160 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 nvocations..*/.s
ab170 74 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b truct VdbeFunc {
ab180 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e . FuncDef *pFun
ab190 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
ab1a0 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 /* The definiti
ab1b0 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 on of the functi
ab1c0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78 on */. int nAux
ab1d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ab1e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ab1f0 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
ab200 61 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d ated for apAux[]
ab210 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78 */. struct Aux
ab220 44 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20 Data {. void
ab230 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 *pAux;
ab240 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20 /* Aux
ab250 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74 data for the i-t
ab260 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 h argument */.
ab270 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 void (*xDelete
ab280 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 )(void *);
ab290 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
ab2a0 72 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a r the aux data *
ab2b0 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20 /. } apAux[1];
ab2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab2d0 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f /* One slot fo
ab2e0 72 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20 r each function
ab2f0 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a argument */.};..
ab300 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65 /*.** The "conte
ab310 78 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 xt" argument for
ab320 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66 a installable f
ab330 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e unction. A poin
ab340 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 ter to an.** ins
ab350 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
ab360 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66 ructure is the f
ab370 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
ab380 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73 the routines us
ab390 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 ed.** implement
ab3a0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
ab3b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 s..**.** There i
ab3c0 73 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20 s a typedef for
ab3d0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
ab3e0 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20 n sqlite.h. So
ab3f0 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a all routines,.**
ab400 20 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63 even the public
ab410 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51 interface to SQ
ab420 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20 Lite, can use a
ab430 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
ab440 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 structure..** Bu
ab450 74 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 t this file is t
ab460 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68 he only place wh
ab470 65 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ere the internal
ab480 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73 details of this
ab490 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 .** structure ar
ab4a0 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 e known..**.** T
ab4b0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
ab4c0 20 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20 defined inside
ab4d0 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63 of vdbeInt.h bec
ab4e0 61 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62 ause it uses sub
ab4f0 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d structures.** (M
ab500 65 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e em) which are on
ab510 6c 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65 ly defined there
ab520 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
ab530 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20 te3_context {.
ab540 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 FuncDef *pFunc;
ab550 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
ab560 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66 to function inf
ab570 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20 ormation. MUST
ab580 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64 BE FIRST */. Vd
ab590 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
ab5a0 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20 c; /* Auxilary
ab5b0 64 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64 data, if created
ab5c0 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20 . */. Mem s;
ab5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ab5e0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
ab5f0 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 is stored here
ab600 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 */. Mem *pMem;
ab610 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
ab620 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 mory cell used t
ab630 6f 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74 o store aggregat
ab640 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 e context */. i
ab650 6e 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20 nt isError;
ab660 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
ab670 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 de returned by t
ab680 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a he function. */.
ab690 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
ab6a0 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 ; /* Colla
ab6b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
ab6c0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74 .};../*.** A Set
ab6d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 structure is us
ab6e0 65 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73 ed for quick tes
ab6f0 74 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61 ting to see if a
ab700 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72 value.** is par
ab710 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74 t of a small set
ab720 2e 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64 . Sets are used
ab730 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f to implement co
ab740 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a de like.** this:
ab750 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 .** x
ab760 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f .y IN ('hi','hoo
ab770 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65 ','hum').*/.type
ab780 64 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53 def struct Set S
ab790 65 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b et;.struct Set {
ab7a0 0a 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20 . Hash hash;
ab7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 /* A s
ab7c0 65 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73 et is just a has
ab7d0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 h table */. Has
ab7e0 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20 hElem *prev;
ab7f0 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c /* Previousl
ab800 79 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20 y accessed hash
ab810 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a elemen */.};../*
ab820 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
ab830 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d of the virtual m
ab840 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 achine. This st
ab850 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
ab860 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a the complete.**
ab870 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 state of the vi
ab880 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a rtual machine..*
ab890 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 *.** The "sqlite
ab8a0 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 3_stmt" structur
ab8b0 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 e pointer that i
ab8c0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 s returned by sq
ab8d0 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a lite3_compile().
ab8e0 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 ** is really a p
ab8f0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
ab900 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
ab910 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
ab920 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 he Vdbe.inVtabMe
ab930 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 thod variable is
ab940 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f set to non-zero
ab950 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f for the duratio
ab960 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 n of.** any virt
ab970 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 ual table method
ab980 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 invocations mad
ab990 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 e by the vdbe pr
ab9a0 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 ogram. It is.**
ab9b0 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 set to 2 for xDe
ab9c0 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c stroy method cal
ab9d0 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c ls and 1 for all
ab9e0 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 other methods.
ab9f0 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 This.** variable
aba00 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f is used for two
aba10 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c purposes: to al
aba20 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 low xDestroy met
aba30 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a hods to execute.
aba40 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 ** "DROP TABLE"
aba50 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 statements and t
aba60 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e o prevent some n
aba70 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63 74 asty side effect
aba80 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 s of.** malloc f
aba90 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 ailure when SQLi
abaa0 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 te is invoked re
abab0 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 cursively by a v
abac0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a irtual table .**
abad0 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e method function
abae0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 ..*/.struct Vdbe
abaf0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
abb00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
abb10 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
abb20 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 6f 77 6e nection that own
abb30 73 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 s this statement
abb40 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 */. Vdbe *pPre
abb50 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a v,*pNext; /*
abb60 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 Linked list of
abb70 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73 VDBEs with the s
abb80 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20 ame Vdbe.db */.
abb90 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 int nOp;
abba0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
abbb0 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f er of instructio
abbc0 6e 73 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61 ns in the progra
abbd0 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c m */. int nOpAl
abbe0 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f loc; /
abbf0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
abc00 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
abc10 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 aOp[] */. Op *a
abc20 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
abc30 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 /* Space to h
abc40 6f 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 old the virtual
abc50 6d 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 machine's progra
abc60 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 m */. int nLabe
abc70 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f l; /
abc80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65 * Number of labe
abc90 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 ls used */. int
abca0 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20 nLabelAlloc;
abcb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
abcc0 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 f slots allocate
abcd0 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f d in aLabel[] */
abce0 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 . int *aLabel;
abcf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
abd00 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
abd10 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 labels */. Mem
abd20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 **apArg;
abd30 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 /* Arguments
abd40 20 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 to currently ex
abd50 65 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e ecuting user fun
abd60 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a ction */. Mem *
abd70 61 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 aColName;
abd80 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d /* Column nam
abd90 65 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a es to return */.
abda0 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 Mem *pResultSe
abdb0 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 t; /* Poi
abdc0 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 nter to an array
abdd0 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 of results */.
abde0 20 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b u16 nResColumn;
abdf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
abe00 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
abe10 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 one row of the
abe20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
abe30 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 u16 nCursor;
abe40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
abe50 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 r of slots in ap
abe60 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43 Csr[] */. VdbeC
abe70 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 ursor **apCsr;
abe80 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e /* One elemen
abe90 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 20 t of this array
abea0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 75 for each open cu
abeb0 72 73 6f 72 20 2a 2f 0a 20 20 75 38 20 65 72 72 rsor */. u8 err
abec0 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 orAction;
abed0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 /* Recovery ac
abee0 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 tion to do in ca
abef0 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a se of an error *
abf00 2f 0a 20 20 75 38 20 6f 6b 56 61 72 3b 20 20 20 /. u8 okVar;
abf10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
abf20 72 75 65 20 69 66 20 61 7a 56 61 72 5b 5d 20 68 rue if azVar[] h
abf30 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 as been initiali
abf40 7a 65 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 6e zed */. ynVar n
abf50 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Var;
abf60 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
abf70 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 tries in aVar[]
abf80 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 */. Mem *aVar;
abf90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
abfa0 56 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f Values for the O
abfb0 50 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 P_Variable opcod
abfc0 65 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 e. */. char **a
abfd0 7a 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 zVar;
abfe0 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61 /* Name of varia
abff0 62 6c 65 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 bles */. u32 ma
ac000 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 gic;
ac010 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 /* Magic numbe
ac020 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 r for sanity che
ac030 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e cking */. int n
ac040 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
ac050 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ac060 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 memory locations
ac070 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 currently alloc
ac080 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 ated */. Mem *a
ac090 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
ac0a0 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 /* The memory
ac0b0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 locations */. u
ac0c0 33 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20 32 cacheCtr;
ac0d0 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75 /* VdbeCu
ac0e0 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 rsor row cache g
ac0f0 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 eneration counte
ac100 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 r */. int pc;
ac110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ac120 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f * The program co
ac130 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 unter */. int r
ac140 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
ac150 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
ac160 65 74 75 72 6e 20 2a 2f 0a 20 20 63 68 61 72 20 eturn */. char
ac170 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 *zErrMsg;
ac180 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 /* Error mess
ac190 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 age written here
ac1a0 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e */. u8 explain
ac1b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
ac1c0 20 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e True if EXPLAIN
ac1d0 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 present on SQL
ac1e0 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 command */. u8
ac1f0 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 changeCntOn;
ac200 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
ac210 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
ac220 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 e-counter */. u
ac230 38 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20 8 expired;
ac240 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
ac250 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74 f the VM needs t
ac260 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 o be recompiled
ac270 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 */. u8 minWrite
ac280 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 FileFormat; /*
ac290 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 Minimum file for
ac2a0 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 mat for writable
ac2b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 database files
ac2c0 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65 */. u8 inVtabMe
ac2d0 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 thod; /*
ac2e0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f See comments abo
ac2f0 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53 ve */. u8 usesS
ac300 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 tmtJournal;
ac310 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20 /* True if uses
ac320 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 a statement jour
ac330 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 nal */. u8 read
ac340 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 Only;
ac350 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61 /* True for rea
ac360 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 d-only statement
ac370 73 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 65 70 s */. u8 isPrep
ac380 61 72 65 56 32 3b 20 20 20 20 20 20 20 20 20 2f areV2; /
ac390 2a 20 54 72 75 65 20 69 66 20 70 72 65 70 61 72 * True if prepar
ac3a0 65 64 20 77 69 74 68 20 70 72 65 70 61 72 65 5f ed with prepare_
ac3b0 76 32 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 v2() */. int nC
ac3c0 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 hange;
ac3d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 /* Number of d
ac3e0 62 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73 b changes made s
ac3f0 69 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20 ince last reset
ac400 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65 65 4d 61 */. int btreeMa
ac410 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sk; /*
ac420 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d 3e 61 Bitmask of db->a
ac430 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 65 66 Db[] entries ref
ac440 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 36 34 erenced */. i64
ac450 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 startTime;
ac460 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 /* Time whe
ac470 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 n query started
ac480 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 - used for profi
ac490 6c 69 6e 67 20 2a 2f 0a 20 20 42 74 72 65 65 4d ling */. BtreeM
ac4a0 75 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78 utexArray aMutex
ac4b0 3b 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 ; /* An array of
ac4c0 20 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65 Btree used here
ac4d0 20 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 and needing loc
ac4e0 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 ks */. int aCou
ac4f0 6e 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 nter[2];
ac500 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64 /* Counters used
ac510 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 by sqlite3_stmt
ac520 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 63 _status() */. c
ac530 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 har *zSql;
ac540 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f /* Text o
ac550 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
ac560 65 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 ent that generat
ac570 65 64 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69 ed this */. voi
ac580 64 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20 d *pFree;
ac590 20 20 20 20 20 2f 2a 20 46 72 65 65 20 74 68 69 /* Free thi
ac5a0 73 20 77 68 65 6e 20 64 65 6c 65 74 69 6e 67 20 s when deleting
ac5b0 74 68 65 20 76 64 62 65 20 2a 2f 0a 20 20 69 36 the vdbe */. i6
ac5c0 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3b 4 nFkConstraint;
ac5d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ac5e0 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e 73 74 of imm. FK const
ac5f0 72 61 69 6e 74 73 20 74 68 69 73 20 56 4d 20 2a raints this VM *
ac600 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74 44 65 66 /. i64 nStmtDef
ac610 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e Cons; /* N
ac620 75 6d 62 65 72 20 6f 66 20 64 65 66 2e 20 63 6f umber of def. co
ac630 6e 73 74 72 61 69 6e 74 73 20 77 68 65 6e 20 73 nstraints when s
ac640 74 6d 74 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 tmt started */.
ac650 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 3b int iStatement;
ac660 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
ac670 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 28 6f 72 ement number (or
ac680 20 30 20 69 66 20 68 61 73 20 6e 6f 74 20 6f 70 0 if has not op
ac690 65 6e 65 64 20 73 74 6d 74 29 20 2a 2f 0a 23 69 ened stmt) */.#i
ac6a0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
ac6b0 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b G. FILE *trace;
ac6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
ac6d0 72 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f rite an executio
ac6e0 6e 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 n trace here, if
ac6f0 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e not NULL */.#en
ac700 64 69 66 0a 20 20 56 64 62 65 46 72 61 6d 65 20 dif. VdbeFrame
ac710 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a *pFrame; /*
ac720 20 50 61 72 65 6e 74 20 66 72 61 6d 65 20 2a 2f Parent frame */
ac730 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b 20 20 . int nFrame;
ac740 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
ac750 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 mber of frames i
ac760 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 20 2a 2f n pFrame list */
ac770 0a 20 20 75 33 32 20 65 78 70 6d 61 73 6b 3b 20 . u32 expmask;
ac780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 /* Bi
ac790 6e 64 69 6e 67 20 74 6f 20 74 68 65 73 65 20 76 nding to these v
ac7a0 61 72 73 20 69 6e 76 61 6c 69 64 61 74 65 73 20 ars invalidates
ac7b0 56 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 VM */.};../*.**
ac7c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 The following ar
ac7d0 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 e allowed values
ac7e0 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a for Vdbe.magic.
ac7f0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
ac800 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 MAGIC_INIT 0
ac810 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 x26bceaa5 /*
ac820 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 Building a VDBE
ac830 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 program */.#defi
ac840 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 ne VDBE_MAGIC_RU
ac850 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 N 0xbdf20da
ac860 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 3 /* VDBE is
ac870 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 ready to execute
ac880 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 */.#define VDBE
ac890 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 _MAGIC_HALT
ac8a0 30 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 0x519c2973 /*
ac8b0 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 VDBE has comple
ac8c0 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f ted execution */
ac8d0 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 .#define VDBE_MA
ac8e0 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 GIC_DEAD 0xb
ac8f0 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 606c3c8 /* Th
ac900 65 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 e VDBE has been
ac910 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a deallocated */..
ac920 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 /*.** Function p
ac930 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c rototypes.*/.SQL
ac940 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
ac950 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 sqlite3VdbeFree
ac960 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 Cursor(Vdbe *, V
ac970 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 dbeCursor*);.voi
ac980 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 d sqliteVdbePopS
ac990 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b tack(Vdbe*,int);
ac9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac9b0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
ac9c0 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 ursorMoveto(Vdbe
ac9d0 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 Cursor*);.#if de
ac9e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
ac9f0 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 UG) || defined(V
aca00 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c DBE_PROFILE).SQL
aca10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
aca20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
aca30 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 tOp(FILE*, int,
aca40 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c Op*);.#endif.SQL
aca50 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
aca60 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
aca70 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 lTypeLen(u32);.S
aca80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
aca90 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 2 sqlite3VdbeSer
acaa0 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e ialType(Mem*, in
acab0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
acac0 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 TE u32 sqlite3Vd
acad0 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 beSerialPut(unsi
acae0 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c gned char*, int,
acaf0 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Mem*, int);.SQL
acb00 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
acb10 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
acb20 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 lGet(const unsig
acb30 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 ned char*, u32,
acb40 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
acb50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
acb60 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 e3VdbeDeleteAuxD
acb70 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 ata(VdbeFunc*, i
acb80 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 nt);..int sqlite
acb90 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 2BtreeKeyCompare
acba0 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e (BtCursor *, con
acbb0 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 st void *, int,
acbc0 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c int, int *);.SQL
acbd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
acbe0 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 sqlite3VdbeIdxKe
acbf0 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 yCompare(VdbeCur
acc00 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 sor*,UnpackedRec
acc10 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 ord*,int*);.SQLI
acc20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
acc30 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 qlite3VdbeIdxRow
acc40 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43 id(sqlite3*, BtC
acc50 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b ursor *, i64 *);
acc60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acc70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f int sqlite3MemCo
acc80 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a mpare(const Mem*
acc90 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f , const Mem*, co
acca0 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 nst CollSeq*);.S
accb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
accc0 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 t sqlite3VdbeExe
accd0 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 c(Vdbe*);.SQLITE
acce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
accf0 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 ite3VdbeList(Vdb
acd00 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
acd10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acd20 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a dbeHalt(Vdbe*);.
acd30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acd40 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 nt sqlite3VdbeCh
acd50 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d angeEncoding(Mem
acd60 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
acd70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acd80 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 ite3VdbeMemTooBi
acd90 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f g(Mem*);.SQLITE_
acda0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acdb0 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d te3VdbeMemCopy(M
acdc0 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 em*, const Mem*)
acdd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
acde0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
acdf0 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 eMemShallowCopy(
ace00 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a Mem*, const Mem*
ace10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
ace20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ace30 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d te3VdbeMemMove(M
ace40 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 em*, Mem*);.SQLI
ace50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ace60 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c qlite3VdbeMemNul
ace70 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b Terminate(Mem*);
ace80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ace90 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
acea0 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 emSetStr(Mem*, c
aceb0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c onst char*, int,
acec0 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 u8, void(*)(voi
aced0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d*));.SQLITE_PRI
acee0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
acef0 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 3VdbeMemSetInt64
acf00 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c (Mem*, i64);.SQL
acf10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
acf20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
acf30 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 etDouble(Mem*, d
acf40 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 ouble);.SQLITE_P
acf50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
acf60 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
acf70 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f l(Mem*);.SQLITE_
acf80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
acf90 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 ite3VdbeMemSetZe
acfa0 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 roBlob(Mem*,int)
acfb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
acfc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
acfd0 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 eMemSetRowSet(Me
acfe0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
acff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ad000 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
ad010 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ble(Mem*);.SQLIT
ad020 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ad030 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
ad040 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 ngify(Mem*, int)
ad050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ad060 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 i64 sqlite3Vdbe
ad070 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a IntValue(Mem*);.
ad080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ad090 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ad0a0 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a mIntegerify(Mem*
ad0b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ad0c0 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 E double sqlite3
ad0d0 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 VdbeRealValue(Me
ad0e0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
ad0f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ad100 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e VdbeIntegerAffin
ad110 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ity(Mem*);.SQLIT
ad120 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ad130 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
ad140 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ify(Mem*);.SQLIT
ad150 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ad160 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 lite3VdbeMemNume
ad170 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 rify(Mem*);.SQLI
ad180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ad190 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
ad1a0 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a mBtree(BtCursor*
ad1b0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d ,int,int,int,Mem
ad1c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
ad1d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
ad1e0 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 dbeMemRelease(Me
ad1f0 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 m *p);.SQLITE_PR
ad200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ad210 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
ad220 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 External(Mem *p)
ad230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ad240 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ad250 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a MemFinalize(Mem*
ad260 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c , FuncDef*);.SQL
ad270 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
ad280 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f t char *sqlite3O
ad290 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a pcodeName(int);.
ad2a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ad2b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ad2c0 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c mGrow(Mem *pMem,
ad2d0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 int n, int pres
ad2e0 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 erve);.SQLITE_PR
ad2f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ad300 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 3VdbeCloseStatem
ad310 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 ent(Vdbe *, int)
ad320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ad330 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
ad340 65 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 eFrameDelete(Vdb
ad350 65 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 eFrame*);.SQLITE
ad360 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ad370 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 ite3VdbeFrameRes
ad380 74 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a tore(VdbeFrame *
ad390 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ad3a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
ad3b0 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d beMemStoreType(M
ad3c0 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e em *pMem);..#ifn
ad3d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
ad3e0 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 FOREIGN_KEY.SQLI
ad3f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ad400 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 qlite3VdbeCheckF
ad410 6b 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a k(Vdbe *, int);.
ad420 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
ad430 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 qlite3VdbeCheckF
ad440 6b 28 70 2c 69 29 20 30 0a 23 65 6e 64 69 66 0a k(p,i) 0.#endif.
ad450 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
ad460 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
ad470 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 E.SQLITE_PRIVATE
ad480 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
ad490 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 eMutexArrayEnter
ad4a0 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6c 73 65 (Vdbe *p);.#else
ad4b0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
ad4c0 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 3VdbeMutexArrayE
ad4d0 6e 74 65 72 28 70 29 0a 23 65 6e 64 69 66 0a 0a nter(p).#endif..
ad4e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ad4f0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ad500 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c mTranslate(Mem*,
ad510 20 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51 4c u8);.#ifdef SQL
ad520 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
ad530 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
ad540 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
ad550 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 Sql(Vdbe*);.SQLI
ad560 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
ad570 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
ad580 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 PrettyPrint(Mem
ad590 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 *pMem, char *zBu
ad5a0 66 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 f);.#endif.SQLIT
ad5b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ad5c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 lite3VdbeMemHand
ad5d0 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 leBom(Mem *pMem)
ad5e0 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
ad5f0 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
ad600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
ad610 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ad620 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 MemExpandBlob(Me
ad630 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 m *);.#else. #d
ad640 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 efine sqlite3Vdb
ad650 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 eMemExpandBlob(x
ad660 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 ) SQLITE_OK.#end
ad670 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 if..#endif /* !d
ad680 65 66 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f efined(_VDBEINT_
ad690 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
ad6a0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 ******* End of v
ad6b0 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a dbeInt.h *******
ad6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad6e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
ad6f0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
ad700 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
ad710 20 6f 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a off in utf.c **
ad720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad730 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 ******/..#ifndef
ad740 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 SQLITE_AMALGAMA
ad750 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 TION./*.** The f
ad760 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e ollowing constan
ad770 74 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 t value is used
ad780 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 42 49 by the SQLITE_BI
ad790 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 GENDIAN and.** S
ad7a0 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 QLITE_LITTLEENDI
ad7b0 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 AN macros..*/.SQ
ad7c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
ad7d0 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e st int sqlite3on
ad7e0 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 2f 2a e = 1;.#endif /*
ad7f0 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 SQLITE_AMALGAMA
ad800 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 TION */../*.** T
ad810 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 his lookup table
ad820 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 is used to help
ad830 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 decode the firs
ad840 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 6d t byte of.** a m
ad850 75 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 63 ulti-byte UTF8 c
ad860 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 haracter..*/.sta
ad870 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
ad880 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 ed char sqlite3U
ad890 74 66 38 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a tf8Trans1[] = {.
ad8a0 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 0x00, 0x01, 0x
ad8b0 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 02, 0x03, 0x04,
ad8c0 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 0x05, 0x06, 0x07
ad8d0 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 ,. 0x08, 0x09,
ad8e0 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 0x0a, 0x0b, 0x0c
ad8f0 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 , 0x0d, 0x0e, 0x
ad900 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31 0f,. 0x10, 0x11
ad910 2c 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78 , 0x12, 0x13, 0x
ad920 31 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20 14, 0x15, 0x16,
ad930 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78 0x17,. 0x18, 0x
ad940 31 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20 19, 0x1a, 0x1b,
ad950 30 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65 0x1c, 0x1d, 0x1e
ad960 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20 , 0x1f,. 0x00,
ad970 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
ad980 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 , 0x04, 0x05, 0x
ad990 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 06, 0x07,. 0x08
ad9a0 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 , 0x09, 0x0a, 0x
ad9b0 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 0b, 0x0c, 0x0d,
ad9c0 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 0x0e, 0x0f,. 0x
ad9d0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
ad9e0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 0x03, 0x04, 0x05
ad9f0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 , 0x06, 0x07,.
ada00 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 0x00, 0x01, 0x02
ada10 2c 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78 , 0x03, 0x00, 0x
ada20 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a 01, 0x00, 0x00,.
ada30 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 };...#define WRI
ada40 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 TE_UTF8(zOut, c)
ada50 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
ada60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ada70 69 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b if( c<0x00080 ){
ada80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaa0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
adab0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46 ++ = (u8)(c&0xFF
adac0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
adae0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
adaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb10 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 \. else
adb20 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b if( c<0x00800 ){
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb50 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
adb60 30 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0xC0 + (u8)((c>>
adb70 36 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20 6)&0x1F);
adb80 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
adb90 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
adba0 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 (u8)(c & 0x3F);
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbc0 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
adbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 \. e
adc00 6c 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30 lse if( c<0x1000
adc10 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
adc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc30 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
adc40 2b 20 3d 20 30 78 45 30 20 2b 20 28 75 38 29 28 + = 0xE0 + (u8)(
adc50 28 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20 (c>>12)&0x0F);
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
adc70 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
adc80 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 0 + (u8)((c>>6)
adc90 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
adca0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
adcb0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
adcc0 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 )(c & 0x3F);
adcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adce0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 . }else{
adcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add10 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
add20 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28 Out++ = 0xF0 + (
add30 75 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 78 u8)((c>>18) & 0x
add40 30 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 07);
add50 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
add60 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 0x80 + (u8)((c>
add70 3e 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20 >12) & 0x3F);
add80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
add90 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
adda0 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 (u8)((c>>6) & 0
addb0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
addc0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
addd0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 = 0x80 + (u8)(c
adde0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
addf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ade00 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ade10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade30 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e \.}..#defin
ade40 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 e WRITE_UTF16LE(
ade50 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 zOut, c) {
ade60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ade80 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 if( c<=0xFFFF
ade90 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adec0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
aded0 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 + = (u8)(c&0x00F
adee0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
adef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf00 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
adf10 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 *zOut++ = (u8)((
adf20 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 c>>8)&0x00FF);
adf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf50 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 \. }else{
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf90 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
adfa0 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e t++ = (u8)(((c>>
adfb0 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 10)&0x003F) + ((
adfc0 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 (c-0x10000)>>10)
adfd0 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 &0x00C0)); \.
adfe0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
adff0 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 (0x00D8 + (((c-0
ae000 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 x10000)>>18)&0x0
ae010 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3));
ae020 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ae030 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 = (u8)(c&0x00FF)
ae040 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae060 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ae070 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 Out++ = (u8)(0x0
ae080 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 0DC + ((c>>8)&0x
ae090 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 03));
ae0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae0b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0f0 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 \.}..#define
ae100 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a WRITE_UTF16BE(z
ae110 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 Out, c) {
ae120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae130 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae140 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 if( c<=0xFFFF )
ae150 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
ae160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae180 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ae190 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 = (u8)((c>>8)&0
ae1a0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
ae1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ae1d0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 zOut++ = (u8)(c&
ae1e0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
ae1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae210 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 . }else{
ae220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae250 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ae260 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 ++ = (u8)(0x00D8
ae270 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 + (((c-0x10000)
ae280 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 >>18)&0x03));
ae290 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae2a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
ae2b0 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 ((c>>10)&0x003F)
ae2c0 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 + (((c-0x10000)
ae2d0 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 >>10)&0x00C0));
ae2e0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ae2f0 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 (u8)(0x00DC + (
ae300 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 (c>>8)&0x03));
ae310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae320 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ae330 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 ut++ = (u8)(c&0x
ae340 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 00FF);
ae350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae360 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae370 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ae380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3b0 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 \.}..#define
ae3c0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e READ_UTF16LE(zIn
ae3d0 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 , TERM, c){
ae3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae400 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 c = (*zIn++);
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae440 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 \. c += (
ae450 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 (*zIn++)<<8);
ae460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae490 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 if( c>=0xD800
ae4a0 26 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54 && c<0xE000 && T
ae4b0 45 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20 ERM ){
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4d0 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 \. int
ae4e0 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 c2 = (*zIn++);
ae4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae520 20 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e c2 += ((*zIn
ae530 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 ++)<<8);
ae540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae560 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 \. c =
ae570 28 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 (c2&0x03FF) + ((
ae580 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b c&0x003F)<<10) +
ae590 20 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 (((c&0x03C0)+0x
ae5a0 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 0040)<<10); \.
ae5b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ae5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5f0 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 \.}..#defi
ae600 6e 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 ne READ_UTF16BE(
ae610 7a 49 6e 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20 zIn, TERM, c){
ae620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae640 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b \. c = ((*zIn+
ae650 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
ae660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae680 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b \. c +
ae690 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 = (*zIn++);
ae6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae6d0 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 \. if( c>=0xD8
ae6e0 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 26 00 && c<0xE000 &
ae6f0 26 20 54 45 52 4d 20 29 7b 20 20 20 20 20 20 20 & TERM ){
ae700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae710 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
ae720 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b nt c2 = ((*zIn++
ae730 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 )<<8);
ae740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae760 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a \. c2 += (*z
ae770 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 In++);
ae780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae7a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 \. c
ae7b0 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b = (c2&0x03FF) +
ae7c0 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 ((c&0x003F)<<10
ae7d0 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 ) + (((c&0x03C0)
ae7e0 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 +0x0040)<<10);
ae7f0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
ae800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae830 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a \.}../*
ae840 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 .** Translate a
ae850 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 single UTF-8 cha
ae860 72 61 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 racter. Return
ae870 74 68 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 the unicode valu
ae880 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 e..**.** During
ae890 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 translation, ass
ae8a0 75 6d 65 20 74 68 61 74 20 74 68 65 20 62 79 74 ume that the byt
ae8b0 65 20 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 e that zTerm poi
ae8c0 6e 74 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 nts.** is a 0x00
ae8d0 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ..**.** Write a
ae8e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
ae8f0 65 78 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 ext unread byte
ae900 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 back into *pzNex
ae910 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f t..**.** Notes O
ae920 6e 20 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a n Invalid UTF-8:
ae930 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 .**.** * This
ae940 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c routine never al
ae950 6c 6f 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 lows a 7-bit cha
ae960 72 61 63 74 65 72 20 28 30 78 30 30 20 74 68 72 racter (0x00 thr
ae970 6f 75 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a ough 0x7f) to.**
ae980 20 20 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 be encoded
ae990 61 73 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 as a multi-byte
ae9a0 63 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 character. Any
ae9b0 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 multi-byte chara
ae9c0 63 74 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 cter that.**
ae9d0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 attempts to enc
ae9e0 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 ode a value betw
ae9f0 65 65 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 een 0x00 and 0x7
aea00 66 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 f is rendered as
aea10 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 0xfffd..**.**
aea20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 * This routine
aea30 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 never allows a U
aea40 54 46 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 TF16 surrogate v
aea50 61 6c 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 alue to be encod
aea60 65 64 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 ed..** If a
aea70 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 multi-byte chara
aea80 63 74 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f cter attempts to
aea90 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 encode a value
aeaa0 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 between.** 0
aeab0 78 64 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 xd800 and 0xe000
aeac0 20 74 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 then it is rend
aead0 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a ered as 0xfffd..
aeae0 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 **.** * Bytes
aeaf0 69 6e 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 in the range of
aeb00 30 78 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 0x80 through 0xb
aeb10 66 20 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 f which occur as
aeb20 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 the first.**
aeb30 20 20 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 byte of a char
aeb40 61 63 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 acter are interp
aeb50 72 65 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d reted as single-
aeb60 62 79 74 65 20 63 68 61 72 61 63 74 65 72 73 0a byte characters.
aeb70 2a 2a 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 ** and rende
aeb80 72 65 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 red as themselve
aeb90 73 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 s even though th
aeba0 65 79 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c ey are technical
aebb0 6c 79 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 ly.** invali
aebc0 64 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a d characters..**
aebd0 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 .** * This rou
aebe0 74 69 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 tine accepts an
aebf0 69 6e 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 infinite number
aec00 6f 66 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 of different UTF
aec10 38 20 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 8 encodings.**
aec20 20 20 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 for unicode v
aec30 61 6c 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 alues 0x80 and g
aec40 72 65 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e reater. It do n
aec50 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c ot change over-l
aec60 65 6e 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 ength.** enc
aec70 6f 64 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 odings to 0xfffd
aec80 20 61 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 as some systems
aec90 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 recommend..*/.#
aeca0 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 define READ_UTF8
aecb0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 (zIn, zTerm, c)
aecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aecd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 \. c
aece0 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 = *(zIn++);
aecf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aed10 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 \. if( c
aed20 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 >=0xc0 ){
aed30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aed40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aed50 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 \. c = sq
aed60 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b lite3Utf8Trans1[
aed70 63 2d 30 78 63 30 5d 3b 20 20 20 20 20 20 20 20 c-0xc0];
aed80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aed90 20 5c 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 \. while( zI
aeda0 6e 21 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 n!=zTerm && (*zI
aedb0 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 n & 0xc0)==0x80
aedc0 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a ){ \.
aedd0 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 c = (c<<6)
aede0 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e + (0x3f & *(zIn
aedf0 2b 2b 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 ++));
aee00 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
aee10 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
aee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee40 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 \. if
aee50 28 20 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 ( c<0x80
aee60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aee80 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c \. |
aee90 7c 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 | (c&0xFFFFF800)
aeea0 3d 3d 30 78 44 38 30 30 20 20 20 20 20 20 20 20 ==0xD800
aeeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeec0 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 \. || (
aeed0 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 c&0xFFFFFFFE)==0
aeee0 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 xFFFE ){ c = 0x
aeef0 46 46 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c FFFD; } \
aef00 0a 20 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 . }.SQLITE_PRIV
aef10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
aef20 74 66 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 tf8Read(. const
aef30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
aef40 7a 49 6e 2c 20 20 20 20 20 20 20 2f 2a 20 46 69 zIn, /* Fi
aef50 72 73 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d rst byte of UTF-
aef60 38 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 8 character */.
aef70 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
aef80 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20 char **pzNext
aef90 20 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20 /* Write first
aefa0 62 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20 byte past UTF-8
aefb0 63 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a char here */.){.
aefc0 20 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53 int c;.. /* S
aefd0 61 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38 ame as READ_UTF8
aefe0 28 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74 () above but wit
aeff0 68 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70 hout the zTerm p
af000 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46 arameter.. ** F
af010 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c or this routine,
af020 20 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55 we assume the U
af030 54 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c TF8 string is al
af040 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e ways zero-termin
af050 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d ated.. */. c =
af060 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20 69 66 28 *(zIn++);. if(
af070 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20 20 c>=0xc0 ){.
af080 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 c = sqlite3Utf8T
af090 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a 20 rans1[c-0xc0];.
af0a0 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 while( (*zIn
af0b0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
af0c0 0a 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 . c = (c<<6
af0d0 29 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 ) + (0x3f & *(zI
af0e0 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 n++));. }.
af0f0 20 69 66 28 20 63 3c 30 78 38 30 0a 20 20 20 20 if( c<0x80.
af100 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 || (c&0xFFFF
af110 46 38 30 30 29 3d 3d 30 78 44 38 30 30 0a 20 20 F800)==0xD800.
af120 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 || (c&0xFF
af130 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
af140 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 ){ c = 0xFFFD;
af150 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65 78 74 20 }. }. *pzNext
af160 3d 20 7a 49 6e 3b 0a 20 20 72 65 74 75 72 6e 20 = zIn;. return
af170 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 c;.}...../*.** I
af180 66 20 74 68 65 20 54 52 41 4e 53 4c 41 54 45 5f f the TRANSLATE_
af190 54 52 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 TRACE macro is d
af1a0 65 66 69 6e 65 64 2c 20 74 68 65 20 76 61 6c 75 efined, the valu
af1b0 65 20 6f 66 20 65 61 63 68 20 4d 65 6d 20 69 73 e of each Mem is
af1c0 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f 6e 20 73 .** printed on s
af1d0 74 64 65 72 72 20 6f 6e 20 74 68 65 20 77 61 79 tderr on the way
af1e0 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74 20 6f 66 into and out of
af1f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
af200 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a 2f 20 0a ranslate()..*/ .
af210 2f 2a 20 23 64 65 66 69 6e 65 20 54 52 41 4e 53 /* #define TRANS
af220 4c 41 54 45 5f 54 52 41 43 45 20 31 20 2a 2f 0a LATE_TRACE 1 */.
af230 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
af240 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a OMIT_UTF16./*.**
af250 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 This routine tr
af260 61 6e 73 66 6f 72 6d 73 20 74 68 65 20 69 6e 74 ansforms the int
af270 65 72 6e 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 ernal text encod
af280 69 6e 67 20 75 73 65 64 20 62 79 20 70 4d 65 6d ing used by pMem
af290 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65 64 45 6e to.** desiredEn
af2a0 63 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f c. It is an erro
af2b0 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 r if the string
af2c0 69 73 20 61 6c 72 65 61 64 79 20 6f 66 20 74 68 is already of th
af2d0 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 65 6e 63 e desired.** enc
af2e0 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20 2a 70 4d oding, or if *pM
af2f0 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 em does not cont
af300 61 69 6e 20 61 20 73 74 72 69 6e 67 20 76 61 6c ain a string val
af310 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
af320 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
af330 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
af340 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 75 38 20 e(Mem *pMem, u8
af350 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 desiredEnc){. i
af360 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 nt len;
af370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
af380 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
af390 6f 75 74 70 75 74 20 73 74 72 69 6e 67 20 69 6e output string in
af3a0 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 bytes */. unsi
af3b0 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b gned char *zOut;
af3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
af3d0 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 /* Output buff
af3e0 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 er */. unsigned
af3f0 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 char *zIn;
af400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
af410 20 49 6e 70 75 74 20 69 74 65 72 61 74 6f 72 20 Input iterator
af420 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
af430 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 ar *zTerm;
af440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
af450 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 20 20 d of input */.
af460 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
af470 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
af480 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
af490 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e iterator */. un
af4a0 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 signed int c;..
af4b0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
af4c0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
af4d0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
af4e0 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
af4f0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 assert( pMem->f
af500 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 3b 0a lags&MEM_Str );.
af510 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
af520 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 20 enc!=desiredEnc
af530 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
af540 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a 20 20 61 m->enc!=0 );. a
af550 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d ssert( pMem->n>=
af560 30 20 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 0 );..#if define
af570 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 d(TRANSLATE_TRAC
af580 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 E) && defined(SQ
af590 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a LITE_DEBUG). {.
af5a0 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 char zBuf[10
af5b0 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0];. sqlite3V
af5c0 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e dbeMemPrettyPrin
af5d0 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 t(pMem, zBuf);.
af5e0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
af5f0 72 2c 20 22 49 4e 50 55 54 3a 20 20 25 73 5c 6e r, "INPUT: %s\n
af600 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 ", zBuf);. }.#e
af610 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ndif.. /* If th
af620 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 e translation is
af630 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 20 between UTF-16
af640 6c 69 74 74 6c 65 20 61 6e 64 20 62 69 67 20 65 little and big e
af650 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a 20 20 2a ndian, then . *
af660 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 * all that is re
af670 71 75 69 72 65 64 20 69 73 20 74 6f 20 73 77 61 quired is to swa
af680 70 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 p the byte order
af690 2e 20 54 68 69 73 20 63 61 73 65 20 69 73 20 68 . This case is h
af6a0 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 69 66 66 andled. ** diff
af6b0 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 erently from the
af6c0 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 others.. */.
af6d0 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53 if( pMem->enc!=S
af6e0 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20 64 65 QLITE_UTF8 && de
af6f0 73 69 72 65 64 45 6e 63 21 3d 53 51 4c 49 54 45 siredEnc!=SQLITE
af700 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 75 38 20 _UTF8 ){. u8
af710 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74 20 72 63 temp;. int rc
af720 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
af730 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
af740 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 teable(pMem);.
af750 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
af760 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 _OK ){. ass
af770 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
af780 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 72 NOMEM );. r
af790 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
af7a0 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 49 EM;. }. zI
af7b0 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a n = (u8*)pMem->z
af7c0 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a ;. zTerm = &z
af7d0 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a In[pMem->n&~1];.
af7e0 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a while( zIn<z
af7f0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 74 65 Term ){. te
af800 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 20 20 20 mp = *zIn;.
af810 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e 2b 31 29 *zIn = *(zIn+1)
af820 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 ;. zIn++;.
af830 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d 20 74 65 *zIn++ = te
af840 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d mp;. }. pM
af850 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 em->enc = desire
af860 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74 6f 20 74 dEnc;. goto t
af870 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b 0a 20 20 ranslate_out;.
af880 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c 65 6e 20 }.. /* Set len
af890 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e to the maximum n
af8a0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
af8b0 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f equired in the o
af8c0 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f utput buffer. */
af8d0 0a 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e . if( desiredEn
af8e0 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c==SQLITE_UTF8 )
af8f0 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f {. /* When co
af900 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 nverting from UT
af910 46 2d 31 36 2c 20 74 68 65 20 6d 61 78 69 6d 75 F-16, the maximu
af920 6d 20 67 72 6f 77 74 68 20 72 65 73 75 6c 74 73 m growth results
af930 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 72 61 from. ** tra
af940 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d 62 79 74 nslating a 2-byt
af950 65 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 e character to a
af960 20 34 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 4-byte UTF-8 ch
af970 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 aracter.. **
af980 41 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 73 A single byte is
af990 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 required for th
af9a0 65 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 0a e output string.
af9b0 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 ** nul-termi
af9c0 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 nator.. */.
af9d0 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 7e 31 3b pMem->n &= ~1;
af9e0 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d . len = pMem-
af9f0 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 20 7d 65 >n * 2 + 1;. }e
afa00 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e lse{. /* When
afa10 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d converting from
afa20 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 UTF-8 to UTF-16
afa30 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f the maximum gro
afa40 77 74 68 20 69 73 20 63 61 75 73 65 64 0a 20 20 wth is caused.
afa50 20 20 2a 2a 20 77 68 65 6e 20 61 20 31 2d 62 79 ** when a 1-by
afa60 74 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 te UTF-8 charact
afa70 65 72 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64 er is translated
afa80 20 69 6e 74 6f 20 61 20 32 2d 62 79 74 65 20 55 into a 2-byte U
afa90 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20 63 68 61 TF-16. ** cha
afaa0 72 61 63 74 65 72 2e 20 54 77 6f 20 62 79 74 65 racter. Two byte
afab0 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20 69 s are required i
afac0 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 n the output buf
afad0 66 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 fer for the.
afae0 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f ** nul-terminato
afaf0 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 r.. */. le
afb00 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 n = pMem->n * 2
afb10 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 + 2;. }.. /* S
afb20 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e 74 20 et zIn to point
afb30 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
afb40 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 the input buffer
afb50 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20 70 6f and zTerm to po
afb60 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79 74 65 20 int 1. ** byte
afb70 70 61 73 74 20 74 68 65 20 65 6e 64 2e 0a 20 20 past the end..
afb80 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 **. ** Variable
afb90 20 7a 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 zOut is set to
afba0 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 75 74 point at the out
afbb0 70 75 74 20 62 75 66 66 65 72 2c 20 73 70 61 63 put buffer, spac
afbc0 65 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 e obtained. **
afbd0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
afbe0 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 20 7a 49 loc().. */. zI
afbf0 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a n = (u8*)pMem->z
afc00 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e ;. zTerm = &zIn
afc10 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75 [pMem->n];. zOu
afc20 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
afc30 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c locRaw(pMem->db,
afc40 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 4f len);. if( !zO
afc50 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ut ){. return
afc60 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
afc70 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a }. z = zOut;..
afc80 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d if( pMem->enc=
afc90 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a =SQLITE_UTF8 ){.
afca0 20 20 20 20 69 66 28 20 64 65 73 69 72 65 64 45 if( desiredE
afcb0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
afcc0 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 LE ){. /* U
afcd0 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 4c TF-8 -> UTF-16 L
afce0 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a ittle-endian */.
afcf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e while( zIn
afd00 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 <zTerm ){.
afd10 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 /* c = sqlite3
afd20 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 Utf8Read(zIn, zT
afd30 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a erm, (const u8**
afd40 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 )&zIn); */.
afd50 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e READ_UTF8(zIn
afd60 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 , zTerm, c);.
afd70 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 WRITE_UTF16
afd80 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 LE(z, c);.
afd90 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
afda0 20 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 assert( desir
afdb0 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
afdc0 46 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 2f F16BE );. /
afdd0 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 * UTF-8 -> UTF-1
afde0 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 2a 2f 0a 6 Big-endian */.
afdf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e while( zIn
afe00 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 <zTerm ){.
afe10 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33 /* c = sqlite3
afe20 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 Utf8Read(zIn, zT
afe30 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a erm, (const u8**
afe40 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20 )&zIn); */.
afe50 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e READ_UTF8(zIn
afe60 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20 , zTerm, c);.
afe70 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 WRITE_UTF16
afe80 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 BE(z, c);.
afe90 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d }. }. pMem
afea0 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 ->n = (int)(z -
afeb0 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b 2b 20 zOut);. *z++
afec0 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
afed0 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
afee0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
afef0 38 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 8 );. if( pMe
aff00 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 m->enc==SQLITE_U
aff10 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 TF16LE ){.
aff20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 /* UTF-16 Little
aff30 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 -endian -> UTF-8
aff40 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
aff50 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
aff60 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 READ_UTF16
aff70 4c 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65 72 LE(zIn, zIn<zTer
aff80 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 m, c); .
aff90 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 WRITE_UTF8(z, c)
affa0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
affb0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 lse{. /* UT
affc0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 F-16 Big-endian
affd0 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 -> UTF-8 */.
affe0 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 while( zIn<zTe
afff0 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 rm ){. RE
b0000 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 AD_UTF16BE(zIn,
b0010 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0a zIn<zTerm, c); .
b0020 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
b0030 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 F8(z, c);.
b0040 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d }. }. pMem
b0050 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 ->n = (int)(z -
b0060 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 zOut);. }. *z
b0070 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 = 0;. assert( (
b0080 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 pMem->n+(desired
b0090 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc==SQLITE_UTF8
b00a0 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a ?1:2))<=len );..
b00b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
b00c0 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
b00d0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 pMem->flags &=
b00e0 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d ~(MEM_Static|MEM
b00f0 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b _Dyn|MEM_Ephem);
b0100 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 . pMem->enc = d
b0110 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 esiredEnc;. pMe
b0120 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d m->flags |= (MEM
b0130 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a _Term|MEM_Dyn);.
b0140 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 pMem->z = (cha
b0150 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d r*)zOut;. pMem-
b0160 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d >zMalloc = pMem-
b0170 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f >z;..translate_o
b0180 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 ut:.#if defined(
b0190 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 TRANSLATE_TRACE)
b01a0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
b01b0 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 TE_DEBUG). {.
b01c0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
b01d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
b01e0 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 eMemPrettyPrint(
b01f0 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 pMem, zBuf);.
b0200 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
b0210 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c "OUTPUT: %s\n",
b0220 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 zBuf);. }.#end
b0230 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 if. return SQLI
b0240 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
b0250 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
b0260 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f cks for a byte-o
b0270 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 rder mark at the
b0280 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
b0290 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 e .** UTF-16 str
b02a0 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 ing stored in *p
b02b0 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 Mem. If one is p
b02c0 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 resent, it is re
b02d0 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 moved and.** the
b02e0 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
b02f0 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 Mem adjusted. T
b0300 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
b0310 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 not do any.** b
b0320 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 yte-swapping, it
b0330 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 just sets Mem.e
b0340 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 nc appropriately
b0350 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f ..**.** The allo
b0360 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 cation (static,
b0370 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e dynamic etc.) an
b0380 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 d encoding of th
b0390 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 e Mem may be.**
b03a0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
b03b0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c function..*/.SQL
b03c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b03d0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 sqlite3VdbeMemHa
b03e0 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 ndleBom(Mem *pMe
b03f0 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 m){. int rc = S
b0400 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 QLITE_OK;. u8 b
b0410 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 om = 0;.. asser
b0420 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b t( pMem->n>=0 );
b0430 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 . if( pMem->n>1
b0440 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 ){. u8 b1 =
b0450 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a *(u8 *)pMem->z;.
b0460 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 u8 b2 = *(((
b0470 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 u8 *)pMem->z) +
b0480 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 1);. if( b1==
b0490 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 0xFE && b2==0xFF
b04a0 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 ){. bom =
b04b0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a SQLITE_UTF16BE;.
b04c0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 }. if( b1
b04d0 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 ==0xFF && b2==0x
b04e0 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 FE ){. bom
b04f0 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 = SQLITE_UTF16LE
b0500 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 ;. }. }. .
b0510 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 if( bom ){.
b0520 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
b0530 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
b0540 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 (pMem);. if(
b0550 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
b0560 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d . pMem->n -
b0570 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f = 2;. memmo
b0580 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 ve(pMem->z, &pMe
b0590 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e m->z[2], pMem->n
b05a0 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a );. pMem->z
b05b0 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 [pMem->n] = '\0'
b05c0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b ;. pMem->z[
b05d0 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 pMem->n+1] = '\0
b05e0 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ';. pMem->f
b05f0 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
b0600 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e ;. pMem->en
b0610 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 c = bom;. }.
b0620 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
b0630 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
b0640 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
b0650 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 ../*.** pZ is a
b0660 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e UTF-8 encoded un
b0670 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 icode string. If
b0680 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 nByte is less t
b0690 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 han zero,.** ret
b06a0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
b06b0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 f unicode charac
b06c0 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f ters in pZ up to
b06d0 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 (but not includ
b06e0 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 ing).** the firs
b06f0 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 t 0x00 byte. If
b0700 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 nByte is not les
b0710 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 s than zero, ret
b0720 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 urn the.** numbe
b0730 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 r of unicode cha
b0740 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 racters in the f
b0750 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a irst nByte of pZ
b0760 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 (or up to .** t
b0770 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 he first 0x00, w
b0780 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 hichever comes f
b0790 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 irst)..*/.SQLITE
b07a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b07b0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 ite3Utf8CharLen(
b07c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c const char *zIn,
b07d0 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 int nByte){. i
b07e0 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 nt r = 0;. cons
b07f0 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 t u8 *z = (const
b0800 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 u8*)zIn;. cons
b0810 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 t u8 *zTerm;. i
b0820 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 f( nByte>=0 ){.
b0830 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 zTerm = &z[nB
b0840 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 yte];. }else{.
b0850 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 zTerm = (cons
b0860 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a t u8*)(-1);. }.
b0870 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 assert( z<=zTe
b0880 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a rm );. while( *
b0890 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 z!=0 && z<zTerm
b08a0 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b ){. SQLITE_SK
b08b0 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 IP_UTF8(z);.
b08c0 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 r++;. }. retur
b08d0 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 n r;.}../* This
b08e0 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 test function is
b08f0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 not currently u
b0900 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d sed by the autom
b0910 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e ated test-suite.
b0920 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 .** Hence it is
b0930 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
b0940 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e in debug builds.
b0950 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
b0960 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 SQLITE_TEST) &&
b0970 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
b0980 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e EBUG)./*.** Tran
b0990 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 slate UTF-8 to U
b09a0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 TF-8..**.** This
b09b0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 has the effect
b09c0 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 of making sure t
b09d0 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 hat the string i
b09e0 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a s well-formed.**
b09f0 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 UTF-8. Miscode
b0a00 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 d characters are
b0a10 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 removed..**.**
b0a20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 The translation
b0a30 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 is done in-place
b0a40 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d (since it is im
b0a50 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 possible for the
b0a60 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d .** correct UTF-
b0a70 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 8 encoding to be
b0a80 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d longer than a m
b0a90 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e alformed encodin
b0aa0 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 g)..*/.SQLITE_PR
b0ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0ac0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 3Utf8To8(unsigne
b0ad0 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 d char *zIn){.
b0ae0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b0af0 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 Out = zIn;. uns
b0b00 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 igned char *zSta
b0b10 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 rt = zIn;. u32
b0b20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e c;.. while( zIn
b0b30 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 [0] ){. c = s
b0b40 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
b0b50 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 In, (const u8**)
b0b60 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 &zIn);. if( c
b0b70 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 !=0xfffd ){.
b0b80 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 WRITE_UTF8(zOu
b0b90 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d t, c);. }. }
b0ba0 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 . *zOut = 0;.
b0bb0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 return (int)(zOu
b0bc0 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 t - zStart);.}.#
b0bd0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
b0be0 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
b0bf0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
b0c00 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 UTF-16 string i
b0c10 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 n the native enc
b0c20 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 oding into a UTF
b0c30 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 -8 string..** Me
b0c40 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
b0c50 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 UTF-8 string is
b0c60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
b0c70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e qlite3_malloc an
b0c80 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 d must.** be fre
b0c90 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
b0ca0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a g function..**.*
b0cb0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
b0cc0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ed if there is a
b0cd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 n allocation err
b0ce0 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
b0cf0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
b0d00 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 te3Utf16to8(sqli
b0d10 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 te3 *db, const v
b0d20 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 oid *z, int nByt
b0d30 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d e){. Mem m;. m
b0d40 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
b0d50 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
b0d60 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
b0d70 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
b0d80 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 z, nByte, SQLIT
b0d90 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 E_UTF16NATIVE, S
b0da0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
b0db0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
b0dc0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 geEncoding(&m, S
b0dd0 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 QLITE_UTF8);. i
b0de0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
b0df0 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
b0e00 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
b0e10 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 (&m);. m.z =
b0e20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 0;. }. assert(
b0e30 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b0e40 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e Term)!=0 || db->
b0e50 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
b0e60 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 assert( (m.fla
b0e70 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 gs & MEM_Str)!=0
b0e80 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
b0e90 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e iled );. return
b0ea0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b0eb0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 Dyn)!=0 ? m.z :
b0ec0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
b0ed0 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a db, m.z);.}../*.
b0ee0 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 ** Convert a UTF
b0ef0 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 -8 string to the
b0f00 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 UTF-16 encoding
b0f10 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
b0f20 72 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 rameter.** enc.
b0f30 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
b0f40 20 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 new string is r
b0f50 65 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 eturned, and the
b0f60 20 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 value of *pnOut
b0f70 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 .** is set to th
b0f80 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
b0f90 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
b0fa0 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 in bytes. The ca
b0fb0 6c 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 ll should.** arr
b0fc0 61 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c ange to call sql
b0fd0 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 ite3DbFree() on
b0fe0 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 the returned poi
b0ff0 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a nter when it is.
b1000 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 ** no longer req
b1010 75 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 uired..** .** If
b1020 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
b1030 65 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 e occurs, NULL i
b1040 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
b1050 68 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c he db.mallocFail
b1060 65 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a ed.** flag set..
b1070 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b1080 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 _ENABLE_STAT2.SQ
b1090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
b10a0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f r *sqlite3Utf8to
b10b0 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 16(sqlite3 *db,
b10c0 75 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c u8 enc, char *z,
b10d0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f int n, int *pnO
b10e0 75 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 ut){. Mem m;.
b10f0 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 memset(&m, 0, si
b1100 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 zeof(m));. m.db
b1110 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 = db;. sqlite3
b1120 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d VdbeMemSetStr(&m
b1130 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
b1140 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 TF8, SQLITE_STAT
b1150 49 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 IC);. if( sqlit
b1160 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
b1170 74 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 te(&m, enc) ){.
b1180 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d assert( db->m
b1190 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
b11a0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b11b0 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d . assert( m.z==
b11c0 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a m.zMalloc );. *
b11d0 70 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 pnOut = m.n;. r
b11e0 65 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e eturn m.z;.}.#en
b11f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 7a 49 6e 20 69 dif../*.** zIn i
b1200 73 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 s a UTF-16 encod
b1210 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e ed unicode strin
b1220 67 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 g at least nChar
b1230 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
b1240 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
b1250 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
b1260 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 in the first nCh
b1270 61 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 ar unicode chara
b1280 63 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 cters.** in pZ.
b1290 20 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e nChar must be n
b12a0 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a on-negative..*/.
b12b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b12c0 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 nt sqlite3Utf16B
b12d0 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 yteLen(const voi
b12e0 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 d *zIn, int nCha
b12f0 72 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 r){. int c;. u
b1300 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e nsigned char con
b1310 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 st *z = zIn;. i
b1320 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 0a 20 20 69 nt n = 0;. . i
b1330 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e f( SQLITE_UTF16N
b1340 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 ATIVE==SQLITE_UT
b1350 46 31 36 42 45 20 29 7b 0a 20 20 20 20 77 68 69 F16BE ){. whi
b1360 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 le( n<nChar ){.
b1370 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 READ_UTF16B
b1380 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 E(z, 1, c);.
b1390 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d n++;. }. }
b13a0 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 else{. while(
b13b0 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 n<nChar ){.
b13c0 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a READ_UTF16LE(z
b13d0 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e , 1, c);. n
b13e0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ++;. }. }.
b13f0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d 28 return (int)(z-(
b1400 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f unsigned char co
b1410 6e 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 nst *)zIn);.}..#
b1420 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
b1430 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 E_TEST)./*.** Th
b1440 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
b1450 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 43 lled from the TC
b1460 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 L test function
b1470 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74 "translate_selft
b1480 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63 est"..** It chec
b1490 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69 6d ks that the prim
b14a0 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69 61 itives for seria
b14b0 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65 72 lizing and deser
b14c0 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 ializing.** char
b14d0 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20 65 acters in each e
b14e0 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76 65 ncoding are inve
b14f0 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74 68 rses of each oth
b1500 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
b1510 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
b1520 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 76 6f e3UtfSelfTest(vo
b1530 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 id){. unsigned
b1540 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 int i, t;. unsi
b1550 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b 32 gned char zBuf[2
b1560 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 0];. unsigned c
b1570 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b har *z;. int n;
b1580 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
b1590 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 c;.. for(i=0; i
b15a0 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b <0x00110000; i++
b15b0 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b ){. z = zBuf;
b15c0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 . WRITE_UTF8(
b15d0 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 z, i);. n = (
b15e0 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 int)(z-zBuf);.
b15f0 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 assert( n>0 &&
b1600 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 n<=4 );. z[0
b1610 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a ] = 0;. z = z
b1620 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 6c Buf;. c = sql
b1630 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c 20 ite3Utf8Read(z,
b1640 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b (const u8**)&z);
b1650 0a 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 . t = i;.
b1660 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 if( i>=0xD800 &&
b1670 20 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d i<=0xDFFF ) t =
b1680 20 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 0xFFFD;. if(
b1690 20 28 69 26 30 78 46 46 46 46 46 46 46 45 29 3d (i&0xFFFFFFFE)=
b16a0 3d 30 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 =0xFFFE ) t = 0x
b16b0 46 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 FFFD;. assert
b16c0 28 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 ( c==t );. as
b16d0 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d sert( (z-zBuf)==
b16e0 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 n );. }. for(i
b16f0 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 =0; i<0x00110000
b1700 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
b1710 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 i>=0xD800 && i<0
b1720 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 xE000 ) continue
b1730 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a ;. z = zBuf;.
b1740 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c WRITE_UTF16L
b1750 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d E(z, i);. n =
b1760 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a (int)(z-zBuf);.
b1770 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 assert( n>0
b1780 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a && n<=4 );. z
b1790 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d [0] = 0;. z =
b17a0 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f zBuf;. READ_
b17b0 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29 UTF16LE(z, 1, c)
b17c0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d ;. assert( c=
b17d0 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =i );. assert
b17e0 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b ( (z-zBuf)==n );
b17f0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
b1800 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b i<0x00110000; i+
b1810 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 +){. if( i>=0
b1820 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 xD800 && i<0xE00
b1830 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
b1840 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b1850 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c WRITE_UTF16BE(z,
b1860 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e i);. n = (in
b1870 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 t)(z-zBuf);.
b1880 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e assert( n>0 && n
b1890 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 <=4 );. z[0]
b18a0 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 = 0;. z = zBu
b18b0 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 f;. READ_UTF1
b18c0 36 42 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 6BE(z, 1, c);.
b18d0 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 assert( c==i )
b18e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a ;. assert( (z
b18f0 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d -zBuf)==n );. }
b1900 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
b1910 49 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 ITE_TEST */.#end
b1920 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
b1930 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a T_UTF16 */../***
b1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
b1950 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a of utf.c *******
b1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
b1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
b19a0 6e 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a n file util.c **
b19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b19d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
b19e0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
b19f0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
b1a00 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
b1a10 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
b1a20 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
b1a30 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
b1a40 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
b1a50 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
b1a60 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
b1a70 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
b1a80 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
b1a90 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
b1aa0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
b1ab0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
b1ac0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
b1ad0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
b1ae0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
b1af0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
b1b00 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
b1b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1b50 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 **.** Utility fu
b1b60 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 nctions used thr
b1b70 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a oughout sqlite..
b1b80 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
b1b90 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f contains functio
b1ba0 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e ns for allocatin
b1bb0 67 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 g memory, compar
b1bc0 69 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 ing.** strings,
b1bd0 61 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 and stuff like t
b1be0 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 hat..**.*/.#ifde
b1bf0 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 f SQLITE_HAVE_IS
b1c00 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d NAN.# include <m
b1c10 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f ath.h>.#endif../
b1c20 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65 *.** Routine nee
b1c30 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ded to support t
b1c40 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 he testcase() ma
b1c50 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 cro..*/.#ifdef S
b1c60 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 QLITE_COVERAGE_T
b1c70 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
b1c80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
b1c90 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a overage(int x){.
b1ca0 20 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d static int dum
b1cb0 6d 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20 my = 0;. dummy
b1cc0 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a += x;.}.#endif..
b1cd0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
b1ce0 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e e if the floatin
b1cf0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 g point value is
b1d00 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e Not a Number (N
b1d10 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 aN)..**.** Use t
b1d20 68 65 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20 he math library
b1d30 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e isnan() function
b1d40 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 if compiled wit
b1d50 68 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 h SQLITE_HAVE_IS
b1d60 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 NAN..** Otherwis
b1d70 65 2c 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f e, we have our o
b1d80 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f wn implementatio
b1d90 6e 20 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20 n that works on
b1da0 6d 6f 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f most systems..*/
b1db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b1dc0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e int sqlite3IsNaN
b1dd0 28 64 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e (double x){. in
b1de0 74 20 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76 t rc; /* The v
b1df0 61 6c 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23 alue return */.#
b1e00 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
b1e10 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20 TE_HAVE_ISNAN).
b1e20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73 /*. ** Systems
b1e30 20 74 68 61 74 20 73 75 70 70 6f 72 74 20 74 68 that support th
b1e40 65 20 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72 e isnan() librar
b1e50 79 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c y function shoul
b1e60 64 20 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20 d probably. **
b1e70 6d 61 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62 make use of it b
b1e80 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 y compiling with
b1e90 20 2d 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 -DSQLITE_HAVE_I
b1ea0 53 4e 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61 SNAN. But we ha
b1eb0 76 65 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68 ve. ** found th
b1ec0 61 74 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20 at many systems
b1ed0 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f do not have a wo
b1ee0 72 6b 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75 rking isnan() fu
b1ef0 6e 63 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74 nction so. ** t
b1f00 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
b1f10 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 on is provided a
b1f20 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 s an alternative
b1f30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
b1f40 20 4e 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 NaN test someti
b1f50 6d 65 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d mes fails if com
b1f60 70 69 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74 piled on GCC wit
b1f70 68 20 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 h -ffast-math..
b1f80 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 ** On the other
b1f90 20 68 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f hand, the use o
b1fa0 66 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f f -ffast-math co
b1fb0 6d 65 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c mes with the fol
b1fc0 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e lowing. ** warn
b1fd0 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 ing:. **. **
b1fe0 20 20 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 This option
b1ff0 5b 2d 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 [-ffast-math] sh
b2000 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75 ould never be tu
b2010 72 6e 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 rned on by any.
b2020 20 2a 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 ** -O opti
b2030 6f 6e 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20 on since it can
b2040 72 65 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 result in incorr
b2050 65 63 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70 ect output for p
b2060 72 6f 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 rograms. **
b2070 20 20 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f which depend o
b2080 6e 20 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 n an exact imple
b2090 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 mentation of IEE
b20a0 45 20 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 E or ISO . **
b20b0 20 20 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66 rules/specif
b20c0 69 63 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 ications for mat
b20d0 68 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a h functions.. *
b20e0 2a 0a 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 *. ** Under MSV
b20f0 43 2c 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74 C, this NaN test
b2100 20 6d 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d may fail if com
b2110 70 69 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f piled with a flo
b2120 61 74 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e ating-. ** poin
b2130 74 20 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 t precision mode
b2140 20 6f 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a other than /fp:
b2150 70 72 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 precise. From t
b2160 68 65 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f he MSDN . ** do
b2170 63 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a cumentation:. *
b2180 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20 *. ** The
b2190 63 6f 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f compiler [with /
b21a0 66 70 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c fp:precise] will
b21b0 20 70 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 properly handle
b21c0 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 comparisons .
b21d0 2a 2a 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e ** involvin
b21e0 67 20 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 g NaN. For examp
b21f0 6c 65 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75 le, x != x evalu
b2200 61 74 65 73 20 74 6f 20 74 72 75 65 20 69 66 20 ates to true if
b2210 78 20 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 x is NaN . **
b2220 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 .... */.#if
b2230 64 65 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f def __FAST_MATH_
b2240 5f 0a 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65 _.# error SQLite
b2250 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 will not work c
b2260 6f 72 72 65 63 74 6c 79 20 77 69 74 68 20 74 68 orrectly with th
b2270 65 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 e -ffast-math op
b2280 74 69 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e tion of GCC..#en
b2290 64 69 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 dif. volatile d
b22a0 6f 75 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 ouble y = x;. v
b22b0 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a olatile double z
b22c0 20 3d 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21 = y;. rc = (y!
b22d0 3d 7a 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69 =z);.#else /* i
b22e0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
b22f0 5f 48 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a _HAVE_ISNAN) */.
b2300 20 20 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b rc = isnan(x);
b2310 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
b2320 45 5f 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a E_HAVE_ISNAN */.
b2330 20 20 74 65 73 74 63 61 73 65 28 20 72 63 20 29 testcase( rc )
b2340 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
b2350 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
b2360 61 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 a string length
b2370 74 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20 that is limited
b2380 74 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20 73 to what can be s
b2390 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65 tored in.** lowe
b23a0 72 20 33 30 20 62 69 74 73 20 6f 66 20 61 20 33 r 30 bits of a 3
b23b0 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 2-bit signed int
b23c0 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eger..**.** The
b23d0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 value returned w
b23e0 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67 ill never be neg
b23f0 61 74 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c ative. Nor will
b2400 20 69 74 20 65 76 65 72 20 62 65 20 67 72 65 61 it ever be grea
b2410 74 65 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 ter.** than the
b2420 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66 actual length of
b2430 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f the string. Fo
b2440 72 20 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69 r very long stri
b2450 6e 67 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20 ngs (greater.**
b2460 74 68 61 6e 20 31 47 69 42 29 20 74 68 65 20 76 than 1GiB) the v
b2470 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69 alue returned mi
b2480 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e ght be less than
b2490 20 74 68 65 20 74 72 75 65 20 73 74 72 69 6e 67 the true string
b24a0 20 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49 length..*/.SQLI
b24b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b24c0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 qlite3Strlen30(c
b24d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 onst char *z){.
b24e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 const char *z2
b24f0 3d 20 7a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 = z;. if( z==0
b2500 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 ) return 0;. wh
b2510 69 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b ile( *z2 ){ z2++
b2520 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 78 33 ; }. return 0x3
b2530 66 66 66 66 66 66 66 20 26 20 28 69 6e 74 29 28 fffffff & (int)(
b2540 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a z2 - z);.}../*.*
b2550 2a 20 53 65 74 20 74 68 65 20 6d 6f 73 74 20 72 * Set the most r
b2560 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 ecent error code
b2570 20 61 6e 64 20 65 72 72 6f 72 20 73 74 72 69 6e and error strin
b2580 67 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 g for the sqlite
b2590 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62 22 2e .** handle "db".
b25a0 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 The error code
b25b0 69 73 20 73 65 74 20 74 6f 20 22 65 72 72 5f 63 is set to "err_c
b25c0 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 ode"..**.** If i
b25d0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 t is not NULL, s
b25e0 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 tring zFormat sp
b25f0 65 63 69 66 69 65 73 20 74 68 65 20 66 6f 72 6d ecifies the form
b2600 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 at of the.** err
b2610 6f 72 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 or string in the
b2620 20 73 74 79 6c 65 20 6f 66 20 74 68 65 20 70 72 style of the pr
b2630 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 intf functions:
b2640 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a The following.**
b2650 20 66 6f 72 6d 61 74 20 63 68 61 72 61 63 74 65 format characte
b2660 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a rs are allowed:.
b2670 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 **.** %s
b2680 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 Insert a stri
b2690 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 ng.** %z
b26a0 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 A string that
b26b0 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 should be freed
b26c0 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 after use.**
b26d0 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 %d Inser
b26e0 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t an integer.**
b26f0 20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 %T Ins
b2700 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 ert a token.**
b2710 20 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 %S Inse
b2720 72 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 rt the first ele
b2730 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 ment of a SrcLis
b2740 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 t.**.** zFormat
b2750 61 6e 64 20 61 6e 79 20 73 74 72 69 6e 67 20 74 and any string t
b2760 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f okens that follo
b2770 77 20 69 74 20 61 72 65 20 61 73 73 75 6d 65 64 w it are assumed
b2780 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 to be.** encode
b2790 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a d in UTF-8..**.*
b27a0 2a 20 54 6f 20 63 6c 65 61 72 20 74 68 65 20 6d * To clear the m
b27b0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
b27c0 20 66 6f 72 20 73 71 6c 69 74 65 20 68 61 6e 64 for sqlite hand
b27d0 6c 65 20 22 64 62 22 2c 20 73 71 6c 69 74 65 33 le "db", sqlite3
b27e0 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 Error.** should
b27f0 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 65 be called with e
b2800 72 72 5f 63 6f 64 65 20 73 65 74 20 74 6f 20 53 rr_code set to S
b2810 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f QLITE_OK and zFo
b2820 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e rmat set.** to N
b2830 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ULL..*/.SQLITE_P
b2840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b2850 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 te3Error(sqlite3
b2860 20 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f *db, int err_co
b2870 64 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a de, const char *
b2880 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
b2890 20 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e if( db && (db->
b28a0 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 pErr || (db->pEr
b28b0 72 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 r = sqlite3Value
b28c0 4e 65 77 28 64 62 29 29 21 3d 30 29 20 29 7b 0a New(db))!=0) ){.
b28d0 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 db->errCode
b28e0 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 = err_code;.
b28f0 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 if( zFormat ){.
b2900 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 char *z;.
b2910 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a va_list ap;.
b2920 20 20 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 va_start(a
b2930 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 p, zFormat);.
b2940 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d z = sqlite3VM
b2950 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d Printf(db, zForm
b2960 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 20 20 76 at, ap);. v
b2970 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 a_end(ap);.
b2980 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
b2990 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 Str(db->pErr, -1
b29a0 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , z, SQLITE_UTF8
b29b0 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 , SQLITE_DYNAMIC
b29c0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
b29d0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
b29e0 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c SetStr(db->pErr,
b29f0 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 0, 0, SQLITE_UT
b2a00 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 F8, SQLITE_STATI
b2a10 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a C);. }. }.}.
b2a20 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 ./*.** Add an er
b2a30 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 ror message to p
b2a40 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 Parse->zErrMsg a
b2a50 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 nd increment pPa
b2a60 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68 rse->nErr..** Th
b2a70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d e following form
b2a80 61 74 74 69 6e 67 20 63 68 61 72 61 63 74 65 72 atting character
b2a90 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a s are allowed:.*
b2aa0 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 *.** %s
b2ab0 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e Insert a strin
b2ac0 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 g.** %z
b2ad0 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 A string that
b2ae0 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 should be freed
b2af0 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 after use.**
b2b00 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 %d Insert
b2b10 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 an integer.**
b2b20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 %T Inse
b2b30 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 rt a token.**
b2b40 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 %S Inser
b2b50 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d t the first elem
b2b60 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 ent of a SrcList
b2b70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
b2b80 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 75 tion should be u
b2b90 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20 61 6e sed to report an
b2ba0 79 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 y error that occ
b2bb0 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f urs whilst.** co
b2bc0 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73 mpiling an SQL s
b2bd0 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77 tatement (i.e. w
b2be0 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 70 72 ithin sqlite3_pr
b2bf0 65 70 61 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a epare()). The.**
b2c00 20 6c 61 73 74 20 74 68 69 6e 67 20 74 68 65 20 last thing the
b2c10 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
b2c20 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 ) function does
b2c30 69 73 20 63 6f 70 79 20 74 68 65 20 65 72 72 6f is copy the erro
b2c40 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79 20 74 r.** stored by t
b2c50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 his function int
b2c60 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 o the database h
b2c70 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 71 6c 69 andle using sqli
b2c80 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46 te3Error()..** F
b2c90 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 unction sqlite3E
b2ca0 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 20 62 65 rror() should be
b2cb0 20 75 73 65 64 20 64 75 72 69 6e 67 20 73 74 61 used during sta
b2cc0 74 65 6d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e tement execution
b2cd0 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73 74 65 .** (sqlite3_ste
b2ce0 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51 p() etc.)..*/.SQ
b2cf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b2d00 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 d sqlite3ErrorMs
b2d10 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c g(Parse *pParse,
b2d20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
b2d30 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
b2d40 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 _list ap;. sqli
b2d50 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
b2d60 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e ->db;. pParse->
b2d70 6e 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 nErr++;. sqlite
b2d80 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 3DbFree(db, pPar
b2d90 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 se->zErrMsg);.
b2da0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
b2db0 72 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 65 2d rmat);. pParse-
b2dc0 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 >zErrMsg = sqlit
b2dd0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
b2de0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
b2df0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 70 50 61 a_end(ap);. pPa
b2e00 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 rse->rc = SQLITE
b2e10 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _ERROR;.}../*.**
b2e20 20 43 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 Clear the error
b2e30 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 message in pPar
b2e40 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 se, if any.*/.SQ
b2e50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b2e60 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c d sqlite3ErrorCl
b2e70 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 ear(Parse *pPars
b2e80 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e){. sqlite3DbF
b2e90 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ree(pParse->db,
b2ea0 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 pParse->zErrMsg)
b2eb0 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 ;. pParse->zErr
b2ec0 4d 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 Msg = 0;. pPars
b2ed0 65 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a e->nErr = 0;.}..
b2ee0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e /*.** Convert an
b2ef0 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 SQL-style quote
b2f00 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 d string into a
b2f10 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 normal string by
b2f20 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 removing.** the
b2f30 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 quote character
b2f40 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 s. The conversi
b2f50 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c on is done in-pl
b2f60 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ace. If the.**
b2f70 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 input does not b
b2f80 65 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 egin with a quot
b2f90 65 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 e character, the
b2fa0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
b2fb0 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a * is a no-op..**
b2fc0 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 .** The input st
b2fd0 72 69 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 ring must be zer
b2fe0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 o-terminated. A
b2ff0 20 6e 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e new zero-termin
b3000 61 74 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 ator.** is added
b3010 20 74 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 to the dequoted
b3020 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 string..**.** T
b3030 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
b3040 69 73 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 is -1 if no dequ
b3050 6f 74 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 oting occurs or
b3060 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
b3070 65 0a 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 e.** dequoted st
b3080 72 69 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 ring, exclusive
b3090 6f 66 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d of the zero term
b30a0 69 6e 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f inator, if dequo
b30b0 74 69 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 ting does.** occ
b30c0 75 72 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 ur..**.** 2002-F
b30d0 65 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 eb-14: This rout
b30e0 69 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 ine is extended
b30f0 74 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 to remove MS-Acc
b3100 65 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 ess style.** bra
b3110 63 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e ckets from aroun
b3120 64 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 d identifers. F
b3130 6f 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 or example: "[a
b3140 2d 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a -b-c]" becomes.*
b3150 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 * "a-b-c"..*/.SQ
b3160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b3170 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 sqlite3Dequote(
b3180 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 char *z){. char
b3190 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c quote;. int i,
b31a0 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 j;. if( z==0 )
b31b0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 return -1;. qu
b31c0 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 ote = z[0];. sw
b31d0 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 itch( quote ){.
b31e0 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 case '\'': b
b31f0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 reak;. case '
b3200 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 "': break;.
b3210 20 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 case '`': bre
b3220 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ak;
b3230 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 /* For MySQL
b3240 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f compatibility */
b3250 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 . case '[':
b3260 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 quote = ']'; b
b3270 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 reak; /* For MS
b3280 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 SqlServer compa
b3290 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 tibility */.
b32a0 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 default: retu
b32b0 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 rn -1;. }. for
b32c0 28 69 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 (i=1, j=0; ALWAY
b32d0 53 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 S(z[i]); i++){.
b32e0 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f if( z[i]==quo
b32f0 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 te ){. if(
b3300 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b z[i+1]==quote ){
b3310 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 . z[j++]
b3320 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 = quote;.
b3330 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 i++;. }els
b3340 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b e{. break
b3350 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
b3360 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b lse{. z[j++
b3370 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a ] = z[i];. }.
b3380 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a }. z[j] = 0;.
b3390 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f return j;.}../
b33a0 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f * Convenient sho
b33b0 72 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 rt-hand */.#defi
b33c0 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 ne UpperToLower
b33d0 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
b33e0 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 wer../*.** Some
b33f0 73 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 systems have str
b3400 69 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 icmp(). Others
b3410 68 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 have strcasecmp(
b3420 29 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 ). Because.** t
b3430 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 here is no consi
b3440 73 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 stency, we will
b3450 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a define our own..
b3460 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b3470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 E int sqlite3Str
b3480 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 ICmp(const char
b3490 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 *zLeft, const ch
b34a0 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 ar *zRight){. r
b34b0 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 egister unsigned
b34c0 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 char *a, *b;.
b34d0 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 a = (unsigned ch
b34e0 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 ar *)zLeft;. b
b34f0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
b3500 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 *)zRight;. whi
b3510 6c 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 le( *a!=0 && Upp
b3520 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 erToLower[*a]==U
b3530 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 pperToLower[*b])
b3540 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 { a++; b++; }.
b3550 72 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f return UpperToLo
b3560 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 wer[*a] - UpperT
b3570 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 oLower[*b];.}.SQ
b3580 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
b3590 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f ite3_strnicmp(co
b35a0 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c nst char *zLeft,
b35b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 const char *zRi
b35c0 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 ght, int N){. r
b35d0 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 egister unsigned
b35e0 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 char *a, *b;.
b35f0 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 a = (unsigned ch
b3600 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 ar *)zLeft;. b
b3610 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
b3620 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 *)zRight;. whi
b3630 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a le( N-- > 0 && *
b3640 61 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c a!=0 && UpperToL
b3650 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 ower[*a]==UpperT
b3660 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b oLower[*b]){ a++
b3670 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 ; b++; }. retur
b3680 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 n N<0 ? 0 : Uppe
b3690 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 rToLower[*a] - U
b36a0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b pperToLower[*b];
b36b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
b36c0 20 54 52 55 45 20 69 66 20 7a 20 69 73 20 61 20 TRUE if z is a
b36d0 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74 72 pure numeric str
b36e0 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41 4c ing. Return FAL
b36f0 53 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 SE and leave.**
b3700 2a 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67 *realnum unchang
b3710 65 64 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 ed if the string
b3720 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 contains any ch
b3730 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73 aracter which is
b3740 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 not.** part of
b3750 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 a number..**.**
b3760 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 If the string is
b3770 20 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20 73 pure numeric, s
b3780 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54 et *realnum to T
b3790 52 55 45 20 69 66 20 74 68 65 20 73 74 72 69 6e RUE if the strin
b37a0 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 g.** contains th
b37b0 65 20 27 2e 27 20 63 68 61 72 61 63 74 65 72 20 e '.' character
b37c0 6f 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73 74 or an "E+000" st
b37d0 79 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74 69 yle exponentiati
b37e0 6f 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74 on suffix..** Ot
b37f0 68 65 72 77 69 73 65 20 73 65 74 20 2a 72 65 61 herwise set *rea
b3800 6c 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20 lnum to FALSE.
b3810 4e 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20 62 Note that just b
b3820 65 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69 ecaue *realnum i
b3830 73 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73 20 s.** false does
b3840 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 not mean that th
b3850 65 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65 20 e number can be
b3860 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e successfully con
b3870 76 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 verted into.** a
b3880 6e 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20 6d n integer - it m
b3890 69 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67 2e ight be too big.
b38a0 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 .**.** An empty
b38b0 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 string is consid
b38c0 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 ered non-numeric
b38d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b38e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
b38f0 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 sNumber(const ch
b3900 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c ar *z, int *real
b3910 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 num, u8 enc){.
b3920 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d int incr = (enc=
b3930 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 =SQLITE_UTF8?1:2
b3940 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 );. if( enc==SQ
b3950 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a LITE_UTF16BE ) z
b3960 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d ++;. if( *z=='-
b3970 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a ' || *z=='+' ) z
b3980 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 += incr;. if(
b3990 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 !sqlite3Isdigit(
b39a0 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 *z) ){. retur
b39b0 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 n 0;. }. z +=
b39c0 69 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d incr;. *realnum
b39d0 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 = 0;. while( s
b39e0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b39f0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 ) ){ z += incr;
b3a00 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 }. if( *z=='.'
b3a10 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 ){. z += incr
b3a20 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 ;. if( !sqlit
b3a30 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 e3Isdigit(*z) )
b3a40 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 return 0;. wh
b3a50 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b3a60 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 git(*z) ){ z +=
b3a70 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 incr; }. *rea
b3a80 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 lnum = 1;. }.
b3a90 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a if( *z=='e' || *
b3aa0 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a 20 z=='E' ){. z
b3ab0 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 += incr;. if(
b3ac0 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d *z=='+' || *z==
b3ad0 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b '-' ) z += incr;
b3ae0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
b3af0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 3Isdigit(*z) ) r
b3b00 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 eturn 0;. whi
b3b10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 le( sqlite3Isdig
b3b20 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 it(*z) ){ z += i
b3b30 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c ncr; }. *real
b3b40 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 num = 1;. }. r
b3b50 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a eturn *z==0;.}..
b3b60 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 /*.** The string
b3b70 20 7a 5b 5d 20 69 73 20 61 6e 20 41 53 43 49 49 z[] is an ASCII
b3b80 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
b3b90 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 of a real number
b3ba0 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 ..** Convert thi
b3bb0 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f s string to a do
b3bc0 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 uble..**.** This
b3bd0 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73 routine assumes
b3be0 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 that z[] really
b3bf0 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 is a valid numb
b3c00 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 er. If it.** is
b3c10 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74 not, the result
b3c20 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
b3c30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b3c40 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 e is used instea
b3c50 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 d of the library
b3c60 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e atof() function
b3c70 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 because.** the
b3c80 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d library atof() m
b3c90 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65 ight want to use
b3ca0 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 "," as the deci
b3cb0 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 mal point instea
b3cc0 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 d.** of "." depe
b3cd0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 nding on how loc
b3ce0 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74 ale is set. But
b3cf0 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73 that would caus
b3d00 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f e problems.** fo
b3d10 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 r SQL. So this
b3d20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75 routine always u
b3d30 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65 ses "." regardle
b3d40 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f ss of locale..*/
b3d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b3d60 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 int sqlite3AtoF(
b3d70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 const char *z, d
b3d80 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b ouble *pResult){
b3d90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
b3da0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
b3db0 49 4e 54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 INT. const char
b3dc0 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 *zBegin = z;.
b3dd0 2f 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66 /* sign * signif
b3de0 69 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65 icand * (10 ^ (e
b3df0 73 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29 sign * exponent)
b3e00 29 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20 ) */. int sign
b3e10 3d 20 31 3b 20 20 20 2f 2a 20 73 69 67 6e 20 6f = 1; /* sign o
b3e20 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f f significand */
b3e30 0a 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20 20 . i64 s = 0;
b3e40 20 20 20 2f 2a 20 73 69 67 6e 69 66 69 63 61 6e /* significan
b3e50 64 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d 20 30 d */. int d = 0
b3e60 3b 20 20 20 20 20 20 2f 2a 20 61 64 6a 75 73 74 ; /* adjust
b3e70 20 65 78 70 6f 6e 65 6e 74 20 66 6f 72 20 73 68 exponent for sh
b3e80 69 66 74 69 6e 67 20 64 65 63 69 6d 61 6c 20 70 ifting decimal p
b3e90 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 73 oint */. int es
b3ea0 69 67 6e 20 3d 20 31 3b 20 20 2f 2a 20 73 69 67 ign = 1; /* sig
b3eb0 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f n of exponent */
b3ec0 0a 20 20 69 6e 74 20 65 20 3d 20 30 3b 20 20 20 . int e = 0;
b3ed0 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 2a /* exponent *
b3ee0 2f 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75 6c /. double resul
b3ef0 74 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74 73 t;. int nDigits
b3f00 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 73 6b 69 70 = 0;.. /* skip
b3f10 20 6c 65 61 64 69 6e 67 20 73 70 61 63 65 73 20 leading spaces
b3f20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 */. while( sqli
b3f30 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 te3Isspace(*z) )
b3f40 20 7a 2b 2b 3b 0a 20 20 2f 2a 20 67 65 74 20 73 z++;. /* get s
b3f50 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 ign of significa
b3f60 6e 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d nd */. if( *z==
b3f70 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 '-' ){. sign
b3f80 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 = -1;. z++;.
b3f90 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 }else if( *z=='
b3fa0 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 +' ){. z++;.
b3fb0 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 }. /* skip lea
b3fc0 64 69 6e 67 20 7a 65 72 6f 65 73 20 2a 2f 0a 20 ding zeroes */.
b3fd0 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 while( z[0]=='0
b3fe0 27 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 ' ) z++, nDigits
b3ff0 2b 2b 3b 0a 0a 20 20 2f 2a 20 63 6f 70 79 20 6d ++;.. /* copy m
b4000 61 78 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 ax significant d
b4010 69 67 69 74 73 20 74 6f 20 73 69 67 6e 69 66 69 igits to signifi
b4020 63 61 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 cand */. while(
b4030 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
b4040 2a 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 *z) && s<((LARGE
b4050 53 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 ST_INT64-9)/10)
b4060 29 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31 30 20 ){. s = s*10
b4070 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 + (*z - '0');.
b4080 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b z++, nDigits++
b4090 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 ;. }. /* skip
b40a0 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 20 non-significant
b40b0 73 69 67 6e 69 66 69 63 61 6e 64 20 64 69 67 69 significand digi
b40c0 74 73 0a 20 20 2a 2a 20 28 69 6e 63 72 65 61 73 ts. ** (increas
b40d0 65 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 e exponent by d
b40e0 74 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c to shift decimal
b40f0 20 6c 65 66 74 29 20 2a 2f 0a 20 20 77 68 69 6c left) */. whil
b4100 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 e( sqlite3Isdigi
b4110 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 t(*z) ) z++, nDi
b4120 67 69 74 73 2b 2b 2c 20 64 2b 2b 3b 0a 0a 20 20 gits++, d++;..
b4130 2f 2a 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f /* if decimal po
b4140 69 6e 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a int is present *
b4150 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 /. if( *z=='.'
b4160 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 ){. z++;.
b4170 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20 66 /* copy digits f
b4180 72 6f 6d 20 61 66 74 65 72 20 64 65 63 69 6d 61 rom after decima
b4190 6c 20 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 l to significand
b41a0 0a 20 20 20 20 2a 2a 20 28 64 65 63 72 65 61 73 . ** (decreas
b41b0 65 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20 e exponent by d
b41c0 74 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c to shift decimal
b41d0 20 72 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 77 right) */. w
b41e0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 hile( sqlite3Isd
b41f0 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 igit(*z) && s<((
b4200 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 LARGEST_INT64-9)
b4210 2f 31 30 29 20 29 7b 0a 20 20 20 20 20 20 73 20 /10) ){. s
b4220 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 = s*10 + (*z - '
b4230 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 2c 20 0');. z++,
b4240 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2d 2d 3b 0a nDigits++, d--;.
b4250 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 6b 69 }. /* ski
b4260 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e p non-significan
b4270 74 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 t digits */.
b4280 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
b4290 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c digit(*z) ) z++,
b42a0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a nDigits++;. }.
b42b0 0a 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e . /* if exponen
b42c0 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a t is present */.
b42d0 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c if( *z=='e' ||
b42e0 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 *z=='E' ){.
b42f0 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 67 65 74 20 z++;. /* get
b4300 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74 sign of exponent
b4310 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d */. if( *z==
b4320 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69 '-' ){. esi
b4330 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a gn = -1;. z
b4340 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ++;. }else if
b4350 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 ( *z=='+' ){.
b4360 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 z++;. }.
b4370 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73 /* copy digits
b4380 20 74 6f 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a to exponent */.
b4390 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
b43a0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
b43b0 0a 20 20 20 20 20 20 65 20 3d 20 65 2a 31 30 20 . e = e*10
b43c0 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 + (*z - '0');.
b43d0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 z++;. }.
b43e0 20 7d 0a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20 }.. /* adjust
b43f0 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61 exponent by d, a
b4400 6e 64 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a nd update sign *
b4410 2f 0a 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e /. e = (e*esign
b4420 29 20 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30 ) + d;. if( e<0
b4430 20 29 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d ) {. esign =
b4440 20 2d 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31 -1;. e *= -1
b4450 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
b4460 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a esign = 1;. }.
b4470 0a 20 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69 . /* if 0 signi
b4480 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 ficand */. if(
b4490 21 73 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e !s ) {. /* In
b44a0 20 74 68 65 20 49 45 45 45 20 37 35 34 20 73 74 the IEEE 754 st
b44b0 61 6e 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20 andard, zero is
b44c0 73 69 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41 signed.. ** A
b44d0 64 64 20 74 68 65 20 73 69 67 6e 20 69 66 20 77 dd the sign if w
b44e0 65 27 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61 e've seen at lea
b44f0 73 74 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a st one digit */.
b4500 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69 result = (si
b4510 67 6e 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29 gn<0 && nDigits)
b4520 20 3f 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 ? -(double)0 :
b4530 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65 (double)0;. } e
b4540 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 lse {. /* att
b4550 65 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 empt to reduce e
b4560 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 xponent */. i
b4570 66 28 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20 f( esign>0 ){.
b4580 20 20 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41 while( s<(LA
b4590 52 47 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20 RGEST_INT64/10)
b45a0 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d && e>0 ) e--,s*=
b45b0 31 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 10;. }else{.
b45c0 20 20 20 20 20 77 68 69 6c 65 28 20 21 28 73 25 while( !(s%
b45d0 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 10) && e>0 ) e--
b45e0 2c 73 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20 ,s/=10;. }..
b45f0 20 20 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65 /* adjust the
b4600 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 sign of signifi
b4610 63 61 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20 cand */. s =
b4620 73 69 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b sign<0 ? -s : s;
b4630 0a 0a 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f .. /* if expo
b4640 6e 65 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e nent, scale sign
b4650 69 66 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f ificand as appro
b4660 70 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e priate. ** an
b4670 64 20 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c d store in resul
b4680 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20 t. */. if( e
b4690 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 ){. double
b46a0 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 scale = 1.0;.
b46b0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f /* attempt to
b46c0 20 68 61 6e 64 6c 65 20 65 78 74 72 65 6d 65 6c handle extremel
b46d0 79 20 73 6d 61 6c 6c 2f 6c 61 72 67 65 20 6e 75 y small/large nu
b46e0 6d 62 65 72 73 20 62 65 74 74 65 72 20 2a 2f 0a mbers better */.
b46f0 20 20 20 20 20 20 69 66 28 20 65 3e 33 30 37 20 if( e>307
b4700 26 26 20 65 3c 33 34 32 20 29 7b 0a 20 20 20 20 && e<342 ){.
b4710 20 20 20 20 77 68 69 6c 65 28 20 65 25 33 30 38 while( e%308
b4720 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e ) { scale *= 1.
b4730 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 0e+1; e -= 1; }.
b4740 20 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 if( esig
b4750 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 n<0 ){.
b4760 20 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 result = s / sc
b4770 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ale;. r
b4780 65 73 75 6c 74 20 2f 3d 20 31 2e 30 65 2b 33 30 esult /= 1.0e+30
b4790 38 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 8;. }else
b47a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 {. resu
b47b0 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a lt = s * scale;.
b47c0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 result
b47d0 20 2a 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 *= 1.0e+308;.
b47e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
b47f0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
b4800 31 2e 30 65 2b 32 32 20 69 73 20 74 68 65 20 6c 1.0e+22 is the l
b4810 61 72 67 65 73 74 20 70 6f 77 65 72 20 6f 66 20 argest power of
b4820 31 30 20 74 68 61 6e 20 63 61 6e 20 62 65 20 0a 10 than can be .
b4830 20 20 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 ** repre
b4840 73 65 6e 74 65 64 20 65 78 61 63 74 6c 79 2e 20 sented exactly.
b4850 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 */. while
b4860 28 20 65 25 32 32 20 29 20 7b 20 73 63 61 6c 65 ( e%22 ) { scale
b4870 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d *= 1.0e+1; e -=
b4880 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 77 68 1; }. wh
b4890 69 6c 65 28 20 65 3e 30 20 29 20 7b 20 73 63 61 ile( e>0 ) { sca
b48a0 6c 65 20 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65 le *= 1.0e+22; e
b48b0 20 2d 3d 20 32 32 3b 20 7d 0a 20 20 20 20 20 20 -= 22; }.
b48c0 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b if( esign<0 ){
b48d0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c . resul
b48e0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 t = s / scale;.
b48f0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
b4900 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d result =
b4910 20 73 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 s * scale;.
b4920 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
b4930 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
b4940 20 72 65 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c result = (doubl
b4950 65 29 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a e)s;. }. }..
b4960 20 20 2f 2a 20 73 74 6f 72 65 20 74 68 65 20 72 /* store the r
b4970 65 73 75 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73 esult */. *pRes
b4980 75 6c 74 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20 ult = result;..
b4990 20 2f 2a 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 /* return numbe
b49a0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
b49b0 75 73 65 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e used */. return
b49c0 20 28 69 6e 74 29 28 7a 20 2d 20 7a 42 65 67 69 (int)(z - zBegi
b49d0 6e 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 n);.#else. retu
b49e0 72 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 rn sqlite3Atoi64
b49f0 28 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23 65 (z, pResult);.#e
b4a00 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
b4a10 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
b4a20 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 NT */.}../*.** C
b4a30 6f 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63 68 ompare the 19-ch
b4a40 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 7a aracter string z
b4a50 4e 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65 20 Num against the
b4a60 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 text representat
b4a70 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e 36 ion.** value 2^6
b4a80 33 3a 20 20 39 32 32 33 33 37 32 30 33 36 38 35 3: 922337203685
b4a90 34 37 37 35 38 30 38 2e 20 20 52 65 74 75 72 6e 4775808. Return
b4aa0 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c negative, zero,
b4ab0 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 or positive.**
b4ac0 69 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20 if zNum is less
b4ad0 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 than, equal to,
b4ae0 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
b4af0 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a the string..**.*
b4b00 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 * Unlike memcmp(
b4b10 29 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 ) this routine i
b4b20 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 s guaranteed to
b4b30 72 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65 return the diffe
b4b40 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 rence.** in the
b4b50 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61 values of the la
b4b60 73 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20 st digit if the
b4b70 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 only difference
b4b80 69 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 is in the.** las
b4b90 74 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f t digit. So, fo
b4ba0 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a r example,.**.**
b4bb0 20 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f compare2po
b4bc0 77 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38 w63("92233720368
b4bd0 35 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 54775800").**.**
b4be0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 2e will return -8.
b4bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
b4c00 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f 6e ompare2pow63(con
b4c10 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a st char *zNum){.
b4c20 20 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20 6d int c;. c = m
b4c30 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32 33 emcmp(zNum,"9223
b4c40 33 37 32 30 33 36 38 35 34 37 37 35 38 30 22 2c 37203685477580",
b4c50 31 38 29 2a 31 30 3b 0a 20 20 69 66 28 20 63 3d 18)*10;. if( c=
b4c60 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e =0 ){. c = zN
b4c70 75 6d 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20 um[18] - '8';.
b4c80 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a }. return c;.}.
b4c90 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
b4ca0 52 55 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61 RUE if zNum is a
b4cb0 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
b4cc0 6e 74 65 67 65 72 20 61 6e 64 20 77 72 69 74 65 nteger and write
b4cd0 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
b4ce0 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74 the integer int
b4cf0 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75 o *pNum. If zNu
b4d00 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 m is not an inte
b4d10 67 65 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20 ger.** or is an
b4d20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 integer that is
b4d30 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20 too large to be
b4d40 65 78 70 72 65 73 73 65 64 20 77 69 74 68 20 36 expressed with 6
b4d50 34 20 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20 4 bits,.** then
b4d60 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a return false..**
b4d70 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f .** When this ro
b4d80 75 74 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e utine was origin
b4d90 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20 ally written it
b4da0 64 65 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a dealt with only.
b4db0 2a 2a 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72 ** 32-bit number
b4dc0 73 2e 20 20 41 74 20 74 68 61 74 20 74 69 6d 65 s. At that time
b4dd0 2c 20 69 74 20 77 61 73 20 6d 75 63 68 20 66 61 , it was much fa
b4de0 73 74 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a ster than the.**
b4df0 20 61 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20 atoi() library
b4e00 72 6f 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61 routine in RedHa
b4e10 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 t 7.2..*/.SQLITE
b4e20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b4e30 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 ite3Atoi64(const
b4e40 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 char *zNum, i64
b4e50 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76 *pNum){. i64 v
b4e60 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b = 0;. int neg;
b4e70 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63 . int i, c;. c
b4e80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 onst char *zStar
b4e90 74 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 t;. while( sqli
b4ea0 74 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75 6d te3Isspace(*zNum
b4eb0 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 ) ) zNum++;. if
b4ec0 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a ( *zNum=='-' ){.
b4ed0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 neg = 1;.
b4ee0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 zNum++;. }else
b4ef0 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 if( *zNum=='+'
b4f00 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a ){. neg = 0;.
b4f10 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 zNum++;. }e
b4f20 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 lse{. neg = 0
b4f30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d ;. }. zStart =
b4f40 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 zNum;. while(
b4f50 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 zNum[0]=='0' ){
b4f60 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 zNum++; } /* Ski
b4f70 70 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 7a p over leading z
b4f80 65 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 34 eros. Ticket #24
b4f90 35 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 54 */. for(i=0;
b4fa0 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 (c=zNum[i])>='0
b4fb0 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b ' && c<='9'; i++
b4fc0 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 ){. v = v*10
b4fd0 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 + c - '0';. }.
b4fe0 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 2d *pNum = neg ? -
b4ff0 76 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 3d v : v;. if( c!=
b5000 30 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a 53 0 || (i==0 && zS
b5010 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69 tart==zNum) || i
b5020 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e >19 ){. /* zN
b5030 75 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 um is empty or c
b5040 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 ontains non-nume
b5050 72 69 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c ric text or is l
b5060 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 onger. ** tha
b5070 6e 20 31 39 20 64 69 67 69 74 73 20 28 74 68 75 n 19 digits (thu
b5080 73 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68 61 s guaranting tha
b5090 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 t it is too larg
b50a0 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e e) */. return
b50b0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
b50c0 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 4c i<19 ){. /* L
b50d0 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 ess than 19 digi
b50e0 74 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 ts, so we know t
b50f0 68 61 74 20 69 74 20 66 69 74 73 20 69 6e 20 36 hat it fits in 6
b5100 34 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 4 bits */. re
b5110 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b turn 1;. }else{
b5120 0a 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69 74 . /* 19-digit
b5130 20 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62 65 numbers must be
b5140 20 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e 20 no larger than
b5150 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
b5160 38 30 37 20 69 66 20 70 6f 73 69 74 69 76 65 0a 807 if positive.
b5170 20 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33 37 ** or 922337
b5180 32 30 33 36 38 35 34 37 37 35 38 30 38 20 69 66 2036854775808 if
b5190 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 negative. Note
b51a0 20 74 68 61 74 20 39 32 32 33 33 37 32 30 33 36 that 9223372036
b51b0 38 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a 2a 854665808. **
b51c0 20 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 is 2^63. */.
b51d0 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 return compare2
b51e0 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b pow63(zNum)<neg;
b51f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
b5200 65 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72 65 e string zNum re
b5210 70 72 65 73 65 6e 74 73 20 61 6e 20 75 6e 73 69 presents an unsi
b5220 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 54 gned integer. T
b5230 68 65 20 7a 4e 75 6d 20 73 74 72 69 6e 67 0a 2a he zNum string.*
b5240 2a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e * consists of on
b5250 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 20 e or more digit
b5260 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 69 characters and i
b5270 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 0a s terminated by.
b5280 2a 2a 20 61 20 7a 65 72 6f 20 63 68 61 72 61 63 ** a zero charac
b5290 74 65 72 2e 20 20 41 6e 79 20 73 74 72 61 79 20 ter. Any stray
b52a0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e characters in zN
b52b0 75 6d 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 um result in und
b52c0 65 66 69 6e 65 64 0a 2a 2a 20 62 65 68 61 76 69 efined.** behavi
b52d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 or..**.** If the
b52e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
b52f0 72 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 70 72 r that zNum repr
b5300 65 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 esents will fit
b5310 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 in a.** 64-bit s
b5320 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 igned integer, r
b5330 65 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 eturn TRUE. Oth
b5340 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 erwise return FA
b5350 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 LSE..**.** If th
b5360 65 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65 e negFlag parame
b5370 74 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61 ter is true, tha
b5380 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75 t means that zNu
b5390 6d 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65 m really represe
b53a0 6e 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 nts.** a negativ
b53b0 65 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20 e number. (The
b53c0 6c 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f leading "-" is o
b53d0 6d 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d mitted from zNum
b53e0 2e 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61 .) This.** para
b53f0 6d 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20 meter is needed
b5400 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62 to determine a b
b5410 6f 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41 oundary case. A
b5420 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39 string.** of "9
b5430 32 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38 2233730368547758
b5440 30 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73 08" returns fals
b5450 65 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 e if negFlag is
b5460 66 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a false or true.**
b5470 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74 if negFlag is t
b5480 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69 rue..**.** Leadi
b5490 6e 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e ng zeros are ign
b54a0 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ored..*/.SQLITE_
b54b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b54c0 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
b54d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d const char *zNum
b54e0 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a , int negFlag){.
b54f0 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
b5500 65 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 eg = 0;.. asser
b5510 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30 27 20 t( zNum[0]>='0'
b5520 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39 27 20 && zNum[0]<='9'
b5530 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 61 6e ); /* zNum is an
b5540 20 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62 65 72 unsigned number
b5550 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67 46 6c */.. if( negFl
b5560 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 ag ) neg = 1-neg
b5570 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d ;. while( *zNum
b5580 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 =='0' ){. zNu
b5590 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c m++; /* Skip l
b55a0 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 eading zeros. T
b55b0 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 icket #2454 */.
b55c0 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 4e }. for(i=0; zN
b55d0 75 6d 5b 69 5d 3b 20 69 2b 2b 29 7b 20 61 73 73 um[i]; i++){ ass
b55e0 65 72 74 28 20 7a 4e 75 6d 5b 69 5d 3e 3d 27 30 ert( zNum[i]>='0
b55f0 27 20 26 26 20 7a 4e 75 6d 5b 69 5d 3c 3d 27 39 ' && zNum[i]<='9
b5600 27 20 29 3b 20 7d 0a 20 20 69 66 28 20 69 3c 31 ' ); }. if( i<1
b5610 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 9 ){. /* Guar
b5620 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66 anteed to fit if
b5630 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 less than 19 di
b5640 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 gits */. retu
b5650 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rn 1;. }else if
b5660 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a ( i>19 ){. /*
b5670 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 Guaranteed to b
b5680 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65 e too big if gre
b5690 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67 ater than 19 dig
b56a0 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 its */. retur
b56b0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 0;. }else{.
b56c0 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61 /* Compare aga
b56d0 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 inst 2^63. */.
b56e0 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 return compare
b56f0 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 2pow63(zNum)<neg
b5700 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
b5710 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 f zNum represent
b5720 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 s an integer tha
b5730 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32 t will fit in 32
b5740 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a -bits, then set.
b5750 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68 ** *pValue to th
b5760 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 at integer and r
b5770 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 eturn true. Oth
b5780 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61 erwise return fa
b5790 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e lse..**.** Any n
b57a0 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61 on-numeric chara
b57b0 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f cters that follo
b57c0 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67 wing zNum are ig
b57d0 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69 nored..** This i
b57e0 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d s different from
b57f0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29 sqlite3Atoi64()
b5800 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20 which requires
b5810 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d the.** input num
b5820 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74 ber to be zero-t
b5830 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 erminated..*/.SQ
b5840 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b5850 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 sqlite3GetInt32
b5860 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 (const char *zNu
b5870 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b m, int *pValue){
b5880 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 . sqlite_int64
b5890 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 v = 0;. int i,
b58a0 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 c;. int neg = 0
b58b0 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d ;. if( zNum[0]=
b58c0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 ='-' ){. neg
b58d0 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b = 1;. zNum++;
b58e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75 . }else if( zNu
b58f0 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 m[0]=='+' ){.
b5900 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 zNum++;. }. w
b5910 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 hile( zNum[0]=='
b5920 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 0' ) zNum++;. f
b5930 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20 or(i=0; i<11 &&
b5940 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 (c = zNum[i] - '
b5950 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20 0')>=0 && c<=9;
b5960 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a i++){. v = v*
b5970 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 10 + c;. }.. /
b5980 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65 * The longest de
b5990 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61 cimal representa
b59a0 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74 tion of a 32 bit
b59b0 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64 integer is 10 d
b59c0 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a igits:. **. **
b59d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 123
b59e0 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20 4567890. **
b59f0 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33 2^31 -> 2147483
b5a00 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 648. */. if( i
b5a10 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 >10 ){. retur
b5a20 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 n 0;. }. if( v
b5a30 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20 -neg>2147483647
b5a40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
b5a50 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29 . }. if( neg )
b5a60 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20 {. v = -v;.
b5a70 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69 }. *pValue = (i
b5a80 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31 nt)v;. return 1
b5a90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 ;.}../*.** The v
b5aa0 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
b5ab0 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20 nteger encoding
b5ac0 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
b5ad0 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 *.** KEY:.**
b5ae0 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 A = 0xxxxxx
b5af0 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 x 7 bits of d
b5b00 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 ata and one flag
b5b10 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 bit.**
b5b20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 B = 1xxxxxxx
b5b30 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 7 bits of data a
b5b40 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a nd one flag bit.
b5b50 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78 ** C = x
b5b60 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74 xxxxxxx 8 bit
b5b70 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 s of data.**.**
b5b80 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 7 bits - A.** 1
b5b90 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 4 bits - BA.** 2
b5ba0 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 1 bits - BBA.**
b5bb0 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a 28 bits - BBBA.*
b5bc0 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42 * 35 bits - BBBB
b5bd0 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42 A.** 42 bits - B
b5be0 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73 BBBBA.** 49 bits
b5bf0 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36 - BBBBBBA.** 56
b5c00 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41 bits - BBBBBBBA
b5c10 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42 .** 64 bits - BB
b5c20 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a BBBBBBC.*/../*.*
b5c30 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74 * Write a 64-bit
b5c40 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
b5c50 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f integer to memo
b5c60 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 ry starting at p
b5c70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 [0]..** The leng
b5c80 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65 th of data write
b5c90 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e will be between
b5ca0 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20 1 and 9 bytes.
b5cb0 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f The number.** o
b5cc0 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 f bytes written
b5cd0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
b5ce0 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 ** A variable-le
b5cf0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e ngth integer con
b5d00 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77 sists of the low
b5d10 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 er 7 bits of eac
b5d20 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c h byte.** for al
b5d30 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 l bytes that hav
b5d40 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65 e the 8th bit se
b5d50 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77 t and one byte w
b5d60 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62 ith the 8th.** b
b5d70 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70 it clear. Excep
b5d80 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20 t, if we get to
b5d90 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74 the 9th byte, it
b5da0 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c stores the full
b5db0 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69 .** 8 bits and i
b5dc0 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e s the last byte.
b5dd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b5de0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
b5df0 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 tVarint(unsigned
b5e00 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29 char *p, u64 v)
b5e10 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b {. int i, j, n;
b5e20 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20 . u8 buf[10];.
b5e30 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29 if( v & (((u64)
b5e40 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29 0xff000000)<<32)
b5e50 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28 ){. p[8] = (
b5e60 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 u8)v;. v >>=
b5e70 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20 8;. for(i=7;
b5e80 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 i>=0; i--){.
b5e90 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76 p[i] = (u8)((v
b5ea0 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 & 0x7f) | 0x80)
b5eb0 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b ;. v >>= 7;
b5ec0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
b5ed0 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e n 9;. } . n
b5ee0 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 = 0;. do{.
b5ef0 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 buf[n++] = (u8)(
b5f00 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 (v & 0x7f) | 0x8
b5f10 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0);. v >>= 7;
b5f20 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 . }while( v!=0
b5f30 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30 );. buf[0] &= 0
b5f40 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e x7f;. assert( n
b5f50 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 <=9 );. for(i=0
b5f60 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a , j=n-1; j>=0; j
b5f70 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b --, i++){. p[
b5f80 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d i] = buf[j];. }
b5f90 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
b5fa0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
b5fb0 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76 ne is a faster v
b5fc0 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 ersion of sqlite
b5fd0 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61 3PutVarint() tha
b5fe0 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 t only.** works
b5ff0 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74 for 32-bit posit
b6000 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64 ive integers and
b6010 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69 which is optimi
b6020 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 zed for.** the c
b6030 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d ommon case of sm
b6040 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41 all integers. A
b6050 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 MACRO version,
b6060 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 putVarint32,.**
b6070 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 is provided whic
b6080 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69 h inlines the si
b6090 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 ngle-byte case.
b60a0 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 All code should
b60b0 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52 use.** the MACR
b60c0 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69 O version as thi
b60d0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
b60e0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
b60f0 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61 te case has.** a
b6100 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 lready been hand
b6110 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
b6120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b6130 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e e3PutVarint32(un
b6140 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
b6150 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20 u32 v){.#ifndef
b6160 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 putVarint32. if
b6170 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30 ( (v & ~0x7f)==0
b6180 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76 ){. p[0] = v
b6190 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
b61a0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 }.#endif. if(
b61b0 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d (v & ~0x3fff)==
b61c0 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 0 ){. p[0] =
b61d0 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78 (u8)((v>>7) | 0x
b61e0 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 80);. p[1] =
b61f0 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a (u8)(v & 0x7f);.
b6200 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
b6210 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
b6220 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76 e3PutVarint(p, v
b6230 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
b6240 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 a 64-bit variab
b6250 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
b6260 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 r from memory st
b6270 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a arting at p[0]..
b6280 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
b6290 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
b62a0 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 ad. The value i
b62b0 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a s stored in *v..
b62c0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b62d0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 E u8 sqlite3GetV
b62e0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 arint(const unsi
b62f0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 gned char *p, u6
b6300 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 4 *v){. u32 a,b
b6310 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 ,s;.. a = *p;.
b6320 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 /* a: p0 (unmas
b6330 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b6340 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
b6350 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 *v = a;. ret
b6360 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 1;. }.. p+
b6370 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f +;. b = *p;. /
b6380 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 * b: p1 (unmaske
b6390 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b63a0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 0x80)). {. a
b63b0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 &= 0x7f;. a
b63c0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = a<<7;. a |=
b63d0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a b;. *v = a;.
b63e0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
b63f0 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 }.. p++;. a =
b6400 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
b6410 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 ;. /* a: p0<<14
b6420 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 | p2 (unmasked)
b6430 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b6440 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
b6450 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b6460 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 7f);. b &= 0x
b6470 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 7f;. b = b<<7
b6480 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6490 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b64a0 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f turn 3;. }.. /
b64b0 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f * CSE1 from belo
b64c0 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 w */. a &= (0x7
b64d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b64e0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
b64f0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
b6500 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
b6510 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
b6520 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
b6530 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 . {. b &= (0
b6540 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6550 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 . /* moved CS
b6560 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 E1 up */. /*
b6570 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
b6580 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 (0x7f); */. a
b6590 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
b65a0 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b = b;. *v = a;
b65b0 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 . return 4;.
b65c0 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c }.. /* a: p0<<
b65d0 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 14 | p2 (masked)
b65e0 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c */. /* b: p1<<
b65f0 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 14 | p3 (unmaske
b6600 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 d) */. /* 1:sav
b6610 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 e off p0<<21 | p
b6620 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 1<<14 | p2<<7 |
b6630 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 p3 (masked) */.
b6640 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 /* moved CSE1 u
b6650 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 p */. /* a &= (
b6660 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b6670 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 ; */. b &= (0x7
b6680 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b6690 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 s = a;. /* s:
b66a0 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 p0<<14 | p2 (mas
b66b0 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a ked) */.. p++;.
b66c0 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
b66d0 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
b66e0 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 p0<<28 | p2<<14
b66f0 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p4 (unmasked)
b6700 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
b6710 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 0)). {. /* w
b6720 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65 e can skip these
b6730 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65 cause they were
b6740 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 (effectively) d
b6750 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c one above in cal
b6760 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f c'ing s */. /
b6770 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 * a &= (0x7f<<28
b6780 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 )|(0x7f<<14)|(0x
b6790 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 7f); */. /* b
b67a0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b67b0 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20 0x7f); */. b
b67c0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = b<<7;. a |=
b67d0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 b;. s = s>>1
b67e0 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 8;. *v = ((u6
b67f0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
b6800 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a return 5;. }.
b6810 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66 . /* 2:save off
b6820 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 p0<<21 | p1<<14
b6830 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d | p2<<7 | p3 (m
b6840 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20 asked) */. s =
b6850 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a s<<7;. s |= b;.
b6860 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c /* s: p0<<21 |
b6870 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 p1<<14 | p2<<7
b6880 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f | p3 (masked) */
b6890 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 .. p++;. b = b
b68a0 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b <<14;. b |= *p;
b68b0 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20 . /* b: p1<<28
b68c0 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75 | p3<<14 | p5 (u
b68d0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b68e0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
b68f0 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 . /* we can s
b6900 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69 kip this cause i
b6910 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65 t was (effective
b6920 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 ly) done above i
b6930 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a n calc'ing s */.
b6940 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 /* b &= (0x7
b6950 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
b6960 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 )|(0x7f); */.
b6970 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b6980 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d |(0x7f);. a =
b6990 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 a<<7;. a |=
b69a0 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 b;. s = s>>18
b69b0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 ;. *v = ((u64
b69c0 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 )s)<<32 | a;.
b69d0 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a return 6;. }..
b69e0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c p++;. a = a<<
b69f0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 14;. a |= *p;.
b6a00 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20 /* a: p2<<28 |
b6a10 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d p4<<14 | p6 (unm
b6a20 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b6a30 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
b6a40 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 a &= (0x1f<<2
b6a50 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b6a60 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 x7f);. b &= (
b6a70 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b6a80 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a ;. b = b<<7;.
b6a90 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 a |= b;.
b6aa0 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a s = s>>11;. *
b6ab0 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 v = ((u64)s)<<32
b6ac0 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e | a;. return
b6ad0 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 7;. }.. /* CS
b6ae0 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f E2 from below */
b6af0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 . a &= (0x7f<<1
b6b00 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 4)|(0x7f);. p++
b6b10 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
b6b20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
b6b30 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 : p3<<28 | p5<<1
b6b40 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 4 | p7 (unmasked
b6b50 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
b6b60 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 x80)). {. b
b6b70 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 &= (0x1f<<28)|(0
b6b80 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6b90 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 . /* moved CS
b6ba0 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 E2 up */. /*
b6bb0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
b6bc0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 (0x7f); */. a
b6bd0 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = a<<7;. a |
b6be0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
b6bf0 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 4;. *v = ((u6
b6c00 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
b6c10 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a return 8;. }.
b6c20 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b6c30 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <15;. a |= *p;.
b6c40 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c /* a: p4<<29 |
b6c50 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e p6<<15 | p8 (un
b6c60 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a masked) */.. /*
b6c70 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a moved CSE2 up *
b6c80 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 /. /* a &= (0x7
b6c90 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 f<<29)|(0x7f<<15
b6ca0 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 )|(0xff); */. b
b6cb0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b6cc0 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 0x7f);. b = b<<
b6cd0 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 8;. a |= b;..
b6ce0 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 s = s<<4;. b =
b6cf0 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78 p[-4];. b &= 0x
b6d00 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 7f;. b = b>>3;.
b6d10 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 s |= b;.. *v
b6d20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c = ((u64)s)<<32 |
b6d30 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b a;.. return 9;
b6d40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
b6d50 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65 32-bit variable
b6d60 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
b6d70 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 from memory star
b6d80 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a ting at p[0]..**
b6d90 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
b6da0 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 er of bytes read
b6db0 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 . The value is
b6dc0 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a stored in *v..**
b6dd0 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 72 69 6e .** If the varin
b6de0 74 20 73 74 6f 72 65 64 20 69 6e 20 70 5b 30 5d t stored in p[0]
b6df0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
b6e00 63 61 6e 20 66 69 74 20 69 6e 20 61 20 33 32 2d can fit in a 32-
b6e10 62 69 74 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 bit unsigned.**
b6e20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 73 65 integer, then se
b6e30 74 20 2a 76 20 74 6f 20 30 78 66 66 66 66 66 66 t *v to 0xffffff
b6e40 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 41 43 52 ff..**.** A MACR
b6e50 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61 O version, getVa
b6e60 72 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69 rint32, is provi
b6e70 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 ded which inline
b6e80 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65 s the .** single
b6e90 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c -byte case. All
b6ea0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 code should use
b6eb0 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 the MACRO versi
b6ec0 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66 on as .** this f
b6ed0 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 unction assumes
b6ee0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 the single-byte
b6ef0 63 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79 case has already
b6f00 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a been handled..*
b6f10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b6f20 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
b6f30 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 rint32(const uns
b6f40 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b6f50 33 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 32 *v){. u32 a,
b6f60 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 31 2d 62 b;.. /* The 1-b
b6f70 79 74 65 20 63 61 73 65 2e 20 20 4f 76 65 72 77 yte case. Overw
b6f80 68 65 6c 6d 69 6e 67 6c 79 20 74 68 65 20 6d 6f helmingly the mo
b6f90 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 61 6e 64 st common. Hand
b6fa0 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 20 2a 2a 20 led inline. **
b6fb0 62 79 20 74 68 65 20 67 65 74 56 61 72 69 6e 33 by the getVarin3
b6fc0 32 28 29 20 6d 61 63 72 6f 20 2a 2f 0a 20 20 61 2() macro */. a
b6fd0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 = *p;. /* a: p
b6fe0 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0 (unmasked) */.
b6ff0 23 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e #ifndef getVarin
b7000 74 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78 t32. if (!(a&0x
b7010 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b7020 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 30 Values between 0
b7030 20 61 6e 64 20 31 32 37 20 2a 2f 0a 20 20 20 20 and 127 */.
b7040 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 *v = a;. retu
b7050 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 rn 1;. }.#endif
b7060 0a 0a 20 20 2f 2a 20 54 68 65 20 32 2d 62 79 74 .. /* The 2-byt
b7070 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b e case */. p++;
b7080 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . b = *p;. /*
b7090 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 b: p1 (unmasked)
b70a0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b70b0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b70c0 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 Values between 1
b70d0 32 38 20 61 6e 64 20 31 36 33 38 33 20 2a 2f 0a 28 and 16383 */.
b70e0 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 a &= 0x7f;.
b70f0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b7100 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 *v = a | b;.
b7110 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a return 2;. }..
b7120 20 20 2f 2a 20 54 68 65 20 33 2d 62 79 74 65 20 /* The 3-byte
b7130 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 case */. p++;.
b7140 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 a = a<<14;. a
b7150 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 |= *p;. /* a: p
b7160 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 0<<14 | p2 (unma
b7170 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b7180 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b7190 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 /* Values betw
b71a0 65 65 6e 20 31 36 33 38 34 20 61 6e 64 20 32 30 een 16384 and 20
b71b0 39 37 31 35 31 20 2a 2f 0a 20 20 20 20 61 20 26 97151 */. a &
b71c0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b71d0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78 7f);. b &= 0x
b71e0 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 7f;. b = b<<7
b71f0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 ;. *v = a | b
b7200 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a ;. return 3;.
b7210 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 33 32 2d 62 }.. /* A 32-b
b7220 69 74 20 76 61 72 69 6e 74 20 69 73 20 75 73 65 it varint is use
b7230 64 20 74 6f 20 73 74 6f 72 65 20 73 69 7a 65 20 d to store size
b7240 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 62 information in b
b7250 74 72 65 65 73 2e 0a 20 20 2a 2a 20 4f 62 6a 65 trees.. ** Obje
b7260 63 74 73 20 61 72 65 20 72 61 72 65 6c 79 20 6c cts are rarely l
b7270 61 72 67 65 72 20 74 68 61 6e 20 32 4d 69 42 20 arger than 2MiB
b7280 6c 69 6d 69 74 20 6f 66 20 61 20 33 2d 62 79 74 limit of a 3-byt
b7290 65 20 76 61 72 69 6e 74 2e 0a 20 20 2a 2a 20 41 e varint.. ** A
b72a0 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 20 69 3-byte varint i
b72b0 73 20 73 75 66 66 69 63 69 65 6e 74 2c 20 66 6f s sufficient, fo
b72c0 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 72 65 r example, to re
b72d0 63 6f 72 64 20 74 68 65 20 73 69 7a 65 0a 20 20 cord the size.
b72e0 2a 2a 20 6f 66 20 61 20 31 30 34 38 35 36 39 2d ** of a 1048569-
b72f0 62 79 74 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 byte BLOB or str
b7300 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 ing.. **. ** W
b7310 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c 20 74 68 e only unroll th
b7320 65 20 66 69 72 73 74 20 31 2d 2c 20 32 2d 2c 20 e first 1-, 2-,
b7330 61 6e 64 20 33 2d 20 62 79 74 65 20 63 61 73 65 and 3- byte case
b7340 73 2e 20 20 54 68 65 20 76 65 72 79 0a 20 20 2a s. The very. *
b7350 2a 20 72 61 72 65 20 6c 61 72 67 65 72 20 63 61 * rare larger ca
b7360 73 65 73 20 63 61 6e 20 62 65 20 68 61 6e 64 6c ses can be handl
b7370 65 64 20 62 79 20 74 68 65 20 73 6c 6f 77 65 72 ed by the slower
b7380 20 36 34 2d 62 69 74 20 76 61 72 69 6e 74 0a 20 64-bit varint.
b7390 20 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a ** routine.. *
b73a0 2f 0a 23 69 66 20 31 0a 20 20 7b 0a 20 20 20 20 /.#if 1. {.
b73b0 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 u64 v64;. u8
b73c0 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 32 3b 0a n;.. p -= 2;.
b73d0 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 n = sqlite3G
b73e0 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 etVarint(p, &v64
b73f0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
b7400 3e 33 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 >3 && n<=9 );.
b7410 20 20 69 66 28 20 28 76 36 34 20 26 20 53 51 4c if( (v64 & SQL
b7420 49 54 45 5f 4d 41 58 5f 55 33 32 29 21 3d 76 36 ITE_MAX_U32)!=v6
b7430 34 20 29 7b 0a 20 20 20 20 20 20 2a 76 20 3d 20 4 ){. *v =
b7440 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 20 20 0xffffffff;.
b7450 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 76 20 }else{. *v
b7460 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20 = (u32)v64;.
b7470 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a }. return n;.
b7480 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 }..#else. /*
b7490 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f For following co
b74a0 64 65 20 28 6b 65 70 74 20 66 6f 72 20 68 69 73 de (kept for his
b74b0 74 6f 72 69 63 61 6c 20 72 65 63 6f 72 64 20 6f torical record o
b74c0 6e 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a 20 20 nly) shows an.
b74d0 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66 6f 72 ** unrolling for
b74e0 20 74 68 65 20 33 2d 20 61 6e 64 20 34 2d 62 79 the 3- and 4-by
b74f0 74 65 20 76 61 72 69 6e 74 20 63 61 73 65 73 2e te varint cases.
b7500 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 0a 20 This code is.
b7510 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73 ** slightly fas
b7520 74 65 72 2c 20 62 75 74 20 69 74 20 69 73 20 61 ter, but it is a
b7530 6c 73 6f 20 6c 61 72 67 65 72 20 61 6e 64 20 6d lso larger and m
b7540 75 63 68 20 68 61 72 64 65 72 20 74 6f 20 74 65 uch harder to te
b7550 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b 3b 0a st.. */. p++;.
b7560 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 b = b<<14;. b
b7570 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 |= *p;. /* b:
b7580 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d p1<<14 | p3 (unm
b7590 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b75a0 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b75b0 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 /* Values bet
b75c0 77 65 65 6e 20 32 30 39 37 31 35 32 20 61 6e 64 ween 2097152 and
b75d0 20 32 36 38 34 33 35 34 35 35 20 2a 2f 0a 20 20 268435455 */.
b75e0 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 b &= (0x7f<<14
b75f0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 )|(0x7f);. a
b7600 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b7610 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c x7f);. a = a<
b7620 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c <7;. *v = a |
b7630 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 b;. return 4
b7640 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 ;. }.. p++;.
b7650 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c a = a<<14;. a |
b7660 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 = *p;. /* a: p0
b7670 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 <<28 | p2<<14 |
b7680 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p4 (unmasked) */
b7690 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 . if (!(a&0x80)
b76a0 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57 61 6c ). {. /* Wal
b76b0 75 65 73 20 20 62 65 74 77 65 65 6e 20 32 36 38 ues between 268
b76c0 34 33 35 34 35 36 20 61 6e 64 20 33 34 33 35 39 435456 and 34359
b76d0 37 33 38 33 36 37 20 2a 2f 0a 20 20 20 20 61 20 738367 */. a
b76e0 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 &= (0x1f<<28)|(0
b76f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b7700 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c . b &= (0x1f<
b7710 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c <28)|(0x7f<<14)|
b7720 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 (0x7f);. b =
b7730 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 b<<7;. *v = a
b7740 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
b7750 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 5;. }.. /* We
b7760 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 can only reach
b7770 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 this point when
b7780 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 reading a corrup
b7790 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 t database. **
b77a0 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 file. In that c
b77b0 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 ase we are not i
b77c0 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 n any hurry. Us
b77d0 65 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c e the (relativel
b77e0 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e y. ** slow) gen
b77f0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c eral-purpose sql
b7800 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 ite3GetVarint()
b7810 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 routine to extra
b7820 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 ct the. ** valu
b7830 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 e. */. {. u6
b7840 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b 4 v64;. u8 n;
b7850 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20 .. p -= 4;.
b7860 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 n = sqlite3Get
b7870 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b Varint(p, &v64);
b7880 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35 . assert( n>5
b7890 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 && n<=9 );.
b78a0 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 *v = (u32)v64;.
b78b0 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d return n;. }
b78c0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
b78d0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
b78e0 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 er of bytes that
b78f0 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 will be needed
b7900 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 to store the giv
b7910 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 en.** 64-bit int
b7920 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eger..*/.SQLITE_
b7930 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b7940 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 te3VarintLen(u64
b7950 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 v){. int i = 0
b7960 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b ;. do{. i++;
b7970 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 . v >>= 7;.
b7980 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 }while( v!=0 &&
b7990 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20 ALWAYS(i<9) );.
b79a0 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f return i;.}.../
b79b0 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 *.** Read or wri
b79c0 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62 te a four-byte b
b79d0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
b79e0 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 r value..*/.SQLI
b79f0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
b7a00 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 qlite3Get4byte(c
b7a10 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72 onst u8 *p){. r
b7a20 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 eturn (p[0]<<24)
b7a30 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 | (p[1]<<16) |
b7a40 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d (p[2]<<8) | p[3]
b7a50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
b7a60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
b7a70 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64 ut4byte(unsigned
b7a80 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 char *p, u32 v)
b7a90 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 {. p[0] = (u8)(
b7aa0 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d v>>24);. p[1] =
b7ab0 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20 (u8)(v>>16);.
b7ac0 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38 p[2] = (u8)(v>>8
b7ad0 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29 );. p[3] = (u8)
b7ae0 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66 v;.}....#if !def
b7af0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
b7b00 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c _BLOB_LITERAL) |
b7b10 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
b7b20 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a _HAS_CODEC)./*.*
b7b30 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 * Translate a si
b7b40 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78 ngle byte of Hex
b7b50 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 into an integer
b7b60 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
b7b70 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 e only works if
b7b80 68 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 h really is a va
b7b90 6c 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c 0a lid hexadecimal.
b7ba0 2a 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20 30 ** character: 0
b7bb0 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 ..9a..fA..F.*/.s
b7bc0 74 61 74 69 63 20 75 38 20 68 65 78 54 6f 49 6e tatic u8 hexToIn
b7bd0 74 28 69 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 t(int h){. asse
b7be0 72 74 28 20 28 68 3e 3d 27 30 27 20 26 26 20 68 rt( (h>='0' && h
b7bf0 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 <='9') || (h>='
b7c00 61 27 20 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c a' && h<='f') ||
b7c10 20 20 28 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d (h>='A' && h<=
b7c20 27 46 27 29 20 29 3b 0a 23 69 66 64 65 66 20 53 'F') );.#ifdef S
b7c30 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 68 20 QLITE_ASCII. h
b7c40 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b += 9*(1&(h>>6));
b7c50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
b7c60 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 68 QLITE_EBCDIC. h
b7c70 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 += 9*(1&~(h>>4)
b7c80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 );.#endif. retu
b7c90 72 6e 20 28 75 38 29 28 68 20 26 20 30 78 66 29 rn (u8)(h & 0xf)
b7ca0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
b7cb0 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
b7cc0 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 LITERAL || SQLIT
b7cd0 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a E_HAS_CODEC */..
b7ce0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
b7cf0 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 ITE_OMIT_BLOB_LI
b7d00 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 TERAL) || define
b7d10 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 d(SQLITE_HAS_COD
b7d20 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 EC)./*.** Conver
b7d30 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c t a BLOB literal
b7d40 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27 of the form "x'
b7d50 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74 hhhhhh'" into it
b7d60 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 s binary.** valu
b7d70 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 e. Return a poi
b7d80 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61 nter to its bina
b7d90 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65 ry value. Space
b7da0 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 to hold the.**
b7db0 62 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73 binary value has
b7dc0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 been obtained f
b7dd0 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d rom malloc and m
b7de0 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a ust be freed by.
b7df0 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 ** the calling r
b7e00 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 outine..*/.SQLIT
b7e10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
b7e20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 sqlite3HexToBlob
b7e30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
b7e40 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
b7e50 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c n){. char *zBl
b7e60 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 ob;. int i;..
b7e70 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 zBlob = (char *)
b7e80 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
b7e90 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b aw(db, n/2 + 1);
b7ea0 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 . n--;. if( zB
b7eb0 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 lob ){. for(i
b7ec0 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a =0; i<n; i+=2){.
b7ed0 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d zBlob[i/2]
b7ee0 20 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 = (hexToInt(z[i
b7ef0 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e ])<<4) | hexToIn
b7f00 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d t(z[i+1]);. }
b7f10 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 . zBlob[i/2]
b7f20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
b7f30 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 n zBlob;.}.#endi
b7f40 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 f /* !SQLITE_OMI
b7f50 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c T_BLOB_LITERAL |
b7f60 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 | SQLITE_HAS_COD
b7f70 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 EC */.../*.** Ch
b7f80 61 6e 67 65 20 74 68 65 20 73 71 6c 69 74 65 2e ange the sqlite.
b7f90 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 magic from SQLIT
b7fa0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 E_MAGIC_OPEN to
b7fb0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
b7fc0 59 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 Y..** Return an
b7fd0 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 error (non-zero)
b7fe0 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 if the magic wa
b7ff0 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 s not SQLITE_MAG
b8000 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 IC_OPEN.** when
b8010 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
b8020 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 called..**.** Th
b8030 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
b8040 6c 6c 65 64 20 77 68 65 6e 20 65 6e 74 65 72 69 lled when enteri
b8050 6e 67 20 61 6e 20 53 51 4c 69 74 65 20 41 50 49 ng an SQLite API
b8060 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 . The SQLITE_MA
b8070 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 GIC_OPEN.** valu
b8080 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 e indicates that
b8090 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
b80a0 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20 nnection passed
b80b0 69 6e 74 6f 20 74 68 65 20 41 50 49 20 69 73 0a into the API is.
b80c0 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e ** open and is n
b80d0 6f 74 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 ot being used by
b80e0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e another thread.
b80f0 20 20 42 79 20 63 68 61 6e 67 69 6e 67 20 74 68 By changing th
b8100 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 e value.** to SQ
b8110 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
b8120 77 65 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 we indicate that
b8130 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
b8140 69 73 20 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 is in use..** sq
b8150 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 29 lite3SafetyOff()
b8160 20 62 65 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e below will chan
b8170 67 65 20 74 68 65 20 76 61 6c 75 65 20 62 61 63 ge the value bac
b8180 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 k to SQLITE_MAGI
b8190 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 C_OPEN.** when t
b81a0 68 65 20 41 50 49 20 65 78 69 74 73 2e 20 0a 2a he API exits. .*
b81b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b81c0 65 20 69 73 20 61 20 61 74 74 65 6d 70 74 20 74 e is a attempt t
b81d0 6f 20 64 65 74 65 63 74 20 69 66 20 74 77 6f 20 o detect if two
b81e0 74 68 72 65 61 64 73 20 75 73 65 20 74 68 65 0a threads use the.
b81f0 2a 2a 20 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 ** same sqlite*
b8200 70 6f 69 6e 74 65 72 20 61 74 20 74 68 65 20 73 pointer at the s
b8210 61 6d 65 20 74 69 6d 65 2e 20 20 54 68 65 72 65 ame time. There
b8220 20 69 73 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 is a race .** c
b8230 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 ondition so it i
b8240 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 s possible that
b8250 74 68 65 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 the error is not
b8260 20 64 65 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 detected..** Bu
b8270 74 20 75 73 75 61 6c 6c 79 20 74 68 65 20 70 72 t usually the pr
b8280 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 oblem will be se
b8290 65 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 en. The result
b82a0 77 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 will be an.** er
b82b0 72 6f 72 20 77 68 69 63 68 20 63 61 6e 20 62 65 ror which can be
b82c0 20 75 73 65 64 20 74 6f 20 64 65 62 75 67 20 74 used to debug t
b82d0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 he application t
b82e0 68 61 74 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 hat is.** using
b82f0 53 51 4c 69 74 65 20 69 6e 63 6f 72 72 65 63 74 SQLite incorrect
b8300 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 ly..**.** Ticket
b8310 20 23 32 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d #202: If db->m
b8320 61 67 69 63 20 69 73 20 6e 6f 74 20 61 20 76 61 agic is not a va
b8330 6c 69 64 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 lid open value,
b8340 74 61 6b 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a take care not.**
b8350 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 to modify the d
b8360 62 20 73 74 72 75 63 74 75 72 65 20 61 74 20 61 b structure at a
b8370 6c 6c 2e 20 20 49 74 20 63 6f 75 6c 64 20 62 65 ll. It could be
b8380 20 74 68 61 74 20 64 62 20 69 73 20 61 20 73 74 that db is a st
b8390 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 ale.** pointer.
b83a0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
b83b0 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 it could be tha
b83c0 74 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e t there has been
b83d0 20 61 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c a prior.** call
b83e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 to sqlite3_clos
b83f0 65 28 64 62 29 20 61 6e 64 20 64 62 20 68 61 73 e(db) and db has
b8400 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 been deallocate
b8410 64 2e 20 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a d. And we do.**
b8420 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 77 72 69 not want to wri
b8430 74 65 20 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 te into dealloca
b8440 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 ted memory..*/.#
b8450 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b8460 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
b8470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
b8480 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64 etyOn(sqlite3 *d
b8490 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 b){. if( db->ma
b84a0 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic==SQLITE_MAGI
b84b0 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 64 62 C_OPEN ){. db
b84c0 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 ->magic = SQLITE
b84d0 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 20 _MAGIC_BUSY;.
b84e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
b84f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
b8500 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 mutex) );. re
b8510 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
b8520 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 if( db->magic==S
b8530 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b8540 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 ){. db->magi
b8550 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
b8560 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e _ERROR;. db->
b8570 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
b8580 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
b8590 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a rn 1;.}.#endif..
b85a0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
b85b0 20 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 magic from SQLI
b85c0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 74 6f TE_MAGIC_BUSY to
b85d0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b85e0 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e EN..** Return an
b85f0 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f error (non-zero
b8600 29 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 ) if the magic w
b8610 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 as not SQLITE_MA
b8620 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e GIC_BUSY.** when
b8630 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
b8640 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 called..*/.#ifd
b8650 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
b8660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b8670 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
b8680 4f 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29 Off(sqlite3 *db)
b8690 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 {. if( db->magi
b86a0 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c==SQLITE_MAGIC_
b86b0 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e BUSY ){. db->
b86c0 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d magic = SQLITE_M
b86d0 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 AGIC_OPEN;. a
b86e0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
b86f0 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
b8700 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 tex) );. retu
b8710 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn 0;. }else{.
b8720 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 db->magic = S
b8730 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f QLITE_MAGIC_ERRO
b8740 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 R;. db->u1.is
b8750 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b Interrupted = 1;
b8760 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
b8770 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a }.}.#endif../*.
b8780 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 ** Check to make
b8790 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 20 sure we have a
b87a0 76 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72 valid db pointer
b87b0 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73 20 . This test is
b87c0 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 not.** foolproof
b87d0 20 62 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f but it does pro
b87e0 76 69 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 vide some measur
b87f0 65 20 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 e of protection
b8800 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 against.** misus
b8810 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61 e of the interfa
b8820 63 65 20 73 75 63 68 20 61 73 20 70 61 73 73 69 ce such as passi
b8830 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 ng in db pointer
b8840 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 s that are.** NU
b8850 4c 4c 20 6f 72 20 77 68 69 63 68 20 68 61 76 65 LL or which have
b8860 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 been previously
b8870 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 closed. If thi
b8880 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
b8890 73 0a 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 s.** 1 it means
b88a0 74 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e that the db poin
b88b0 74 65 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64 ter is valid and
b88c0 20 30 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 0 if it should
b88d0 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 not be.** derefe
b88e0 72 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72 renced for any r
b88f0 65 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c eason. The call
b8900 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f ing function sho
b8910 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 uld invoke.** SQ
b8920 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 LITE_MISUSE imme
b8930 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 diately..**.** s
b8940 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
b8950 6b 4f 6b 28 29 20 72 65 71 75 69 72 65 73 20 74 kOk() requires t
b8960 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 hat the db point
b8970 65 72 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a er be valid for.
b8980 2a 2a 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33 ** use. sqlite3
b8990 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f SafetyCheckSickO
b89a0 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 rOk() allows a d
b89b0 62 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 b pointer that f
b89c0 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e ailed to.** open
b89d0 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 properly and is
b89e0 20 6e 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e not fit for gen
b89f0 65 72 61 6c 20 75 73 65 20 62 75 74 20 77 68 69 eral use but whi
b8a00 63 68 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 ch can be.** use
b8a10 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
b8a20 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d to sqlite3_errm
b8a30 73 67 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f sg() or sqlite3_
b8a40 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 close()..*/.SQLI
b8a50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b8a60 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
b8a70 6b 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 kOk(sqlite3 *db)
b8a80 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 {. u32 magic;.
b8a90 20 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 if( db==0 ) ret
b8aa0 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d urn 0;. magic =
b8ab0 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 db->magic;. if
b8ac0 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f ( magic!=SQLITE_
b8ad0 4d 41 47 49 43 5f 4f 50 45 4e 20 0a 23 69 66 64 MAGIC_OPEN .#ifd
b8ae0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
b8af0 20 20 20 20 20 26 26 20 6d 61 67 69 63 21 3d 53 && magic!=S
b8b00 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b8b10 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 .#endif. ){.
b8b20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
b8b30 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 se{. return 1
b8b40 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 ;. }.}.SQLITE_P
b8b50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b8b60 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 e3SafetyCheckSic
b8b70 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 kOrOk(sqlite3 *d
b8b80 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b b){. u32 magic;
b8b90 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d . magic = db->m
b8ba0 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 agic;. if( magi
b8bb0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
b8bc0 53 49 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61 SICK &&. ma
b8bd0 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic!=SQLITE_MAGI
b8be0 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 C_OPEN &&.
b8bf0 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 magic!=SQLITE_MA
b8c00 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 GIC_BUSY ) retur
b8c10 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b n 0;. return 1;
b8c20 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
b8c30 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e *** End of util.
b8c40 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b8c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8c70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
b8c80 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
b8c90 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ash.c **********
b8ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8cc0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
b8cd0 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a eptember 22.**.*
b8ce0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
b8cf0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
b8d00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
b8d10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
b8d20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
b8d30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
b8d40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
b8d50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
b8d60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
b8d70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
b8d80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
b8d90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
b8da0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
b8db0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
b8dc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
b8dd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
b8de0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
b8df0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
b8e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
b8e40 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d is is the implem
b8e50 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 entation of gene
b8e60 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a ric hash-tables.
b8e70 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 ** used in SQLit
b8e80 65 2e 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 e..*/../* Turn b
b8e90 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 ulk memory into
b8ea0 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a a hash table obj
b8eb0 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a ect by initializ
b8ec0 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 ing the.** field
b8ed0 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 s of the Hash st
b8ee0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 ructure..**.** "
b8ef0 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 pNew" is a point
b8f00 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 er to the hash t
b8f10 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 able that is to
b8f20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a be initialized..
b8f30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b8f40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
b8f50 73 68 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 shInit(Hash *pNe
b8f60 77 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e w){. assert( pN
b8f70 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d ew!=0 );. pNew-
b8f80 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e >first = 0;. pN
b8f90 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 ew->count = 0;.
b8fa0 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 pNew->htsize =
b8fb0 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 0;. pNew->ht =
b8fc0 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 0;.}../* Remove
b8fd0 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d all entries from
b8fe0 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 a hash table.
b8ff0 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f Reclaim all memo
b9000 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 ry..** Call this
b9010 20 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 routine to dele
b9020 74 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 te a hash table
b9030 6f 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 or to reset a ha
b9040 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 sh table.** to t
b9050 68 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a he empty state..
b9060 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b9070 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
b9080 73 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 shClear(Hash *pH
b9090 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 ){. HashElem *e
b90a0 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 lem; /*
b90b0 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 For looping over
b90c0 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 all elements of
b90d0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 the table */..
b90e0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 assert( pH!=0 )
b90f0 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 ;. elem = pH->f
b9100 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 irst;. pH->firs
b9110 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 t = 0;. sqlite3
b9120 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 _free(pH->ht);.
b9130 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 pH->ht = 0;. p
b9140 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 H->htsize = 0;.
b9150 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a while( elem ){.
b9160 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 HashElem *ne
b9170 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e xt_elem = elem->
b9180 6e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 next;. sqlite
b9190 33 5f 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 3_free(elem);.
b91a0 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c elem = next_el
b91b0 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f em;. }. pH->co
b91c0 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a unt = 0;.}../*.*
b91d0 2a 20 54 68 65 20 68 61 73 68 69 6e 67 20 66 75 * The hashing fu
b91e0 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
b91f0 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 c unsigned int s
b9200 74 72 48 61 73 68 28 63 6f 6e 73 74 20 63 68 61 trHash(const cha
b9210 72 20 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b r *z, int nKey){
b9220 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 . int h = 0;.
b9230 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 assert( nKey>=0
b9240 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 );. while( nKey
b9250 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d > 0 ){. h =
b9260 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 (h<<3) ^ h ^ sq
b9270 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
b9280 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 r[(unsigned char
b9290 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 )*z++];. nKey
b92a0 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e --;. }. return
b92b0 20 68 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 h;.}.../* Link
b92c0 70 4e 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 pNew element int
b92d0 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 o the hash table
b92e0 20 70 48 2e 20 20 49 66 20 70 45 6e 74 72 79 21 pH. If pEntry!
b92f0 3d 30 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 =0 then also.**
b9300 69 6e 73 65 72 74 20 70 4e 65 77 20 69 6e 74 6f insert pNew into
b9310 20 74 68 65 20 70 45 6e 74 72 79 20 68 61 73 68 the pEntry hash
b9320 20 62 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 bucket..*/.stat
b9330 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c ic void insertEl
b9340 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 ement(. Hash *p
b9350 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H,
b9360 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 /* The complete
b9370 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 hash table */.
b9380 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 struct _ht *pEnt
b9390 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e ry, /* The en
b93a0 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70 try into which p
b93b0 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 New is inserted
b93c0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 */. HashElem *p
b93d0 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54 New /* T
b93e0 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 he element to be
b93f0 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a inserted */.){.
b9400 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 HashElem *pHea
b9410 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 d; /* Firs
b9420 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 t element alread
b9430 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 y in pEntry */.
b9440 20 69 66 28 20 70 45 6e 74 72 79 20 29 7b 0a 20 if( pEntry ){.
b9450 20 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 pHead = pEntr
b9460 79 2d 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72 y->count ? pEntr
b9470 79 2d 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20 y->chain : 0;.
b9480 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b pEntry->count+
b9490 2b 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 +;. pEntry->c
b94a0 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d hain = pNew;. }
b94b0 65 6c 73 65 7b 0a 20 20 20 20 70 48 65 61 64 20 else{. pHead
b94c0 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 = 0;. }. if( p
b94d0 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 Head ){. pNew
b94e0 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a ->next = pHead;.
b94f0 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d pNew->prev =
b9500 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 pHead->prev;.
b9510 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 if( pHead->pre
b9520 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 v ){ pHead->prev
b9530 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d ->next = pNew; }
b9540 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 . else
b9550 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 { pH->firs
b9560 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 t = pNew; }.
b9570 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e pHead->prev = pN
b9580 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ew;. }else{.
b9590 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 pNew->next = pH
b95a0 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 ->first;. if(
b95b0 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 pH->first ){ pH
b95c0 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 ->first->prev =
b95d0 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 pNew; }. pNew
b95e0 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ->prev = 0;.
b95f0 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 pH->first = pNew
b9600 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 ;. }.}.../* Res
b9610 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 ize the hash tab
b9620 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 le so that it ca
b9630 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 ntains "new_size
b9640 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a " buckets..**.**
b9650 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 The hash table
b9660 6d 69 67 68 74 20 66 61 69 6c 20 74 6f 20 72 65 might fail to re
b9670 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f size if sqlite3_
b9680 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f malloc() fails o
b9690 72 0a 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 20 r.** if the new
b96a0 73 69 7a 65 20 69 73 20 74 68 65 20 73 61 6d 65 size is the same
b96b0 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 73 69 as the prior si
b96c0 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ze..** Return TR
b96d0 55 45 20 69 66 20 74 68 65 20 72 65 73 69 7a 65 UE if the resize
b96e0 20 6f 63 63 75 72 73 20 61 6e 64 20 66 61 6c 73 occurs and fals
b96f0 65 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 e if not..*/.sta
b9700 74 69 63 20 69 6e 74 20 72 65 68 61 73 68 28 48 tic int rehash(H
b9710 61 73 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65 ash *pH, unsigne
b9720 64 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b d int new_size){
b9730 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e . struct _ht *n
b9740 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 ew_ht;
b9750 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 /* The new has
b9760 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 h table */. Has
b9770 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 hElem *elem, *ne
b9780 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 xt_elem; /* F
b9790 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
b97a0 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 existing element
b97b0 73 20 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 s */..#if SQLITE
b97c0 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
b97d0 49 54 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f 73 IT>0. if( new_s
b97e0 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 ize*sizeof(struc
b97f0 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 t _ht)>SQLITE_MA
b9800 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 LLOC_SOFT_LIMIT
b9810 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 ){. new_size
b9820 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f = SQLITE_MALLOC_
b9830 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f SOFT_LIMIT/sizeo
b9840 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 f(struct _ht);.
b9850 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a }. if( new_siz
b9860 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 e==pH->htsize )
b9870 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
b9880 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 .. /* The inabi
b9890 6c 69 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 lity to allocate
b98a0 73 20 73 70 61 63 65 20 66 6f 72 20 61 20 6c 61 s space for a la
b98b0 72 67 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 rger hash table
b98c0 69 73 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 is. ** a perfor
b98d0 6d 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 74 mance hit but it
b98e0 20 69 73 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 is not a fatal
b98f0 65 72 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 error. So mark
b9900 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 the. ** allocat
b9910 69 6f 6e 20 61 73 20 61 20 62 65 6e 69 67 6e 2e ion as a benign.
b9920 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 . */. sqlite3B
b9930 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
b9940 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 ();. new_ht = (
b9950 73 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c struct _ht *)sql
b9960 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f ite3Malloc( new_
b9970 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 size*sizeof(stru
b9980 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c ct _ht) );. sql
b9990 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
b99a0 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 loc();.. if( ne
b99b0 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e w_ht==0 ) return
b99c0 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 0;. sqlite3_fr
b99d0 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 ee(pH->ht);. pH
b99e0 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 ->ht = new_ht;.
b99f0 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 pH->htsize = ne
b9a00 77 5f 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 w_size = sqlite3
b9a10 4d 61 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68 MallocSize(new_h
b9a20 74 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 t)/sizeof(struct
b9a30 20 5f 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 _ht);. memset(
b9a40 6e 65 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73 new_ht, 0, new_s
b9a50 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 ize*sizeof(struc
b9a60 74 20 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28 65 t _ht));. for(e
b9a70 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 lem=pH->first, p
b9a80 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d H->first=0; elem
b9a90 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c ; elem = next_el
b9aa0 65 6d 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 em){. unsigne
b9ab0 64 20 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73 d int h = strHas
b9ac0 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c h(elem->pKey, el
b9ad0 65 6d 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f em->nKey) % new_
b9ae0 73 69 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65 size;. next_e
b9af0 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 lem = elem->next
b9b00 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d ;. insertElem
b9b10 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b ent(pH, &new_ht[
b9b20 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 h], elem);. }.
b9b30 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
b9b40 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 This function (
b9b50 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 for internal use
b9b60 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 only) locates a
b9b70 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a n element in an.
b9b80 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 ** hash table th
b9b90 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 67 at matches the g
b9ba0 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 iven key. The h
b9bb0 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 ash for this key
b9bc0 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 has.** already
b9bd0 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e been computed an
b9be0 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 d is passed as t
b9bf0 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
b9c00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 68 ..*/.static Hash
b9c10 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e Elem *findElemen
b9c20 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63 6f tGivenHash(. co
b9c30 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20 20 nst Hash *pH,
b9c40 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62 /* The pH to b
b9c50 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 e searched */.
b9c60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 const char *pKey
b9c70 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 , /* The key w
b9c80 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 e are searching
b9c90 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 for */. int nKe
b9ca0 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y, /*
b9cb0 42 79 74 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f Bytes in key (no
b9cc0 74 20 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20 t counting zero
b9cd0 74 65 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 terminator) */.
b9ce0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 unsigned int h
b9cf0 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 /* The hash
b9d00 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a for this key. *
b9d10 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 /.){. HashElem
b9d20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *elem;
b9d30 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
b9d40 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 loop thru the e
b9d50 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 lement list */.
b9d60 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 int count;
b9d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9d80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 /* Number of ele
b9d90 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 ments left to te
b9da0 73 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d st */.. if( pH-
b9db0 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 >ht ){. struc
b9dc0 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 t _ht *pEntry =
b9dd0 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 &pH->ht[h];.
b9de0 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 elem = pEntry->c
b9df0 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 hain;. count
b9e00 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b = pEntry->count;
b9e10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c . }else{. el
b9e20 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a em = pH->first;.
b9e30 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e count = pH->
b9e40 63 6f 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69 count;. }. whi
b9e50 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41 le( count-- && A
b9e60 4c 57 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20 LWAYS(elem) ){.
b9e70 20 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 if( elem->nKe
b9e80 79 3d 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69 74 y==nKey && sqlit
b9e90 65 33 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d e3StrNICmp(elem-
b9ea0 3e 70 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 >pKey,pKey,nKey)
b9eb0 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 72 65 ==0 ){ . re
b9ec0 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d turn elem;. }
b9ed0 0a 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d . elem = elem
b9ee0 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 ->next;. }. re
b9ef0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 turn 0;.}../* Re
b9f00 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e move a single en
b9f10 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 try from the has
b9f20 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 h table given a
b9f30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a pointer to that.
b9f40 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 ** element and a
b9f50 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 hash on the ele
b9f60 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 ment's key..*/.s
b9f70 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 tatic void remov
b9f80 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 eElementGivenHas
b9f90 68 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 h(. Hash *pH,
b9fa0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 /* The pH
b9fb0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 containing "ele
b9fc0 6d 22 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d m" */. HashElem
b9fd0 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 * elem, /* The
b9fe0 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 element to be r
b9ff0 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 emoved from the
ba000 70 48 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 pH */. unsigned
ba010 20 69 6e 74 20 68 20 20 20 20 2f 2a 20 48 61 73 int h /* Has
ba020 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 h value for the
ba030 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 element */.){.
ba040 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 struct _ht *pEnt
ba050 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e ry;. if( elem->
ba060 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d prev ){. elem
ba070 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 ->prev->next = e
ba080 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 lem->next; . }e
ba090 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 lse{. pH->fir
ba0a0 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b st = elem->next;
ba0b0 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d . }. if( elem-
ba0c0 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 >next ){. ele
ba0d0 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 m->next->prev =
ba0e0 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a elem->prev;. }.
ba0f0 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a if( pH->ht ){.
ba100 20 20 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48 pEntry = &pH
ba110 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28 ->ht[h];. if(
ba120 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d pEntry->chain==
ba130 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 45 elem ){. pE
ba140 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c ntry->chain = el
ba150 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a em->next;. }.
ba160 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e pEntry->coun
ba170 74 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t--;. assert(
ba180 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d pEntry->count>=
ba190 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 0 );. }. sqlit
ba1a0 65 33 5f 66 72 65 65 28 20 65 6c 65 6d 20 29 3b e3_free( elem );
ba1b0 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a . pH->count--;.
ba1c0 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c if( pH->count<
ba1d0 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
ba1e0 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 ( pH->first==0 )
ba1f0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 ;. assert( pH
ba200 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 ->count==0 );.
ba210 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 sqlite3HashCle
ba220 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ar(pH);. }.}../
ba230 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 * Attempt to loc
ba240 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f ate an element o
ba250 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 f the hash table
ba260 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a pH with a key.*
ba270 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 * that matches p
ba280 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 Key,nKey. Retur
ba290 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 n the data for t
ba2a0 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 his element if i
ba2b0 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f t is.** found, o
ba2c0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 r NULL if there
ba2d0 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a is no match..*/.
ba2e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
ba2f0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 oid *sqlite3Hash
ba300 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20 Find(const Hash
ba310 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *pH, const char
ba320 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 *pKey, int nKey)
ba330 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c {. HashElem *el
ba340 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c em; /* The el
ba350 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 ement that match
ba360 65 73 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 es key */. unsi
ba370 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 2f gned int h; /
ba380 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 * A hash on key
ba390 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 */.. assert( pH
ba3a0 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
ba3b0 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 pKey!=0 );. as
ba3c0 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b sert( nKey>=0 );
ba3d0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b . if( pH->ht ){
ba3e0 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 . h = strHash
ba3f0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 (pKey, nKey) % p
ba400 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c H->htsize;. }el
ba410 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 se{. h = 0;.
ba420 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 }. elem = find
ba430 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 ElementGivenHash
ba440 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c (pH, pKey, nKey,
ba450 20 68 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c h);. return el
ba460 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 em ? elem->data
ba470 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 : 0;.}../* Inser
ba480 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 t an element int
ba490 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 o the hash table
ba4a0 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69 73 pH. The key is
ba4b0 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e pKey,nKey.** an
ba4c0 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22 64 d the data is "d
ba4d0 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ata"..**.** If n
ba4e0 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 o element exists
ba4f0 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 with a matching
ba500 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 key, then a new
ba510 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 .** element is c
ba520 72 65 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20 reated and NULL
ba530 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
ba540 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c ** If another el
ba550 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 ement already ex
ba560 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61 ists with the sa
ba570 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 me key, then the
ba580 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 .** new data rep
ba590 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 laces the old da
ba5a0 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 ta and the old d
ba5b0 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e ata is returned.
ba5c0 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e .** The key is n
ba5d0 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 ot copied in thi
ba5e0 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 s instance. If
ba5f0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 a malloc fails,
ba600 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 then.** the new
ba610 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 data is returned
ba620 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 and the hash ta
ba630 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ble is unchanged
ba640 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 ..**.** If the "
ba650 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 data" parameter
ba660 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
ba670 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 is NULL, then t
ba680 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f he.** element co
ba690 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 rresponding to "
ba6a0 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 key" is removed
ba6b0 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 from the hash ta
ba6c0 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
ba6d0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
ba6e0 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 ite3HashInsert(H
ba6f0 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 ash *pH, const c
ba700 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e har *pKey, int n
ba710 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 Key, void *data)
ba720 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
ba730 20 68 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65 h; /* the
ba740 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79 hash of the key
ba750 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62 modulo hash tab
ba760 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 le size */. Has
ba770 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 hElem *elem;
ba780 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f /* Used to lo
ba790 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d op thru the elem
ba7a0 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 ent list */. Ha
ba7b0 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d shElem *new_elem
ba7c0 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 ; /* New eleme
ba7d0 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 nt added to the
ba7e0 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 pH */.. assert(
ba7f0 20 70 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pH!=0 );. asse
ba800 72 74 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 rt( pKey!=0 );.
ba810 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 assert( nKey>=0
ba820 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 );. if( pH->ht
ba830 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 size ){. h =
ba840 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b strHash(pKey, nK
ba850 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 ey) % pH->htsize
ba860 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
ba870 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d = 0;. }. elem
ba880 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 = findElementGi
ba890 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c venHash(pH,pKey,
ba8a0 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 nKey,h);. if( e
ba8b0 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 lem ){. void
ba8c0 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d *old_data = elem
ba8d0 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20 ->data;. if(
ba8e0 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 data==0 ){.
ba8f0 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 removeElementGi
ba900 76 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c venHash(pH,elem,
ba910 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 h);. }else{.
ba920 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 elem->data
ba930 3d 20 64 61 74 61 3b 0a 20 20 20 20 20 20 65 6c = data;. el
ba940 65 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b em->pKey = pKey;
ba950 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4b . assert(nK
ba960 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b ey==elem->nKey);
ba970 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
ba980 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a n old_data;. }.
ba990 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20 if( data==0 )
ba9a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f return 0;. new_
ba9b0 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d elem = (HashElem
ba9c0 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 *)sqlite3Malloc(
ba9d0 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d sizeof(HashElem
ba9e0 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 ) );. if( new_e
ba9f0 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 lem==0 ) return
baa00 64 61 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d data;. new_elem
baa10 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 ->pKey = pKey;.
baa20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 new_elem->nKey
baa30 3d 20 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c = nKey;. new_el
baa40 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b em->data = data;
baa50 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a . pH->count++;.
baa60 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e if( pH->count>
baa70 3d 31 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74 =10 && pH->count
baa80 20 3e 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20 > 2*pH->htsize
baa90 29 7b 0a 20 20 20 20 69 66 28 20 72 65 68 61 73 ){. if( rehas
baaa0 68 28 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a h(pH, pH->count*
baab0 32 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 2) ){. asse
baac0 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 rt( pH->htsize>0
baad0 20 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 73 74 );. h = st
baae0 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 rHash(pKey, nKey
baaf0 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a ) % pH->htsize;.
bab00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
bab10 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e pH->ht ){. in
bab20 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 sertElement(pH,
bab30 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f &pH->ht[h], new_
bab40 65 6c 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a elem);. }else{.
bab50 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e insertElemen
bab60 74 28 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 t(pH, 0, new_ele
bab70 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e m);. }. return
bab80 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../********
bab90 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 ****** End of ha
baba0 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a sh.c ***********
babb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
babc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
babd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
babe0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
babf0 65 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a e opcodes.c ****
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac20 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 *****/./* Automa
bac30 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 tically generate
bac40 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 d. Do not edit
bac50 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b */./* See the mk
bac60 6f 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 opcodec.awk scri
bac70 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 pt for details.
bac80 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
bac90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
baca0 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 AIN) || !defined
bacb0 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
bacc0 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 ned(VDBE_PROFILE
bacd0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
bace0 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 ITE_DEBUG).SQLIT
bacf0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
bad00 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 char *sqlite3Opc
bad10 6f 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a odeName(int i){.
bad20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 static const ch
bad30 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 ar *const azName
bad40 5b 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 [] = { "?",.
bad50 20 2f 2a 20 20 20 31 20 2a 2f 20 22 47 6f 74 6f /* 1 */ "Goto
bad60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a ",. /* 2 *
bad70 2f 20 22 47 6f 73 75 62 22 2c 0a 20 20 20 20 20 / "Gosub",.
bad80 2f 2a 20 20 20 33 20 2a 2f 20 22 52 65 74 75 72 /* 3 */ "Retur
bad90 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 n",. /* 4
bada0 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20 20 20 20 */ "Yield",.
badb0 20 2f 2a 20 20 20 35 20 2a 2f 20 22 48 61 6c 74 /* 5 */ "Halt
badc0 49 66 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a IfNull",. /*
badd0 20 20 20 36 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 6 */ "Halt",.
bade0 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 /* 7 */ "
badf0 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f Integer",. /
bae00 2a 20 20 20 38 20 2a 2f 20 22 49 6e 74 36 34 22 * 8 */ "Int64"
bae10 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f ,. /* 9 */
bae20 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 20 "String",.
bae30 2f 2a 20 20 31 30 20 2a 2f 20 22 4e 75 6c 6c 22 /* 10 */ "Null"
bae40 2c 0a 20 20 20 20 20 2f 2a 20 20 31 31 20 2a 2f ,. /* 11 */
bae50 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a "Blob",. /*
bae60 20 20 31 32 20 2a 2f 20 22 56 61 72 69 61 62 6c 12 */ "Variabl
bae70 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20 e",. /* 13
bae80 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20 */ "Move",.
bae90 2f 2a 20 20 31 34 20 2a 2f 20 22 43 6f 70 79 22 /* 14 */ "Copy"
baea0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f ,. /* 15 */
baeb0 20 22 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f "SCopy",. /
baec0 2a 20 20 31 36 20 2a 2f 20 22 52 65 73 75 6c 74 * 16 */ "Result
baed0 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 Row",. /* 1
baee0 37 20 2a 2f 20 22 43 6f 6c 6c 53 65 71 22 2c 0a 7 */ "CollSeq",.
baef0 20 20 20 20 20 2f 2a 20 20 31 38 20 2a 2f 20 22 /* 18 */ "
baf00 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 Function",.
baf10 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c /* 19 */ "Not",
baf20 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 . /* 20 */
baf30 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 2f "AddImm",. /
baf40 2a 20 20 32 31 20 2a 2f 20 22 4d 75 73 74 42 65 * 21 */ "MustBe
baf50 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 Int",. /* 2
baf60 32 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 2 */ "RealAffini
baf70 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33 ty",. /* 23
baf80 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e */ "Permutation
baf90 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a ",. /* 24 *
bafa0 2f 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20 20 / "Compare",.
bafb0 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 4a 75 6d /* 25 */ "Jum
bafc0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 p",. /* 26
bafd0 2a 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a */ "If",. /*
bafe0 20 20 32 37 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 27 */ "IfNot",
baff0 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a 2f 20 . /* 28 */
bb000 22 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f "Column",. /
bb010 2a 20 20 32 39 20 2a 2f 20 22 41 66 66 69 6e 69 * 29 */ "Affini
bb020 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 30 ty",. /* 30
bb030 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 */ "MakeRecord"
bb040 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31 20 2a 2f ,. /* 31 */
bb050 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f "Count",. /
bb060 2a 20 20 33 32 20 2a 2f 20 22 53 61 76 65 70 6f * 32 */ "Savepo
bb070 69 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 int",. /* 3
bb080 33 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 74 3 */ "AutoCommit
bb090 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a ",. /* 34 *
bb0a0 2f 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c / "Transaction",
bb0b0 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20 . /* 35 */
bb0c0 22 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 "ReadCookie",.
bb0d0 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 53 65 /* 36 */ "Se
bb0e0 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f tCookie",. /
bb0f0 2a 20 20 33 37 20 2a 2f 20 22 56 65 72 69 66 79 * 37 */ "Verify
bb100 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a Cookie",. /*
bb110 20 20 33 38 20 2a 2f 20 22 4f 70 65 6e 52 65 61 38 */ "OpenRea
bb120 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20 d",. /* 39
bb130 2a 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a */ "OpenWrite",.
bb140 20 20 20 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22 /* 40 */ "
bb150 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a OpenEphemeral",.
bb160 20 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 /* 41 */ "
bb170 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 OpenPseudo",.
bb180 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 43 6c 6f /* 42 */ "Clo
bb190 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 se",. /* 43
bb1a0 20 2a 2f 20 22 53 65 65 6b 4c 74 22 2c 0a 20 20 */ "SeekLt",.
bb1b0 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 53 65 /* 44 */ "Se
bb1c0 65 6b 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ekLe",. /*
bb1d0 34 35 20 2a 2f 20 22 53 65 65 6b 47 65 22 2c 0a 45 */ "SeekGe",.
bb1e0 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 /* 46 */ "
bb1f0 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a SeekGt",. /*
bb200 20 20 34 37 20 2a 2f 20 22 53 65 65 6b 22 2c 0a 47 */ "Seek",.
bb210 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 22 /* 48 */ "
bb220 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 NotFound",.
bb230 2f 2a 20 20 34 39 20 2a 2f 20 22 46 6f 75 6e 64 /* 49 */ "Found
bb240 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a ",. /* 50 *
bb250 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 / "IsUnique",.
bb260 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 4e 6f /* 51 */ "No
bb270 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f tExists",. /
bb280 2a 20 20 35 32 20 2a 2f 20 22 53 65 71 75 65 6e * 52 */ "Sequen
bb290 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 ce",. /* 53
bb2a0 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a */ "NewRowid",.
bb2b0 20 20 20 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 /* 54 */ "
bb2c0 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a Insert",. /*
bb2d0 20 20 35 35 20 2a 2f 20 22 49 6e 73 65 72 74 49 55 */ "InsertI
bb2e0 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 nt",. /* 56
bb2f0 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 */ "Delete",.
bb300 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20 22 52 65 /* 57 */ "Re
bb310 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 setCount",.
bb320 2f 2a 20 20 35 38 20 2a 2f 20 22 52 6f 77 4b 65 /* 58 */ "RowKe
bb330 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39 20 y",. /* 59
bb340 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c 0a 20 20 */ "RowData",.
bb350 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20 22 52 6f /* 60 */ "Ro
bb360 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 wid",. /* 6
bb370 31 20 2a 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a 1 */ "NullRow",.
bb380 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 /* 62 */ "
bb390 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Last",. /*
bb3a0 36 33 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 63 */ "Sort",.
bb3b0 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 52 65 /* 64 */ "Re
bb3c0 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 wind",. /*
bb3d0 36 35 20 2a 2f 20 22 50 72 65 76 22 2c 0a 20 20 65 */ "Prev",.
bb3e0 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e 65 /* 66 */ "Ne
bb3f0 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37 xt",. /* 67
bb400 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22 2c */ "IdxInsert",
bb410 0a 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20 . /* 68 */
bb420 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 "Or",. /* 6
bb430 39 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 9 */ "And",.
bb440 20 2f 2a 20 20 37 30 20 2a 2f 20 22 49 64 78 44 /* 70 */ "IdxD
bb450 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 elete",. /*
bb460 20 37 31 20 2a 2f 20 22 49 64 78 52 6f 77 69 64 71 */ "IdxRowid
bb470 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a ",. /* 72 *
bb480 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20 / "IdxLT",.
bb490 2f 2a 20 20 37 33 20 2a 2f 20 22 49 73 4e 75 6c /* 73 */ "IsNul
bb4a0 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 l",. /* 74
bb4b0 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 */ "NotNull",.
bb4c0 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20 22 4e 65 /* 75 */ "Ne
bb4d0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 36 20 2a ",. /* 76 *
bb4e0 2f 20 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Eq",. /*
bb4f0 20 37 37 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 77 */ "Gt",.
bb500 20 20 2f 2a 20 20 37 38 20 2a 2f 20 22 4c 65 22 /* 78 */ "Le"
bb510 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f ,. /* 79 */
bb520 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Lt",. /*
bb530 38 30 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 80 */ "Ge",.
bb540 20 2f 2a 20 20 38 31 20 2a 2f 20 22 49 64 78 47 /* 81 */ "IdxG
bb550 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 E",. /* 82
bb560 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 */ "BitAnd",.
bb570 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42 69 74 /* 83 */ "Bit
bb580 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 34 Or",. /* 84
bb590 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 22 2c */ "ShiftLeft",
bb5a0 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20 . /* 85 */
bb5b0 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a 20 20 "ShiftRight",.
bb5c0 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 41 64 /* 86 */ "Ad
bb5d0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20 d",. /* 87
bb5e0 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 0a 20 */ "Subtract",.
bb5f0 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 4d /* 88 */ "M
bb600 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f ultiply",. /
bb610 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69 64 65 * 89 */ "Divide
bb620 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a ",. /* 90 *
bb630 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 / "Remainder",.
bb640 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 43 /* 91 */ "C
bb650 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 oncat",. /*
bb660 20 39 32 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 92 */ "Destroy"
bb670 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f ,. /* 93 */
bb680 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 "BitNot",.
bb690 2f 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e /* 94 */ "Strin
bb6a0 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 g8",. /* 95
bb6b0 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20 */ "Clear",.
bb6c0 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 43 72 65 /* 96 */ "Cre
bb6d0 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 ateIndex",.
bb6e0 2f 2a 20 20 39 37 20 2a 2f 20 22 43 72 65 61 74 /* 97 */ "Creat
bb6f0 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a eTable",. /*
bb700 20 20 39 38 20 2a 2f 20 22 50 61 72 73 65 53 63 98 */ "ParseSc
bb710 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 hema",. /*
bb720 39 39 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 79 99 */ "LoadAnaly
bb730 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 sis",. /* 10
bb740 30 20 2a 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 0 */ "DropTable"
bb750 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f ,. /* 101 */
bb760 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 "DropIndex",.
bb770 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 44 72 /* 102 */ "Dr
bb780 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20 20 opTrigger",.
bb790 20 2f 2a 20 31 30 33 20 2a 2f 20 22 49 6e 74 65 /* 103 */ "Inte
bb7a0 67 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f grityCk",. /
bb7b0 2a 20 31 30 34 20 2a 2f 20 22 52 6f 77 53 65 74 * 104 */ "RowSet
bb7c0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 Add",. /* 10
bb7d0 35 20 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64 5 */ "RowSetRead
bb7e0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a ",. /* 106 *
bb7f0 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a / "RowSetTest",.
bb800 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22 /* 107 */ "
bb810 50 72 6f 67 72 61 6d 22 2c 0a 20 20 20 20 20 2f Program",. /
bb820 2a 20 31 30 38 20 2a 2f 20 22 50 61 72 61 6d 22 * 108 */ "Param"
bb830 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f ,. /* 109 */
bb840 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a 20 20 "FkCounter",.
bb850 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22 46 6b /* 110 */ "Fk
bb860 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a IfZero",. /*
bb870 20 31 31 31 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 111 */ "MemMax"
bb880 2c 0a 20 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f ,. /* 112 */
bb890 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f "IfPos",. /
bb8a0 2a 20 31 31 33 20 2a 2f 20 22 49 66 4e 65 67 22 * 113 */ "IfNeg"
bb8b0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a 2f ,. /* 114 */
bb8c0 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 "IfZero",.
bb8d0 2f 2a 20 31 31 35 20 2a 2f 20 22 41 67 67 53 74 /* 115 */ "AggSt
bb8e0 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 ep",. /* 116
bb8f0 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0a */ "AggFinal",.
bb900 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 /* 117 */ "
bb910 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a Vacuum",. /*
bb920 20 31 31 38 20 2a 2f 20 22 49 6e 63 72 56 61 63 118 */ "IncrVac
bb930 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 uum",. /* 11
bb940 39 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 9 */ "Expire",.
bb950 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 54 /* 120 */ "T
bb960 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 ableLock",.
bb970 2f 2a 20 31 32 31 20 2a 2f 20 22 56 42 65 67 69 /* 121 */ "VBegi
bb980 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20 n",. /* 122
bb990 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 */ "VCreate",.
bb9a0 20 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 56 44 /* 123 */ "VD
bb9b0 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a estroy",. /*
bb9c0 20 31 32 34 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 124 */ "VOpen",
bb9d0 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20 . /* 125 */
bb9e0 22 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 "VFilter",.
bb9f0 2f 2a 20 31 32 36 20 2a 2f 20 22 56 43 6f 6c 75 /* 126 */ "VColu
bba00 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 37 mn",. /* 127
bba10 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 20 */ "VNext",.
bba20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 56 52 65 /* 128 */ "VRe
bba30 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 name",. /* 1
bba40 32 39 20 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 29 */ "VUpdate",
bba50 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 . /* 130 */
bba60 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 "Real",. /*
bba70 31 33 31 20 2a 2f 20 22 50 61 67 65 63 6f 75 6e 131 */ "Pagecoun
bba80 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 20 t",. /* 132
bba90 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 20 20 20 */ "Trace",.
bbaa0 20 2f 2a 20 31 33 33 20 2a 2f 20 22 4e 6f 6f 70 /* 133 */ "Noop
bbab0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a ",. /* 134 *
bbac0 2f 20 22 45 78 70 6c 61 69 6e 22 2c 0a 20 20 20 / "Explain",.
bbad0 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 /* 135 */ "Not
bbae0 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 Used_135",.
bbaf0 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 /* 136 */ "NotUs
bbb00 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a ed_136",. /*
bbb10 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 137 */ "NotUsed
bbb20 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _137",. /* 1
bbb30 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 38 */ "NotUsed_1
bbb40 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 38",. /* 139
bbb50 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39 */ "NotUsed_139
bbb60 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a ",. /* 140 *
bbb70 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c / "NotUsed_140",
bbb80 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20 . /* 141 */
bbb90 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f "ToText",. /
bbba0 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62 * 142 */ "ToBlob
bbbb0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a ",. /* 143 *
bbbc0 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20 / "ToNumeric",.
bbbd0 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54 /* 144 */ "T
bbbe0 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 oInt",. /* 1
bbbf0 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 45 */ "ToReal",.
bbc00 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a };. return az
bbc10 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69 Name[i];.}.#endi
bbc20 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
bbc30 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 ** End of opcode
bbc40 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.c ************
bbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
bbc80 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 ** Begin file os
bbc90 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _os2.c *********
bbca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbcc0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65 */./*.** 2006 Fe
bbcd0 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 b 14.**.** The a
bbce0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
bbcf0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
bbd00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
bbd10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
bbd20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
bbd30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
bbd40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
bbd50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
bbd60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
bbd70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
bbd80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
bbd90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
bbda0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
bbdb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
bbdc0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
bbdd0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
bbde0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
bbdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe30 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
bbe40 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
bbe50 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 s code that is s
bbe60 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e pecific to OS/2.
bbe70 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 .*/...#if SQLITE
bbe80 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 _OS_OS2../*.** A
bbe90 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f Note About Memo
bbea0 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a ry Allocation:.*
bbeb0 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 *.** This driver
bbec0 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 uses malloc()/f
bbed0 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 ree() directly r
bbee0 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 ather than going
bbef0 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 through.** the
bbf00 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 SQLite-wrappers
bbf10 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
bbf20 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e /sqlite3_free().
bbf30 20 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 Those wrappers
bbf40 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 .** are designed
bbf50 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 for use on embe
bbf60 64 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 dded systems whe
bbf70 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 re memory is sca
bbf80 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f rce and.** mallo
bbf90 63 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 c failures happe
bbfa0 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f n frequently. O
bbfb0 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 S/2 does not typ
bbfc0 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a ically run on.**
bbfd0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
bbfe0 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 s, and when it d
bbff0 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 oes the develope
bc000 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 rs normally have
bc010 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c bigger.** probl
bc020 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f ems to worry abo
bc030 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 ut than running
bc040 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 out of memory.
bc050 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a So there is not.
bc060 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 ** a compelling
bc070 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 need to use the
bc080 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 wrappers..**.**
bc090 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 But there is a g
bc0a0 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f ood reason to no
bc0b0 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 t use the wrappe
bc0c0 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 rs. If we use t
bc0d0 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 he.** wrappers t
bc0e0 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 hen we will get
bc0f0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 simulated malloc
bc100 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 () failures with
bc110 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 in this.** drive
bc120 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 r. And that cau
bc130 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 ses all kinds of
bc140 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 problems for ou
bc150 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 r tests. We.**
bc160 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 could enhance SQ
bc170 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 Lite to deal wit
bc180 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c h simulated mall
bc190 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 oc failures with
bc1a0 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 in.** the OS dri
bc1b0 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 ver, but the cod
bc1c0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 e to deal with t
bc1d0 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 hose failure wou
bc1e0 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 ld not.** be exe
bc1f0 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 rcised on Linux
bc200 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 (which does not
bc210 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 need to malloc()
bc220 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a in the driver).
bc230 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 ** and so we wou
bc240 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c ld have difficul
bc250 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 ty writing cover
bc260 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 age tests for th
bc270 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 at.** code. Bet
bc280 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 ter to leave the
bc290 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 code out, we th
bc2a0 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ink..**.** The p
bc2b0 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 oint of this dis
bc2c0 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f cussion is as fo
bc2d0 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 llows: When cre
bc2e0 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f ating a new.** O
bc2f0 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 S layer for an e
bc300 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 mbedded system,
bc310 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 if you use this
bc320 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 file as an examp
bc330 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 le,.** avoid the
bc340 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 use of malloc()
bc350 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 /free(). Those
bc360 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b routines work ok
bc370 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b on OS/2.** desk
bc380 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 tops but not so
bc390 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 well in embedded
bc3a0 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a systems..*/../*
bc3b0 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 .** Macros used
bc3c0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 to determine whe
bc3d0 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 ther or not to u
bc3e0 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 se threads..*/.#
bc3f0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
bc400 45 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26 E_THREADSAFE) &&
bc410 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
bc420 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 FE.# define SQLI
bc430 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31 TE_OS2_THREADS 1
bc440 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
bc450 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 nclude code that
bc460 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
bc470 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a l os_*.c files.*
bc480 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bc490 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d * Include os_com
bc4a0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 mon.h in the mid
bc4b0 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 dle of os_os2.c
bc4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc4d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bc4e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
bc4f0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
bc500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc520 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
bc530 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
bc540 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
bc550 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
bc560 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
bc570 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
bc580 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
bc590 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
bc5a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
bc5b0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
bc5c0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
bc5d0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
bc5e0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
bc5f0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
bc600 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
bc610 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
bc620 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
bc630 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
bc640 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
bc650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc690 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
bc6a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
bc6b0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 macros and a li
bc6c0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 ttle bit of code
bc6d0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
bc6e0 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 to.** all of the
bc6f0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 platform-specif
bc700 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 ic files (os_*.c
bc710 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 ) and is #includ
bc720 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a ed into those.**
bc730 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 files..**.** Th
bc740 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 is file should b
bc750 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 e #included by t
bc760 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 he os_*.c files
bc770 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 only. It is not
bc780 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 a.** general pu
bc790 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c rpose header fil
bc7a0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f e..*/.#ifndef _O
bc7b0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
bc7c0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
bc7d0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
bc7e0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
bc7f0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
bc800 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
bc810 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
bc820 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
bc830 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
bc840 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
bc850 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
bc860 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
bc870 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
bc880 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
bc890 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
bc8a0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
bc8b0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
bc8c0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
bc8d0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
bc8e0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
bc8f0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
bc900 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
bc910 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
bc920 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
bc930 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
bc940 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
bc950 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
bc960 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
bc970 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bc980 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bc990 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
bc9a0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
bc9b0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
bc9c0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
bc9d0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
bc9e0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
bc9f0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
bca00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bca10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bca20 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
bca30 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
bca40 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
bca50 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
bca60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bca70 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
bca80 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
bca90 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
bcaa0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
bcab0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
bcac0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
bcad0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
bcae0 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
bcaf0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
bcb00 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
bcb10 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
bcb20 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bcb30 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
bcb40 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
bcb50 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
bcb60 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bcb70 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
bcb80 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
bcb90 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
bcba0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
bcbb0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
bcbc0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
bcbd0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
bcbe0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
bcbf0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
bcc00 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
bcc10 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
bcc20 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
bcc30 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
bcc40 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
bcc50 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
bcc60 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
bcc70 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
bcc80 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
bcc90 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
bcca0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
bccb0 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
bccc0 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
bccd0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
bcce0 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
bccf0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
bcd00 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
bcd10 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
bcd20 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
bcd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
bcd40 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
bcd50 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
bcd60 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
bcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
bcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
bcd90 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
bcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
bcdd0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
bcde0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
bcdf0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
bce00 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
bce10 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
bce20 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
bce30 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
bce40 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
bce50 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
bce60 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
bce70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bce80 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
bce90 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
bcea0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
bceb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bcec0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
bced0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
bcee0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
bcef0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
bcf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
bcf50 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
bcf60 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
bcf70 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
bcf80 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
bcf90 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
bcfa0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 class CPUs..*/.#
bcfb0 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 ifndef _HWTIME_H
bcfc0 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d _.#define _HWTIM
bcfd0 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 E_H_../*.** The
bcfe0 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
bcff0 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 e only works on
bd000 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f pentium-class (o
bd010 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 r newer) process
bd020 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 ors..** It uses
bd030 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 the RDTSC opcode
bd040 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 to read the cyc
bd050 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f le count value o
bd060 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f ut of the.** pro
bd070 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 cessor and retur
bd080 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 ns that value.
bd090 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
bd0a0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a for high-res.**
bd0b0 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 profiling..*/.#
bd0c0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
bd0d0 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 UC__) || defined
bd0e0 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c (_MSC_VER)) && \
bd0f0 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 . (defined(
bd100 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 i386) || defined
bd110 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 (__i386__) || de
bd120 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a fined(_M_IX86)).
bd130 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f . #if defined(_
bd140 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e _GNUC__).. __in
bd150 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
bd160 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
bd170 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 me(void){. u
bd180 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 nsigned int lo,
bd190 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f hi;. __asm__
bd1a0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
bd1b0 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c rdtsc" : "=a" (l
bd1c0 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a o), "=d" (hi));.
bd1d0 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c return (sql
bd1e0 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c ite_uint64)hi <<
bd1f0 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 32 | lo;. }..
bd200 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f #elif defined(_
bd210 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 MSC_VER).. __de
bd220 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f clspec(naked) __
bd230 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 inline sqlite_ui
bd240 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c nt64 __cdecl sql
bd250 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
bd260 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 {. __asm {.
bd270 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 rdtsc.
bd280 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b ret ;
bd290 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 return value at
bd2a0 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a EDX:EAX. }.
bd2b0 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 }.. #endif..#
bd2c0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
bd2d0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
bd2e0 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a ed(__x86_64__)).
bd2f0 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
bd300 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
bd310 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
bd320 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
bd330 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 long val;.
bd340 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 __asm__ __volati
bd350 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 le__ ("rdtsc" :
bd360 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 "=A" (val));.
bd370 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 return val;.
bd380 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 }. .#elif (defi
bd390 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
bd3a0 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f defined(__ppc__
bd3b0 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
bd3c0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
bd3d0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
bd3e0 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
bd3f0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 ed long long ret
bd400 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 val;. unsig
bd410 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 ned long junk;.
bd420 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
bd430 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a olatile__ ("\n\.
bd440 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 1:
bd450 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a mftbu %1\n\.
bd460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd470 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c mftb %L0\n\
bd480 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bd490 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c mftbu %0\n\
bd4a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bd4b0 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 cmpw %0,%1
bd4c0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
bd4d0 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 bne 1b
bd4e0 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
bd4f0 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 : "=r" (retv
bd500 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 al), "=r" (junk)
bd510 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
bd520 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c retval;. }..#el
bd530 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 se.. #error Nee
bd540 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e d implementation
bd550 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d of sqlite3Hwtim
bd560 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 e() for your pla
bd570 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a tform... /*. *
bd580 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 * To compile wit
bd590 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e hout implementin
bd5a0 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 g sqlite3Hwtime(
bd5b0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
bd5c0 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 orm,. ** you ca
bd5d0 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f n remove the abo
bd5e0 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 ve #error and us
bd5f0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a e the following.
bd600 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 ** stub functi
bd610 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f on. You will lo
bd620 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 se timing suppor
bd630 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 t for many. **
bd640 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 of the debugging
bd650 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 and testing uti
bd660 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 lities, but it s
bd670 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 hould at. ** le
bd680 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 ast compile and
bd690 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 run.. */.SQLITE
bd6a0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 _PRIVATE sqlit
bd6b0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
bd6c0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 Hwtime(void){ re
bd6d0 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 turn ((sqlite_ui
bd6e0 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 nt64)0); }..#end
bd6f0 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 if..#endif /* !d
bd700 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 efined(_HWTIME_H
bd710 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
bd720 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 ****** End of hw
bd730 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a time.h *********
bd740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd760 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
bd770 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
bd780 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
bd790 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e off in os_common
bd7a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bd7b0 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 *****/..static s
bd7c0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 qlite_uint64 g_s
bd7d0 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c tart;.static sql
bd7e0 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 ite_uint64 g_ela
bd7f0 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 psed;.#define TI
bd800 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 MER_START
bd810 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 g_start=sqlite3H
bd820 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 wtime().#define
bd830 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 TIMER_END
bd840 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 g_elapsed=sqli
bd850 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 te3Hwtime()-g_st
bd860 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 art.#define TIME
bd870 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f R_ELAPSED g_
bd880 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 elapsed.#else.#d
bd890 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 efine TIMER_STAR
bd8a0 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f T.#define TIMER_
bd8b0 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 END.#define TIME
bd8c0 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 R_ELAPSED ((
bd8d0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 sqlite_uint64)0)
bd8e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
bd8f0 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 f we compile wit
bd900 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 h the SQLITE_TES
bd910 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 T macro set, the
bd920 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
bd930 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 block.** of code
bd940 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 will give us th
bd950 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d e ability to sim
bd960 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f ulate a disk I/O
bd970 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a error. This.**
bd980 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
bd990 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 ting the I/O rec
bd9a0 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a overy logic..*/.
bd9b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
bd9c0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
bd9d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
bd9e0 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 or_hit = 0;
bd9f0 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
bda00 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 number of I/O Er
bda10 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
bda20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
bda30 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 o_error_hardhit
bda40 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e = 0; /* N
bda50 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e umber of non-ben
bda60 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 ign errors */.SQ
bda70 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bda80 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
bda90 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 nding = 0;
bdaa0 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 /* Count down
bdab0 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 to first I/O err
bdac0 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 or */.SQLITE_API
bdad0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
bdae0 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 error_persist =
bdaf0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 0; /* Tru
bdb00 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 e if I/O errors
bdb10 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 persist */.SQLIT
bdb20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bdb30 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
bdb40 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f n = 0; /
bdb50 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 * True if errors
bdb60 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 are benign */.S
bdb70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bdb80 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
bdb90 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 ending = 0;.SQLI
bdba0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bdbb0 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b e3_diskfull = 0;
bdbc0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
bdbd0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
bdbe0 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 ) sqlite3_io_err
bdbf0 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 or_benign=(X).#d
bdc00 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
bdc10 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 Error(CODE) \.
bdc20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f if( (sqlite3_io
bdc30 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 _error_persist &
bdc40 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 & sqlite3_io_err
bdc50 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 or_hit) \.
bdc60 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 || sqlite3_io_e
bdc70 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d rror_pending-- =
bdc80 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 = 1 ) \.
bdc90 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 { local_i
bdca0 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a oerr(); CODE; }.
bdcb0 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 static void loca
bdcc0 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 l_ioerr(){. IOT
bdcd0 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 RACE(("IOERR\n")
bdce0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f );. sqlite3_io_
bdcf0 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 error_hit++;. i
bdd00 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( !sqlite3_io_e
bdd10 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 rror_benign ) sq
bdd20 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bdd30 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 ardhit++;.}.#def
bdd40 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ine SimulateDisk
bdd50 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 fullError(CODE)
bdd60 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 \. if( sqlite3
bdd70 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
bdd80 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 g ){ \. if(
bdd90 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
bdda0 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b _pending == 1 ){
bddb0 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f \. local_
bddc0 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 ioerr(); \.
bddd0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
bdde0 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 ll = 1; \.
bddf0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bde00 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 r_hit = 1; \.
bde10 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 CODE; \.
bde20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 }else{ \.
bde30 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
bde40 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 l_pending--; \.
bde50 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c } \. }.#el
bde60 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c se.#define Simul
bde70 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
bde80 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (X).#define Simu
bde90 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 lateIOError(A).#
bdea0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
bdeb0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a iskfullError(A).
bdec0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 #endif../*.** Wh
bded0 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 en testing, keep
bdee0 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 a count of the
bdef0 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 number of open f
bdf00 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 iles..*/.#ifdef
bdf10 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
bdf20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bdf30 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 e3_open_file_cou
bdf40 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 nt = 0;.#define
bdf50 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 OpenCounter(X)
bdf60 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c sqlite3_open_fil
bdf70 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c e_count+=(X).#el
bdf80 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 se.#define OpenC
bdf90 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 ounter(X).#endif
bdfa0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
bdfb0 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ined(_OS_COMMON_
bdfc0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
bdfd0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
bdfe0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
bdff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be010 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
be020 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
be030 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
be040 20 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 off in os_os2.c
be050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
be060 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ******/../*.** T
be070 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 63 he os2File struc
be080 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 ture is subclass
be090 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
be0a0 20 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68 specific for th
be0b0 65 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 e OS/2.** protab
be0c0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a ility layer..*/.
be0d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 6f typedef struct o
be0e0 73 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a s2File os2File;.
be0f0 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 7b struct os2File {
be100 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
be110 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
be120 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 thod; /* Always
be130 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
be140 20 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 20 */. HFILE h;
be150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be160 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 /* Handle for ac
be170 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65 cessing the file
be180 20 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74 68 */. char* path
be190 54 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20 20 ToDel;
be1a0 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 /* Name of file
be1b0 74 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f to delete on clo
be1c0 73 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 se, NULL if not
be1d0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
be1e0 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f ar locktype; /
be1f0 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 * Type of lock c
be200 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e urrently held on
be210 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b this file */.};
be220 0a 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 ..#define LOCK_T
be230 49 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68 IMEOUT 10L /* th
be240 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e e default lockin
be250 67 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a g timeout */../*
be260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
be2b0 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f The next group o
be2c0 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 f routines imple
be2d0 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 ment the I/O met
be2e0 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a hods specified.*
be2f0 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 * by the sqlite3
be300 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
be310 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ct..************
be320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be360 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 **/../*.** Close
be370 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
be380 69 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 ic int os2Close(
be390 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
be3a0 64 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 d ){. APIRET rc
be3b0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f = NO_ERROR;. o
be3c0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 s2File *pFile;.
be3d0 20 69 66 28 20 69 64 20 26 26 20 28 70 46 69 6c if( id && (pFil
be3e0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
be3f0 29 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 ) != 0 ){. OS
be400 54 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20 25 TRACE2( "CLOSE %
be410 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 d\n", pFile->h )
be420 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43 6c ;. rc = DosCl
be430 6f 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b ose( pFile->h );
be440 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
be450 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
be460 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
be470 61 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c athToDel != NULL
be480 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44 ){. rc = D
be490 6f 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28 osForceDelete( (
be4a0 50 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 PSZ)pFile->pathT
be4b0 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 oDel );. fr
be4c0 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 ee( pFile->pathT
be4d0 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 oDel );. pF
be4e0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d ile->pathToDel =
be4f0 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 NULL;. }.
be500 20 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 id = 0;. Ope
be510 6e 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a nCounter( -1 );.
be520 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
be530 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 == NO_ERROR ? S
be540 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
be550 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a E_IOERR;.}../*.*
be560 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
be570 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 a file into a b
be580 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 uffer. Return S
be590 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a QLITE_OK if all.
be5a0 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 ** bytes were re
be5b0 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ad successfully
be5c0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 and SQLITE_IOERR
be5d0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
be5e0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 s.** wrong..*/.s
be5f0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65 61 tatic int os2Rea
be600 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c d(. sqlite3_fil
be610 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 e *id,
be620 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
be630 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 read from */. v
be640 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
be650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be660 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 * Write content
be670 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 into this buffer
be680 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 */. int amt,
be690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be6a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
be6b0 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 of bytes to read
be6c0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
be6d0 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 t64 offset
be6e0 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 /* Begin r
be6f0 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f eading at this o
be700 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c ffset */.){. UL
be710 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e ONG fileLocation
be720 20 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 = 0L;. ULONG g
be730 6f 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 ot;. os2File *p
be740 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
be750 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 )id;. assert( i
be760 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
be770 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
be780 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 n SQLITE_IOERR_R
be790 45 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 EAD );. OSTRACE
be7a0 33 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3( "READ %d lock
be7b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
be7c0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
be7d0 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 e );. if( DosSe
be7e0 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e tFilePtr(pFile->
be7f0 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f h, offset, FILE_
be800 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 BEGIN, &fileLoca
be810 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f tion) != NO_ERRO
be820 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 R ){. return
be830 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
be840 7d 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64 28 }. if( DosRead(
be850 20 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c pFile->h, pBuf,
be860 20 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d 20 amt, &got ) !=
be870 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
be880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
be890 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 ERR_READ;. }.
be8a0 69 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e if( got == (ULON
be8b0 47 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74 75 G)amt ). retu
be8c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
be8d0 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e else {. /* Un
be8e0 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 read portions of
be8f0 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 the input buffe
be900 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 r must be zero-f
be910 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d illed */. mem
be920 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 set(&((char*)pBu
be930 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d f)[got], 0, amt-
be940 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e got);. return
be950 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 SQLITE_IOERR_SH
be960 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a ORT_READ;. }.}.
be970 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
be980 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 a from a buffer
be990 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 into a file. Re
be9a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
be9b0 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 n success.** or
be9c0 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 some other error
be9d0 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 code on failure
be9e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
be9f0 6f 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c 69 os2Write(. sqli
bea00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
bea10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
bea20 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 ile to write int
bea30 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 o */. const voi
bea40 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 d *pBuf,
bea50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79 /* The by
bea60 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 tes to be writte
bea70 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 n */. int amt,
bea80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bea90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
beaa0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 of bytes to wri
beab0 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f te */. sqlite3_
beac0 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 int64 offset
bead0 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
beae0 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 t into the file
beaf0 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 to begin writing
beb00 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e at */.){. ULON
beb10 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d G fileLocation =
beb20 20 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 63 0L;. APIRET rc
beb30 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 = NO_ERROR;. U
beb40 4c 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 LONG wrote;. os
beb50 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
beb60 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 os2File*)id;. a
beb70 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
beb80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
beb90 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
beba0 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b 0a _IOERR_WRITE );.
bebb0 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 SimulateDiskfu
bebc0 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 llError( return
bebd0 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 SQLITE_FULL );.
bebe0 20 4f 53 54 52 41 43 45 33 28 20 22 57 52 49 54 OSTRACE3( "WRIT
bebf0 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c E %d lock=%d\n",
bec00 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
bec10 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
bec20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 if( DosSetFilePt
bec30 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 r(pFile->h, offs
bec40 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 et, FILE_BEGIN,
bec50 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 &fileLocation) !
bec60 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bec70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
bec80 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 IOERR;. }. ass
bec90 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 ert( amt>0 );.
beca0 77 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 while( amt > 0 &
becb0 26 0a 20 20 20 20 20 20 20 20 20 28 20 72 63 20 &. ( rc
becc0 3d 20 44 6f 73 57 72 69 74 65 28 20 70 46 69 6c = DosWrite( pFil
becd0 65 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75 e->h, (PVOID)pBu
bece0 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 f, amt, &wrote )
becf0 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 ) == NO_ERROR &
bed00 26 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74 65 &. wrote
bed10 20 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d > 0. ){. am
bed20 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t -= wrote;.
bed30 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 pBuf = &((char*)
bed40 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 pBuf)[wrote];.
bed50 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72 63 }.. return ( rc
bed60 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 != NO_ERROR ||
bed70 61 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 amt > (int)wrote
bed80 20 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c ) ? SQLITE_FULL
bed90 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a : SQLITE_OK;.}.
beda0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
bedb0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 an open file to
bedc0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 a specified size
bedd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bede0 73 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c 69 s2Truncate( sqli
bedf0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
bee00 34 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50 49 4 nByte ){. API
bee10 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f RET rc = NO_ERRO
bee20 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 R;. os2File *pF
bee30 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 ile = (os2File*)
bee40 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 id;. OSTRACE3(
bee50 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c "TRUNCATE %d %ll
bee60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
bee70 6e 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c nByte );. Simul
bee80 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
bee90 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
beea0 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 TRUNCATE );. rc
beeb0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a = DosSetFileSiz
beec0 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 e( pFile->h, nBy
beed0 74 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 te );. return r
beee0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c == NO_ERROR ?
beef0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
bef00 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
bef10 45 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c E;.}..#ifdef SQL
bef20 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 ITE_TEST./*.** C
bef30 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 ount the number
bef40 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 of fullsyncs and
bef50 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 normal syncs.
bef60 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
bef70 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e test.** that syn
bef80 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 cs and fullsyncs
bef90 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 are occuring at
befa0 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 the right times
befb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
befc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 int sqlite3_sync
befd0 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 _count = 0;.SQLI
befe0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
beff0 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
bf000 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
bf010 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 *.** Make sure a
bf020 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 ll writes to a p
bf030 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 articular file a
bf040 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 re committed to
bf050 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 disk..*/.static
bf060 69 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71 6c int os2Sync( sql
bf070 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
bf080 6e 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73 nt flags ){. os
bf090 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
bf0a0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f os2File*)id;. O
bf0b0 53 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 25 STRACE3( "SYNC %
bf0c0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
bf0d0 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
bf0e0 6f 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64 65 ocktype );.#ifde
bf0f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
bf100 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
bf110 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 TE_SYNC_FULL){.
bf120 20 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 sqlite3_fulls
bf130 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d ync_count++;. }
bf140 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f . sqlite3_sync_
bf150 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
bf160 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 /* If we compi
bf170 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c led with the SQL
bf180 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 ITE_NO_SYNC flag
bf190 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 , then syncing i
bf1a0 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 s a. ** no-op.
bf1b0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
bf1c0 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 E_NO_SYNC. UNUS
bf1d0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 46 69 ED_PARAMETER(pFi
bf1e0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 le);. return SQ
bf1f0 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 LITE_OK;.#else.
bf200 20 72 65 74 75 72 6e 20 44 6f 73 52 65 73 65 74 return DosReset
bf210 42 75 66 66 65 72 28 20 70 46 69 6c 65 2d 3e 68 Buffer( pFile->h
bf220 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f ) == NO_ERROR ?
bf230 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
bf240 49 54 45 5f 49 4f 45 52 52 3b 0a 23 65 6e 64 69 ITE_IOERR;.#endi
bf250 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 f.}../*.** Deter
bf260 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74 mine the current
bf270 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 size of a file
bf280 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 in bytes.*/.stat
bf290 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 53 69 ic int os2FileSi
bf2a0 7a 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ze( sqlite3_file
bf2b0 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e *id, sqlite3_in
bf2c0 74 36 34 20 2a 70 53 69 7a 65 20 29 7b 0a 20 20 t64 *pSize ){.
bf2d0 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
bf2e0 52 52 4f 52 3b 0a 20 20 46 49 4c 45 53 54 41 54 RROR;. FILESTAT
bf2f0 55 53 33 20 66 73 74 73 33 46 69 6c 65 49 6e 66 US3 fsts3FileInf
bf300 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 26 66 73 74 o;. memset(&fst
bf310 73 33 46 69 6c 65 49 6e 66 6f 2c 20 30 2c 20 73 s3FileInfo, 0, s
bf320 69 7a 65 6f 66 28 66 73 74 73 33 46 69 6c 65 49 izeof(fsts3FileI
bf330 6e 66 6f 29 29 3b 0a 20 20 61 73 73 65 72 74 28 nfo));. assert(
bf340 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 id!=0 );. Simu
bf350 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
bf360 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
bf370 5f 46 53 54 41 54 20 29 3b 0a 20 20 72 63 20 3d _FSTAT );. rc =
bf380 20 44 6f 73 51 75 65 72 79 46 69 6c 65 49 6e 66 DosQueryFileInf
bf390 6f 28 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 o( ((os2File*)id
bf3a0 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 41 4e 44 41 )->h, FIL_STANDA
bf3b0 52 44 2c 20 26 66 73 74 73 33 46 69 6c 65 49 6e RD, &fsts3FileIn
bf3c0 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 fo, sizeof(FILES
bf3d0 54 41 54 55 53 33 29 20 29 3b 0a 20 20 69 66 28 TATUS3) );. if(
bf3e0 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc == NO_ERROR
bf3f0 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 ){. *pSize =
bf400 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2e 63 62 fsts3FileInfo.cb
bf410 46 69 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 6e File;. return
bf420 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
bf430 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
bf440 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 SQLITE_IOERR_FST
bf450 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a AT;. }.}../*.**
bf460 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 65 Acquire a reade
bf470 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 r lock..*/.stati
bf480 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 c int getReadLoc
bf490 6b 28 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c k( os2File *pFil
bf4a0 65 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 e ){. FILELOCK
bf4b0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
bf4c0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
bf4d0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b a;. APIRET res;
bf4e0 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
bf4f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
bf500 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
bf510 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
bf520 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
bf530 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 kArea));. LockA
bf540 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 rea.lOffset = SH
bf550 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 4c 6f ARED_FIRST;. Lo
bf560 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
bf570 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 55 SHARED_SIZE;. U
bf580 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
bf590 74 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b t = 0L;. Unlock
bf5a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
bf5b0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 ;. res = DosSet
bf5c0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 FileLocks( pFile
bf5d0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 ->h, &UnlockArea
bf5e0 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 , &LockArea, LOC
bf5f0 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b K_TIMEOUT, 1L );
bf600 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 47 45 . OSTRACE3( "GE
bf610 54 52 45 41 44 4c 4f 43 4b 20 25 64 20 72 65 73 TREADLOCK %d res
bf620 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
bf630 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75 72 , res );. retur
bf640 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n res;.}../*.**
bf650 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a Undo a readlock.
bf660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
bf670 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73 lockReadLock( os
bf680 32 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 2File *id ){. F
bf690 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 ILELOCK LockAre
bf6a0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 a,. U
bf6b0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 nlockArea;. API
bf6c0 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 RET res;. memse
bf6d0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 t(&LockArea, 0,
bf6e0 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 sizeof(LockArea)
bf6f0 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c );. memset(&Unl
bf700 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
bf710 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
bf720 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 . LockArea.lOff
bf730 73 65 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b set = 0L;. Lock
bf740 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
bf750 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c ;. UnlockArea.l
bf760 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
bf770 46 49 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41 FIRST;. UnlockA
bf780 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 rea.lRange = SHA
bf790 52 45 44 5f 53 49 5a 45 3b 0a 20 20 72 65 73 20 RED_SIZE;. res
bf7a0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
bf7b0 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63 s( id->h, &Unloc
bf7c0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
bf7d0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
bf7e0 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 1L );. OSTRACE3
bf7f0 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 4f ( "UNLOCK-READLO
bf800 43 4b 20 66 69 6c 65 20 68 61 6e 64 6c 65 3d 25 CK file handle=%
bf810 64 20 72 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 64 d res=%d?\n", id
bf820 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 ->h, res );. re
bf830 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
bf840 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
bf850 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
bf860 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
bf870 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
bf880 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
bf890 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
bf8a0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
bf8b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
bf8c0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
bf8d0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
bf8e0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
bf8f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
bf900 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
bf910 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
bf920 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
bf930 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
bf940 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
bf950 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
bf960 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
bf970 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
bf980 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
bf990 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
bf9a0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
bf9b0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
bf9c0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
bf9d0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
bf9e0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
bf9f0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
bfa00 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
bfa10 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
bfa20 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
bfa30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
bfa40 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
bfa50 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
bfa60 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
bfa70 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
bfa80 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
bfa90 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
bfaa0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
bfab0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
bfac0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
bfad0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
bfae0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
bfaf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
bfb00 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
bfb10 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 ease a lock. Th
bfb20 65 20 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f e os2Unlock() ro
bfb30 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 utine.** erases
bfb40 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 all locks at onc
bfb50 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 e and returns us
bfb60 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 immediately to
bfb70 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e locking level 0.
bfb80 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
bfb90 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 ssible to lower
bfba0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
bfbb0 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 l one step at a
bfbc0 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 time. You.** mu
bfbd0 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 st go straight t
bfbe0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 o locking level
bfbf0 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0..*/.static int
bfc00 20 6f 73 32 4c 6f 63 6b 28 20 73 71 6c 69 74 65 os2Lock( sqlite
bfc10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
bfc20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e locktype ){. in
bfc30 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
bfc40 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 ; /* Retur
bfc50 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 n code from subr
bfc60 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 41 50 49 outines */. API
bfc70 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 RET res = NO_ERR
bfc80 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 OR; /* Result
bfc90 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c 6f 63 6b of an OS/2 lock
bfca0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e call */. int n
bfcb0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 ewLocktype;
bfcc0 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e /* Set pFile->
bfcd0 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 locktype to this
bfce0 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 value before ex
bfcf0 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 iting */. int g
bfd00 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
bfd10 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 0;/* True if we
bfd20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 acquired a PENDI
bfd30 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d NG lock this tim
bfd40 65 20 2a 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 e */. FILELOCK
bfd50 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
bfd60 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
bfd70 61 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 a;. os2File *pF
bfd80 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 ile = (os2File*)
bfd90 69 64 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f id;. memset(&Lo
bfda0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f ckArea, 0, sizeo
bfdb0 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 f(LockArea));.
bfdc0 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 memset(&UnlockAr
bfdd0 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e ea, 0, sizeof(Un
bfde0 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 lockArea));. as
bfdf0 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 sert( pFile!=0 )
bfe00 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c ;. OSTRACE4( "L
bfe10 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 OCK %d %d was %d
bfe20 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c \n", pFile->h, l
bfe30 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
bfe40 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 0a 20 20 2f locktype );.. /
bfe50 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c * If there is al
bfe60 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 ready a lock of
bfe70 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 this type or mor
bfe80 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e e restrictive on
bfe90 20 74 68 65 0a 20 20 2a 2a 20 6f 73 32 46 69 6c the. ** os2Fil
bfea0 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 e, do nothing. D
bfeb0 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 on't use the end
bfec0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 _lock: exit path
bfed0 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 , as. ** sqlite
bfee0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
bfef0 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c hasn't been call
bff00 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 ed yet.. */. i
bff10 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
bff20 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe>=locktype ){.
bff30 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c OSTRACE3( "L
bff40 4f 43 4b 20 25 64 20 25 64 20 6f 6b 20 28 61 6c OCK %d %d ok (al
bff50 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 ready held)\n",
bff60 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
bff70 70 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e pe );. return
bff80 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
bff90 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
bffa0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 the locking sequ
bffb0 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a ence is correct.
bffc0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
bffd0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d File->locktype!=
bffe0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 NO_LOCK || lockt
bfff0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
c0000 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
c0010 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f cktype!=PENDING_
c0020 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
c0030 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 ( locktype!=RESE
c0040 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 RVED_LOCK || pFi
c0050 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 le->locktype==SH
c0060 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 ARED_LOCK );..
c0070 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 /* Lock the PEND
c0080 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 ING_LOCK byte if
c0090 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 we need to acqu
c00a0 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ire a PENDING lo
c00b0 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 ck or. ** a SHA
c00c0 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 RED lock. If we
c00d0 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 are acquiring a
c00e0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 SHARED lock, th
c00f0 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 e acquisition of
c0100 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e . ** the PENDIN
c0110 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 G_LOCK byte is t
c0120 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 emporary.. */.
c0130 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 newLocktype = p
c0140 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a File->locktype;.
c0150 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
c0160 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a 20 ktype==NO_LOCK.
c0170 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 || (locktyp
c0180 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
c0190 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K && pFile->lock
c01a0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
c01b0 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 4c 6f OCK). ){. Lo
c01c0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c01d0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 PENDING_BYTE;.
c01e0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
c01f0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c ge = 1L;. Unl
c0200 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0210 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
c0220 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c0230 3b 0a 0a 20 20 20 20 2f 2a 20 77 61 69 74 20 6c ;.. /* wait l
c0240 6f 6e 67 65 72 20 74 68 61 6e 20 4c 4f 43 4b 5f onger than LOCK_
c0250 54 49 4d 45 4f 55 54 20 68 65 72 65 20 6e 6f 74 TIMEOUT here not
c0260 20 74 6f 20 68 61 76 65 20 74 6f 20 74 72 79 20 to have to try
c0270 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 2a multiple times *
c0280 2f 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 /. res = DosS
c0290 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
c02a0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
c02b0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 31 ea, &LockArea, 1
c02c0 30 30 4c 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 00L, 0L );. i
c02d0 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
c02e0 4f 52 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 50 OR ){. gotP
c02f0 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b 0a endingLock = 1;.
c0300 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 OSTRACE3(
c0310 22 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 "LOCK %d pending
c0320 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 65 lock boolean se
c0330 74 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 t. res=%d\n", p
c0340 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
c0350 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
c0360 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 Acquire a shared
c0370 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
c0380 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
c0390 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d D_LOCK && res ==
c03a0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c03b0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
c03c0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
c03d0 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 K );. res = g
c03e0 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 etReadLock(pFile
c03f0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d );. if( res =
c0400 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c0410 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
c0420 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
c0430 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 }. OSTRACE
c0440 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 3( "LOCK %d acqu
c0450 69 72 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e ire shared lock.
c0460 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c0470 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d e->h, res );. }
c0480 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
c0490 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 RESERVED lock.
c04a0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
c04b0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
c04c0 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 K && res == NO_E
c04d0 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 RROR ){. asse
c04e0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
c04f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
c0500 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 );. LockArea
c0510 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 .lOffset = RESER
c0520 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f VED_BYTE;. Lo
c0530 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c0540 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 1L;. UnlockAr
c0550 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
c0560 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c0570 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 lRange = 0L;.
c0580 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c res = DosSetFil
c0590 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
c05a0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c05b0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c05c0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
c05d0 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f if( res == NO_
c05e0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e ERROR ){. n
c05f0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 ewLocktype = RES
c0600 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 ERVED_LOCK;.
c0610 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 }. OSTRACE3(
c0620 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 "LOCK %d acquire
c0630 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 reserved lock.
c0640 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 res=%d\n", pFile
c0650 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a ->h, res );. }.
c0660 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 . /* Acquire a
c0670 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a PENDING lock. *
c0680 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
c0690 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
c06a0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 && res == NO_ER
c06b0 52 4f 52 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f ROR ){. newLo
c06c0 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 cktype = PENDING
c06d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 _LOCK;. gotPe
c06e0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 ndingLock = 0;.
c06f0 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4c 4f OSTRACE2( "LO
c0700 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 70 65 CK %d acquire pe
c0710 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e 64 nding lock. pend
c0720 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e ing lock boolean
c0730 20 75 6e 73 65 74 2e 5c 6e 22 2c 20 70 46 69 6c unset.\n", pFil
c0740 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f e->h );. }.. /
c0750 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 * Acquire an EXC
c0760 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f LUSIVE lock. */
c0770 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
c0780 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
c0790 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 && res == NO_ERR
c07a0 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OR ){. assert
c07b0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
c07c0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e>=SHARED_LOCK )
c07d0 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f ;. res = unlo
c07e0 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 ckReadLock(pFile
c07f0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 );. OSTRACE2(
c0800 20 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 "unreadlock = %
c0810 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 d\n", res );.
c0820 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c0830 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
c0840 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c0850 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 Range = SHARED_S
c0860 49 5a 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 IZE;. UnlockA
c0870 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
c0880 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c0890 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
c08a0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
c08b0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e leLocks( pFile->
c08c0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
c08d0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
c08e0 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 TIMEOUT, 0L );.
c08f0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f if( res == NO
c0900 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
c0910 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 newLocktype = EX
c0920 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 CLUSIVE_LOCK;.
c0930 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f }else{. O
c0940 53 54 52 41 43 45 32 28 20 22 4f 53 2f 32 20 65 STRACE2( "OS/2 e
c0950 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e rror-code = %d\n
c0960 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 20 20 ", res );.
c0970 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
c0980 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 e);. }. OS
c0990 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
c09a0 20 61 63 71 75 69 72 65 20 65 78 63 6c 75 73 69 acquire exclusi
c09b0 76 65 20 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 64 ve lock. res=%d
c09c0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
c09d0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
c09e0 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e If we are holdin
c09f0 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b g a PENDING lock
c0a00 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62 that ought to b
c0a10 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e e released, then
c0a20 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 . ** release it
c0a30 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 now.. */. if(
c0a40 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
c0a50 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 && locktype==SHA
c0a60 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
c0a70 69 6e 74 20 72 3b 0a 20 20 20 20 4c 6f 63 6b 41 int r;. LockA
c0a80 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
c0a90 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c0aa0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 Range = 0L;.
c0ab0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c0ac0 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 et = PENDING_BYT
c0ad0 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 E;. UnlockAre
c0ae0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 a.lRange = 1L;.
c0af0 20 20 20 72 20 3d 20 44 6f 73 53 65 74 46 69 6c r = DosSetFil
c0b00 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
c0b10 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c0b20 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c0b30 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
c0b40 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 OSTRACE3( "LOC
c0b50 4b 20 25 64 20 75 6e 6c 6f 63 6b 69 6e 67 20 70 K %d unlocking p
c0b60 65 6e 64 69 6e 67 2f 69 73 20 73 68 61 72 65 64 ending/is shared
c0b70 2e 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 . r=%d\n", pFile
c0b80 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a 20 ->h, r );. }..
c0b90 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 /* Update the s
c0ba0 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b tate of the lock
c0bb0 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 has held in the
c0bc0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
c0bd0 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 then. ** retur
c0be0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
c0bf0 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 e result code..
c0c00 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 3d 3d */. if( res ==
c0c10 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c0c20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
c0c30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 . }else{. OS
c0c40 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 46 41 TRACE4( "LOCK FA
c0c50 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 ILED %d trying f
c0c60 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 or %d but got %d
c0c70 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 \n", pFile->h,.
c0c80 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 loc
c0c90 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 ktype, newLockty
c0ca0 70 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 pe );. rc = S
c0cb0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
c0cc0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
c0cd0 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b e = newLocktype;
c0ce0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f . OSTRACE3( "LO
c0cf0 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c CK %d now %d\n",
c0d00 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
c0d10 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
c0d20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
c0d30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
c0d40 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
c0d50 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
c0d60 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
c0d70 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
c0d80 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
c0d90 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
c0da0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
c0db0 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a s held, return.*
c0dc0 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 * non-zero, othe
c0dd0 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 rwise zero..*/.s
c0de0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 68 65 tatic int os2Che
c0df0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 20 ckReservedLock(
c0e00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c0e10 2c 20 69 6e 74 20 2a 70 4f 75 74 20 29 7b 0a 20 , int *pOut ){.
c0e20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 6f 73 int r = 0;. os
c0e30 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
c0e40 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 os2File*)id;. a
c0e50 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 ssert( pFile!=0
c0e60 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
c0e70 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 locktype>=RESERV
c0e80 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
c0e90 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 = 1;. OSTRAC
c0ea0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 E3( "TEST WR-LOC
c0eb0 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c K %d %d (local)\
c0ec0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 n", pFile->h, r
c0ed0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
c0ee0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
c0ef0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
c0f00 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 UnlockArea;.
c0f10 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
c0f20 5f 45 52 52 4f 52 3b 0a 20 20 20 20 6d 65 6d 73 _ERROR;. mems
c0f30 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
c0f40 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
c0f50 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 ));. memset(&
c0f60 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 UnlockArea, 0, s
c0f70 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 izeof(UnlockArea
c0f80 29 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 ));. LockArea
c0f90 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 .lOffset = RESER
c0fa0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f VED_BYTE;. Lo
c0fb0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c0fc0 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 1L;. UnlockAr
c0fd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
c0fe0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c0ff0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 lRange = 0L;.
c1000 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 rc = DosSetFile
c1010 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
c1020 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
c1030 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
c1040 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
c1050 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 OSTRACE3( "TEST
c1060 20 57 52 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63 6b WR-LOCK %d lock
c1070 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72 reserved byte r
c1080 63 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e c=%d\n", pFile->
c1090 68 2c 20 72 63 20 29 3b 0a 20 20 20 20 69 66 28 h, rc );. if(
c10a0 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc == NO_ERROR
c10b0 29 7b 0a 20 20 20 20 20 20 41 50 49 52 45 54 20 ){. APIRET
c10c0 72 63 75 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 rcu = NO_ERROR;
c10d0 2f 2a 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 /* return code f
c10e0 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a or unlocking */.
c10f0 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c LockArea.l
c1100 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 Offset = 0L;.
c1110 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e LockArea.lRan
c1120 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 55 ge = 0L;. U
c1130 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
c1140 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
c1150 45 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 E;. UnlockA
c1160 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
c1170 0a 20 20 20 20 20 20 72 63 75 20 3d 20 44 6f 73 . rcu = Dos
c1180 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
c1190 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
c11a0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
c11b0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
c11c0 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 );. OSTRAC
c11d0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 E3( "TEST WR-LOC
c11e0 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 72 65 73 65 K %d unlock rese
c11f0 72 76 65 64 20 62 79 74 65 20 72 3d 25 64 5c 6e rved byte r=%d\n
c1200 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 75 ", pFile->h, rcu
c1210 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 20 );. }. r
c1220 3d 20 21 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 = !(rc == NO_ERR
c1230 4f 52 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 OR);. OSTRACE
c1240 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 3( "TEST WR-LOCK
c1250 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c %d %d (remote)\
c1260 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 n", pFile->h, r
c1270 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d );. }. *pOut =
c1280 20 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c r;. return SQL
c1290 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
c12a0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
c12b0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
c12c0 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 74 descriptor id t
c12d0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 o locktype. loc
c12e0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 ktype.** must be
c12f0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
c1300 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
c1310 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 **.** If the loc
c1320 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 king level of th
c1330 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
c1340 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 r is already at
c1350 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 or below.** the
c1360 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e requested lockin
c1370 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f g level, this ro
c1380 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
c1390 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f ..**.** It is no
c13a0 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 t possible for t
c13b0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 his routine to f
c13c0 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e ail if the secon
c13d0 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 d argument.** is
c13e0 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 NO_LOCK. If th
c13f0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
c1400 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b t is SHARED_LOCK
c1410 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
c1420 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 ne.** might retu
c1430 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
c1440 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c1450 73 32 55 6e 6c 6f 63 6b 28 20 73 71 6c 69 74 65 s2Unlock( sqlite
c1460 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
c1470 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e locktype ){. in
c1480 74 20 74 79 70 65 3b 0a 20 20 6f 73 32 46 69 6c t type;. os2Fil
c1490 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 e *pFile = (os2F
c14a0 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 ile*)id;. APIRE
c14b0 54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b T rc = SQLITE_OK
c14c0 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d ;. APIRET res =
c14d0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c NO_ERROR;. FIL
c14e0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
c14f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c . Unl
c1500 6f 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65 ockArea;. memse
c1510 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 t(&LockArea, 0,
c1520 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 sizeof(LockArea)
c1530 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c );. memset(&Unl
c1540 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
c1550 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
c1560 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
c1570 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
c1580 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 locktype<=SHARE
c1590 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 D_LOCK );. OSTR
c15a0 41 43 45 34 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE4( "UNLOCK %d
c15b0 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c 6e 22 to %d was %d\n"
c15c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
c15d0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 type, pFile->loc
c15e0 6b 74 79 70 65 20 29 3b 0a 20 20 74 79 70 65 20 ktype );. type
c15f0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 = pFile->locktyp
c1600 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45 e;. if( type>=E
c1610 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
c1620 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
c1630 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
c1640 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
c1650 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
c1660 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 Area.lOffset = S
c1670 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 HARED_FIRST;.
c1680 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
c1690 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 ge = SHARED_SIZE
c16a0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 ;. res = DosS
c16b0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
c16c0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
c16d0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
c16e0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
c16f0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
c1700 20 22 55 4e 4c 4f 43 4b 20 25 64 20 65 78 63 6c "UNLOCK %d excl
c1710 75 73 69 76 65 20 6c 6f 63 6b 20 72 65 73 3d 25 usive lock res=%
c1720 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c1730 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 6c res );. if( l
c1740 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
c1750 4c 4f 43 4b 20 26 26 20 67 65 74 52 65 61 64 4c LOCK && getReadL
c1760 6f 63 6b 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f ock(pFile) != NO
c1770 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
c1780 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e /* This should n
c1790 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 ever happen. We
c17a0 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 should always b
c17b0 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 e able to.
c17c0 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 ** reacquire the
c17d0 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 read lock */.
c17e0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 OSTRACE3( "U
c17f0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 67 NLOCK %d to %d g
c1800 65 74 52 65 61 64 4c 6f 63 6b 28 29 20 66 61 69 etReadLock() fai
c1810 6c 65 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 led\n", pFile->h
c1820 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 , locktype );.
c1830 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
c1840 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 IOERR_UNLOCK;.
c1850 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 }. }. if( ty
c1860 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe>=RESERVED_LOC
c1870 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 K ){. LockAre
c1880 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
c1890 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
c18a0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e nge = 0L;. Un
c18b0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
c18c0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c18d0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c18e0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
c18f0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
c1900 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e leLocks( pFile->
c1910 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
c1920 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
c1930 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 TIMEOUT, 0L );.
c1940 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e OSTRACE3( "UN
c1950 4c 4f 43 4b 20 25 64 20 72 65 73 65 72 76 65 64 LOCK %d reserved
c1960 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c1970 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d e->h, res );. }
c1980 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
c1990 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 =NO_LOCK && type
c19a0 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b >=SHARED_LOCK ){
c19b0 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 . res = unloc
c19c0 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 kReadLock(pFile)
c19d0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28 20 ;. OSTRACE5(
c19e0 22 55 4e 4c 4f 43 4b 20 25 64 20 69 73 20 25 64 "UNLOCK %d is %d
c19f0 20 77 61 6e 74 20 25 64 20 72 65 73 3d 25 64 5c want %d res=%d\
c1a00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 74 79 n", pFile->h, ty
c1a10 70 65 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 72 65 pe, locktype, re
c1a20 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 s );. }. if( t
c1a30 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
c1a40 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 K ){. LockAre
c1a50 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
c1a60 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
c1a70 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e nge = 0L;. Un
c1a80 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
c1a90 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b = PENDING_BYTE;
c1aa0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c1ab0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
c1ac0 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c res = DosSetFil
c1ad0 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
c1ae0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c1af0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c1b00 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
c1b10 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c OSTRACE3( "UNL
c1b20 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20 72 OCK %d pending r
c1b30 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d es=%d\n", pFile-
c1b40 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 >h, res );. }.
c1b50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c1b60 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f = locktype;. O
c1b70 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b STRACE3( "UNLOCK
c1b80 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 %d now %d\n", p
c1b90 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
c1ba0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 65 locktype );. re
c1bb0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
c1bc0 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 * Control and qu
c1bd0 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 ery of the open
c1be0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a file handle..*/.
c1bf0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69 static int os2Fi
c1c00 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
c1c10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
c1c20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
c1c30 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
c1c40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
c1c50 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
c1c60 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a : {. *(int*
c1c70 29 70 41 72 67 20 3d 20 28 28 6f 73 32 46 69 6c )pArg = ((os2Fil
c1c80 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 e*)id)->locktype
c1c90 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 ;. OSTRACE3
c1ca0 28 20 22 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 ( "FCNTL_LOCKSTA
c1cb0 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 TE %d lock=%d\n"
c1cc0 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 , ((os2File*)id)
c1cd0 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 ->h, ((os2File*)
c1ce0 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b id)->locktype );
c1cf0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
c1d00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
c1d10 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
c1d20 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a TE_ERROR;.}../*.
c1d30 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 ** Return the se
c1d40 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 ctor size in byt
c1d50 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c es of the underl
c1d60 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 ying block devic
c1d70 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 e for.** the spe
c1d80 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 cified file. Thi
c1d90 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 s is almost alwa
c1da0 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 ys 512 bytes, bu
c1db0 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 t may be.** larg
c1dc0 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 er for some devi
c1dd0 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ces..**.** SQLit
c1de0 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 e code assumes t
c1df0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e his function can
c1e00 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 not fail. It als
c1e10 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a o assumes that.*
c1e20 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 * if two files a
c1e30 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 re created in th
c1e40 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 e same file-syst
c1e50 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e em directory (i.
c1e60 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 e..** a database
c1e70 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c and its journal
c1e80 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 file) that the
c1e90 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c sector size will
c1ea0 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 be the.** same
c1eb0 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 for both..*/.sta
c1ec0 74 69 63 20 69 6e 74 20 6f 73 32 53 65 63 74 6f tic int os2Secto
c1ed0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
c1ee0 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 le *id){. retur
c1ef0 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 n SQLITE_DEFAULT
c1f00 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a _SECTOR_SIZE;.}.
c1f10 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
c1f20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 vector of device
c1f30 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
c1f40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c1f50 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 74 os2DeviceCharact
c1f60 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 eristics(sqlite3
c1f70 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 _file *id){. re
c1f80 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn 0;.}.../*.*
c1f90 2a 20 43 68 61 72 61 63 74 65 72 20 73 65 74 20 * Character set
c1fa0 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 conversion objec
c1fb0 74 73 20 75 73 65 64 20 62 79 20 63 6f 6e 76 65 ts used by conve
c1fc0 72 73 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a rsion routines..
c1fd0 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f */.static UconvO
c1fe0 62 6a 65 63 74 20 75 63 55 74 66 38 20 3d 20 4e bject ucUtf8 = N
c1ff0 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 20 ULL; /* convert
c2000 62 65 74 77 65 65 6e 20 55 54 46 2d 38 20 61 6e between UTF-8 an
c2010 64 20 55 43 53 2d 32 20 2a 2f 0a 73 74 61 74 69 d UCS-2 */.stati
c2020 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 c UconvObject uc
c2030 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 lCp = NULL; /*
c2040 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 convert between
c2050 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 61 local codepage a
c2060 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a 0a nd UCS-2 */../*.
c2070 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 ** Helper functi
c2080 6f 6e 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 on to initialize
c2090 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c20a0 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 objects from and
c20b0 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 to UTF-8..*/.st
c20c0 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 55 63 atic void initUc
c20d0 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 onvObjects( void
c20e0 20 29 7b 0a 20 20 69 66 28 20 55 6e 69 43 72 65 ){. if( UniCre
c20f0 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 ateUconvObject(
c2100 55 54 46 5f 38 2c 20 26 75 63 55 74 66 38 20 29 UTF_8, &ucUtf8 )
c2110 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 != ULS_SUCCESS
c2120 29 0a 20 20 20 20 75 63 55 74 66 38 20 3d 20 4e ). ucUtf8 = N
c2130 55 4c 4c 3b 0a 20 20 69 66 20 28 20 55 6e 69 43 ULL;. if ( UniC
c2140 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 reateUconvObject
c2150 28 20 28 55 6e 69 43 68 61 72 20 2a 29 4c 22 40 ( (UniChar *)L"@
c2160 70 61 74 68 3d 79 65 73 22 2c 20 26 75 63 6c 43 path=yes", &uclC
c2170 70 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 p ) != ULS_SUCCE
c2180 53 53 20 29 0a 20 20 20 20 75 63 6c 43 70 20 3d SS ). uclCp =
c2190 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 NULL;.}../*.**
c21a0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c21b0 74 6f 20 66 72 65 65 20 74 68 65 20 63 6f 6e 76 to free the conv
c21c0 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 ersion objects f
c21d0 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 rom and to UTF-8
c21e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
c21f0 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 freeUconvObject
c2200 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 20 s( void ){. if
c2210 28 20 75 63 55 74 66 38 20 29 0a 20 20 20 20 55 ( ucUtf8 ). U
c2220 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 niFreeUconvObjec
c2230 74 28 20 75 63 55 74 66 38 20 29 3b 0a 20 20 69 t( ucUtf8 );. i
c2240 66 20 28 20 75 63 6c 43 70 20 29 0a 20 20 20 20 f ( uclCp ).
c2250 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 UniFreeUconvObje
c2260 63 74 28 20 75 63 6c 43 70 20 29 3b 0a 20 20 75 ct( uclCp );. u
c2270 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 cUtf8 = NULL;.
c2280 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a uclCp = NULL;.}.
c2290 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 ./*.** Helper fu
c22a0 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 nction to conver
c22b0 74 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 t UTF-8 filename
c22c0 73 20 74 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32 20 s to local OS/2
c22d0 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 codepage..** The
c22e0 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 two-step proces
c22f0 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74 s: first convert
c2300 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 55 54 the incoming UT
c2310 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e F-8 string.** in
c2320 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65 to UCS-2 and the
c2330 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 n from UCS-2 to
c2340 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 64 65 the current code
c2350 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 page..** The ret
c2360 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e 74 urned char point
c2370 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 65 er has to be fre
c2380 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 ed..*/.static ch
c2390 61 72 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 50 ar *convertUtf8P
c23a0 61 74 68 54 6f 43 70 28 20 63 6f 6e 73 74 20 63 athToCp( const c
c23b0 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 har *in ){. Uni
c23c0 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 Char tempPath[CC
c23d0 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 HMAXPATH];. cha
c23e0 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a r *out = (char *
c23f0 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 )calloc( CCHMAXP
c2400 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 ATH, 1 );.. if(
c2410 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 !out ). retu
c2420 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 rn NULL;.. if(
c2430 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 !ucUtf8 || !uclC
c2440 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e p ). initUcon
c2450 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f vObjects();.. /
c2460 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72 69 * determine stri
c2470 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 ng for the conve
c2480 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 rsion of UTF-8 w
c2490 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20 2a hich is CP1208 *
c24a0 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f /. if( UniStrTo
c24b0 55 63 73 28 20 75 63 55 74 66 38 2c 20 74 65 6d Ucs( ucUtf8, tem
c24c0 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 pPath, (char *)i
c24d0 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 n, CCHMAXPATH )
c24e0 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 != ULS_SUCCESS )
c24f0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b . return out;
c2500 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f /* if conversio
c2510 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 n fails, return
c2520 74 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 the empty string
c2530 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 */.. /* conver
c2540 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 sion for current
c2550 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20 codepage which
c2560 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 can be used for
c2570 70 61 74 68 73 20 2a 2f 0a 20 20 55 6e 69 53 74 paths */. UniSt
c2580 72 46 72 6f 6d 55 63 73 28 20 75 63 6c 43 70 2c rFromUcs( uclCp,
c2590 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 out, tempPath,
c25a0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 CCHMAXPATH );..
c25b0 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a return out;.}..
c25c0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e /*.** Helper fun
c25d0 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 ction to convert
c25e0 20 66 69 6c 65 6e 61 6d 65 73 20 66 72 6f 6d 20 filenames from
c25f0 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 74 local codepage t
c2600 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 o UTF-8..** The
c2610 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 73 two-step process
c2620 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74 20 : first convert
c2630 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 64 the incoming cod
c2640 65 70 61 67 65 2d 73 70 65 63 69 66 69 63 0a 2a epage-specific.*
c2650 2a 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 55 43 * string into UC
c2660 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f S-2 and then fro
c2670 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 m UCS-2 to the c
c2680 6f 64 65 70 61 67 65 20 6f 66 20 55 54 46 2d 38 odepage of UTF-8
c2690 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 ..** The returne
c26a0 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 d char pointer h
c26b0 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a as to be freed..
c26c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
c26d0 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73 74 61 74 69 ion is non-stati
c26e0 63 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 c to be able to
c26f0 75 73 65 20 74 68 69 73 20 69 6e 20 73 68 65 6c use this in shel
c2700 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73 69 6d 69 6c l.c and.** simil
c2710 61 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ar applications
c2720 74 68 61 74 20 74 61 6b 65 20 63 6f 6d 6d 61 6e that take comman
c2730 64 20 6c 69 6e 65 20 61 72 67 75 6d 65 6e 74 73 d line arguments
c2740 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63 6f 6e 76 65 ..*/.char *conve
c2750 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 rtCpPathToUtf8(
c2760 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 const char *in )
c2770 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 {. UniChar temp
c2780 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d Path[CCHMAXPATH]
c2790 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 ;. char *out =
c27a0 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 (char *)calloc(
c27b0 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b CCHMAXPATH, 1 );
c27c0 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 .. if( !out ).
c27d0 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a return NULL;.
c27e0 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c . if( !ucUtf8 |
c27f0 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 | !uclCp ). i
c2800 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 nitUconvObjects(
c2810 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73 );.. /* convers
c2820 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 20 ion for current
c2830 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20 63 codepage which c
c2840 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 70 an be used for p
c2850 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 55 6e aths */. if( Un
c2860 69 53 74 72 54 6f 55 63 73 28 20 75 63 6c 43 70 iStrToUcs( uclCp
c2870 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61 , tempPath, (cha
c2880 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 r *)in, CCHMAXPA
c2890 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 TH ) != ULS_SUCC
c28a0 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e ESS ). return
c28b0 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 out; /* if conv
c28c0 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 ersion fails, re
c28d0 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73 turn the empty s
c28e0 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 64 tring */.. /* d
c28f0 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 etermine string
c2900 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73 69 for the conversi
c2910 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69 63 on of UTF-8 whic
c2920 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 h is CP1208 */.
c2930 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 UniStrFromUcs(
c2940 75 63 55 74 66 38 2c 20 6f 75 74 2c 20 74 65 6d ucUtf8, out, tem
c2950 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 pPath, CCHMAXPAT
c2960 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f H );.. return o
c2970 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ut;.}../*.** Thi
c2980 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 s vector defines
c2990 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 all the methods
c29a0 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 that can operat
c29b0 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 e on an.** sqlit
c29c0 65 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73 32 2e e3_file for os2.
c29d0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
c29e0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
c29f0 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f 64 20 ods os2IoMethod
c2a00 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 = {. 1,
c2a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2a20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
c2a30 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 32 os2Close,. os2
c2a40 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69 74 65 Read,. os2Write
c2a50 2c 0a 20 20 6f 73 32 54 72 75 6e 63 61 74 65 2c ,. os2Truncate,
c2a60 0a 20 20 6f 73 32 53 79 6e 63 2c 0a 20 20 6f 73 . os2Sync,. os
c2a70 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 32 2FileSize,. os2
c2a80 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 63 Lock,. os2Unloc
c2a90 6b 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52 65 73 k,. os2CheckRes
c2aa0 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 32 ervedLock,. os2
c2ab0 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 6f FileControl,. o
c2ac0 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 s2SectorSize,.
c2ad0 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 74 os2DeviceCharact
c2ae0 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a eristics.};../**
c2af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 *********.** Her
c2b40 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d e ends the I/O m
c2b50 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d ethods that form
c2b60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
c2b70 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
c2b80 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 **.** The next b
c2b90 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 lock of code imp
c2ba0 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 lements the VFS
c2bb0 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a methods..*******
c2bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2c00 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 72 *****/../*.** Cr
c2c10 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 eate a temporary
c2c20 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 file name in zB
c2c30 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 uf. zBuf must b
c2c40 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a e big enough to.
c2c50 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d ** hold at pVfs-
c2c60 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 >mxPathname char
c2c70 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 acters..*/.stati
c2c80 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d c int getTempnam
c2c90 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 e(int nBuf, char
c2ca0 20 2a 7a 42 75 66 20 29 7b 0a 20 20 73 74 61 74 *zBuf ){. stat
c2cb0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
c2cc0 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 d char zChars[]
c2cd0 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 =. "abcdefghi
c2ce0 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 jklmnopqrstuvwxy
c2cf0 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 z". "ABCDEFGH
c2d00 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 IJKLMNOPQRSTUVWX
c2d10 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 YZ". "0123456
c2d20 37 38 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 789";. int i, j
c2d30 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61 ;. char zTempPa
c2d40 74 68 42 75 66 5b 33 5d 3b 0a 20 20 50 53 5a 20 thBuf[3];. PSZ
c2d50 7a 54 65 6d 70 50 61 74 68 20 3d 20 28 50 53 5a zTempPath = (PSZ
c2d60 29 26 7a 54 65 6d 70 50 61 74 68 42 75 66 3b 0a )&zTempPathBuf;.
c2d70 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 if( sqlite3_te
c2d80 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a mp_directory ){.
c2d90 20 20 20 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 zTempPath =
c2da0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
c2db0 65 63 74 6f 72 79 3b 0a 20 20 7d 65 6c 73 65 7b ectory;. }else{
c2dc0 0a 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e . if( DosScan
c2dd0 45 6e 76 28 20 28 50 53 5a 29 22 54 45 4d 50 22 Env( (PSZ)"TEMP"
c2de0 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 , &zTempPath ) )
c2df0 7b 0a 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 {. if( DosS
c2e00 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d canEnv( (PSZ)"TM
c2e10 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 P", &zTempPath )
c2e20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
c2e30 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a DosScanEnv( (PSZ
c2e40 29 22 54 4d 50 44 49 52 22 2c 20 26 7a 54 65 6d )"TMPDIR", &zTem
c2e50 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 pPath ) ){.
c2e60 20 20 20 20 20 20 55 4c 4f 4e 47 20 75 6c 44 72 ULONG ulDr
c2e70 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75 6c 44 72 iveNum = 0, ulDr
c2e80 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20 20 20 20 iveMap = 0;.
c2e90 20 20 20 20 20 20 20 44 6f 73 51 75 65 72 79 43 DosQueryC
c2ea0 75 72 72 65 6e 74 44 69 73 6b 28 20 26 75 6c 44 urrentDisk( &ulD
c2eb0 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76 riveNum, &ulDriv
c2ec0 65 4d 61 70 20 29 3b 0a 20 20 20 20 20 20 20 20 eMap );.
c2ed0 20 20 20 73 70 72 69 6e 74 66 28 20 28 63 68 61 sprintf( (cha
c2ee0 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 r*)zTempPath, "%
c2ef0 63 3a 22 2c 20 28 63 68 61 72 29 28 20 27 41 27 c:", (char)( 'A'
c2f00 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d 20 2d 20 + ulDriveNum -
c2f10 31 20 29 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 1 ) );. }
c2f20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
c2f30 20 7d 0a 20 20 2f 2a 20 53 74 72 69 70 20 6f 66 }. /* Strip of
c2f40 66 20 61 20 74 72 61 69 6c 69 6e 67 20 73 6c 61 f a trailing sla
c2f50 73 68 65 73 20 6f 72 20 62 61 63 6b 73 6c 61 73 shes or backslas
c2f60 68 65 73 2c 20 6f 74 68 65 72 77 69 73 65 20 77 hes, otherwise w
c2f70 65 20 77 6f 75 6c 64 20 67 65 74 20 2a 0a 20 20 e would get *.
c2f80 20 2a 20 6d 75 6c 74 69 70 6c 65 20 28 62 61 63 * multiple (bac
c2f90 6b 29 73 6c 61 73 68 65 73 20 77 68 69 63 68 20 k)slashes which
c2fa0 63 61 75 73 65 73 20 44 6f 73 4f 70 65 6e 28 29 causes DosOpen()
c2fb0 20 74 6f 20 66 61 69 6c 2e 20 20 20 20 20 20 20 to fail.
c2fc0 20 20 20 20 20 20 20 2a 0a 20 20 20 2a 20 54 72 *. * Tr
c2fd0 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 61 72 ailing spaces ar
c2fe0 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 2c 20 65 e not allowed, e
c2ff0 69 74 68 65 72 2e 20 20 20 20 20 20 20 20 20 20 ither.
c3000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3010 20 20 2a 2f 0a 20 20 6a 20 3d 20 73 71 6c 69 74 */. j = sqlit
c3020 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 e3Strlen30(zTemp
c3030 50 61 74 68 29 3b 0a 20 20 77 68 69 6c 65 28 20 Path);. while(
c3040 6a 20 3e 20 30 20 26 26 20 28 20 7a 54 65 6d 70 j > 0 && ( zTemp
c3050 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c Path[j-1] == '\\
c3060 27 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a ' || zTempPath[j
c3070 2d 31 5d 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20 -1] == '/'.
c3080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
c3090 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d | zTempPath[j-1]
c30a0 20 3d 3d 20 27 20 27 20 29 20 29 7b 0a 20 20 20 == ' ' ) ){.
c30b0 20 6a 2d 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d j--;. }. zTem
c30c0 70 50 61 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b pPath[j] = '\0';
c30d0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f . if( !sqlite3_
c30e0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 temp_directory )
c30f0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d {. char *zTem
c3100 70 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 pPathUTF = conve
c3110 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 rtCpPathToUtf8(
c3120 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 zTempPath );.
c3130 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
c3140 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 f( nBuf-30, zBuf
c3150 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c3160 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 "%s\\"SQ
c3170 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
c3180 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 REFIX, zTempPath
c3190 55 54 46 20 29 3b 0a 20 20 20 20 66 72 65 65 28 UTF );. free(
c31a0 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b zTempPathUTF );
c31b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
c31c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
c31d0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 nBuf-30, zBuf,.
c31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c31f0 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 "%s\\"SQLIT
c3200 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
c3210 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 20 29 3b IX, zTempPath );
c3220 0a 20 20 7d 0a 20 20 6a 20 3d 20 73 71 6c 69 74 . }. j = sqlit
c3230 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 42 75 66 e3Strlen30( zBuf
c3240 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 );. sqlite3_ra
c3250 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20 26 7a ndomness( 20, &z
c3260 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72 28 Buf[j] );. for(
c3270 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30 3b 20 i = 0; i < 20;
c3280 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20 20 i++, j++ ){.
c3290 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 zBuf[j] = (char)
c32a0 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e zChars[ ((unsign
c32b0 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 ed char)zBuf[j])
c32c0 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 %(sizeof(zChars)
c32d0 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 -1) ];. }. zBu
c32e0 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 f[j] = 0;. OSTR
c32f0 41 43 45 32 28 20 22 54 45 4d 50 20 46 49 4c 45 ACE2( "TEMP FILE
c3300 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 NAME: %s\n", zBu
c3310 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 f );. return SQ
c3320 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
c3330 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 ** Turn a relati
c3340 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f ve pathname into
c3350 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 a full pathname
c3360 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75 6c . Write the ful
c3370 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e l.** pathname in
c3380 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75 to zFull[]. zFu
c3390 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 ll[] will be at
c33a0 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 least pVfs->mxPa
c33b0 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 thname.** bytes
c33c0 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 in size..*/.stat
c33d0 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c 50 61 ic int os2FullPa
c33e0 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 thname(. sqlite
c33f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
c3400 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
c3410 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a to vfs object *
c3420 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
c3430 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 20 zRelative,
c3440 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 /* Possibly rela
c3450 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 tive input path
c3460 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 */. int nFull,
c3470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3480 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 /* Size of outp
c3490 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 ut buffer in byt
c34a0 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 es */. char *zF
c34b0 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 ull
c34c0 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 /* Output bu
c34d0 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 ffer */.){. cha
c34e0 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70 20 3d r *zRelativeCp =
c34f0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 convertUtf8Path
c3500 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76 65 20 ToCp( zRelative
c3510 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c 43 );. char zFullC
c3520 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20 3d 20 p[CCHMAXPATH] =
c3530 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a 7a 46 "\0";. char *zF
c3540 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52 45 54 ullUTF;. APIRET
c3550 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61 rc = DosQueryPa
c3560 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74 69 76 thInfo( zRelativ
c3570 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59 46 55 eCp, FIL_QUERYFU
c3580 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43 70 2c LLNAME, zFullCp,
c3590 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c35a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c35b0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 20 CCHMAXPATH );.
c35c0 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 76 65 free( zRelative
c35d0 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55 54 46 Cp );. zFullUTF
c35e0 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68 = convertCpPath
c35f0 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 70 20 ToUtf8( zFullCp
c3600 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 );. sqlite3_snp
c3610 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20 7a 46 rintf( nFull, zF
c3620 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20 29 3b ull, zFullUTF );
c3630 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c 55 54 . free( zFullUT
c3640 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 F );. return rc
c3650 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 == NO_ERROR ? S
c3660 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
c3670 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a 0a E_IOERR;.}.../*.
c3680 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a ** Open a file..
c3690 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c36a0 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2Open(. sqlite3
c36b0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
c36c0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
c36d0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
c36e0 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
c36f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
c3700 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 the file */. s
c3710 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
c3720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c3730 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74 65 Write the SQLite
c3740 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 file handle her
c3750 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
c3760 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c3770 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 /* Open mod
c3780 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 e flags */. int
c3790 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
c37a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
c37b0 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 atus return flag
c37c0 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 20 s */.){. HFILE
c37d0 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46 69 6c h;. ULONG ulFil
c37e0 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c eAttribute = FIL
c37f0 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f 4e E_NORMAL;. ULON
c3800 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 3d 20 G ulOpenFlags =
c3810 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 0;. ULONG ulOpe
c3820 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f 73 32 nMode = 0;. os2
c3830 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
c3840 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 s2File*)id;. AP
c3850 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 IRET rc = NO_ERR
c3860 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 63 OR;. ULONG ulAc
c3870 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 4e tion;. char *zN
c3880 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20 7a 54 ameCp;. char zT
c3890 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50 41 54 mpname[CCHMAXPAT
c38a0 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75 66 66 H+1]; /* Buff
c38b0 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 er to hold name
c38c0 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a 2f 0a of temp file */.
c38d0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 . /* If the sec
c38e0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
c38f0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
c3900 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 NULL, generate
c3910 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 a . ** temporar
c3920 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 y file name to u
c3930 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 se . */. if( !
c3940 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 zName ){. int
c3950 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d rc = getTempnam
c3960 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31 2c 20 e(CCHMAXPATH+1,
c3970 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 zTmpname);. i
c3980 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
c3990 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
c39a0 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a rc;. }. z
c39b0 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b Name = zTmpname;
c39c0 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74 28 . }... memset(
c39d0 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f pFile, 0, sizeo
c39e0 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a 20 20 f(*pFile) );..
c39f0 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20 OSTRACE2( "OPEN
c3a00 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c 61 67 want %d\n", flag
c3a10 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 s );.. if( flag
c3a20 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
c3a30 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 READWRITE ){.
c3a40 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f ulOpenMode |= O
c3a50 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 57 PEN_ACCESS_READW
c3a60 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 RITE;. OSTRAC
c3a70 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 2f 77 E1( "OPEN read/w
c3a80 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c rite\n" );. }el
c3a90 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f se{. ulOpenMo
c3aa0 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 de |= OPEN_ACCES
c3ab0 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 S_READONLY;.
c3ac0 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 OSTRACE1( "OPEN
c3ad0 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a read only\n" );.
c3ae0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
c3af0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 & SQLITE_OPEN_C
c3b00 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f REATE ){. ulO
c3b10 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e penFlags |= OPEN
c3b20 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f _ACTION_OPEN_IF_
c3b30 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 EXISTS | OPEN_AC
c3b40 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e TION_CREATE_IF_N
c3b50 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 EW;. OSTRACE1
c3b60 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e 65 77 ( "OPEN open new
c3b70 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a 20 20 /create\n" );.
c3b80 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 }else{. ulOpe
c3b90 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 nFlags |= OPEN_A
c3ba0 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 CTION_OPEN_IF_EX
c3bb0 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49 ISTS | OPEN_ACTI
c3bc0 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57 3b 0a ON_FAIL_IF_NEW;.
c3bd0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c3be0 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74 69 6e PEN open existin
c3bf0 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 g\n" );. }.. i
c3c00 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c3c10 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 E_OPEN_MAIN_DB )
c3c20 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 {. ulOpenMode
c3c30 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 |= OPEN_SHARE_D
c3c40 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53 54 ENYNONE;. OST
c3c50 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61 RACE1( "OPEN sha
c3c60 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 re read/write\n"
c3c70 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
c3c80 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f ulOpenMode |= O
c3c90 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57 52 PEN_SHARE_DENYWR
c3ca0 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 ITE;. OSTRACE
c3cb0 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 1( "OPEN share r
c3cc0 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20 ead only\n" );.
c3cd0 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 }.. if( flags
c3ce0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 & SQLITE_OPEN_DE
c3cf0 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 LETEONCLOSE ){.
c3d00 20 20 20 63 68 61 72 20 70 61 74 68 55 74 66 38 char pathUtf8
c3d10 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 23 69 [CCHMAXPATH];.#i
c3d20 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a 20 77 fdef NDEBUG /* w
c3d30 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 77 65 hen debugging we
c3d40 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 73 75 want to make su
c3d50 72 65 20 69 74 20 69 73 20 64 65 6c 65 74 65 64 re it is deleted
c3d60 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65 41 74 */. ulFileAt
c3d70 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f 48 tribute = FILE_H
c3d80 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a 20 20 IDDEN;.#endif.
c3d90 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d os2FullPathnam
c3da0 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 e( pVfs, zName,
c3db0 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61 74 68 CCHMAXPATH, path
c3dc0 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46 69 6c Utf8 );. pFil
c3dd0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 63 e->pathToDel = c
c3de0 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f onvertUtf8PathTo
c3df0 43 70 28 20 70 61 74 68 55 74 66 38 20 29 3b 0a Cp( pathUtf8 );.
c3e00 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c3e10 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c 65 74 PEN hidden/delet
c3e20 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c 65 20 e on close file
c3e30 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20 29 3b attributes\n" );
c3e40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 . }else{. pF
c3e50 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d ile->pathToDel =
c3e60 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54 52 41 NULL;. OSTRA
c3e70 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72 6d 61 CE1( "OPEN norma
c3e80 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 l file attribute
c3e90 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a \n" );. }.. /*
c3ea0 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69 6e 20 always open in
c3eb0 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20 6d 6f random access mo
c3ec0 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c 79 20 de for possibly
c3ed0 62 65 74 74 65 72 20 73 70 65 65 64 20 2a 2f 0a better speed */.
c3ee0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
c3ef0 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f OPEN_FLAGS_RANDO
c3f00 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 M;. ulOpenMode
c3f10 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41 |= OPEN_FLAGS_FA
c3f20 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20 75 IL_ON_ERROR;. u
c3f30 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c3f40 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 49 N_FLAGS_NOINHERI
c3f50 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20 3d 20 T;.. zNameCp =
c3f60 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 convertUtf8PathT
c3f70 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 20 oCp( zName );.
c3f80 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50 rc = DosOpen( (P
c3f90 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 20 SZ)zNameCp,.
c3fa0 20 20 20 20 20 20 20 20 20 20 20 20 26 68 2c 0a &h,.
c3fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3fc0 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20 20 20 &ulAction,.
c3fd0 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0a 20 0L,.
c3fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
c3ff0 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 0a lFileAttribute,.
c4000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4010 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 ulOpenFlags,.
c4020 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f ulO
c4030 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 penMode,.
c4040 20 20 20 20 20 20 20 20 20 28 50 45 41 4f 50 32 (PEAOP2
c4050 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65 65 28 )NULL );. free(
c4060 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20 69 66 zNameCp );. if
c4070 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ( rc != NO_ERROR
c4080 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 37 ){. OSTRACE7
c4090 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69 64 20 ( "OPEN Invalid
c40a0 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20 7a 4e handle rc=%d: zN
c40b0 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69 6f 6e ame=%s, ulAction
c40c0 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d 25 23 =%#lx, ulAttr=%#
c40d0 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23 6c 78 lx, ulFlags=%#lx
c40e0 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e 22 , ulMode=%#lx\n"
c40f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c4100 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 74 rc, zName, ulAct
c4110 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74 72 69 ion, ulFileAttri
c4120 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 67 bute, ulOpenFlag
c4130 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 3b s, ulOpenMode );
c4140 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
c4150 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20 20 20 pathToDel ).
c4160 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e 70 free( pFile->p
c4170 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 athToDel );.
c4180 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c4190 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 28 = NULL;. if(
c41a0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
c41b0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
c41c0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 {. OSTRACE2
c41d0 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 61 6c ( "OPEN %d Inval
c41e0 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 28 id handle\n", ((
c41f0 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f flags | SQLITE_O
c4200 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 PEN_READONLY) &
c4210 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 ~SQLITE_OPEN_REA
c4220 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 20 20 DWRITE) );.
c4230 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28 return os2Open(
c4240 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 pVfs, zName, id
c4250 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c4260 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 20 ((flags
c4270 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 | SQLITE_OPEN_RE
c4280 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 ADONLY) & ~SQLIT
c4290 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
c42a0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
c42b0 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 6c 61 pOutFla
c42c0 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b gs );. }else{
c42d0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
c42e0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 LITE_CANTOPEN;.
c42f0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
c4300 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 pOutFlags ){.
c4310 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c *pOutFlags = fl
c4320 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
c4330 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20 53 51 N_READWRITE ? SQ
c4340 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c4350 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f 50 45 ITE : SQLITE_OPE
c4360 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a N_READONLY;. }.
c4370 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f . pFile->pMetho
c4380 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68 6f 64 d = &os2IoMethod
c4390 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 ;. pFile->h = h
c43a0 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 ;. OpenCounter(
c43b0 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 +1);. OSTRACE3(
c43c0 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74 46 6c "OPEN %d pOutFl
c43d0 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ags=%d\n", pFile
c43e0 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73 20 29 ->h, pOutFlags )
c43f0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c4400 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
c4410 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 20 elete the named
c4420 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
c4430 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28 0a 20 int os2Delete(.
c4440 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
c4450 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 fs,
c4460 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 /* Not u
c4470 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 sed on os2 */.
c4480 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
c4490 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 ename,
c44a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
c44b0 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 f file to delete
c44c0 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 */. int syncDi
c44d0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
c44e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c44f0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 Not used on os2
c4500 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54 20 72 */.){. APIRET r
c4510 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c4520 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 char *zFilenameC
c4530 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
c4540 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 athToCp( zFilena
c4550 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 me );. Simulate
c4560 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
c4570 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
c4580 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f ETE );. rc = Do
c4590 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29 7a 46 sDelete( (PSZ)zF
c45a0 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 66 ilenameCp );. f
c45b0 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 ree( zFilenameCp
c45c0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 );. OSTRACE2(
c45d0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e "DELETE \"%s\"\n
c45e0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a ", zFilename );.
c45f0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e return rc == N
c4600 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
c4610 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
c4620 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a RR_DELETE;.}../*
c4630 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78 .** Check the ex
c4640 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74 istance and stat
c4650 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f us of a file..*/
c4660 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 41 .static int os2A
c4670 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
c4680 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
c4690 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
c46a0 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 n os2 */. const
c46b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
c46c0 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 , /* Name of
c46d0 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f file to check */
c46e0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
c46f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c4700 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 Type of test to
c4710 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c make on this fil
c4720 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 e */. int *pOut
c4730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4740 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 /* Write result
c4750 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 46 s here */.){. F
c4760 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 ILESTATUS3 fsts3
c4770 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 41 50 ConfigInfo;. AP
c4780 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 IRET rc = NO_ERR
c4790 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c OR;. char *zFil
c47a0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 enameCp = conver
c47b0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c47c0 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20 20 6d Filename );.. m
c47d0 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 6f 6e emset( &fsts3Con
c47e0 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 figInfo, 0, size
c47f0 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e of(fsts3ConfigIn
c4800 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f fo) );. rc = Do
c4810 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 sQueryPathInfo(
c4820 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 (PSZ)zFilenameCp
c4830 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 0a , FIL_STANDARD,.
c4840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4850 20 20 20 20 20 20 20 20 20 26 66 73 74 73 33 43 &fsts3C
c4860 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65 6f onfigInfo, sizeo
c4870 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 f(FILESTATUS3) )
c4880 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e ;. free( zFilen
c4890 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 ameCp );. OSTRA
c48a0 43 45 34 28 20 22 41 43 43 45 53 53 20 66 73 74 CE4( "ACCESS fst
c48b0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 s3ConfigInfo.att
c48c0 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73 3d 25 rFile=%d flags=%
c48d0 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 d rc=%d\n",.
c48e0 20 20 20 20 20 20 20 20 66 73 74 73 33 43 6f 6e fsts3Con
c48f0 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 figInfo.attrFile
c4900 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b 0a 20 , flags, rc );.
c4910 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 switch( flags )
c4920 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
c4930 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 E_ACCESS_READ:.
c4940 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
c4950 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 CCESS_EXISTS:.
c4960 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 rc = (rc ==
c4970 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 20 NO_ERROR);.
c4980 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43 45 OSTRACE3( "ACCE
c4990 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 SS %s access of
c49a0 72 65 61 64 20 61 6e 64 20 65 78 69 73 74 73 20 read and exists
c49b0 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 rc=%d\n", zFile
c49c0 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 name, rc );.
c49d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
c49e0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
c49f0 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 READWRITE:.
c4a00 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f rc = (rc == NO_
c4a10 45 52 52 4f 52 29 20 26 26 20 28 20 28 66 73 74 ERROR) && ( (fst
c4a20 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 s3ConfigInfo.att
c4a30 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52 45 41 rFile & FILE_REA
c4a40 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b 0a 20 DONLY) == 0 );.
c4a50 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 OSTRACE3( "
c4a60 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 73 ACCESS %s access
c4a70 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 20 20 of read/write
c4a80 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e rc=%d\n", zFilen
c4a90 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 20 ame, rc );.
c4aa0 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 break;. defa
c4ab0 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72 ult:. asser
c4ac0 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 t( !"Invalid fla
c4ad0 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 3b 0a gs argument" );.
c4ae0 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72 63 }. *pOut = rc
c4af0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c4b00 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 E_OK;.}...#ifnde
c4b10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
c4b20 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a AD_EXTENSION./*.
c4b30 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f ** Interfaces fo
c4b40 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 r opening a shar
c4b50 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 ed library, find
c4b60 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 ing entry points
c4b70 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 .** within the s
c4b80 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 hared library, a
c4b90 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 nd closing the s
c4ba0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a hared library..*
c4bb0 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 /./*.** Interfac
c4bc0 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 es for opening a
c4bd0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
c4be0 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 finding entry p
c4bf0 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 oints.** within
c4c00 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
c4c10 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ry, and closing
c4c20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
c4c30 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
c4c40 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28 73 71 id *os2DlOpen(sq
c4c50 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c4c60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
c4c70 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48 41 52 lename){. UCHAR
c4c80 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b 0a 20 loadErr[256];.
c4c90 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a 20 HMODULE hmod;.
c4ca0 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 63 68 APIRET rc;. ch
c4cb0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ar *zFilenameCp
c4cc0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 = convertUtf8Pat
c4cd0 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d 65 29 hToCp(zFilename)
c4ce0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f 61 64 ;. rc = DosLoad
c4cf0 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f 61 64 Module((PSZ)load
c4d00 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f 61 64 Err, sizeof(load
c4d10 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d 65 43 Err), zFilenameC
c4d20 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66 72 65 p, &hmod);. fre
c4d30 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29 3b 0a e(zFilenameCp);.
c4d40 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e return rc != N
c4d50 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 O_ERROR ? 0 : (v
c4d60 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a 0a oid*)hmod;.}./*.
c4d70 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e 63 65 ** A no-op since
c4d80 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
c4d90 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 74 is returned on t
c4da0 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 he DosLoadModule
c4db0 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c 6f call..** os2Dlo
c4dc0 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 6f pen returns zero
c4dd0 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c if DosLoadModul
c4de0 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65 73 73 e is not success
c4df0 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ful..*/.static v
c4e00 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72 28 73 oid os2DlError(s
c4e10 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
c4e20 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
c4e30 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a 20 6e *zBufOut){./* n
c4e40 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74 69 63 o-op */.}.static
c4e50 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 6d 28 void *os2DlSym(
c4e60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c4e70 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 s, void *pHandle
c4e80 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 , const char *zS
c4e90 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 66 ymbol){. PFN pf
c4ea0 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a n;. APIRET rc;.
c4eb0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 rc = DosQueryP
c4ec0 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 rocAddr((HMODULE
c4ed0 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a 53 )pHandle, 0L, zS
c4ee0 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 ymbol, &pfn);.
c4ef0 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 if( rc != NO_ERR
c4f00 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 66 20 OR ){. /* if
c4f10 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c the symbol itsel
c4f20 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c f was not found,
c4f30 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 66 6f search again fo
c4f40 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 r the same.
c4f50 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e * symbol with an
c4f60 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f 72 extra underscor
c4f70 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 e, that might be
c4f80 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69 6e needed dependin
c4f90 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65 20 g. * on the
c4fa0 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69 calling conventi
c4fb0 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 5f on */. char _
c4fc0 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 22 zSymbol[256] = "
c4fd0 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 74 28 _";. strncat(
c4fe0 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 6f _zSymbol, zSymbo
c4ff0 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 63 20 l, 255);. rc
c5000 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 = DosQueryProcAd
c5010 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e dr((HMODULE)pHan
c5020 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 6f dle, 0L, _zSymbo
c5030 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 20 l, &pfn);. }.
c5040 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f return rc != NO_
c5050 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69 ERROR ? 0 : (voi
c5060 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74 69 63 d*)pfn;.}.static
c5070 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f 73 65 void os2DlClose
c5080 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
c5090 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
c50a0 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d 6f 64 e){. DosFreeMod
c50b0 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 ule((HMODULE)pHa
c50c0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f ndle);.}.#else /
c50d0 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * if SQLITE_OMIT
c50e0 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 _LOAD_EXTENSION
c50f0 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 is defined: */.
c5100 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 4f 70 #define os2DlOp
c5110 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f en 0. #define o
c5120 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 s2DlError 0. #d
c5130 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d 20 30 efine os2DlSym 0
c5140 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c . #define os2Dl
c5150 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a Close 0.#endif..
c5160 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 ./*.** Write up
c5170 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 to nBuf bytes of
c5180 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f randomness into
c5190 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 zBuf..*/.static
c51a0 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 int os2Randomne
c51b0 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ss(sqlite3_vfs *
c51c0 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 pVfs, int nBuf,
c51d0 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 char *zBuf ){.
c51e0 69 6e 74 20 6e 20 3d 20 30 3b 0a 23 69 66 20 64 int n = 0;.#if d
c51f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
c5200 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a ST). n = nBuf;.
c5210 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 memset(zBuf, 0
c5220 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 , nBuf);.#else.
c5230 20 69 6e 74 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 int sizeofULong
c5240 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f 4e 47 29 = sizeof(ULONG)
c5250 3b 0a 20 20 69 66 28 20 28 69 6e 74 29 73 69 7a ;. if( (int)siz
c5260 65 6f 66 28 44 41 54 45 54 49 4d 45 29 20 3c 3d eof(DATETIME) <=
c5270 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
c5280 20 44 41 54 45 54 49 4d 45 20 78 3b 0a 20 20 20 DATETIME x;.
c5290 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28 DosGetDateTime(
c52a0 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 &x);. memcpy(
c52b0 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 &zBuf[n], &x, si
c52c0 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 zeof(x));. n
c52d0 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 += sizeof(x);.
c52e0 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 }.. if( sizeofU
c52f0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c5300 20 29 7b 0a 20 20 20 20 50 50 49 42 20 70 70 69 ){. PPIB ppi
c5310 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 b;. DosGetInf
c5320 6f 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 oBlocks(NULL, &p
c5330 70 69 62 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 pib);. memcpy
c5340 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62 (&zBuf[n], &ppib
c5350 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a ->pib_ulpid, siz
c5360 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e eofULong);. n
c5370 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c5380 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 . }.. if( size
c5390 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 ofULong <= nBuf
c53a0 2d 20 6e 20 29 7b 0a 20 20 20 20 50 54 49 42 20 - n ){. PTIB
c53b0 70 74 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 ptib;. DosGet
c53c0 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 InfoBlocks(&ptib
c53d0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d , NULL);. mem
c53e0 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 cpy(&zBuf[n], &p
c53f0 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e tib->tib_ptib2->
c5400 74 69 62 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65 tib2_ultid, size
c5410 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 ofULong);. n
c5420 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a += sizeofULong;.
c5430 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 }.. /* if we
c5440 73 74 69 6c 6c 20 68 61 76 65 6e 27 74 20 66 69 still haven't fi
c5450 6c 6c 65 64 20 74 68 65 20 62 75 66 66 65 72 20 lled the buffer
c5460 79 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e yet the followin
c5470 67 20 77 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67 g will */. /* g
c5480 72 61 62 20 65 76 65 72 79 74 68 69 6e 67 20 6f rab everything o
c5490 6e 63 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6d nce instead of m
c54a0 61 6b 69 6e 67 20 73 65 76 65 72 61 6c 20 63 61 aking several ca
c54b0 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 lls for a single
c54c0 20 69 74 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73 item */. if( s
c54d0 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 izeofULong <= nB
c54e0 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c uf - n ){. UL
c54f0 4f 4e 47 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ONG ulSysInfo[QS
c5500 56 5f 4d 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51 V_MAX];. DosQ
c5510 75 65 72 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20 uerySysInfo(1L,
c5520 51 53 56 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e QSV_MAX, ulSysIn
c5530 66 6f 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 fo, sizeofULong
c5540 2a 20 51 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20 * QSV_MAX);..
c5550 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c5560 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 , &ulSysInfo[QSV
c5570 5f 4d 53 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20 _MS_COUNT - 1],
c5580 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c5590 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f n += sizeofULo
c55a0 6e 67 3b 0a 0a 20 20 20 20 69 66 28 20 73 69 7a ng;.. if( siz
c55b0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 eofULong <= nBuf
c55c0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 - n ){. me
c55d0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c55e0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 ulSysInfo[QSV_TI
c55f0 4d 45 52 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31 MER_INTERVAL - 1
c5600 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b ], sizeofULong);
c5610 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 . n += size
c5620 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 ofULong;. }.
c5630 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f if( sizeofULo
c5640 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c5650 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 {. memcpy(&
c5660 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 zBuf[n], &ulSysI
c5670 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57 nfo[QSV_TIME_LOW
c5680 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f - 1], sizeofULo
c5690 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 ng);. n +=
c56a0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 sizeofULong;.
c56b0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f }. if( sizeo
c56c0 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d fULong <= nBuf -
c56d0 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 n ){. memc
c56e0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c py(&zBuf[n], &ul
c56f0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 SysInfo[QSV_TIME
c5700 5f 48 49 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65 _HIGH - 1], size
c5710 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
c5720 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
c5730 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
c5740 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e sizeofULong <= n
c5750 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 Buf - n ){.
c5760 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c5770 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 , &ulSysInfo[QSV
c5780 5f 54 4f 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31 _TOTAVAILMEM - 1
c5790 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b ], sizeofULong);
c57a0 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 . n += size
c57b0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 ofULong;. }.
c57c0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 }.#endif.. ret
c57d0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
c57e0 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 Sleep for a litt
c57f0 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 le while. Retur
c5800 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 n the amount of
c5810 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 time slept..** T
c5820 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 he argument is t
c5830 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 he number of mic
c5840 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e roseconds we wan
c5850 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 t to sleep..** T
c5860 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
c5870 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
c5880 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 microseconds of
c5890 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a sleep actually.
c58a0 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f ** requested fro
c58b0 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 m the underlying
c58c0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
c58d0 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 m, a number whic
c58e0 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 h.** might be gr
c58f0 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
c5900 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d ual to the argum
c5910 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 ent, but not les
c5920 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 s.** than the ar
c5930 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 gument..*/.stati
c5940 63 20 69 6e 74 20 6f 73 32 53 6c 65 65 70 28 20 c int os2Sleep(
c5950 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c5960 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 20 s, int microsec
c5970 29 7b 0a 20 20 44 6f 73 53 6c 65 65 70 28 20 28 ){. DosSleep( (
c5980 6d 69 63 72 6f 73 65 63 2f 31 30 30 30 29 20 29 microsec/1000) )
c5990 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f ;. return micro
c59a0 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 sec;.}../*.** Th
c59b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
c59c0 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 able, if set to
c59d0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
c59e0 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 , becomes the re
c59f0 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 sult.** returned
c5a00 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 from sqlite3OsC
c5a10 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 urrentTime(). T
c5a20 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
c5a30 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 testing..*/.#ifd
c5a40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
c5a50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
c5a60 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
c5a70 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a me = 0;.#endif..
c5a80 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 /*.** Find the c
c5a90 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 urrent time (in
c5aa0 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 Universal Coordi
c5ab0 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 nated Time). Wr
c5ac0 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 ite the.** curre
c5ad0 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 nt time and date
c5ae0 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 as a Julian Day
c5af0 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 number into *pr
c5b00 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 Now and.** retur
c5b10 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 n 0. Return 1 i
c5b20 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 f the time and d
c5b30 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f ate cannot be fo
c5b40 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 und..*/.int os2C
c5b50 75 72 72 65 6e 74 54 69 6d 65 28 20 73 71 6c 69 urrentTime( sqli
c5b60 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 te3_vfs *pVfs, d
c5b70 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 20 29 7b 0a ouble *prNow ){.
c5b80 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20 20 double now;.
c5b90 53 48 4f 52 54 20 6d 69 6e 75 74 65 3b 20 2f 2a SHORT minute; /*
c5ba0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 62 6c needs to be abl
c5bb0 65 20 74 6f 20 63 6f 70 65 20 77 69 74 68 20 6e e to cope with n
c5bc0 65 67 61 74 69 76 65 20 74 69 6d 65 7a 6f 6e 65 egative timezone
c5bd0 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 55 53 48 offset */. USH
c5be0 4f 52 54 20 73 65 63 6f 6e 64 2c 20 68 6f 75 72 ORT second, hour
c5bf0 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79 2c 20 ,. day,
c5c00 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20 20 44 month, year;. D
c5c10 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20 44 6f ATETIME dt;. Do
c5c20 73 47 65 74 44 61 74 65 54 69 6d 65 28 20 26 64 sGetDateTime( &d
c5c30 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20 3d 20 t );. second =
c5c40 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63 6f 6e (USHORT)dt.secon
c5c50 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d 20 28 ds;. minute = (
c5c60 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75 74 65 73 SHORT)dt.minutes
c5c70 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b 0a + dt.timezone;.
c5c80 20 20 68 6f 75 72 20 3d 20 28 55 53 48 4f 52 54 hour = (USHORT
c5c90 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20 64 61 79 )dt.hours;. day
c5ca0 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 64 61 = (USHORT)dt.da
c5cb0 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20 28 55 53 y;. month = (US
c5cc0 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a 20 HORT)dt.month;.
c5cd0 20 79 65 61 72 20 3d 20 28 55 53 48 4f 52 54 29 year = (USHORT)
c5ce0 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f 2a 20 43 dt.year;.. /* C
c5cf0 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66 72 6f 6d alculations from
c5d00 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 http://www.astr
c5d10 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 o.keele.ac.uk/~r
c5d20 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 no/Astronomy/hjd
c5d30 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74 74 70 3a .html. http:
c5d40 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c //www.astro.keel
c5d50 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 e.ac.uk/~rno/Ast
c5d60 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e 63 ronomy/hjd-0.1.c
c5d70 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 */. /* Calcula
c5d80 74 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 64 61 te the Julian da
c5d90 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64 61 ys */. now = da
c5da0 79 20 2d 20 33 32 30 37 36 20 2b 0a 20 20 20 20 y - 32076 +.
c5db0 31 34 36 31 2a 28 79 65 61 72 20 2b 20 34 38 30 1461*(year + 480
c5dc0 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 0 + (month - 14)
c5dd0 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20 33 36 37 /12)/4 +. 367
c5de0 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d 20 28 6d *(month - 2 - (m
c5df0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 2a 31 32 onth - 14)/12*12
c5e00 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a 28 28 79 )/12 -. 3*((y
c5e10 65 61 72 20 2b 20 34 39 30 30 20 2b 20 28 6d 6f ear + 4900 + (mo
c5e20 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 31 30 nth - 14)/12)/10
c5e30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 0)/4;.. /* Add
c5e40 74 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 20 68 the fractional h
c5e50 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e 64 20 73 ours, mins and s
c5e60 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77 20 econds */. now
c5e70 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32 2e 30 29 += (hour + 12.0)
c5e80 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 /24.0;. now +=
c5e90 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30 3b 0a 20 minute/1440.0;.
c5ea0 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e 64 2f 38 now += second/8
c5eb0 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f 77 6400.0;. *prNow
c5ec0 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65 66 20 53 = now;.#ifdef S
c5ed0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
c5ee0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
c5ef0 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 _time ){. *pr
c5f00 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 Now = sqlite3_cu
c5f10 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 rrent_time/86400
c5f20 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a .0 + 2440587.5;.
c5f30 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
c5f40 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 urn 0;.}..static
c5f50 20 69 6e 74 20 6f 73 32 47 65 74 4c 61 73 74 45 int os2GetLastE
c5f60 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
c5f70 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
c5f80 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
c5f90 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
c5fa0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
c5fb0 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
c5fc0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
c5fd0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
c5fe0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
c5ff0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 t sqlite3_os_ini
c6000 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
c6010 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 73 c sqlite3_vfs os
c6020 32 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 2Vfs = {. 1,
c6030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6040 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
c6050 20 20 20 73 69 7a 65 6f 66 28 6f 73 32 46 69 6c sizeof(os2Fil
c6060 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c e), /* szOsFil
c6070 65 20 2a 2f 0a 20 20 20 20 43 43 48 4d 41 58 50 e */. CCHMAXP
c6080 41 54 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 6d ATH, /* m
c6090 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 xPathname */.
c60a0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
c60b0 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a /* pNext */.
c60c0 20 20 20 20 22 6f 73 32 22 2c 20 20 20 20 20 20 "os2",
c60d0 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 /* zName
c60e0 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
c60f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70 /* pAp
c6100 70 44 61 74 61 20 2a 2f 0a 0a 20 20 20 20 6f 73 pData */.. os
c6110 32 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 2Open,
c6120 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20 /* xOpen */.
c6130 20 6f 73 32 44 65 6c 65 74 65 2c 20 20 20 20 20 os2Delete,
c6140 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a /* xDelete *
c6150 2f 0a 20 20 20 20 6f 73 32 41 63 63 65 73 73 2c /. os2Access,
c6160 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63 /* xAcc
c6170 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 46 75 ess */. os2Fu
c6180 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a llPathname, /*
c6190 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a xFullPathname *
c61a0 2f 0a 20 20 20 20 6f 73 32 44 6c 4f 70 65 6e 2c /. os2DlOpen,
c61b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f /* xDlO
c61c0 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c pen */. os2Dl
c61d0 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a Error, /*
c61e0 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 xDlError */.
c61f0 20 6f 73 32 44 6c 53 79 6d 2c 20 20 20 20 20 20 os2DlSym,
c6200 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f /* xDlSym */
c6210 0a 20 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c . os2DlClose,
c6220 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c /* xDlCl
c6230 6f 73 65 20 2a 2f 0a 20 20 20 20 6f 73 32 52 61 ose */. os2Ra
c6240 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a ndomness, /*
c6250 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a xRandomness */.
c6260 20 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20 20 os2Sleep,
c6270 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 /* xSleep
c6280 20 2a 2f 0a 20 20 20 20 6f 73 32 43 75 72 72 65 */. os2Curre
c6290 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43 ntTime, /* xC
c62a0 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 urrentTime */.
c62b0 20 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 6f os2GetLastErro
c62c0 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 r /* xGetLast
c62d0 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20 Error */. };.
c62e0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
c62f0 73 74 65 72 28 26 6f 73 32 56 66 73 2c 20 31 29 ster(&os2Vfs, 1)
c6300 3b 0a 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a ;. initUconvObj
c6310 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72 6e ects();. return
c6320 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 SQLITE_OK;.}.SQ
c6330 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c6340 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
c6350 29 7b 0a 20 20 66 72 65 65 55 63 6f 6e 76 4f 62 ){. freeUconvOb
c6360 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72 jects();. retur
c6370 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
c6380 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
c6390 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a _OS_OS2 */../***
c63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
c63b0 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a of os_os2.c ****
c63c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c63d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c63e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
c63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
c6400 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e 63 n file os_unix.c
c6410 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
c6440 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a * 2004 May 22.**
c6450 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
c6460 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
c6470 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
c6480 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
c6490 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
c64a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
c64b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
c64c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
c64d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
c64e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
c64f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
c6500 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
c6510 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
c6520 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
c6530 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
c6540 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
c6550 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
c6560 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
c6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c65a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c65b0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
c65c0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 56 e contains the V
c65d0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
c65e0 6e 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 n for unix-like
c65f0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
c6600 73 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 4c 69 6e s.** include Lin
c6610 75 78 2c 20 4d 61 63 4f 53 58 2c 20 2a 42 53 44 ux, MacOSX, *BSD
c6620 2c 20 51 4e 58 2c 20 56 78 57 6f 72 6b 73 2c 20 , QNX, VxWorks,
c6630 41 49 58 2c 20 48 50 55 58 2c 20 61 6e 64 20 6f AIX, HPUX, and o
c6640 74 68 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 thers..**.** The
c6650 72 65 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20 re are actually
c6660 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e several differen
c6670 74 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 t VFS implementa
c6680 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69 tions in this fi
c6690 6c 65 2e 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 le..** The diffe
c66a0 72 65 6e 63 65 73 20 61 72 65 20 69 6e 20 74 68 rences are in th
c66b0 65 20 77 61 79 20 74 68 61 74 20 66 69 6c 65 20 e way that file
c66c0 6c 6f 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e locking is done.
c66d0 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a The default.**
c66e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
c66f0 75 73 65 73 20 50 6f 73 69 78 20 41 64 76 69 73 uses Posix Advis
c6700 6f 72 79 20 4c 6f 63 6b 73 2e 20 20 41 6c 74 65 ory Locks. Alte
c6710 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e rnative implemen
c6720 74 61 74 69 6f 6e 73 0a 2a 2a 20 75 73 65 20 66 tations.** use f
c6730 6c 6f 63 6b 28 29 2c 20 64 6f 74 2d 66 69 6c 65 lock(), dot-file
c6740 73 2c 20 76 61 72 69 6f 75 73 20 70 72 6f 70 72 s, various propr
c6750 69 65 74 61 72 79 20 6c 6f 63 6b 69 6e 67 20 73 ietary locking s
c6760 63 68 65 6d 61 73 2c 20 6f 72 20 73 69 6d 70 6c chemas, or simpl
c6770 79 0a 2a 2a 20 73 6b 69 70 20 6c 6f 63 6b 69 6e y.** skip lockin
c6780 67 20 61 6c 6c 20 74 6f 67 65 74 68 65 72 2e 0a g all together..
c6790 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 6f 75 72 63 **.** This sourc
c67a0 65 20 66 69 6c 65 20 69 73 20 6f 72 67 61 6e 69 e file is organi
c67b0 7a 65 64 20 69 6e 74 6f 20 64 69 76 69 73 69 6f zed into divisio
c67c0 6e 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f 67 ns where the log
c67d0 69 63 20 66 6f 72 20 76 61 72 69 6f 75 73 0a 2a ic for various.*
c67e0 2a 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 69 * subfunctions i
c67f0 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 s contained with
c6800 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 in the appropria
c6810 74 65 20 64 69 76 69 73 69 6f 6e 2e 20 20 50 4c te division. PL
c6820 45 41 53 45 0a 2a 2a 20 4b 45 45 50 20 54 48 45 EASE.** KEEP THE
c6830 20 53 54 52 55 43 54 55 52 45 20 4f 46 20 54 48 STRUCTURE OF TH
c6840 49 53 20 46 49 4c 45 20 49 4e 54 41 43 54 2e 20 IS FILE INTACT.
c6850 20 4e 65 77 20 63 6f 64 65 20 73 68 6f 75 6c 64 New code should
c6860 20 62 65 20 70 6c 61 63 65 64 0a 2a 2a 20 69 6e be placed.** in
c6870 20 74 68 65 20 63 6f 72 72 65 63 74 20 64 69 76 the correct div
c6880 69 73 69 6f 6e 20 61 6e 64 20 73 68 6f 75 6c 64 ision and should
c6890 20 62 65 20 63 6c 65 61 72 6c 79 20 6c 61 62 65 be clearly labe
c68a0 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c led..**.** The l
c68b0 61 79 6f 75 74 20 6f 66 20 64 69 76 69 73 69 6f ayout of divisio
c68c0 6e 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ns is as follows
c68d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 47 65 6e :.**.** * Gen
c68e0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 64 65 63 eral-purpose dec
c68f0 6c 61 72 61 74 69 6f 6e 73 20 61 6e 64 20 75 74 larations and ut
c6900 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 2e ility functions.
c6910 0a 2a 2a 20 20 20 2a 20 20 55 6e 69 71 75 65 20 .** * Unique
c6920 66 69 6c 65 20 49 44 20 6c 6f 67 69 63 20 75 73 file ID logic us
c6930 65 64 20 62 79 20 56 78 57 6f 72 6b 73 2e 0a 2a ed by VxWorks..*
c6940 2a 20 20 20 2a 20 20 56 61 72 69 6f 75 73 20 6c * * Various l
c6950 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 ocking primitive
c6960 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
c6970 20 28 61 6c 6c 20 65 78 63 65 70 74 20 70 72 6f (all except pro
c6980 78 79 20 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a 20 xy locking):.**
c6990 20 20 20 20 20 2b 20 66 6f 72 20 50 6f 73 69 78 + for Posix
c69a0 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 0a Advisory Locks.
c69b0 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 6f ** + for no
c69c0 2d 6f 70 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 -op locks.**
c69d0 20 20 2b 20 66 6f 72 20 64 6f 74 2d 66 69 6c 65 + for dot-file
c69e0 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b locks.** +
c69f0 20 66 6f 72 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 for flock() loc
c6a00 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 2b 20 66 king.** + f
c6a10 6f 72 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f or named semapho
c6a20 72 65 20 6c 6f 63 6b 73 20 28 56 78 57 6f 72 6b re locks (VxWork
c6a30 73 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 20 20 s only).**
c6a40 2b 20 66 6f 72 20 41 46 50 20 66 69 6c 65 73 79 + for AFP filesy
c6a50 73 74 65 6d 20 6c 6f 63 6b 73 20 28 4d 61 63 4f stem locks (MacO
c6a60 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 SX only).** *
c6a70 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 sqlite3_file me
c6a80 74 68 6f 64 73 20 6e 6f 74 20 61 73 73 6f 63 69 thods not associ
c6a90 61 74 65 64 20 77 69 74 68 20 6c 6f 63 6b 69 6e ated with lockin
c6aa0 67 2e 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e g..** * Defin
c6ab0 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 itions of sqlite
c6ac0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
c6ad0 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 ects for all loc
c6ae0 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 king.** met
c6af0 68 6f 64 73 20 70 6c 75 73 20 22 66 69 6e 64 65 hods plus "finde
c6b00 72 22 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 r" functions for
c6b10 20 65 61 63 68 20 6c 6f 63 6b 69 6e 67 20 6d 65 each locking me
c6b20 74 68 6f 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 thod..** * sq
c6b30 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 lite3_vfs method
c6b40 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
c6b50 2e 0a 2a 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 6e ..** * Lockin
c6b60 67 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72 g primitives for
c6b70 20 74 68 65 20 70 72 6f 78 79 20 75 62 65 72 2d the proxy uber-
c6b80 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e 20 locking-method.
c6b90 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a (MacOSX only).**
c6ba0 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e * Definition
c6bb0 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 73 s of sqlite3_vfs
c6bc0 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c objects for all
c6bd0 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 locking methods
c6be0 0a 2a 2a 20 20 20 20 20 20 70 6c 75 73 20 69 6d .** plus im
c6bf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
c6c00 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
c6c10 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f () and sqlite3_o
c6c20 73 5f 65 6e 64 28 29 2e 0a 2a 2f 0a 23 69 66 20 s_end()..*/.#if
c6c30 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 SQLITE_OS_UNIX
c6c40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
c6c50 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 his file is used
c6c60 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f on unix only */
c6c70 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 ../*.** There ar
c6c80 65 20 76 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 e various method
c6c90 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 s for file locki
c6ca0 6e 67 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 ng used for conc
c6cb0 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 urrency.** contr
c6cc0 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 ol:.**.** 1. P
c6cd0 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 OSIX locking (th
c6ce0 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 e default),.**
c6cf0 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2. No locking,.
c6d00 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 ** 3. Dot-file
c6d10 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 locking,.** 4
c6d20 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e . flock() lockin
c6d30 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c g,.** 5. AFP l
c6d40 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 ocking (OSX only
c6d50 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 ),.** 6. Named
c6d60 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 POSIX semaphore
c6d70 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 s (VXWorks only)
c6d80 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 ,.** 7. proxy
c6d90 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e locking. (OSX on
c6da0 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 ly).**.** Styles
c6db0 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 61 72 65 4, 5, and 7 are
c6dc0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
c6dd0 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 of SQLITE_ENABLE
c6de0 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a _LOCKING_STYLE.*
c6df0 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 * is defined to
c6e00 31 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 45 1. The SQLITE_E
c6e10 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
c6e20 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 YLE also enables
c6e30 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 automatic.** se
c6e40 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 lection of the a
c6e50 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 ppropriate locki
c6e60 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f ng style based o
c6e70 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d n the filesystem
c6e80 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 64 61 .** where the da
c6e90 74 61 62 61 73 65 20 69 73 20 6c 6f 63 61 74 65 tabase is locate
c6ea0 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 d. .*/.#if !def
c6eb0 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
c6ec0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
c6ed0 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 ).# if defined(
c6ee0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 __APPLE__).#
c6ef0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e define SQLITE_EN
c6f00 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c6f10 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 LE 1.# else.#
c6f20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
c6f30 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
c6f40 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a TYLE 0.# endif.
c6f50 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 #endif../*.** De
c6f60 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 58 57 4f fine the OS_VXWO
c6f70 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 73 6f RKS pre-processo
c6f80 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 66 20 r macro to 1 if
c6f90 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 building on .**
c6fa0 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 vxworks, or 0 ot
c6fb0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e herwise..*/.#ifn
c6fc0 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 def OS_VXWORKS.#
c6fd0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 if defined(__R
c6fe0 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 TP__) || defined
c6ff0 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 (_WRS_KERNEL).#
c7000 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 define OS_VXW
c7010 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 ORKS 1.# else.#
c7020 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 define OS_VX
c7030 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 WORKS 0.# endif
c7040 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
c7050 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 hese #defines sh
c7060 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 ould enable >2GB
c7070 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e file support on
c7080 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a Posix if the.**
c7090 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
c70a0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 ating system sup
c70b0 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 ports it. If th
c70c0 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 e OS lacks.** la
c70d0 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
c70e0 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 , these should b
c70f0 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
c7100 4c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f Large file suppo
c7110 72 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c rt can be disabl
c7120 65 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 ed using the -DS
c7130 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 QLITE_DISABLE_LF
c7140 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 S switch.** on t
c7150 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d he compiler comm
c7160 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 and line. This
c7170 69 73 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 is necessary if
c7180 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e you are compilin
c7190 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 g.** on a recent
c71a0 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 machine (ex: Re
c71b0 64 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f dHat 7.2) but yo
c71c0 75 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 u want your code
c71d0 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 to work.** on a
c71e0 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 n older machine
c71f0 28 65 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 (ex: RedHat 6.0)
c7200 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
c7210 65 20 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a e on RedHat 7.2.
c7220 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 ** without this
c7230 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 option, LFS is e
c7240 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 nable. But LFS
c7250 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 does not exist i
c7260 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 n the kernel.**
c7270 69 6e 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 in RedHat 6.0, s
c7280 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 o the code won't
c7290 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 work. Hence, f
c72a0 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 or maximum binar
c72b0 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 y.** portability
c72c0 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 you should omit
c72d0 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 LFS..**.** The
c72e0 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61 previous paragra
c72f0 70 68 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 ph was written i
c7300 6e 20 32 30 30 35 2e 20 20 28 54 68 69 73 20 70 n 2005. (This p
c7310 61 72 61 67 72 61 70 68 20 69 73 20 77 72 69 74 aragraph is writ
c7320 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 ten.** on 2008-1
c7330 31 2d 32 38 2e 29 20 54 68 65 73 65 20 64 61 79 1-28.) These day
c7340 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 s, all Linux ker
c7350 6e 65 6c 73 20 73 75 70 70 6f 72 74 20 6c 61 72 nels support lar
c7360 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 ge files, so.**
c7370 79 6f 75 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 you should proba
c7380 62 6c 79 20 6c 65 61 76 65 20 4c 46 53 20 65 6e bly leave LFS en
c7390 61 62 6c 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 abled. But some
c73a0 20 65 6d 62 65 64 64 65 64 20 70 6c 61 74 66 6f embedded platfo
c73b0 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 rms might.** lac
c73c0 6b 20 4c 46 53 20 69 6e 20 77 68 69 63 68 20 63 k LFS in which c
c73d0 61 73 65 20 74 68 65 20 53 51 4c 49 54 45 5f 44 ase the SQLITE_D
c73e0 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f ISABLE_LFS macro
c73f0 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 62 65 20 might still be
c7400 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 useful..*/.#ifnd
c7410 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
c7420 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 20 5f E_LFS.# define _
c7430 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 20 20 LARGE_FILE
c7440 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 1.# ifndef _FIL
c7450 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a 23 20 E_OFFSET_BITS.#
c7460 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f define _FILE_O
c7470 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a 23 20 FFSET_BITS 64.#
c7480 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 5f endif.# define _
c7490 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 LARGEFILE_SOURCE
c74a0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
c74b0 20 73 74 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 standard includ
c74c0 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 e files..*/.#inc
c74d0 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e lude <sys/types.
c74e0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 h>.#include <sys
c74f0 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 /stat.h>.#includ
c7500 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 e <fcntl.h>.#inc
c7510 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a lude <unistd.h>.
c7520 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 #include <sys/ti
c7530 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c me.h>.#include <
c7540 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 errno.h>..#if SQ
c7550 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c7560 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c ING_STYLE.# incl
c7570 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 ude <sys/ioctl.h
c7580 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b >.# if OS_VXWORK
c7590 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 S.# include <se
c75a0 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e maphore.h>.# in
c75b0 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e clude <limits.h>
c75c0 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 .# else.# inclu
c75d0 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a de <sys/file.h>.
c75e0 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f # include <sys/
c75f0 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c param.h>.# incl
c7600 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 ude <sys/mount.h
c7610 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 >.# endif.#endif
c7620 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
c7630 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
c7640 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 */../*.** If we
c7650 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 are to be thread
c7660 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 -safe, include t
c7670 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64 he pthreads head
c7680 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a er and define.**
c7690 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 the SQLITE_UNIX
c76a0 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a _THREADS macro..
c76b0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c76c0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
c76d0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
c76e0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
c76f0 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 /*.** Default pe
c7700 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 rmissions when c
c7710 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 reating a new fi
c7720 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 le.*/.#ifndef SQ
c7730 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
c7740 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 E_PERMISSIONS.#
c7750 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
c7760 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
c7770 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 SSIONS 0644.#end
c7780 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 if../*. ** Defau
c7790 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 lt permissions w
c77a0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74 hen creating aut
c77b0 6f 20 70 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a o proxy dir. */.
c77c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
c77d0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
c77e0 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 PERMISSIONS.# de
c77f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
c7800 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 ULT_PROXYDIR_PER
c7810 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 MISSIONS 0755.#e
c7820 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 ndif../*.** Maxi
c7830 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 mum supported pa
c7840 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 th-length..*/.#d
c7850 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 efine MAX_PATHNA
c7860 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e ME 512../*.** On
c7870 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74 45 ly set the lastE
c7880 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f rrno if the erro
c7890 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c r code is a real
c78a0 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a error and not .
c78b0 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 ** a normal expe
c78c0 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65 cted return code
c78d0 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 of SQLITE_BUSY
c78e0 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a or SQLITE_OK.*/.
c78f0 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f #define IS_LOCK_
c7900 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d ERROR(x) ((x !=
c7910 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 SQLITE_OK) && (
c7920 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 x != SQLITE_BUSY
c7930 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 )).../*.** Somet
c7940 69 6d 65 73 2c 20 61 66 74 65 72 20 61 20 66 69 imes, after a fi
c7950 6c 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f le handle is clo
c7960 73 65 64 20 62 79 20 53 51 4c 69 74 65 2c 20 74 sed by SQLite, t
c7970 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
c7980 6f 72 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 or.** cannot be
c7990 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69 61 74 65 closed immediate
c79a0 6c 79 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 ly. In these cas
c79b0 65 73 2c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 es, instances of
c79c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a the following.*
c79d0 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 * structure are
c79e0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
c79f0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
c7a00 72 20 77 68 69 6c 65 20 77 61 69 74 69 6e 67 20 r while waiting
c7a10 66 6f 72 20 61 6e 0a 2a 2a 20 6f 70 70 6f 72 74 for an.** opport
c7a20 75 6e 69 74 79 20 74 6f 20 65 69 74 68 65 72 20 unity to either
c7a30 63 6c 6f 73 65 20 6f 72 20 72 65 75 73 65 20 69 close or reuse i
c7a40 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 t..*/.typedef st
c7a50 72 75 63 74 20 55 6e 69 78 55 6e 75 73 65 64 46 ruct UnixUnusedF
c7a60 64 20 55 6e 69 78 55 6e 75 73 65 64 46 64 3b 0a d UnixUnusedFd;.
c7a70 73 74 72 75 63 74 20 55 6e 69 78 55 6e 75 73 65 struct UnixUnuse
c7a80 64 46 64 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 dFd {. int fd;
c7a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7aa0 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
c7ab0 70 74 6f 72 20 74 6f 20 63 6c 6f 73 65 20 2a 2f ptor to close */
c7ac0 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 . int flags;
c7ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c7ae0 46 6c 61 67 73 20 74 68 69 73 20 66 69 6c 65 20 Flags this file
c7af0 64 65 73 63 72 69 70 74 6f 72 20 77 61 73 20 6f descriptor was o
c7b00 70 65 6e 65 64 20 77 69 74 68 20 2a 2f 0a 20 20 pened with */.
c7b10 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e UnixUnusedFd *pN
c7b20 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
c7b30 74 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65 t unused file de
c7b40 73 63 72 69 70 74 6f 72 20 6f 6e 20 73 61 6d 65 scriptor on same
c7b50 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a file */.};../*.
c7b60 2a 2a 20 54 68 65 20 75 6e 69 78 46 69 6c 65 20 ** The unixFile
c7b70 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62 structure is sub
c7b80 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 class of sqlite3
c7b90 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 74 _file specific t
c7ba0 6f 20 74 68 65 20 75 6e 69 78 0a 2a 2a 20 56 46 o the unix.** VF
c7bb0 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e S implementation
c7bc0 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
c7bd0 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 75 6e ruct unixFile un
c7be0 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 75 ixFile;.struct u
c7bf0 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 nixFile {. sqli
c7c00 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 te3_io_methods c
c7c10 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b 20 20 onst *pMethod;
c7c20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 /* Always the fi
c7c30 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 rst entry */. s
c7c40 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
c7c50 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 20 20 t *pOpen;
c7c60 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 61 6c /* Info about al
c7c70 6c 20 6f 70 65 6e 20 66 64 27 73 20 6f 6e 20 74 l open fd's on t
c7c80 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73 his inode */. s
c7c90 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
c7ca0 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 fo *pLock;
c7cb0 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f /* Info about lo
c7cc0 63 6b 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 cks on this inod
c7cd0 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 e */. int h;
c7ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
c7d00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
c7d10 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 3b 20 20 /. int dirfd;
c7d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7d30 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 /* File des
c7d40 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 criptor for the
c7d50 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 75 directory */. u
c7d60 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 nsigned char loc
c7d70 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 ktype;
c7d80 2f 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 6c /* The type of l
c7d90 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 ock held on this
c7da0 20 66 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 fd */. int las
c7db0 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 tErrno;
c7dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c7dd0 20 75 6e 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d unix errno from
c7de0 20 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 the last I/O er
c7df0 72 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c ror */. void *l
c7e00 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 ockingContext;
c7e10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 /* Loc
c7e20 6b 69 6e 67 20 73 74 79 6c 65 20 73 70 65 63 69 king style speci
c7e30 66 69 63 20 73 74 61 74 65 20 2a 2f 0a 20 20 55 fic state */. U
c7e40 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e nixUnusedFd *pUn
c7e50 75 73 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 used;
c7e60 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 /* Pre-allocated
c7e70 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2f UnixUnusedFd */
c7e80 0a 20 20 69 6e 74 20 66 69 6c 65 46 6c 61 67 73 . int fileFlags
c7e90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c7ea0 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e /* Miscellan
c7eb0 6f 75 73 20 66 6c 61 67 73 20 2a 2f 0a 23 69 66 ous flags */.#if
c7ec0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c7ed0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69 OCKING_STYLE. i
c7ee0 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 nt openFlags;
c7ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7f00 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 73 70 65 /* The flags spe
c7f10 63 69 66 69 65 64 20 61 74 20 6f 70 65 6e 28 29 cified at open()
c7f20 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 */.#endif.#if S
c7f30 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
c7f40 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
c7f50 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 nux__). pthread
c7f60 5f 74 20 74 69 64 3b 20 20 20 20 20 20 20 20 20 _t tid;
c7f70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c7f80 20 74 68 72 65 61 64 20 74 68 61 74 20 22 6f 77 thread that "ow
c7f90 6e 73 22 20 74 68 69 73 20 75 6e 69 78 46 69 6c ns" this unixFil
c7fa0 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 e */.#endif.#if
c7fb0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 6e 74 OS_VXWORKS. int
c7fc0 20 69 73 44 65 6c 65 74 65 3b 20 20 20 20 20 20 isDelete;
c7fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c7fe0 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 Delete on close
c7ff0 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 73 74 if true */. st
c8000 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
c8010 49 64 20 2a 70 49 64 3b 20 20 20 20 20 20 20 2f Id *pId; /
c8020 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 * Unique file ID
c8030 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 */.#endif.#ifnd
c8040 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 ef NDEBUG. /* T
c8050 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 he next group of
c8060 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 75 variables are u
c8070 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77 68 65 sed to track whe
c8080 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a ther or not the.
c8090 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
c80a0 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79 74 65 counter in byte
c80b0 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74 61 62 s 24-27 of datab
c80c0 61 73 65 20 66 69 6c 65 73 20 61 72 65 20 75 70 ase files are up
c80d0 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65 6e 65 dated. ** whene
c80e0 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f 66 20 ver any part of
c80f0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 68 61 the database cha
c8100 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65 72 74 nges. An assert
c8110 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 0a 20 ion fault will.
c8120 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61 20 66 ** occur if a f
c8130 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 77 ile is updated w
c8140 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70 64 61 ithout also upda
c8150 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 ting the transac
c8160 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 tion. ** counte
c8170 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73 r. This test is
c8180 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64 20 6e made to avoid n
c8190 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69 6d 69 ew problems simi
c81a0 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 lar to the. **
c81b0 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20 62 79 one described by
c81c0 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e 20 0a ticket #3584. .
c81d0 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 */. unsigned
c81e0 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72 43 68 char transCntrCh
c81f0 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ng; /* True if
c8200 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
c8210 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 64 counter changed
c8220 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
c8230 68 61 72 20 64 62 55 70 64 61 74 65 3b 20 20 20 har dbUpdate;
c8240 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
c8250 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61 74 61 any part of data
c8260 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
c8270 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 d */. unsigned
c8280 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57 72 69 char inNormalWri
c8290 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 te; /* True if
c82a0 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 in a normal wri
c82b0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a te operation */.
c82c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
c82d0 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 LITE_TEST. /* I
c82e0 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 n test mode, inc
c82f0 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f rease the size o
c8300 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
c8310 20 61 20 62 69 74 20 73 6f 20 74 68 61 74 20 0a a bit so that .
c8320 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 67 65 ** it is large
c8330 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 75 63 r than the struc
c8340 74 20 43 72 61 73 68 46 69 6c 65 20 64 65 66 69 t CrashFile defi
c8350 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a ned in test6.c..
c8360 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 61 64 */. char aPad
c8370 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 ding[32];.#endif
c8380 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
c8390 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 ollowing macros
c83a0 64 65 66 69 6e 65 20 62 69 74 73 20 69 6e 20 75 define bits in u
c83b0 6e 69 78 46 69 6c 65 2e 66 69 6c 65 46 6c 61 67 nixFile.fileFlag
c83c0 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s.*/.#define SQL
c83d0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
c83e0 4f 43 4b 49 4e 47 20 20 30 78 30 30 30 31 20 20 OCKING 0x0001
c83f0 20 2f 2a 20 55 73 65 20 77 68 6f 6c 65 2d 66 69 /* Use whole-fi
c8400 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f le locking */../
c8410 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 *.** Include cod
c8420 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
c8430 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 to all os_*.c f
c8440 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a iles.*/./*******
c8450 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
c8460 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 os_common.h in t
c8470 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
c8480 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a unix.c *********
c8490 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
c84a0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
c84b0 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a le os_common.h *
c84c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c84d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c84e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
c84f0 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 04 May 22.**.**
c8500 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
c8510 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
c8520 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
c8530 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
c8540 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
c8550 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
c8560 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
c8570 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
c8580 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
c8590 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
c85a0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
c85b0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
c85c0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
c85d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
c85e0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
c85f0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
c8600 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
c8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
c8660 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
c8670 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e ntains macros an
c8680 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f d a little bit o
c8690 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 f code that is c
c86a0 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 ommon to.** all
c86b0 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d of the platform-
c86c0 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 specific files (
c86d0 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 os_*.c) and is #
c86e0 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 included into th
c86f0 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a ose.** files..**
c8700 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 .** This file sh
c8710 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 ould be #include
c8720 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 d by the os_*.c
c8730 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 files only. It
c8740 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 is not a.** gene
c8750 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 ral purpose head
c8760 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e er file..*/.#ifn
c8770 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 def _OS_COMMON_H
c8780 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f _.#define _OS_CO
c8790 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 MMON_H_../*.** A
c87a0 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 t least two bugs
c87b0 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e have slipped in
c87c0 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e because we chan
c87d0 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 ged the MEMORY_D
c87e0 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f EBUG.** macro to
c87f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e SQLITE_DEBUG an
c8800 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b d some older mak
c8810 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 efiles have not
c8820 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 yet made the.**
c8830 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c switch. The fol
c8840 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 lowing code shou
c8850 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72 ld catch this pr
c8860 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 oblem at compile
c8870 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 -time..*/.#ifdef
c8880 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 MEMORY_DEBUG.#
c8890 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 error "The MEMOR
c88a0 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 Y_DEBUG macro is
c88b0 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 obsolete. Use
c88c0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 SQLITE_DEBUG ins
c88d0 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 tead.".#endif..#
c88e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
c88f0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
c8900 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 E int sqlite3OST
c8910 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e race = 0;.#defin
c8920 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 e OSTRACE1(X)
c8930 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
c8940 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
c8950 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 e3DebugPrintf(X)
c8960 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8970 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 2(X,Y) if(
c8980 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
c8990 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c89a0 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e intf(X,Y).#defin
c89b0 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a e OSTRACE3(X,Y,Z
c89c0 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ) if( sqlite
c89d0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
c89e0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
c89f0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
c8a00 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 RACE4(X,Y,Z,A)
c8a10 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
c8a20 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
c8a30 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
c8a40 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8a50 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 E5(X,Y,Z,A,B) if
c8a60 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
c8a70 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
c8a80 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 rintf(X,Y,Z,A,B)
c8a90 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8aa0 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 6(X,Y,Z,A,B,C) \
c8ab0 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f . if(sqlite3O
c8ac0 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 STrace) sqlite3D
c8ad0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
c8ae0 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 ,A,B,C).#define
c8af0 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 OSTRACE7(X,Y,Z,A
c8b00 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 ,B,C,D) \. if
c8b10 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 (sqlite3OSTrace)
c8b20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
c8b30 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c ntf(X,Y,Z,A,B,C,
c8b40 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 D).#else.#define
c8b50 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 OSTRACE1(X).#de
c8b60 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c fine OSTRACE2(X,
c8b70 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Y).#define OSTRA
c8b80 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 CE3(X,Y,Z).#defi
c8b90 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c ne OSTRACE4(X,Y,
c8ba0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
c8bb0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
c8bc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8bd0 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 6(X,Y,Z,A,B,C).#
c8be0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 define OSTRACE7(
c8bf0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 X,Y,Z,A,B,C,D).#
c8c00 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 endif../*.** Mac
c8c10 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 ros for performa
c8c20 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f nce tracing. No
c8c30 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 rmally turned of
c8c40 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a f. Only works.*
c8c50 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 * on i486 hardwa
c8c60 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 re..*/.#ifdef SQ
c8c70 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 LITE_PERFORMANCE
c8c80 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 _TRACE../* .** h
c8c90 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 wtime.h contains
c8ca0 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 inline assemble
c8cb0 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 r code for imple
c8cc0 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 menting .** high
c8cd0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d -performance tim
c8ce0 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f ing routines..*/
c8cf0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c8d00 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e Include hwtime.
c8d10 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
c8d20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a of os_common.h *
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c8d40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c8d50 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 Begin file hwti
c8d60 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
c8d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c8d90 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 ./*.** 2008 May
c8da0 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 27.**.** The aut
c8db0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
c8dc0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
c8dd0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
c8de0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
c8df0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
c8e00 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
c8e10 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
c8e20 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
c8e30 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
c8e40 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
c8e50 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
c8e60 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
c8e70 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
c8e80 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
c8e90 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
c8ea0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
c8eb0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
c8ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8f00 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
c8f10 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
c8f20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 inline asm code
c8f30 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 for retrieving "
c8f40 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 high-performance
c8f50 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f ".** counters fo
c8f60 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 r x86 class CPUs
c8f70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 ..*/.#ifndef _HW
c8f80 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 TIME_H_.#define
c8f90 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a _HWTIME_H_../*.*
c8fa0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
c8fb0 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
c8fc0 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c ks on pentium-cl
c8fd0 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 ass (or newer) p
c8fe0 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 rocessors..** It
c8ff0 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 uses the RDTSC
c9000 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 opcode to read t
c9010 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 he cycle count v
c9020 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a alue out of the.
c9030 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 ** processor and
c9040 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 returns that va
c9050 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 lue. This can b
c9060 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d e used for high-
c9070 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 res.** profiling
c9080 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 ..*/.#if (define
c9090 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 d(__GNUC__) || d
c90a0 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
c90b0 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 ) && \. (de
c90c0 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 fined(i386) || d
c90d0 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 efined(__i386__)
c90e0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 || defined(_M_I
c90f0 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 X86)).. #if def
c9100 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a ined(__GNUC__)..
c9110 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
c9120 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
c9130 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
c9140 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e unsigned in
c9150 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f t lo, hi;. _
c9160 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
c9170 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
c9180 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 =a" (lo), "=d" (
c9190 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 hi));. retur
c91a0 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 n (sqlite_uint64
c91b0 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a )hi << 32 | lo;.
c91c0 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 }.. #elif def
c91d0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a ined(_MSC_VER)..
c91e0 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b __declspec(nak
c91f0 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c ed) __inline sql
c9200 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 ite_uint64 __cde
c9210 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 cl sqlite3Hwtime
c9220 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 (void){. __a
c9230 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 sm {. rdt
c9240 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 sc. ret
c9250 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 ; return va
c9260 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 lue at EDX:EAX.
c9270 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e }. }.. #en
c9280 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 dif..#elif (defi
c9290 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
c92a0 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
c92b0 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 4__)).. __inlin
c92c0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
c92d0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
c92e0 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
c92f0 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a igned long val;.
c9300 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
c9310 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
c9320 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 sc" : "=A" (val)
c9330 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
c9340 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 val;. }. .#elif
c9350 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
c9360 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
c9370 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e _ppc__)).. __in
c9380 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
c9390 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
c93a0 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
c93b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
c93c0 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 ng retval;.
c93d0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a unsigned long j
c93e0 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d unk;. __asm
c93f0 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
c9400 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 ("\n\.
c9410 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 1: mftbu
c9420 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
c9430 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 mftb
c9440 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %L0\n\.
c9450 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 mftbu
c9460 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %0\n\.
c9470 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 cmpw
c9480 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 %0,%1\n\.
c9490 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 bne
c94a0 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 1b".
c94b0 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 : "=r"
c94c0 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 (retval), "=r"
c94d0 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 (junk));. r
c94e0 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 eturn retval;.
c94f0 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 }..#else.. #err
c9500 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e or Need implemen
c9510 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
c9520 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
c9530 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 ur platform...
c9540 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 /*. ** To compi
c9550 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 le without imple
c9560 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 menting sqlite3H
c9570 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
c9580 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 platform,. **
c9590 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 you can remove t
c95a0 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 he above #error
c95b0 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c and use the foll
c95c0 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 owing. ** stub
c95d0 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 function. You w
c95e0 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 ill lose timing
c95f0 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 support for many
c9600 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 . ** of the deb
c9610 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 ugging and testi
c9620 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 ng utilities, bu
c9630 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 t it should at.
c9640 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c ** least compil
c9650 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a e and run.. */.
c9660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
c9670 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
c9680 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
c9690 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c d){ return ((sql
c96a0 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d ite_uint64)0); }
c96b0 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ..#endif..#endif
c96c0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 /* !defined(_HW
c96d0 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a TIME_H_) */../**
c96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
c96f0 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a of hwtime.h ***
c9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
c9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
c9740 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
c9750 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
c9760 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 ***********/..st
c9780 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
c9790 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 64 g_start;.stat
c97a0 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ic sqlite_uint64
c97b0 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 g_elapsed;.#def
c97c0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 ine TIMER_START
c97d0 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 g_start=sq
c97e0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 lite3Hwtime().#d
c97f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 efine TIMER_END
c9800 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 g_elapse
c9810 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 d=sqlite3Hwtime(
c9820 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e )-g_start.#defin
c9830 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
c9840 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 g_elapsed.#e
c9850 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 lse.#define TIME
c9860 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 R_START.#define
c9870 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e TIMER_END.#defin
c9880 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
c9890 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e ((sqlite_uin
c98a0 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f t64)0).#endif../
c98b0 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 *.** If we compi
c98c0 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 le with the SQLI
c98d0 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 TE_TEST macro se
c98e0 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c t, then the foll
c98f0 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f owing block.** o
c9900 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 f code will give
c9910 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 us the ability
c9920 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 to simulate a di
c9930 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 sk I/O error. T
c9940 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 his.** is used f
c9950 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 or testing the I
c9960 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 /O recovery logi
c9970 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c c..*/.#ifdef SQL
c9980 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
c9990 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c99a0 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 io_error_hit = 0
c99b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
c99c0 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
c99d0 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 I/O Errors */.SQ
c99e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c99f0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
c9a00 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 rdhit = 0;
c9a10 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e /* Number of n
c9a20 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 on-benign errors
c9a30 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
c9a40 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c9a50 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ror_pending = 0;
c9a60 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
c9a70 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 down to first I
c9a80 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 /O error */.SQLI
c9a90 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c9aa0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
c9ab0 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ist = 0;
c9ac0 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 /* True if I/O e
c9ad0 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f rrors persist */
c9ae0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
c9af0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c9b00 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 _benign = 0;
c9b10 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
c9b20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 errors are benig
c9b30 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 n */.SQLITE_API
c9b40 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b int sqlite3_disk
c9b50 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 full_pending = 0
c9b60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
c9b70 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c9b80 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 l = 0;.#define S
c9b90 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
c9ba0 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f nign(X) sqlite3_
c9bb0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d io_error_benign=
c9bc0 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (X).#define Simu
c9bd0 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 lateIOError(CODE
c9be0 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 ) \. if( (sqli
c9bf0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 te3_io_error_per
c9c00 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f sist && sqlite3_
c9c10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a io_error_hit) \.
c9c20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
c9c30 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
c9c40 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 ng-- == 1 ) \.
c9c50 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c { l
c9c60 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f ocal_ioerr(); CO
c9c70 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 DE; }.static voi
c9c80 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b d local_ioerr(){
c9c90 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 . IOTRACE(("IOE
c9ca0 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 RR\n"));. sqlit
c9cb0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b e3_io_error_hit+
c9cc0 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 +;. if( !sqlite
c9cd0 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
c9ce0 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 n ) sqlite3_io_e
c9cf0 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a rror_hardhit++;.
c9d00 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 }.#define Simula
c9d10 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
c9d20 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 CODE) \. if( s
c9d30 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c9d40 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 pending ){ \.
c9d50 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
c9d60 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
c9d70 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 = 1 ){ \.
c9d80 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c local_ioerr(); \
c9d90 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
c9da0 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a diskfull = 1; \.
c9db0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
c9dc0 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b o_error_hit = 1;
c9dd0 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 \. CODE;
c9de0 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a \. }else{ \.
c9df0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
c9e00 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d iskfull_pending-
c9e10 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 -; \. } \.
c9e20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }.#else.#define
c9e30 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
c9e40 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e Benign(X).#defin
c9e50 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
c9e60 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d r(A).#define Sim
c9e70 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
c9e80 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a or(A).#endif../*
c9e90 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 .** When testing
c9ea0 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f , keep a count o
c9eb0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
c9ec0 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 open files..*/.#
c9ed0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
c9ee0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
c9ef0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
c9f00 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 le_count = 0;.#d
c9f10 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 efine OpenCounte
c9f20 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 r(X) sqlite3_op
c9f30 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 en_file_count+=(
c9f40 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 X).#else.#define
c9f50 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a OpenCounter(X).
c9f60 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
c9f70 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 * !defined(_OS_C
c9f80 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a OMMON_H_) */../*
c9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
c9fa0 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 d of os_common.h
c9fb0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c9fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
c9ff0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
ca000 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 e left off in os
ca010 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a _unix.c ********
ca020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
ca030 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 76 61 72 69 *.** Define vari
ca040 6f 75 73 20 6d 61 63 72 6f 73 20 74 68 61 74 20 ous macros that
ca050 61 72 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d are missing from
ca060 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 0a 2a some systems..*
ca070 2f 0a 23 69 66 6e 64 65 66 20 4f 5f 4c 41 52 47 /.#ifndef O_LARG
ca080 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f EFILE.# define O
ca090 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e _LARGEFILE 0.#en
ca0a0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
ca0b0 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 E_DISABLE_LFS.#
ca0c0 75 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c undef O_LARGEFIL
ca0d0 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 E.# define O_LAR
ca0e0 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a GEFILE 0.#endif.
ca0f0 23 69 66 6e 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c #ifndef O_NOFOLL
ca100 4f 57 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4e 4f OW.# define O_NO
ca110 46 4f 4c 4c 4f 57 20 30 0a 23 65 6e 64 69 66 0a FOLLOW 0.#endif.
ca120 23 69 66 6e 64 65 66 20 4f 5f 42 49 4e 41 52 59 #ifndef O_BINARY
ca130 0a 23 20 64 65 66 69 6e 65 20 4f 5f 42 49 4e 41 .# define O_BINA
ca140 52 59 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RY 0.#endif../*.
ca150 2a 2a 20 54 68 65 20 44 4a 47 50 50 20 63 6f 6d ** The DJGPP com
ca160 70 69 6c 65 72 20 65 6e 76 69 72 6f 6e 6d 65 6e piler environmen
ca170 74 20 6c 6f 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c t looks mostly l
ca180 69 6b 65 20 55 6e 69 78 2c 20 62 75 74 20 69 74 ike Unix, but it
ca190 0a 2a 2a 20 6c 61 63 6b 73 20 74 68 65 20 66 63 .** lacks the fc
ca1a0 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c ntl() system cal
ca1b0 6c 2e 20 20 53 6f 20 72 65 64 65 66 69 6e 65 20 l. So redefine
ca1c0 66 63 6e 74 6c 28 29 20 74 6f 20 62 65 20 73 6f fcntl() to be so
ca1d0 6d 65 74 68 69 6e 67 0a 2a 2a 20 74 68 61 74 20 mething.** that
ca1e0 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e always succeeds.
ca1f0 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 This means tha
ca200 74 20 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 20 6e t locking does n
ca210 6f 74 20 6f 63 63 75 72 20 75 6e 64 65 72 0a 2a ot occur under.*
ca220 2a 20 44 4a 47 50 50 2e 20 20 42 75 74 20 69 74 * DJGPP. But it
ca230 20 69 73 20 44 4f 53 20 2d 20 77 68 61 74 20 64 is DOS - what d
ca240 69 64 20 79 6f 75 20 65 78 70 65 63 74 3f 0a 2a id you expect?.*
ca250 2f 0a 23 69 66 64 65 66 20 5f 5f 44 4a 47 50 50 /.#ifdef __DJGPP
ca260 5f 5f 0a 23 20 64 65 66 69 6e 65 20 66 63 6e 74 __.# define fcnt
ca270 6c 28 41 2c 42 2c 43 29 20 30 0a 23 65 6e 64 69 l(A,B,C) 0.#endi
ca280 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 68 72 f../*.** The thr
ca290 65 61 64 69 64 20 6d 61 63 72 6f 20 72 65 73 6f eadid macro reso
ca2a0 6c 76 65 73 20 74 6f 20 74 68 65 20 74 68 72 65 lves to the thre
ca2b0 61 64 2d 69 64 20 6f 72 20 74 6f 20 30 2e 20 20 ad-id or to 0.
ca2c0 55 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 Used for.** test
ca2d0 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e ing and debuggin
ca2e0 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 20 53 g only..*/.#if S
ca2f0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
ca300 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 .#define threadi
ca310 64 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 d pthread_self()
ca320 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 74 .#else.#define t
ca330 68 72 65 61 64 69 64 20 30 0a 23 65 6e 64 69 66 hreadid 0.#endif
ca340 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 .../*.** Helper
ca350 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 6f 62 74 functions to obt
ca360 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e 71 75 69 ain and relinqui
ca370 73 68 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 sh the global mu
ca380 74 65 78 2e 20 54 68 65 0a 2a 2a 20 67 6c 6f 62 tex. The.** glob
ca390 61 6c 20 6d 75 74 65 78 20 69 73 20 75 73 65 64 al mutex is used
ca3a0 20 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65 20 to protect the
ca3b0 75 6e 69 78 4f 70 65 6e 43 6e 74 2c 20 75 6e 69 unixOpenCnt, uni
ca3c0 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 0a 2a 2a xLockInfo and.**
ca3d0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f vxworksFileId o
ca3e0 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 74 bjects used by t
ca3f0 68 69 73 20 66 69 6c 65 2c 20 61 6c 6c 20 6f 66 his file, all of
ca400 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 0a 2a which may be .*
ca410 2a 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 * shared by mult
ca420 69 70 6c 65 20 74 68 72 65 61 64 73 2e 0a 2a 2a iple threads..**
ca430 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 75 6e 69 .** Function uni
ca440 78 4d 75 74 65 78 48 65 6c 64 28 29 20 69 73 20 xMutexHeld() is
ca450 75 73 65 64 20 74 6f 20 61 73 73 65 72 74 28 29 used to assert()
ca460 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c that the global
ca470 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 68 65 mutex .** is he
ca480 6c 64 20 77 68 65 6e 20 72 65 71 75 69 72 65 64 ld when required
ca490 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
ca4a0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 is only used as
ca4b0 70 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29 part of assert()
ca4c0 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e .** statements.
ca4d0 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 75 6e e.g..**.** un
ca4e0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 0a 2a ixEnterMutex().*
ca4f0 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 75 6e * assert( un
ca500 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b ixMutexHeld() );
ca510 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4c .** unixEnterL
ca520 65 61 76 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63 eave().*/.static
ca530 20 76 6f 69 64 20 75 6e 69 78 45 6e 74 65 72 4d void unixEnterM
ca540 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 utex(void){. sq
ca550 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
ca560 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c r(sqlite3MutexAl
ca570 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
ca580 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
ca590 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
ca5a0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 76 unixLeaveMutex(v
ca5b0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
ca5c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 mutex_leave(sqli
ca5d0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
ca5e0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
ca5f0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 69 C_MASTER));.}.#i
ca600 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
ca610 47 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 G.static int uni
ca620 78 4d 75 74 65 78 48 65 6c 64 28 76 6f 69 64 29 xMutexHeld(void)
ca630 20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 {. return sqli
ca640 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 te3_mutex_held(s
ca650 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
ca660 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
ca670 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d ATIC_MASTER));.}
ca680 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66 .#endif...#ifdef
ca690 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
ca6a0 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
ca6b0 69 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 ion for printing
ca6c0 20 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f 72 out trace infor
ca6d0 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 75 mation from debu
ca6e0 67 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 65 gging.** binarie
ca6f0 73 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 20 s. This returns
ca700 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 the string repre
ca710 73 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 setation of the
ca720 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 supplied.** inte
ca730 67 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a ger lock-type..*
ca740 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
ca750 68 61 72 20 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d har *locktypeNam
ca760 65 28 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b e(int locktype){
ca770 0a 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 . switch( lockt
ca780 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ype ){. case
ca790 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 NO_LOCK: return
ca7a0 22 4e 4f 4e 45 22 3b 0a 20 20 20 20 63 61 73 65 "NONE";. case
ca7b0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3a 20 72 65 SHARED_LOCK: re
ca7c0 74 75 72 6e 20 22 53 48 41 52 45 44 22 3b 0a 20 turn "SHARED";.
ca7d0 20 20 20 63 61 73 65 20 52 45 53 45 52 56 45 44 case RESERVED
ca7e0 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 52 _LOCK: return "R
ca7f0 45 53 45 52 56 45 44 22 3b 0a 20 20 20 20 63 61 ESERVED";. ca
ca800 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3a se PENDING_LOCK:
ca810 20 72 65 74 75 72 6e 20 22 50 45 4e 44 49 4e 47 return "PENDING
ca820 22 3b 0a 20 20 20 20 63 61 73 65 20 45 58 43 4c ";. case EXCL
ca830 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 75 USIVE_LOCK: retu
ca840 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b 0a rn "EXCLUSIVE";.
ca850 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 52 }. return "ER
ca860 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ROR";.}.#endif..
ca870 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f #ifdef SQLITE_LO
ca880 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50 CK_TRACE./*.** P
ca890 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 rint out informa
ca8a0 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c tion about all l
ca8b0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
ca8c0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
ca8d0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
ca8e0 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e r troubleshootin
ca8f0 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69 g locks on multi
ca900 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74 threaded.** plat
ca910 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62 forms. Enable b
ca920 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 y compiling with
ca930 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f the -DSQLITE_LO
ca940 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d CK_TRACE.** comm
ca950 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 and-line option
ca960 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e on the compiler.
ca970 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e This code is n
ca980 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65 ormally.** turne
ca990 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 d off..*/.static
ca9a0 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69 int lockTrace(i
ca9b0 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73 nt fd, int op, s
ca9c0 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b truct flock *p){
ca9d0 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65 . char *zOpName
ca9e0 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20 , *zType;. int
ca9f0 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72 s;. int savedEr
caa00 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46 rno;. if( op==F
caa10 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f _GETLK ){. zO
caa20 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b pName = "GETLK";
caa30 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d . }else if( op=
caa40 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20 =F_SETLK ){.
caa50 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b zOpName = "SETLK
caa60 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ";. }else{.
caa70 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 s = fcntl(fd, op
caa80 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , p);. sqlite
caa90 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 3DebugPrintf("fc
caaa0 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25 ntl unknown %d %
caab0 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c d %d\n", fd, op,
caac0 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 s);. return
caad0 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e s;. }. if( p->
caae0 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 l_type==F_RDLCK
caaf0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 ){. zType = "
cab00 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 RDLCK";. }else
cab10 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 if( p->l_type==F
cab20 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 _WRLCK ){. zT
cab30 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 ype = "WRLCK";.
cab40 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f }else if( p->l_
cab50 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b type==F_UNLCK ){
cab60 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e . zType = "UN
cab70 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 LCK";. }else{.
cab80 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
cab90 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d }. assert( p-
caba0 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f >l_whence==SEEK_
cabb0 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e SET );. s = fcn
cabc0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 tl(fd, op, p);.
cabd0 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72 savedErrno = er
cabe0 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 rno;. sqlite3De
cabf0 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c bugPrintf("fcntl
cac00 20 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20 %d %d %s %s %d
cac10 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 %d %d %d\n",.
cac20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20 threadid, fd,
cac30 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20 zOpName, zType,
cac40 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c (int)p->l_start,
cac50 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a (int)p->l_len,.
cac60 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70 (int)p->l_p
cac70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d id, s);. if( s=
cac80 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53 =(-1) && op==F_S
cac90 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79 ETLK && (p->l_ty
caca0 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70 pe==F_RDLCK || p
cacb0 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 ->l_type==F_WRLC
cacc0 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 K) ){. struct
cacd0 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c flock l2;. l
cace0 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74 2 = *p;. fcnt
cacf0 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 l(fd, F_GETLK, &
cad00 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e l2);. if( l2.
cad10 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 l_type==F_RDLCK
cad20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d ){. zType =
cad30 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 "RDLCK";. }e
cad40 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 lse if( l2.l_typ
cad50 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 e==F_WRLCK ){.
cad60 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c zType = "WRL
cad70 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 CK";. }else i
cad80 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f f( l2.l_type==F_
cad90 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a UNLCK ){. z
cada0 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a Type = "UNLCK";.
cadb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
cadc0 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 assert( 0 );.
cadd0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 }. sqlite3D
cade0 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 ebugPrintf("fcnt
cadf0 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e l-failure-reason
cae00 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22 : %s %d %d %d\n"
cae10 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20 ,. zType,
cae20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c (int)l2.l_start,
cae30 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 (int)l2.l_len,
cae40 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a (int)l2.l_pid);.
cae50 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61 }. errno = sa
cae60 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75 vedErrno;. retu
cae70 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 rn s;.}.#define
cae80 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a fcntl lockTrace.
cae90 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
caea0 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a _LOCK_TRACE */..
caeb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
caec0 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20 tine translates
caed0 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58 a standard POSIX
caee0 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f errno code into
caef0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73 something.** us
caf00 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65 eful to the clie
caf10 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 nts of the sqlit
caf20 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53 e3 functions. S
caf30 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 pecifically, it
caf40 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74 is.** intended t
caf50 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61 o translate a va
caf60 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67 riety of "try ag
caf70 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f ain" errors into
caf80 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20 SQLITE_BUSY.**
caf90 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66 and a variety of
cafa0 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74 "please close t
cafb0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
cafc0 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69 or NOW" errors i
cafd0 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 nto .** SQLITE_I
cafe0 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f OERR.** .** Erro
caff0 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 rs during initia
cb000 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b lization of lock
cb010 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65 s, or file syste
cb020 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f m support for lo
cb030 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68 cks,.** should h
cb040 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e andle ENOLCK, EN
cb050 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50 OTSUP, EOPNOTSUP
cb060 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f P separately..*/
cb070 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
cb080 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
cb090 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45 Error(int posixE
cb0a0 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65 rror, int sqlite
cb0b0 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63 IOErr) {. switc
cb0c0 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b h (posixError) {
cb0d0 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20 . case 0: .
cb0e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
cb0f0 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 ;. . case EA
cb100 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 49 GAIN:. case ETI
cb110 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 45 MEDOUT:. case E
cb120 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 4e BUSY:. case EIN
cb130 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c 43 TR:. case ENOLC
cb140 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e 64 K: . /* rand
cb150 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 72 om NFS retry err
cb160 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e or, unless durin
cb170 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 g file system su
cb180 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e pport . * in
cb190 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 trospection, in
cb1a0 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c which it actuall
cb1b0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 y means what it
cb1c0 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 says */. retu
cb1d0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a rn SQLITE_BUSY;.
cb1e0 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 43 . case EACC
cb1f0 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 43 ES: . /* EACC
cb200 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 49 ES is like EAGAI
cb210 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 N during locking
cb220 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 74 operations, but
cb230 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 74 not any other t
cb240 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 73 ime*/. if( (s
cb250 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
cb260 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 LITE_IOERR_LOCK)
cb270 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 || ..(sqliteIOE
cb280 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rr == SQLITE_IOE
cb290 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a 09 RR_UNLOCK) || ..
cb2a0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
cb2b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c SQLITE_IOERR_RDL
cb2c0 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74 65 OCK) ||..(sqlite
cb2d0 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f IOErr == SQLITE_
cb2e0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
cb2f0 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 VEDLOCK) ){.
cb300 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cb310 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BUSY;. }.
cb320 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68 72 /* else fall thr
cb330 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20 45 ough */. case E
cb340 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 72 PERM: . retur
cb350 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 n SQLITE_PERM;.
cb360 20 20 20 0a 20 20 63 61 73 65 20 45 44 45 41 44 . case EDEAD
cb370 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 LK:. return S
cb380 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
cb390 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45 4f KED;. .#if EO
cb3a0 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53 55 PNOTSUPP!=ENOTSU
cb3b0 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 53 P. case EOPNOTS
cb3c0 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f 6d UPP: . /* som
cb3d0 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 72 ething went terr
cb3e0 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 73 ibly awry, unles
cb3f0 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 s during file sy
cb400 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 stem support .
cb410 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 * introspecti
cb420 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 on, in which it
cb430 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 actually means w
cb440 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 hat it says */.#
cb450 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e 4f endif.#ifdef ENO
cb460 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f 54 TSUP. case ENOT
cb470 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e 76 SUP: . /* inv
cb480 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73 20 alid fd, unless
cb490 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 during file syst
cb4a0 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72 6f em support intro
cb4b0 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 spection, in whi
cb4c0 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61 63 ch . * it ac
cb4d0 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 tually means wha
cb4e0 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e t it says */.#en
cb4f0 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a 0a dif. case EIO:.
cb500 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20 20 case EBADF:.
cb510 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20 63 case EINVAL:. c
cb520 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 ase ENOTCONN:.
cb530 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20 63 case ENODEV:. c
cb540 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61 73 ase ENXIO:. cas
cb550 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73 65 e ENOENT:. case
cb560 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65 20 ESTALE:. case
cb570 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20 74 ENOSYS:. /* t
cb580 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72 63 hese should forc
cb590 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f 20 e the client to
cb5a0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 close the file a
cb5b0 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f 0a nd reconnect */.
cb5c0 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a 20 . default:
cb5d0 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
cb5e0 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a 0a teIOErr;. }.}..
cb5f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
cb600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb640 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
cb650 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 75 **** Begin Uniqu
cb660 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 e File ID Utilit
cb670 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b y Used By VxWork
cb680 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
cb690 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 20 *.**.** On most
cb6a0 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 versions of unix
cb6b0 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 75 , we can get a u
cb6c0 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 66 nique ID for a f
cb6d0 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e 61 ile by concatena
cb6e0 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 69 ting.** the devi
cb6f0 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 ce number and th
cb700 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 e inode number.
cb710 20 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 6e But this does n
cb720 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 ot work on VxWor
cb730 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 6b ks..** On VxWork
cb740 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 s, a unique file
cb750 20 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 65 id must be base
cb760 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 63 d on the canonic
cb770 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a al filename..**.
cb780 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** A pointer to
cb790 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
cb7a0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
cb7b0 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 ucture can be us
cb7c0 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 75 ed as a.** uniqu
cb7d0 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 57 e file ID in VxW
cb7e0 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 74 orks. Each inst
cb7f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
cb800 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a ucture contains.
cb810 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ** a copy of the
cb820 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e canonical filen
cb830 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 ame. There is a
cb840 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 20 lso a reference
cb850 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 20 count. .** The
cb860 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 63 structure is rec
cb870 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 20 laimed when the
cb880 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 number of pointe
cb890 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 74 rs to it drops t
cb8a0 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a o.** zero..**.**
cb8b0 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72 There are never
cb8c0 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73 very many files
cb8d0 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d open at one tim
cb8e0 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72 e and lookups ar
cb8f0 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 6f e not.** a perfo
cb900 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c 20 rmance-critical
cb910 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 73 path, so it is s
cb920 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 74 ufficient to put
cb930 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 74 these.** struct
cb940 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 ures on a linked
cb950 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 list..*/.struct
cb960 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 7b vxworksFileId {
cb970 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b . struct vxwork
cb980 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b 20 sFileId *pNext;
cb990 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c 69 /* Next in a li
cb9a0 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a st of them all *
cb9b0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
cb9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb9d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
cb9e0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 eferences to thi
cb9f0 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e s one */. int n
cba00 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
cba10 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
cba20 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f 6e th of the zCanon
cba30 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 6e icalName[] strin
cba40 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 61 g */. char *zCa
cba50 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 20 nonicalName;
cba60 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 61 /* Canonica
cba70 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d 3b l filename */.};
cba80 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ..#if OS_VXWORKS
cba90 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71 ./* .** All uniq
cbaa0 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 ue filenames are
cbab0 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 held on a linke
cbac0 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 d list headed by
cbad0 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c this.** variabl
cbae0 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 e:.*/.static str
cbaf0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cbb00 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 d *vxworksFileLi
cbb10 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 st = 0;../*.** S
cbb20 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e 61 implify a filena
cbb30 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e 6f me into its cano
cbb40 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 79 nical form.** by
cbb50 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c 6c making the foll
cbb60 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a 2a owing changes:.*
cbb70 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e 67 *.** * removing
cbb80 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 6e any trailing an
cbb90 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a 2a d duplicate /.**
cbba0 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f 20 * convert /./
cbbb0 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 20 into just /.**
cbbc0 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e 2f * convert /A/../
cbbd0 20 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 20 where A is any
cbbe0 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 6f simple name into
cbbf0 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 just /.**.** Ch
cbc00 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 anges are made i
cbc10 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 6e n-place. Return
cbc20 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c 65 the new name le
cbc30 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ngth..**.** The
cbc40 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d original filenam
cbc50 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 e is in z[0..n-1
cbc60 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e ]. Return the n
cbc70 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 umber of.** char
cbc80 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 69 acters in the si
cbc90 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a 2a mplified name..*
cbca0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77 /.static int vxw
cbcb0 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 orksSimplifyName
cbcc0 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 (char *z, int n)
cbcd0 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 {. int i, j;.
cbce0 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 5b while( n>1 && z[
cbcf0 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d n-1]=='/' ){ n--
cbd00 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b ; }. for(i=j=0;
cbd10 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<n; i++){.
cbd20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 7b if( z[i]=='/' ){
cbd30 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 . if( z[i+1
cbd40 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 ]=='/' ) continu
cbd50 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 e;. if( z[i
cbd60 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 3c +1]=='.' && i+2<
cbd70 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 n && z[i+2]=='/'
cbd80 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d ){. i +=
cbd90 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 1;. cont
cbda0 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 inue;. }.
cbdb0 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d if( z[i+1]==
cbdc0 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 20 '.' && i+3<n &&
cbdd0 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 7a z[i+2]=='.' && z
cbde0 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 [i+3]=='/' ){.
cbdf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 while( j>0
cbe00 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 20 && z[j-1]!='/'
cbe10 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 ){ j--; }.
cbe20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d 2d if( j>0 ){ j--
cbe30 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d ; }. i +=
cbe40 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 2;. cont
cbe50 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 inue;. }.
cbe60 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d }. z[j++] =
cbe70 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6a z[i];. }. z[j
cbe80 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 ] = 0;. return
cbe90 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 j;.}../*.** Find
cbea0 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 a unique file I
cbeb0 44 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 D for the given
cbec0 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 6d absolute pathnam
cbed0 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 e. Return.** a
cbee0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 76 pointer to the v
cbef0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a xworksFileId obj
cbf00 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e 74 ect. This point
cbf10 65 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 65 er is the unique
cbf20 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a 0a .** file ID..**.
cbf30 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 6c ** The nRef fiel
cbf40 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b 73 d of the vxworks
cbf50 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 FileId object is
cbf60 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 66 incremented bef
cbf70 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 ore.** the objec
cbf80 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 t is returned.
cbf90 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 6c A new vxworksFil
cbfa0 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 72 eId object is cr
cbfb0 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 64 eated.** and add
cbfc0 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c ed to the global
cbfd0 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 61 list if necessa
cbfe0 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d ry..**.** If a m
cbff0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
cc000 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 error occurs, r
cc010 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 eturn NULL..*/.s
cc020 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 tatic struct vxw
cc030 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f orksFileId *vxwo
cc040 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 6f rksFindFileId(co
cc050 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f 6c nst char *zAbsol
cc060 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 75 uteName){. stru
cc070 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cc080 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 *pNew;
cc090 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 6e /* search key an
cc0a0 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f d new file ID */
cc0b0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b . struct vxwork
cc0c0 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 64 sFileId *pCandid
cc0d0 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c 6f ate; /* For lo
cc0e0 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 oping over exist
cc0f0 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f 0a ing file IDs */.
cc100 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 int n;
cc110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cc120 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
cc130 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 of zAbsoluteName
cc140 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 73 string */.. as
cc150 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 4e sert( zAbsoluteN
cc160 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20 ame[0]=='/' );.
cc170 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e n = (int)strlen
cc180 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 3b (zAbsoluteName);
cc190 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
cc1a0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 3_malloc( sizeof
cc1b0 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 20 (*pNew) + (n+1)
cc1c0 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
cc1d0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
cc1e0 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e New->zCanonicalN
cc1f0 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 4e ame = (char*)&pN
cc200 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 ew[1];. memcpy(
cc210 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c pNew->zCanonical
cc220 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 4e Name, zAbsoluteN
cc230 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 3d ame, n+1);. n =
cc240 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 vxworksSimplify
cc250 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f Name(pNew->zCano
cc260 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a 0a nicalName, n);..
cc270 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 /* Search for
cc280 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 an existing entr
cc290 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 20 y that matching
cc2a0 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e 61 the canonical na
cc2b0 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 6e me.. ** If foun
cc2c0 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 d, increment the
cc2d0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
cc2e0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f and return a po
cc2f0 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 68 inter to. ** th
cc300 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 e existing file
cc310 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 ID.. */. unixE
cc320 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 66 nterMutex();. f
cc330 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 78 or(pCandidate=vx
cc340 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 70 worksFileList; p
cc350 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e 64 Candidate; pCand
cc360 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 65 idate=pCandidate
cc370 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
cc380 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 4e ( pCandidate->nN
cc390 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 20 ame==n . &&
cc3a0 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 74 memcmp(pCandidat
cc3b0 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d e->zCanonicalNam
cc3c0 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 e, pNew->zCanoni
cc3d0 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a 20 calName, n)==0.
cc3e0 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 6c ){. sql
cc3f0 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b ite3_free(pNew);
cc400 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 61 . pCandida
cc410 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 te->nRef++;.
cc420 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
cc430 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 75 x();. retu
cc440 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a 20 rn pCandidate;.
cc450 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e }. }.. /* N
cc460 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 6e o match was foun
cc470 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b 65 d. We will make
cc480 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a a new file ID *
cc490 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d /. pNew->nRef =
cc4a0 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 6d 1;. pNew->nNam
cc4b0 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e 70 e = n;. pNew->p
cc4c0 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 69 Next = vxworksFi
cc4d0 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 6b leList;. vxwork
cc4e0 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 77 sFileList = pNew
cc4f0 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 ;. unixLeaveMut
cc500 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ex();. return p
cc510 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 New;.}../*.** De
cc520 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
cc530 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 rence count on a
cc540 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f vxworksFileId o
cc550 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a 20 bject. Free.**
cc560 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 the object when
cc570 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
cc580 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f unt reaches zero
cc590 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
cc5a0 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 vxworksReleaseF
cc5b0 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 77 ileId(struct vxw
cc5c0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 29 orksFileId *pId)
cc5d0 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 {. unixEnterMut
cc5e0 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ex();. assert(
cc5f0 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 pId->nRef>0 );.
cc600 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 pId->nRef--;.
cc610 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d 30 if( pId->nRef==0
cc620 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 76 ){. struct v
cc630 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a 70 xworksFileId **p
cc640 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 76 p;. for(pp=&v
cc650 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 xworksFileList;
cc660 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 3b *pp && *pp!=pId;
cc670 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e 70 pp = &((*pp)->p
cc680 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 73 Next)){}. ass
cc690 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 3b ert( *pp==pId );
cc6a0 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d 3e . *pp = pId->
cc6b0 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 pNext;. sqlit
cc6c0 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 20 e3_free(pId);.
cc6d0 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 }. unixLeaveMut
cc6e0 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ex();.}.#endif /
cc6f0 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a * OS_VXWORKS */.
cc700 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
cc710 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 46 End of Unique F
cc720 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 55 ile ID Utility U
cc730 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a sed By VxWorks *
cc740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
cc750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
cc7a0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
cc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc7f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
cc800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f ************* Po
cc810 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 six Advisory Loc
cc820 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a king ***********
cc830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc840 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 64 *.**.** POSIX ad
cc850 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65 visory locks are
cc860 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 67 broken by desig
cc870 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 30 n. ANSI STD 100
cc880 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 65 3.1 (1996).** se
cc890 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c 69 ction 6.5.2.2 li
cc8a0 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 20 nes 483 through
cc8b0 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61 74 490 specify that
cc8c0 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 0a when a process.
cc8d0 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 72 ** sets or clear
cc8e0 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 6f s a lock, that o
cc8f0 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 64 peration overrid
cc900 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f 63 es any prior loc
cc910 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 65 ks set.** by the
cc920 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 same process.
cc930 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 6c It does not expl
cc940 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 62 icitly say so, b
cc950 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 0a ut this implies.
cc960 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 72 ** that it overr
cc970 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 62 ides locks set b
cc980 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 y the same proce
cc990 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 65 ss using a diffe
cc9a0 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 rent.** file des
cc9b0 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 64 criptor. Consid
cc9c0 65 72 20 74 68 69 73 20 74 65 73 74 20 63 61 73 er this test cas
cc9d0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69 e:.**.** i
cc9e0 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 2e nt fd1 = open(".
cc9f0 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 7c /file1", O_RDWR|
cca00 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a O_CREAT, 0644);.
cca10 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 32 ** int fd2
cca20 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32 = open("./file2
cca30 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 ", O_RDWR|O_CREA
cca40 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 T, 0644);.**.**
cca50 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20 Suppose ./file1
cca60 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20 and ./file2 are
cca70 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 really the same
cca80 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a file (because.**
cca90 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f one is a hard o
ccaa0 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20 r symbolic link
ccab0 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 68 to the other) th
ccac0 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a en if you set.**
ccad0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
ccae0 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20 ck on fd1, then
ccaf0 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78 try to get an ex
ccb00 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 clusive lock.**
ccb10 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 on fd2, it works
ccb20 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20 . I would have
ccb30 65 78 70 65 63 74 65 64 20 74 68 65 20 73 65 63 expected the sec
ccb40 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66 ond lock to.** f
ccb50 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 ail since there
ccb60 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f was already a lo
ccb70 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 ck on the file d
ccb80 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75 ue to fd1..** Bu
ccb90 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 t not so. Since
ccba0 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 both locks came
ccbb0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 from the same p
ccbc0 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73 rocess, the.** s
ccbd0 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20 econd overrides
ccbe0 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20 the first, even
ccbf0 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 65 though they were
ccc00 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a on different.**
ccc10 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
ccc20 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66 s opened on diff
ccc30 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73 erent file names
ccc40 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 61 ..**.** This mea
ccc50 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e 6f ns that we canno
ccc60 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 6b t use POSIX lock
ccc70 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 s to synchronize
ccc80 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a 20 file access.**
ccc90 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 20 among competing
ccca0 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 73 threads of the s
cccb0 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 4f ame process. PO
cccc0 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 77 SIX locks will w
cccd0 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 73 ork fine.** to s
ccce0 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 73 ynchronize acces
cccf0 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 6e s for threads in
ccd00 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 separate proces
ccd10 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a 20 ses, but not.**
ccd20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 74 threads within t
ccd30 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e he same process.
ccd40 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 .**.** To work a
ccd50 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 round the proble
ccd60 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 6f m, SQLite has to
ccd70 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f 63 manage file loc
ccd80 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a ks internally.**
ccd90 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 68 on its own. Wh
ccda0 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 74 enever a new dat
ccdb0 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2c abase is opened,
ccdc0 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e 64 we have to find
ccdd0 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 the.** specific
ccde0 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 61 inode of the da
ccdf0 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 65 tabase file (the
cce00 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 6d inode is determ
cce10 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 ined by the.** s
cce20 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e 6f t_dev and st_ino
cce30 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 73 fields of the s
cce40 74 61 74 20 73 74 72 75 63 74 75 72 65 20 74 68 tat structure th
cce50 61 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c 73 at fstat() fills
cce60 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 63 in).** and chec
cce70 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 65 k for locks alre
cce80 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e 20 ady existing on
cce90 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 65 that inode. Whe
ccea0 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 63 n locks are.** c
cceb0 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 65 reated or remove
ccec0 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f d, we have to lo
cced0 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e ok at our own in
ccee0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 ternal record of
ccef0 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f the.** locks to
ccf00 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20 see if another
ccf10 74 68 72 65 61 64 20 68 61 73 20 70 72 65 76 69 thread has previ
ccf20 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b ously set a lock
ccf30 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a on that same.**
ccf40 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 41 inode..**.** (A
ccf50 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f 66 side: The use of
ccf60 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 inode numbers a
ccf70 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f 65 s unique IDs doe
ccf80 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 s not work on Vx
ccf90 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 78 Works..** For Vx
ccfa0 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 74 Works, we have t
ccfb0 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 6e o use the altern
ccfc0 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 20 ative unique ID
ccfd0 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e 0a system based on.
ccfe0 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c ** canonical fil
ccff0 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 6d ename and implem
cd000 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 65 ented in the pre
cd010 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e 29 vious division.)
cd020 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
cd030 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 e3_file structur
cd040 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e e for POSIX is n
cd050 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e o longer just an
cd060 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a integer file.**
cd070 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74 descriptor. It
cd080 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74 is now a struct
cd090 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 ure that holds t
cd0a0 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a he integer file.
cd0b0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e ** descriptor an
cd0c0 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 d a pointer to a
cd0d0 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
cd0e0 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6e describes the in
cd0f0 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 ternal.** locks
cd100 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e on the correspon
cd110 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 ding inode. The
cd120 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e re is one lockin
cd130 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 g structure.** p
cd140 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 er inode, so if
cd150 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 the same inode i
cd160 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20 s opened twice,
cd170 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74 both unixFile st
cd180 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e ructures.** poin
cd190 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f t to the same lo
cd1a0 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e cking structure.
cd1b0 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 The locking st
cd1c0 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a ructure keeps.**
cd1d0 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 a reference cou
cd1e0 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b nt (so we will k
cd1f0 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65 now when to dele
cd200 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e te it) and a "cn
cd210 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74 t".** field that
cd220 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e tells us its in
cd230 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 ternal lock stat
cd240 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e us. cnt==0 mean
cd250 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 s the.** file is
cd260 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d unlocked. cnt=
cd270 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69 =-1 means the fi
cd280 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 le has an exclus
cd290 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 ive lock..** cnt
cd2a0 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 >0 means there a
cd2b0 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f re cnt shared lo
cd2c0 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e cks on the file.
cd2d0 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d .**.** Any attem
cd2e0 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e pt to lock or un
cd2f0 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73 lock a file firs
cd300 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63 t checks the loc
cd310 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 king.** structur
cd320 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20 e. The fcntl()
cd330 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f system call is o
cd340 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 nly invoked to s
cd350 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c et a .** POSIX l
cd360 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72 ock if the inter
cd370 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75 nal lock structu
cd380 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 re transitions b
cd390 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b etween.** a lock
cd3a0 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b ed and an unlock
cd3b0 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ed state..**.**
cd3c0 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 65 But wait: there
cd3d0 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 72 are yet more pr
cd3e0 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 49 oblems with POSI
cd3f0 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 X advisory locks
cd400 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 ..**.** If you c
cd410 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63 lose a file desc
cd420 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e riptor that poin
cd430 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61 ts to a file tha
cd440 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 t has locks,.**
cd450 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 all locks on tha
cd460 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 t file that are
cd470 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72 owned by the cur
cd480 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65 rent process are
cd490 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54 .** released. T
cd4a0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
cd4b0 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 is problem, each
cd4c0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
cd4d0 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ure contains.**
cd4e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
cd4f0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cd500 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 69 73 cture. There is
cd510 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 one unixOpenCnt
cd520 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 structure.** pe
cd530 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 68 r open inode, wh
cd540 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6d ich means that m
cd550 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c 65 ultiple unixFile
cd560 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 20 can point to a
cd570 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f 70 single.** unixOp
cd580 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20 enCnt. When an
cd590 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
cd5a0 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78 to close an unix
cd5b0 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61 File, if there a
cd5c0 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78 re.** other unix
cd5d0 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 File open on the
cd5e0 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74 same inode that
cd5f0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 are holding loc
cd600 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20 ks, the call.**
cd610 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66 to close() the f
cd620 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
cd630 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c s deferred until
cd640 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b all of the lock
cd650 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20 s clear..** The
cd660 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cd670 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69 cture keeps a li
cd680 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72 st of file descr
cd690 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 iptors that need
cd6a0 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64 to.** be closed
cd6b0 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69 and that list i
cd6c0 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c s walked (and cl
cd6d0 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20 eared) when the
cd6e0 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 last lock.** cle
cd6f0 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20 61 ars..**.** Yet a
cd700 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a 20 nother problem:
cd710 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64 6f LinuxThreads do
cd720 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20 77 not play well w
cd730 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73 2e ith posix locks.
cd740 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64 65 .**.** Many olde
cd750 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c 69 r versions of li
cd760 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e 75 nux use the Linu
cd770 78 54 68 72 65 61 64 73 20 6c 69 62 72 61 72 79 xThreads library
cd780 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f 74 which is.** not
cd790 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e 74 posix compliant
cd7a0 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54 68 . Under LinuxTh
cd7b0 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63 72 reads, a lock cr
cd7c0 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 0a eated by thread.
cd7d0 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20 6d ** A cannot be m
cd7e0 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72 72 odified or overr
cd7f0 69 64 64 65 6e 20 62 79 20 61 20 64 69 66 66 65 idden by a diffe
cd800 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a 2a rent thread B..*
cd810 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41 20 * Only thread A
cd820 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 6c can modify the l
cd830 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62 65 ock. Locking be
cd840 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65 63 havior is correc
cd850 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c t.** if the appl
cd860 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 iation uses the
cd870 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f 73 newer Native Pos
cd880 69 78 20 54 68 72 65 61 64 20 4c 69 62 72 61 72 ix Thread Librar
cd890 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20 6c y (NPTL).** on l
cd8a0 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54 4c inux - with NPTL
cd8b0 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 a lock created
cd8c0 62 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 by thread A can
cd8d0 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a override locks.*
cd8e0 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 20 * in thread B.
cd8f0 42 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 But there is no
cd900 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 63 way to know at c
cd910 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 63 ompile-time whic
cd920 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 6c h.** threading l
cd930 69 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 20 ibrary is being
cd940 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 20 used. So there
cd950 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f is no way to kno
cd960 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d w at.** compile-
cd970 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72 20 time whether or
cd980 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 61 6e not thread A can
cd990 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 20 override locks
cd9a0 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20 on thread B..**
cd9b0 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61 20 We have to do a
cd9c0 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20 74 run-time check t
cd9d0 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 62 o discover the b
cd9e0 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a 2a ehavior of the.*
cd9f0 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 * current proces
cda00 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73 74 s..**.** On syst
cda10 65 6d 73 20 77 68 65 72 65 20 74 68 72 65 61 64 ems where thread
cda20 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 A is unable to
cda30 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72 65 modify locks cre
cda40 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65 61 ated by.** threa
cda50 64 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f 20 d B, we have to
cda60 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77 68 keep track of wh
cda70 69 63 68 20 74 68 72 65 61 64 20 63 72 65 61 74 ich thread creat
cda80 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b 2e ed each.** lock.
cda90 20 20 48 65 6e 63 65 20 74 68 65 72 65 20 69 73 Hence there is
cdaa0 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64 20 an extra field
cdab0 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 in the key to th
cdac0 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a 2a e unixLockInfo.*
cdad0 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 * structure to r
cdae0 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f 72 ecord this infor
cdaf0 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e 20 mation. And on
cdb00 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69 74 those systems it
cdb10 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 .** is illegal t
cdb20 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73 61 o begin a transa
cdb30 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 ction in one thr
cdb40 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20 69 ead and finish i
cdb50 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72 2e t.** in another.
cdb60 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74 65 For this latte
cdb70 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 74 r restriction, t
cdb80 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 2d here is no work-
cdb90 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69 73 around..** It is
cdba0 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f 66 a limitation of
cdbb0 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a LinuxThreads..*
cdbc0 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 /../*.** Set or
cdbd0 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46 69 check the unixFi
cdbe0 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20 54 le.tid field. T
cdbf0 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 65 74 his field is set
cdc00 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69 6c when an unixFil
cdc10 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f 70 e.** is first op
cdc20 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 65 ened. All subse
cdc30 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 68 quent uses of th
cdc40 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69 66 e unixFile verif
cdc50 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 61 y that the.** sa
cdc60 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f 70 65 me thread is ope
cdc70 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75 6e rating on the un
cdc80 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f 70 ixFile. Some op
cdc90 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 erating systems
cdca0 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77 20 do.** not allow
cdcb0 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65 72 locks to be over
cdcc0 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72 20 ridden by other
cdcd0 74 68 72 65 61 64 73 20 61 6e 64 20 74 68 61 74 threads and that
cdce0 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 restriction.**
cdcf0 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69 74 means that sqlit
cdd00 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e e3* database han
cdd10 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 6d dles cannot be m
cdd20 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 oved from one th
cdd30 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74 68 read.** to anoth
cdd40 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20 61 er while locks a
cdd50 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56 re held..**.** V
cdd60 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32 30 ersion 3.3.1 (20
cdd70 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69 78 06-01-15): unix
cdd80 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76 65 File can be move
cdd90 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 d from one threa
cdda0 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 d to.** another
cddb0 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61 72 as long as we ar
cddc0 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20 73 e running on a s
cddd0 79 73 74 65 6d 20 74 68 61 74 20 73 75 70 70 6f ystem that suppo
cdde0 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20 6f rts threads.** o
cddf0 76 65 72 72 69 64 69 6e 67 20 65 61 63 68 20 6f verriding each o
cde00 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68 69 thers locks (whi
cde10 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d 6f ch is now the mo
cde20 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76 69 st common behavi
cde30 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f 20 or).** or if no
cde40 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20 locks are held.
cde50 20 42 75 74 20 74 68 65 20 75 6e 69 78 46 69 6c But the unixFil
cde60 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e 65 e.pLock field ne
cde70 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65 63 eds to be.** rec
cde80 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73 65 20 omputed because
cde90 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65 73 its key includes
cdea0 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e 20 the thread-id.
cdeb0 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72 61 See the .** tra
cdec0 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 29 nsferOwnership()
cded0 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77 20 function below
cdee0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
cdef0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23 69 nformation.*/.#i
cdf00 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
cdf10 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
cdf20 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 _linux__).# defi
cdf30 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 ne SET_THREADID(
cdf40 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20 X) (X)->tid =
cdf50 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 pthread_self().#
cdf60 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 define CHECK_TH
cdf70 52 45 41 44 49 44 28 58 29 20 28 74 68 72 65 61 READID(X) (threa
cdf80 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cdf90 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 hersLocks==0 &&
cdfa0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
cdfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 70 !p
cdfc0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29 thread_equal((X)
cdfd0 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73 ->tid, pthread_s
cdfe0 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20 elf())).#else.#
cdff0 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 define SET_THREA
ce000 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20 DID(X).# define
ce010 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 CHECK_THREADID(X
ce020 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ) 0.#endif../*.*
ce030 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
ce040 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
ce050 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 tructure serves
ce060 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a as the key used.
ce070 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 ** to locate a p
ce080 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f 70 articular unixOp
ce090 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 enCnt structure
ce0a0 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e given its inode.
ce0b0 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 This.** is the
ce0c0 20 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e 69 same as the uni
ce0d0 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 20 xLockKey except
ce0e0 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20 that the thread
ce0f0 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a ID is omitted..*
ce100 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c /.struct unixFil
ce110 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 eId {. dev_t de
ce120 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
ce130 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e 75 /* Device nu
ce140 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f 56 mber */.#if OS_V
ce150 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 XWORKS. struct
ce160 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 vxworksFileId *p
ce170 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 Id; /* Unique f
ce180 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f 72 ile ID for vxwor
ce190 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 69 ks. */.#else. i
ce1a0 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20 no_t ino;
ce1b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
ce1c0 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65 ode number */.#e
ce1d0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
ce1e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
ce1f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
ce200 63 74 75 72 65 20 73 65 72 76 65 73 20 61 73 20 cture serves as
ce210 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 the key used.**
ce220 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 to locate a part
ce230 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b 49 icular unixLockI
ce240 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 67 69 nfo structure gi
ce250 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a ven its inode..*
ce260 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20 *.** If threads
ce270 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 cannot override
ce280 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
ce290 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 29 s (LinuxThreads)
ce2a0 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65 74 , then we.** set
ce2b0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 the unixLockKey
ce2c0 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74 68 .tid field to th
ce2d0 65 20 74 68 72 65 61 64 20 49 44 2e 20 20 49 66 e thread ID. If
ce2e0 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 threads can ove
ce2f0 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f 74 rride.** each ot
ce300 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73 69 hers locks (Posi
ce310 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65 6e x and NPTL) then
ce320 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20 73 tid is always s
ce330 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 74 et to zero..** t
ce340 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66 id is omitted if
ce350 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 we compile with
ce360 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20 73 75 out threading su
ce370 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20 4f pport or on an O
ce380 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 S.** other than
ce390 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 linux..*/.struct
ce3a0 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a 20 unixLockKey {.
ce3b0 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 struct unixFile
ce3c0 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69 71 Id fid; /* Uniq
ce3d0 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 6f ue identifier fo
ce3e0 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23 69 r the file */.#i
ce3f0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
ce400 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
ce410 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 _linux__). pthr
ce420 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20 54 ead_t tid; /* T
ce430 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63 6b hread ID of lock
ce440 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66 20 owner. Zero if
ce450 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78 54 not using LinuxT
ce460 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69 66 hreads */.#endif
ce470 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
ce480 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
ce490 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
ce4a0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
ce4b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 or each open.**
ce4c0 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20 4c inode. Or, on L
ce4d0 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68 65 inuxThreads, the
ce4e0 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 re is one of the
ce4f0 73 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f se structures fo
ce500 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65 20 r.** each inode
ce510 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68 20 74 opened by each t
ce520 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 hread..**.** A s
ce530 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20 ingle inode can
ce540 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69 have multiple fi
ce550 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 le descriptors,
ce560 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 so each unixFile
ce570 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f .** structure co
ce580 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
ce590 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 to an instance
ce5a0 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 of this object a
ce5b0 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 nd this.** objec
ce5c0 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20 t keeps a count
ce5d0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
ce5e0 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69 unixFile pointi
ce5f0 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 72 ng to it..*/.str
ce600 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
ce610 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 {. struct unix
ce620 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b LockKey lockKey;
ce630 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b /* The look
ce640 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 up key */. int
ce650 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 cnt;
ce660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ce670 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 umber of SHARED
ce680 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 locks held */.
ce690 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 int locktype;
ce6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce6b0 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45 44 /* One of SHARED
ce6c0 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44 5f _LOCK, RESERVED_
ce6d0 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 69 LOCK etc. */. i
ce6e0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
ce6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ce700 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e * Number of poin
ce710 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 ters to this str
ce720 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 ucture */. stru
ce730 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
ce740 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 4c *pNext; /* L
ce750 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c ist of all unixL
ce760 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73 20 ockInfo objects
ce770 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
ce780 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76 3b LockInfo *pPrev;
ce790 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e 20 /* ....
ce7a0 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a 2f doubly linked */
ce7b0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
ce7c0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
ce7d0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
ce7e0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
ce7f0 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 or each open.**
ce800 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72 inode. This str
ce810 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 ucture keeps tra
ce820 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 ck of the number
ce830 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 of locks on tha
ce840 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20 t.** inode. If
ce850 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d a close is attem
ce860 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20 pted against an
ce870 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f inode that is ho
ce880 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 lding.** locks,
ce890 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66 the close is def
ce8a0 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 erred until all
ce8b0 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61 locks clear by a
ce8c0 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c dding the.** fil
ce8d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 e descriptor to
ce8e0 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65 be closed to the
ce8f0 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a pending list..*
ce900 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e 73 *.** TODO: Cons
ce910 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68 ider changing th
ce920 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 is so that there
ce930 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c is only a singl
ce940 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 e file.** descri
ce950 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f 70 ptor for each op
ce960 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77 68 en file, even wh
ce970 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20 en it is opened
ce980 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a multiple times..
ce990 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20 73 ** The close() s
ce9a0 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c 64 ystem call would
ce9b0 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 6e only occur when
ce9c0 20 74 68 65 20 6c 61 73 74 20 64 61 74 61 62 61 the last databa
ce9d0 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 se.** using the
ce9e0 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f 0a file closes..*/.
ce9f0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
cea00 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e nt {. struct un
cea10 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b ixFileId fileId;
cea20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 /* The lookup
cea30 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 key */. int nR
cea40 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
cea50 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
cea60 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 of pointers to t
cea70 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
cea80 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 . int nLock;
cea90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ceaa0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 * Number of outs
ceab0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f tanding locks */
ceac0 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 . UnixUnusedFd
cead0 2a 70 55 6e 75 73 65 64 3b 20 20 20 20 20 20 2f *pUnused; /
ceae0 2a 20 55 6e 75 73 65 64 20 66 69 6c 65 20 64 65 * Unused file de
ceaf0 73 63 72 69 70 74 6f 72 73 20 74 6f 20 63 6c 6f scriptors to clo
ceb00 73 65 20 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 se */.#if OS_VXW
ceb10 4f 52 4b 53 0a 20 20 73 65 6d 5f 74 20 2a 70 53 ORKS. sem_t *pS
ceb20 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
ceb30 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64 /* Named
ceb40 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 POSIX semaphore
ceb50 20 2a 2f 0a 20 20 63 68 61 72 20 61 53 65 6d 4e */. char aSemN
ceb60 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ame[MAX_PATHNAME
ceb70 2b 32 5d 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f +2]; /* Name o
ceb80 66 20 74 68 61 74 20 73 65 6d 61 70 68 6f 72 65 f that semaphore
ceb90 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 73 74 72 */.#endif. str
ceba0 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 uct unixOpenCnt
cebb0 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 *pNext, *pPrev;
cebc0 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c /* List of all
cebd0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a unixOpenCnt obj
cebe0 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ects */.};../*.*
cebf0 2a 20 4c 69 73 74 73 20 6f 66 20 61 6c 6c 20 75 * Lists of all u
cec00 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 nixLockInfo and
cec10 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 unixOpenCnt obje
cec20 63 74 73 2e 20 20 54 68 65 73 65 20 75 73 65 64 cts. These used
cec30 20 74 6f 20 62 65 20 68 61 73 68 0a 2a 2a 20 74 to be hash.** t
cec40 61 62 6c 65 73 2e 20 20 42 75 74 20 74 68 65 20 ables. But the
cec50 6e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 74 number of object
cec60 73 20 69 73 20 72 61 72 65 6c 79 20 6d 6f 72 65 s is rarely more
cec70 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 61 6e than a dozen an
cec80 64 0a 2a 2a 20 6e 65 76 65 72 20 65 78 63 65 65 d.** never excee
cec90 64 73 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e ds a few thousan
ceca0 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b 75 70 20 69 d. And lookup i
cecb0 73 20 6e 6f 74 20 6f 6e 20 61 20 63 72 69 74 69 s not on a criti
cecc0 63 61 6c 0a 2a 2a 20 70 61 74 68 20 73 6f 20 61 cal.** path so a
cecd0 20 73 69 6d 70 6c 65 20 6c 69 6e 6b 65 64 20 6c simple linked l
cece0 69 73 74 20 77 69 6c 6c 20 73 75 66 66 69 63 65 ist will suffice
cecf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
ced00 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
ced10 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20 30 3b 0a 73 *lockList = 0;.s
ced20 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 tatic struct uni
ced30 78 4f 70 65 6e 43 6e 74 20 2a 6f 70 65 6e 4c 69 xOpenCnt *openLi
ced40 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 54 st = 0;../*.** T
ced50 68 69 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d his variable rem
ced60 65 6d 62 65 72 73 20 77 68 65 74 68 65 72 20 6f embers whether o
ced70 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63 61 r not threads ca
ced80 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 n override each
ced90 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f 63 6b 73 2e others.** locks.
ceda0 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20 20 4e 6f .**.** 0: No
cedb0 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 6e 6f . Threads canno
cedc0 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 t override each
cedd0 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 others locks. (
cede0 4c 69 6e 75 78 54 68 72 65 61 64 73 29 0a 2a 2a LinuxThreads).**
cedf0 20 20 20 20 31 3a 20 20 59 65 73 2e 20 20 54 68 1: Yes. Th
cee00 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 reads can overri
cee10 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c de each others l
cee20 6f 63 6b 73 2e 20 20 28 50 6f 73 69 78 20 26 20 ocks. (Posix &
cee30 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d 31 3a 20 20 NLPT).** -1:
cee40 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 77 20 79 65 We don't know ye
cee50 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 t..**.** On some
cee60 20 73 79 73 74 65 6d 73 2c 20 77 65 20 6b 6e 6f systems, we kno
cee70 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d w at compile-tim
cee80 65 20 69 66 20 74 68 72 65 61 64 73 20 63 61 6e e if threads can
cee90 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 0a 2a override each.*
ceea0 2a 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 * others locks.
ceeb0 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d On those system
ceec0 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 54 48 s, the SQLITE_TH
ceed0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
ceee0 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 77 69 6c 6c CK macro.** will
ceef0 20 62 65 20 73 65 74 20 61 70 70 72 6f 70 72 69 be set appropri
cef00 61 74 65 6c 79 2e 20 20 4f 6e 20 6f 74 68 65 72 ately. On other
cef10 20 73 79 73 74 65 6d 73 2c 20 77 65 20 68 61 76 systems, we hav
cef20 65 20 74 6f 20 63 68 65 63 6b 20 61 74 0a 2a 2a e to check at.**
cef30 20 72 75 6e 74 69 6d 65 2e 20 20 4f 6e 20 74 68 runtime. On th
cef40 65 73 65 20 6c 61 74 74 65 72 20 73 79 73 74 65 ese latter syste
cef50 6d 73 2c 20 53 51 4c 54 49 45 5f 54 48 52 45 41 ms, SQLTIE_THREA
cef60 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 D_OVERRIDE_LOCK
cef70 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e is.** undefined.
cef80 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 .**.** This vari
cef90 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 able normally ha
cefa0 73 20 66 69 6c 65 20 73 63 6f 70 65 20 6f 6e 6c s file scope onl
cefb0 79 2e 20 20 42 75 74 20 64 75 72 69 6e 67 20 74 y. But during t
cefc0 65 73 74 69 6e 67 2c 20 77 65 20 6d 61 6b 65 0a esting, we make.
cefd0 2a 2a 20 69 74 20 61 20 67 6c 6f 62 61 6c 20 73 ** it a global s
cefe0 6f 20 74 68 61 74 20 74 68 65 20 74 65 73 74 20 o that the test
ceff0 63 6f 64 65 20 63 61 6e 20 63 68 61 6e 67 65 20 code can change
cf000 69 74 73 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 its value in ord
cf010 65 72 20 74 6f 20 76 65 72 69 66 79 0a 2a 2a 20 er to verify.**
cf020 74 68 61 74 20 74 68 65 20 72 69 67 68 74 20 73 that the right s
cf030 74 75 66 66 20 68 61 70 70 65 6e 73 20 69 6e 20 tuff happens in
cf040 65 69 74 68 65 72 20 63 61 73 65 2e 0a 2a 2f 0a either case..*/.
cf050 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
cf060 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
cf070 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 20 69 (__linux__).# i
cf080 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52 fndef SQLITE_THR
cf090 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
cf0a0 4b 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 K.# define SQ
cf0b0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LITE_THREAD_OVER
cf0c0 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 0a 23 20 20 RIDE_LOCK -1.#
cf0d0 65 6e 64 69 66 0a 23 20 20 69 66 64 65 66 20 53 endif.# ifdef S
cf0e0 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 74 QLITE_TEST.int t
cf0f0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
cf100 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 chOthersLocks =
cf110 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 SQLITE_THREAD_OV
cf120 45 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 20 20 ERRIDE_LOCK;.#
cf130 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 else.static int
cf140 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
cf150 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d achOthersLocks =
cf160 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f SQLITE_THREAD_O
cf170 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 20 VERRIDE_LOCK;.#
cf180 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f endif.#endif../
cf190 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 *.** This struct
cf1a0 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d ure holds inform
cf1b0 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 ation passed int
cf1c0 6f 20 69 6e 64 69 76 69 64 75 61 6c 20 74 65 73 o individual tes
cf1d0 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 62 79 20 t.** threads by
cf1e0 74 68 65 20 74 65 73 74 54 68 72 65 61 64 4c 6f the testThreadLo
cf1f0 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29 20 ckingBehavior()
cf200 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 72 75 routine..*/.stru
cf210 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 ct threadTestDat
cf220 61 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 a {. int fd;
cf230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cf240 46 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 File to be locke
cf250 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 66 6c d */. struct fl
cf260 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 2f 2a ock lock; /*
cf270 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 The locking ope
cf280 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ration */. int
cf290 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 result;
cf2a0 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 /* Result of
cf2b0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 the locking oper
cf2c0 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 ation */.};..#if
cf2d0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cf2e0 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cf2f0 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 linux__)./*.** T
cf300 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
cf310 75 73 65 64 20 61 73 20 74 68 65 20 6d 61 69 6e used as the main
cf320 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 20 74 routine for a t
cf330 68 72 65 61 64 20 6c 61 75 6e 63 68 65 64 20 62 hread launched b
cf340 79 0a 2a 2a 20 74 65 73 74 54 68 72 65 61 64 4c y.** testThreadL
cf350 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29 ockingBehavior()
cf360 2e 20 49 74 20 74 65 73 74 73 20 77 68 65 74 68 . It tests wheth
cf370 65 72 20 74 68 65 20 73 68 61 72 65 64 2d 6c 6f er the shared-lo
cf380 63 6b 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 62 ck obtained.** b
cf390 79 20 74 68 65 20 6d 61 69 6e 20 74 68 72 65 61 y the main threa
cf3a0 64 20 69 6e 20 74 65 73 74 54 68 72 65 61 64 4c d in testThreadL
cf3b0 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29 ockingBehavior()
cf3c0 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 conflicts with
cf3d0 61 0a 2a 2a 20 68 79 70 6f 74 68 65 74 69 63 61 a.** hypothetica
cf3e0 6c 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 62 74 l write-lock obt
cf3f0 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 74 68 ained by this th
cf400 72 65 61 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 read on the same
cf410 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 file..**.** The
cf420 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e write-lock is n
cf430 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 63 71 75 ot actually acqu
cf440 69 72 65 64 2c 20 61 73 20 74 68 69 73 20 69 73 ired, as this is
cf450 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 69 66 not possible if
cf460 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 69 73 .** the file is
cf470 20 6f 70 65 6e 20 69 6e 20 72 65 61 64 2d 6f 6e open in read-on
cf480 6c 79 20 6d 6f 64 65 20 28 73 65 65 20 74 69 63 ly mode (see tic
cf490 6b 65 74 20 23 33 34 37 32 29 2e 0a 2a 2f 20 0a ket #3472)..*/ .
cf4a0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68 72 static void *thr
cf4b0 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 28 76 eadLockingTest(v
cf4c0 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 74 oid *pArg){. st
cf4d0 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 ruct threadTestD
cf4e0 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 73 74 ata *pData = (st
cf4f0 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 ruct threadTestD
cf500 61 74 61 2a 29 70 41 72 67 3b 0a 20 20 70 44 61 ata*)pArg;. pDa
cf510 74 61 2d 3e 72 65 73 75 6c 74 20 3d 20 66 63 6e ta->result = fcn
cf520 74 6c 28 70 44 61 74 61 2d 3e 66 64 2c 20 46 5f tl(pData->fd, F_
cf530 47 45 54 4c 4b 2c 20 26 70 44 61 74 61 2d 3e 6c GETLK, &pData->l
cf540 6f 63 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ock);. return p
cf550 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a Arg;.}.#endif /*
cf560 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cf570 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cf580 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 0a 23 69 linux__) */...#i
cf590 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
cf5a0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
cf5b0 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20 _linux__)./*.**
cf5c0 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 61 This procedure a
cf5d0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
cf5e0 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 mine whether or
cf5f0 6e 6f 74 20 74 68 72 65 61 64 73 0a 2a 2a 20 63 not threads.** c
cf600 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 an override each
cf610 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 others locks th
cf620 65 6e 20 73 65 74 73 20 74 68 65 20 0a 2a 2a 20 en sets the .**
cf630 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
cf640 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 76 achOthersLocks v
cf650 61 72 69 61 62 6c 65 20 61 70 70 72 6f 70 72 69 ariable appropri
cf660 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 ately..*/.static
cf670 20 76 6f 69 64 20 74 65 73 74 54 68 72 65 61 64 void testThread
cf680 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 LockingBehavior(
cf690 69 6e 74 20 66 64 5f 6f 72 69 67 29 7b 0a 20 20 int fd_orig){.
cf6a0 69 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 72 63 int fd;. int rc
cf6b0 3b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61 ;. struct threa
cf6c0 64 54 65 73 74 44 61 74 61 20 64 3b 0a 20 20 73 dTestData d;. s
cf6d0 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 3b 0a 20 truct flock l;.
cf6e0 20 70 74 68 72 65 61 64 5f 74 20 74 3b 0a 0a 20 pthread_t t;..
cf6f0 20 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69 fd = dup(fd_ori
cf700 67 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 g);. if( fd<0 )
cf710 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 return;. memse
cf720 74 28 26 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&l, 0, sizeof(
cf730 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74 79 70 65 20 l));. l.l_type
cf740 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 6c 2e 6c = F_RDLCK;. l.l
cf750 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 2e 6c 5f _len = 1;. l.l_
cf760 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 2e 6c start = 0;. l.l
cf770 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 _whence = SEEK_S
cf780 45 54 3b 0a 20 20 72 63 20 3d 20 66 63 6e 74 6c ET;. rc = fcntl
cf790 28 66 64 5f 6f 72 69 67 2c 20 46 5f 53 45 54 4c (fd_orig, F_SETL
cf7a0 4b 2c 20 26 6c 29 3b 0a 20 20 69 66 28 20 72 63 K, &l);. if( rc
cf7b0 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !=0 ) return;.
cf7c0 6d 65 6d 73 65 74 28 26 64 2c 20 30 2c 20 73 69 memset(&d, 0, si
cf7d0 7a 65 6f 66 28 64 29 29 3b 0a 20 20 64 2e 66 64 zeof(d));. d.fd
cf7e0 20 3d 20 66 64 3b 0a 20 20 64 2e 6c 6f 63 6b 20 = fd;. d.lock
cf7f0 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63 6b 2e 6c 5f = l;. d.lock.l_
cf800 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a type = F_WRLCK;.
cf810 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 63 72 if( pthread_cr
cf820 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68 72 65 eate(&t, 0, thre
cf830 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 adLockingTest, &
cf840 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 d)==0 ){. pth
cf850 72 65 61 64 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b read_join(t, 0);
cf860 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 28 66 64 29 . }. close(fd)
cf870 3b 0a 20 20 69 66 28 20 64 2e 72 65 73 75 6c 74 ;. if( d.result
cf880 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !=0 ) return;.
cf890 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
cf8a0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d achOthersLocks =
cf8b0 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 3d (d.lock.l_type=
cf8c0 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d 0a 23 65 6e =F_UNLCK);.}.#en
cf8d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 dif /* SQLITE_TH
cf8e0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
cf8f0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a ned(__linux__) *
cf900 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 /../*.** Release
cf910 20 61 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 a unixLockInfo
cf920 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f structure previo
cf930 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 usly allocated b
cf940 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 y findLockInfo()
cf950 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 ..**.** The mute
cf960 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 x entered using
cf970 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 the unixEnterMut
cf980 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 ex() function mu
cf990 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 st be held.** wh
cf9a0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
cf9b0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
cf9c0 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
cf9d0 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 seLockInfo(struc
cf9e0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a t unixLockInfo *
cf9f0 70 4c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 pLock){. assert
cfa00 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 ( unixMutexHeld(
cfa10 29 20 29 3b 0a 20 20 69 66 28 20 70 4c 6f 63 6b ) );. if( pLock
cfa20 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e ){. pLock->n
cfa30 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 Ref--;. if( p
cfa40 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b Lock->nRef==0 ){
cfa50 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b . if( pLock
cfa60 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
cfa70 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
cfa80 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d ->pPrev->pNext==
cfa90 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 pLock );.
cfaa0 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 pLock->pPrev->p
cfab0 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e Next = pLock->pN
cfac0 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ext;. }else
cfad0 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
cfae0 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63 ( lockList==pLoc
cfaf0 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 k );. loc
cfb00 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 kList = pLock->p
cfb10 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
cfb20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 if( pLock->p
cfb30 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Next ){.
cfb40 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 assert( pLock->p
cfb50 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f Next->pPrev==pLo
cfb60 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c ck );. pL
cfb70 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 ock->pNext->pPre
cfb80 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 v = pLock->pPrev
cfb90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
cfba0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f sqlite3_free(pLo
cfbb0 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ck);. }. }.}
cfbc0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
cfbd0 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 a unixOpenCnt st
cfbe0 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 ructure previous
cfbf0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ly allocated by
cfc00 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a findLockInfo()..
cfc10 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 **.** The mutex
cfc20 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 entered using th
cfc30 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 e unixEnterMutex
cfc40 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 () function must
cfc50 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e be held.** when
cfc60 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
cfc70 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
cfc80 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
cfc90 4f 70 65 6e 43 6e 74 28 73 74 72 75 63 74 20 75 OpenCnt(struct u
cfca0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
cfcb0 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e n){. assert( un
cfcc0 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b ixMutexHeld() );
cfcd0 0a 20 20 69 66 28 20 70 4f 70 65 6e 20 29 7b 0a . if( pOpen ){.
cfce0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2d pOpen->nRef-
cfcf0 2d 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e -;. if( pOpen
cfd00 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ->nRef==0 ){.
cfd10 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 50 if( pOpen->pP
cfd20 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 rev ){. a
cfd30 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 50 ssert( pOpen->pP
cfd40 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70 65 rev->pNext==pOpe
cfd50 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 n );. pOp
cfd60 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 en->pPrev->pNext
cfd70 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b = pOpen->pNext;
cfd80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
cfd90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 assert( op
cfda0 65 6e 4c 69 73 74 3d 3d 70 4f 70 65 6e 20 29 3b enList==pOpen );
cfdb0 0a 20 20 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 . openLis
cfdc0 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 t = pOpen->pNext
cfdd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
cfde0 69 66 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 if( pOpen->pNext
cfdf0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
cfe00 72 74 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 rt( pOpen->pNext
cfe10 2d 3e 70 50 72 65 76 3d 3d 70 4f 70 65 6e 20 29 ->pPrev==pOpen )
cfe20 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
cfe30 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
cfe40 70 4f 70 65 6e 2d 3e 70 50 72 65 76 3b 0a 20 20 pOpen->pPrev;.
cfe50 20 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 }.#if SQLITE
cfe60 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
cfe70 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
cfe80 29 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ). assert(
cfe90 21 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 !pOpen->pUnused
cfea0 7c 7c 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 || threadsOverri
cfeb0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b deEachOthersLock
cfec0 73 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a s==0 );.#endif..
cfed0 20 20 20 20 20 20 2f 2a 20 49 66 20 70 4f 70 65 /* If pOpe
cfee0 6e 2d 3e 70 55 6e 75 73 65 64 20 69 73 20 6e 6f n->pUnused is no
cfef0 74 20 6e 75 6c 6c 2c 20 74 68 65 6e 20 6d 65 6d t null, then mem
cff00 6f 72 79 20 61 6e 64 20 66 69 6c 65 2d 64 65 73 ory and file-des
cff10 63 72 69 70 74 6f 72 73 0a 20 20 20 20 20 20 2a criptors. *
cff20 2a 20 61 72 65 20 6c 65 61 6b 65 64 2e 0a 20 20 * are leaked..
cff30 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
cff40 54 68 69 73 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 This will only h
cff50 61 70 70 65 6e 20 69 66 2c 20 75 6e 64 65 72 20 appen if, under
cff60 4c 69 6e 75 78 74 68 72 65 61 64 73 2c 20 74 68 Linuxthreads, th
cff70 65 20 75 73 65 72 20 68 61 73 20 6f 70 65 6e 65 e user has opene
cff80 64 0a 20 20 20 20 20 20 2a 2a 20 61 20 74 72 61 d. ** a tra
cff90 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 nsaction in one
cffa0 74 68 72 65 61 64 2c 20 74 68 65 6e 20 61 74 74 thread, then att
cffb0 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 empts to close t
cffc0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
cffd0 20 20 2a 2a 20 68 61 6e 64 6c 65 20 66 72 6f 6d ** handle from
cffe0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 another thread
cfff0 28 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 75 (without first u
d0000 6e 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 62 20 nlocking the db
d0010 66 69 6c 65 29 2e 0a 20 20 20 20 20 20 2a 2a 20 file).. **
d0020 54 68 69 73 20 69 73 20 61 20 6d 69 73 75 73 65 This is a misuse
d0030 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 . */. sqli
d0040 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b te3_free(pOpen);
d0050 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
d0060 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 .** Given a file
d0070 20 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 descriptor, loc
d0080 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f ate unixLockInfo
d0090 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 and unixOpenCnt
d00a0 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 structures that
d00b0 0a 2a 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 .** describes th
d00c0 61 74 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 at file descript
d00d0 6f 72 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 or. Create new
d00e0 6f 6e 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 ones if necessar
d00f0 79 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 y. The.** retur
d0100 6e 20 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 n values might b
d0110 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 e uninitialized
d0120 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
d0130 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 rs..**.** The mu
d0140 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e tex entered usin
d0150 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d g the unixEnterM
d0160 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 utex() function
d0170 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20 must be held.**
d0180 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
d0190 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a on is called..**
d01a0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61 70 .** Return an ap
d01b0 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 propriate error
d01c0 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 code..*/.static
d01d0 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f int findLockInfo
d01e0 28 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 (. unixFile *pF
d01f0 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ile,
d0200 20 20 20 2f 2a 20 55 6e 69 78 20 66 69 6c 65 20 /* Unix file
d0210 77 69 74 68 20 66 69 6c 65 20 64 65 73 63 20 75 with file desc u
d0220 73 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 2a sed in the key *
d0230 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c /. struct unixL
d0240 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f 63 6b ockInfo **ppLock
d0250 2c 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 , /* Return the
d0260 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 unixLockInfo st
d0270 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a ructure here */.
d0280 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 struct unixOpe
d0290 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20 nCnt **ppOpen
d02a0 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 /* Return the u
d02b0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
d02c0 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a ture here */.){.
d02d0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
d02e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d02f0 20 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20 /* System call
d0300 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 return code */.
d0310 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 int fd;
d0320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d0330 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
d0340 72 69 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65 riptor for pFile
d0350 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
d0360 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 xLockKey lockKey
d0370 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b ; /* Lookup k
d0380 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78 4c ey for the unixL
d0390 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ockInfo structur
d03a0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e e */. struct un
d03b0 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b ixFileId fileId;
d03c0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 /* Lookup
d03d0 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78 key for the unix
d03e0 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 20 2a OpenCnt struct *
d03f0 2f 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 /. struct stat
d0400 73 74 61 74 62 75 66 3b 20 20 20 20 20 20 20 20 statbuf;
d0410 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 /* Low-level
d0420 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e file information
d0430 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
d0440 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b xLockInfo *pLock
d0450 20 3d 20 30 3b 2f 2a 20 43 61 6e 64 69 64 61 74 = 0;/* Candidat
d0460 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f e unixLockInfo o
d0470 62 6a 65 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 bject */. struc
d0480 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 t unixOpenCnt *p
d0490 4f 70 65 6e 3b 20 20 20 20 20 2f 2a 20 43 61 6e Open; /* Can
d04a0 64 69 64 61 74 65 20 75 6e 69 78 4f 70 65 6e 43 didate unixOpenC
d04b0 6e 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 0a 20 20 nt object */..
d04c0 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 assert( unixMute
d04d0 78 48 65 6c 64 28 29 20 29 3b 0a 0a 20 20 2f 2a xHeld() );.. /*
d04e0 20 47 65 74 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 Get low-level i
d04f0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
d0500 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 77 the file that w
d0510 65 20 63 61 6e 20 75 73 65 64 20 74 6f 0a 20 20 e can used to.
d0520 2a 2a 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 ** create a uniq
d0530 75 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 ue name for the
d0540 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 66 64 20 file.. */. fd
d0550 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 72 63 = pFile->h;. rc
d0560 20 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74 = fstat(fd, &st
d0570 61 74 62 75 66 29 3b 0a 20 20 69 66 28 20 72 63 atbuf);. if( rc
d0580 21 3d 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 !=0 ){. pFile
d0590 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d05a0 72 6e 6f 3b 0a 23 69 66 64 65 66 20 45 4f 56 45 rno;.#ifdef EOVE
d05b0 52 46 4c 4f 57 0a 20 20 20 20 69 66 28 20 70 46 RFLOW. if( pF
d05c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3d 3d ile->lastErrno==
d05d0 45 4f 56 45 52 46 4c 4f 57 20 29 20 72 65 74 75 EOVERFLOW ) retu
d05e0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 3b rn SQLITE_NOLFS;
d05f0 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 .#endif. retu
d0600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
d0610 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 5f 5f 41 . }..#ifdef __A
d0620 50 50 4c 45 5f 5f 0a 20 20 2f 2a 20 4f 6e 20 4f PPLE__. /* On O
d0630 53 20 58 20 6f 6e 20 61 6e 20 6d 73 64 6f 73 20 S X on an msdos
d0640 66 69 6c 65 73 79 73 74 65 6d 2c 20 74 68 65 20 filesystem, the
d0650 69 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 73 20 inode number is
d0660 72 65 70 6f 72 74 65 64 0a 20 20 2a 2a 20 69 6e reported. ** in
d0670 63 6f 72 72 65 63 74 6c 79 20 66 6f 72 20 7a 65 correctly for ze
d0680 72 6f 2d 73 69 7a 65 20 66 69 6c 65 73 2e 20 20 ro-size files.
d0690 53 65 65 20 74 69 63 6b 65 74 20 23 33 32 36 30 See ticket #3260
d06a0 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 20 2a 2a 20 . To work. **
d06b0 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 around this prob
d06c0 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69 64 65 72 lem (we consider
d06d0 20 69 74 20 61 20 62 75 67 20 69 6e 20 4f 53 20 it a bug in OS
d06e0 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65 29 0a 20 X, not SQLite).
d06f0 20 2a 2a 20 77 65 20 61 6c 77 61 79 73 20 69 6e ** we always in
d0700 63 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 crease the file
d0710 73 69 7a 65 20 74 6f 20 31 20 62 79 20 77 72 69 size to 1 by wri
d0720 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 62 79 ting a single by
d0730 74 65 0a 20 20 2a 2a 20 70 72 69 6f 72 20 74 6f te. ** prior to
d0740 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 69 accessing the i
d0750 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 node number. Th
d0760 65 20 6f 6e 65 20 62 79 74 65 20 77 72 69 74 74 e one byte writt
d0770 65 6e 20 69 73 0a 20 20 2a 2a 20 61 6e 20 41 53 en is. ** an AS
d0780 43 49 49 20 27 53 27 20 63 68 61 72 61 63 74 65 CII 'S' characte
d0790 72 20 77 68 69 63 68 20 61 6c 73 6f 20 68 61 70 r which also hap
d07a0 70 65 6e 73 20 74 6f 20 62 65 20 74 68 65 20 66 pens to be the f
d07b0 69 72 73 74 20 62 79 74 65 0a 20 20 2a 2a 20 69 irst byte. ** i
d07c0 6e 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 n the header of
d07d0 65 76 65 72 79 20 53 51 4c 69 74 65 20 64 61 74 every SQLite dat
d07e0 61 62 61 73 65 2e 20 20 49 6e 20 74 68 69 73 20 abase. In this
d07f0 77 61 79 2c 20 69 66 20 74 68 65 72 65 0a 20 20 way, if there.
d0800 2a 2a 20 69 73 20 61 20 72 61 63 65 20 63 6f 6e ** is a race con
d0810 64 69 74 69 6f 6e 20 73 75 63 68 20 74 68 61 74 dition such that
d0820 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 another thread
d0830 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 has already popu
d0840 6c 61 74 65 64 0a 20 20 2a 2a 20 74 68 65 20 66 lated. ** the f
d0850 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
d0860 20 64 61 74 61 62 61 73 65 2c 20 6e 6f 20 64 61 database, no da
d0870 6d 61 67 65 20 69 73 20 64 6f 6e 65 2e 0a 20 20 mage is done..
d0880 2a 2f 0a 20 20 69 66 28 20 73 74 61 74 62 75 66 */. if( statbuf
d0890 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 .st_size==0 ){.
d08a0 20 20 20 72 63 20 3d 20 77 72 69 74 65 28 66 64 rc = write(fd
d08b0 2c 20 22 53 22 2c 20 31 29 3b 0a 20 20 20 20 69 , "S", 1);. i
d08c0 66 28 20 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 f( rc!=1 ){.
d08d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d08e0 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 20 IOERR;. }.
d08f0 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c 20 rc = fstat(fd,
d0900 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20 20 69 &statbuf);. i
d0910 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 f( rc!=0 ){.
d0920 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d0930 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d0940 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d0950 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 7d IOERR;. }. }
d0960 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73 65 .#endif.. memse
d0970 74 28 26 6c 6f 63 6b 4b 65 79 2c 20 30 2c 20 73 t(&lockKey, 0, s
d0980 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 3b izeof(lockKey));
d0990 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 64 . lockKey.fid.d
d09a0 65 76 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f ev = statbuf.st_
d09b0 64 65 76 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f dev;.#if OS_VXWO
d09c0 52 4b 53 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 RKS. lockKey.fi
d09d0 64 2e 70 49 64 20 3d 20 70 46 69 6c 65 2d 3e 70 d.pId = pFile->p
d09e0 49 64 3b 0a 23 65 6c 73 65 0a 20 20 6c 6f 63 6b Id;.#else. lock
d09f0 4b 65 79 2e 66 69 64 2e 69 6e 6f 20 3d 20 73 74 Key.fid.ino = st
d0a00 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 65 atbuf.st_ino;.#e
d0a10 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
d0a20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
d0a30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
d0a40 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 . if( threadsOv
d0a50 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
d0a60 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20 20 20 20 74 Locks<0 ){. t
d0a70 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
d0a80 42 65 68 61 76 69 6f 72 28 66 64 29 3b 0a 20 20 Behavior(fd);.
d0a90 7d 0a 20 20 6c 6f 63 6b 4b 65 79 2e 74 69 64 20 }. lockKey.tid
d0aa0 3d 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 = threadsOverrid
d0ab0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 eEachOthersLocks
d0ac0 20 3f 20 30 20 3a 20 70 74 68 72 65 61 64 5f 73 ? 0 : pthread_s
d0ad0 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 elf();.#endif.
d0ae0 66 69 6c 65 49 64 20 3d 20 6c 6f 63 6b 4b 65 79 fileId = lockKey
d0af0 2e 66 69 64 3b 0a 20 20 69 66 28 20 70 70 4c 6f .fid;. if( ppLo
d0b00 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c 6f ck!=0 ){. pLo
d0b10 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20 ck = lockList;.
d0b20 20 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20 while( pLock
d0b30 26 26 20 6d 65 6d 63 6d 70 28 26 6c 6f 63 6b 4b && memcmp(&lockK
d0b40 65 79 2c 20 26 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ey, &pLock->lock
d0b50 4b 65 79 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b Key, sizeof(lock
d0b60 4b 65 79 29 29 20 29 7b 0a 20 20 20 20 20 20 70 Key)) ){. p
d0b70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e Lock = pLock->pN
d0b80 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ext;. }. i
d0b90 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 f( pLock==0 ){.
d0ba0 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c pLock = sql
d0bb0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
d0bc0 65 6f 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 eof(*pLock) );.
d0bd0 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d if( pLock==
d0be0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
d0bf0 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
d0c00 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 goto exi
d0c10 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a t_findlockinfo;.
d0c20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 }. me
d0c30 6d 63 70 79 28 26 70 4c 6f 63 6b 2d 3e 6c 6f 63 mcpy(&pLock->loc
d0c40 6b 4b 65 79 2c 26 6c 6f 63 6b 4b 65 79 2c 73 69 kKey,&lockKey,si
d0c50 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 3b 0a zeof(lockKey));.
d0c60 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
d0c70 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f f = 1;. pLo
d0c80 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 ck->cnt = 0;.
d0c90 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
d0ca0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c pe = 0;. pL
d0cb0 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 ock->pNext = loc
d0cc0 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f kList;. pLo
d0cd0 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 ck->pPrev = 0;.
d0ce0 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 if( lockLis
d0cf0 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 t ) lockList->pP
d0d00 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 rev = pLock;.
d0d10 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c lockList = pL
d0d20 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ock;. }else{.
d0d30 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
d0d40 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
d0d50 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a ppLock = pLock;.
d0d60 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e }. if( ppOpen
d0d70 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e !=0 ){. pOpen
d0d80 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 = openList;.
d0d90 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 while( pOpen &&
d0da0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c memcmp(&fileId,
d0db0 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c &pOpen->fileId,
d0dc0 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 sizeof(fileId))
d0dd0 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 ){. pOpen
d0de0 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a = pOpen->pNext;.
d0df0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f }. if( pO
d0e00 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
d0e10 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f pOpen = sqlite3_
d0e20 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a malloc( sizeof(*
d0e30 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 pOpen) );.
d0e40 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a if( pOpen==0 ){.
d0e50 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c releaseL
d0e60 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a ockInfo(pLock);.
d0e70 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
d0e80 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
d0e90 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e goto exit_fin
d0ea0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 dlockinfo;.
d0eb0 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
d0ec0 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 pOpen, 0, sizeof
d0ed0 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20 (*pOpen));.
d0ee0 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d pOpen->fileId =
d0ef0 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70 fileId;. p
d0f00 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a Open->nRef = 1;.
d0f10 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 pOpen->pNe
d0f20 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 xt = openList;.
d0f30 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 if( openLis
d0f40 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 t ) openList->pP
d0f50 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 rev = pOpen;.
d0f60 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
d0f70 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a pen;. }else{.
d0f80 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 pOpen->nRe
d0f90 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
d0fa0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a ppOpen = pOpen;.
d0fb0 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f }..exit_findlo
d0fc0 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e ckinfo:. return
d0fd0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
d0fe0 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c we are currentl
d0ff0 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 y in a different
d1000 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65 thread than the
d1010 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65 thread that the
d1020 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67 .** unixFile arg
d1030 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f ument belongs to
d1040 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 , then transfer
d1050 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 ownership of the
d1060 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 unixFile.** ove
d1070 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 r to the current
d1080 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 thread..**.** A
d1090 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c unixFile is onl
d10a0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72 y owned by a thr
d10b0 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 ead on systems t
d10c0 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72 hat use LinuxThr
d10d0 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 eads..**.** Owne
d10e0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 rship transfer i
d10f0 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 s only allowed i
d1100 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 f the unixFile i
d1110 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f s currently unlo
d1120 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 cked..** If the
d1130 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b unixFile is lock
d1140 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 ed and an owners
d1150 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 hip is wrong, th
d1160 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c en return.** SQL
d1170 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c ITE_MISUSE. SQL
d1180 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
d1190 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 ed if everything
d11a0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 works..*/.#if S
d11b0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
d11c0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
d11d0 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e nux__).static in
d11e0 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 t transferOwners
d11f0 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 hip(unixFile *pF
d1200 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ile){. int rc;.
d1210 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c pthread_t hSel
d1220 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 f;. if( threads
d1230 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
d1240 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f rsLocks ){. /
d1250 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e * Ownership tran
d1260 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 sfers not needed
d1270 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20 on this system
d1280 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
d1290 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 LITE_OK;. }. h
d12a0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 Self = pthread_s
d12b0 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68 elf();. if( pth
d12c0 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 read_equal(pFile
d12d0 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b ->tid, hSelf) ){
d12e0 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73 . /* We are s
d12f0 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 till in the same
d1300 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f thread */. O
d1310 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e STRACE1("No-tran
d1320 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61 sfer, same threa
d1330 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 d\n");. retur
d1340 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d1350 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d1360 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
d1370 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e ){. /* We can
d1380 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 not change owner
d1390 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72 ship while we ar
d13a0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b e holding a lock
d13b0 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ! */. return
d13c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
d13d0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
d13e0 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 ransfer ownershi
d13f0 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20 p of %d from %d
d1400 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 to %d\n",.
d1410 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 pFile->h,
d1420 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c pFile->tid, hSel
d1430 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 f);. pFile->tid
d1440 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 = hSelf;. if (
d1450 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 pFile->pLock !=
d1460 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 NULL) {. rele
d1470 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c aseLockInfo(pFil
d1480 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 e->pLock);. r
d1490 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f c = findLockInfo
d14a0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e (pFile, &pFile->
d14b0 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f pLock, 0);. O
d14c0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 STRACE5("LOCK
d14d0 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 %d is now %s(%s
d14e0 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ,%d)\n", pFile->
d14f0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f h,. lo
d1500 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d1510 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 ->locktype),.
d1520 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d1530 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Name(pFile->pLoc
d1540 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 k->locktype), pF
d1550 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 ile->pLock->cnt)
d1560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
d1570 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
d1580 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d1590 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f ;. }.}.#else /
d15a0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f * if not SQLITE_
d15b0 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 THREADSAFE */.
d15c0 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 /* On single-thr
d15d0 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 eaded builds, ow
d15e0 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 nership transfer
d15f0 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 is a no-op */.#
d1600 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 define transfer
d1610 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c Ownership(X) SQL
d1620 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a ITE_OK.#endif /*
d1630 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
d1640 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 FE */.../*.** Th
d1650 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
d1660 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
d1670 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
d1680 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
d1690 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
d16a0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
d16b0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
d16c0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
d16d0 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
d16e0 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
d16f0 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
d1700 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d1710 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
d1720 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
d1730 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
d1740 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
d1750 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
d1760 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
d1770 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
d1780 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 unixCheckReserv
d1790 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
d17a0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
d17b0 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 esOut){. int rc
d17c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d17d0 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
d17e0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
d17f0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d1800 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 )id;.. Simulate
d1810 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
d1820 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d1830 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
d1840 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 );.. assert( pF
d1850 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 ile );. unixEnt
d1860 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 erMutex(); /* Be
d1870 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f cause pFile->pLo
d1880 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 ck is shared acr
d1890 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a oss threads */..
d18a0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 /* Check if a
d18b0 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 thread in this p
d18c0 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 rocess holds suc
d18d0 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 h a lock */. if
d18e0 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e ( pFile->pLock->
d18f0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d1900 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d1910 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 rved = 1;. }..
d1920 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
d1930 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
d1940 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
d1950 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f .. */.#ifndef _
d1960 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 _DJGPP__. if( !
d1970 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
d1980 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
d1990 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 k;. lock.l_wh
d19a0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d19b0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d19c0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
d19d0 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 E;. lock.l_le
d19e0 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e n = 1;. lock.
d19f0 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b l_type = F_WRLCK
d1a00 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 ;. if (-1 ==
d1a10 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d1a20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 F_GETLK, &lock))
d1a30 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 {. int tEr
d1a40 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d1a50 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d1a60 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d1a70 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d1a80 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
d1a90 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 RVEDLOCK);.
d1aa0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d1ab0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d1ac0 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e } else if( lock.
d1ad0 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 l_type!=F_UNLCK
d1ae0 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 ){. reserve
d1af0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d d = 1;. }. }
d1b00 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 .#endif. . uni
d1b10 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d1b20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 OSTRACE4("TEST
d1b30 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
d1b40 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c (unix)\n", pFil
d1b50 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 e->h, rc, reserv
d1b60 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 ed);.. *pResOut
d1b70 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
d1b80 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d1b90 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 66 69 6c ** Perform a fil
d1ba0 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 e locking operat
d1bb0 69 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f ion on a range o
d1bc0 66 20 62 79 74 65 73 20 69 6e 20 61 20 66 69 6c f bytes in a fil
d1bd0 65 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22 20 70 e..** The "op" p
d1be0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
d1bf0 62 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43 be one of F_RDLC
d1c00 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46 K, F_WRLCK, or F
d1c10 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72 _UNLCK..** Retur
d1c20 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 6f n 0 on success o
d1c30 72 20 2d 31 20 66 6f 72 20 66 61 69 6c 75 72 65 r -1 for failure
d1c40 2e 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77 . On failure, w
d1c50 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 0a 2a rite the error.*
d1c60 2a 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72 * code into *pEr
d1c70 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rcode..**.** If
d1c80 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 the SQLITE_WHOLE
d1c90 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69 _FILE_LOCKING bi
d1ca0 74 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e t is clear, then
d1cb0 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68 only lock.** th
d1cc0 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 e range of bytes
d1cd0 20 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 on the locking
d1ce0 70 61 67 65 20 62 65 74 77 65 65 6e 20 53 48 41 page between SHA
d1cf0 52 45 44 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a RED_FIRST and.**
d1d00 20 53 48 41 52 45 44 5f 53 49 5a 45 2e 20 20 49 SHARED_SIZE. I
d1d10 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 f SQLITE_WHOLE_F
d1d20 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73 ILE_LOCKING is s
d1d30 65 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c et, then lock al
d1d40 6c 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f 6d 20 l.** bytes from
d1d50 30 20 75 70 20 74 6f 20 62 75 74 20 6e 6f 74 20 0 up to but not
d1d60 69 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e including PENDIN
d1d70 47 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20 G_BYTE, and all
d1d80 62 79 74 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f bytes.** that fo
d1d90 6c 6c 6f 77 20 53 48 41 52 45 44 5f 46 49 52 53 llow SHARED_FIRS
d1da0 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 T..**.** In othe
d1db0 72 20 77 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49 r words, of SQLI
d1dc0 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f TE_WHOLE_FILE_LO
d1dd0 43 4b 49 4e 47 20 69 66 20 66 61 6c 73 65 20 28 CKING if false (
d1de0 74 68 65 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a the historical.*
d1df0 2a 20 64 65 66 61 75 6c 74 20 63 61 73 65 29 20 * default case)
d1e00 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61 then only lock a
d1e10 20 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20 small range of
d1e20 62 79 74 65 73 20 66 72 6f 6d 20 53 48 41 52 45 bytes from SHARE
d1e30 44 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75 D_FIRST.** throu
d1e40 67 68 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b gh SHARED_FIRST+
d1e50 53 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20 SHARED_SIZE-1.
d1e60 42 75 74 20 69 66 20 53 51 4c 49 54 45 5f 57 48 But if SQLITE_WH
d1e70 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 OLE_FILE_LOCKING
d1e80 20 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e is.** true then
d1e90 20 6c 6f 63 6b 20 65 76 65 72 79 20 62 79 74 65 lock every byte
d1ea0 20 69 6e 20 74 68 65 20 66 69 6c 65 20 65 78 63 in the file exc
d1eb0 65 70 74 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f ept for PENDING_
d1ec0 42 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45 BYTE and.** RESE
d1ed0 52 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a RVED_BYTE..**.**
d1ee0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d1ef0 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 LE_LOCKING=true
d1f00 6f 76 65 72 6c 61 70 73 20 53 51 4c 49 54 45 5f overlaps SQLITE_
d1f10 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
d1f20 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20 NG=false.** and
d1f30 73 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 so the locking s
d1f40 63 68 65 6d 65 73 20 61 72 65 20 63 6f 6d 70 61 chemes are compa
d1f50 74 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65 tible. One type
d1f60 20 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a of lock will.**
d1f70 20 65 66 66 65 63 74 69 76 65 6c 79 20 65 78 63 effectively exc
d1f80 6c 75 64 65 20 74 68 65 20 6f 74 68 65 72 20 74 lude the other t
d1f90 79 70 65 2e 20 20 54 68 65 20 72 65 61 73 6f 6e ype. The reason
d1fa0 20 66 6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a for using the.*
d1fb0 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 * SQLITE_WHOLE_F
d1fc0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 ILE_LOCKING=true
d1fd0 20 69 73 20 74 68 61 74 20 62 79 20 69 6e 64 69 is that by indi
d1fe0 63 61 74 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 cating the full
d1ff0 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65 range.** of byte
d2000 73 20 74 6f 20 62 65 20 72 65 61 64 20 6f 72 20 s to be read or
d2010 77 72 69 74 74 65 6e 2c 20 77 65 20 67 69 76 65 written, we give
d2020 20 68 69 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f hints to NFS to
d2030 20 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e help it.** main
d2040 74 61 69 6e 20 63 61 63 68 65 20 63 6f 68 65 72 tain cache coher
d2050 65 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74 ency. On the ot
d2060 68 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20 her hand, whole
d2070 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 file locking.**
d2080 69 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65 is slower, so we
d2090 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 don't want to u
d20a0 73 65 20 69 74 20 65 78 63 65 70 74 20 66 6f 72 se it except for
d20b0 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 NFS..*/.static
d20c0 69 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e int rangeLock(un
d20d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 ixFile *pFile, i
d20e0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72 nt op, int *pErr
d20f0 63 6f 64 65 29 7b 0a 20 20 73 74 72 75 63 74 20 code){. struct
d2100 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e flock lock;. in
d2110 74 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74 t rc;. lock.l_t
d2120 79 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b ype = op;. lock
d2130 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 .l_start = SHARE
d2140 44 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e D_FIRST;. lock.
d2150 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d2160 53 45 54 3b 0a 20 20 69 66 28 20 28 70 46 69 6c SET;. if( (pFil
d2170 65 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26 20 53 e->fileFlags & S
d2180 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 QLITE_WHOLE_FILE
d2190 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a _LOCKING)==0 ){.
d21a0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d21b0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
d21c0 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 rc = fcntl(pFi
d21d0 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d21e0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 &lock);. *pEr
d21f0 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 rcode = errno;.
d2200 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b }else{. lock
d2210 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 .l_len = 0;.
d2220 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 rc = fcntl(pFile
d2230 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d2240 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 ock);. *pErrc
d2250 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ode = errno;.
d2260 20 69 66 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46 if( NEVER(op==F
d2270 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28 _UNLCK) || rc!=(
d2280 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 -1) ){. loc
d2290 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 k.l_start = 0;.
d22a0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 lock.l_len
d22b0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d22c0 20 20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c rc = fcntl
d22d0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 (pFile->h, F_SET
d22e0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 LK, &lock);.
d22f0 20 20 69 66 28 20 41 4c 57 41 59 53 28 6f 70 21 if( ALWAYS(op!
d2300 3d 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d =F_UNLCK) && rc=
d2310 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 =(-1) ){.
d2320 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72 *pErrcode = err
d2330 6e 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b no;. lock
d2340 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 .l_type = F_UNLC
d2350 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e K;. lock.
d2360 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 l_start = SHARED
d2370 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 _FIRST;.
d2380 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a lock.l_len = 0;.
d2390 20 20 20 20 20 20 20 20 66 63 6e 74 6c 28 70 46 fcntl(pF
d23a0 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c ile->h, F_SETLK,
d23b0 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d &lock);. }
d23c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
d23d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
d23e0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 Lock the file w
d23f0 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 ith the lock spe
d2400 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 cified by parame
d2410 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f ter locktype - o
d2420 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ne.** of the fol
d2430 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
d2440 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 (1) SHARED_LOC
d2450 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 K.** (2) RES
d2460 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 ERVED_LOCK.**
d2470 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f (3) PENDING_LO
d2480 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 CK.** (4) EX
d2490 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a CLUSIVE_LOCK.**.
d24a0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 ** Sometimes whe
d24b0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 n requesting one
d24c0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 lock state, add
d24d0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 itional lock sta
d24e0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 tes.** are inser
d24f0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 ted in between.
d2500 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 The locking mig
d2510 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f ht fail on one o
d2520 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 f the later.** t
d2530 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 ransitions leavi
d2540 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 ng the lock stat
d2550 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
d2560 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 what it started
d2570 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 but.** still sh
d2580 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e ort of its goal.
d2590 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
d25a0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 chart shows the
d25b0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 allowed.** trans
d25c0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 itions and the i
d25d0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 nserted intermed
d25e0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a iate states:.**.
d25f0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d ** UNLOCKED -
d2600 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 > SHARED.** S
d2610 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 HARED -> RESERVE
d2620 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d2630 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
d2640 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 XCLUSIVE.** R
d2650 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 ESERVED -> (PEND
d2660 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d2670 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 E.** PENDING
d2680 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a -> EXCLUSIVE.**.
d2690 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
d26a0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 will only increa
d26b0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 se a lock. Use
d26c0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c the sqlite3OsUnl
d26d0 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ock().** routine
d26e0 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b to lower a lock
d26f0 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 ing level..*/.st
d2700 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 atic int unixLoc
d2710 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d2720 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d2730 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c ){. /* The foll
d2740 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65 73 20 owing describes
d2750 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
d2760 6f 6e 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 on of the variou
d2770 73 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a s locks and. **
d2780 20 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e lock transition
d2790 73 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 s in terms of th
d27a0 65 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 e POSIX advisory
d27b0 20 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c shared and excl
d27c0 75 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 usive. ** lock
d27d0 70 72 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c primitives (call
d27e0 65 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e ed read-locks an
d27f0 64 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 d write-locks be
d2800 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 low, to avoid.
d2810 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 ** confusion wit
d2820 68 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 h SQLite lock na
d2830 6d 65 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 mes). The algori
d2840 74 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 thms are complic
d2850 61 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 ated. ** slight
d2860 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 ly in order to b
d2870 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 e compatible wit
d2880 68 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d h windows system
d2890 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 s simultaneously
d28a0 0a 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 . ** accessing
d28b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
d28c0 65 20 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 e file, in case
d28d0 74 68 61 74 20 69 73 20 65 76 65 72 20 72 65 71 that is ever req
d28e0 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a uired.. **. **
d28f0 20 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 Symbols defined
d2900 20 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 in os.h indenti
d2910 66 79 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 fy the 'pending
d2920 62 79 74 65 27 20 61 6e 64 20 74 68 65 20 27 72 byte' and the 'r
d2930 65 73 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 eserved. ** byt
d2940 65 27 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20 e', each single
d2950 62 79 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e bytes at well kn
d2960 6f 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 own offsets, and
d2970 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 the 'shared byt
d2980 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 e. ** range', a
d2990 20 72 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79 range of 510 by
d29a0 74 65 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e tes at a well kn
d29b0 6f 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a own offset.. **
d29c0 0a 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 . ** To obtain
d29d0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 a SHARED lock, a
d29e0 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 read-lock is ob
d29f0 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 tained on the 'p
d2a00 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 ending. ** byte
d2a10 27 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 73 '. If this is s
d2a20 75 63 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e uccessful, a ran
d2a30 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68 dom byte from th
d2a40 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 e 'shared byte.
d2a50 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65 ** range' is re
d2a60 61 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 ad-locked and th
d2a70 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 e lock on the 'p
d2a80 65 6e 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c ending byte' rel
d2a90 65 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a eased.. **. **
d2aa0 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f A process may o
d2ab0 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53 nly obtain a RES
d2ac0 45 52 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 ERVED lock after
d2ad0 20 69 74 20 68 61 73 20 61 20 53 48 41 52 45 44 it has a SHARED
d2ae0 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 lock.. ** A RE
d2af0 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 SERVED lock is i
d2b00 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72 mplemented by gr
d2b10 61 62 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c abbing a write-l
d2b20 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ock on the. **
d2b30 27 72 65 73 65 72 76 65 64 20 62 79 74 65 27 2e 'reserved byte'.
d2b40 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 . **. ** A pr
d2b50 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f ocess may only o
d2b60 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 btain a PENDING
d2b70 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 lock after it ha
d2b80 73 20 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a s obtained a. *
d2b90 2a 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 * SHARED lock. A
d2ba0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 PENDING lock is
d2bb0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
d2bc0 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 obtaining a writ
d2bd0 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 e-lock. ** on t
d2be0 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 he 'pending byte
d2bf0 27 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 '. This ensures
d2c00 74 68 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 that no new SHAR
d2c10 45 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a ED locks can be.
d2c20 20 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 ** obtained, b
d2c30 75 74 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 ut existing SHAR
d2c40 45 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c ED locks are all
d2c50 6f 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e owed to persist.
d2c60 20 41 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 A process. **
d2c70 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f does not have to
d2c80 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 obtain a RESERV
d2c90 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 ED lock on the w
d2ca0 61 79 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 ay to a PENDING
d2cb0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 lock.. ** This
d2cc0 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 property is used
d2cd0 20 62 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68 by the algorith
d2ce0 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 m for rolling ba
d2cf0 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ck a journal fil
d2d00 65 0a 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63 e. ** after a c
d2d10 72 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rash.. **. **
d2d20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 An EXCLUSIVE loc
d2d30 6b 2c 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65 k, obtained afte
d2d40 72 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b r a PENDING lock
d2d50 20 69 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a is held, is. *
d2d60 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 * implemented by
d2d70 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 obtaining a wri
d2d80 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 te-lock on the e
d2d90 6e 74 69 72 65 20 27 73 68 61 72 65 64 20 62 79 ntire 'shared by
d2da0 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 te. ** range'.
d2db0 53 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 Since all other
d2dc0 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 20 61 20 locks require a
d2dd0 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 read-lock on one
d2de0 20 6f 66 20 74 68 65 20 62 79 74 65 73 0a 20 20 of the bytes.
d2df0 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 72 ** within this r
d2e00 61 6e 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72 ange, this ensur
d2e10 65 73 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 es that no other
d2e20 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 locks are held
d2e30 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 on the. ** data
d2e40 62 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a base. . **. **
d2e50 20 54 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69 The reason a si
d2e60 6e 67 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 ngle byte cannot
d2e70 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 be used instead
d2e80 20 6f 66 20 74 68 65 20 27 73 68 61 72 65 64 20 of the 'shared
d2e90 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 byte. ** range'
d2ea0 20 69 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65 is that some ve
d2eb0 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 rsions of window
d2ec0 73 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 s do not support
d2ed0 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a read-locks. By.
d2ee0 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 ** locking a r
d2ef0 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 andom byte from
d2f00 61 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 a range, concurr
d2f10 65 6e 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 ent SHARED locks
d2f20 20 6d 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 may exist. **
d2f30 65 76 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b even if the lock
d2f40 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20 75 73 ing primitive us
d2f50 65 64 20 69 73 20 61 6c 77 61 79 73 20 61 20 77 ed is always a w
d2f60 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a rite-lock.. */.
d2f70 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
d2f80 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 E_OK;. unixFile
d2f90 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d2fa0 69 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 ile*)id;. struc
d2fb0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a t unixLockInfo *
d2fc0 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 pLock = pFile->p
d2fd0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 Lock;. struct f
d2fe0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 lock lock;. int
d2ff0 20 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45 s = 0;. int tE
d3000 72 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 rrno;.. assert(
d3010 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
d3020 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE7("LOCK %d
d3030 20 25 73 20 77 61 73 20 25 73 28 25 73 2c 25 64 %s was %s(%s,%d
d3040 29 20 70 69 64 3d 25 64 20 28 75 6e 69 78 29 5c ) pid=%d (unix)\
d3050 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
d3060 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
d3070 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b (locktype), lock
d3080 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e typeName(pFile->
d3090 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 locktype),.
d30a0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c locktypeName(pL
d30b0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 ock->locktype),
d30c0 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 pLock->cnt , get
d30d0 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 pid());.. /* If
d30e0 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
d30f0 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 y a lock of this
d3100 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 type or more re
d3110 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 strictive on the
d3120 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 . ** unixFile,
d3130 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 do nothing. Don'
d3140 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f t use the end_lo
d3150 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
d3160 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 s. ** unixEnter
d3170 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 Mutex() hasn't b
d3180 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
d3190 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
d31a0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
d31b0 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 type ){. OSTR
d31c0 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE3("LOCK %d
d31d0 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %s ok (already
d31e0 68 65 6c 64 29 20 28 75 6e 69 78 29 5c 6e 22 2c held) (unix)\n",
d31f0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
d3200 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e locktypeN
d3210 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a ame(locktype));.
d3220 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d3230 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
d3240 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
d3250 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 cking sequence i
d3260 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 20 s correct.. **
d3270 20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d 6f (1) We never mo
d3280 76 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 ve from unlocked
d3290 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69 67 to anything hig
d32a0 68 65 72 20 74 68 61 6e 20 73 68 61 72 65 64 20 her than shared
d32b0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29 20 lock.. ** (2)
d32c0 53 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78 70 SQLite never exp
d32d0 6c 69 63 69 74 6c 79 20 72 65 71 75 65 73 74 73 licitly requests
d32e0 20 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a a pendig lock..
d32f0 20 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72 ** (3) A shar
d3300 65 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79 ed lock is alway
d3310 73 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65 s held when a re
d3320 73 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65 serve lock is re
d3330 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 quested.. */.
d3340 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
d3350 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
d3360 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
d3370 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
d3380 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
d3390 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
d33a0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d33b0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
d33c0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
d33d0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d33e0 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 CK );.. /* This
d33f0 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 mutex is needed
d3400 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e because pFile->
d3410 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 pLock is shared
d3420 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 across threads.
d3430 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d */. unixEnterM
d3440 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 utex();.. /* Ma
d3450 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 ke sure the curr
d3460 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 ent thread owns
d3470 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a the pFile.. */.
d3480 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f rc = transferO
d3490 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b wnership(pFile);
d34a0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
d34b0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 E_OK ){. unix
d34c0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
d34d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
d34e0 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 . pLock = pFile
d34f0 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 ->pLock;.. /* I
d3500 66 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 f some thread us
d3510 69 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73 ing this PID has
d3520 20 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 a lock via a di
d3530 66 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 fferent unixFile
d3540 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 *. ** handle th
d3550 61 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65 at precludes the
d3560 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c requested lock,
d3570 20 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 return BUSY..
d3580 2a 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d */. if( (pFile-
d3590 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b >locktype!=pLock
d35a0 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 ->locktype && .
d35b0 20 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d (pLock-
d35c0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 >locktype>=PENDI
d35d0 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 NG_LOCK || lockt
d35e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 ype>SHARED_LOCK)
d35f0 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ). ){. rc =
d3600 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
d3610 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a goto end_lock;.
d3620 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 }.. /* If a S
d3630 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 HARED lock is re
d3640 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d quested, and som
d3650 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 e thread using t
d3660 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a his PID already.
d3670 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45 ** has a SHARE
d3680 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f D or RESERVED lo
d3690 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 ck, then increme
d36a0 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 nt reference cou
d36b0 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 nts and. ** ret
d36c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 urn SQLITE_OK..
d36d0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
d36e0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
d36f0 26 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b && . (pLock
d3700 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ->locktype==SHAR
d3710 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b ED_LOCK || pLock
d3720 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ->locktype==RESE
d3730 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 RVED_LOCK) ){.
d3740 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
d3750 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
d3760 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
d3770 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
d3780 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
d3790 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b pLock->cnt>0 );
d37a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d37b0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
d37c0 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 CK;. pLock->c
d37d0 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d nt++;. pFile-
d37e0 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b >pOpen->nLock++;
d37f0 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f . goto end_lo
d3800 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 ck;. }... /* A
d3810 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 PENDING lock is
d3820 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 needed before a
d3830 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
d3840 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 D lock and befor
d3850 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 e. ** acquiring
d3860 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
d3870 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 ck. For the SHA
d3880 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 RED lock, the PE
d3890 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 NDING will. **
d38a0 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a be released.. *
d38b0 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d /. lock.l_len =
d38c0 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 1L;. lock.l_wh
d38d0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d38e0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
d38f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 =SHARED_LOCK .
d3900 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 || (locktype
d3910 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
d3920 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 && pFile->lockt
d3930 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ype<PENDING_LOCK
d3940 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e ). ){. lock.
d3950 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 l_type = (lockty
d3960 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f pe==SHARED_LOCK?
d3970 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 F_RDLCK:F_WRLCK)
d3980 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 ;. lock.l_sta
d3990 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 rt = PENDING_BYT
d39a0 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c E;. s = fcntl
d39b0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 (pFile->h, F_SET
d39c0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 LK, &lock);.
d39d0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 if( s==(-1) ){.
d39e0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
d39f0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
d3a00 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d3a10 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d3a20 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c , SQLITE_IOERR_L
d3a30 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
d3a40 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d3a50 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d3a60 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d3a70 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a tErrno;. }.
d3a80 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c goto end_l
d3a90 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ock;. }. }..
d3aa0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c . /* If control
d3ab0 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f gets to this po
d3ac0 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c int, then actual
d3ad0 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 ly go ahead and
d3ae0 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 make. ** operat
d3af0 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 ing system calls
d3b00 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
d3b10 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 ed lock.. */.
d3b20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d3b30 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d3b40 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d3b50 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 cnt==0 );. as
d3b60 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 sert( pLock->loc
d3b70 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 20 ktype==0 );..
d3b80 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 /* Now get the
d3b90 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 read-lock */.
d3ba0 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 s = rangeLock(p
d3bb0 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 File, F_RDLCK, &
d3bc0 74 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f 2a tErrno);.. /*
d3bd0 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 Drop the tempor
d3be0 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b ary PENDING lock
d3bf0 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 */. lock.l_s
d3c00 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 tart = PENDING_B
d3c10 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f YTE;. lock.l_
d3c20 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f len = 1L;. lo
d3c30 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e ck.l_type = F_UN
d3c40 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63 6e LCK;. if( fcn
d3c50 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 tl(pFile->h, F_S
d3c60 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20 ETLK, &lock)!=0
d3c70 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20 21 ){. if( s !
d3c80 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 = -1 ){.
d3c90 2f 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68 61 /* This could ha
d3ca0 70 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74 77 ppen with a netw
d3cb0 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20 ork mount */.
d3cc0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
d3cd0 72 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72 63 rno; . rc
d3ce0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d3cf0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d3d00 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d3d10 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 R_UNLOCK); .
d3d20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d3d30 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
d3d40 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d3d50 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d3d60 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
d3d70 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 goto end_loc
d3d80 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
d3d90 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 . if( s==(-1)
d3da0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
d3db0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d3dc0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d3dd0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d3de0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d3df0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d3e00 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d3e10 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d3e20 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d3e30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d3e40 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d3e50 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
d3e60 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 pFile->pOpe
d3e70 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 n->nLock++;.
d3e80 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 pLock->cnt = 1
d3e90 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
d3ea0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 if( locktype==EX
d3eb0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 CLUSIVE_LOCK &&
d3ec0 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a pLock->cnt>1 ){.
d3ed0 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 72 /* We are tr
d3ee0 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 6c ying for an excl
d3ef0 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 usive lock but a
d3f00 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 6e nother thread in
d3f10 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d this. ** sam
d3f20 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 e process is sti
d3f30 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 ll holding a sha
d3f40 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 red lock. */.
d3f50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
d3f60 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 Y;. }else{.
d3f70 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 /* The request w
d3f80 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 as for a RESERVE
d3f90 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c D or EXCLUSIVE l
d3fa0 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 ock. It is.
d3fb0 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 ** assumed that
d3fc0 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 there is a SHARE
d3fd0 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 D or greater loc
d3fe0 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 k on the file.
d3ff0 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 ** already..
d4000 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
d4010 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 0!=pFile->lockt
d4020 79 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e ype );. lock.
d4030 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b l_type = F_WRLCK
d4040 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c 6f ;. switch( lo
d4050 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 cktype ){.
d4060 63 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f case RESERVED_LO
d4070 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b CK:. lock
d4080 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 .l_start = RESER
d4090 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 VED_BYTE;.
d40a0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c s = fcntl(pFil
d40b0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
d40c0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 74 lock);. t
d40d0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d40e0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
d40f0 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 case EXCLUSI
d4100 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20 VE_LOCK:.
d4110 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 s = rangeLock(p
d4120 46 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 26 File, F_WRLCK, &
d4130 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20 tErrno);.
d4140 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 break;. de
d4150 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 fault:. a
d4160 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a ssert(0);. }.
d4170 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 if( s==(-1)
d4180 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
d4190 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d41a0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d41b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
d41c0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 K);. if( IS
d41d0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d41e0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
d41f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d4200 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rrno;. }.
d4210 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e }. }. ..#ifn
d4220 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 def NDEBUG. /*
d4230 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73 Set up the trans
d4240 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 action-counter c
d4250 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66 hange checking f
d4260 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 lags when. ** t
d4270 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f ransitioning fro
d4280 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20 m a SHARED to a
d4290 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 RESERVED lock.
d42a0 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 The change. **
d42b0 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52 from SHARED to R
d42c0 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68 ESERVED marks th
d42d0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 e beginning of a
d42e0 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 normal. ** wri
d42f0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f te operation (no
d4300 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 t a hot journal
d4310 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a rollback).. */.
d4320 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
d4330 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d _OK. && pFile-
d4340 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 >locktype<=SHARE
d4350 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 D_LOCK. && loc
d4360 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
d4370 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 LOCK. ){. pF
d4380 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 ile->transCntrCh
d4390 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c ng = 0;. pFil
d43a0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b e->dbUpdate = 0;
d43b0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f . pFile->inNo
d43c0 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 rmalWrite = 1;.
d43d0 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 }.#endif... if
d43e0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
d43f0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f ){. pFile->lo
d4400 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d4410 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f e;. pLock->lo
d4420 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d4430 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c e;. }else if( l
d4440 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
d4450 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 VE_LOCK ){. p
d4460 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d4470 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
d4480 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
d4490 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 pe = PENDING_LOC
d44a0 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b K;. }..end_lock
d44b0 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 :. unixLeaveMut
d44c0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 ex();. OSTRACE4
d44d0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
d44e0 25 73 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 %s (unix)\n", pF
d44f0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
d4500 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 Name(locktype),
d4510 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 . rc==SQLIT
d4520 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
d4530 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 ailed");. retur
d4540 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
d4550 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 20 64 65 lose all file de
d4560 73 63 72 69 70 74 6f 72 73 20 61 63 63 75 6d 75 scriptors accumu
d4570 61 74 65 64 20 69 6e 20 74 68 65 20 75 6e 69 78 ated in the unix
d4580 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64 OpenCnt->pUnused
d4590 20 6c 69 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c list..** If all
d45a0 20 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 such file descr
d45b0 69 70 74 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 iptors are close
d45c0 64 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 2c d without error,
d45d0 20 74 68 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20 the list is.**
d45e0 63 6c 65 61 72 65 64 20 61 6e 64 20 53 51 4c 49 cleared and SQLI
d45f0 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a TE_OK returned..
d4600 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
d4610 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
d4620 75 72 73 2c 20 74 68 65 6e 20 73 75 63 63 65 73 urs, then succes
d4630 73 66 75 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69 sfully closed fi
d4640 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a le descriptor.**
d4650 20 65 6e 74 72 69 65 73 20 61 72 65 20 72 65 6d entries are rem
d4660 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6c 69 oved from the li
d4670 73 74 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 st, and SQLITE_I
d4680 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 OERR_CLOSE retur
d4690 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c ned. .** not del
d46a0 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f eted and SQLITE_
d46b0 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 IOERR_CLOSE retu
d46c0 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 rned..*/ .static
d46d0 20 69 6e 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e int closePendin
d46e0 67 46 64 73 28 75 6e 69 78 46 69 6c 65 20 2a 70 gFds(unixFile *p
d46f0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 File){. int rc
d4700 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 = SQLITE_OK;. s
d4710 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
d4720 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 t *pOpen = pFile
d4730 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 ->pOpen;. UnixU
d4740 6e 75 73 65 64 46 64 20 2a 70 45 72 72 6f 72 20 nusedFd *pError
d4750 3d 20 30 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 = 0;. UnixUnuse
d4760 64 46 64 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e dFd *p;. UnixUn
d4770 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20 usedFd *pNext;.
d4780 20 66 6f 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55 for(p=pOpen->pU
d4790 6e 75 73 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78 nused; p; p=pNex
d47a0 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 t){. pNext =
d47b0 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 p->pNext;. if
d47c0 28 20 63 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29 ( close(p->fd) )
d47d0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d47e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
d47f0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
d4800 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b ITE_IOERR_CLOSE;
d4810 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 . p->pNext
d4820 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 = pError;.
d4830 70 45 72 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20 pError = p;.
d4840 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
d4850 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
d4860 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d }. }. pOpen-
d4870 3e 70 55 6e 75 73 65 64 20 3d 20 70 45 72 72 6f >pUnused = pErro
d4880 72 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a r;. return rc;.
d4890 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 }../*.** Add the
d48a0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d48b0 20 75 73 65 64 20 62 79 20 66 69 6c 65 20 68 61 used by file ha
d48c0 6e 64 6c 65 20 70 46 69 6c 65 20 74 6f 20 74 68 ndle pFile to th
d48d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a e corresponding.
d48e0 2a 2a 20 70 55 6e 75 73 65 64 20 6c 69 73 74 2e ** pUnused list.
d48f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
d4900 73 65 74 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 setPendingFd(uni
d4910 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 xFile *pFile){.
d4920 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
d4930 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 Cnt *pOpen = pFi
d4940 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 le->pOpen;. Uni
d4950 78 55 6e 75 73 65 64 46 64 20 2a 70 20 3d 20 70 xUnusedFd *p = p
d4960 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 File->pUnused;.
d4970 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 p->pNext = pOpe
d4980 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f n->pUnused;. pO
d4990 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 pen->pUnused = p
d49a0 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d ;. pFile->h = -
d49b0 31 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75 1;. pFile->pUnu
d49c0 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a sed = 0;.}../*.*
d49d0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
d49e0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
d49f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 e descriptor pFi
d4a00 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 le to locktype.
d4a10 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 locktype.** mus
d4a20 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c t be either NO_L
d4a30 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f OCK or SHARED_LO
d4a40 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 CK..**.** If the
d4a50 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
d4a60 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
d4a70 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
d4a80 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 at or below.**
d4a90 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
d4aa0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 cking level, thi
d4ab0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
d4ac0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
d4ad0 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 int unixUnlock(s
d4ae0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d4af0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
d4b00 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d4b10 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d4b20 64 3b 20 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66 d; /* The open f
d4b30 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ile */. struct
d4b40 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c unixLockInfo *pL
d4b50 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 53 74 72 ock; /* Str
d4b60 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e ucture describin
d4b70 67 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73 g current lock s
d4b80 74 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 tate */. struct
d4b90 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 flock lock;
d4ba0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
d4bb0 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 formation passed
d4bc0 20 69 6e 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f into fcntl() */
d4bd0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d4be0 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
d4bf0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
d4c00 64 65 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 74 de from this int
d4c10 65 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 erface */. int
d4c20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
d4c30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d4c40 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 The underlying f
d4c50 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
d4c60 2f 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 /. int tErrno;
d4c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d4c80 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
d4c90 64 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 de from system c
d4ca0 61 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20 all errors */..
d4cb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d4cc0 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e ;. OSTRACE7("UN
d4cd0 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
d4ce0 25 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 %d(%d,%d) pid=%d
d4cf0 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c (unix)\n", pFil
d4d00 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a e->h, locktype,.
d4d10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
d4d20 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c ktype, pFile->pL
d4d30 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 ock->locktype, p
d4d40 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 File->pLock->cnt
d4d50 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
d4d60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d4d70 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
d4d80 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d4d90 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 cktype<=locktype
d4da0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
d4db0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d4dc0 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 if( CHECK_THREAD
d4dd0 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ID(pFile) ){.
d4de0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
d4df0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 ISUSE;. }. uni
d4e00 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
d4e10 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 h = pFile->h;.
d4e20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e pLock = pFile->
d4e30 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 pLock;. assert(
d4e40 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 pLock->cnt!=0 )
d4e50 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c ;. if( pFile->l
d4e60 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d4e70 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OCK ){. asser
d4e80 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 t( pLock->lockty
d4e90 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pe==pFile->lockt
d4ea0 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c ype );. Simul
d4eb0 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
d4ec0 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 (1);. Simulat
d4ed0 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 eIOError( h=(-1)
d4ee0 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 ). SimulateI
d4ef0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
d4f00 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
d4f10 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 . /* When red
d4f20 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 ucing a lock suc
d4f30 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 6f h that other pro
d4f40 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 74 cesses can start
d4f50 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
d4f60 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
d4f70 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 e again, make su
d4f80 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 re that the.
d4f90 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 ** transaction c
d4fa0 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 ounter was updat
d4fb0 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f ed if any part o
d4fc0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 f the database.
d4fd0 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 ** file chang
d4fe0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e ed. If the tran
d4ff0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
d5000 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a is not updated,.
d5010 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e ** other con
d5020 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 nections to the
d5030 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 20 same file might
d5040 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 74 not realize that
d5050 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 . ** the file
d5060 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e 64 has changed and
d5070 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 hence might not
d5080 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 know to flush t
d5090 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 heir. ** cach
d50a0 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 61 e. The use of a
d50b0 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 6e stale cache can
d50c0 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 73 lead to databas
d50d0 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 e corruption..
d50e0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
d50f0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c pFile->inNormal
d5100 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 Write==0.
d5110 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70 || pFile->dbUp
d5120 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 date==0.
d5130 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 || pFile->trans
d5140 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 CntrChng==1 );.
d5150 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d pFile->inNorm
d5160 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e alWrite = 0;.#en
d5170 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f dif... if( lo
d5180 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d5190 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 OCK ){. if(
d51a0 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 rangeLock(pFile
d51b0 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 , F_RDLCK, &tErr
d51c0 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 no)==(-1) ){.
d51d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d51e0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d51f0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d5200 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 TE_IOERR_RDLOCK)
d5210 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 ;. if( IS
d5220 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d5230 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
d5240 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d5250 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
d5260 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 }. goto e
d5270 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 nd_unlock;.
d5280 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 }. }. loc
d5290 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c k.l_type = F_UNL
d52a0 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 CK;. lock.l_w
d52b0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
d52c0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 ;. lock.l_sta
d52d0 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 rt = PENDING_BYT
d52e0 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 E;. lock.l_le
d52f0 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28 n = 2L; assert(
d5300 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d PENDING_BYTE+1=
d5310 3d 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 29 =RESERVED_BYTE )
d5320 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 ;. if( fcntl(
d5330 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 h, F_SETLK, &loc
d5340 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 k)!=(-1) ){.
d5350 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d5360 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
d5370 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
d5380 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f tErrno = errno
d5390 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
d53a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d53b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d53c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
d53d0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d53e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d53f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d5400 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d5410 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d5420 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e goto end_un
d5430 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lock;. }. }.
d5440 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
d5450 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 NO_LOCK ){. s
d5460 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
d5470 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f t *pOpen;.. /
d5480 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 * Decrement the
d5490 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e shared lock coun
d54a0 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 68 ter. Release th
d54b0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a e lock using an.
d54c0 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f ** OS call o
d54d0 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 nly when all thr
d54e0 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 6d eads in this sam
d54f0 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 72 e process have r
d5500 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 eleased. ** t
d5510 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a he lock.. */.
d5520 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d pLock->cnt--
d5530 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d ;. if( pLock-
d5540 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 >cnt==0 ){.
d5550 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 lock.l_type = F
d5560 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f _UNLCK;. lo
d5570 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
d5580 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f EK_SET;. lo
d5590 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63 ck.l_start = loc
d55a0 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 k.l_len = 0L;.
d55b0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d55c0 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 rorBenign(1);.
d55d0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d55e0 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 ror( h=(-1) ).
d55f0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d5600 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 rorBenign(0);.
d5610 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c if( fcntl(h,
d5620 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d5630 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 !=(-1) ){.
d5640 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d5650 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 e = NO_LOCK;.
d5660 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d5670 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f tErrno = errno
d5680 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
d5690 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d56a0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d56b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
d56c0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 LOCK);. i
d56d0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d56e0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d56f0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d5700 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d5710 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
d5720 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d5730 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
d5740 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d5750 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 e = NO_LOCK;.
d5760 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
d5770 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
d5780 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 count of locks
d5790 61 67 61 69 6e 73 74 20 74 68 69 73 20 73 61 6d against this sam
d57a0 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 e file. When th
d57b0 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 e. ** count r
d57c0 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f eaches zero, clo
d57d0 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c se any other fil
d57e0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 77 68 e descriptors wh
d57f0 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a ose close. **
d5800 20 77 61 73 20 64 65 66 65 72 72 65 64 20 62 65 was deferred be
d5810 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74 61 6e cause of outstan
d5820 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 ding locks..
d5830 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 */. pOpen = p
d5840 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 File->pOpen;.
d5850 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b pOpen->nLock--;
d5860 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
d5870 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a en->nLock>=0 );.
d5880 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e if( pOpen->n
d5890 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Lock==0 ){.
d58a0 20 69 6e 74 20 72 63 32 20 3d 20 63 6c 6f 73 65 int rc2 = close
d58b0 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69 6c 65 PendingFds(pFile
d58c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
d58d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d58e0 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a rc = rc2;.
d58f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
d5900 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a }...end_unlock:.
d5910 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
d5920 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 ();. if( rc==SQ
d5930 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d LITE_OK ) pFile-
d5940 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d5950 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 type;. return r
d5960 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
d5970 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 function perfor
d5980 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 ms the parts of
d5990 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 the "close file"
d59a0 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 operation .** c
d59b0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 ommon to all loc
d59c0 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 king schemes. It
d59d0 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 closes the dire
d59e0 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a ctory and file.*
d59f0 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 * handles, if th
d5a00 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e ey are valid, an
d5a10 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 d sets all field
d5a20 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c s of the unixFil
d5a30 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 e.** structure t
d5a40 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 o 0..**.** It is
d5a50 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 *not* necessary
d5a60 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 to hold the mut
d5a70 65 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 ex when this rou
d5a80 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a tine is called,.
d5a90 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 ** even on VxWor
d5aa0 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c ks. A mutex wil
d5ab0 6c 20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e l be acquired on
d5ac0 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a VxWorks by the.
d5ad0 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 ** vxworksReleas
d5ae0 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e eFileId() routin
d5af0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
d5b00 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 closeUnixFile(s
d5b10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d5b20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
d5b30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d5b40 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 )id;. if( pFile
d5b50 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c ){. if( pFil
d5b60 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 e->dirfd>=0 ){.
d5b70 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 int err = c
d5b80 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 lose(pFile->dirf
d5b90 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 d);. if( er
d5ba0 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 r ){. pFi
d5bb0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d5bc0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 errno;. r
d5bd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d5be0 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 RR_DIR_CLOSE;.
d5bf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d5c00 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d pFile->dirfd=
d5c10 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 -1;. }.
d5c20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d }. if( pFile-
d5c30 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 >h>=0 ){. i
d5c40 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 nt err = close(p
d5c50 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 File->h);.
d5c60 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 if( err ){.
d5c70 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d5c80 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d5c90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
d5ca0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a TE_IOERR_CLOSE;.
d5cb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 }. }.#i
d5cc0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 f OS_VXWORKS.
d5cd0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 if( pFile->pId
d5ce0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 ){. if( pFi
d5cf0 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a le->isDelete ){.
d5d00 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 unlink(p
d5d10 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f File->pId->zCano
d5d20 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 nicalName);.
d5d30 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b }. vxwork
d5d40 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70 sReleaseFileId(p
d5d50 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 File->pId);.
d5d60 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 pFile->pId = 0
d5d70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
d5d80 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f OSTRACE2("CLO
d5d90 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 SE %-3d\n", pF
d5da0 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 ile->h);. Ope
d5db0 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 nCounter(-1);.
d5dc0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
d5dd0 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a File->pUnused);.
d5de0 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 memset(pFile
d5df0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 , 0, sizeof(unix
d5e00 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 File));. }. re
d5e10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d5e20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d5e30 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
d5e40 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 int unixClose(s
d5e50 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d5e60 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
d5e70 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 ITE_OK;. if( id
d5e80 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 ){. unixFile
d5e90 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d5ea0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e ile *)id;. un
d5eb0 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ixUnlock(id, NO_
d5ec0 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 LOCK);. unixE
d5ed0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
d5ee0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 if( pFile->pOpe
d5ef0 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 n && pFile->pOpe
d5f00 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 n->nLock ){.
d5f10 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
d5f20 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f e outstanding lo
d5f30 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 cks, do not actu
d5f40 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 ally close the f
d5f50 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a ile just. *
d5f60 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68 * yet because th
d5f70 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 at would clear t
d5f80 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 hose locks. Ins
d5f90 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 tead, add the fi
d5fa0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 le. ** desc
d5fb0 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d riptor to pOpen-
d5fc0 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 >pUnused list.
d5fd0 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d It will be autom
d5fe0 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 atically closed
d5ff0 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 . ** when t
d6000 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 he last lock is
d6010 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a cleared.. *
d6020 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 /. setPendi
d6030 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 ngFd(pFile);.
d6040 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f }. releaseLo
d6050 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c ckInfo(pFile->pL
d6060 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 ock);. releas
d6070 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e eOpenCnt(pFile->
d6080 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d pOpen);. rc =
d6090 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 closeUnixFile(i
d60a0 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 d);. unixLeav
d60b0 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 eMutex();. }.
d60c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
d60e0 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61 d of the posix a
d60f0 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 dvisory lock imp
d6100 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
d6110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
d6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d61c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f ************* No
d61e0 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a -op Locking ****
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d6210 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69 *.** Of the vari
d6220 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c ous locking impl
d6230 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 ementations avai
d6240 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62 lable, this is b
d6250 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d y far the.** sim
d6260 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 plest: locking
d6270 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 is ignored. No
d6280 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
d6290 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 to lock the data
d62a0 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 base.** file for
d62b0 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 reading or writ
d62c0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ing..**.** This
d62d0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 locking mode is
d62e0 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 appropriate for
d62f0 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 use on read-only
d6300 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 databases.** (e
d6310 78 3a 20 64 61 74 61 62 61 73 65 73 20 74 68 61 x: databases tha
d6320 74 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74 t are burned int
d6330 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 o CD-ROM, for ex
d6340 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a ample.) It can.
d6350 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 ** also be used
d6360 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 if the applicati
d6370 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 on employs some
d6380 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 external mechani
d6390 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 sm to.** prevent
d63a0 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 simultaneous ac
d63b0 63 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65 cess of the same
d63c0 20 64 61 74 61 62 61 73 65 20 62 79 20 74 77 6f database by two
d63d0 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 or more.** data
d63e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
d63f0 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20 . But there is
d6400 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f a serious risk o
d6410 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f f database.** co
d6420 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73 rruption if this
d6430 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 locking mode is
d6440 20 75 73 65 64 20 69 6e 20 73 69 74 75 61 74 69 used in situati
d6450 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70 ons where multip
d6460 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 le.** database c
d6470 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61 onnections are a
d6480 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
d6490 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
d64a0 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 at the same.** t
d64b0 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d ime and one or m
d64c0 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e ore of those con
d64d0 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69 nections are wri
d64e0 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 ting..*/..static
d64f0 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b int nolockCheck
d6500 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
d6510 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
d6520 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 ed, int *pResOut
d6530 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
d6540 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
d6550 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a *pResOut = 0;.
d6560 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d6570 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK;.}.static int
d6580 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 nolockLock(sqli
d6590 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 te3_file *NotUse
d65a0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 d, int NotUsed2)
d65b0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
d65c0 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
d65d0 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 otUsed2);. retu
d65e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d65f0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
d6600 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f kUnlock(sqlite3_
d6610 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 file *NotUsed, i
d6620 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 nt NotUsed2){.
d6630 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d6640 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
d6650 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ed2);. return S
d6660 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
d6670 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
d6680 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
d6690 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c nolockClose(sql
d66a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
d66b0 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 . return closeU
d66c0 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a nixFile(id);.}..
d66d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
d66e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
d66f0 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 no-op lock imple
d6700 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a mentation ******
d6710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
d6720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
d6770 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d6780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d67a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d67b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d67c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
d67d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
d67e0 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e dot-file Lockin
d67f0 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a g **************
d6800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6810 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 .**.** The dotfi
d6820 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 le locking imple
d6830 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74 mentation uses t
d6840 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 he existance of
d6850 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a separate lock.**
d6860 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 files in order
d6870 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
d6880 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 s to the databas
d6890 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 6f e. This works o
d68a0 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 n just.** about
d68b0 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65 6d every filesystem
d68c0 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 imaginable. Bu
d68d0 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72 69 t there are seri
d68e0 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a ous downsides:.*
d68f0 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65 *.** (1) The
d6900 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 re is zero concu
d6910 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c rrency. A singl
d6920 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 e reader blocks
d6930 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 all other.**
d6940 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 connections
d6950 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 from reading or
d6960 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 writing the dat
d6970 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 abase..**.**
d6980 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61 74 (2) An applicat
d6990 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f 77 ion crash or pow
d69a0 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 er loss can leav
d69b0 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c e stale lock fil
d69c0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 69 es.** si
d69d0 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 tting around tha
d69e0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 t need to be cle
d69f0 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a ared manually..*
d6a00 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 *.** Nevertheles
d6a10 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 s, a dotlock is
d6a20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c an appropriate l
d6a30 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 ocking mode for
d6a40 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 use if no.** oth
d6a50 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 er locking strat
d6a60 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c 65 egy is available
d6a70 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 ..**.** Dotfile
d6a80 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 locking works by
d6a90 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c 65 creating a file
d6aa0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 in the same dir
d6ab0 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a ectory as the.**
d6ac0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 69 database and wi
d6ad0 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
d6ae0 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f but with a ".lo
d6af0 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 ck" extension ad
d6b00 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 ded..** The exis
d6b10 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 tance of a lock
d6b20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 file implies an
d6b30 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
d6b40 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a All other lock.
d6b50 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45 44 ** types (SHARED
d6b60 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e 44 , RESERVED, PEND
d6b70 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64 20 ING) are mapped
d6b80 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a into EXCLUSIVE..
d6b90 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 */../*.** The fi
d6ba0 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64 20 le suffix added
d6bb0 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73 65 to the data base
d6bc0 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 filename in ord
d6bd0 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 er to create the
d6be0 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a .** lock file..*
d6bf0 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 /.#define DOTLOC
d6c00 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 K_SUFFIX ".lock"
d6c10 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
d6c20 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
d6c30 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
d6c40 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
d6c50 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
d6c60 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
d6c70 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
d6c80 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
d6c90 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 ck is held, set
d6ca0 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 *pResOut.** to a
d6cb0 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
d6cc0 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f otherwise *pResO
d6cd0 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 ut is set to zer
d6ce0 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 o. The return v
d6cf0 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 alue.** is set t
d6d00 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 o SQLITE_OK unle
d6d10 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 ss an I/O error
d6d20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f occurs during lo
d6d30 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a ck checking..**.
d6d40 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f ** In dotfile lo
d6d50 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61 20 cking, either a
d6d60 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20 69 lock exists or i
d6d70 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 t does not. So
d6d80 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 in this.** varia
d6d90 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 tion of CheckRes
d6da0 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 ervedLock(), *pR
d6db0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
d6dc0 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b true if any lock
d6dd0 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 .** is held on t
d6de0 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 he file and fals
d6df0 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 e if the file is
d6e00 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 unlocked..*/.st
d6e10 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b atic int dotlock
d6e20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d6e30 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d6e40 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d6e50 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d6e60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d6e70 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d6e80 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d6e90 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d6ea0 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 .. SimulateIOEr
d6eb0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d6ec0 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d6ed0 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 SERVEDLOCK; );.
d6ee0 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d6ef0 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b e );.. /* Check
d6f00 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d6f10 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d6f20 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d6f30 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d6f40 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d6f50 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 OCK ){. /* Ei
d6f60 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 ther this connec
d6f70 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 tion or some oth
d6f80 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e er connection in
d6f90 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
d6fa0 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 s. ** holds a
d6fb0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c lock on the fil
d6fc0 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 e. No need to c
d6fd0 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f heck further. */
d6fe0 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 . reserved =
d6ff0 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
d7000 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 /* The lock is h
d7010 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 eld if and only
d7020 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 if the lockfile
d7030 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f exists */. co
d7040 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 nst char *zLockF
d7050 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 ile = (const cha
d7060 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e r*)pFile->lockin
d7070 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 gContext;. re
d7080 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73 28 served = access(
d7090 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 zLockFile, 0)==0
d70a0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 ;. }. OSTRACE4
d70b0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
d70c0 64 20 25 64 20 25 64 20 28 64 6f 74 6c 6f 63 6b d %d %d (dotlock
d70d0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
d70e0 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20 rc, reserved);.
d70f0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
d7100 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
d7110 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b c;.}../*.** Lock
d7120 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
d7130 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
d7140 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c d by parameter l
d7150 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a ocktype - one.**
d7160 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
d7170 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
d7180 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
d7190 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
d71a0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
d71b0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
d71c0 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
d71d0 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
d71e0 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
d71f0 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
d7200 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
d7210 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
d7220 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
d7230 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
d7240 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
d7250 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
d7260 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
d7270 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
d7280 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
d7290 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
d72a0 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
d72b0 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
d72c0 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
d72d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
d72e0 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
d72f0 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
d7300 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
d7310 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
d7320 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
d7330 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
d7340 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
d7350 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
d7360 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
d7370 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
d7380 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
d7390 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
d73a0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
d73b0 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
d73c0 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
d73d0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
d73e0 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
d73f0 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 lock. Use the s
d7400 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
d7410 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c .** routine to l
d7420 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c ower a locking l
d7430 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 evel..**.** With
d7440 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 dotfile locking
d7450 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 , we really only
d7460 20 73 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 support state (
d7470 34 29 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 4): EXCLUSIVE..*
d7480 2a 20 42 75 74 20 77 65 20 74 72 61 63 6b 20 74 * But we track t
d7490 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 he other locking
d74a0 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c levels internal
d74b0 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ly..*/.static in
d74c0 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 t dotlockLock(sq
d74d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d74e0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
d74f0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d7500 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d7510 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 d;. int fd;. c
d7520 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d har *zLockFile =
d7530 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e (char *)pFile->
d7540 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
d7550 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
d7560 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 E_OK;... /* If
d7570 77 65 20 68 61 76 65 20 61 6e 79 20 6c 6f 63 6b we have any lock
d7580 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 , then the lock
d7590 66 69 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 file already exi
d75a0 73 74 73 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 sts. All we hav
d75b0 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20 e. ** to do is
d75c0 61 64 6a 75 73 74 20 6f 75 72 20 69 6e 74 65 72 adjust our inter
d75d0 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 nal record of th
d75e0 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 e lock level..
d75f0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d7600 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f locktype > NO_LO
d7610 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
d7620 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d7630 74 79 70 65 3b 0a 23 69 66 20 21 4f 53 5f 56 58 type;.#if !OS_VX
d7640 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77 WORKS. /* Alw
d7650 61 79 73 20 75 70 64 61 74 65 20 74 68 65 20 74 ays update the t
d7660 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 imestamp on the
d7670 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 old file */.
d7680 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 utimes(zLockFile
d7690 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a , NULL);.#endif.
d76a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d76b0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d76c0 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 * grab an exclus
d76d0 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 ive lock */. fd
d76e0 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c = open(zLockFil
d76f0 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 e,O_RDONLY|O_CRE
d7700 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 30 29 3b AT|O_EXCL,0600);
d7710 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 . if( fd<0 ){.
d7720 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 /* failed to
d7730 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20 open/create the
d7740 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c file, someone el
d7750 73 65 20 6d 61 79 20 68 61 76 65 20 73 74 6f 6c se may have stol
d7760 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 en the lock */.
d7770 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d7780 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 errno;. if( E
d7790 45 58 49 53 54 20 3d 3d 20 74 45 72 72 6e 6f 20 EXIST == tErrno
d77a0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
d77b0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
d77c0 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 63 else {. rc
d77d0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d77e0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d77f0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d7800 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 R_LOCK);. i
d7810 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d7820 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d7830 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d7840 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d7850 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 }. }. ret
d7860 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a 20 20 69 urn rc;. } . i
d7870 66 28 20 63 6c 6f 73 65 28 66 64 29 20 29 7b 0a f( close(fd) ){.
d7880 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d7890 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d78a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
d78b0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 ERR_CLOSE;. }.
d78c0 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 . /* got it, s
d78d0 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 et the type and
d78e0 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 return ok */. p
d78f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d7900 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 locktype;. ret
d7910 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
d7920 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
d7930 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
d7940 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
d7950 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
d7960 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
d7970 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
d7980 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
d7990 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
d79a0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
d79b0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d79c0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
d79d0 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
d79e0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
d79f0 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
d7a00 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
d7a10 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 -op..**.** When
d7a20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d7a30 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 l reaches NO_LOC
d7a40 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f K, delete the lo
d7a50 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 ck file..*/.stat
d7a60 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e ic int dotlockUn
d7a70 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
d7a80 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
d7a90 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ype) {. unixFil
d7aa0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d7ab0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 File*)id;. char
d7ac0 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 *zLockFile = (c
d7ad0 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 har *)pFile->loc
d7ae0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 kingContext;..
d7af0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d7b00 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c . OSTRACE5("UNL
d7b10 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
d7b20 64 20 70 69 64 3d 25 64 20 28 64 6f 74 6c 6f 63 d pid=%d (dotloc
d7b30 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c k)\n", pFile->h,
d7b40 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 locktype,.. p
d7b50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 File->locktype,
d7b60 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 getpid());. ass
d7b70 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 ert( locktype<=S
d7b80 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d7b90 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 . /* no-op if p
d7ba0 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 ossible */. if(
d7bb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d7bc0 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 ==locktype ){.
d7bd0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d7be0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f OK;. }.. /* To
d7bf0 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 73 68 downgrade to sh
d7c00 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 ared, simply upd
d7c10 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c ate our internal
d7c20 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 notion of the.
d7c30 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 ** lock state.
d7c40 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 No need to mess
d7c50 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 6f with the file o
d7c60 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 n disk.. */. i
d7c70 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
d7c80 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
d7c90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d7ca0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
d7cb0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d7cc0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a _OK;. }. . /*
d7cd0 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b To fully unlock
d7ce0 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 64 the database, d
d7cf0 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 elete the lock f
d7d00 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ile */. assert(
d7d10 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
d7d20 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 CK );. if( unli
d7d30 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b nk(zLockFile) ){
d7d40 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b . int rc = 0;
d7d50 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
d7d60 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 = errno;. if(
d7d70 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e ENOENT != tErrn
d7d80 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 o ){. rc =
d7d90 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d7da0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d7db0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d7dc0 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 NLOCK);. }.
d7dd0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d7de0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d7df0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d7e00 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d7e10 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d7e20 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c . }. pFile->l
d7e30 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
d7e40 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 K;. return SQLI
d7e50 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
d7e60 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d Close a file. M
d7e70 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
d7e80 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 k has been relea
d7e90 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 sed before closi
d7ea0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
d7eb0 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 t dotlockClose(s
d7ec0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d7ed0 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 {. int rc;. i
d7ee0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 f( id ){. uni
d7ef0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d7f00 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
d7f10 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 dotlockUnlock(
d7f20 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 id, NO_LOCK);.
d7f30 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
d7f40 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
d7f50 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 text);. }. rc
d7f60 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 = closeUnixFile(
d7f70 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 id);. return rc
d7f80 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}./***********
d7f90 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 ******* End of t
d7fa0 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b he dot-file lock
d7fb0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d7fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7fd0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
d7fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8020 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/../**********
d8030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8070 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
d8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d8090 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b Begin flock Lock
d80a0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ing ************
d80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d80c0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 ****.**.** Use t
d80d0 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 he flock() syste
d80e0 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c m call to do fil
d80f0 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a e locking..**.**
d8100 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 flock() locking
d8110 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c is like dot-fil
d8120 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 e locking in tha
d8130 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a t the various.**
d8140 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b fine-grain lock
d8150 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f ing levels suppo
d8160 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 rted by SQLite a
d8170 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 re collapsed int
d8180 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 o.** a single ex
d8190 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 clusive lock. I
d81a0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53 n other words, S
d81b0 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c HARED, RESERVED,
d81c0 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 and.** PENDING
d81d0 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73 61 locks are the sa
d81e0 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45 me thing as an E
d81f0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
d8200 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 SQLite.** still
d8210 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 works when you d
d8220 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 o this, but conc
d8230 75 72 72 65 6e 63 79 20 69 73 20 72 65 64 75 63 urrency is reduc
d8240 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 ed since.** only
d8250 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 a single proces
d8260 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 s can be reading
d8270 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 the database at
d8280 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f a time..**.** O
d8290 6d 69 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e mit this section
d82a0 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c if SQLITE_ENABL
d82b0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d82c0 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 is turned off or
d82d0 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 if.** compiling
d82e0 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f for VXWORKS..*/
d82f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
d8300 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
d8310 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a && !OS_VXWORKS.
d8320 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
d8330 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
d8340 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
d8350 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
d8360 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
d8370 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
d8380 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
d8390 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
d83a0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
d83b0 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
d83c0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
d83d0 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
d83e0 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
d83f0 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
d8400 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
d8410 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
d8420 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
d8430 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
d8440 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
d8450 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 tatic int flockC
d8460 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
d8470 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d8480 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
d8490 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
d84a0 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 ITE_OK;. int re
d84b0 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e served = 0;. un
d84c0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d84d0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d84e0 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 . SimulateIOEr
d84f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d8500 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d8510 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 SERVEDLOCK; );.
d8520 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d8530 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 e );. . /* Che
d8540 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 ck if a thread i
d8550 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 n this process h
d8560 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b olds such a lock
d8570 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d8580 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 >locktype>SHARED
d8590 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
d85a0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 erved = 1;. }.
d85b0 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 . /* Otherwise
d85c0 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 see if some oth
d85d0 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 er process holds
d85e0 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 it. */. if( !r
d85f0 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f eserved ){. /
d8600 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 * attempt to get
d8610 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 the lock */.
d8620 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b int lrc = flock
d8630 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f (pFile->h, LOCK_
d8640 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 EX | LOCK_NB);.
d8650 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 if( !lrc ){.
d8660 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 /* got the
d8670 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 lock, unlock it
d8680 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66 */. lrc = f
d8690 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d86a0 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 OCK_UN);. i
d86b0 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 f ( lrc ) {.
d86c0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
d86d0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
d86e0 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 /* unlock failed
d86f0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a with an error *
d8700 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20 /. lrc =
d8710 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d8720 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d8730 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d8740 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 NLOCK); .
d8750 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d8760 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 OR(lrc) ){.
d8770 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d8780 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d8790 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c rc = l
d87a0 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
d87b0 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 }. } else
d87c0 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 {. int tEr
d87d0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d87e0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
d87f0 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e . /* someon
d8800 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76 e else might hav
d8810 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f e it reserved */
d8820 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c . lrc = sql
d8830 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d8840 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d8850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
d8860 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 53 ); . if( IS
d8870 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 _LOCK_ERROR(lrc)
d8880 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d8890 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d88a0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 Errno;. r
d88b0 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d c = lrc;. }
d88c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 . }. }. OST
d88d0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c RACE4("TEST WR-L
d88e0 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 66 6c OCK %d %d %d (fl
d88f0 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ock)\n", pFile->
d8900 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
d8910 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
d8920 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d8930 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 CK_ERRORS. if(
d8940 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 (rc & SQLITE_IOE
d8950 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f RR) == SQLITE_IO
d8960 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ERR ){. rc =
d8970 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 SQLITE_OK;. r
d8980 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a 23 eserved=1;. }.#
d8990 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
d89a0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 IGNORE_FLOCK_LOC
d89b0 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a 70 K_ERRORS */. *p
d89c0 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
d89d0 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
d89e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
d89f0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
d8a00 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
d8a10 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
d8a20 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
d8a30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
d8a40 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
d8a50 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
d8a60 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
d8a70 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
d8a80 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
d8a90 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
d8aa0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
d8ab0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
d8ac0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
d8ad0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
d8ae0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
d8af0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
d8b00 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
d8b10 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
d8b20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
d8b30 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
d8b40 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
d8b50 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
d8b60 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
d8b70 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
d8b80 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
d8b90 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
d8ba0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
d8bb0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
d8bc0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
d8bd0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
d8be0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
d8bf0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
d8c00 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
d8c10 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d8c20 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
d8c30 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
d8c40 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d8c50 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
d8c60 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
d8c70 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
d8c80 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
d8c90 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b SIVE.**.** flock
d8ca0 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 () only really s
d8cb0 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 upport EXCLUSIVE
d8cc0 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 locks. We trac
d8cd0 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a k intermediate.*
d8ce0 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e * lock states in
d8cf0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c the sqlite3_fil
d8d00 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 e structure, but
d8d10 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 all locks SHARE
d8d20 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 D or.** above ar
d8d30 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 e really EXCLUSI
d8d40 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 VE locks and exc
d8d50 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 lude all other p
d8d60 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a rocesses from.**
d8d70 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 access the file
d8d80 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
d8d90 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
d8da0 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
d8db0 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 Use the sqlite3
d8dc0 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f OsUnlock().** ro
d8dd0 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 utine to lower a
d8de0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a locking level..
d8df0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c */.static int fl
d8e00 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f ockLock(sqlite3_
d8e10 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d8e20 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 cktype) {. int
d8e30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d8e40 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d8e50 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d8e60 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 d;.. assert( pF
d8e70 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 ile );.. /* if
d8e80 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 we already have
d8e90 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 a lock, it is ex
d8ea0 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 clusive. . **
d8eb0 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 Just adjust leve
d8ec0 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 l and punt on ou
d8ed0 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 tta here. */. i
d8ee0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f (pFile->lockty
d8ef0 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a pe > NO_LOCK) {.
d8f00 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d8f10 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d8f20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d8f30 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d8f40 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 * grab an exclus
d8f50 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 ive lock */. .
d8f60 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65 if (flock(pFile
d8f70 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c ->h, LOCK_EX | L
d8f80 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69 OCK_NB)) {. i
d8f90 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d8fa0 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74 o;. /* didn't
d8fb0 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75 get, must be bu
d8fc0 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 sy */. rc = s
d8fd0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d8fe0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d8ff0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d9000 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f CK);. if( IS_
d9010 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d9020 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d9030 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d9040 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 o;. }. } els
d9050 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69 e {. /* got i
d9060 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 t, set the type
d9070 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f and return ok */
d9080 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d9090 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d90a0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
d90b0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 "LOCK %d %s %
d90c0 73 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46 s (flock)\n", pF
d90d0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
d90e0 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 Name(locktype),
d90f0 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d . rc==
d9100 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 SQLITE_OK ? "ok"
d9110 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 : "failed");.#i
d9120 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f fdef SQLITE_IGNO
d9130 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 RE_FLOCK_LOCK_ER
d9140 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 RORS. if( (rc &
d9150 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d SQLITE_IOERR) =
d9160 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 = SQLITE_IOERR )
d9170 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
d9180 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 E_BUSY;. }.#end
d9190 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e if /* SQLITE_IGN
d91a0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
d91b0 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 RRORS */. retur
d91c0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
d91d0 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e Lower the lockin
d91e0 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 g level on file
d91f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
d9200 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
d9210 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
d9220 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
d9230 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
d9240 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
d9250 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
d9260 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
d9270 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
d9280 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
d9290 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
d92a0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
d92b0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
d92c0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
d92d0 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 t flockUnlock(sq
d92e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d92f0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
d9300 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d9310 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d9320 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 d;. . assert(
d9330 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 pFile );. OSTRA
d9340 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 CE5("UNLOCK %d
d9350 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 %d was %d pid=%d
d9360 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 (flock)\n", pFi
d9370 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
d9380 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c . pFil
d9390 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
d93a0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 pid());. assert
d93b0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
d93c0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 ED_LOCK );. .
d93d0 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 /* no-op if poss
d93e0 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 ible */. if( pF
d93f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c ile->locktype==l
d9400 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
d9410 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d9420 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 . }. . /* sha
d9430 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 red can just be
d9440 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 set because we a
d9450 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 lways have an ex
d9460 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 clusive */. if
d9470 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 (locktype==SHARE
d9480 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 D_LOCK) {. pF
d9490 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d94a0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 locktype;. re
d94b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d94c0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 }. . /* no,
d94d0 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 really, unlock.
d94e0 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c */. int rc = fl
d94f0 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f ock(pFile->h, LO
d9500 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63 CK_UN);. if (rc
d9510 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74 ) {. int r, t
d9520 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d9530 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72 r = sqliteErr
d9540 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d9550 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d9560 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d9570 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d9580 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20 RROR(r) ){.
d9590 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d95a0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d95b0 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
d95c0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 IGNORE_FLOCK_LOC
d95d0 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66 28 K_ERRORS. if(
d95e0 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 (r & SQLITE_IOE
d95f0 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f RR) == SQLITE_IO
d9600 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20 3d ERR ){. r =
d9610 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d9620 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
d9630 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d9640 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f K_LOCK_ERRORS */
d9650 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72 6e . . return
d9660 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 r;. } else {.
d9670 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d9680 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 pe = NO_LOCK;.
d9690 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d96a0 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a OK;. }.}../*.**
d96b0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
d96c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f /.static int flo
d96d0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f ckClose(sqlite3_
d96e0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
d96f0 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f 63 ( id ){. floc
d9700 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c kUnlock(id, NO_L
d9710 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 OCK);. }. retu
d9720 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 rn closeUnixFile
d9730 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 (id);.}..#endif
d9740 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
d9750 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 _LOCKING_STYLE &
d9760 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a & !OS_VXWORK */.
d9770 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d9780 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 ***** End of the
d9790 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c flock lock impl
d97a0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
d97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d9810 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9860 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d9870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
d9880 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 65 Named Semaphore
d9890 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a Locking *******
d98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d98b0 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 *.**.** Named se
d98c0 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
d98d0 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 is only supporte
d98e0 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a d on VxWorks..**
d98f0 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f .** Semaphore lo
d9900 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f cking is like do
d9910 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 6b t-lock and flock
d9920 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61 6c in that it real
d9930 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f ly only.** suppo
d9940 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c 6f rts EXCLUSIVE lo
d9950 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 73 cking. Only a s
d9960 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 ingle process ca
d9970 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 0a n read or write.
d9980 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
d9990 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e 20 file at a time.
d99a0 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70 6f This reduces po
d99b0 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 65 tential concurre
d99c0 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b 65 ncy, but.** make
d99d0 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 s the lock imple
d99e0 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 65 mentation much e
d99f0 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53 asier..*/.#if OS
d9a00 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 _VXWORKS../*.**
d9a10 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
d9a20 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
d9a30 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
d9a40 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
d9a50 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
d9a60 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
d9a70 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
d9a80 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
d9a90 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
d9aa0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
d9ab0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
d9ac0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
d9ad0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
d9ae0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
d9af0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
d9b00 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
d9b10 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
d9b20 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
d9b30 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
d9b40 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 nt semCheckReser
d9b50 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
d9b60 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
d9b70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 ResOut) {. int
d9b80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d9b90 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d int reserved =
d9ba0 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 0;. unixFile *
d9bb0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d9bc0 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 e*)id;.. Simula
d9bd0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
d9be0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
d9bf0 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
d9c00 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 ; );. . assert
d9c10 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a ( pFile );.. /*
d9c20 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 Check if a thre
d9c30 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 ad in this proce
d9c40 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 ss holds such a
d9c50 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 lock */. if( pF
d9c60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d9c70 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d9c80 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 reserved = 1;.
d9c90 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 }. . /* Other
d9ca0 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 wise see if some
d9cb0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 other process h
d9cc0 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 olds it. */. if
d9cd0 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 ( !reserved ){.
d9ce0 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d sem_t *pSem =
d9cf0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 pFile->pOpen->p
d9d00 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 Sem;. struct
d9d10 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a 20 stat statBuf;..
d9d20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 if( sem_trywa
d9d30 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a it(pSem)==-1 ){.
d9d40 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
d9d50 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d9d60 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74 45 if( EAGAIN != tE
d9d70 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 rrno ){.
d9d80 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d9d90 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d9da0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
d9db0 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 ERR_CHECKRESERVE
d9dc0 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 DLOCK);.
d9dd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d9de0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d9df0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
d9e00 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 /* someone els
d9e10 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 77 e has the lock w
d9e20 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e 4f hen we are in NO
d9e30 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 20 _LOCK */.
d9e40 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46 69 reserved = (pFi
d9e50 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53 le->locktype < S
d9e60 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 HARED_LOCK);.
d9e70 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
d9e80 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c /* we coul
d9e90 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65 20 d have it if we
d9ea0 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 want it */.
d9eb0 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b sem_post(pSem);
d9ec0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 . }. }. OST
d9ed0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c RACE4("TEST WR-L
d9ee0 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 73 65 OCK %d %d %d (se
d9ef0 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c m)\n", pFile->h,
d9f00 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a rc, reserved);.
d9f10 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
d9f20 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
d9f30 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
d9f40 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
d9f50 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
d9f60 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
d9f70 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
d9f80 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
d9f90 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
d9fa0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
d9fb0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
d9fc0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
d9fd0 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
d9fe0 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
d9ff0 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
da000 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
da010 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
da020 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
da030 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
da040 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
da050 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
da060 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
da070 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
da080 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
da090 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
da0a0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
da0b0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
da0c0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
da0d0 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
da0e0 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
da0f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
da100 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
da110 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
da120 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
da130 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
da140 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
da150 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
da160 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
da170 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
da180 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
da190 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
da1a0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
da1b0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
da1c0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
da1d0 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
da1e0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
da1f0 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 Semaphore locks
da200 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 only really supp
da210 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f ort EXCLUSIVE lo
da220 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 cks. We track i
da230 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c ntermediate.** l
da240 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 ock states in th
da250 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 e sqlite3_file s
da260 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c tructure, but al
da270 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f l locks SHARED o
da280 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 r.** above are r
da290 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 eally EXCLUSIVE
da2a0 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 locks and exclud
da2b0 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 e all other proc
da2c0 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 esses from.** ac
da2d0 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a cess the file..*
da2e0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
da2f0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
da300 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
da310 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
da320 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
da330 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
da340 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a cking level..*/.
da350 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f static int semLo
da360 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
da370 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
da380 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
da390 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
da3a0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 le*)id;. int fd
da3b0 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 ;. sem_t *pSem
da3c0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e = pFile->pOpen->
da3d0 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d pSem;. int rc =
da3e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f SQLITE_OK;.. /
da3f0 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 * if we already
da400 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 have a lock, it
da410 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a is exclusive. .
da420 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 ** Just adjust
da430 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 level and punt
da440 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a on outta here. *
da450 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c /. if (pFile->l
da460 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 ocktype > NO_LOC
da470 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
da480 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
da490 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 ype;. rc = SQ
da4a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 LITE_OK;. got
da4b0 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o sem_end_lock;.
da4c0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b }. . /* lock
da4d0 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 semaphore now b
da4e0 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e ut bail out when
da4f0 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e already locked.
da500 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 */. if( sem_tr
da510 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 ywait(pSem)==-1
da520 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
da530 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 TE_BUSY;. got
da540 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o sem_end_lock;.
da550 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 }.. /* got it
da560 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 , set the type a
da570 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a nd return ok */.
da580 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
da590 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 e = locktype;..
da5a0 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 sem_end_lock:.
da5b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
da5c0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
da5d0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
da5e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
da5f0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
da600 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
da610 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
da620 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
da630 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
da640 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
da650 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
da660 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
da670 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
da680 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
da690 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
da6a0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
da6b0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
da6c0 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 c int semUnlock(
da6d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
da6e0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
da6f0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
da700 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
da710 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 )id;. sem_t *pS
da720 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 em = pFile->pOpe
da730 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 n->pSem;.. asse
da740 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 rt( pFile );. a
da750 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 ssert( pSem );.
da760 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 OSTRACE5("UNLOC
da770 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 K %d %d was %d
da780 70 69 64 3d 25 64 20 28 73 65 6d 29 5c 6e 22 2c pid=%d (sem)\n",
da790 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
da7a0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e ype,.. pFile->
da7b0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 locktype, getpid
da7c0 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c ());. assert( l
da7d0 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f ocktype<=SHARED_
da7e0 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 LOCK );. . /*
da7f0 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c no-op if possibl
da800 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 e */. if( pFile
da810 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b ->locktype==lock
da820 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 type ){. retu
da830 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
da840 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 }. . /* shared
da850 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 can just be set
da860 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 because we alwa
da870 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 ys have an exclu
da880 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f sive */. if (lo
da890 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
da8a0 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 OCK) {. pFile
da8b0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
da8c0 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 ktype;. retur
da8d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
da8e0 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 . . /* no, rea
da8f0 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 lly unlock. */.
da900 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 if ( sem_post(p
da910 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 Sem)==-1 ) {.
da920 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 int rc, tErrno
da930 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 = errno;. rc
da940 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
da950 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
da960 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
da970 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 _UNLOCK);. if
da980 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
da990 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
da9a0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
da9b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
da9c0 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 return rc; .
da9d0 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 }. pFile->lockt
da9e0 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
da9f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
daa00 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f K;.}../*. ** Clo
daa10 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 se a file.. */.s
daa20 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f tatic int semClo
daa30 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
daa40 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 *id) {. if( id
daa50 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
daa60 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
daa70 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 le*)id;. semU
daa80 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
daa90 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 K);. assert(
daaa0 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 pFile );. uni
daab0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
daac0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
daad0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 fo(pFile->pLock)
daae0 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 ;. releaseOpe
daaf0 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 nCnt(pFile->pOpe
dab00 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 n);. unixLeav
dab10 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c eMutex();. cl
dab20 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
dab30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
dab40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 LITE_OK;.}..#end
dab50 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 if /* OS_VXWORKS
dab60 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 */./*.** Named
dab70 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e semaphore lockin
dab80 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 g is only availa
dab90 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a ble on VxWorks..
daba0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
dabb0 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 ** End of the na
dabc0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
dabd0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
dabe0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
dabf0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
dac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac40 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
dac50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dac90 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
daca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dacb0 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 Begin AFP Locki
dacc0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
dacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dace0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 ****.**.** AFP i
dacf0 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69 s the Apple Fili
dad00 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 ng Protocol. AF
dad10 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 P is a network f
dad20 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a ilesystem found.
dad30 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 ** on Apple Maci
dad40 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20 ntosh computers
dad50 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f - both OS9 and O
dad60 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d SX..**.** Third-
dad70 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 party implementa
dad80 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65 tions of AFP are
dad90 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 available. But
dada0 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a this code here.
dadb0 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ** only works on
dadc0 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 OSX..*/..#if de
dadd0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
dade0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
dadf0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
dae00 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 /*.** The afpLoc
dae10 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 kingContext stru
dae20 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
dae30 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 ll afp lock spec
dae40 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 ific state.*/.ty
dae50 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70 pedef struct afp
dae60 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 LockingContext a
dae70 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
dae80 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b ;.struct afpLock
dae90 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 ingContext {. u
daea0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
daeb0 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 g sharedByte;.
daec0 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 const char *dbPa
daed0 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 th;
daee0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f /* Name of the o
daef0 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a pen file */.};..
daf00 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 struct ByteRange
daf10 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 LockPB2.{. unsi
daf20 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f gned long long o
daf30 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
daf40 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 offset to first
daf50 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f byte to lock */
daf60 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 . unsigned long
daf70 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 long length;
daf80 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 /* nbr of b
daf90 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a ytes to lock */.
dafa0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
dafb0 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 long retRangeSta
dafc0 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 rt; /* nbr of 1s
dafd0 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 t byte locked if
dafe0 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 successful */.
daff0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 unsigned char u
db000 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 nLockFlag;
db010 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b /* 1 = unlock
db020 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 , 0 = lock */.
db030 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 unsigned char st
db040 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 artEndFlag;
db050 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e /* 1=rel to en
db060 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c d of fork, 0=rel
db070 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 to start */. i
db080 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 nt fd;
db090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db0a0 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f /* file desc to
db0b0 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b assoc this lock
db0c0 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 with */.};..#de
db0d0 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 fine afpfsByteRa
db0e0 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 ngeLock2FSCTL
db0f0 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 _IOWR('z',
db100 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 23, struct ByteR
db110 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a angeLockPB2)../*
db120 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 .** This is a ut
db130 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e ility for settin
db140 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 g or clearing a
db150 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f bit-range lock o
db160 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 n an.** AFP file
db170 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 system..** .** R
db180 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
db190 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 on success, SQLI
db1a0 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 TE_BUSY on failu
db1b0 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e re..*/.static in
db1c0 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 t afpSetLock(.
db1d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
db1e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
db1f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 * Name of the fi
db200 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 le to be locked
db210 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 or unlocked */.
db220 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
db230 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
db240 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 /* Open file des
db250 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 criptor on path
db260 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f */. unsigned lo
db270 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 ng long offset,
db280 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
db290 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
db2a0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e /. unsigned lon
db2b0 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 g long length,
db2c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
db2d0 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f bytes to lock */
db2e0 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c . int setLockFl
db2f0 61 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ag
db300 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 /* True to set
db310 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f lock. False to
db320 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 clear lock */.)
db330 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52 {. struct ByteR
db340 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a angeLockPB2 pb;.
db350 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 int err;. .
db360 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 pb.unLockFlag =
db370 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 setLockFlag ? 0
db380 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 : 1;. pb.startE
db390 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 ndFlag = 0;. pb
db3a0 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 .offset = offset
db3b0 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 ;. pb.length =
db3c0 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 length; . pb.fd
db3d0 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a = pFile->h;. .
db3e0 20 20 4f 53 54 52 41 43 45 36 28 22 41 46 50 53 OSTRACE6("AFPS
db3f0 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 ETLOCK [%s] for
db400 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c %d%s in range %l
db410 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 lx:%llx\n", .
db420 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f (setLockFlag?"O
db430 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 N":"OFF"), pFile
db440 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f ->h, (pb.fd==-1?
db450 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 "[testval-1]":""
db460 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c ),. offset, l
db470 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 ength);. err =
db480 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 fsctl(path, afpf
db490 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 sByteRangeLock2F
db4a0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 SCTL, &pb, 0);.
db4b0 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 if ( err==-1 )
db4c0 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
db4d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
db4e0 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 rrno;. OSTRAC
db4f0 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 E4("AFPSETLOCK f
db500 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29 ailed to fsctl()
db510 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a '%s' %d %s\n",.
db520 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
db530 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 h, tErrno, strer
db540 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 ror(tErrno));.#i
db550 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f fdef SQLITE_IGNO
db560 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f RE_AFP_LOCK_ERRO
db570 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 RS. rc = SQLI
db580 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 TE_BUSY;.#else.
db590 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
db5a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
db5b0 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 r(tErrno,.
db5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 65 se
db5d0 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 tLockFlag ? SQLI
db5e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 TE_IOERR_LOCK :
db5f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
db600 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 OCK);.#endif /*
db610 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 SQLITE_IGNORE_AF
db620 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f P_LOCK_ERRORS */
db630 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
db640 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
db650 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
db660 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
db670 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
db680 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 rc;. } else {.
db690 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
db6a0 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
db6b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
db6c0 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
db6d0 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
db6e0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
db6f0 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
db700 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
db710 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
db720 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
db730 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
db740 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
db750 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
db760 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
db770 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
db780 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
db790 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
db7a0 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
db7b0 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
db7c0 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
db7d0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
db7e0 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73 int afpCheckRes
db7f0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
db800 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
db810 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
db820 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
db830 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
db840 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
db850 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
db860 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d le*)id;. . Sim
db870 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
db880 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
db890 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
db8a0 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 OCK; );. . ass
db8b0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
db8c0 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
db8d0 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 t *context = (af
db8e0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
db8f0 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *) pFile->lockin
db900 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f gContext;. . /
db910 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 * Check if a thr
db920 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 ead in this proc
db930 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 ess holds such a
db940 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 lock */. if( p
db950 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 File->locktype>S
db960 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
db970 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
db980 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 }. . /* Othe
db990 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d rwise see if som
db9a0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 e other process
db9b0 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a holds it.. */.
db9c0 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20 if( !reserved
db9d0 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 ){. /* lock t
db9e0 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74 65 he RESERVED byte
db9f0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 */. int lrc
dba00 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
dba10 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
dba20 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ile, RESERVED_BY
dba30 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 TE, 1,1); .
dba40 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c if( SQLITE_OK==l
dba50 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 rc ){. /* i
dba60 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 f we succeeded i
dba70 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 n taking the res
dba80 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f erved lock, unlo
dba90 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 ck it to restore
dbaa0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 . ** the or
dbab0 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a iginal state */.
dbac0 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53 lrc = afpS
dbad0 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
dbae0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
dbaf0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
dbb00 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 0);. } else
dbb10 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 {. /* if we
dbb20 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74 failed to get t
dbb30 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d he lock then som
dbb40 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68 eone else must h
dbb50 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 ave it */.
dbb60 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
dbb70 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c }. if( IS_L
dbb80 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 OCK_ERROR(lrc) )
dbb90 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a {. rc=lrc;.
dbba0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f }. }. . O
dbbb0 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 STRACE4("TEST WR
dbbc0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 -LOCK %d %d %d (
dbbd0 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e afp)\n", pFile->
dbbe0 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
dbbf0 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 ;. . *pResOut
dbc00 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
dbc10 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
dbc20 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
dbc30 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
dbc40 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
dbc50 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 eter locktype -
dbc60 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f one.** of the fo
dbc70 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
dbc80 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f (1) SHARED_LO
dbc90 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 CK.** (2) RE
dbca0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SERVED_LOCK.**
dbcb0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c (3) PENDING_L
dbcc0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 OCK.** (4) E
dbcd0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a XCLUSIVE_LOCK.**
dbce0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 .** Sometimes wh
dbcf0 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e en requesting on
dbd00 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 e lock state, ad
dbd10 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 ditional lock st
dbd20 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 ates.** are inse
dbd30 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e rted in between.
dbd40 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 The locking mi
dbd50 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 ght fail on one
dbd60 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 of the later.**
dbd70 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 transitions leav
dbd80 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 ing the lock sta
dbd90 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
dbda0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 m what it starte
dbdb0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 d but.** still s
dbdc0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c hort of its goal
dbdd0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
dbde0 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 chart shows the
dbdf0 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e allowed.** tran
dbe00 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 sitions and the
dbe10 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 inserted interme
dbe20 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a diate states:.**
dbe30 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 .** UNLOCKED
dbe40 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 -> SHARED.**
dbe50 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 SHARED -> RESERV
dbe60 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
dbe70 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
dbe80 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
dbe90 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e RESERVED -> (PEN
dbea0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
dbeb0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 VE.** PENDING
dbec0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
dbed0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
dbee0 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
dbef0 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 ase a lock. Use
dbf00 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e the sqlite3OsUn
dbf10 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e lock().** routin
dbf20 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 e to lower a loc
dbf30 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 king level..*/.s
dbf40 74 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 tatic int afpLoc
dbf50 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
dbf60 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
dbf70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
dbf80 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 LITE_OK;. unixF
dbf90 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
dbfa0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 ixFile*)id;. af
dbfb0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
dbfc0 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c *context = (afpL
dbfd0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
dbfe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 pFile->lockingC
dbff0 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 ontext;. . ass
dc000 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
dc010 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 OSTRACE5("LOCK
dc020 20 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70 %d %s was %s p
dc030 69 64 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20 id=%d (afp)\n",
dc040 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
dc050 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
dc060 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 locktype), lockt
dc070 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c ypeName(pFile->l
dc080 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 ocktype), getpid
dc090 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ());.. /* If th
dc0a0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
dc0b0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 lock of this ty
dc0c0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 pe or more restr
dc0d0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 ictive on the.
dc0e0 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 ** unixFile, do
dc0f0 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 nothing. Don't u
dc100 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c se the afp_end_l
dc110 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 ock: exit path,
dc120 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 as. ** unixEnte
dc130 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 rMutex() hasn't
dc140 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e been called yet.
dc150 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
dc160 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 e->locktype>=loc
dc170 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 ktype ){. OST
dc180 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 RACE3("LOCK %
dc190 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 d %s ok (already
dc1a0 20 68 65 6c 64 29 20 28 61 66 70 29 5c 6e 22 2c held) (afp)\n",
dc1b0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
dc1c0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
dc1d0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 me(locktype));.
dc1e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
dc1f0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
dc200 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
dc210 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
dc220 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
dc230 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
dc240 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
dc250 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
dc260 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
dc270 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
dc280 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
dc290 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
dc2a0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
dc2b0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
dc2c0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
dc2d0 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 CK );. . /* Th
dc2e0 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 is mutex is need
dc2f0 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 ed because pFile
dc300 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 ->pLock is share
dc310 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 d across threads
dc320 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 . */. unixEnte
dc330 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 rMutex();.. /*
dc340 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 Make sure the cu
dc350 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e rrent thread own
dc360 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a s the pFile.. *
dc370 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 /. rc = transfe
dc380 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 rOwnership(pFile
dc390 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
dc3a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e ITE_OK ){. un
dc3b0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
dc3c0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
dc3d0 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 }. . /* A P
dc3e0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e ENDING lock is n
dc3f0 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 eeded before acq
dc400 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 uiring a SHARED
dc410 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a lock and before.
dc420 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 ** acquiring a
dc430 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
dc440 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 . For the SHARE
dc450 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 D lock, the PEND
dc460 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 ING will. ** be
dc470 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a released.. */.
dc480 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
dc490 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 SHARED_LOCK .
dc4a0 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d || (locktype=
dc4b0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
dc4c0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
dc4d0 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 pe<PENDING_LOCK)
dc4e0 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 . ){. int fa
dc4f0 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 iled;. failed
dc500 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f = afpSetLock(co
dc510 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 ntext->dbPath, p
dc520 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 File, PENDING_BY
dc530 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 TE, 1, 1);. i
dc540 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 f (failed) {.
dc550 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a rc = failed;.
dc560 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 goto afp_e
dc570 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 nd_lock;. }.
dc580 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f }. . /* If co
dc590 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 ntrol gets to th
dc5a0 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 is point, then a
dc5b0 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 ctually go ahead
dc5c0 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f and make. ** o
dc5d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
dc5e0 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 calls for the sp
dc5f0 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 ecified lock..
dc600 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
dc610 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
dc620 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 {. int lk, lr
dc630 63 31 2c 20 6c 72 63 32 3b 0a 20 20 20 20 69 6e c1, lrc2;. in
dc640 74 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 30 3b t lrc1Errno = 0;
dc650 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 . . /* Now
dc660 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f get the read-lo
dc670 63 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a ck SHARED_LOCK *
dc680 2f 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 /. /* note th
dc690 61 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f at the quality o
dc6a0 66 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 f the randomness
dc6b0 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 doesn't matter
dc6c0 74 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 that much */.
dc6d0 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 lk = random();
dc6e0 0a 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 . context->sh
dc6f0 61 72 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 aredByte = (lk &
dc700 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48 0x7fffffff)%(SH
dc710 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a ARED_SIZE - 1);.
dc720 20 20 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65 lrc1 = afpSe
dc730 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
dc740 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 bPath, pFile, .
dc750 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f SHARED_
dc760 46 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 FIRST+context->s
dc770 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 haredByte, 1, 1)
dc780 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 ;. if( IS_LOC
dc790 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b K_ERROR(lrc1) ){
dc7a0 0a 20 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f . lrc1Errno
dc7b0 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 = pFile->lastEr
dc7c0 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f rno;. }. /
dc7d0 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f * Drop the tempo
dc7e0 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 rary PENDING loc
dc7f0 6b 20 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20 k */. lrc2 =
dc800 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
dc810 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
dc820 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c e, PENDING_BYTE,
dc830 20 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20 1, 0);. .
dc840 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
dc850 4f 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20 OR(lrc1) ) {.
dc860 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
dc870 72 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b rno = lrc1Errno;
dc880 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 . rc = lrc1
dc890 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 ;. goto afp
dc8a0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d _end_lock;. }
dc8b0 20 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 else if( IS_LOC
dc8c0 4b 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b K_ERROR(lrc2) ){
dc8d0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32 . rc = lrc2
dc8e0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 ;. goto afp
dc8f0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d _end_lock;. }
dc900 20 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21 else if( lrc1 !
dc910 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a = SQLITE_OK ) {.
dc920 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b rc = lrc1;
dc930 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
dc940 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
dc950 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
dc960 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e K;. pFile->
dc970 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a pOpen->nLock++;.
dc980 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
dc990 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 /* The reques
dc9a0 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 t was for a RESE
dc9b0 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 RVED or EXCLUSIV
dc9c0 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 E lock. It is.
dc9d0 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 ** assumed th
dc9e0 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 at there is a SH
dc9f0 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 ARED or greater
dca00 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 lock on the file
dca10 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e . ** already.
dca20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
dca30 66 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 failed = 0;.
dca40 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 assert( 0!=pFile
dca50 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
dca60 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e if (locktype >
dca70 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 = RESERVED_LOCK
dca80 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
dca90 70 65 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f pe < RESERVED_LO
dcaa0 43 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a CK) {. /*
dcab0 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 Acquire a RESER
dcac0 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 VED lock */.
dcad0 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 failed = afp
dcae0 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
dcaf0 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
dcb00 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 RESERVED_BYTE, 1
dcb10 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ,1);. }. i
dcb20 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f f (!failed && lo
dcb30 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53 cktype == EXCLUS
dcb40 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 IVE_LOCK) {.
dcb50 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 /* Acquire an
dcb60 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a EXCLUSIVE lock *
dcb70 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 /. .
dcb80 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 /* Remove the s
dcb90 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 hared lock befor
dcba0 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e e trying the ran
dcbb0 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 ge. we'll need
dcbc0 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 to . ** ree
dcbd0 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 stablish the sha
dcbe0 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 red lock if we c
dcbf0 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 an't get the af
dcc00 70 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f pUnlock. */
dcc10 0a 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69 . if( !(fai
dcc20 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b led = afpSetLock
dcc30 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
dcc40 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f , pFile, SHARED_
dcc50 46 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 FIRST +.
dcc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcc70 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 context->shared
dcc80 42 79 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a Byte, 1, 0)) ){.
dcc90 20 20 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c int fail
dcca0 65 64 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b ed2 = SQLITE_OK;
dccb0 0a 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 . /* now
dccc0 61 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 attemmpt to get
dccd0 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f the exclusive lo
dcce0 63 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 ck range */.
dccf0 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 failed = afp
dcd00 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
dcd10 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
dcd20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 SHARED_FIRST, .
dcd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48 SH
dcd50 41 52 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 ARED_SIZE, 1);.
dcd60 20 20 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 if( faile
dcd70 64 20 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 d && (failed2 =
dcd80 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
dcd90 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
dcda0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e, .
dcdb0 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 SHARE
dcdc0 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78 D_FIRST + contex
dcdd0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 t->sharedByte, 1
dcde0 2c 20 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20 , 1)) ){.
dcdf0 20 20 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 /* Can't rees
dce00 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 tablish the shar
dce10 65 64 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 ed lock. Sqlite
dce20 20 63 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 can't deal, thi
dce30 73 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a s is. *
dce40 2a 20 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f * a critical I/O
dce50 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20 error.
dce60 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 */. rc
dce70 20 3d 20 28 28 66 61 69 6c 65 64 20 26 20 53 51 = ((failed & SQ
dce80 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 LITE_IOERR) == S
dce90 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 QLITE_IOERR) ? f
dcea0 61 69 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20 ailed2 : .
dceb0 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
dcec0 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 IOERR_LOCK;.
dced0 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 goto afp_e
dcee0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 nd_lock;.
dcef0 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b } . }else{
dcf00 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 . rc = fa
dcf10 69 6c 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 iled; . }.
dcf20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69 }. if( fai
dcf30 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 led ){. rc
dcf40 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a = failed;. }.
dcf50 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d }. . if( rc=
dcf60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
dcf70 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
dcf80 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
dcf90 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 }else if( lockty
dcfa0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
dcfb0 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
dcfc0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 >locktype = PEND
dcfd0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 ING_LOCK;. }.
dcfe0 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 .afp_end_lock:.
dcff0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
dd000 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c );. OSTRACE4("L
dd010 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 20 OCK %d %s %s
dd020 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d (afp)\n", pFile-
dd030 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 >h, locktypeName
dd040 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 (locktype), .
dd050 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
dd060 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 _OK ? "ok" : "fa
dd070 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e iled");. return
dd080 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
dd090 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
dd0a0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
dd0b0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
dd0c0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 o locktype. loc
dd0d0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 ktype.** must be
dd0e0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
dd0f0 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
dd100 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 **.** If the loc
dd110 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 king level of th
dd120 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
dd130 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 r is already at
dd140 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 or below.** the
dd150 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e requested lockin
dd160 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f g level, this ro
dd170 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
dd180 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
dd190 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 afpUnlock(sqlite
dd1a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
dd1b0 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e locktype) {. in
dd1c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
dd1d0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
dd1e0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
dd1f0 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e )id;. afpLockin
dd200 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
dd210 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 (afpLockingCont
dd220 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f ext *) pFile->lo
dd230 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 ckingContext;..
dd240 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
dd250 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
dd260 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
dd270 25 64 20 70 69 64 3d 25 64 20 28 61 66 70 29 5c %d pid=%d (afp)\
dd280 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
dd290 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
dd2a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dd2b0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
dd2c0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
dd2d0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
dd2e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
dd2f0 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 cktype<=locktype
dd300 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
dd310 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
dd320 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 if( CHECK_THREAD
dd330 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ID(pFile) ){.
dd340 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
dd350 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 ISUSE;. }. uni
dd360 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
dd370 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
dd380 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
dd390 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 ){. . if(
dd3a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dd3b0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
dd3c0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
dd3d0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dd3e0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 dbPath, pFile, S
dd3f0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 HARED_FIRST, SHA
dd400 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
dd410 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
dd420 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 TE_OK && locktyp
dd430 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
dd440 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c {. /* onl
dd450 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 y re-establish t
dd460 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 he shared lock i
dd470 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 f necessary */.
dd480 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 int share
dd490 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 dLockByte = SHAR
dd4a0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 ED_FIRST+pCtx->s
dd4b0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 haredByte;.
dd4c0 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f rc = afpSetLo
dd4d0 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c ck(pCtx->dbPath,
dd4e0 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f pFile, sharedLo
dd4f0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 ckByte, 1, 1);.
dd500 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
dd510 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
dd520 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 OK && pFile->loc
dd530 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c ktype>=PENDING_L
dd540 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OCK ){. rc
dd550 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
dd560 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
dd570 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
dd580 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 1, 0);. } .
dd590 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
dd5a0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f _OK && pFile->lo
dd5b0 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 cktype>=RESERVED
dd5c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 _LOCK ){. r
dd5d0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 c = afpSetLock(p
dd5e0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 Ctx->dbPath, pFi
dd5f0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
dd600 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 1, 0);. }.
dd610 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
dd620 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
dd630 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 . /* clear th
dd640 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f e shared lock */
dd650 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c . int sharedL
dd660 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 ockByte = SHARED
dd670 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 _FIRST+pCtx->sha
dd680 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 redByte;. rc
dd690 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
dd6a0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
dd6b0 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 , sharedLockByte
dd6c0 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 , 1, 0);. }..
dd6d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dd6e0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 K ){. if( loc
dd6f0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype==NO_LOCK )
dd700 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 {. struct u
dd710 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
dd720 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e n = pFile->pOpen
dd730 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ;. pOpen->n
dd740 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 Lock--;. as
dd750 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f sert( pOpen->nLo
dd760 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 ck>=0 );. i
dd770 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d f( pOpen->nLock=
dd780 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
dd790 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 = closePendingF
dd7a0 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 ds(pFile);.
dd7b0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 }. }. }. u
dd7c0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
dd7d0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
dd7e0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c E_OK ){. pFil
dd7f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
dd800 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 cktype;. }. re
dd810 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
dd820 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 * Close a file &
dd830 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 cleanup AFP spe
dd840 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f cific locking co
dd850 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 ntext .*/.static
dd860 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 int afpClose(sq
dd870 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
dd880 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
dd890 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
dd8a0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
dd8b0 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b d;. afpUnlock
dd8c0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
dd8d0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
dd8e0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 x();. if( pFi
dd8f0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 le->pOpen && pFi
dd900 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
dd910 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
dd920 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 there are outsta
dd930 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 nding locks, do
dd940 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f not actually clo
dd950 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 se the file just
dd960 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 . ** yet be
dd970 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 cause that would
dd980 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 clear those loc
dd990 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 ks. Instead, ad
dd9a0 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 d the file.
dd9b0 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 ** descriptor t
dd9c0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e o pOpen->aPendin
dd9d0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 g. It will be a
dd9e0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f utomatically clo
dd9f0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a sed when. *
dda00 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 * the last lock
dda10 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 is cleared..
dda20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 */. setPe
dda30 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a ndingFd(pFile);.
dda40 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 }. releas
dda50 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e eOpenCnt(pFile->
dda60 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 pOpen);. sqli
dda70 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
dda80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
dda90 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 . closeUnixFi
ddaa0 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 le(id);. unix
ddab0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
ddac0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
ddad0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
ddae0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 /* defined(__APP
ddaf0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
ddb00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
ddb10 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 TYLE */./*.** Th
ddb20 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 e code above is
ddb30 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 the AFP lock imp
ddb40 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
ddb50 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 e code is specif
ddb60 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 ic.** to MacOSX
ddb70 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 and does not wor
ddb80 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 k on other unix
ddb90 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 platforms. No a
ddba0 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 lternative.** is
ddbb0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 available. If
ddbc0 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c you don't compil
ddbd0 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 e for a mac, the
ddbe0 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 n the "unix-afp"
ddbf0 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 .** VFS is not a
ddc00 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a vailable..**.***
ddc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddc20 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 ** End of the AF
ddc30 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 P lock implement
ddc40 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
ddc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
ddc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f ***********/.../
ddcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
ddd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
ddd10 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 Non-locking sqli
ddd20 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
ddd30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
ddd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
ddd50 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69 *.** The next di
ddd60 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 vision contains
ddd70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
ddd80 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 for all methods
ddd90 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 of the .** sqlit
ddda0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f e3_file object o
dddb0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f ther than the lo
dddc0 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 cking methods.
dddd0 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d The locking.** m
ddde0 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69 ethods were defi
dddf0 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 ned in divisions
dde00 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b above (one lock
dde10 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a ing method per.*
dde20 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 * division). Th
dde30 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 ose methods that
dde40 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 are common to a
dde50 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 ll locking modes
dde60 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74 .** are gather t
dde70 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69 ogether into thi
dde80 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a s division..*/..
dde90 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 /*.** Seek to th
ddea0 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 e offset passed
ddeb0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
ddec0 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 gument, then rea
dded0 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 d cnt .** bytes
ddee0 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72 into pBuf. Retur
ddef0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
ddf00 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 bytes actually r
ddf10 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 ead..**.** NB:
ddf20 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53 If you define US
ddf30 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50 E_PREAD or USE_P
ddf40 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20 READ64, then it
ddf50 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 might also.** be
ddf60 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65 necessary to de
ddf70 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 fine _XOPEN_SOUR
ddf80 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54 CE to be 500. T
ddf90 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a his varies from.
ddfa0 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f ** one system to
ddfb0 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 another. Since
ddfc0 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 SQLite does not
ddfd0 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 define USE_PREA
ddfe0 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 D.** any any for
ddff0 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65 m by default, we
de000 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 will not attemp
de010 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 t to define _XOP
de020 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 EN_SOURCE..** Se
de030 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20 e tickets #2741
de040 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a and #2681..**.**
de050 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 To avoid stompi
de060 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c ng the errno val
de070 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 ue on a failed r
de080 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e ead the lastErrn
de090 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 o value.** is se
de0a0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
de0b0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
de0c0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e t seekAndRead(un
de0d0 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 ixFile *id, sqli
de0e0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
de0f0 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e , void *pBuf, in
de100 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f t cnt){. int go
de110 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 t;. i64 newOffs
de120 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 et;. TIMER_STAR
de130 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
de140 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 SE_PREAD). got
de150 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 = pread(id->h, p
de160 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
de170 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
de180 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
de190 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
de1a0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 USE_PREAD64). g
de1b0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d ot = pread64(id-
de1c0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
de1d0 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 ffset);. Simula
de1e0 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d teIOError( got =
de1f0 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e -1 );.#else. n
de200 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b ewOffset = lseek
de210 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 (id->h, offset,
de220 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d SEEK_SET);. Sim
de230 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 ulateIOError( ne
de240 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 wOffset-- );. i
de250 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 f( newOffset!=of
de260 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 fset ){. if(
de270 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 newOffset == -1
de280 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 ){. ((unixF
de290 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
de2a0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
de2b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 }else{. ((
de2c0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
de2d0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 astErrno = 0;...
de2e0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
de2f0 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 n -1;. }. got
de300 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 = read(id->h, pB
de310 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 uf, cnt);.#endif
de320 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 . TIMER_END;.
de330 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
de340 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
de350 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
de360 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 rno;. }. OSTRA
de370 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33 CE5("READ %-3
de380 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 d %5d %7lld %llu
de390 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c \n", id->h, got,
de3a0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 offset, TIMER_E
de3b0 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 LAPSED);. retur
de3c0 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n got;.}../*.**
de3d0 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 Read data from a
de3e0 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 file into a buf
de3f0 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c fer. Return SQL
de400 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a ITE_OK if all.**
de410 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 bytes were read
de420 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e successfully an
de430 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 d SQLITE_IOERR i
de440 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
de450 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 ** wrong..*/.sta
de460 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 tic int unixRead
de470 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
de480 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 *id, . void *p
de490 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c Buf, . int amt,
de4a0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
de4b0 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 offset.){. uni
de4c0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
de4d0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 unixFile *)id;.
de4e0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 int got;. asse
de4f0 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 rt( id );.. /*
de500 49 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74 If this is a dat
de510 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 abase file (not
de520 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 a journal, maste
de530 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d r-journal or tem
de540 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 p. ** file), th
de550 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c e bytes in the l
de560 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f ocking range sho
de570 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61 uld never be rea
de580 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f d or written. */
de590 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
de5a0 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 ->pUnused==0.
de5b0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 || offset>=P
de5c0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a ENDING_BYTE+512.
de5d0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
de5e0 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 +amt<=PENDING_BY
de5f0 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 TE . );.. got
de600 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 = seekAndRead(pF
de610 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 ile, offset, pBu
de620 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 f, amt);. if( g
de630 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 ot==amt ){. r
de640 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
de650 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 . }else if( got
de660 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 <0 ){. /* las
de670 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 tErrno set by se
de680 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 ekAndRead */.
de690 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
de6a0 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c OERR_READ;. }el
de6b0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c se{. pFile->l
de6c0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a astErrno = 0; /*
de6d0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 not a system er
de6e0 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e ror */. /* Un
de6f0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 read parts of th
de700 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 e buffer must be
de710 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a zero-filled */.
de720 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 memset(&((ch
de730 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 ar*)pBuf)[got],
de740 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 0, amt-got);.
de750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
de760 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b OERR_SHORT_READ;
de770 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
de780 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 ek to the offset
de790 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 in id->offset t
de7a0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 hen read cnt byt
de7b0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a es into pBuf..**
de7c0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
de7d0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
de7e0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 ally read. Upda
de7f0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a te the offset..*
de800 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 *.** To avoid st
de810 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f omping the errno
de820 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c value on a fail
de830 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 ed write the las
de840 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 tErrno value.**
de850 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 is set before re
de860 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 turning..*/.stat
de870 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 ic int seekAndWr
de880 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 ite(unixFile *id
de890 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f , i64 offset, co
de8a0 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 nst void *pBuf,
de8b0 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 int cnt){. int
de8c0 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 got;. i64 newOf
de8d0 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 fset;. TIMER_ST
de8e0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 ART;.#if defined
de8f0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f (USE_PREAD). go
de900 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 t = pwrite(id->h
de910 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 , pBuf, cnt, off
de920 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 set);.#elif defi
de930 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 ned(USE_PREAD64)
de940 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 . got = pwrite6
de950 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 4(id->h, pBuf, c
de960 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c nt, offset);.#el
de970 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d se. newOffset =
de980 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 lseek(id->h, of
de990 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b fset, SEEK_SET);
de9a0 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 . if( newOffset
de9b0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 !=offset ){.
de9c0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d if( newOffset ==
de9d0 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 -1 ){. ((u
de9e0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
de9f0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
dea00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
dea10 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
dea20 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 )->lastErrno = 0
dea30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 ;.... }. r
dea40 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 eturn -1;. }.
dea50 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e got = write(id->
dea60 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 h, pBuf, cnt);.#
dea70 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e endif. TIMER_EN
dea80 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 D;. if( got<0 )
dea90 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 {. ((unixFile
deaa0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
deab0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 = errno;. }..
deac0 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45 OSTRACE5("WRITE
dead0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c %-3d %5d %7ll
deae0 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 d %llu\n", id->h
deaf0 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 , got, offset, T
deb00 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 IMER_ELAPSED);.
deb10 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a return got;.}..
deb20 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
deb30 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 a from a buffer
deb40 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 into a file. Re
deb50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
deb60 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 n success.** or
deb70 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 some other error
deb80 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 code on failure
deb90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
deba0 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c unixWrite(. sql
debb0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a ite3_file *id, .
debc0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 const void *pB
debd0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a uf, . int amt,.
debe0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
debf0 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 offset .){. uni
dec00 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
dec10 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
dec20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 int wrote = 0;.
dec30 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 assert( id );.
dec40 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 assert( amt>0 )
dec50 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
dec60 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 is a database fi
dec70 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 le (not a journa
dec80 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 l, master-journa
dec90 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 l or temp. ** f
deca0 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20 ile), the bytes
decb0 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 in the locking r
decc0 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 ange should neve
decd0 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 r be read or wri
dece0 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 tten. */. asser
decf0 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 t( pFile->pUnuse
ded00 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f d==0. || o
ded10 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 ffset>=PENDING_B
ded20 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c YTE+512. |
ded30 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 | offset+amt<=PE
ded40 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b NDING_BYTE . );
ded50 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
ded60 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
ded70 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 doing a normal w
ded80 72 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 rite to a databa
ded90 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f se file (as oppo
deda0 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e sed to. ** doin
dedb0 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 g a hot-journal
dedc0 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 rollback or a wr
dedd0 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 ite to some file
dede0 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 other than a.
dedf0 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 ** normal databa
dee00 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 se file) then re
dee10 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 cord the fact th
dee20 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a at the database.
dee30 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 ** has changed
dee40 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 . If the transa
dee50 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 ction counter is
dee60 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 modified, recor
dee70 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 d that. ** fact
dee80 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 too.. */. if(
dee90 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c pFile->inNormal
deea0 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69 Write ){. pFi
deeb0 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31 le->dbUpdate = 1
deec0 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 ; /* The databa
deed0 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 se has been modi
deee0 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 fied */. if(
deef0 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 offset<=24 && of
def00 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a fset+amt>=27 ){.
def10 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 int rc;.
def20 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 char oldCntr
def30 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c [4];. Simul
def40 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
def50 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 (1);. rc =
def60 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c seekAndRead(pFil
def70 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 e, 24, oldCntr,
def80 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 4);. Simula
def90 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
defa0 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
defb0 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c !=4 || memcmp(ol
defc0 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 dCntr, &((char*)
defd0 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d pBuf)[24-offset]
defe0 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 , 4)!=0 ){.
deff0 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 pFile->transC
df000 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a ntrChng = 1; /*
df010 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e The transaction
df020 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 counter has cha
df030 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a nged */. }.
df040 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
df050 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 .. while( amt>0
df060 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 && (wrote = see
df070 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c kAndWrite(pFile,
df080 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 offset, pBuf, a
df090 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d mt))>0 ){. am
df0a0 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t -= wrote;.
df0b0 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b offset += wrote;
df0c0 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 . pBuf = &((c
df0d0 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 har*)pBuf)[wrote
df0e0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 ];. }. Simulat
df0f0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 eIOError(( wrote
df100 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b =(-1), amt=1 ));
df110 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
df120 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65 ullError(( wrote
df130 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 =0, amt=1 ));.
df140 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 if( amt>0 ){.
df150 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a if( wrote<0 ){.
df160 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 /* lastErr
df170 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e no set by seekAn
df180 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 dWrite */.
df190 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
df1a0 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d ERR_WRITE;. }
df1b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c else{. pFil
df1c0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 e->lastErrno = 0
df1d0 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 ; /* not a syste
df1e0 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 m error */.
df1f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
df200 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ULL;. }. }.
df210 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
df220 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c K;.}..#ifdef SQL
df230 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 ITE_TEST./*.** C
df240 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 ount the number
df250 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 of fullsyncs and
df260 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 normal syncs.
df270 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
df280 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e test.** that syn
df290 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 cs and fullsyncs
df2a0 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 are occurring a
df2b0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 t the right time
df2c0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
df2d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e int sqlite3_syn
df2e0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c c_count = 0;.SQL
df2f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
df300 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
df310 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
df320 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 /*.** We do not
df330 74 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f trust systems to
df340 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 provide a worki
df350 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 ng fdatasync().
df360 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 Some do..** Oth
df370 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 ers do no. To b
df380 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 e safe, we will
df390 73 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 stick with the (
df3a0 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e slower) fsync().
df3b0 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 .** If you know
df3c0 74 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d that your system
df3d0 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 does support fd
df3e0 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 atasync() correc
df3f0 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d tly,.** then sim
df400 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 ply compile with
df410 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 -Dfdatasync=fda
df420 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 tasync.*/.#if !d
df430 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 efined(fdatasync
df440 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f ) && !defined(__
df450 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e linux__).# defin
df460 65 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e e fdatasync fsyn
df470 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 c.#endif../*.**
df480 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c Define HAVE_FULL
df490 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 FSYNC to 0 or 1
df4a0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 depending on whe
df4b0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 ther or not.** t
df4c0 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d he F_FULLFSYNC m
df4d0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e acro is defined.
df4e0 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 F_FULLFSYNC is
df4f0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e currently.** on
df500 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ly available on
df510 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 Mac OS X. But t
df520 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 hat could change
df530 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 ..*/.#ifdef F_FU
df540 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 LLFSYNC.# define
df550 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 HAVE_FULLFSYNC
df560 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 1.#else.# define
df570 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 HAVE_FULLFSYNC
df580 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
df590 20 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 The fsync() sys
df5a0 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f tem call does no
df5b0 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 t work as advert
df5c0 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 ised on many.**
df5d0 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 unix systems. T
df5e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f he following pro
df5f0 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 cedure is an att
df600 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 empt to make.**
df610 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a it work better..
df620 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
df630 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 _NO_SYNC macro d
df640 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e isables all fsyn
df650 63 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 c()s. This is u
df660 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 seful.** for tes
df670 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e ting when we wan
df680 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 t to run through
df690 20 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 the test suite
df6a0 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 quickly..** You
df6b0 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 are strongly adv
df6c0 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 ised *not* to de
df6d0 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 ploy with SQLITE
df6e0 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 _NO_SYNC.** enab
df6f0 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 led, however, si
df700 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f nce with SQLITE_
df710 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c NO_SYNC enabled,
df720 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 an OS crash.**
df730 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 or power failure
df740 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 will likely cor
df750 72 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 rupt the databas
df760 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 e file..**.** SQ
df770 4c 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 Lite sets the da
df780 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 taOnly flag if t
df790 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
df7a0 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ile is unchanged
df7b0 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 ..** The idea be
df7c0 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 hind dataOnly is
df7d0 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 that it should
df7e0 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 only write the f
df7f0 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 ile content.** t
df800 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 o disk, not the
df810 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 inode. We only
df820 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 set dataOnly if
df830 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 the file size is
df840 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 .** unchanged s
df850 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 ince the file si
df860 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 ze is part of th
df870 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 e inode. Howeve
df880 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 r, .** Ted Ts'o
df890 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 tells us that fd
df8a0 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 atasync() will a
df8b0 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e lso write the in
df8c0 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 ode if the.** fi
df8d0 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e le size has chan
df8e0 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 ged. The only r
df8f0 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 eal difference b
df900 65 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 etween fdatasync
df910 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 ().** and fsync(
df920 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c ), Ted tells us,
df930 20 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 is that fdatasy
df940 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c nc() will not fl
df950 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 ush the.** inode
df960 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 if the mtime or
df970 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 owner or other
df980 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 inode attributes
df990 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a have changed..*
df9a0 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 * We only care a
df9b0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 bout the file si
df9c0 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 ze, not the othe
df9d0 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 r file attribute
df9e0 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 s, so.** as far
df9f0 61 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e as SQLite is con
dfa00 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 cerned, an fdata
dfa10 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 sync() is always
dfa20 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f adequate..** So
dfa30 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 , we always use
dfa40 66 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 fdatasync() if i
dfa50 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 t is available,
dfa60 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a regardless of.**
dfa70 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
dfa80 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e e dataOnly flag.
dfa90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
dfaa0 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 ull_fsync(int fd
dfab0 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 , int fullSync,
dfac0 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 int dataOnly){.
dfad0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 int rc;.. /* T
dfae0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 he following "if
dfaf0 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 def/elif/else/"
dfb00 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 block has the sa
dfb10 6d 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a me structure as.
dfb20 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c ** the one bel
dfb30 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 ow. It is replic
dfb40 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 ated here solely
dfb50 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 to avoid clutte
dfb60 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 ring . ** up th
dfb70 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 e real code with
dfb80 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 the UNUSED_PARA
dfb90 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a METER() macros..
dfba0 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
dfbb0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 TE_NO_SYNC. UNU
dfbc0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 SED_PARAMETER(fd
dfbd0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
dfbe0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
dfbf0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
dfc00 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
dfc10 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 elif HAVE_FULLFS
dfc20 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 YNC. UNUSED_PAR
dfc30 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 AMETER(dataOnly)
dfc40 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 ;.#else. UNUSED
dfc50 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 _PARAMETER(fullS
dfc60 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ync);. UNUSED_P
dfc70 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c ARAMETER(dataOnl
dfc80 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a y);.#endif.. /*
dfc90 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 Record the numb
dfca0 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
dfcb0 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 we do a normal
dfcc0 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a fsync() and . *
dfcd0 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 * FULLSYNC. Thi
dfce0 73 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 s is used during
dfcf0 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 testing to veri
dfd00 66 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f fy that this pro
dfd10 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 cedure. ** gets
dfd20 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 called with the
dfd30 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e correct argumen
dfd40 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 ts.. */.#ifdef
dfd50 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 SQLITE_TEST. if
dfd60 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c ( fullSync ) sql
dfd70 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
dfd80 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 unt++;. sqlite3
dfd90 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 _sync_count++;.#
dfda0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 endif.. /* If w
dfdb0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 e compiled with
dfdc0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 the SQLITE_NO_SY
dfdd0 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 NC flag, then sy
dfde0 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 ncing is a. **
dfdf0 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 no-op. */.#ifde
dfe00 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 f SQLITE_NO_SYNC
dfe10 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f . rc = SQLITE_O
dfe20 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 K;.#elif HAVE_FU
dfe30 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 LLFSYNC. if( fu
dfe40 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 llSync ){. rc
dfe50 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 = fcntl(fd, F_F
dfe60 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 ULLFSYNC, 0);.
dfe70 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
dfe80 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 1;. }. /* If t
dfe90 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 he FULLFSYNC fai
dfea0 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 led, fall back t
dfeb0 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 o attempting an
dfec0 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 fsync().. ** It
dfed0 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f shouldn't be po
dfee0 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 ssible for fullf
dfef0 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 sync to fail on
dff00 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 the local . **
dff10 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 file system (on
dff20 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 OSX), so failure
dff30 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
dff40 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 FULLFSYNC. ** i
dff50 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 sn't supported f
dff60 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 or this file sys
dff70 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 tem. So, attempt
dff80 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 an fsync . **
dff90 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 and (for now) ig
dffa0 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 nore the overhea
dffb0 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f d of a superfluo
dffc0 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 us fcntl call.
dffd0 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 . ** It'd be be
dffe0 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 tter to detect f
dfff0 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 ullfsync support
e0000 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 once and avoid
e0010 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 . ** the fcntl
e0020 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 call every time
e0030 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a sync is called..
e0040 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 */. if( rc )
e0050 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a rc = fsync(fd);.
e0060 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 .#else . rc = f
e0070 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 datasync(fd);.#i
e0080 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 f OS_VXWORKS. i
e0090 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 f( rc==-1 && err
e00a0 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 no==ENOTSUP ){.
e00b0 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 rc = fsync(fd
e00c0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a );. }.#endif /*
e00d0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 OS_VXWORKS */.#
e00e0 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 endif /* ifdef S
e00f0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c QLITE_NO_SYNC el
e0100 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e if HAVE_FULLFSYN
e0110 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 C */.. if( OS_V
e0120 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d XWORKS && rc!= -
e0130 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 1 ){. rc = 0;
e0140 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
e0150 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
e0160 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
e0170 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
e0180 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
e0190 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a ed to disk..**.*
e01a0 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 * If dataOnly==0
e01b0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 then both the f
e01c0 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 ile itself and i
e01d0 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c ts metadata (fil
e01e0 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 e.** size, acces
e01f0 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 s time, etc) are
e0200 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 synced. If dat
e0210 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e aOnly!=0 then on
e0220 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 ly the.** file d
e0230 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a ata is synced..*
e0240 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c *.** Under Unix,
e0250 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 also make sure
e0260 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f that the directo
e0270 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ry entry for the
e0280 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 file.** has bee
e0290 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 n created by fsy
e02a0 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 nc-ing the direc
e02b0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 tory that contai
e02c0 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 ns the file..**
e02d0 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 If we do not do
e02e0 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f this and we enco
e02f0 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 unter a power fa
e0300 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 ilure, the direc
e0310 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f tory.** entry fo
e0320 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 r the journal mi
e0330 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 ght not exist af
e0340 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 ter we reboot.
e0350 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 The next.** SQLi
e0360 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 te to access the
e0370 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b file will not k
e0380 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 now that the jou
e0390 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 rnal exists (bec
e03a0 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 ause.** the dire
e03b0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 ctory entry for
e03c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 the journal was
e03d0 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 never created) a
e03e0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
e03f0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 on.** will not r
e0400 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 oll back - possi
e0410 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 bly leading to d
e0420 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
e0430 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
e0440 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 t unixSync(sqlit
e0450 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
e0460 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 flags){. int r
e0470 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 c;. unixFile *p
e0480 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
e0490 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 *)id;.. int isD
e04a0 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 ataOnly = (flags
e04b0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 &SQLITE_SYNC_DAT
e04c0 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 AONLY);. int is
e04d0 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 Fullsync = (flag
e04e0 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f s&0x0F)==SQLITE_
e04f0 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a SYNC_FULL;.. /*
e0500 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 Check that one
e0510 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e of SQLITE_SYNC_N
e0520 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 ORMAL or FULL wa
e0530 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 s passed */. as
e0540 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 sert((flags&0x0F
e0550 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e )==SQLITE_SYNC_N
e0560 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 ORMAL. || (
e0570 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c flags&0x0F)==SQL
e0580 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 ITE_SYNC_FULL.
e0590 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 );.. /* Unix ca
e05a0 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 nnot, but some s
e05b0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 ystems may retur
e05c0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 n SQLITE_FULL fr
e05d0 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 om here. This.
e05e0 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 ** line is to te
e05f0 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f st that doing so
e0600 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 does not cause
e0610 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 any problems..
e0620 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 */. SimulateDis
e0630 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 kfullError( retu
e0640 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 rn SQLITE_FULL )
e0650 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
e0660 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 le );. OSTRACE2
e0670 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e ("SYNC %-3d\n
e0680 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
e0690 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 rc = full_fsync(
e06a0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c pFile->h, isFull
e06b0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 sync, isDataOnly
e06c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
e06d0 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 rror( rc=1 );.
e06e0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 if( rc ){. pF
e06f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
e0700 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 errno;. retu
e0710 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e0720 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 FSYNC;. }. if(
e0730 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 pFile->dirfd>=0
e0740 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b ){. int err;
e0750 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 . OSTRACE4("D
e0760 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 IRSYNC %-3d (hav
e0770 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 e_fullfsync=%d f
e0780 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 ullsync=%d)\n",
e0790 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 pFile->dirfd,.
e07a0 20 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 HAVE_F
e07b0 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c ULLFSYNC, isFull
e07c0 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 sync);.#ifndef S
e07d0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 QLITE_DISABLE_DI
e07e0 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 RSYNC. /* The
e07f0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
e0800 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 is only attempte
e0810 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 d if full_fsync
e0820 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 is. ** turned
e0830 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 off or unavaila
e0840 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f ble. If a full_
e0850 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 fsync occurred a
e0860 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 bove,. ** the
e0870 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 n the directory
e0880 73 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 sync is superflu
e0890 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ous.. */.
e08a0 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 if( (!HAVE_FULLF
e08b0 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 SYNC || !isFulls
e08c0 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 ync) && full_fsy
e08d0 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c nc(pFile->dirfd,
e08e0 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 0,0) ){. /
e08f0 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 *. ** We h
e0900 61 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c ave received mul
e0910 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 tiple reports of
e0920 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 fsync() returni
e0930 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 ng. ** err
e0940 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 ors when applied
e0950 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 to directories
e0960 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 on certain file
e0970 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 systems..
e0980 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 ** A failed dire
e0990 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f ctory sync is no
e09a0 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 t a big deal. S
e09b0 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 o it seems.
e09c0 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 ** better to i
e09d0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e gnore the error.
e09e0 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 Ticket #1657.
e09f0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
e0a00 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 /* pFile->lastEr
e0a10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a rno = errno; */.
e0a20 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e /* return
e0a30 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a SQLITE_IOERR; *
e0a40 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 /. }.#endif.
e0a50 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 err = close(p
e0a60 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a File->dirfd); /*
e0a70 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 Only need to sy
e0a80 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 nc once, so clos
e0a90 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 e the */. if(
e0aa0 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 err==0 ){
e0ab0 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 /* direc
e0ac0 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 tory when we are
e0ad0 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 done */. p
e0ae0 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 File->dirfd = -1
e0af0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e0b00 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
e0b10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
e0b20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
e0b30 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a OERR_DIR_CLOSE;.
e0b40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
e0b50 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
e0b60 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e Truncate an open
e0b70 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 file to a speci
e0b80 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 fied size.*/.sta
e0b90 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e tic int unixTrun
e0ba0 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
e0bb0 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 e *id, i64 nByte
e0bc0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
e0bd0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 ssert( id );. S
e0be0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e0bf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e0c00 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a ERR_TRUNCATE );.
e0c10 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 rc = ftruncate
e0c20 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 (((unixFile*)id)
e0c30 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 ->h, (off_t)nByt
e0c40 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a e);. if( rc ){.
e0c50 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
e0c60 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
e0c70 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 errno;. retu
e0c80 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e0c90 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 TRUNCATE;. }els
e0ca0 65 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 e{.#ifndef NDEBU
e0cb0 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 G. /* If we a
e0cc0 72 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 re doing a norma
e0cd0 6c 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74 l write to a dat
e0ce0 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f abase file (as o
e0cf0 70 70 6f 73 65 64 20 74 6f 0a 20 20 20 20 2a 2a pposed to. **
e0d00 20 64 6f 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 doing a hot-jou
e0d10 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 rnal rollback or
e0d20 20 61 20 77 72 69 74 65 20 74 6f 20 73 6f 6d 65 a write to some
e0d30 20 66 69 6c 65 20 6f 74 68 65 72 20 74 68 61 6e file other than
e0d40 20 61 0a 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c a. ** normal
e0d50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 database file)
e0d60 61 6e 64 20 77 65 20 74 72 75 6e 63 61 74 65 20 and we truncate
e0d70 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f the file to zero
e0d80 20 6c 65 6e 67 74 68 2c 0a 20 20 20 20 2a 2a 20 length,. **
e0d90 74 68 61 74 20 65 66 66 65 63 74 69 76 65 6c 79 that effectively
e0da0 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 61 updates the cha
e0db0 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 nge counter. Th
e0dc0 69 73 20 6d 69 67 68 74 20 68 61 70 70 65 6e 0a is might happen.
e0dd0 20 20 20 20 2a 2a 20 77 68 65 6e 20 72 65 73 74 ** when rest
e0de0 6f 72 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 oring a database
e0df0 20 75 73 69 6e 67 20 74 68 65 20 62 61 63 6b 75 using the backu
e0e00 70 20 41 50 49 20 66 72 6f 6d 20 61 20 7a 65 72 p API from a zer
e0e10 6f 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20 o-length. **
e0e20 73 6f 75 72 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 source.. */.
e0e30 20 20 20 69 66 28 20 28 28 75 6e 69 78 46 69 6c if( ((unixFil
e0e40 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f 72 6d 61 6c e*)id)->inNormal
e0e50 57 72 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d Write && nByte==
e0e60 30 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 0 ){. ((uni
e0e70 78 46 69 6c 65 2a 29 69 64 29 2d 3e 74 72 61 6e xFile*)id)->tran
e0e80 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 0a 20 sCntrChng = 1;.
e0e90 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
e0ea0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e0eb0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 K;. }.}../*.**
e0ec0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 Determine the cu
e0ed0 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 rrent size of a
e0ee0 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f file in bytes.*/
e0ef0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e0f00 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
e0f10 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a _file *id, i64 *
e0f20 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 pSize){. int rc
e0f30 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 ;. struct stat
e0f40 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 69 buf;. assert( i
e0f50 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 d );. rc = fsta
e0f60 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 t(((unixFile*)id
e0f70 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 )->h, &buf);. S
e0f80 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e0f90 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63 rc=1 );. if( rc
e0fa0 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 !=0 ){. ((uni
e0fb0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
e0fc0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e0fd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e0fe0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
e0ff0 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 66 }. *pSize = buf
e1000 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 .st_size;.. /*
e1010 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a When opening a z
e1020 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 73 ero-size databas
e1030 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49 e, the findLockI
e1040 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 0a nfo() procedure.
e1050 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 69 ** writes a si
e1060 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 74 ngle byte into t
e1070 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 hat file in orde
e1080 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 r to work around
e1090 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74 a bug. ** in t
e10a0 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69 he OS-X msdos fi
e10b0 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72 lesystem. In or
e10c0 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f der to avoid pro
e10d0 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 72 blems with upper
e10e0 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65 . ** layers, we
e10f0 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 20 need to report
e1100 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 61 this file size a
e1110 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f 75 s zero even thou
e1120 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 65 gh it is. ** re
e1130 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 74 ally 1. Ticket
e1140 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69 #3260.. */. i
e1150 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a f( *pSize==1 ) *
e1160 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72 pSize = 0;... r
e1170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e1180 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 .}..#if SQLITE_E
e1190 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e11a0 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f YLE && defined(_
e11b0 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 _APPLE__)./*.**
e11c0 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f 78 Handler for prox
e11d0 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63 y-locking file-c
e11e0 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 44 ontrol verbs. D
e11f0 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20 efined below in
e1200 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20 the.** proxying
e1210 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e locking division
e1220 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e1230 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c proxyFileControl
e1240 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 (sqlite3_file*,i
e1250 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 nt,void*);.#endi
e1260 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d f.../*.** Inform
e1270 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f ation and contro
e1280 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c l of an open fil
e1290 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 e handle..*/.sta
e12a0 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 tic int unixFile
e12b0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f Control(sqlite3_
e12c0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 file *id, int op
e12d0 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 , void *pArg){.
e12e0 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 switch( op ){.
e12f0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 case SQLITE_F
e1300 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 CNTL_LOCKSTATE:
e1310 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 {. *(int*)p
e1320 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 Arg = ((unixFile
e1330 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b *)id)->locktype;
e1340 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e1350 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
e1360 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c case SQLITE_L
e1370 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 AST_ERRNO: {.
e1380 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
e1390 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
e13a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 ->lastErrno;.
e13b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e13c0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 _OK;. }.#ifnd
e13d0 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a ef NDEBUG. /*
e13e0 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73 The pager calls
e13f0 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 this method to
e1400 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68 signal that it h
e1410 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 as done. ** a
e1420 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 rollback and th
e1430 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
e1440 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63 is therefore unc
e1450 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 2a hanged and. *
e1460 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 73 * it hence it is
e1470 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 6e OK for the tran
e1480 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 63 saction change c
e1490 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 20 ounter to be.
e14a0 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 ** unchanged..
e14b0 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 */. case S
e14c0 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 QLITE_FCNTL_DB_U
e14d0 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20 NCHANGED: {.
e14e0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
e14f0 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b )->dbUpdate = 0;
e1500 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e1510 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 LITE_OK;. }.#
e1520 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
e1530 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e1540 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
e1550 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 (__APPLE__).
e1560 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f case SQLITE_SET_
e1570 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20 LOCKPROXYFILE:.
e1580 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 case SQLITE_G
e1590 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
e15a0 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
e15b0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f proxyFileContro
e15c0 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20 l(id,op,pArg);.
e15d0 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
e15e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e15f0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
e1600 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e1610 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e */. }. return
e1620 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d SQLITE_ERROR;.}
e1630 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
e1640 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 he sector size i
e1650 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 n bytes of the u
e1660 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 nderlying block
e1670 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 device for.** th
e1680 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 e specified file
e1690 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 . This is almost
e16a0 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
e16b0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a s, but may be.**
e16c0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 larger for some
e16d0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 devices..**.**
e16e0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 SQLite code assu
e16f0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f mes this functio
e1700 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 n cannot fail. I
e1710 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 t also assumes t
e1720 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 hat.** if two fi
e1730 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 les are created
e1740 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 in the same file
e1750 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 -system director
e1760 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 y (i.e..** a dat
e1770 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f abase and its jo
e1780 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 urnal file) that
e1790 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
e17a0 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 will be the.**
e17b0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a same for both..*
e17c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e17d0 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 xSectorSize(sqli
e17e0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 te3_file *NotUse
e17f0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
e1800 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e1810 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e1820 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
e1830 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 SIZE;.}../*.** R
e1840 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65 eturn the device
e1850 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
e1860 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54 for the file. T
e1870 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 20 his is always 0
e1880 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61 for unix..*/.sta
e1890 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 69 tic int unixDevi
e18a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
e18b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
e18c0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
e18d0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e18e0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
e18f0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 0;.}../*.** Here
e1900 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 6d ends the implem
e1910 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 entation of all
e1920 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 sqlite3_file met
e1930 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a hods..**.*******
e1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
e1950 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 End sqlite3_file
e1960 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a Methods *******
e1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1980 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
e1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19d0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
e19e0 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f This division co
e19f0 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f ntains definitio
e1a00 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f ns of sqlite3_io
e1a10 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 _methods objects
e1a20 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 that.** impleme
e1a30 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 20 nt various file
e1a40 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 69 locking strategi
e1a50 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e es. It also con
e1a60 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e tains definition
e1a70 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 22 s.** of "finder"
e1a80 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66 functions. A f
e1a90 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 inder-function i
e1aa0 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 s used to locate
e1ab0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
e1ac0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d .** sqlite3_io_m
e1ad0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f ethods object fo
e1ae0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 r a particular d
e1af0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
e1b00 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 66 he pAppData.** f
e1b10 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 ield of the sqli
e1b20 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a 65 te3_vfs VFS obje
e1b30 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 cts are initiali
e1b40 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 65 zed to be pointe
e1b50 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 rs to.** the cor
e1b60 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e 63 rect finder-func
e1b70 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 46 tion for that VF
e1b80 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 S..**.** Most fi
e1b90 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 nder functions r
e1ba0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
e1bb0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 to a fixed sqlit
e1bc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a e3_io_methods.**
e1bd0 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e object. The on
e1be0 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 66 ly interesting f
e1bf0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 inder-function i
e1c00 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 s autolockIoFind
e1c10 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f er, which.** loo
e1c20 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79 ks at the filesy
e1c30 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 72 stem type and tr
e1c40 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 65 ies to guess the
e1c50 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a best locking.**
e1c60 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 74 strategy from t
e1c70 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 hat..**.** For f
e1c80 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c inder-funtion F,
e1c90 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 65 two objects are
e1ca0 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 created:.**.**
e1cb0 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c 20 (1) The real
e1cc0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e1cd0 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 2e named "FImpt()".
e1ce0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 20 .**.** (2) A
e1cf0 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 72 constant pointer
e1d00 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
e1d10 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22 n named just "F"
e1d20 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ..**.**.** A poi
e1d30 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f nter to the F po
e1d40 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
e1d50 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61 the pAppData va
e1d60 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f lue for VFS.** o
e1d70 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65 bjects. We have
e1d80 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74 to do this inst
e1d90 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70 ead of letting p
e1da0 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a AppData point.**
e1db0 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65 directly at the
e1dc0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
e1dd0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73 since C90 rules
e1de0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a prevent a void*
e1df0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74 .** from be cast
e1e00 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e into a function
e1e10 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a pointer..**.**.
e1e20 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 ** Each instance
e1e30 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67 of this macro g
e1e40 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a enerates two obj
e1e50 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 ects:.**.** *
e1e60 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69 A constant sqli
e1e70 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e1e80 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f bject call METHO
e1e90 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69 D that has locki
e1ea0 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f ng.** metho
e1eb0 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 ds CLOSE, LOCK,
e1ec0 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 UNLOCK, CKRESLOC
e1ed0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e K..**.** * An
e1ee0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 I/O method find
e1ef0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c er function call
e1f00 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72 ed FINDER that r
e1f10 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
e1f20 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 .** to the
e1f30 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e METHOD object in
e1f40 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75 the previous bu
e1f50 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 llet..*/.#define
e1f60 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45 IOMETHODS(FINDE
e1f70 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45 R, METHOD, CLOSE
e1f80 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 , LOCK, UNLOCK,
e1f90 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20 CKLOCK)
e1fa0 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 \.static c
e1fb0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e1fc0 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d methods METHOD =
e1fd0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
e1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ff0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20 \. 1,
e2000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2010 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
e2020 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 n */
e2030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2040 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 \. CLOSE,
e2050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2060 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a /* xClose *
e2070 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2090 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 64 \. unixRead
e20a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e20b0 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20 /* xRead */
e20c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20e0 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65 \. unixWrite
e20f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2100 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20 /* xWrite */
e2110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2130 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61 \. unixTrunca
e2140 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
e2150 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a /* xTruncate *
e2160 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e2170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2180 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20 \. unixSync,
e2190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e21a0 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20 /* xSync */
e21b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e21c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e21d0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 . unixFileSize
e21e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e21f0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20 /* xFileSize */
e2200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e2220 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 LOCK,
e2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e2240 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 * xLock */
e2250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e2270 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20 UNLOCK,
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2290 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 xUnlock */
e22a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e22b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e22c0 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 CKLOCK,
e22d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e22e0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e22f0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ck */
e2300 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e2310 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c unixFileControl,
e2320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2330 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 FileControl */
e2340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2350 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
e2360 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20 nixSectorSize,
e2370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
e2380 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20 ectorSize */
e2390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e23a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e \. un
e23b0 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 ixDeviceCharacte
e23c0 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65 ristics /* xDe
e23d0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 viceCapabilities
e23e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e23f0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20 \.};
e2400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2440 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
e2450 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e2460 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52 _methods *FINDER
e2470 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 ##Impl(const cha
e2480 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 2a r *z, unixFile *
e2490 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 44 p){ \. UNUSED
e24a0 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55 _PARAMETER(z); U
e24b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e24c0 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p);
e24d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e24e0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20 \. return
e24f0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20 &METHOD;
e2500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2530 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20 \.}
e2540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2580 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 \.static cons
e2590 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e25a0 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49 hods *(*const FI
e25b0 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72 NDER)(const char
e25c0 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 20 *,unixFile *p)
e25d0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52 \. = FINDER
e25e0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48 ##Impl;../*.** H
e25f0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74 ere are all of t
e2600 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
e2610 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f thods objects fo
e2620 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a r each of the.**
e2630 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
e2640 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20 ies. Functions
e2650 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e that return poin
e2660 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65 ters to these me
e2670 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73 thods.** are als
e2680 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f o created..*/.IO
e2690 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78 METHODS(. posix
e26a0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e26b0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e26c0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e26d0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 posixIoMethods
e26e0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
e26f0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e2700 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e2710 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 . unixClose,
e2720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2730 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e2740 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 . unixLock,
e2750 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2760 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e2770 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20 unixUnlock,
e2780 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2790 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e27a0 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 . unixCheckRese
e27b0 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20 rvedLock /*
e27c0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e27d0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 ck method */.).I
e27e0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f OMETHODS(. nolo
e27f0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 ckIoFinder,
e2800 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e2810 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e2820 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f . nolockIoMetho
e2830 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ds, /*
e2840 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2850 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e2860 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c /. nolockClose,
e2870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2880 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e2890 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 /. nolockLock,
e28a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e28b0 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e28c0 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c . nolockUnlock,
e28d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e28e0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e28f0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 /. nolockCheckR
e2900 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a eservedLock /*
e2910 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e2920 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e2930 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74 IOMETHODS(. dot
e2940 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 lockIoFinder,
e2950 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
e2960 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
e2970 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 /. dotlockIoMet
e2980 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a hods, /*
e2990 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e29a0 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
e29b0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 */. dotlockClos
e29c0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e, /
e29d0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
e29e0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b */. dotlockLock
e29f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e2a00 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
e2a10 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 /. dotlockUnloc
e2a20 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a k, /*
e2a30 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
e2a40 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63 */. dotlockChec
e2a50 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f kReservedLock /
e2a60 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
e2a70 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
e2a80 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
e2a90 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2aa0 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 E && !OS_VXWORKS
e2ab0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c .IOMETHODS(. fl
e2ac0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
e2ad0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e2ae0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e2af0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68 */. flockIoMeth
e2b00 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
e2b10 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e2b20 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e2b30 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 */. flockClose
e2b40 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2b50 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e2b60 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c */. flockLock,
e2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2b80 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e2b90 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b */. flockUnlock
e2ba0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e2bb0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e2bc0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b */. flockCheck
e2bd0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
e2be0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e2bf0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e2c00 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53 ).#endif..#if OS
e2c10 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f _VXWORKS.IOMETHO
e2c20 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65 DS(. semIoFinde
e2c30 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
e2c40 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e2c50 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d on name */. sem
e2c60 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
e2c70 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e2c80 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e2c90 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 ect name */. se
e2ca0 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 mClose,
e2cb0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e2cc0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 e method */. se
e2cd0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 mLock,
e2ce0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e2cf0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d method */. sem
e2d00 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e2d10 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e2d20 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
e2d30 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f mCheckReservedLo
e2d40 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 ck /* xChec
e2d50 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e2d60 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
e2d70 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
e2d80 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
e2d90 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e2da0 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 G_STYLE.IOMETHOD
e2db0 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72 S(. afpIoFinder
e2dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e2dd0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
e2de0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49 n name */. afpI
e2df0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
e2e00 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
e2e10 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e2e20 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 ct name */. afp
e2e30 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
e2e40 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
e2e50 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e2e60 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
e2e70 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e2e80 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 method */. afpU
e2e90 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
e2ea0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e2eb0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e2ec0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e2ed0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b k /* xCheck
e2ee0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
e2ef0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
e2f00 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f 6c ./*.** The "Whol
e2f10 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 20 e File Locking"
e2f20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 74 finder returns t
e2f30 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 6d he same set of m
e2f40 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 65 ethods as.** the
e2f50 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 66 posix locking f
e2f60 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 61 inder. But it a
e2f70 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 4c lso sets the SQL
e2f80 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
e2f90 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 74 OCKING.** flag t
e2fa0 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 69 o force the posi
e2fb0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 x advisory locks
e2fc0 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 68 to cover the wh
e2fd0 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64 ole file instead
e2fe0 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 6d .** of just a sm
e2ff0 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 65 all span of byte
e3000 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 20 s near the 1GiB
e3010 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c 65 boundary. Whole
e3020 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a 2a File Locking.**
e3030 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e 46 is useful on NF
e3040 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 20 S-mounted files
e3050 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 4e since it helps N
e3060 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 63 FS to maintain c
e3070 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e 63 ache.** coherenc
e3080 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 20 y. But it is a
e3090 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 68 detriment to oth
e30a0 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 73 er filesystems s
e30b0 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a 20 ince it runs.**
e30c0 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 slower..*/.stati
e30d0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
e30e0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 69 io_methods *posi
e30f0 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c xWflIoFinderImpl
e3100 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 75 (const char*z, u
e3110 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 4e nixFile*p){. UN
e3120 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
e3130 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 67 );. p->fileFlag
e3140 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 s = SQLITE_WHOLE
e3150 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 _FILE_LOCKING;.
e3160 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e3170 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 69 Methods;.}.stati
e3180 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
e3190 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 io_methods . *(
e31a0 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c 49 *const posixWflI
e31b0 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 oFinder)(const c
e31c0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 har*,unixFile *p
e31d0 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 ) = posixWflIoFi
e31e0 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a nderImpl;../*.**
e31f0 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 The proxy locki
e3200 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 22 ng method is a "
e3210 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 6e super-method" in
e3220 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 20 the sense that
e3230 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 6f it.** opens seco
e3240 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 72 ndary file descr
e3250 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 iptors for the c
e3260 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 69 onch and lock fi
e3270 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 73 les and.** it us
e3280 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 69 es proxy, dot-fi
e3290 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c 6f le, AFP, and flo
e32a0 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 ck() locking met
e32b0 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a 2a hods on those.**
e32c0 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 73 secondary files
e32d0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 . For this reas
e32e0 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e on, the division
e32f0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
e3300 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e .** proxy lockin
e3310 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 63 g is located muc
e3320 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69 h further down i
e3330 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 74 n the file. But
e3340 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 67 we need.** to g
e3350 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 69 o ahead and defi
e3360 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 ne the sqlite3_i
e3370 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 69 o_methods and fi
e3380 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a nder function.**
e3390 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 for proxy locki
e33a0 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20 ng here. So we
e33b0 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 20 forward declare
e33c0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 2e the I/O methods.
e33d0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
e33e0 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 __APPLE__) && SQ
e33f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
e3400 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 63 ING_STYLE.static
e3410 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 int proxyClose(
e3420 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a sqlite3_file*);.
e3430 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
e3440 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
e3450 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 e*, int);.static
e3460 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b int proxyUnlock
e3470 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
e3480 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 int);.static int
e3490 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 proxyCheckReser
e34a0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
e34b0 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 4f file*, int*);.IO
e34c0 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 79 METHODS(. proxy
e34d0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e34e0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e34f0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e3500 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 proxyIoMethods
e3510 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
e3520 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e3530 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e3540 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 20 . proxyClose,
e3550 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e3560 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e3570 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 20 . proxyLock,
e3580 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e3590 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e35a0 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20 proxyUnlock,
e35b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e35c0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e35d0 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 . proxyCheckRes
e35e0 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 ervedLock /*
e35f0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e3600 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 ck method */.).#
e3610 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 69 endif...#if defi
e3620 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
e3630 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
e3640 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a LOCKING_STYLE./*
e3650 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 65 .** This "finde
e3660 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 r" function atte
e3670 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e mpts to determin
e3680 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 e the best locki
e3690 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a 20 ng strategy .**
e36a0 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
e36b0 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 22 file "filePath"
e36c0 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 72 . It then retur
e36d0 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 ns the sqlite3_i
e36e0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a o_methods.** obj
e36f0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ect that impleme
e3700 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 67 nts that strateg
e3710 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 y..**.** This is
e3720 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c 79 for MacOSX only
e3730 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
e3740 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e3750 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f hods *autolockIo
e3760 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f FinderImpl(. co
e3770 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61 nst char *filePa
e3780 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f th, /* name o
e3790 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
e37a0 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c ile */. unixFil
e37b0 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 e *pNew
e37c0 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 6f /* open file o
e37d0 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 61 bject for the da
e37e0 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 tabase file */.)
e37f0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
e3800 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20 struct Mapping
e3810 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
e3820 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 20 *zFilesystem;
e3830 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
e3840 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 6e ilesystem type n
e3850 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 ame */. const
e3860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3870 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 ods *pMethods;
e3880 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 20 /* Appropriate
e3890 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a locking method *
e38a0 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b /. } aMap[] = {
e38b0 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 20 . { "hfs",
e38c0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 &posixIoMethods
e38d0 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 2c },. { "ufs",
e38e0 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 &posixIoMeth
e38f0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 ods },. { "af
e3900 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 pfs", &afpIoMet
e3910 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 53 hods },.#ifdef S
e3920 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 QLITE_ENABLE_AFP
e3930 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 _LOCKING_SMB.
e3940 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 66 { "smbfs", &af
e3950 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 pIoMethods },.#e
e3960 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 lse. { "smbfs
e3970 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68 ", &flockIoMeth
e3980 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 ods },.#endif.
e3990 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 6e { "webdav", &n
e39a0 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d olockIoMethods }
e39b0 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 ,. { 0, 0 }.
e39c0 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 };. int i;. s
e39d0 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 truct statfs fsI
e39e0 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 6c nfo;. struct fl
e39f0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 ock lockInfo;..
e3a00 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 if( !filePath )
e3a10 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 {. /* If file
e3a20 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 Path==NULL that
e3a30 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 means we are dea
e3a40 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e ling with a tran
e3a50 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a sient file. *
e3a60 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 * that does not
e3a70 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 need to be locke
e3a80 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e d. */. return
e3a90 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e3aa0 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 61 s;. }. if( sta
e3ab0 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 66 tfs(filePath, &f
e3ac0 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a sInfo) != -1 ){.
e3ad0 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e 66 if( fsInfo.f
e3ae0 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f _flags & MNT_RDO
e3af0 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 74 NLY ){. ret
e3b00 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 urn &nolockIoMet
e3b10 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 hods;. }.
e3b20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d for(i=0; aMap[i]
e3b30 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b .zFilesystem; i+
e3b40 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 +){. if( st
e3b50 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 rcmp(fsInfo.f_fs
e3b60 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 typename, aMap[i
e3b70 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d ].zFilesystem)==
e3b80 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 0 ){. ret
e3b90 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 urn aMap[i].pMet
e3ba0 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 hods;. }.
e3bb0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 }. }.. /* De
e3bc0 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e 64 fault case. Hand
e3bd0 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68 les, amongst oth
e3be0 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a ers, "nfs".. **
e3bf0 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65 Test byte-range
e3c00 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 lock using fcnt
e3c10 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c l(). If the call
e3c20 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a succeeds, . **
e3c30 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 assume that the
e3c40 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 file-system sup
e3c50 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c ports POSIX styl
e3c60 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 e locks. . */.
e3c70 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 lockInfo.l_len
e3c80 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e = 1;. lockInfo.
e3c90 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c l_start = 0;. l
e3ca0 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 ockInfo.l_whence
e3cb0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c = SEEK_SET;. l
e3cc0 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d ockInfo.l_type =
e3cd0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 F_RDLCK;. if(
e3ce0 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 fcntl(pNew->h, F
e3cf0 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 _GETLK, &lockInf
e3d00 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 70 o)!=-1 ) {. p
e3d10 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d New->fileFlags =
e3d20 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
e3d30 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 20 LE_LOCKING;.
e3d40 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d return &posixIoM
e3d50 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b ethods;. }else{
e3d60 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74 . return &dot
e3d70 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
e3d80 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 }.}.static cons
e3d90 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e3da0 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 hods . *(*const
e3db0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
e3dc0 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 r)(const char*,u
e3dd0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f nixFile*) = auto
e3de0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c lockIoFinderImpl
e3df0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 ;..#endif /* def
e3e00 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
e3e10 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
e3e20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a _LOCKING_STYLE *
e3e30 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b /..#if OS_VXWORK
e3e40 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 S && SQLITE_ENAB
e3e50 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e3e60 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 ./* .** This "fi
e3e70 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 nder" function a
e3e80 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
e3e90 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f mine the best lo
e3ea0 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a cking strategy .
e3eb0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ** for the datab
e3ec0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 ase file "filePa
e3ed0 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 th". It then re
e3ee0 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 turns the sqlite
e3ef0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 3_io_methods.**
e3f00 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c object that impl
e3f10 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 ements that stra
e3f20 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
e3f30 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20 is for VXWorks
e3f40 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 only..*/.static
e3f50 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e3f60 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f _methods *autolo
e3f70 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a ckIoFinderImpl(.
e3f80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 const char *fi
e3f90 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 lePath, /* na
e3fa0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
e3fb0 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 se file */. uni
e3fc0 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 xFile *pNew
e3fd0 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65 /* the ope
e3fe0 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f n file object */
e3ff0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f .){. struct flo
e4000 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 ck lockInfo;..
e4010 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b if( !filePath ){
e4020 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 . /* If fileP
e4030 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d ath==NULL that m
e4040 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c eans we are deal
e4050 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 ing with a trans
e4060 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a ient file. **
e4070 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e that does not n
e4080 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 eed to be locked
e4090 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
e40a0 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 &nolockIoMethods
e40b0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 ;. }.. /* Test
e40c0 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73 if fcntl() is s
e40d0 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65 upported and use
e40e0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 POSIX style loc
e40f0 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 ks.. ** Otherwi
e4100 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 se fall back to
e4110 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 the named semaph
e4120 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f ore method.. */
e4130 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 . lockInfo.l_le
e4140 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 n = 1;. lockInf
e4150 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 o.l_start = 0;.
e4160 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e lockInfo.l_when
e4170 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
e4180 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 lockInfo.l_type
e4190 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 = F_RDLCK;. if
e41a0 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c ( fcntl(pNew->h,
e41b0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 F_GETLK, &lockI
e41c0 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 nfo)!=-1 ) {.
e41d0 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e41e0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 Methods;. }else
e41f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 65 {. return &se
e4200 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a mIoMethods;. }.
e4210 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 }.static const s
e4220 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e4230 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75 s . *(*const au
e4240 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 tolockIoFinder)(
e4250 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 const char*,unix
e4260 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63 File*) = autoloc
e4270 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a kIoFinderImpl;..
e4280 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 #endif /* OS_VXW
e4290 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 ORKS && SQLITE_E
e42a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e42b0 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e YLE */../*.** An
e42c0 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 abstract type f
e42d0 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 or a pointer to
e42e0 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 a IO method find
e42f0 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a er function:.*/.
e4300 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71 typedef const sq
e4310 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e4320 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29 *(*finder_type)
e4330 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
e4340 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a 2a xFile*);.../****
e4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4390 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
e43a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e43b0 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 ***** sqlite3_vf
e43c0 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a s methods ******
e43d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e43e0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
e43f0 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 s division conta
e4400 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e ins the implemen
e4410 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 tation of method
e4420 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 s on the.** sqli
e4430 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a te3_vfs object..
e4440 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 */../*.** Initia
e4450 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 lize the content
e4460 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c s of the unixFil
e4470 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e e structure poin
e4480 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a ted to by pId..*
e4490 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
e44a0 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73 lInUnixFile(. s
e44b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
e44c0 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 , /* Pointe
e44d0 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 r to vfs object
e44e0 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 */. int h,
e44f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e4500 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 Open file descri
e4510 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69 ptor of file bei
e4520 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 ng opened */. i
e4530 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20 nt dirfd,
e4540 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 /* Direct
e4550 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 ory file descrip
e4560 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 tor */. sqlite3
e4570 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 _file *pId,
e4580 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65 /* Write to the
e4590 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
e45a0 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f ure here */. co
e45b0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
e45c0 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 ame, /* Name of
e45d0 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20 the file being
e45e0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 opened */. int
e45f0 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 noLock,
e4600 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b /* Omit lock
e4610 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 ing if true */.
e4620 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 int isDelete
e4630 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 /* Dele
e4640 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 te on close if t
e4650 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 rue */.){. cons
e4660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e4670 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74 hods *pLockingSt
e4680 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 yle;. unixFile
e4690 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c *pNew = (unixFil
e46a0 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72 e *)pId;. int r
e46b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
e46c0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
e46d0 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 pLock==NULL );.
e46e0 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 assert( pNew->p
e46f0 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 Open==NULL );..
e4700 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73 /* Parameter is
e4710 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 Delete is only u
e4720 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 sed on vxworks.
e4730 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70 Express this exp
e4740 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 65 licitly . ** he
e4750 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f re to prevent co
e4760 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 mpiler warnings
e4770 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72 about unused par
e4780 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 ameters.. */.
e4790 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e47a0 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 4f (isDelete);.. O
e47b0 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20 STRACE3("OPEN
e47c0 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20 %-3d %s\n", h,
e47d0 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a zFilename); .
e47e0 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 pNew->h = h;.
e47f0 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64 pNew->dirfd = d
e4800 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45 irfd;. SET_THRE
e4810 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 4e ADID(pNew);. pN
e4820 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20 ew->fileFlags =
e4830 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 0;..#if OS_VXWOR
e4840 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d KS. pNew->pId =
e4850 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 vxworksFindFile
e4860 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 Id(zFilename);.
e4870 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d if( pNew->pId==
e4880 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 0 ){. noLock
e4890 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 = 1;. rc = SQ
e48a0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
e48b0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f #endif.. if( no
e48c0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 Lock ){. pLoc
e48d0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c kingStyle = &nol
e48e0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 ockIoMethods;.
e48f0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b }else{. pLock
e4900 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 ingStyle = (**(f
e4910 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73 inder_type*)pVfs
e4920 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c ->pAppData)(zFil
e4930 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 69 ename, pNew);.#i
e4940 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e4950 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
e4960 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65 /* Cache zFile
e4970 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b name in the lock
e4980 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50 ing context (AFP
e4990 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 and dotlock ove
e49a0 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a rride) for. *
e49b0 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69 * proxyLock acti
e49c0 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 vation is possib
e49d0 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79 le (remote proxy
e49e0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20 is based on db
e49f0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69 name). ** zFi
e4a00 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76 lename remains v
e4a10 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20 alid until file
e4a20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75 is closed, to su
e4a30 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65 pport */. pNe
e4a40 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
e4a50 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65 t = (void*)zFile
e4a60 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d name;.#endif. }
e4a70 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 .. if( pLocking
e4a80 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49 Style == &posixI
e4a90 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
e4aa0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e4ab0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
e4ac0 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
e4ad0 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
e4ae0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 w->pOpen);. i
e4af0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
e4b00 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
e4b10 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 an error occured
e4b20 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f in findLockInfo
e4b30 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69 (), close the fi
e4b40 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 20 le descriptor.
e4b50 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 ** immediate
e4b60 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61 ly, before relea
e4b70 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20 sing the mutex.
e4b80 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 6d findLockInfo() m
e4b90 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a 2a ay fail. **
e4ba0 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 6f in two scenario
e4bb0 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 s:. **.
e4bc0 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 6c ** (a) A cal
e4bd0 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 69 l to fstat() fai
e4be0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20 led.. **
e4bf0 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 69 (b) A malloc fai
e4c00 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 led.. **.
e4c10 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f 20 ** Scenario
e4c20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 63 (b) may only occ
e4c30 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 73 ur if the proces
e4c40 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f 20 s is holding no
e4c50 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66 other. ** f
e4c60 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
e4c70 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
e4c80 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 file. If there
e4c90 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 0a were other file.
e4ca0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 ** descrip
e4cb0 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 6c tors on this fil
e4cc0 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c 6f e, then no mallo
e4cd0 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 69 c would be requi
e4ce0 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 red by. **
e4cf0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 20 findLockInfo().
e4d00 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
e4d10 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 65 ase, it is quite
e4d20 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a 20 safe to close.
e4d30 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 68 ** handle h
e4d40 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 72 - as it is guar
e4d50 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 70 anteed that no p
e4d60 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 osix locks will
e4d70 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 be released.
e4d80 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 6f ** by doing so
e4d90 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
e4da0 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f 20 ** If scenario
e4db0 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 65 (a) caused the e
e4dc0 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 73 rror then things
e4dd0 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 65 are not so safe
e4de0 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 . The. ** i
e4df0 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 69 mplicit assumpti
e4e00 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20 on here is that
e4e10 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c 73 if fstat() fails
e4e20 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e 0a , things are in.
e4e30 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 61 ** such ba
e4e40 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 6f d shape that dro
e4e50 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 20 pping a lock or
e4e60 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 two doesn't matt
e4e70 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 2a er much.. *
e4e80 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 29 /. close(h)
e4e90 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a ;. h = -1;.
e4ea0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 }. unixLe
e4eb0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
e4ec0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e4ed0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e4ee0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 && defined(__AP
e4ef0 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 PLE__). else if
e4f00 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
e4f10 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 == &afpIoMethods
e4f20 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c ){. /* AFP l
e4f30 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
e4f40 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
e4f50 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c needs to be incl
e4f60 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 uded in. ** t
e4f70 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e he afpLockingCon
e4f80 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 text.. */.
e4f90 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
e4fa0 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e xt *pCtx;. pN
e4fb0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e4fc0 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 xt = pCtx = sqli
e4fd0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
e4fe0 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 of(*pCtx) );.
e4ff0 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a if( pCtx==0 ){.
e5000 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e5010 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c E_NOMEM;. }el
e5020 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a se{. /* NB:
e5030 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 zFilename exist
e5040 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 s and remains va
e5050 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 lid until the fi
e5060 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 le is closed.
e5070 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 ** according
e5080 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 to requirement F
e5090 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 11141. So we do
e50a0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
e50b0 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 e a. ** cop
e50c0 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d y of the filenam
e50d0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 e. */. pCtx
e50e0 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 ->dbPath = zFile
e50f0 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e name;. sran
e5100 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20 domdev();.
e5110 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e5120 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e ;. rc = fin
e5130 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 dLockInfo(pNew,
e5140 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 NULL, &pNew->pOp
e5150 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c en);. unixL
e5160 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 eaveMutex();
e5170 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 . }. }.#
e5180 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 endif.. else if
e5190 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
e51a0 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 == &dotlockIoMet
e51b0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 hods ){. /* D
e51c0 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 otfile locking u
e51d0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 ses the file pat
e51e0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f h so it needs to
e51f0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a be included in.
e5200 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f ** the dotlo
e5210 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ckLockingContext
e5220 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 . */. cha
e5230 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 r *zLockFile;.
e5240 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b int nFilename;
e5250 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d . nFilename =
e5260 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69 (int)strlen(zFi
e5270 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 lename) + 6;.
e5280 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 zLockFile = (ch
e5290 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c ar *)sqlite3_mal
e52a0 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a loc(nFilename);.
e52b0 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c if( zLockFil
e52c0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 e==0 ){. rc
e52d0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
e52e0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
e52f0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
e5300 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c tf(nFilename, zL
e5310 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f ockFile, "%s" DO
e5320 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46 TLOCK_SUFFIX, zF
e5330 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a ilename);. }.
e5340 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e pNew->lockin
e5350 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b gContext = zLock
e5360 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f File;. }..#if O
e5370 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65 S_VXWORKS. else
e5380 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 if( pLockingSty
e5390 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68 le == &semIoMeth
e53a0 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61 ods ){. /* Na
e53b0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
e53c0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 cking uses the f
e53d0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e ile path so it n
e53e0 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a eeds to be. *
e53f0 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 * included in th
e5400 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74 e semLockingCont
e5410 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 ext. */. u
e5420 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
e5430 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f . rc = findLo
e5440 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e ckInfo(pNew, &pN
e5450 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 ew->pLock, &pNew
e5460 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66 ->pOpen);. if
e5470 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b ( (rc==SQLITE_OK
e5480 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65 ) && (pNew->pOpe
e5490 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 n->pSem==NULL) )
e54a0 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 {. char *zS
e54b0 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 emName = pNew->p
e54c0 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a Open->aSemName;.
e54d0 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 int n;.
e54e0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
e54f0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ntf(MAX_PATHNAME
e5500 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73 , zSemName, "/%s
e5510 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 .sem",.
e5520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e pN
e5530 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 ew->pId->zCanoni
e5540 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 calName);.
e5550 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61 for( n=1; zSemNa
e5560 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 me[n]; n++ ).
e5570 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d if( zSemNam
e5580 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d e[n]=='/' ) zSem
e5590 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 Name[n] = '_';.
e55a0 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e pNew->pOpen
e55b0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 ->pSem = sem_ope
e55c0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 n(zSemName, O_CR
e55d0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 EAT, 0666, 1);.
e55e0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 if( pNew->p
e55f0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 Open->pSem == SE
e5600 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 M_FAILED ){.
e5610 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e5620 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 NOMEM;. p
e5630 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d New->pOpen->aSem
e5640 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a Name[0] = '\0';.
e5650 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
e5660 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
e5670 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ();. }.#endif.
e5680 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72 . pNew->lastEr
e5690 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f rno = 0;.#if OS_
e56a0 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
e56b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
e56c0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e unlink(zFilen
e56d0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 ame);. isDele
e56e0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e te = 0;. }. pN
e56f0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69 ew->isDelete = i
e5700 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a sDelete;.#endif.
e5710 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
e5720 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 _OK ){. if( d
e5730 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 irfd>=0 ) close(
e5740 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e dirfd); /* silen
e5750 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 t leak if fail,
e5760 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 already in error
e5770 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 */. if( h>=0
e5780 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d ) close(h);. }
e5790 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e else{. pNew->
e57a0 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69 pMethod = pLocki
e57b0 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65 ngStyle;. Ope
e57c0 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
e57d0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
e57e0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 ../*.** Open a f
e57f0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
e5800 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 o the directory
e5810 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 containing file
e5820 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 zFilename..** If
e5830 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 successful, *pF
e5840 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 d is set to the
e5850 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 opened file desc
e5860 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 riptor and.** SQ
e5870 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
e5880 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ned. If an error
e5890 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 occurs, either
e58a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 SQLITE_NOMEM.**
e58b0 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 or SQLITE_CANTOP
e58c0 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 EN is returned a
e58d0 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 nd *pFd is set t
e58e0 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a o an undefined.*
e58f0 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 * value..**.** I
e5900 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 f SQLITE_OK is r
e5910 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c eturned, the cal
e5920 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ler is responsib
e5930 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a le for closing.*
e5940 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 * the file descr
e5950 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67 iptor *pFd using
e5960 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61 close()..*/.sta
e5970 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65 tic int openDire
e5980 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72 ctory(const char
e5990 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74 *zFilename, int
e59a0 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69 *pFd){. int ii
e59b0 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b ;. int fd = -1;
e59c0 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65 . char zDirname
e59d0 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d [MAX_PATHNAME+1]
e59e0 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 ;.. sqlite3_snp
e59f0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 rintf(MAX_PATHNA
e5a00 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25 ME, zDirname, "%
e5a10 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a s", zFilename);.
e5a20 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74 for(ii=(int)st
e5a30 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 rlen(zDirname);
e5a40 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65 ii>1 && zDirname
e5a50 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 [ii]!='/'; ii--)
e5a60 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a ;. if( ii>0 ){.
e5a70 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d zDirname[ii]
e5a80 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 = '\0';. fd
e5a90 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c = open(zDirname,
e5aa0 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 O_RDONLY|O_BINA
e5ab0 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 RY, 0);. if(
e5ac0 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 fd>=0 ){.#ifdef
e5ad0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 FD_CLOEXEC.
e5ae0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 fcntl(fd, F_SET
e5af0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f FD, fcntl(fd, F_
e5b00 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 GETFD, 0) | FD_C
e5b10 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a LOEXEC);.#endif.
e5b20 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 OSTRACE3("
e5b30 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c OPENDIR %-3d %s\
e5b40 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 n", fd, zDirname
e5b50 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a );. }. }. *
e5b60 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 pFd = fd;. retu
e5b70 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 rn (fd>=0?SQLITE
e5b80 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f _OK:SQLITE_CANTO
e5b90 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 PEN);.}../*.** C
e5ba0 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 reate a temporar
e5bb0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a y file name in z
e5bc0 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 Buf. zBuf must
e5bd0 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 be allocated.**
e5be0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 by the calling p
e5bf0 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 rocess and must
e5c00 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f be big enough to
e5c10 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a hold at least.*
e5c20 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 * pVfs->mxPathna
e5c30 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 me bytes..*/.sta
e5c40 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e tic int getTempn
e5c50 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 ame(int nBuf, ch
e5c60 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 ar *zBuf){. sta
e5c70 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
e5c80 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 azDirs[] = {.
e5c90 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20 0,. 0,.
e5ca0 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20 "/var/tmp",.
e5cb0 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20 "/usr/tmp",.
e5cc0 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20 "/tmp",.
e5cd0 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 ".",. };. sta
e5ce0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
e5cf0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d ed char zChars[]
e5d00 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 =. "abcdefgh
e5d10 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 ijklmnopqrstuvwx
e5d20 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 yz". "ABCDEFG
e5d30 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 HIJKLMNOPQRSTUVW
e5d40 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 XYZ". "012345
e5d50 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 6789";. unsigne
e5d60 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 d int i, j;. st
e5d70 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 ruct stat buf;.
e5d80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 const char *zDi
e5d90 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 r = ".";.. /* I
e5da0 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c t's odd to simul
e5db0 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 ate an io-error
e5dc0 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 here, but really
e5dd0 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 this is just.
e5de0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d ** using the io-
e5df0 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 error infrastruc
e5e00 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 ture to test tha
e5e10 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 t SQLite handles
e5e20 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 this. ** funct
e5e30 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 ion failing. .
e5e40 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 */. SimulateIOE
e5e50 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
e5e60 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 ITE_IOERR );..
e5e70 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 azDirs[0] = sqli
e5e80 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
e5e90 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d ry;. if (NULL =
e5ea0 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20 = azDirs[1]) {.
e5eb0 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67 azDirs[1] = g
e5ec0 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b etenv("TMPDIR");
e5ed0 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d . }. . for(i=
e5ee0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69 0; i<sizeof(azDi
e5ef0 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72 rs)/sizeof(azDir
e5f00 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 s[0]); i++){.
e5f10 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d if( azDirs[i]==
e5f20 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
e5f30 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 72 if( stat(azDir
e5f40 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f s[i], &buf) ) co
e5f50 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 ntinue;. if(
e5f60 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f !S_ISDIR(buf.st_
e5f70 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 mode) ) continue
e5f80 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 73 ;. if( access
e5f90 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20 (azDirs[i], 07)
e5fa0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
e5fb0 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d zDir = azDirs[i]
e5fc0 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d ;. break;. }
e5fd0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
e5fe0 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 t the output buf
e5ff0 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f fer is large eno
e6000 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70 ugh for the temp
e6010 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a orary file . **
e6020 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20 name. If it is
e6030 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 not, return SQLI
e6040 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20 TE_ERROR.. */.
e6050 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69 if( (strlen(zDi
e6060 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49 r) + strlen(SQLI
e6070 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
e6080 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73 FIX) + 17) >= (s
e6090 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20 ize_t)nBuf ){.
e60a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e60b0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f ERROR;. }.. do
e60c0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
e60d0 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20 printf(nBuf-17,
e60e0 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 zBuf, "%s/"SQLIT
e60f0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
e6100 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a IX, zDir);. j
e6110 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a = (int)strlen(z
e6120 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Buf);. sqlite
e6130 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 3_randomness(15,
e6140 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 &zBuf[j]);.
e6150 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69 for(i=0; i<15; i
e6160 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ++, j++){.
e6170 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 zBuf[j] = (char)
e6180 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e zChars[ ((unsign
e6190 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 ed char)zBuf[j])
e61a0 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 %(sizeof(zChars)
e61b0 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 -1) ];. }.
e61c0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 zBuf[j] = 0;.
e61d0 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a }while( access(z
e61e0 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 Buf,0)==0 );. r
e61f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e6200 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 .}..#if SQLITE_E
e6210 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e6220 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f YLE && defined(_
e6230 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 _APPLE__)./*.**
e6240 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 Routine to trans
e6250 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 form a unixFile
e6260 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 into a proxy-loc
e6270 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a king unixFile..*
e6280 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
e6290 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f in the proxy-lo
e62a0 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 ck division, but
e62b0 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 used by unixOpe
e62c0 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 n().** if SQLITE
e62d0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f _PREFER_PROXY_LO
e62e0 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 CKING is defined
e62f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e6300 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e proxyTransformUn
e6310 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a ixFile(unixFile*
e6320 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
e6330 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 #endif../*.** Se
e6340 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 arch for an unus
e6350 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
e6360 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e or that was open
e6370 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ed on the databa
e6380 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74 se .** file (not
e6390 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61 a journal or ma
e63a0 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c ster-journal fil
e63b0 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 e) identified by
e63c0 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61 pathname.** zPa
e63d0 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f th with SQLITE_O
e63e0 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61 PEN_XXX flags ma
e63f0 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73 tching those pas
e6400 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
e6410 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f d.** argument to
e6420 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
e6430 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c **.** Such a fil
e6440 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 e descriptor may
e6450 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61 exist if a data
e6460 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
e6470 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75 was closed.** bu
e6480 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 t the associated
e6490 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e64a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c could not be cl
e64b0 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d osed because som
e64c0 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20 e.** other file
e64d0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 descriptor open
e64e0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 on the same file
e64f0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69 is holding a fi
e6500 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65 le-lock..** Refe
e6510 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e r to comments in
e6520 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29 the unixClose()
e6530 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68 function and th
e6540 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e e lengthy commen
e6550 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 t.** describing
e6560 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 "Posix Advisory
e6570 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20 Locking" at the
e6580 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69 start of this fi
e6590 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68 le for .** furth
e65a0 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f er details. Also
e65b0 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a , ticket #4018..
e65c0 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61 **.** If a suita
e65d0 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 ble file descrip
e65e0 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 tor is found, th
e65f0 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 en it is returne
e6600 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68 d. If no.** such
e6610 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e6620 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20 is located, -1
e6630 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
e6640 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65 static UnixUnuse
e6650 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c dFd *findReusabl
e6660 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a eFd(const char *
e6670 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 zPath, int flags
e6680 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 ){. UnixUnusedF
e6690 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a d *pUnused = 0;.
e66a0 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61 . /* Do not sea
e66b0 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65 rch for an unuse
e66c0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
e66d0 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f r on vxworks. No
e66e0 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76 t because. ** v
e66f0 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74 xworks would not
e6700 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68 benefit from th
e6710 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67 e change (it mig
e6720 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75 ht, we're not su
e6730 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65 re),. ** but be
e6740 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20 cause no way to
e6750 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65 test it is curre
e6760 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 ntly available.
e6770 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20 It is better .
e6780 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62 ** not to risk b
e6790 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20 reaking vxworks
e67a0 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20 support for the
e67b0 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20 sake of such an
e67c0 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65 obscure . ** fe
e67d0 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21 ature. */.#if !
e67e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
e67f0 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20 uct stat sStat;
e6800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6810 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 /* Results of
e6820 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a stat() call */..
e6830 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61 /* A stat() ca
e6840 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20 ll may fail for
e6850 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e various reasons.
e6860 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 If this happens
e6870 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d , it is. ** alm
e6880 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74 ost certain that
e6890 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 an open() call
e68a0 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68 on the same path
e68b0 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e will also fail.
e68c0 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 . ** For this r
e68d0 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72 eason, if an err
e68e0 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 or occurs in the
e68f0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72 stat() call her
e6900 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67 e, it is. ** ig
e6910 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20 nored and -1 is
e6920 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61 returned. The ca
e6930 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f ller will try to
e6940 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 open a new file
e6950 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 . ** descriptor
e6960 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 on the same pat
e6970 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74 h, fail, and ret
e6980 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 urn an error to
e6990 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a SQLite.. **. *
e69a0 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73 * Even if a subs
e69b0 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61 equent open() ca
e69c0 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c ll does succeed,
e69d0 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 the consequence
e69e0 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 s of. ** not se
e69f0 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65 arching for a re
e6a00 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73 susable file des
e6a10 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20 criptor are not
e6a20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 dire. */. if(
e6a30 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 26 0==stat(zPath, &
e6a40 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 74 sStat) ){. st
e6a50 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
e6a60 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 74 *pO;. struct
e6a70 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b 0a unixFileId id;.
e6a80 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 74 id.dev = sSt
e6a90 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 69 at.st_dev;. i
e6aa0 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 74 d.ino = sStat.st
e6ab0 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 45 _ino;.. unixE
e6ac0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
e6ad0 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 74 for(pO=openList
e6ae0 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 26 ; pO && memcmp(&
e6af0 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 2c id, &pO->fileId,
e6b00 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 4f sizeof(id)); pO
e6b10 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 20 =pO->pNext);.
e6b20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 20 if( pO ){.
e6b30 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2a UnixUnusedFd **
e6b40 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70 pp;. for(pp
e6b50 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 2a =&pO->pUnused; *
e6b60 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c 61 pp && (*pp)->fla
e6b70 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 28 gs!=flags; pp=&(
e6b80 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a (*pp)->pNext));.
e6b90 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 pUnused =
e6ba0 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 *pp;. if( p
e6bb0 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20 Unused ){.
e6bc0 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 2d *pp = pUnused-
e6bd0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
e6be0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 }. unixLe
e6bf0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
e6c00 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 20 #endif /* if
e6c10 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 20 !OS_VXWORKS */.
e6c20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 3b return pUnused;
e6c30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 .}../*.** Open t
e6c40 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a he file zPath..*
e6c50 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 * .** Previously
e6c60 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20 , the SQLite OS
e6c70 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 65 layer used three
e6c80 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c functions in pl
e6c90 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f ace of this.** o
e6ca0 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 ne:.**.** sq
e6cb0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 lite3OsOpenReadW
e6cc0 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 rite();.** s
e6cd0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
e6ce0 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 Only();.** s
e6cf0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c qlite3OsOpenExcl
e6d00 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 usive();.**.** T
e6d10 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 hese calls corre
e6d20 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c spond to the fol
e6d30 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 lowing combinati
e6d40 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a ons of flags:.**
e6d50 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 74 .** ReadWrit
e6d60 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44 e() -> (READ
e6d70 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a WRITE | CREATE).
e6d80 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 ** ReadOnly(
e6d90 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f ) -> (READO
e6da0 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 NLY) .** Ope
e6db0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 nExclusive() ->
e6dc0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 (READWRITE | CRE
e6dd0 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29 ATE | EXCLUSIVE)
e6de0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f .**.** The old O
e6df0 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61 penExclusive() a
e6e00 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 ccepted a boolea
e6e10 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 n argument - "de
e6e20 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 lFlag". If.** tr
e6e30 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73 ue, the file was
e6e40 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62 configured to b
e6e50 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 e automatically
e6e60 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 deleted when the
e6e70 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 .** file handle
e6e80 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65 closed. To achie
e6e90 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 ve the same effe
e6ea0 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65 ct using this ne
e6eb0 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c w .** interface,
e6ec0 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f add the DELETEO
e6ed0 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 NCLOSE flag to t
e6ee0 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 61 hose specified a
e6ef0 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 bove for .** Ope
e6f00 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f nExclusive()..*/
e6f10 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e6f20 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
e6f30 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
e6f40 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
e6f50 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 69 for which this i
e6f60 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 s the xOpen meth
e6f70 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 od */. const ch
e6f80 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
e6f90 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65 /* Pathname
e6fa0 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f of file to be o
e6fb0 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 pened */. sqlit
e6fc0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
e6fd0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
e6fe0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
e6ff0 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a o be filled in *
e7000 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
e7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7020 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20 /* Input flags
e7030 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f to control the o
e7040 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 pening */. int
e7050 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 *pOutFlags
e7060 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
e7070 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 ut flags returne
e7080 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65 d to SQLite core
e7090 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c */.){. unixFil
e70a0 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c 65 e *p = (unixFile
e70b0 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 20 *)pFile;. int
e70c0 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 fd = -1;
e70d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
e70e0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65 le descriptor re
e70f0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29 turned by open()
e7100 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20 */. int dirfd
e7110 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 = -1;
e7120 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 /* Director
e7130 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f y file descripto
e7140 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 r */. int openF
e7150 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 lags = 0;
e7160 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
e7170 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 o pass to open()
e7180 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20 */. int eType
e7190 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46 = flags&0xFFFFFF
e71a0 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 00; /* Type of
e71b0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
e71c0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 int noLock;
e71d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e71e0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
e71f0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 locking primiti
e7200 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ves */. int rc
e7210 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
e7220 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 /* Funct
e7230 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20 ion Return Code
e7240 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c */.. int isExcl
e7250 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20 usive = (flags
e7260 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 & SQLITE_OPEN_EX
e7270 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20 CLUSIVE);. int
e7280 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28 isDelete = (
e7290 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
e72a0 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
e72b0 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 E);. int isCrea
e72c0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 te = (flags
e72d0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 & SQLITE_OPEN_CR
e72e0 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52 EATE);. int isR
e72f0 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61 eadonly = (fla
e7300 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
e7310 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e _READONLY);. in
e7320 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d t isReadWrite =
e7330 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
e7340 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 _OPEN_READWRITE)
e7350 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 ;.. /* If creat
e7360 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 ing a master or
e7370 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 main-file journa
e7380 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e l, this function
e7390 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 will open. **
e73a0 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f a file-descripto
e73b0 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f r on the directo
e73c0 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 ry too. The firs
e73d0 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 t time unixSync(
e73e0 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ). ** is called
e73f0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 the directory f
e7400 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 ile descriptor w
e7410 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 ill be fsync()ed
e7420 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 and close()d..
e7430 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e */. int isOpen
e7440 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43 Directory = (isC
e7450 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20 reate && .
e7460 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f (eType==SQLITE_O
e7470 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e PEN_MASTER_JOURN
e7480 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c AL || eType==SQL
e7490 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
e74a0 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f URNAL). );.. /
e74b0 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 * If argument zP
e74c0 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f ath is a NULL po
e74d0 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 inter, this func
e74e0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 tion is required
e74f0 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 to open. ** a
e7500 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
e7510 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20 Use this buffer
e7520 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c to store the fil
e7530 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a e name in.. */.
e7540 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b char zTmpname[
e7550 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b MAX_PATHNAME+1];
e7560 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
e7570 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20 Name = zPath;..
e7580 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f /* Check the fo
e7590 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e llowing statemen
e75a0 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20 ts are true: .
e75b0 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78 **. ** (a) Ex
e75c0 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 actly one of the
e75d0 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52 READWRITE and R
e75e0 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75 EADONLY flags mu
e75f0 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a st be set, and .
e7600 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52 ** (b) if CR
e7610 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65 EATE is set, the
e7620 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74 n READWRITE must
e7630 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e also be set, an
e7640 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 d. ** (c) if
e7650 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 EXCLUSIVE is set
e7660 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 , then CREATE mu
e7670 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a st also be set..
e7680 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 ** (d) if DE
e7690 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 LETEONCLOSE is s
e76a0 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 et, then CREATE
e76b0 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 must also be set
e76c0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
e76d0 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c (isReadonly==0 |
e76e0 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 | isReadWrite==0
e76f0 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74 ) && (isReadWrit
e7700 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 e || isReadonly)
e7710 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 );. assert(isCr
e7720 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 eate==0 || isRea
e7730 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72 dWrite);. asser
e7740 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 t(isExclusive==0
e7750 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20 || isCreate);.
e7760 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 assert(isDelete
e7770 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 ==0 || isCreate)
e7780 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e ;.. /* The main
e7790 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 DB, main journa
e77a0 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f l, and master jo
e77b0 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20 urnal are never
e77c0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20 automatically.
e77d0 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 20 ** deleted. Nor
e77e0 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 65 are they ever te
e77f0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 20 mporary files.
e7800 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 */. assert( (!i
e7810 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 sDelete && zName
e7820 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 ) || eType!=SQLI
e7830 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
e7840 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 );. assert( (!i
e7850 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 sDelete && zName
e7860 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 ) || eType!=SQLI
e7870 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
e7880 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 RNAL );. assert
e7890 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 ( (!isDelete &&
e78a0 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 zName) || eType!
e78b0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 =SQLITE_OPEN_MAS
e78c0 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 0a TER_JOURNAL );..
e78d0 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
e78e0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
e78f0 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 has set one of
e7900 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 the "file-type"
e7910 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 flags. */. asse
e7920 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 rt( eType==SQLIT
e7930 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 E_OPEN_MAIN_DB
e7940 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 || eType==SQ
e7950 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 LITE_OPEN_TEMP_D
e7960 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 B . || eTy
e7970 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
e7980 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 MAIN_JOURNAL ||
e7990 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e79a0 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 EN_TEMP_JOURNAL
e79b0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
e79c0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 ==SQLITE_OPEN_SU
e79d0 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 BJOURNAL || eT
e79e0 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e79f0 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 _MASTER_JOURNAL
e7a00 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
e7a10 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 ==SQLITE_OPEN_TR
e7a20 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a ANSIENT_DB. );.
e7a30 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
e7a40 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 sizeof(unixFile)
e7a50 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d );.. if( eType=
e7a60 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e7a70 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 78 N_DB ){. Unix
e7a80 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 UnusedFd *pUnuse
e7a90 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d d;. pUnused =
e7aa0 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28 findReusableFd(
e7ab0 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a 20 zName, flags);.
e7ac0 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29 if( pUnused )
e7ad0 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 6e {. fd = pUn
e7ae0 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d 65 used->fd;. }e
e7af0 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 73 lse{. pUnus
e7b00 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ed = sqlite3_mal
e7b10 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75 loc(sizeof(*pUnu
e7b20 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 28 sed));. if(
e7b30 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 !pUnused ){.
e7b40 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
e7b50 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
e7b60 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 }. }. p->p
e7b70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 64 Unused = pUnused
e7b80 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a ;. }else if( !z
e7b90 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Name ){. /* I
e7ba0 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c f zName is NULL,
e7bb0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
e7bc0 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 is requesting a
e7bd0 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20 temp file. */.
e7be0 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 assert(isDele
e7bf0 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72 te && !isOpenDir
e7c00 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20 ectory);. rc
e7c10 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 = getTempname(MA
e7c20 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 X_PATHNAME+1, zT
e7c30 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 mpname);. if(
e7c40 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e7c50 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
e7c60 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 c;. }. zNa
e7c70 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 me = zTmpname;.
e7c80 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 }.. /* Determi
e7c90 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ne the value of
e7ca0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
e7cb0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 4f ter passed to PO
e7cc0 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a SIX function. *
e7cd0 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 20 * open(). These
e7ce0 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 74 must be calculat
e7cf0 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e 28 ed even if open(
e7d00 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2c ) is not called,
e7d10 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d 61 as. ** they ma
e7d20 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 70 y be stored as p
e7d30 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 art of the file
e7d40 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 20 handle and used
e7d50 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 6f by the . ** 'co
e7d60 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 6e nch file' lockin
e7d70 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 65 g functions late
e7d80 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 20 r on. */. if(
e7d90 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70 isReadonly ) op
e7da0 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f enFlags |= O_RDO
e7db0 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61 NLY;. if( isRea
e7dc0 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61 dWrite ) openFla
e7dd0 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 gs |= O_RDWR;.
e7de0 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20 if( isCreate )
e7df0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f openFlags |= O
e7e00 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73 _CREAT;. if( is
e7e10 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e Exclusive ) open
e7e20 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c Flags |= (O_EXCL
e7e30 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20 |O_NOFOLLOW);.
e7e40 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f openFlags |= (O_
e7e50 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 LARGEFILE|O_BINA
e7e60 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c 30 RY);.. if( fd<0
e7e70 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 6f ){. mode_t o
e7e80 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 6c penMode = (isDel
e7e90 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f ete?0600:SQLITE_
e7ea0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 DEFAULT_FILE_PER
e7eb0 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 66 MISSIONS);. f
e7ec0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 d = open(zName,
e7ed0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d openFlags, openM
e7ee0 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 ode);. OSTRAC
e7ef0 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64 E4("OPENX %-3d
e7f00 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20 %s 0%o\n", fd,
e7f10 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 zName, openFlags
e7f20 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 );. if( fd<0
e7f30 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52 && errno!=EISDIR
e7f40 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20 && isReadWrite
e7f50 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20 && !isExclusive
e7f60 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 6c ){. /* Fail
e7f70 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 ed to open the f
e7f80 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 69 ile for read/wri
e7f90 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 72 te access. Try r
e7fa0 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 ead-only. */.
e7fb0 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 flags &= ~(SQ
e7fc0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
e7fd0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
e7fe0 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 6f CREATE);. o
e7ff0 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f 5f penFlags &= ~(O_
e8000 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a 20 RDWR|O_CREAT);.
e8010 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 flags |= SQ
e8020 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
e8030 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c LY;. openFl
e8040 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b ags |= O_RDONLY;
e8050 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 6e . fd = open
e8060 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 (zName, openFlag
e8070 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 s, openMode);.
e8080 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c 30 }. if( fd<0
e8090 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
e80a0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
e80b0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f goto open_
e80c0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a finished;. }.
e80d0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 64 }. assert( fd
e80e0 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 75 >=0 );. if( pOu
e80f0 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 tFlags ){. *p
e8100 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 OutFlags = flags
e8110 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
e8120 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 70 pUnused ){. p
e8130 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d 20 ->pUnused->fd =
e8140 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 fd;. p->pUnus
e8150 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ed->flags = flag
e8160 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 s;. }.. if( is
e8170 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53 Delete ){.#if OS
e8180 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61 _VXWORKS. zPa
e8190 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73 th = zName;.#els
e81a0 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 e. unlink(zNa
e81b0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a me);.#endif. }.
e81c0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
e81d0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
e81e0 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f else{. p->o
e81f0 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 penFlags = openF
e8200 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 lags;. }.#endif
e8210 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 69 .. if( isOpenDi
e8220 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 72 rectory ){. r
e8230 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 c = openDirector
e8240 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29 y(zPath, &dirfd)
e8250 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
e8260 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
e8270 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 /* It is safe t
e8280 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 68 o close fd at th
e8290 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 73 is point, becaus
e82a0 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 e it is guarante
e82b0 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 ed not. **
e82c0 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 20 to be open on a
e82d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
e82e0 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 6f f it were open o
e82f0 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c n a database fil
e8300 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 77 e,. ** it w
e8310 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65 ould not be safe
e8320 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 69 to close as thi
e8330 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 20 s would release
e8340 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a 20 any locks held.
e8350 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 66 ** on the f
e8360 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f 63 ile by this proc
e8370 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 ess. */. a
e8380 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 ssert( eType!=SQ
e8390 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
e83a0 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 B );. close
e83b0 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 20 (fd);
e83c0 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 /* silently le
e83d0 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 ak if fail, alre
e83e0 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a ady in error */.
e83f0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f goto open_
e8400 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a finished;. }.
e8410 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 }..#ifdef FD_C
e8420 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 LOEXEC. fcntl(f
e8430 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 d, F_SETFD, fcnt
e8440 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 l(fd, F_GETFD, 0
e8450 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b ) | FD_CLOEXEC);
e8460 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 .#endif.. noLoc
e8470 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54 k = eType!=SQLIT
e8480 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a E_OPEN_MAIN_DB;.
e8490 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 .#if SQLITE_PREF
e84a0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 ER_PROXY_LOCKING
e84b0 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e 55 . if( zPath!=NU
e84c0 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 26 LL && !noLock &&
e84d0 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b 0a pVfs->xOpen ){.
e84e0 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72 char *envfor
e84f0 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c ce = getenv("SQL
e8500 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f ITE_FORCE_PROXY_
e8510 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69 LOCKING");. i
e8520 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b nt useProxy = 0;
e8530 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f .. /* SQLITE_
e8540 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b FORCE_PROXY_LOCK
e8550 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72 ING==1 means for
e8560 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 72 ce always use pr
e8570 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 20 oxy, 0 means .
e8580 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 70 ** never use p
e8590 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73 roxy, NULL means
e85a0 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e use proxy for n
e85b0 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f on-local files o
e85c0 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 nly. */. if(
e85d0 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20 envforce!=NULL
e85e0 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 ){. useProx
e85f0 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63 y = atoi(envforc
e8600 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b e)>0;. }else{
e8610 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74 . struct st
e8620 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 20 atfs fsInfo;.
e8630 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50 if( statfs(zP
e8640 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d ath, &fsInfo) ==
e8650 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f -1 ){. /
e8660 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 * In theory, the
e8670 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20 close(fd) call
e8680 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20 is sub-optimal.
e8690 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e If the file open
e86a0 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 ed. ** wi
e86b0 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62 th fd is a datab
e86c0 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68 ase file, and th
e86d0 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f ere are other co
e86e0 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20 nnections open.
e86f0 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61 ** on tha
e8700 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 t file that are
e8710 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e currently holdin
e8720 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 g advisory locks
e8730 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20 on it,.
e8740 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c ** then the call
e8750 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c to close() will
e8760 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f cancel those lo
e8770 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65 cks. In practice
e8780 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27 ,. ** we'
e8790 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 re assuming that
e87a0 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27 statfs() doesn'
e87b0 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65 t fail very ofte
e87c0 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20 n. At least.
e87d0 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65 ** not while
e87e0 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 other file desc
e87f0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62 riptors opened b
e8800 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 y the same proce
e8810 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a ss on. **
e8820 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61 the same file a
e8830 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a re working. */.
e8840 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45 p->lastE
e8850 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
e8860 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 3e if( dirfd>
e8870 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
e8880 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a close(dirfd); /*
e8890 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 silently leak i
e88a0 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 f fail, in error
e88b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 */. }.
e88c0 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b close(fd);
e88d0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
e88e0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 k if fail, in er
e88f0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 ror */. r
e8900 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
e8910 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 20 _ACCESS;.
e8920 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 goto open_finis
e8930 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 hed;. }.
e8940 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 28 useProxy = !(
e8950 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d fsInfo.f_flags&M
e8960 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d NT_LOCAL);. }
e8970 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f 78 . if( useProx
e8980 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
e8990 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 fillInUnixFile(p
e89a0 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 Vfs, fd, dirfd,
e89b0 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f pFile, zPath, no
e89c0 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b Lock, isDelete);
e89d0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
e89e0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e89f0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 rc = proxyTr
e8a00 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 ansformUnixFile(
e8a10 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 (unixFile*)pFile
e8a20 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 , ":auto:");.
e8a30 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
e8a40 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 open_finished;.
e8a50 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
e8a60 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e . rc = fillIn
e8a70 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 UnixFile(pVfs, f
e8a80 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c d, dirfd, pFile,
e8a90 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 zPath, noLock,
e8aa0 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e 5f isDelete);.open_
e8ab0 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 finished:. if(
e8ac0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
e8ad0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
e8ae0 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 e(p->pUnused);.
e8af0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
e8b00 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 }.../*.** Delete
e8b10 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61 the file at zPa
e8b20 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79 th. If the dirSy
e8b30 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nc argument is t
e8b40 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20 rue, fsync().**
e8b50 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66 the directory af
e8b60 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 ter deleting the
e8b70 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
e8b80 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28 int unixDelete(
e8b90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
e8ba0 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20 NotUsed, /*
e8bb0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 VFS containing t
e8bc0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65 his as the xDele
e8bd0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 te method */. c
e8be0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
e8bf0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 , /* Name
e8c00 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64 of file to be d
e8c10 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 eleted */. int
e8c20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20 dirSync
e8c30 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
e8c40 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74 , fsync() direct
e8c50 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 ory after deleti
e8c60 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 ng file */.){.
e8c70 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
e8c80 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 OK;. UNUSED_PAR
e8c90 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e8ca0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e8cb0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
e8cc0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a _IOERR_DELETE);.
e8cd0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b unlink(zPath);
e8ce0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
e8cf0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a DISABLE_DIRSYNC.
e8d00 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b if( dirSync ){
e8d10 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 . int fd;.
e8d20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 rc = openDirect
e8d30 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b ory(zPath, &fd);
e8d40 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
e8d50 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53 ITE_OK ){.#if OS
e8d60 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 _VXWORKS. i
e8d70 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 f( fsync(fd)==-1
e8d80 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 ).#else. i
e8d90 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 f( fsync(fd) ).#
e8da0 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 endif. {.
e8db0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e8dc0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
e8dd0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 C;. }.
e8de0 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26 if( close(fd)&&
e8df0 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 !rc ){. r
e8e00 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
e8e10 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 _DIR_CLOSE;.
e8e20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
e8e30 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 ndif. return rc
e8e40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 ;.}../*.** Test
e8e50 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 the existance of
e8e60 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 or access permi
e8e70 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a ssions of file z
e8e80 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 Path. The.** tes
e8e90 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 t performed depe
e8ea0 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 nds on the value
e8eb0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a of flags:.**.**
e8ec0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 SQLITE_ACCE
e8ed0 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 SS_EXISTS: Retur
e8ee0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 n 1 if the file
e8ef0 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 exists.** SQ
e8f00 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
e8f10 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 WRITE: Return 1
e8f20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 if the file is r
e8f30 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ead and writable
e8f40 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ..** SQLITE_
e8f50 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a ACCESS_READONLY:
e8f60 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
e8f70 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c file is readabl
e8f80 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
e8f90 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a se return 0..*/.
e8fa0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 static int unixA
e8fb0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
e8fc0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 _vfs *NotUsed,
e8fd0 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74 /* The VFS cont
e8fe0 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63 aining this xAcc
e8ff0 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ess method */.
e9000 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
e9010 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 h, /* Path
e9020 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65 of the file to e
e9030 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 xamine */. int
e9040 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
e9050 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77 /* What do w
e9060 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20 e want to learn
e9070 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20 about the zPath
e9080 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a file? */. int *
e9090 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 pResOut
e90a0 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 /* Write resu
e90b0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20 lt boolean here
e90c0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 */.){. int amod
e90d0 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f e = 0;. UNUSED_
e90e0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e90f0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d);. SimulateIO
e9100 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
e9110 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
e9120 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 S; );. switch(
e9130 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
e9140 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e9150 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d EXISTS:. am
e9160 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 ode = F_OK;.
e9170 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
e9180 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e9190 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 READWRITE:.
e91a0 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f amode = W_OK|R_
e91b0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
e91c0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e91d0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
e91e0 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b amode = R_OK
e91f0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
e9200 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
e9210 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
e9220 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
e9230 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
e9240 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a sOut = (access(z
e9250 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 Path, amode)==0)
e9260 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e9270 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
e9280 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 Turn a relative
e9290 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 pathname into a
e92a0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 full pathname. T
e92b0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 he relative path
e92c0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 .** is stored as
e92d0 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
e92e0 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 d string in the
e92f0 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
e9300 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a o by.** zPath. .
e9310 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 **.** zOut point
e9320 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 s to a buffer of
e9330 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 at least sqlite
e9340 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 3_vfs.mxPathname
e9350 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 bytes .** (in t
e9360 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 his case, MAX_PA
e9370 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 THNAME bytes). T
e9380 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 he full-path is
e9390 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 written to.** th
e93a0 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 is buffer before
e93b0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
e93c0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 tatic int unixFu
e93d0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
e93e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
e93f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
e9400 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
e9410 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
e9420 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
e9430 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
e9440 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
e9450 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
e9460 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 nOut,
e9470 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
e9480 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
e9490 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
e94a0 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 char *zOut
e94b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e94c0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
e94d0 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 */.){.. /* It'
e94e0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
e94f0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
e9500 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
e9510 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
e9520 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
e9530 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
e9540 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
e9550 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
e9560 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
e9570 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 n failing. This
e9580 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 function could f
e9590 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d ail if, for exam
e95a0 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 ple, the. ** cu
e95b0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 rrent working di
e95c0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e rectory has been
e95d0 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a unlinked.. */.
e95e0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
e95f0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
e9600 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 _ERROR );.. ass
e9610 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 ert( pVfs->mxPat
e9620 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e hname==MAX_PATHN
e9630 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f AME );. UNUSED_
e9640 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
e9650 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d .. zOut[nOut-1]
e9660 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a = '\0';. if( z
e9670 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a Path[0]=='/' ){.
e9680 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e9690 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c intf(nOut, zOut,
e96a0 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 "%s", zPath);.
e96b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
e96c0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65 nCwd;. if( ge
e96d0 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d tcwd(zOut, nOut-
e96e0 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 1)==0 ){. r
e96f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
e9700 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 TOPEN;. }.
e9710 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72 nCwd = (int)str
e9720 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 len(zOut);. s
e9730 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
e9740 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 nOut-nCwd, &zOut
e9750 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a [nCwd], "/%s", z
e9760 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Path);. }. ret
e9770 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
e9780 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
e9790 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
e97a0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
e97b0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
e97c0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
e97d0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
e97e0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
e97f0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
e9800 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
e9810 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
e9820 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c ibrary..*/.#incl
e9830 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74 ude <dlfcn.h>.st
e9840 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44 atic void *unixD
e9850 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
e9860 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73 s *NotUsed, cons
e9870 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
e9880 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e){. UNUSED_PAR
e9890 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e98a0 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e . return dlopen
e98b0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44 (zFilename, RTLD
e98c0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42 _NOW | RTLD_GLOB
e98d0 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 AL);.}../*.** SQ
e98e0 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 Lite calls this
e98f0 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61 function immedia
e9900 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c tely after a cal
e9910 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29 l to unixDlSym()
e9920 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65 or.** unixDlOpe
e9930 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72 n() fails (retur
e9940 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 ns a null pointe
e9950 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65 r). If a more de
e9960 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20 tailed error.**
e9970 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c message is avail
e9980 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74 able, it is writ
e9990 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20 ten to zBufOut.
e99a0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 If no error mess
e99b0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 age.** is availa
e99c0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20 ble, zBufOut is
e99d0 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20 left unmodified
e99e0 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20 and SQLite uses
e99f0 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72 a default.** err
e9a00 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 or message..*/.s
e9a10 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 tatic void unixD
e9a20 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
e9a30 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 fs *NotUsed, int
e9a40 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
e9a50 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a fOut){. char *z
e9a60 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 Err;. UNUSED_PA
e9a70 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e9a80 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 ;. unixEnterMut
e9a90 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 ex();. zErr = d
e9aa0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 lerror();. if(
e9ab0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 zErr ){. sqli
e9ac0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
e9ad0 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 f, zBufOut, "%s"
e9ae0 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 , zErr);. }. u
e9af0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
e9b00 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 .}.static void (
e9b10 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 *unixDlSym(sqlit
e9b20 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e9b30 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 void *p, const
e9b40 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 char*zSym))(void
e9b50 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 ){. /* . ** GC
e9b60 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 C with -pedantic
e9b70 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 -errors says tha
e9b80 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 t C90 does not a
e9b90 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 llow a void* to
e9ba0 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 be. ** cast int
e9bb0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 o a pointer to a
e9bc0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 function. And
e9bd0 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 yet the library
e9be0 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a dlsym() routine.
e9bf0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 ** returns a v
e9c00 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 oid* which is re
e9c10 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 ally a pointer t
e9c20 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 o a function. S
e9c30 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a o how do we. **
e9c40 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 use dlsym() wit
e9c50 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f h -pedantic-erro
e9c60 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 rs?. **. ** Va
e9c70 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 riable x below i
e9c80 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 s defined to be
e9c90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 a pointer to a f
e9ca0 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 unction taking.
e9cb0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 ** parameters v
e9cc0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 oid* and const c
e9cd0 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 har* and returni
e9ce0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ng a pointer to
e9cf0 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a a function.. **
e9d00 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 We initialize x
e9d10 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 by assigning it
e9d20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
e9d30 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 e dlsym() functi
e9d40 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 on.. ** (That a
e9d50 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 ssignment requir
e9d60 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 es a cast.) The
e9d70 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 n we call the fu
e9d80 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a nction that. **
e9d90 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a x points to. .
e9da0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 **. ** This w
e9db0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e ork-around is un
e9dc0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 likely to work c
e9dd0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 orrectly on any
e9de0 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a system where. *
e9df0 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e * you really can
e9e00 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 not cast a funct
e9e10 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f ion pointer into
e9e20 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 void*. But the
e9e30 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f n, on the. ** o
e9e40 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d ther hand, dlsym
e9e50 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b () will not work
e9e60 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 on such a syste
e9e70 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 m either, so we
e9e80 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 have. ** not re
e9e90 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 ally lost anythi
e9ea0 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 ng.. */. void
e9eb0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e (*(*x)(void*,con
e9ec0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 st char*))(void)
e9ed0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e9ee0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e9ef0 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 x = (void(*(*)(
e9f00 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
e9f10 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b *))(void))dlsym;
e9f20 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 . return (*x)(p
e9f30 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 , zSym);.}.stati
e9f40 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f c void unixDlClo
e9f50 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
e9f60 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 NotUsed, void *p
e9f70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 Handle){. UNUSE
e9f80 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e9f90 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 sed);. dlclose(
e9fa0 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 pHandle);.}.#els
e9fb0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f e /* if SQLITE_O
e9fc0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
e9fd0 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a ON is defined: *
e9fe0 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 /. #define unix
e9ff0 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 DlOpen 0. #def
ea000 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 ine unixDlError
ea010 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 0. #define unix
ea020 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
ea030 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 ine unixDlClose
ea040 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
ea050 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 Write nBuf bytes
ea060 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 of random data
ea070 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 to the supplied
ea080 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a buffer zBuf..*/.
ea090 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 static int unixR
ea0a0 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 andomness(sqlite
ea0b0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
ea0c0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
ea0d0 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f zBuf){. UNUSED_
ea0e0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
ea0f0 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 d);. assert((si
ea100 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 ze_t)nBuf>=(size
ea110 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f of(time_t)+sizeo
ea120 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 f(int)));.. /*
ea130 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 We have to initi
ea140 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 alize zBuf to pr
ea150 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 event valgrind f
ea160 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 rom reporting.
ea170 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 ** errors. The
ea180 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 reports issued b
ea190 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 y valgrind are i
ea1a0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f ncorrect - we wo
ea1b0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 uld. ** prefer
ea1c0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e that the randomn
ea1d0 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 ess be increased
ea1e0 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f by making use o
ea1f0 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 f the. ** unini
ea200 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 tialized space i
ea210 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c n zBuf - but val
ea220 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e grind errors ten
ea230 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 d to worry. **
ea240 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 some users. Rat
ea250 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 her than argue,
ea260 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 it seems easier
ea270 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 just to initiali
ea280 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c ze. ** the whol
ea290 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 e array and sile
ea2a0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 nce valgrind, ev
ea2b0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 en if that means
ea2c0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 less randomness
ea2d0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e . ** in the ran
ea2e0 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 dom seed.. **.
ea2f0 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 ** When testing
ea300 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a , initializing z
ea310 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 Buf[] to zero is
ea320 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 all we do. Tha
ea330 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 t means. ** tha
ea340 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 t we always use
ea350 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 the same random
ea360 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e number sequence.
ea370 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 This makes the
ea380 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 . ** tests repe
ea390 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d atable.. */. m
ea3a0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e emset(zBuf, 0, n
ea3b0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e Buf);.#if !defin
ea3c0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
ea3d0 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c {. int pid,
ea3e0 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 fd;. fd = op
ea3f0 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d en("/dev/urandom
ea400 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 ", O_RDONLY);.
ea410 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
ea420 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
ea430 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
ea440 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c memcpy(zBuf,
ea450 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b &t, sizeof(t));
ea460 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 . pid = get
ea470 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d pid();. mem
ea480 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 cpy(&zBuf[sizeof
ea490 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 (t)], &pid, size
ea4a0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 of(pid));.
ea4b0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 assert( sizeof(t
ea4c0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 )+sizeof(pid)<=(
ea4d0 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 size_t)nBuf );.
ea4e0 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 nBuf = size
ea4f0 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 of(t) + sizeof(p
ea500 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a id);. }else{.
ea510 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61 nBuf = rea
ea520 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 d(fd, zBuf, nBuf
ea530 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 );. close(f
ea540 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 d);. }. }.#e
ea550 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 ndif. return nB
ea560 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c uf;.}.../*.** Sl
ea570 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 eep for a little
ea580 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 while. Return
ea590 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 the amount of ti
ea5a0 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 me slept..** The
ea5b0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
ea5c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
ea5d0 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 seconds we want
ea5e0 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 to sleep..** The
ea5f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
ea600 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
ea610 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 icroseconds of s
ea620 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a leep actually.**
ea630 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
ea640 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
ea650 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c perating system,
ea660 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a a number which.
ea670 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 ** might be grea
ea680 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
ea690 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e l to the argumen
ea6a0 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a t, but not less.
ea6b0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 ** than the argu
ea6c0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ment..*/.static
ea6d0 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71 int unixSleep(sq
ea6e0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
ea6f0 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 ed, int microsec
ea700 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58 onds){.#if OS_VX
ea710 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 WORKS. struct t
ea720 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 imespec sp;.. s
ea730 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f p.tv_sec = micro
ea740 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 seconds / 100000
ea750 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 0;. sp.tv_nsec
ea760 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 = (microseconds
ea770 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30 % 1000000) * 100
ea780 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 0;. nanosleep(&
ea790 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55 sp, NULL);. UNU
ea7a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
ea7b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
ea7c0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
ea7d0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 elif defined(HAV
ea7e0 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56 E_USLEEP) && HAV
ea7f0 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65 E_USLEEP. uslee
ea800 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b p(microseconds);
ea810 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea820 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
ea830 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f return microseco
ea840 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 nds;.#else. int
ea850 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 seconds = (micr
ea860 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 oseconds+999999)
ea870 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 /1000000;. slee
ea880 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e p(seconds);. UN
ea890 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
ea8a0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
ea8b0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30 n seconds*100000
ea8c0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 0;.#endif.}../*.
ea8d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
ea8e0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 variable, if se
ea8f0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 t to a non-zero
ea900 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70 value, is interp
ea910 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 reted as.** the
ea920 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64 number of second
ea930 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64 s since 1970 and
ea940 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 is used to set
ea950 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
ea960 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
ea970 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74 tTime() during t
ea980 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 esting..*/.#ifde
ea990 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
ea9a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
ea9b0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
ea9c0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20 e = 0; /* Fake
ea9d0 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73 system time in s
ea9e0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
ea9f0 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0. */.#endif../*
eaa00 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 .** Find the cur
eaa10 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e rent time (in Un
eaa20 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 iversal Coordina
eaa30 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 ted Time). Writ
eaa40 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 e the.** current
eaa50 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 time and date a
eaa60 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e s a Julian Day n
eaa70 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f umber into *prNo
eaa80 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 w and.** return
eaa90 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 0. Return 1 if
eaaa0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 the time and dat
eaab0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e e cannot be foun
eaac0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
eaad0 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 unixCurrentTime
eaae0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
eaaf0 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70 tUsed, double *p
eab00 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e rNow){.#if defin
eab10 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ed(SQLITE_OMIT_F
eab20 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 LOATING_POINT).
eab30 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d time_t t;. tim
eab40 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 e(&t);. *prNow
eab50 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 = (((sqlite3_int
eab60 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 34 64)t)/8640 + 244
eab70 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 66 05875)/10;.#elif
eab80 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 defined(NO_GETT
eab90 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a OD). time_t t;.
eaba0 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 time(&t);. *p
eabb0 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 rNow = t/86400.0
eabc0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 + 2440587.5;.#e
eabd0 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 lif OS_VXWORKS.
eabe0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 struct timespec
eabf0 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 sNow;. clock_g
eac00 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 ettime(CLOCK_REA
eac10 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 LTIME, &sNow);.
eac20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 *prNow = 244058
eac30 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 7.5 + sNow.tv_se
eac40 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 c/86400.0 + sNow
eac50 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 .tv_nsec/8640000
eac60 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0000000.0;.#else
eac70 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 . struct timeva
eac80 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d l sNow;. gettim
eac90 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 eofday(&sNow, 0)
eaca0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 ;. *prNow = 244
eacb0 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 0587.5 + sNow.tv
eacc0 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 _sec/86400.0 + s
eacd0 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 Now.tv_usec/8640
eace0 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 0000000.0;.#endi
eacf0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
ead00 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 _TEST. if( sqli
ead10 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
ead20 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d ){. *prNow =
ead30 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
ead40 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 _time/86400.0 +
ead50 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 2440587.5;. }.#
ead60 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 endif. UNUSED_P
ead70 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
ead80 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
ead90 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 ../*.** We added
eada0 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 the xGetLastErr
eadb0 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 or() method with
eadc0 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f the intention o
eadd0 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 f providing.** b
eade0 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 etter low-level
eadf0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 error messages w
eae00 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 hen operating-sy
eae10 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f stem problems co
eae20 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 me up.** during
eae30 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e SQLite operation
eae40 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e . But so far, n
eae50 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 one of that has
eae60 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 been implemented
eae70 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e .** in the core.
eae80 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e So this routin
eae90 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 e is never calle
eaea0 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 d. For now, it
eaeb0 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 is merely.** a p
eaec0 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a lace-holder..*/.
eaed0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 static int unixG
eaee0 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 etLastError(sqli
eaef0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
eaf00 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 , int NotUsed2,
eaf10 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b char *NotUsed3){
eaf20 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
eaf30 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
eaf40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
eaf50 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e (NotUsed2);. UN
eaf60 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
eaf70 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 otUsed3);. retu
eaf80 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a rn 0;.}../*.****
eaf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafa0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 **** End of sqli
eafb0 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 te3_vfs methods
eafc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eafd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
eafe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
eb030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
eb080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb090 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 ******* Begin Pr
eb0a0 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a oxy Locking ****
eb0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
eb0d0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ** Proxy locking
eb0e0 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b is a "uber-lock
eb0f0 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 ing-method" in t
eb100 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 his sense: It u
eb110 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 ses the.** other
eb120 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 locking methods
eb130 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f on secondary lo
eb140 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 ck files. Proxy
eb150 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a locking is a.**
eb160 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 meta-layer over
eb170 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d top of the prim
eb180 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d itive locking im
eb190 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e plemented above.
eb1a0 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 For.** this re
eb1b0 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 ason, the divisi
eb1c0 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e on that implemen
eb1d0 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b ts of proxy lock
eb1e0 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a ing is deferred.
eb1f0 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e ** until late in
eb200 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 the file (here)
eb210 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 after all of th
eb220 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 e other I/O meth
eb230 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e ods have.** been
eb240 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 defined - so th
eb250 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 at the primitive
eb260 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 locking methods
eb270 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a are available.*
eb280 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f * as services to
eb290 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 help with the i
eb2a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
eb2b0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a proxy locking..
eb2c0 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 **.****.**.** Th
eb2d0 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e e default lockin
eb2e0 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c g schemes in SQL
eb2f0 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e ite use byte-ran
eb300 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a ge locks on the.
eb310 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
eb320 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 to coordinate s
eb330 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 afe, concurrent
eb340 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 access by multip
eb350 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e le readers.** an
eb360 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a d writers [http:
eb370 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 //sqlite.org/loc
eb380 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 kingv3.html]. T
eb390 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 he five file loc
eb3a0 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 king.** states (
eb3b0 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e UNLOCKED, PENDIN
eb3c0 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 G, SHARED, RESER
eb3d0 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 VED, EXCLUSIVE)
eb3e0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a are implemented.
eb3f0 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 ** as POSIX read
eb400 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f & write locks o
eb410 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 ver fixed set of
eb420 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 locations (via
eb430 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 fsctl),.** on AF
eb440 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 P and SMB only e
eb450 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 xclusive byte-ra
eb460 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 nge locks are av
eb470 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 ailable via fsct
eb480 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 l.** with _IOWR(
eb490 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 'z', 23, struct
eb4a0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
eb4b0 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 ) to track the s
eb4c0 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a ame 5 states..**
eb4d0 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 To simulate a F
eb4e0 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 _RDLCK on the sh
eb4f0 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 ared range, on A
eb500 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 FP a randomly se
eb510 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 lected.** addres
eb520 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 s in the shared
eb530 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 range is taken f
eb540 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b or a SHARED lock
eb550 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 , the entire.**
eb560 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 shared range is
eb570 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 taken for an EXC
eb580 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a LUSIVE lock):.**
eb590 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 .** PENDING
eb5a0 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34 _BYTE 0x4
eb5b0 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 0000000.. ..**
eb5c0 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 RESERVED_B
eb5d0 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30 YTE 0x4000
eb5e0 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 0001.** SHA
eb5f0 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 RED_RANGE
eb600 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 0x40000002 -> 0
eb610 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 x40000200.**.**
eb620 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 This works well
eb630 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c on the local fil
eb640 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 e system, but sh
eb650 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 ows a nearly 100
eb660 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e x.** slowdown in
eb670 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 read performanc
eb680 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 e on AFP because
eb690 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 the AFP client
eb6a0 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 disables.** the
eb6b0 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 read cache when
eb6c0 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 byte-range locks
eb6d0 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 are present. E
eb6e0 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 nabling the read
eb6f0 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 .** cache expose
eb700 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 s a cache cohere
eb710 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 ncy problem that
eb720 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 is present on a
eb730 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f ll OS X.** suppo
eb740 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c rted network fil
eb750 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 e systems. NFS
eb760 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 and AFP both obs
eb770 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 erve the.** clos
eb780 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 e-to-open semant
eb790 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 ics for ensuring
eb7a0 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 cache coherency
eb7b0 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e .** [http://nfs.
eb7c0 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f sourceforge.net/
eb7d0 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 #faq_a8], which
eb7e0 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 does not effecti
eb7f0 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 vely.** address
eb800 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 the requirements
eb810 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 for concurrent
eb820 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 database access
eb830 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 by multiple.** r
eb840 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 eaders and write
eb850 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 rs.** [http://ww
eb860 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c w.nabble.com/SQL
eb870 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 ite-on-NFS-cache
eb880 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 -coherency-td156
eb890 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 55701.html]..**.
eb8a0 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 ** To address th
eb8b0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e e performance an
eb8c0 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 d cache coherenc
eb8d0 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 y issues, proxy
eb8e0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 file locking.**
eb8f0 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 changes the way
eb900 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 database access
eb910 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 is controlled by
eb920 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 limiting access
eb930 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 to a.** single
eb940 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 host at a time a
eb950 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c nd moving file l
eb960 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 ocks off of the
eb970 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
eb980 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 and onto a prox
eb990 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f y file on the lo
eb9a0 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e cal file system.
eb9b0 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e .**.**.** Usin
eb9c0 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a g proxy locks.**
eb9d0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
eb9e0 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a --.**.** C APIs.
eb9f0 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 **.** sqlite3_f
eba00 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 ile_control(db,
eba10 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 dbname, SQLITE_S
eba20 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
eba30 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ,.**
eba40 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 <prox
eba50 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f y_path> | ":auto
eba60 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 :");.** sqlite3
eba70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 _file_control(db
eba80 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 , dbname, SQLITE
eba90 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
ebaa0 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 LE, &<proxy_path
ebab0 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c >);.**.**.** SQL
ebac0 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 pragmas.**.**
ebad0 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 PRAGMA [database
ebae0 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c .]lock_proxy_fil
ebaf0 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c e=<proxy_path> |
ebb00 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 :auto:.** PRAG
ebb10 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f MA [database.]lo
ebb20 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a ck_proxy_file.**
ebb30 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 .** Specifying "
ebb40 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 :auto:" means th
ebb50 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 at if there is a
ebb60 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 conch file with
ebb70 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 a matching.** h
ebb80 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 ost ID in it, th
ebb90 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 e proxy path in
ebba0 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 the conch file w
ebbb0 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 ill be used, oth
ebbc0 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 erwise.** a prox
ebbd0 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 y path based on
ebbe0 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 the user's temp
ebbf0 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 dir.** (via conf
ebc00 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 str(_CS_DARWIN_U
ebc10 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e SER_TEMP_DIR,...
ebc20 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 )) will be used
ebc30 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 and the.** actua
ebc40 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d l proxy file nam
ebc50 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 e is generated f
ebc60 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 rom the name and
ebc70 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 path of the.**
ebc80 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
ebc90 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a For example:.**.
ebca0 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 ** For dat
ebcb0 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 abase path "/Use
ebcc0 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a rs/me/foo.db" .*
ebcd0 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b * The lock
ebce0 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c path will be "<
ebcf0 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c tmpdir>/sqlitepl
ebd00 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 ocks/_Users_me_f
ebd10 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a oo.db:auto:").**
ebd20 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 .** Once a lock
ebd30 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 proxy is configu
ebd40 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 red for a databa
ebd50 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 se connection, i
ebd60 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 t can not.** be
ebd70 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 removed, however
ebd80 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 it may be switc
ebd90 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 hed to a differe
ebda0 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 nt proxy path vi
ebdb0 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 a.** the above A
ebdc0 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 PIs (assuming th
ebdd0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 e conch file is
ebde0 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 not being held b
ebdf0 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e y another.** con
ebe00 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 nection or proce
ebe10 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 ss). .**.**.** H
ebe20 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ow proxy locking
ebe30 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d works.** ------
ebe40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
ebe50 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 -.**.** Proxy fi
ebe60 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 le locking relie
ebe70 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 s primarily on t
ebe80 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e wo new supportin
ebe90 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 g files: .**.**
ebea0 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 * conch file
ebeb0 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 to limit access
ebec0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
ebed0 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 file to a single
ebee0 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 host.** at
ebef0 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 a time.**.**
ebf00 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f * proxy file to
ebf10 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 act as a proxy
ebf20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 for the advisory
ebf30 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a locks normally.
ebf40 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e ** taken on
ebf50 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
ebf60 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 .** The conch fi
ebf70 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 le - to use a pr
ebf80 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 oxy file, sqlite
ebf90 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c must first "hol
ebfa0 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 d the conch".**
ebfb0 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c by taking an sql
ebfc0 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 ite-style shared
ebfd0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e lock on the con
ebfe0 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 ch file, reading
ebff0 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 the.** contents
ec000 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 and comparing t
ec010 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 he host's unique
ec020 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 host ID (see be
ec030 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a low) and lock.**
ec040 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 proxy path agai
ec050 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 nst the values s
ec060 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e tored in the con
ec070 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 ch. The conch f
ec080 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 ile is.** stored
ec090 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 in the same dir
ec0a0 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 ectory as the da
ec0b0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
ec0c0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a the file name.**
ec0d0 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 is patterned af
ec0e0 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 ter the database
ec0f0 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e file name as ".
ec100 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 <databasename>-c
ec110 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 onch"..** If the
ec120 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 conch file does
ec130 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 not exist, or i
ec140 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 t's contents do
ec150 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a not match the.**
ec160 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 host ID and/or
ec170 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e proxy path, then
ec180 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 the lock is esc
ec190 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 alated to an exc
ec1a0 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 lusive.** lock a
ec1b0 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c nd the conch fil
ec1c0 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 e contents is up
ec1d0 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68 dated with the h
ec1e0 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 ost ID and proxy
ec1f0 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 .** path and the
ec200 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 lock is downgra
ec210 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 ded to a shared
ec220 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 lock again. If
ec230 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 the conch.** is
ec240 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 held by another
ec250 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 process (with a
ec260 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 shared lock), th
ec270 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
ec280 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e .** will fail an
ec290 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 d SQLITE_BUSY is
ec2a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
ec2b0 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 The proxy file
ec2c0 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 - a single-byte
ec2d0 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c file used for al
ec2e0 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 l advisory file
ec2f0 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c locks.** normall
ec300 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 y taken on the d
ec310 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 atabase file.
ec320 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 This allows for
ec330 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 safe sharing.**
ec340 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
ec350 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c file for multipl
ec360 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 e readers and wr
ec370 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d iters on the sam
ec380 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 e.** host (the c
ec390 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 onch ensures tha
ec3a0 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 t they all use t
ec3b0 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f he same local lo
ec3c0 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 ck file)..**.**
ec3d0 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64 There is a third
ec3e0 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 file - the host
ec3f0 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 ID file - used
ec400 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 as a persistent
ec410 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 record.** of a u
ec420 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 nique identifier
ec430 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 for the host, a
ec440 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 128-byte unique
ec450 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a host id file.**
ec460 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 in the path def
ec470 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 ined by the HOST
ec480 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 IDPATH macro (de
ec490 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a fault value is.*
ec4a0 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 * /Library/Cache
ec4b0 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c s/.com.apple.sql
ec4c0 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e iteConchHostId).
ec4d0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e .**.** Requestin
ec4e0 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 g the lock proxy
ec4f0 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 does not immedi
ec500 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 ately take the c
ec510 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f onch, it is.** o
ec520 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 nly taken when t
ec530 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74 he first request
ec540 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 to lock databas
ec550 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 e file is made.
ec560 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 .** This matche
ec570 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 s the semantics
ec580 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e of the tradition
ec590 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 al locking behav
ec5a0 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 ior, where.** op
ec5b0 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 ening a connecti
ec5c0 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 on to a database
ec5d0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 file does not t
ec5e0 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 ake a lock on it
ec5f0 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 ..** The shared
ec600 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e lock and an open
ec610 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
ec620 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 are maintained
ec630 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f until .** the co
ec640 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 nnection to the
ec650 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 database is clos
ec660 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ed. .**.** The p
ec670 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 roxy file and th
ec680 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 e lock file are
ec690 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f never deleted so
ec6a0 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a they only need.
ec6b0 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 ** to be created
ec6c0 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
ec6d0 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a they are used..*
ec6e0 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 *.** Configurati
ec6f0 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d on options.** --
ec700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
ec710 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 ---.**.** SQLIT
ec720 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c E_PREFER_PROXY_L
ec730 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 OCKING.**.**
ec740 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
ec750 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f s accessed on no
ec760 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 n-local file sys
ec770 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 tems are.**
ec780 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 automatically
ec790 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 configured for p
ec7a0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f roxy locking, lo
ec7b0 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 ck files are.**
ec7c0 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f named auto
ec7d0 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 matically using
ec7e0 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 the same logic a
ec7f0 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d s.** PRAGM
ec800 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c A lock_proxy_fil
ec810 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 e=":auto:".**
ec820 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f .** SQLITE_PRO
ec830 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 XY_DEBUG.**.**
ec840 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 Enables the
ec850 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f logging of erro
ec860 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e r messages durin
ec870 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a g host id file.*
ec880 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61 * retrieva
ec890 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a l and creation.*
ec8a0 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 *.** HOSTIDPATH
ec8b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 .**.** Ove
ec8c0 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 rrides the defau
ec8d0 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 lt host ID file
ec8e0 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a path location.**
ec8f0 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 .** LOCKPROXYDI
ec900 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 R.**.** Ov
ec910 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 errides the defa
ec920 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 ult directory us
ec930 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 ed for lock prox
ec940 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 y files that.**
ec950 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 are named
ec960 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 automatically vi
ec970 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 a the ":auto:" s
ec980 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 etting.**.** SQ
ec990 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f LITE_DEFAULT_PRO
ec9a0 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e XYDIR_PERMISSION
ec9b0 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 S.**.** Pe
ec9c0 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 rmissions to use
ec9d0 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 when creating a
ec9e0 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 directory for s
ec9f0 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 toring the.**
eca00 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 lock proxy f
eca10 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 iles, only used
eca20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 when LOCKPROXYDI
eca30 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a R is not set..**
eca40 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 .** .** A
eca50 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 s mentioned abov
eca60 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 e, when compiled
eca70 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 with SQLITE_PRE
eca80 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e FER_PROXY_LOCKIN
eca90 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 G,.** setting th
ecaa0 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 e environment va
ecab0 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f riable SQLITE_FO
ecac0 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e RCE_PROXY_LOCKIN
ecad0 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 G to 1 will.** f
ecae0 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 orce proxy locki
ecaf0 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f ng to be used fo
ecb00 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 r every database
ecb10 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e file opened, an
ecb20 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 d 0.** will forc
ecb30 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 e automatic prox
ecb40 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 y locking to be
ecb50 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c disabled for all
ecb60 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
ecb70 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 es (explicity ca
ecb80 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 lling the SQLITE
ecb90 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
ecba0 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 LE pragma or.**
ecbb0 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 sqlite_file_cont
ecbc0 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 rol API is not a
ecbd0 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 ffected by SQLIT
ecbe0 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f E_FORCE_PROXY_LO
ecbf0 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a CKING)..*/../*.*
ecc00 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 * Proxy locking
ecc10 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
ecc20 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a e on MacOSX .*/.
ecc30 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
ecc40 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
ecc50 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
ecc60 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 STYLE..#ifdef SQ
ecc70 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d LITE_TEST./* sim
ecc80 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 ulate multiple h
ecc90 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 osts by creating
ecca0 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 unique hostid f
eccb0 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c ile paths */.SQL
eccc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
eccd0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d te3_hostid_num =
ecce0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
eccf0 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 * The proxyLocki
ecd00 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 ngContext has th
ecd10 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 e path and file
ecd20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 structures for t
ecd30 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e he remote .** an
ecd40 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 d local proxy fi
ecd50 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 les in it.*/.typ
ecd60 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 edef struct prox
ecd70 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ecd80 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ecd90 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 ext;.struct prox
ecda0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ecdb0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f {. unixFile *co
ecdc0 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 nchFile;
ecdd0 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 /* Open conch f
ecde0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 ile */. char *c
ecdf0 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 onchFilePath;
ece00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
ece10 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ece20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c */. unixFile *l
ece30 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 ockProxy;
ece40 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 /* Open proxy
ece50 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 lock file */. c
ece60 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 har *lockProxyPa
ece70 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e th; /* N
ece80 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 ame of the proxy
ece90 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 lock file */.
ecea0 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 char *dbPath;
eceb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ecec0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e Name of the open
eced0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 file */. int c
ecee0 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 onchHeld;
ecef0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
ecf00 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 if the conch is
ecf10 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a currently held *
ecf20 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 /. void *oldLoc
ecf30 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 kingContext;
ecf40 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 /* Original loc
ecf50 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 kingcontext to r
ecf60 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 estore on close
ecf70 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f */. sqlite3_io_
ecf80 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 methods const *p
ecf90 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f OldMethod; /
ecfa0 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d * Original I/O m
ecfb0 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 ethods for close
ecfc0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 */.};../* HOSTI
ecfd0 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 DLEN and CONCHLE
ecfe0 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 N both include s
ecff0 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 pace for the str
ed000 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 ing .** terminat
ed010 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 ing nul .*/.#def
ed020 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 ine HOSTIDLEN
ed030 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 128.#defin
ed040 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 e CONCHLEN
ed050 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b (MAXPATHLEN+
ed060 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 HOSTIDLEN+1).#if
ed070 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a ndef HOSTIDPATH.
ed080 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 # define HOSTIDP
ed090 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 ATH "/Libr
ed0a0 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e ary/Caches/.com.
ed0b0 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 apple.sqliteConc
ed0c0 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a hHostId".#endif.
ed0d0 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 ./* basically a
ed0e0 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 copy of unixRand
ed0f0 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 omness with diff
ed100 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 erent.** test be
ed110 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 havior built in
ed120 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
ed130 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 oxyGenerateHostI
ed140 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 D(char *pHostID)
ed150 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c {. int pid, fd,
ed160 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 len;. unsigned
ed170 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e char *key = (un
ed180 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 signed char *)pH
ed190 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 ostID;. . mems
ed1a0 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 et(key, 0, HOSTI
ed1b0 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 DLEN);. len = 0
ed1c0 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f ;. fd = open("/
ed1d0 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f dev/urandom", O_
ed1e0 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 RDONLY);. if( f
ed1f0 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 d>=0 ){. len
ed200 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 = read(fd, key,
ed210 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 HOSTIDLEN);.
ed220 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
ed230 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 lently leak the
ed240 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a fd if it fails *
ed250 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 /. }. if( len
ed260 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 < HOSTIDLEN ){.
ed270 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 time_t t;.
ed280 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d time(&t);. m
ed290 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 emcpy(key, &t, s
ed2a0 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 izeof(t));. p
ed2b0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 id = getpid();.
ed2c0 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 memcpy(&key[s
ed2d0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c izeof(t)], &pid,
ed2e0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 sizeof(pid));.
ed2f0 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b }. .#ifdef MAK
ed300 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a E_PRETTY_HOSTID.
ed310 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
ed320 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 /* filter the
ed330 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e bytes into prin
ed340 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 table ascii char
ed350 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 acters and NUL t
ed360 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 erminate */.
ed370 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 key[(HOSTIDLEN-1
ed380 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 )] = 0x00;. f
ed390 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 or( i=0; i<(HOST
ed3a0 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b IDLEN-1); i++ ){
ed3b0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
ed3c0 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d char pa = key[i]
ed3d0 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 &0x7F;. if(
ed3e0 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 pa<0x20 ){.
ed3f0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 key[i] = (ke
ed400 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 y[i]&0x80 == 0x8
ed410 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 0) ? pa+0x40 : p
ed420 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 a+0x20;. }e
ed430 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 lse if( pa==0x7F
ed440 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b ){. key[
ed450 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 i] = (key[i]&0x8
ed460 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 0 == 0x80) ? pa=
ed470 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 0x20 : pa+0x7E;.
ed480 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
ed490 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 }.#endif. retur
ed4a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
ed4b0 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f /* writes the ho
ed4c0 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 st id path to pa
ed4d0 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 th, path should
ed4e0 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 be an pre-alloca
ed4f0 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 ted buffer.** wi
ed500 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 th enough space
ed510 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 for a path .*/.s
ed520 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 tatic void proxy
ed530 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 GetHostIDPath(ch
ed540 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 ar *path, size_t
ed550 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 len){. strlcpy
ed560 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 (path, HOSTIDPAT
ed570 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 H, len);.#ifdef
ed580 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 SQLITE_TEST. if
ed590 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 ( sqlite3_hostid
ed5a0 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 _num>0 ){. ch
ed5b0 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 ar suffix[2] = "
ed5c0 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 1";. suffix[0
ed5d0 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 ] = suffix[0] +
ed5e0 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e sqlite3_hostid_n
ed5f0 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 um;. strlcat(
ed600 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 path, suffix, le
ed610 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 n);. }.#endif.
ed620 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f OSTRACE3("GETHO
ed630 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64 STIDPATH %s pid
ed640 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 =%d\n", path, ge
ed650 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 tpid());.}../* g
ed660 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 et the host ID f
ed670 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 rom a sqlite hos
ed680 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 tid file stored
ed690 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d in the .** user-
ed6a0 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 specific tmp dir
ed6b0 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 ectory, create t
ed6c0 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f he ID if it's no
ed6d0 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 t there already
ed6e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
ed6f0 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 roxyGetHostID(ch
ed700 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 ar *pHostID, int
ed710 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 *pError){. int
ed720 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 fd;. char path
ed730 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 [MAXPATHLEN]; .
ed740 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 size_t len;. i
ed750 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b nt rc=SQLITE_OK;
ed760 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 .. proxyGetHost
ed770 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 IDPath(path, MAX
ed780 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 PATHLEN);. /* t
ed790 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 ry to create the
ed7a0 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 host ID file, i
ed7b0 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 f it already exi
ed7c0 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e sts read the con
ed7d0 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 tents */. fd =
ed7e0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 open(path, O_CRE
ed7f0 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 AT|O_WRONLY|O_EX
ed800 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 CL, 0644);. if(
ed810 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 fd<0 ){. int
ed820 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 err=errno;....
ed830 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 if( err!=EEXI
ed840 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c ST ){.#ifdef SQL
ed850 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 ITE_PROXY_DEBUG
ed860 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 /* set the sqlit
ed870 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
ed880 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 instead */.
ed890 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
ed8a0 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 "sqlite error c
ed8b0 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 reating host ID
ed8c0 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a file %s: %s\n",.
ed8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
ed8e0 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 th, strerror(err
ed8f0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ));.#endif.
ed900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
ed910 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f ERM;. }. /
ed920 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 * couldn't creat
ed930 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 e the file, read
ed940 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 it instead */.
ed950 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 fd = open(pat
ed960 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 h, O_RDONLY|O_EX
ed970 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c CL);. if( fd<
ed980 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 0 ){.#ifdef SQLI
ed990 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f TE_PROXY_DEBUG /
ed9a0 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 * set the sqlite
ed9b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
ed9c0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 nstead */.
ed9d0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b int err = errno;
ed9e0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 . fprintf(s
ed9f0 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 tderr, "sqlite e
eda00 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 rror opening hos
eda10 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 t ID file %s: %s
eda20 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
eda30 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f path, strerro
eda40 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a r(err));.#endif.
eda50 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
eda60 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a ITE_PERM;. }.
eda70 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 len = pread(
eda80 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 fd, pHostID, HOS
eda90 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 TIDLEN, 0);.
edaa0 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 if( len<0 ){.
edab0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 *pError = err
edac0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 no;. rc = S
edad0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
edae0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
edaf0 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b len<HOSTIDLEN ){
edb00 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d . *pError =
edb10 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 0;. rc = S
edb20 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
edb30 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 T_READ;. }.
edb40 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 close(fd); /*
edb50 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 silently leak th
edb60 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 e fd if it fails
edb70 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 */. OSTRACE3
edb80 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 ("GETHOSTID rea
edb90 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 d %s pid=%d\n",
edba0 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 pHostID, getpid(
edbb0 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ));. return r
edbc0 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 c;. }else{.
edbd0 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e /* we're creatin
edbe0 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 g the host ID fi
edbf0 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d le (use a random
edc00 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 string of bytes
edc10 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 ) */. proxyGe
edc20 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f nerateHostID(pHo
edc30 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d stID);. len =
edc40 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 pwrite(fd, pHos
edc50 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 tID, HOSTIDLEN,
edc60 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 0);. if( len<
edc70 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 0 ){. *pErr
edc80 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 or = errno;.
edc90 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
edca0 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d ERR_WRITE;. }
edcb0 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 else if( len<HOS
edcc0 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 TIDLEN ){.
edcd0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 *pError = 0;.
edce0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
edcf0 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
edd00 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b }. close(fd);
edd10 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
edd20 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 k the fd if it f
edd30 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 ails */. OSTR
edd40 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 ACE3("GETHOSTID
edd50 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 wrote %s pid=%d
edd60 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 \n", pHostID, ge
edd70 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 tpid());. ret
edd80 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 urn rc;. }.}..s
edd90 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 tatic int proxyG
edda0 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 etLockPath(const
eddb0 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 char *dbPath, c
eddc0 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 har *lPath, size
eddd0 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e _t maxLen){. in
edde0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c t len;. int dbL
eddf0 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 en;. int i;..#i
ede00 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 fdef LOCKPROXYDI
ede10 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 R. len = strlcp
ede20 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f y(lPath, LOCKPRO
ede30 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a XYDIR, maxLen);.
ede40 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 #else.# ifdef _C
ede50 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 S_DARWIN_USER_TE
ede60 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 MP_DIR. {. c
ede70 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 onfstr(_CS_DARWI
ede80 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c N_USER_TEMP_DIR,
ede90 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b lPath, maxLen);
edea0 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 . len = strlc
edeb0 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 at(lPath, "sqlit
edec0 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e eplocks", maxLen
eded0 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 );. if( mkdir
edee0 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 (lPath, SQLITE_D
edef0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
edf00 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a PERMISSIONS) ){.
edf10 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 /* if mkdi
edf20 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 r fails, handle
edf30 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 as lock file cre
edf40 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f ation failure */
edf50 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 .# ifdef SQLITE
edf60 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e 74 _DEBUG. int
edf70 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 err = errno;.
edf80 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 if( err!=EEX
edf90 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 IST ){. f
edfa0 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
edfb0 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 proxyGetLockPath
edfc0 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 : mkdir(%s,0%o)
edfd0 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 error %d %s\n",
edfe0 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 lPath,.
edff0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 SQLITE_DE
ee000 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 FAULT_PROXYDIR_P
ee010 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c ERMISSIONS, err,
ee020 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b strerror(err));
ee030 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 . }.# endi
ee040 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 f. }else{.
ee050 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 OSTRACE3("GET
ee060 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 LOCKPATH mkdir
ee070 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 %s pid=%d\n", lP
ee080 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a ath, getpid());.
ee090 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 }. . }.#
ee0a0 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 else. len = st
ee0b0 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 rlcpy(lPath, "/t
ee0c0 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 mp/", maxLen);.#
ee0d0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 endif.#endif..
ee0e0 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 if( lPath[len-1
ee0f0 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 ]!='/' ){. le
ee100 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 n = strlcat(lPat
ee110 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b h, "/", maxLen);
ee120 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 . }. . /* tra
ee130 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 nsform the db pa
ee140 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 th to a unique c
ee150 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 ache name */. d
ee160 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c bLen = (int)strl
ee170 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f en(dbPath);. fo
ee180 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 r( i=0; i<dbLen
ee190 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 && (i+len+7)<max
ee1a0 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 Len; i++){. c
ee1b0 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 har c = dbPath[i
ee1c0 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c ];. lPath[i+l
ee1d0 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 en] = (c=='/')?'
ee1e0 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 _':c;. }. lPat
ee1f0 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 h[i+len]='\0';.
ee200 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 strlcat(lPath,
ee210 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e ":auto:", maxLen
ee220 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
ee230 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
ee240 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 Create a new VFS
ee250 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
ee260 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f (stored in memo
ee270 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
ee280 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c .** sqlite3_mall
ee290 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 oc) and open the
ee2a0 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 file named "pat
ee2b0 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 h" in the file d
ee2c0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a escriptor..**.**
ee2d0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 The caller is r
ee2e0 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f esponsible not o
ee2f0 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 nly for closing
ee300 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
ee310 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 tor.** but also
ee320 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 for freeing the
ee330 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
ee340 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 d with the file
ee350 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 descriptor..*/.s
ee360 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 tatic int proxyC
ee370 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f reateUnixFile(co
ee380 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 nst char *path,
ee390 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c unixFile **ppFil
ee3a0 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
ee3b0 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c 61 *pNew;. int fla
ee3c0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs = SQLITE_OPEN
ee3d0 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 5f _MAIN_DB|SQLITE_
ee3e0 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 OPEN_CREATE|SQLI
ee3f0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
ee400 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 E;. int rc = SQ
ee410 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 LITE_OK;. sqlit
ee420 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b e3_vfs dummyVfs;
ee430 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 78 .. pNew = (unix
ee440 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d File *)sqlite3_m
ee450 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e 69 alloc(sizeof(uni
ee460 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 xFile));. if( !
ee470 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75 pNew ){. retu
ee480 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
ee490 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e . }. memset(pN
ee4a0 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e ew, 0, sizeof(un
ee4b0 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a 20 ixFile));.. /*
ee4c0 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 20 Call unixOpen()
ee4d0 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f 78 to open the prox
ee4e0 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 67 y file. The flag
ee4f0 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 78 s passed to unix
ee500 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 67 Open(). ** sugg
ee510 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 6c est that the fil
ee520 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 69 e being opened i
ee530 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 61 s a "main databa
ee540 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 2a se". This is. *
ee550 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 6f * necessary as o
ee560 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 20 ther file types
ee570 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 do not necessari
ee580 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b 69 ly support locki
ee590 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 62 ng. It. ** is b
ee5a0 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e 69 etter to use uni
ee5b0 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 20 xOpen() instead
ee5c0 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 of opening the f
ee5d0 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 74 ile directly wit
ee5e0 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 61 h. ** open(), a
ee5f0 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 74 s unixOpen() set
ee600 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 73 s up the various
ee610 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 75 mechanisms requ
ee620 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 6b ired to. ** mak
ee630 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 6f e sure a call to
ee640 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e 6f close() does no
ee650 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 74 t cause the syst
ee660 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 20 em to discard.
ee670 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 70 ** POSIX locks p
ee680 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a 2a rematurely.. **
ee690 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f . ** It is impo
ee6a0 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 78 rtant that the x
ee6b0 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 74 Open member of t
ee6c0 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 61 he VFS object pa
ee6d0 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 6e ssed to . ** un
ee6e0 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c 4c ixOpen() is NULL
ee6f0 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e 69 . This tells uni
ee700 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 20 xOpen() may try
ee710 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 2d to open a proxy-
ee720 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 74 file . ** for t
ee730 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 63 he proxy-file (c
ee740 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e 74 reating a potent
ee750 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f ial infinite loo
ee760 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d 79 p).. */. dummy
ee770 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 28 Vfs.pAppData = (
ee780 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b 49 void*)&autolockI
ee790 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d 79 oFinder;. dummy
ee7a0 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a 20 Vfs.xOpen = 0;.
ee7b0 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 26 rc = unixOpen(&
ee7c0 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c 20 dummyVfs, path,
ee7d0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
ee7e0 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 6c pNew, flags, &fl
ee7f0 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ags);. if( rc==
ee800 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 6c SQLITE_OK && (fl
ee810 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ags&SQLITE_OPEN_
ee820 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 20 READONLY) ){.
ee830 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d 3e pNew->pMethod->
ee840 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
ee850 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 20 file *)pNew);.
ee860 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 rc = SQLITE_CA
ee870 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 69 NTOPEN;. }.. i
ee880 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
ee890 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
ee8a0 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 free(pNew);.
ee8b0 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 pNew = 0;. }..
ee8c0 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 3b *ppFile = pNew;
ee8d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
ee8e0 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 6f ./* takes the co
ee8f0 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 20 nch by taking a
ee900 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 shared lock and
ee910 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 read the content
ee920 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20 s conch, if .**
ee930 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d lockPath is non-
ee940 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 49 NULL, the host I
ee950 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20 D and lock file
ee960 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 2e path must match.
ee970 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63 A NULL .** loc
ee980 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 74 kPath means that
ee990 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e the lockPath in
ee9a0 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ee9b0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 20 will be used if
ee9c0 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73 the .** host IDs
ee9d0 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 77 match, or a new
ee9e0 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 lock path will
ee9f0 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 74 be generated aut
eea00 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61 omatically .** a
eea10 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 nd written to th
eea20 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f e conch file..*/
eea30 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
eea40 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46 yTakeConch(unixF
eea50 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70 ile *pFile){. p
eea60 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
eea70 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
eea80 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
eea90 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
eeaa0 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 69 Context; . . i
eeab0 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 f( pCtx->conchHe
eeac0 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 ld>0 ){. retu
eead0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
eeae0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 }else{. unixF
eeaf0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d ile *conchFile =
eeb00 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 pCtx->conchFile
eeb10 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 56 ;. char testV
eeb20 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a alue[CONCHLEN];.
eeb30 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 61 char conchVa
eeb40 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 lue[CONCHLEN];.
eeb50 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 68 char lockPath
eeb60 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 [MAXPATHLEN];.
eeb70 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74 char *tLockPat
eeb80 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e h = NULL;. in
eeb90 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
eeba0 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 63 ;. int readRc
eebb0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
eebc0 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 20 int syncPerms
eebd0 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43 = 0;.. OSTRAC
eebe0 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 E4("TAKECONCH %
eebf0 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c d for %s pid=%d\
eec00 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 n", conchFile->h
eec10 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 ,. (
eec20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
eec30 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b ath ? pCtx->lock
eec40 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75 ProxyPath : ":au
eec50 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 to:"), getpid())
eec60 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 ;.. rc = conc
eec70 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e hFile->pMethod->
eec80 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 xLock((sqlite3_f
eec90 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 ile*)conchFile,
eeca0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
eecb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
eecc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 _OK ){. int
eecd0 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 pError = 0;.
eece0 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 61 memset(testVa
eecf0 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e lue, 0, CONCHLEN
eed00 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66 ); /* conch is f
eed10 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20 ixed size */.
eed20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 74 rc = proxyGet
eed30 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 65 HostID(testValue
eed40 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 20 , &pError);.
eed50 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d if( (rc&0xff)=
eed60 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b =SQLITE_IOERR ){
eed70 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
eed80 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 72 lastErrno = pErr
eed90 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 or;. }.
eeda0 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b if( pCtx->lock
eedb0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 ProxyPath ){.
eedc0 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65 strlcpy(&te
eedd0 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 stValue[HOSTIDLE
eede0 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 N], pCtx->lockPr
eedf0 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 oxyPath, MAXPATH
eee00 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 LEN);. }.
eee10 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
eee20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
eee30 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 goto end_take
eee40 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 conch;. }.
eee50 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 75 . readRc = u
eee60 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 33 nixRead((sqlite3
eee70 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c _file *)conchFil
eee80 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 e, conchValue, C
eee90 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 ONCHLEN, 0);.
eeea0 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c if( readRc!=SQL
eeeb0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
eeec0 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 66 READ ){. if
eeed0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45 ( readRc!=SQLITE
eeee0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 _OK ){. i
eeef0 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 f( (rc&0xff)==SQ
eef00 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
eef10 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
eef20 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68 astErrno = conch
eef30 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b File->lastErrno;
eef40 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
eef50 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b 0a rc = readRc;.
eef60 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
eef70 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 _takeconch;.
eef80 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 }. /* if
eef90 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 61 the conch has da
eefa0 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63 ta compare the c
eefb0 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 ontents */.
eefc0 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b if( !pCtx->lock
eefd0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 ProxyPath ){.
eefe0 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f /* for auto
eeff0 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63 -named local loc
ef000 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65 k file, just che
ef010 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 ck the host ID a
ef020 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20 nd we'll.
ef030 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f 63 ** use the loc
ef040 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 al lock file pat
ef050 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 79 h that's already
ef060 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 20 in there */.
ef070 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 if( !memcmp
ef080 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 (testValue, conc
ef090 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45 hValue, HOSTIDLE
ef0a0 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 N) ){.
ef0b0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 61 tLockPath = (cha
ef0c0 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b r *)&conchValue[
ef0d0 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20 HOSTIDLEN];.
ef0e0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 goto end_t
ef0f0 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 akeconch;.
ef100 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
ef110 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 76 . /* we'v
ef120 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 20 e got the conch
ef130 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61 if conchValue ma
ef140 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 61 tches our path a
ef150 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20 nd host ID */.
ef160 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d if( !memcm
ef170 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e p(testValue, con
ef180 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 chValue, CONCHLE
ef190 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 N) ){.
ef1a0 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e goto end_takecon
ef1b0 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ch;. }.
ef1c0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
ef1d0 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f 72 . /* a shor
ef1e0 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 27 t read means we'
ef1f0 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 68 re "creating" th
ef200 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 68 e conch (even th
ef210 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a 20 ough it could .
ef220 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 65 ** have bee
ef230 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e 74 n user-intervent
ef240 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 75 ion), if we acqu
ef250 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 76 ire the exclusiv
ef260 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a e lock,. **
ef270 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61 we'll try to ma
ef280 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 tch the current
ef290 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 69 on-disk permissi
ef2a0 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 ons of the datab
ef2b0 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ase. */.
ef2c0 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 31 syncPerms = 1
ef2d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 ;. }. .
ef2e0 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 68 /* either conch
ef2f0 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 69 was emtpy or di
ef300 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 dn't match */.
ef310 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 if( !pCtx->loc
ef320 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
ef330 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b proxyGetLock
ef340 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 74 Path(pCtx->dbPat
ef350 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 h, lockPath, MAX
ef360 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 PATHLEN);.
ef370 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b tLockPath = lock
ef380 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 6c Path;. strl
ef390 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 cpy(&testValue[H
ef3a0 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50 OSTIDLEN], lockP
ef3b0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
ef3c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 ;. }. .
ef3d0 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 68 /* update conch
ef3e0 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 70 with host and p
ef3f0 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 66 ath (this will f
ef400 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 6f ail if other pro
ef410 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 73 cess. ** has
ef420 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 a shared lock a
ef430 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 72 lready) */. r
ef440 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
ef450 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 Method->xLock((s
ef460 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
ef470 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 56 chFile, EXCLUSIV
ef480 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 E_LOCK);. if(
ef490 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
ef4a0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 {. rc = uni
ef4b0 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 5f xWrite((sqlite3_
ef4c0 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 file *)conchFile
ef4d0 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e , testValue, CON
ef4e0 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 CHLEN, 0);.
ef4f0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
ef500 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 20 OK && syncPerms
ef510 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 ){. struc
ef520 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20 t stat buf;.
ef530 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 73 int err = fs
ef540 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 tat(pFile->h, &b
ef550 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 uf);. if(
ef560 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 err==0 ){.
ef570 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d /* try to m
ef580 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 73 atch the databas
ef590 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 6f e file permissio
ef5a0 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c 75 ns, ignore failu
ef5b0 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 re */.#ifndef SQ
ef5c0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 LITE_PROXY_DEBUG
ef5d0 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d 6f . fchmo
ef5e0 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 d(conchFile->h,
ef5f0 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65 buf.st_mode);.#e
ef600 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 66 lse. if
ef610 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 ( fchmod(conchFi
ef620 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f le->h, buf.st_mo
ef630 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 de)!=0 ){.
ef640 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d int code =
ef650 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
ef660 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
ef670 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 46 rr, "fchmod %o F
ef680 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 73 AILED with %d %s
ef690 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
ef6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef6b0 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63 buf.st_mode, c
ef6c0 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f ode, strerror(co
ef6d0 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 de));.
ef6e0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 } else {.
ef6f0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ef700 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 err, "fchmod %o
ef710 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e SUCCEDED\n",buf.
ef720 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20 st_mode);.
ef730 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
ef740 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 lse{. i
ef750 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b nt code = errno;
ef760 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e . fprin
ef770 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 54 tf(stderr, "STAT
ef780 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 68 FAILED[%d] with
ef790 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20 %d %s\n", .
ef7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef7b0 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 2c err, code,
ef7c0 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29 strerror(code))
ef7d0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 ;.#endif.
ef7e0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
ef7f0 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e . conchFile->
ef800 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
ef810 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
ef820 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 conchFile, SHARE
ef830 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f D_LOCK);. .end_
ef840 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f takeconch:. O
ef850 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52 STRACE2("TRANSPR
ef860 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e OXY: CLOSE %d\n
ef870 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
ef880 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ef890 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70 _OK && pFile->op
ef8a0 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 enFlags ){.
ef8b0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 if( pFile->h>=0
ef8c0 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 43 ){.#ifdef STRIC
ef8d0 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20 T_CLOSE_ERROR.
ef8e0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 if( close(
ef8f0 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 pFile->h) ){.
ef900 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
ef910 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
ef920 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
ef930 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
ef940 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a LOSE;. }.
ef950 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 6c #else. cl
ef960 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f ose(pFile->h); /
ef970 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
ef980 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65 fd if fail */.#e
ef990 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 ndif. }.
ef9a0 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 pFile->h = -1
ef9b0 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 3d ;. int fd =
ef9c0 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 61 open(pCtx->dbPa
ef9d0 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 th, pFile->openF
ef9e0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lags,.
ef9f0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
efa00 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
efa10 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 RMISSIONS);.
efa20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e OSTRACE2("TRAN
efa30 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64 SPROXY: OPEN %d
efa40 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 20 \n", fd);.
efa50 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 if( fd>=0 ){.
efa60 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 pFile->h =
efa70 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b fd;. }else{
efa80 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c 49 . rc=SQLI
efa90 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20 TE_CANTOPEN; /*
efaa0 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 6f SQLITE_BUSY? pro
efab0 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c xyTakeConch call
efac0 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ed.
efad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efae0 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 during locking
efaf0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 */. }.
efb00 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
efb10 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 78 LITE_OK && !pCtx
efb20 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 ->lockProxy ){.
efb30 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 20 char *path
efb40 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c = tLockPath ? tL
efb50 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e ockPath : pCtx->
efb60 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 lockProxyPath;.
efb70 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65 /* ACS: Nee
efb80 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 d to make a copy
efb90 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 6d of path sometim
efba0 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d es */. rc =
efbb0 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 proxyCreateUnix
efbc0 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 78 File(path, &pCtx
efbd0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 ->lockProxy);.
efbe0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
efbf0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
efc00 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 pCtx->conchHe
efc10 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 ld = 1;.. i
efc20 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a f( tLockPath ){.
efc30 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f pCtx->lo
efc40 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 ckProxyPath = sq
efc50 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c lite3DbStrDup(0,
efc60 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20 tLockPath);.
efc70 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c if( pCtx->l
efc80 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f ockProxy->pMetho
efc90 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f d == &afpIoMetho
efca0 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ds ){.
efcb0 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 ((afpLockingCont
efcc0 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b ext *)pCtx->lock
efcd0 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f Proxy->lockingCo
efce0 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 3d ntext)->dbPath =
efcf0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
efd00 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b pCtx->lock
efd10 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 ProxyPath;.
efd20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
efd30 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
efd40 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
efd50 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
efd60 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
efd70 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a File, NO_LOCK);.
efd80 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 }. OSTRAC
efd90 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 E3("TAKECONCH %
efda0 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d %s\n", conchFi
efdb0 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54 le->h, rc==SQLIT
efdc0 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65 E_OK?"ok":"faile
efdd0 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 d");. return
efde0 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rc;. }.}../*.**
efdf0 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 20 If pFile holds
efe00 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63 a lock on a conc
efe10 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c h file, then rel
efe20 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a ease that lock..
efe30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
efe40 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 oxyReleaseConch(
efe50 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 unixFile *pFile)
efe60 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
efe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efe80 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 65 /* Subroutine re
efe90 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70 turn code */. p
efea0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
efeb0 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68 xt *pCtx; /* Th
efec0 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 e locking contex
efed0 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20 t for the proxy
efee0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69 lock */. unixFi
efef0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 le *conchFile;
eff00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
eff10 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
eff20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 72 */.. pCtx = (pr
eff30 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
eff40 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
eff50 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e ngContext;. con
eff60 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
eff70 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52 onchFile;. OSTR
eff80 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f 4e ACE4("RELEASECON
eff90 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 CH %d for %s pi
effa0 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d=%d\n", conchFi
effb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
effc0 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f (pCtx->lockPro
effd0 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c xyPath ? pCtx->l
effe0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 ockProxyPath : "
efff0 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20 :auto:"), .
f0000 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 3b getpid());
f0010 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 . pCtx->conchHe
f0020 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63 ld = 0;. rc = c
f0030 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
f0040 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 d->xUnlock((sqli
f0050 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
f0060 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 ile, NO_LOCK);.
f0070 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 41 OSTRACE3("RELEA
f0080 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e SECONCH %d %s\n
f0090 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c ", conchFile->h,
f00a0 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d . (rc=
f00b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
f00c0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a " : "failed"));.
f00d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
f00e0 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
f00f0 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 name of a databa
f0100 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65 se file, compute
f0110 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 73 the name of its
f0120 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 conch file..**
f0130 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 20 Store the conch
f0140 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f filename in memo
f0150 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
f0160 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
f0170 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e )..** Make *pCon
f0180 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f 20 chPath point to
f0190 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52 the new name. R
f01a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
f01b0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
f01c0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
f01d0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 unable to obtai
f01e0 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 n memory..**.**
f01f0 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 The caller is re
f0200 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e sponsible for en
f0210 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 suring that the
f0220 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 allocated memory
f0230 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 65 .** space is eve
f0240 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a ntually freed..*
f0250 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68 *.** *pConchPath
f0260 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 is set to NULL
f0270 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f if a memory allo
f0280 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
f0290 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 urs..*/.static i
f02a0 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f nt proxyCreateCo
f02b0 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 72 nchPathname(char
f02c0 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a *dbPath, char *
f02d0 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20 *pConchPath){.
f02e0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
f02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f0300 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
f0310 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e . int len = (in
f0320 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29 t)strlen(dbPath)
f0330 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64 ; /* Length of d
f0340 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
f0350 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 63 - dbPath */. c
f0360 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20 har *conchPath;
f0370 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f0380 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20 buffer in which
f0390 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e to construct con
f03a0 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a ch name */.. /*
f03b0 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 Allocate space
f03c0 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69 for the conch fi
f03d0 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 69 lename and initi
f03e0 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 74 alize the name t
f03f0 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 o. ** the name
f0400 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 of the original
f0410 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a database file. *
f0420 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74 / . *pConchPat
f0430 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d 20 h = conchPath =
f0440 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
f0450 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b malloc(len + 8);
f0460 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68 . if( conchPath
f0470 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f0480 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f0490 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e }. memcpy(con
f04a0 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c 20 chPath, dbPath,
f04b0 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 len+1);. . /*
f04c0 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e 22 now insert a "."
f04d0 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 before the last
f04e0 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a / character */.
f04f0 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29 for( i=(len-1)
f0500 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 ; i>=0; i-- ){.
f0510 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68 if( conchPath
f0520 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 [i]=='/' ){.
f0530 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65 i++;. bre
f0540 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
f0550 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27 conchPath[i]='.'
f0560 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c 65 ;. while ( i<le
f0570 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61 n ){. conchPa
f0580 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69 th[i+1]=dbPath[i
f0590 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a ];. i++;. }.
f05a0 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 65 . /* append the
f05b0 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 78 "-conch" suffix
f05c0 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a to the file */.
f05d0 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 50 memcpy(&conchP
f05e0 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 ath[i+1], "-conc
f05f0 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 74 h", 7);. assert
f0600 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f ( (int)strlen(co
f0610 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b nchPath) == len+
f0620 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 7 );.. return S
f0630 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
f0640 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 63 Takes a fully c
f0650 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 20 onfigured proxy
f0660 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e locking-style un
f0670 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 74 ix file and swit
f0680 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 ches.** the loca
f0690 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 l lock file path
f06a0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
f06b0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 switchLockProxyP
f06c0 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 ath(unixFile *pF
f06d0 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ile, const char
f06e0 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 *path) {. proxy
f06f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f0700 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 pCtx = (proxyLoc
f0710 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 kingContext*)pFi
f0720 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0730 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50 xt;. char *oldP
f0740 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b ath = pCtx->lock
f0750 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e 74 ProxyPath;. int
f0760 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
f0770 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c .. if( pFile->l
f0780 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
f0790 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f07a0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 QLITE_BUSY;. }
f07b0 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 .. /* nothing
f07c0 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 74 to do if the pat
f07d0 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f h is NULL, :auto
f07e0 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 65 : or matches the
f07f0 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 2a existing path *
f0800 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c /. if( !path ||
f0810 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c path[0]=='\0' |
f0820 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 | !strcmp(path,
f0830 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 ":auto:") ||.
f0840 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 74 (oldPath && !st
f0850 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 rncmp(oldPath, p
f0860 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
f0870 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
f0880 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
f0890 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 se{. unixFile
f08a0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 *lockProxy = pC
f08b0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
f08c0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f pCtx->lockPro
f08d0 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 xy=NULL;. pCt
f08e0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 x->conchHeld = 0
f08f0 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 ;. if( lockPr
f0900 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 oxy!=NULL ){.
f0910 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d rc=lockProxy-
f0920 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 >pMethod->xClose
f0930 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ((sqlite3_file *
f0940 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 )lockProxy);.
f0950 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
f0960 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c rn rc;. sql
f0970 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 ite3_free(lockPr
f0980 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 oxy);. }.
f0990 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 sqlite3_free(old
f09a0 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d Path);. pCtx-
f09b0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d >lockProxyPath =
f09c0 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
f09d0 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 (0, path);. }.
f09e0 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d . return rc;.}
f09f0 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 ../*.** pFile is
f0a00 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 a file that has
f0a10 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 been opened by
f0a20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 a prior xOpen ca
f0a30 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 ll. dbPath.** i
f0a40 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 s a string buffe
f0a50 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 41 r at least MAXPA
f0a60 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 65 THLEN+1 characte
f0a70 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a rs in size..**.*
f0a80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 * This routine f
f0a90 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 ind the filename
f0aa0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
f0ab0 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 65 pFile and write
f0ac0 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 s it.** int dbPa
f0ad0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
f0ae0 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 68 t proxyGetDbPath
f0af0 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 ForUnixFile(unix
f0b00 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 File *pFile, cha
f0b10 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 20 r *dbPath){.#if
f0b20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
f0b30 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e _). if( pFile->
f0b40 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 pMethod == &afpI
f0b50 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
f0b60 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 65 /* afp style kee
f0b70 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 ps a reference t
f0b80 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 6e o the db path in
f0b90 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 69 the filePath fi
f0ba0 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 eld . ** of t
f0bb0 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 he struct */.
f0bc0 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 assert( (int)st
f0bd0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c rlen((char*)pFil
f0be0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0bf0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 t)<=MAXPATHLEN )
f0c00 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 ;. strcpy(dbP
f0c10 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e ath, ((afpLockin
f0c20 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 gContext *)pFile
f0c30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f0c40 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d 65 )->dbPath);. }e
f0c50 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 lse.#endif. if(
f0c60 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 pFile->pMethod
f0c70 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 == &dotlockIoMet
f0c80 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64 hods ){. /* d
f0c90 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73 ot lock style us
f0ca0 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 es the locking c
f0cb0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 ontext to store
f0cc0 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20 the dot lock.
f0cd0 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a 2f ** file path */
f0ce0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73 . int len = s
f0cf0 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 46 trlen((char *)pF
f0d00 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0d10 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f ext) - strlen(DO
f0d20 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20 TLOCK_SUFFIX);.
f0d30 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 68 memcpy(dbPath
f0d40 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d , (char *)pFile-
f0d50 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c >lockingContext,
f0d60 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c len + 1);. }el
f0d70 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f se{. /* all o
f0d80 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 20 ther styles use
f0d90 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 the locking cont
f0da0 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ext to store the
f0db0 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a 2f db file path */
f0dc0 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72 . assert( str
f0dd0 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 len((char*)pFile
f0de0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f0df0 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b )<=MAXPATHLEN );
f0e00 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 61 . strcpy(dbPa
f0e10 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c th, (char *)pFil
f0e20 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0e30 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
f0e40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
f0e50 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c *.** Takes an al
f0e60 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e 20 ready filled in
f0e70 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 6c unix file and al
f0e80 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 66 ters it so all f
f0e90 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 ile locking .**
f0ea0 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d 65 will be performe
f0eb0 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 d on the local p
f0ec0 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20 roxy lock file.
f0ed0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
f0ee0 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 65 ields.** are pre
f0ef0 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c 6f served in the lo
f0f00 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f cking context so
f0f10 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 62 that they can b
f0f20 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 0a e restored and .
f0f30 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 75 ** the unix stru
f0f40 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 63 cture properly c
f0f50 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c 6f leaned up at clo
f0f60 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c se time:.** ->l
f0f70 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a ockingContext.**
f0f80 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73 ->pMethod.*/.s
f0f90 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 tatic int proxyT
f0fa0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 ransformUnixFile
f0fb0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
f0fc0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 , const char *pa
f0fd0 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 th) {. proxyLoc
f0fe0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
f0ff0 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 68 x;. char dbPath
f1000 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20 [MAXPATHLEN+1];
f1010 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
f1020 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
f1030 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f le */. char *lo
f1040 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69 ckPath=NULL;. i
f1050 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
f1060 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 6c K;. . if( pFil
f1070 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f e->locktype!=NO_
f1080 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 LOCK ){. retu
f1090 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a rn SQLITE_BUSY;.
f10a0 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 62 }. proxyGetDb
f10b0 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 PathForUnixFile(
f10c0 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a pFile, dbPath);.
f10d0 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 if( !path || p
f10e0 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 ath[0]=='\0' ||
f10f0 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a !strcmp(path, ":
f1100 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c auto:") ){. l
f1110 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 ockPath=NULL;.
f1120 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50 }else{. lockP
f1130 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 68 ath=(char *)path
f1140 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 ;. }. . OSTRA
f1150 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 20 CE4("TRANSPROXY
f1160 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 %d for %s pid=%
f1170 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
f1180 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 6b (lock
f1190 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20 Path ? lockPath
f11a0 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 : ":auto:"), get
f11b0 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 20 pid());.. pCtx
f11c0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
f11d0 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 ( sizeof(*pCtx)
f11e0 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d 30 );. if( pCtx==0
f11f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f1200 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
f1210 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c 20 . memset(pCtx,
f1220 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 0, sizeof(*pCtx)
f1230 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 79 );.. rc = proxy
f1240 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e CreateConchPathn
f1250 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 74 ame(dbPath, &pCt
f1260 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
f1270 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
f1280 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
f1290 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e = proxyCreateUn
f12a0 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e ixFile(pCtx->con
f12b0 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 74 chFilePath, &pCt
f12c0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 x->conchFile);.
f12d0 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 } . if( rc==S
f12e0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b QLITE_OK && lock
f12f0 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 78 Path ){. pCtx
f1300 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
f1310 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
f1320 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a p(0, lockPath);.
f1330 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
f1340 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f1350 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 /* all memory is
f1360 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78 allocated, prox
f1370 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61 ys are created a
f1380 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20 nd assigned, .
f1390 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20 ** switch the
f13a0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
f13b0 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e and pMethod then
f13c0 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a return.. */.
f13d0 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68 pCtx->dbPath
f13e0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
f13f0 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a 20 up(0, dbPath);.
f1400 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b pCtx->oldLock
f1410 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 69 ingContext = pFi
f1420 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f1430 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c xt;. pFile->l
f1440 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
f1450 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e pCtx;. pCtx->
f1460 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 69 pOldMethod = pFi
f1470 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20 le->pMethod;.
f1480 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 pFile->pMethod
f1490 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 = &proxyIoMethod
f14a0 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
f14b0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 if( pCtx->conchF
f14c0 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 63 ile ){ . rc
f14d0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 = pCtx->conchFi
f14e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c le->pMethod->xCl
f14f0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ose((sqlite3_fil
f1500 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46 e *)pCtx->conchF
f1510 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ile);. if(
f1520 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f1530 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
f1540 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 ee(pCtx->conchFi
f1550 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 le);. }. s
f1560 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f1570 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 ->conchFilePath)
f1580 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ; . sqlite3_f
f1590 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 ree(pCtx);. }.
f15a0 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e 53 OSTRACE3("TRANS
f15b0 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 2c PROXY %d %s\n",
f15c0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
f15d0 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 (rc==SQLIT
f15e0 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
f15f0 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 ailed"));. retu
f1600 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
f1610 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 This routine ha
f1620 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69 ndles sqlite3_fi
f1630 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c le_control() cal
f1640 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 63 ls that are spec
f1650 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 ific.** to proxy
f1660 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 locking..*/.sta
f1670 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c tic int proxyFil
f1680 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
f1690 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
f16a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
f16b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
f16c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
f16d0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
f16e0 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 E: {. unixF
f16f0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
f1700 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 ixFile*)id;.
f1710 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 if( pFile->pMe
f1720 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f thod == &proxyIo
f1730 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 Methods ){.
f1740 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 proxyLockingC
f1750 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 ontext *pCtx = (
f1760 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1770 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ext*)pFile->lock
f1780 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f1790 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e proxyTakeCon
f17a0 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 ch(pFile);.
f17b0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
f17c0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
f17d0 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 *(const
f17e0 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 char **)pArg = p
f17f0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
f1800 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 th;. }els
f1810 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 e{. *(c
f1820 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 onst char **)pAr
f1830 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 g = ":auto: (not
f1840 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 held)";.
f1850 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
f1860 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 {. *(cons
f1870 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d t char **)pArg =
f1880 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 NULL;. }.
f1890 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
f18a0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
f18b0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 case SQLITE_SET
f18c0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 _LOCKPROXYFILE:
f18d0 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65 {. unixFile
f18e0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
f18f0 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 ile*)id;. i
f1900 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
f1910 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50 K;. int isP
f1920 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69 roxyStyle = (pFi
f1930 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 le->pMethod == &
f1940 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b proxyIoMethods);
f1950 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 3d . if( pArg=
f1960 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 =NULL || (const
f1970 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 char *)pArg==0 )
f1980 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 {. if( is
f1990 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 ProxyStyle ){.
f19a0 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 /* turn
f19b0 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e off proxy lockin
f19c0 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 g - not supporte
f19d0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 d */. r
f19e0 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
f19f0 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 /*SQLITE_PROTOC
f1a00 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 OL? SQLITE_MISUS
f1a10 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 E?*/;. }e
f1a20 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
f1a30 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 * turn off proxy
f1a40 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 locking - alrea
f1a50 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f dy off - NOOP */
f1a60 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
f1a70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
f1a80 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
f1a90 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
f1aa0 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20 char *proxyPath
f1ab0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 = (const char *)
f1ac0 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66 pArg;. if
f1ad0 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 ( isProxyStyle )
f1ae0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f 78 {. prox
f1af0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f1b00 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20 *pCtx = .
f1b10 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 (proxyLocki
f1b20 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 ngContext*)pFile
f1b30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f1b40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
f1b50 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a !strcmp(pArg, ":
f1b60 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 auto:") .
f1b70 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f || (pCtx->lo
f1b80 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20 ckProxyPath &&.
f1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 73 !s
f1ba0 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 trncmp(pCtx->loc
f1bb0 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 kProxyPath, prox
f1bc0 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 yPath, MAXPATHLE
f1bd0 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b N)). ){
f1be0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
f1bf0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
f1c00 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
f1c10 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
f1c20 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 witchLockProxyPa
f1c30 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 th(pFile, proxyP
f1c40 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ath);.
f1c50 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
f1c60 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 . /* tu
f1c70 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 rn on proxy file
f1c80 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 locking */.
f1c90 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
f1ca0 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
f1cb0 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 e(pFile, proxyPa
f1cc0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 th);. }.
f1cd0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
f1ce0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
f1cf0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
f1d00 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20 assert( 0 );
f1d10 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73 /* The call ass
f1d20 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 ures that only v
f1d30 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65 alid opcodes are
f1d40 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 sent */. }.
f1d50 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 }. /*NOTREACHE
f1d60 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c D*/. return SQL
f1d70 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a ITE_ERROR;.}../*
f1d80 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 .** Within this
f1d90 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72 division (the pr
f1da0 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 oxying locking i
f1db0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 mplementation) t
f1dc0 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a he procedures.**
f1dd0 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e above this poin
f1de0 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 t are all utilit
f1df0 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 ies. The lock-r
f1e00 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f elated methods o
f1e10 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c f the.** proxy-l
f1e20 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 ocking sqlite3_i
f1e30 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 o_method object
f1e40 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a follow..*/.../*.
f1e50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
f1e60 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 checks if there
f1e70 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f is a RESERVED lo
f1e80 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 ck held on the s
f1e90 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 pecified.** file
f1ea0 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 by this or any
f1eb0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 other process. I
f1ec0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 f such a lock is
f1ed0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 held, set *pRes
f1ee0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d Out.** to a non-
f1ef0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 zero value other
f1f00 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 wise *pResOut is
f1f10 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 set to zero. T
f1f20 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
f1f30 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
f1f40 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e ITE_OK unless an
f1f50 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
f1f60 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 s during lock ch
f1f70 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 ecking..*/.stati
f1f80 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b c int proxyCheck
f1f90 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
f1fa0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f1fb0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 nt *pResOut) {.
f1fc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f1fd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f1fe0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
f1ff0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
f2000 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
f2010 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f2020 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f2030 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
f2040 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f2050 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f2060 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
f2070 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 xFile *proxy = p
f2080 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f2090 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 return proxy
f20a0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 ->pMethod->xChec
f20b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 kReservedLock((s
f20c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f qlite3_file*)pro
f20d0 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 xy, pResOut);.
f20e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
f20f0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
f2100 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
f2110 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
f2120 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
f2130 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
f2140 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
f2150 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
f2160 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
f2170 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
f2180 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
f2190 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
f21a0 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
f21b0 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
f21c0 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
f21d0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
f21e0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
f21f0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
f2200 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
f2210 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
f2220 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
f2230 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
f2240 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
f2250 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
f2260 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
f2270 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
f2280 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
f2290 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
f22a0 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
f22b0 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
f22c0 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
f22d0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
f22e0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
f22f0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
f2300 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
f2310 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
f2320 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
f2330 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
f2340 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
f2350 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
f2360 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
f2370 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
f2380 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
f2390 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
f23a0 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
f23b0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
f23c0 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
f23d0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 . Use the sqlit
f23e0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 e3OsUnlock().**
f23f0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 routine to lower
f2400 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c a locking level
f2410 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f2420 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 proxyLock(sqlite
f2430 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
f2440 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
f2450 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
f2460 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
f2470 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54 int rc = proxyT
f2480 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b akeConch(pFile);
f2490 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
f24a0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 E_OK ){. prox
f24b0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f24c0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
f24d0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
f24e0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f24f0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 text;. unixFi
f2500 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 le *proxy = pCtx
f2510 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 ->lockProxy;.
f2520 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 rc = proxy->pMe
f2530 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c thod->xLock((sql
f2540 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 ite3_file*)proxy
f2550 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 , locktype);.
f2560 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
f2570 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 = proxy->lockty
f2580 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e pe;. }. return
f2590 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c rc;.}.../*.** L
f25a0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
f25b0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
f25c0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
f25d0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
f25e0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
f25f0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
f2600 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
f2610 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
f2620 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
f2630 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
f2640 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
f2650 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
f2660 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
f2670 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
f2680 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
f2690 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
f26a0 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c proxyUnlock(sql
f26b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f26c0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
f26d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f26e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f26f0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
f2700 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
f2710 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
f2720 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f2730 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f2740 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
f2750 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f2760 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f2770 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
f2780 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 xFile *proxy = p
f2790 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f27a0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e rc = proxy->
f27b0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
f27c0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
f27d0 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 proxy, locktype)
f27e0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ;. pFile->loc
f27f0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c ktype = proxy->l
f2800 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 ocktype;. }. r
f2810 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
f2820 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 ** Close a file
f2830 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20 that uses proxy
f2840 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 locks..*/.static
f2850 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 int proxyClose(
f2860 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f2870 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a ) {. if( id ){.
f2880 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
f2890 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f28a0 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f )id;. proxyLo
f28b0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
f28c0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
f28d0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
f28e0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f28f0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
f2900 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 *lockProxy = pCt
f2910 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
f2920 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 unixFile *conc
f2930 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f hFile = pCtx->co
f2940 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 nchFile;. int
f2950 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
f2960 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f . . if( lo
f2970 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 ckProxy ){.
f2980 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d rc = lockProxy-
f2990 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
f29a0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
f29b0 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c )lockProxy, NO_L
f29c0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
f29d0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f29e0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 rc = lockP
f29f0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
f2a00 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
f2a10 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b ile*)lockProxy);
f2a20 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
f2a30 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
f2a40 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f sqlite3_free(lo
f2a50 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 ckProxy);.
f2a60 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 pCtx->lockProxy
f2a70 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
f2a80 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a f( conchFile ){.
f2a90 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
f2aa0 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20 conchHeld ){.
f2ab0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52 rc = proxyR
f2ac0 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c eleaseConch(pFil
f2ad0 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
f2ae0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f2af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
f2b00 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d = conchFile->pM
f2b10 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
f2b20 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
f2b30 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 chFile);. i
f2b40 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f2b50 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
f2b60 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29 _free(conchFile)
f2b70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
f2b80 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c te3_free(pCtx->l
f2b90 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a 20 ockProxyPath);.
f2ba0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
f2bb0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 pCtx->conchFileP
f2bc0 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ath);. sqlite
f2bd0 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 50 3_free(pCtx->dbP
f2be0 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73 ath);. /* res
f2bf0 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 tore the origina
f2c00 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 l locking contex
f2c10 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 t and pMethod th
f2c20 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20 en close it */.
f2c30 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e pFile->lockin
f2c40 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 2d gContext = pCtx-
f2c50 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 >oldLockingConte
f2c60 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 xt;. pFile->p
f2c70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70 Method = pCtx->p
f2c80 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73 OldMethod;. s
f2c90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f2ca0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 46 );. return pF
f2cb0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 ile->pMethod->xC
f2cc0 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 lose(id);. }.
f2cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f2ce0 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a ;.}....#endif /*
f2cf0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
f2d00 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e __) && SQLITE_EN
f2d10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
f2d20 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 LE */./*.** The
f2d30 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74 proxy locking st
f2d40 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 yle is intended
f2d50 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 50 for use with AFP
f2d60 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a filesystems..**
f2d70 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 69 And since AFP i
f2d80 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 s only supported
f2d90 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 20 on MacOSX, the
f2da0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 proxy locking is
f2db0 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63 also.** restric
f2dc0 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a ted to MacOSX..*
f2dd0 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a * .**.**********
f2de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
f2df0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 the proxy lock
f2e00 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
f2e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e20 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
f2e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e70 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ****/../*.** Ini
f2e80 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 tialize the oper
f2e90 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
f2ea0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 erface..**.** Th
f2eb0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73 is routine regis
f2ec0 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d 70 ters all VFS imp
f2ed0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 lementations for
f2ee0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 unix-like opera
f2ef0 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e ting.** systems.
f2f00 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 This routine,
f2f10 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f and the sqlite3_
f2f20 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 os_end() routine
f2f30 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a that follows,.*
f2f40 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 * should be the
f2f50 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 6e only routines in
f2f60 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 20 this file that
f2f70 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f 6d are visible from
f2f80 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e other.** files.
f2f90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
f2fa0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e ine is called on
f2fb0 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 ce during SQLite
f2fc0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
f2fd0 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67 and by a.** sing
f2fe0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
f2ff0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f3000 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73 n and mutex subs
f3010 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a ystems have not.
f3020 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 62 ** necessarily b
f3030 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 een initialized
f3040 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
f3050 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64 e is called, and
f3060 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 so they.** shou
f3070 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a ld not be used..
f3080 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f3090 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 t sqlite3_os_ini
f30a0 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a t(void){ . /* .
f30b0 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
f30c0 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 73 ng macro defines
f30d0 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 an initializer
f30e0 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 for an sqlite3_v
f30f0 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 fs object.. **
f3100 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
f3110 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 68 VFS is NAME. Th
f3120 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 20 e pAppData is a
f3130 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 pointer to a poi
f3140 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65 nter. ** to the
f3150 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 "finder" functi
f3160 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 69 on. (pAppData i
f3170 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
f3180 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 65 pointer because
f3190 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20 . ** silly C90
f31a0 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 61 rules prohibit a
f31b0 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e void* from bein
f31c0 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e 63 g cast to a func
f31d0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a tion pointer. *
f31e0 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 65 * and so we have
f31f0 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 to go through t
f3200 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
f3210 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 64 pointer to avoid
f3220 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77 problems. ** w
f3230 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 hen compiling wi
f3240 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 th -pedantic-err
f3250 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a ors on GCC.). *
f3260 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 45 *. ** The FINDE
f3270 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 R parameter to t
f3280 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 65 his macro is the
f3290 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f 69 name of the poi
f32a0 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a nter to the. **
f32b0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
f32c0 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 75 . The finder-fu
f32d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 nction returns a
f32e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a pointer to the.
f32f0 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d ** sqlite_io_m
f3300 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 68 ethods object th
f3310 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
f3320 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e e desired lockin
f3330 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 73 g. ** behaviors
f3340 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 73 . See the divis
f3350 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 63 ion above that c
f3360 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d 45 ontains the IOME
f3370 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f THODS. ** macro
f3380 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 6e for addition in
f3390 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e formation on fin
f33a0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 der-functions..
f33b0 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69 **. ** Most fi
f33c0 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 74 nders simply ret
f33d0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
f33e0 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 a fixed sqlite3
f33f0 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a _io_methods. **
f3400 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 68 object. But th
f3410 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e e "autolockIoFin
f3420 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 6f der" available o
f3430 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 20 n MacOSX does a
f3440 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65 little. ** more
f3450 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 6c than that; it l
f3460 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 ooks at the file
f3470 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 74 system type that
f3480 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a 2a hosts the . **
f3490 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
f34a0 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f 6f nd tries to choo
f34b0 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65 se an locking me
f34c0 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 65 thod appropriate
f34d0 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 66 for. ** that f
f34e0 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a ilesystem time..
f34f0 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 */. #define U
f3500 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c 20 NIXVFS(VFSNAME,
f3510 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 20 FINDER) {
f3520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3530 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 \. 1,
f3540 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f3550 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 iVersion */
f3560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3570 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 . sizeof(unix
f3580 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a File), /* sz
f3590 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 OsFile */
f35a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f35b0 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c MAX_PATHNAME,
f35c0 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 /* mxPa
f35d0 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 thname */
f35e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f35f0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
f3600 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 /* pNext
f3610 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3620 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 56 \. V
f3630 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 FSNAME,
f3640 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f /* zName */
f3650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3660 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 6f \. (vo
f3670 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 20 id*)&FINDER,
f3680 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a /* pAppData *
f3690 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f36a0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f \. unixO
f36b0 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 pen,
f36c0 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 /* xOpen */
f36d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f36e0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c \. unixDel
f36f0 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f ete, /
f3700 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 * xDelete */
f3710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3720 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 73 \. unixAcces
f3730 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
f3740 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 20 xAccess */
f3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3760 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 . unixFullPat
f3770 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 hname, /* xF
f3780 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 ullPathname */
f3790 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f37a0 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 unixDlOpen,
f37b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f /* xDlO
f37c0 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 pen */
f37d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f37e0 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 unixDlError,
f37f0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 /* xDlErr
f3800 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 or */
f3810 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f3820 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 nixDlSym,
f3830 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a /* xDlSym *
f3840 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3850 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
f3860 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 xDlClose,
f3870 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a /* xDlClose *
f3880 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3890 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 \. unixR
f38a0 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 andomness,
f38b0 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 /* xRandomness
f38c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f38d0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 \. unixSle
f38e0 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ep, /
f38f0 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 * xSleep */
f3900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3910 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 65 \. unixCurre
f3920 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 ntTime, /*
f3930 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 xCurrentTime */
f3940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3950 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 74 . unixGetLast
f3960 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 Error /* xG
f3970 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 etLastError */
f3980 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3990 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c }.. /*. ** Al
f39a0 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20 l default VFSes
f39b0 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e for unix are con
f39c0 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f tained in the fo
f39d0 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20 llowing array..
f39e0 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 **. ** Note th
f39f0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 at the sqlite3_v
f3a00 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f fs.pNext field o
f3a10 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 f the VFS object
f3a20 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a is modified. *
f3a30 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 * by the SQLite
f3a40 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46 core when the VF
f3a50 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e S is registered.
f3a60 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 So the followi
f3a70 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61 ng. ** array ca
f3a80 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20 nnot be const..
f3a90 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c */. static sql
f3aa0 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20 ite3_vfs aVfs[]
f3ab0 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 = {.#if SQLITE_E
f3ac0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
f3ad0 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52 YLE && (OS_VXWOR
f3ae0 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f KS || defined(__
f3af0 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e APPLE__)). UN
f3b00 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20 IXVFS("unix",
f3b10 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49 autolockI
f3b20 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65 oFinder ),.#else
f3b30 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
f3b40 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f ix", po
f3b50 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 sixIoFinder ),.#
f3b60 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46 endif. UNIXVF
f3b70 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 S("unix-none",
f3b80 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 nolockIoFinde
f3b90 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f3ba0 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c ("unix-dotfile",
f3bb0 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 dotlockIoFinde
f3bc0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f3bd0 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 20 ("unix-wfl",
f3be0 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 posixWflIoFind
f3bf0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57 er ),.#if OS_VXW
f3c00 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53 ORKS. UNIXVFS
f3c10 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 ("unix-namedsem"
f3c20 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c , semIoFinder ),
f3c30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
f3c40 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
f3c50 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58 G_STYLE. UNIX
f3c60 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 VFS("unix-posix"
f3c70 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 , posixIoFind
f3c80 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58 er ),.#if !OS_VX
f3c90 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 WORKS. UNIXVF
f3ca0 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 S("unix-flock",
f3cb0 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 flockIoFinder
f3cc0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 ),.#endif.#endi
f3cd0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 f.#if SQLITE_ENA
f3ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
f3cf0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
f3d00 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 PPLE__). UNIX
f3d10 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c 20 VFS("unix-afp",
f3d20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 72 afpIoFinder
f3d30 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 ),. UNIXVFS(
f3d40 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 20 "unix-proxy",
f3d50 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 29 proxyIoFinder )
f3d60 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 ,.#endif. };.
f3d70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 20 unsigned int i;
f3d80 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
f3d90 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f counter */.. /
f3da0 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 56 * Register all V
f3db0 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e 20 FSes defined in
f3dc0 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 79 the aVfs[] array
f3dd0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 */. for(i=0; i
f3de0 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f 73 <(sizeof(aVfs)/s
f3df0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 66 izeof(sqlite3_vf
f3e00 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 s)); i++){. s
f3e10 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
f3e20 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 3d ter(&aVfs[i], i=
f3e30 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 =0);. }. retur
f3e40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
f3e50 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
f3e60 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
f3e70 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
f3e80 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 61 **.** Some opera
f3e90 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 67 ting systems mig
f3ea0 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 6f ht need to do so
f3eb0 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 68 me cleanup in th
f3ec0 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 74 is routine,.** t
f3ed0 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d 69 o release dynami
f3ee0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
f3ef0 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e 6f objects. But no
f3f00 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 68 t on unix..** Th
f3f10 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
f3f20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a no-op for unix..
f3f30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f3f40 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 t sqlite3_os_end
f3f50 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 (void){ . retur
f3f60 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
f3f70 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
f3f80 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f TE_OS_UNIX */../
f3f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
f3fa0 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 nd of os_unix.c
f3fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f3fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
f3ff0 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e egin file os_win
f4000 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
f4010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f4030 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 *.** 2004 May 22
f4040 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
f4050 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
f4060 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
f4070 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
f4080 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
f4090 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
f40a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
f40b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
f40c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
f40d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
f40e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
f40f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
f4100 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
f4110 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
f4120 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
f4130 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
f4140 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
f4150 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
f4160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f41a0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
f41b0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
f41c0 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 69 de that is speci
f41d0 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a fic to windows..
f41e0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f41f0 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 _WIN
f4200 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 /* This file
f4210 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64 is used for wind
f4220 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a ows only */.../*
f4230 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 .** A Note About
f4240 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 Memory Allocati
f4250 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 on:.**.** This d
f4260 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f river uses mallo
f4270 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63 c()/free() direc
f4280 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20 tly rather than
f4290 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a going through.**
f42a0 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70 the SQLite-wrap
f42b0 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c pers sqlite3_mal
f42c0 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 loc()/sqlite3_fr
f42d0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61 ee(). Those wra
f42e0 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 ppers.** are des
f42f0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e igned for use on
f4300 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
f4310 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 s where memory i
f4320 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 s scarce and.**
f4330 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
f4340 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c happen frequentl
f4350 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e y. Win32 does n
f4360 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e ot typically run
f4370 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 on.** embedded
f4380 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 systems, and whe
f4390 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65 n it does the de
f43a0 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c velopers normall
f43b0 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a y have bigger.**
f43c0 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 problems to wor
f43d0 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75 ry about than ru
f43e0 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d nning out of mem
f43f0 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69 ory. So there i
f4400 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 s not.** a compe
f4410 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 lling need to us
f4420 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a e the wrappers..
f4430 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20 **.** But there
f4440 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e is a good reason
f4450 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 to not use the
f4460 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65 wrappers. If we
f4470 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 use the.** wrap
f4480 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c pers then we wil
f4490 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20 l get simulated
f44a0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
f44b0 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a s within this.**
f44c0 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68 driver. And th
f44d0 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 at causes all ki
f44e0 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 nds of problems
f44f0 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20 for our tests.
f4500 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 We.** could enha
f4510 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65 nce SQLite to de
f4520 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65 al with simulate
f4530 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 d malloc failure
f4540 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 s within.** the
f4550 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74 OS driver, but t
f4560 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 he code to deal
f4570 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75 with those failu
f4580 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 re would not.**
f4590 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20 be exercised on
f45a0 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65 Linux (which doe
f45b0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 s not need to ma
f45c0 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72 lloc() in the dr
f45d0 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 iver).** and so
f45e0 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69 we would have di
f45f0 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67 fficulty writing
f4600 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20 coverage tests
f4610 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 for that.** code
f4620 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61 . Better to lea
f4630 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c ve the code out,
f4640 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a we think..**.**
f4650 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 The point of th
f4660 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73 is discussion is
f4670 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 as follows: Wh
f4680 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 en creating a ne
f4690 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f w.** OS layer fo
f46a0 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79 r an embedded sy
f46b0 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 stem, if you use
f46c0 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e this file as an
f46d0 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f example,.** avo
f46e0 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61 id the use of ma
f46f0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20 lloc()/free().
f4700 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77 Those routines w
f4710 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 ork ok on window
f4720 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 s.** desktops bu
f4730 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e t not so well in
f4740 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
f4750 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 s..*/..#include
f4760 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66 <winbase.h>..#if
f4770 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 def __CYGWIN__.#
f4780 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79 include <sys/cy
f4790 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a gwin.h>.#endif..
f47a0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 /*.** Macros use
f47b0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 d to determine w
f47c0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f hether or not to
f47d0 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f use threads..*/
f47e0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 .#if defined(THR
f47f0 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45 EADSAFE) && THRE
f4800 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 ADSAFE.# define
f4810 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41 SQLITE_W32_THREA
f4820 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DS 1.#endif../*.
f4830 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 ** Include code
f4840 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
f4850 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c o all os_*.c fil
f4860 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a es.*/./*********
f4870 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 ***** Include os
f4880 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 _common.h in the
f4890 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69 middle of os_wi
f48a0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f48b0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
f48c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
f48d0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
f48e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f48f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4900 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
f4910 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
f4920 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
f4930 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
f4940 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
f4950 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
f4960 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
f4970 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
f4980 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
f4990 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
f49a0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
f49b0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
f49c0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
f49d0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
f49e0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
f49f0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
f4a00 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
f4a10 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
f4a20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
f4a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
f4a80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
f4a90 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 ains macros and
f4aa0 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 a little bit of
f4ab0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
f4ac0 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 mon to.** all of
f4ad0 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 the platform-sp
f4ae0 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 ecific files (os
f4af0 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e _*.c) and is #in
f4b00 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 cluded into thos
f4b10 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a e.** files..**.*
f4b20 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 * This file shou
f4b30 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 ld be #included
f4b40 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 by the os_*.c fi
f4b50 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 les only. It is
f4b60 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 not a.** genera
f4b70 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 l purpose header
f4b80 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 file..*/.#ifnde
f4b90 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a f _OS_COMMON_H_.
f4ba0 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d #define _OS_COMM
f4bb0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 ON_H_../*.** At
f4bc0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 least two bugs h
f4bd0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 ave slipped in b
f4be0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 ecause we change
f4bf0 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 d the MEMORY_DEB
f4c00 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 UG.** macro to S
f4c10 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 QLITE_DEBUG and
f4c20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 some older makef
f4c30 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 iles have not ye
f4c40 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 t made the.** sw
f4c50 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f itch. The follo
f4c60 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 wing code should
f4c70 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 catch this prob
f4c80 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lem at compile-t
f4c90 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d ime..*/.#ifdef M
f4ca0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 EMORY_DEBUG.# er
f4cb0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f ror "The MEMORY_
f4cc0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f DEBUG macro is o
f4cd0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 bsolete. Use SQ
f4ce0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 LITE_DEBUG inste
f4cf0 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ad.".#endif..#if
f4d00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
f4d10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
f4d20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 int sqlite3OSTra
f4d30 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ce = 0;.#define
f4d40 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 OSTRACE1(X)
f4d50 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
f4d60 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f4d70 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 DebugPrintf(X).#
f4d80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
f4d90 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 X,Y) if( s
f4da0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
f4db0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f4dc0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 tf(X,Y).#define
f4dd0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 OSTRACE3(X,Y,Z)
f4de0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
f4df0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f4e00 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f4e10 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
f4e20 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 CE4(X,Y,Z,A) i
f4e30 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
f4e40 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
f4e50 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a Printf(X,Y,Z,A).
f4e60 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
f4e70 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 (X,Y,Z,A,B) if(
f4e80 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
f4e90 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
f4ea0 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 ntf(X,Y,Z,A,B).#
f4eb0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
f4ec0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 X,Y,Z,A,B,C) \.
f4ed0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
f4ee0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
f4ef0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
f4f00 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
f4f10 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
f4f20 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 ,C,D) \. if(s
f4f30 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
f4f40 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
f4f50 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 f(X,Y,Z,A,B,C,D)
f4f60 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
f4f70 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 STRACE1(X).#defi
f4f80 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
f4f90 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f4fa0 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 3(X,Y,Z).#define
f4fb0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
f4fc0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
f4fd0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 CE5(X,Y,Z,A,B).#
f4fe0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
f4ff0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
f5000 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
f5010 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e Y,Z,A,B,C,D).#en
f5020 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
f5030 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
f5040 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d e tracing. Norm
f5050 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e ally turned off.
f5060 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 Only works.**
f5070 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 on i486 hardware
f5080 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f5090 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 TE_PERFORMANCE_T
f50a0 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 RACE../* .** hwt
f50b0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
f50c0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
f50d0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
f50e0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
f50f0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
f5100 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
f5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
f5120 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
f5130 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
f5140 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f5160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
f5170 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
f5180 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
f5190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f51a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f51b0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
f51c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
f51d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
f51e0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
f51f0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
f5200 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
f5210 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
f5220 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
f5230 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
f5240 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
f5250 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
f5260 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
f5270 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
f5280 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
f5290 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
f52a0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
f52b0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
f52c0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
f52d0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
f52e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f52f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5320 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
f5330 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
f5340 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
f5350 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
f5360 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
f5370 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
f5380 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
f5390 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 */.#ifndef _HWTI
f53a0 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 ME_H_.#define _H
f53b0 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 WTIME_H_../*.**
f53c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
f53d0 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 utine only works
f53e0 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 on pentium-clas
f53f0 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f s (or newer) pro
f5400 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 cessors..** It u
f5410 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 ses the RDTSC op
f5420 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 code to read the
f5430 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c cycle count val
f5440 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a ue out of the.**
f5450 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 processor and r
f5460 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 eturns that valu
f5470 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 e. This can be
f5480 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 used for high-re
f5490 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a s.** profiling..
f54a0 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 */.#if (defined(
f54b0 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 __GNUC__) || def
f54c0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 ined(_MSC_VER))
f54d0 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 && \. (defi
f54e0 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 ned(i386) || def
f54f0 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c ined(__i386__) |
f5500 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 | defined(_M_IX8
f5510 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 6)).. #if defin
f5520 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 ed(__GNUC__)..
f5530 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
f5540 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
f5550 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
f5560 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
f5570 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 lo, hi;. __a
f5580 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
f5590 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 _ ("rdtsc" : "=a
f55a0 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 " (lo), "=d" (hi
f55b0 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 ));. return
f55c0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 (sqlite_uint64)h
f55d0 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 i << 32 | lo;.
f55e0 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e }.. #elif defin
f55f0 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 ed(_MSC_VER)..
f5600 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 __declspec(naked
f5610 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 ) __inline sqlit
f5620 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c e_uint64 __cdecl
f5630 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
f5640 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d oid){. __asm
f5650 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 {. rdtsc
f5660 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 . ret
f5670 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 ; return valu
f5680 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 e at EDX:EAX.
f5690 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 }. }.. #endi
f56a0 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 f..#elif (define
f56b0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
f56c0 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f efined(__x86_64_
f56d0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
f56e0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
f56f0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
f5700 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
f5710 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 ned long val;.
f5720 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
f5730 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 latile__ ("rdtsc
f5740 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b " : "=A" (val));
f5750 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 . return va
f5760 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 l;. }. .#elif (
f5770 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
f5780 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 ) && defined(__p
f5790 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 pc__)).. __inli
f57a0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
f57b0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
f57c0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e (void){. un
f57d0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
f57e0 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 retval;. u
f57f0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e nsigned long jun
f5800 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f k;. __asm__
f5810 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
f5820 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a \n\. 1:
f5830 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 mftbu %1
f5840 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f5850 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c mftb %L
f5860 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
f5870 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 mftbu %
f5880 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
f5890 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 cmpw %
f58a0 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 0,%1\n\.
f58b0 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 bne
f58c0 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 1b".
f58d0 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 : "=r" (
f58e0 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a retval), "=r" (j
f58f0 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 unk));. ret
f5900 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a urn retval;. }.
f5910 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 .#else.. #error
f5920 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 Need implementa
f5930 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 tion of sqlite3H
f5940 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
f5950 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a platform... /*
f5960 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 . ** To compile
f5970 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 without impleme
f5980 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 nting sqlite3Hwt
f5990 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 ime() for your p
f59a0 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f latform,. ** yo
f59b0 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 u can remove the
f59c0 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e above #error an
f59d0 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 d use the follow
f59e0 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 ing. ** stub fu
f59f0 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c nction. You wil
f5a00 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 l lose timing su
f5a10 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 pport for many.
f5a20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 ** of the debug
f5a30 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 ging and testing
f5a40 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 utilities, but
f5a50 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a it should at. *
f5a60 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 * least compile
f5a70 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 and run.. */.SQ
f5a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
f5a90 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
f5aa0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
f5ab0 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 { return ((sqlit
f5ac0 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a e_uint64)0); }..
f5ad0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
f5ae0 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 * !defined(_HWTI
f5af0 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ME_H_) */../****
f5b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f5b10 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a f hwtime.h *****
f5b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f5b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
f5b60 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
f5b70 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f eft off in os_co
f5b80 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
f5b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 *********/..stat
f5ba0 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ic sqlite_uint64
f5bb0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 g_start;.static
f5bc0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 sqlite_uint64 g
f5bd0 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e _elapsed;.#defin
f5be0 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 e TIMER_START
f5bf0 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 g_start=sqli
f5c00 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 te3Hwtime().#def
f5c10 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 ine TIMER_END
f5c20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d g_elapsed=
f5c30 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d sqlite3Hwtime()-
f5c40 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 g_start.#define
f5c50 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 TIMER_ELAPSED
f5c60 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 g_elapsed.#els
f5c70 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f e.#define TIMER_
f5c80 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 START.#define TI
f5c90 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 MER_END.#define
f5ca0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 TIMER_ELAPSED
f5cb0 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 ((sqlite_uint6
f5cc0 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 4)0).#endif../*.
f5cd0 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 ** If we compile
f5ce0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 with the SQLITE
f5cf0 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c _TEST macro set,
f5d00 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 then the follow
f5d10 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 ing block.** of
f5d20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 code will give u
f5d30 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f s the ability to
f5d40 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b simulate a disk
f5d50 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 I/O error. Thi
f5d60 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 s.** is used for
f5d70 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f testing the I/O
f5d80 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e recovery logic.
f5d90 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
f5da0 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
f5db0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
f5dc0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 _error_hit = 0;
f5dd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
f5de0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f tal number of I/
f5df0 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 O Errors */.SQLI
f5e00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
f5e10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 e3_io_error_hard
f5e20 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 hit = 0;
f5e30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e /* Number of non
f5e40 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a -benign errors *
f5e50 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f5e60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f5e70 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 r_pending = 0;
f5e80 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 /* Count d
f5e90 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f own to first I/O
f5ea0 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 error */.SQLITE
f5eb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
f5ec0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
f5ed0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a t = 0; /*
f5ee0 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 True if I/O err
f5ef0 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 ors persist */.S
f5f00 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f5f10 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
f5f20 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 enign = 0;
f5f30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 /* True if er
f5f40 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 rors are benign
f5f50 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f5f60 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 t sqlite3_diskfu
f5f70 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a ll_pending = 0;.
f5f80 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f5f90 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 qlite3_diskfull
f5fa0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d = 0;.#define Sim
f5fb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
f5fc0 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f gn(X) sqlite3_io
f5fd0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 _error_benign=(X
f5fe0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
f5ff0 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 teIOError(CODE)
f6000 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 \. if( (sqlite
f6010 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 3_io_error_persi
f6020 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f st && sqlite3_io
f6030 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 _error_hit) \.
f6040 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f || sqlite3_
f6050 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
f6060 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 -- == 1 ) \.
f6070 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 { loc
f6080 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 al_ioerr(); CODE
f6090 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
f60a0 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 local_ioerr(){.
f60b0 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 IOTRACE(("IOERR
f60c0 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 \n"));. sqlite3
f60d0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b _io_error_hit++;
f60e0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f . if( !sqlite3_
f60f0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
f6100 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 ) sqlite3_io_err
f6110 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a or_hardhit++;.}.
f6120 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
f6130 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f DiskfullError(CO
f6140 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c DE) \. if( sql
f6150 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
f6160 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 nding ){ \.
f6170 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b if( sqlite3_disk
f6180 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 full_pending ==
f6190 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 1 ){ \. lo
f61a0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 cal_ioerr(); \.
f61b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 sqlite3_di
f61c0 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 skfull = 1; \.
f61d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f sqlite3_io_
f61e0 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c error_hit = 1; \
f61f0 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a . CODE; \.
f6200 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 }else{ \.
f6210 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
f6220 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b kfull_pending--;
f6230 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d \. } \. }
f6240 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 .#else.#define S
f6250 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
f6260 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 nign(X).#define
f6270 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f6280 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c A).#define Simul
f6290 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 ateDiskfullError
f62a0 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (A).#endif../*.*
f62b0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 * When testing,
f62c0 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 keep a count of
f62d0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 the number of op
f62e0 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 en files..*/.#if
f62f0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
f6300 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f6310 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 qlite3_open_file
f6320 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 _count = 0;.#def
f6330 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 ine OpenCounter(
f6340 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e X) sqlite3_open
f6350 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 _file_count+=(X)
f6360 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
f6370 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 penCounter(X).#e
f6380 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 ndif..#endif /*
f6390 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d !defined(_OS_COM
f63a0 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a MON_H_) */../***
f63b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
f63c0 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a of os_common.h *
f63d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f63e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
f6400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
f6410 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
f6420 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 left off in os_w
f6430 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
f6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
f6450 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 ** Some microsof
f6460 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b t compilers lack
f6470 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e this definition
f6480 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 ..*/.#ifndef INV
f6490 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f64a0 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e UTES.# define IN
f64b0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
f64c0 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 BUTES ((DWORD)-1
f64d0 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ) .#endif../*.**
f64e0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 Determine if we
f64f0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
f6500 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 h WindowsCE - wh
f6510 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a ich has a much.*
f6520 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a * reduced API..*
f6530 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
f6540 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 WINCE.# define A
f6550 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 reFileApisANSI()
f6560 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72 6d 1.# define Form
f6570 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c 63 atMessageW(a,b,c
f6580 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e 64 ,d,e,f,g) 0.#end
f6590 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20 if../*.** WinCE
f65a0 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70 lacks native sup
f65b0 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f port for file lo
f65c0 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65 cking so we have
f65d0 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77 to fake it.** w
f65e0 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66 ith some code of
f65f0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 our own..*/.#if
f6600 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f6610 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
f6620 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e winceLock {. in
f6630 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 t nReaders;
f6640 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
f6650 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 eader locks obta
f6660 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
f6670 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a Pending; /*
f6680 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e Indicates a pen
f6690 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 ding lock has be
f66a0 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 en obtained */.
f66b0 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b BOOL bReserved;
f66c0 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 /* Indicate
f66d0 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 s a reserved loc
f66e0 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 k has been obtai
f66f0 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 ned */. BOOL bE
f6700 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 xclusive; /*
f6710 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 Indicates an exc
f6720 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 lusive lock has
f6730 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f been obtained */
f6740 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 .} winceLock;.#e
f6750 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
f6760 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 winFile structur
f6770 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 e is a subclass
f6780 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a of sqlite3_file*
f6790 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 specific to the
f67a0 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62 win32.** portab
f67b0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a ility layer..*/.
f67c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 typedef struct w
f67d0 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a inFile winFile;.
f67e0 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b struct winFile {
f67f0 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
f6800 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
f6810 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20 thod;/* Must be
f6820 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c first */. HANDL
f6830 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 E h;
f6840 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
f6850 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 accessing the f
f6860 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ile */. unsigne
f6870 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b d char locktype;
f6880 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b /* Type of lock
f6890 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 currently held
f68a0 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a on this file */.
f68b0 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f short sharedLo
f68c0 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e ckByte; /* Ran
f68d0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74 domly chosen byt
f68e0 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 72 e used as a shar
f68f0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 4f ed lock */. DWO
f6900 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 RD lastErrno;
f6910 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64 /* The Wind
f6920 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 ows errno from t
f6930 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f he last I/O erro
f6940 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 63 r */. DWORD sec
f6950 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 2f torSize; /
f6960 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 * Sector size of
f6970 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c 65 the device file
f6980 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 51 is on */.#if SQ
f6990 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
f69a0 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f 6e WCHAR *zDeleteOn
f69b0 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 Close; /* Name
f69c0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
f69d0 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 2a e when closing *
f69e0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 65 /. HANDLE hMute
f69f0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d x; /* M
f6a00 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f 6e utex used to con
f6a10 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 73 trol access to s
f6a20 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a hared lock */ .
f6a30 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 64 HANDLE hShared
f6a40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 ; /* Sha
f6a50 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65 red memory segme
f6a60 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b nt used for lock
f6a70 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ing */. winceLo
f6a80 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 20 ck local;
f6a90 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 6e /* Locks obtain
f6aa0 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 61 ed by this insta
f6ab0 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 2a nce of winFile *
f6ac0 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a 73 /. winceLock *s
f6ad0 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 47 hared; /* G
f6ae0 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f 63 lobal shared loc
f6af0 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 k memory for the
f6b00 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 66 file */.#endif
f6b10 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 .};../*.** Forwa
f6b20 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a 2a rd prototypes..*
f6b30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
f6b40 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 SectorSize(.
f6b50 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f6b60 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 s,. const cha
f6b70 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 r *zRelative
f6b80 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e /* UTF-8 file n
f6b90 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ame */.);../*.**
f6ba0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
f6bb0 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d ariable is (norm
f6bc0 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61 ally) set once a
f6bd0 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73 nd never changes
f6be0 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20 .** thereafter.
f6bf0 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 It records whet
f6c00 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e her the operatin
f6c10 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 g system is Win9
f6c20 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 5.** or WinNT..*
f6c30 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74 *.** 0: Operat
f6c40 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f ing system unkno
f6c50 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72 wn..** 1: Oper
f6c60 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 ating system is
f6c70 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f Win95..** 2: O
f6c80 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f6c90 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 is WinNT..**.**
f6ca0 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69 In order to faci
f6cb0 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f litate testing o
f6cc0 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d n a WinNT system
f6cd0 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75 , the test fixtu
f6ce0 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c re.** can manual
f6cf0 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75 ly set this valu
f6d00 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74 e to 1 to emulat
f6d10 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72 e Win98 behavior
f6d20 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f6d30 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
f6d40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
f6d50 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73 s_type = 0;.#els
f6d60 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c e.static int sql
f6d70 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 ite3_os_type = 0
f6d80 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
f6d90 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e Return true (non
f6da0 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 -zero) if we are
f6db0 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 running under W
f6dc0 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e inNT, Win2K, Win
f6dd0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e XP,.** or WinCE.
f6de0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 Return false (
f6df0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c zero) for Win95,
f6e00 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 Win98, or WinME
f6e10 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 ..**.** Here is
f6e20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f an interesting o
f6e30 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e bservation: Win
f6e40 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 95, Win98, and W
f6e50 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 inME lack.** the
f6e60 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 LockFileEx() AP
f6e70 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 I. But we can s
f6e80 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 till statically
f6e90 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 link against tha
f6ea0 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 t.** API as long
f6eb0 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c as we don't cal
f6ec0 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e l it when runnin
f6ed0 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 g Win95/98/ME.
f6ee0 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 A call to.** thi
f6ef0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
f6f00 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 d to determine i
f6f10 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 f the host is Wi
f6f20 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 n95/98/ME or.**
f6f30 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 WinNT/2K/XP so t
f6f40 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 hat we will know
f6f50 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
f6f60 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 we can safely ca
f6f70 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 ll.** the LockFi
f6f80 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 leEx() API..*/.#
f6f90 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f6fa0 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 CE.# define isNT
f6fb0 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 () (1).#else.
f6fc0 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28 static int isNT(
f6fd0 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73 void){. if( s
f6fe0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d qlite3_os_type==
f6ff0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 0 ){. OSVER
f7000 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a SIONINFO sInfo;.
f7010 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 sInfo.dwOS
f7020 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 VersionInfoSize
f7030 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b = sizeof(sInfo);
f7040 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f . GetVersio
f7050 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 nEx(&sInfo);.
f7060 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 sqlite3_os_ty
f7070 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 pe = sInfo.dwPla
f7080 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 tformId==VER_PLA
f7090 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f TFORM_WIN32_NT ?
f70a0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 2 : 1;. }.
f70b0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
f70c0 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d _os_type==2;. }
f70d0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
f70e0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f E_OS_WINCE */../
f70f0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 *.** Convert a U
f7100 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d TF-8 string to m
f7110 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f7120 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a (UTF-16?). .**.
f7130 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
f7140 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
f7150 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
f7160 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f from malloc..*/
f7170 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75 .static WCHAR *u
f7180 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e tf8ToUnicode(con
f7190 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
f71a0 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72 me){. int nChar
f71b0 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65 ;. WCHAR *zWide
f71c0 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68 Filename;.. nCh
f71d0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
f71e0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
f71f0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f7200 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 -1, NULL, 0);.
f7210 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 zWideFilename =
f7220 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69 malloc( nChar*si
f7230 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61 zeof(zWideFilena
f7240 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 me[0]) );. if(
f7250 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30 zWideFilename==0
f7260 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
f7270 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 ;. }. nChar =
f7280 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 MultiByteToWideC
f7290 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 har(CP_UTF8, 0,
f72a0 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a zFilename, -1, z
f72b0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43 WideFilename, nC
f72c0 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61 har);. if( nCha
f72d0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 r==0 ){. free
f72e0 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b (zWideFilename);
f72f0 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 . zWideFilena
f7300 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 me = 0;. }. re
f7310 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61 turn zWideFilena
f7320 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e me;.}../*.** Con
f7330 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 vert microsoft u
f7340 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e nicode to UTF-8.
f7350 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
f7360 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
f7370 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e ing is.** obtain
f7380 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
f7390 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
f73a0 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 *unicodeToUtf8(
f73b0 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 const WCHAR *zWi
f73c0 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 deFilename){. i
f73d0 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 nt nByte;. char
f73e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 *zFilename;..
f73f0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 nByte = WideChar
f7400 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 ToMultiByte(CP_U
f7410 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c TF8, 0, zWideFil
f7420 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c ename, -1, 0, 0,
f7430 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 0, 0);. zFilen
f7440 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 ame = malloc( nB
f7450 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 yte );. if( zFi
f7460 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 lename==0 ){.
f7470 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
f7480 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
f7490 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f rToMultiByte(CP_
f74a0 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 UTF8, 0, zWideFi
f74b0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c lename, -1, zFil
f74c0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 ename, nByte,.
f74d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f74e0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 0, 0
f74f0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d );. if( nByte =
f7500 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 = 0 ){. free(
f7510 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 zFilename);.
f7520 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 zFilename = 0;.
f7530 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c }. return zFil
f7540 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ename;.}../*.**
f7550 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20 Convert an ansi
f7560 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 string to micros
f7570 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73 oft unicode, bas
f7580 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 ed on the.** cur
f7590 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65 rent codepage se
f75a0 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 ttings for file
f75b0 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 apis..** .** Spa
f75c0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
f75d0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
f75e0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 s obtained.** fr
f75f0 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 om malloc..*/.st
f7600 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73 atic WCHAR *mbcs
f7610 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 ToUnicode(const
f7620 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
f7630 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
f7640 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c WCHAR *zMbcsFil
f7650 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 ename;. int cod
f7660 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 epage = AreFileA
f7670 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 pisANSI() ? CP_A
f7680 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a CP : CP_OEMCP;..
f7690 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 nByte = MultiB
f76a0 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f yteToWideChar(co
f76b0 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 depage, 0, zFile
f76c0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30 name, -1, NULL,0
f76d0 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b )*sizeof(WCHAR);
f76e0 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 . zMbcsFilename
f76f0 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
f7700 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c *sizeof(zMbcsFil
f7710 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 ename[0]) );. i
f7720 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 f( zMbcsFilename
f7730 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f7740 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 n 0;. }. nByte
f7750 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 = MultiByteToWi
f7760 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c deChar(codepage,
f7770 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
f7780 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 1, zMbcsFilename
f7790 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 , nByte);. if(
f77a0 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nByte==0 ){.
f77b0 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61 free(zMbcsFilena
f77c0 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69 me);. zMbcsFi
f77d0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a lename = 0;. }.
f77e0 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69 return zMbcsFi
f77f0 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lename;.}../*.**
f7800 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f Convert microso
f7810 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75 ft unicode to mu
f7820 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 ltibyte characte
f7830 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20 r string, based
f7840 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73 on the.** user's
f7850 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a Ansi codepage..
f7860 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 **.** Space to h
f7870 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
f7880 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
f7890 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c ned from.** mall
f78a0 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oc()..*/.static
f78b0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d char *unicodeToM
f78c0 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20 bcs(const WCHAR
f78d0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b *zWideFilename){
f78e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 . int nByte;.
f78f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b char *zFilename;
f7900 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 . int codepage
f7910 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 = AreFileApisANS
f7920 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 I() ? CP_ACP : C
f7930 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 P_OEMCP;.. nByt
f7940 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 e = WideCharToMu
f7950 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65 ltiByte(codepage
f7960 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 , 0, zWideFilena
f7970 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c me, -1, 0, 0, 0,
f7980 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 0);. zFilename
f7990 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
f79a0 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e );. if( zFilen
f79b0 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ame==0 ){. re
f79c0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 turn 0;. }. nB
f79d0 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f yte = WideCharTo
f79e0 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 MultiByte(codepa
f79f0 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 ge, 0, zWideFile
f7a00 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e name, -1, zFilen
f7a10 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20 ame, nByte,.
f7a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7a30 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b 0, 0);
f7a40 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 . if( nByte ==
f7a50 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46 0 ){. free(zF
f7a60 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 ilename);. zF
f7a70 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
f7a80 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e . return zFilen
f7a90 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f ame;.}../*.** Co
f7aa0 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20 nvert multibyte
f7ab0 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 character string
f7ac0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 to UTF-8. Spac
f7ad0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a e to hold the.**
f7ae0 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 returned string
f7af0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
f7b00 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 m malloc()..*/.S
f7b10 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
f7b20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 sqlite3_win32_mb
f7b30 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74 cs_to_utf8(const
f7b40 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f7b50 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 ){. char *zFile
f7b60 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41 nameUtf8;. WCHA
f7b70 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 R *zTmpWide;..
f7b80 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54 zTmpWide = mbcsT
f7b90 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 oUnicode(zFilena
f7ba0 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 me);. if( zTmpW
f7bb0 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ide==0 ){. re
f7bc0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 turn 0;. }. zF
f7bd0 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e ilenameUtf8 = un
f7be0 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70 icodeToUtf8(zTmp
f7bf0 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 Wide);. free(zT
f7c00 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 mpWide);. retur
f7c10 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b n zFilenameUtf8;
f7c20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
f7c30 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69 t UTF-8 to multi
f7c40 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
f7c50 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f tring. Space to
f7c60 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65 hold the .** re
f7c70 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
f7c80 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
f7c90 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
f7ca0 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d ic char *utf8ToM
f7cb0 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a bcs(const char *
f7cc0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 zFilename){. ch
f7cd0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 ar *zFilenameMbc
f7ce0 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 s;. WCHAR *zTmp
f7cf0 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 Wide;.. zTmpWid
f7d00 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 e = utf8ToUnicod
f7d10 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f7d20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 if( zTmpWide==0
f7d30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
f7d40 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 . }. zFilename
f7d50 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f Mbcs = unicodeTo
f7d60 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a Mbcs(zTmpWide);.
f7d70 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 free(zTmpWide)
f7d80 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 ;. return zFile
f7d90 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66 nameMbcs;.}..#if
f7da0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f7db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
f7e00 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74 his section cont
f7e10 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69 ains code for Wi
f7e20 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a nCE only..*/./*.
f7e30 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65 ** WindowsCE doe
f7e40 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63 s not have a loc
f7e50 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f altime() functio
f7e60 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a n. So create a.
f7e70 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a ** substitute..*
f7e80 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63 /.struct tm *__c
f7e90 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63 decl localtime(c
f7ea0 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a onst time_t *t).
f7eb0 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 {. static struc
f7ec0 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49 t tm y;. FILETI
f7ed0 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 ME uTm, lTm;. S
f7ee0 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 YSTEMTIME pTm;.
f7ef0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 sqlite3_int64 t
f7f00 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a 64;. t64 = *t;.
f7f10 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31 t64 = (t64 + 1
f7f20 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30 1644473600)*1000
f7f30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 0000;. uTm.dwLo
f7f40 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57 4f wDateTime = (DWO
f7f50 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46 46 RD)(t64 & 0xFFFF
f7f60 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77 48 FFFF);. uTm.dwH
f7f70 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44 57 ighDateTime= (DW
f7f80 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29 3b ORD)(t64 >> 32);
f7f90 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 . FileTimeToLoc
f7fa0 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c alFileTime(&uTm,
f7fb0 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d &lTm);. FileTim
f7fc0 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c eToSystemTime(&l
f7fd0 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d Tm,&pTm);. y.tm
f7fe0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 _year = pTm.wYea
f7ff0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d r - 1900;. y.tm
f8000 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 _mon = pTm.wMont
f8010 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 h - 1;. y.tm_wd
f8020 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 ay = pTm.wDayOfW
f8030 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 eek;. y.tm_mday
f8040 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 = pTm.wDay;. y
f8050 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 .tm_hour = pTm.w
f8060 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e Hour;. y.tm_min
f8070 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a = pTm.wMinute;.
f8080 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d y.tm_sec = pTm
f8090 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 .wSecond;. retu
f80a0 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 rn &y;.}../* Thi
f80b0 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 s will never be
f80c0 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69 called, but defi
f80d0 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 ned to make the
f80e0 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a code compile */.
f80f0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50 #define GetTempP
f8100 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 athA(a,b)..#defi
f8110 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c ne LockFile(a,b,
f8120 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e c,d,e) win
f8130 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 ceLockFile(&a, b
f8140 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 , c, d, e).#defi
f8150 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c ne UnlockFile(a,
f8160 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e b,c,d,e) win
f8170 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c ceUnlockFile(&a,
f8180 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 b, c, d, e).#de
f8190 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 fine LockFileEx(
f81a0 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 a,b,c,d,e,f) w
f81b0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 inceLockFileEx(&
f81c0 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 a, b, c, d, e, f
f81d0 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c )..#define HANDL
f81e0 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 E_TO_WINFILE(a)
f81f0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 (winFile*)&((cha
f8200 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66 73 r*)a)[-(int)offs
f8210 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d etof(winFile,h)]
f8220 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
f8230 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 a lock on the ha
f8240 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 ndle h.*/.static
f8250 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 void winceMutex
f8260 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 Acquire(HANDLE h
f8270 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 ){. DWORD dwEr
f8280 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 r;. do {.
f8290 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 dwErr = WaitForS
f82a0 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 ingleObject(h, I
f82b0 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 NFINITE);. } w
f82c0 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 hile (dwErr != W
f82d0 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 AIT_OBJECT_0 &&
f82e0 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 dwErr != WAIT_AB
f82f0 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a ANDONED);.}./*.*
f8300 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b * Release a lock
f8310 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69 6e acquired by win
f8320 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 ceMutexAcquire()
f8330 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 .*/.#define winc
f8340 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 eMutexRelease(h)
f8350 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 ReleaseMutex(h)
f8360 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 ../*.** Create t
f8370 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 he mutex and sha
f8380 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 red memory used
f8390 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 for locking in t
f83a0 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 he file.** descr
f83b0 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 iptor pFile.*/.s
f83c0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 tatic BOOL wince
f83d0 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 CreateLock(const
f83e0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f83f0 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 , winFile *pFile
f8400 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b ){. WCHAR *zTok
f8410 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 ;. WCHAR *zName
f8420 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 = utf8ToUnicode
f8430 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 (zFilename);. B
f8440 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 OOL bInit = TRUE
f8450 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.. /* Initiali
f8460 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 ze the local loc
f8470 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d kdata */. ZeroM
f8480 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f emory(&pFile->lo
f8490 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c cal, sizeof(pFil
f84a0 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f e->local));.. /
f84b0 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 * Replace the ba
f84c0 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 ckslashes from t
f84d0 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 he filename and
f84e0 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a lowercase it. *
f84f0 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 * to derive a mu
f8500 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a tex name. */. z
f8510 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 Tok = CharLowerW
f8520 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 (zName);. for (
f8530 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a ;*zTok;zTok++){.
f8540 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d if (*zTok ==
f8550 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 '\\') *zTok = '
f8560 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 _';. }.. /* Cr
f8570 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 eate/open the na
f8580 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 med mutex */. p
f8590 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 File->hMutex = C
f85a0 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c reateMutexW(NULL
f85b0 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b , FALSE, zName);
f85c0 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 . if (!pFile->h
f85d0 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c Mutex){. pFil
f85e0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
f85f0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f8600 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a free(zName);.
f8610 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 return FALSE
f8620 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
f8630 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 ire the mutex be
f8640 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 fore continuing
f8650 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 */. winceMutexA
f8660 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
f8670 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 utex);. . /* S
f8680 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f ince the names o
f8690 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c f named mutexes,
f86a0 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c semaphores, fil
f86b0 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 e mappings etc a
f86c0 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 re . ** case-se
f86d0 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 nsitive, take ad
f86e0 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 vantage of that
f86f0 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 by uppercasing t
f8700 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 he mutex name.
f8710 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 ** and using tha
f8720 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20 t as the shared
f8730 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 filemapping name
f8740 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 .. */. CharUpp
f8750 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 erW(zName);. pF
f8760 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 ile->hShared = C
f8770 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 reateFileMapping
f8780 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 W(INVALID_HANDLE
f8790 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 _VALUE, NULL,.
f87a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f87b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f87c0 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 PAGE_READWR
f87d0 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 ITE, 0, sizeof(w
f87e0 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 inceLock),.
f87f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8810 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f zName); .. /
f8820 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 * Set a flag tha
f8830 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 t indicates we'r
f8840 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 e the first to c
f8850 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 reate the memory
f8860 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 so it . ** mus
f8870 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 t be zero-initia
f8880 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 lized */. if (G
f8890 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d etLastError() ==
f88a0 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 ERROR_ALREADY_E
f88b0 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 XISTS){. bIni
f88c0 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a t = FALSE;. }..
f88d0 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a free(zName);..
f88e0 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 /* If we succe
f88f0 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 eded in making t
f8900 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f8910 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e handle, map it.
f8920 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d */. if (pFile-
f8930 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 >hShared){. p
f8940 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 File->shared = (
f8950 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 winceLock*)MapVi
f8960 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e ewOfFile(pFile->
f8970 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 hShared, .
f8980 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f FILE_MAP_
f8990 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 READ|FILE_MAP_WR
f89a0 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f ITE, 0, 0, sizeo
f89b0 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 f(winceLock));.
f89c0 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 /* If mapping
f89d0 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 failed, close t
f89e0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f89f0 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 handle and eras
f8a00 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 e it */. if (
f8a10 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b !pFile->shared){
f8a20 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
f8a30 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
f8a40 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 tError();.
f8a50 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f8a60 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 e->hShared);.
f8a70 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 pFile->hShare
f8a80 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a d = NULL;. }.
f8a90 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 }.. /* If sha
f8aa0 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 red memory could
f8ab0 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c not be created,
f8ac0 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 then close the
f8ad0 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a mutex and fail *
f8ae0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 /. if (pFile->h
f8af0 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b Shared == NULL){
f8b00 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 . winceMutexR
f8b10 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
f8b20 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 utex);. Close
f8b30 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d Handle(pFile->hM
f8b40 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 utex);. pFile
f8b50 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b ->hMutex = NULL;
f8b60 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 . return FALS
f8b70 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 E;. }. . /* I
f8b80 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 nitialize the sh
f8b90 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 ared memory if w
f8ba0 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f e're supposed to
f8bb0 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 */. if (bInit)
f8bc0 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 {. ZeroMemor
f8bd0 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c y(pFile->shared,
f8be0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 sizeof(winceLoc
f8bf0 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 k));. }.. winc
f8c00 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
f8c10 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
f8c20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a return TRUE;.}..
f8c30 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 /*.** Destroy th
f8c40 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c e part of winFil
f8c50 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 e that deals wit
f8c60 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f h wince locks.*/
f8c70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
f8c80 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 ceDestroyLock(wi
f8c90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 nFile *pFile){.
f8ca0 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 if (pFile->hMut
f8cb0 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 ex){. /* Acqu
f8cc0 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f ire the mutex */
f8cd0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 . winceMutexA
f8ce0 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
f8cf0 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 utex);.. /* T
f8d00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
f8d10 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 cks should proba
f8d20 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 bly assert in de
f8d30 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 bug mode, but th
f8d40 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f ey. are to
f8d50 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 cleanup in case
f8d60 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 any locks remai
f8d70 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 ned open */.
f8d80 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
f8d90 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 .nReaders){.
f8da0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8db0 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 >nReaders --;.
f8dc0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c }. if (pFil
f8dd0 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 e->local.bReserv
f8de0 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ed){. pFile
f8df0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 ->shared->bReser
f8e00 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ved = FALSE;.
f8e10 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 }. if (pFile
f8e20 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 ->local.bPending
f8e30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f8e40 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 shared->bPending
f8e50 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a = FALSE;. }.
f8e60 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c if (pFile->l
f8e70 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 ocal.bExclusive)
f8e80 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 {. pFile->s
f8e90 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
f8ea0 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d e = FALSE;. }
f8eb0 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 .. /* De-refe
f8ec0 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 rence and close
f8ed0 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 our copy of the
f8ee0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 shared memory ha
f8ef0 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 ndle */. Unma
f8f00 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c pViewOfFile(pFil
f8f10 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 e->shared);.
f8f20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f8f30 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 e->hShared);..
f8f40 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 /* Done with t
f8f50 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 he mutex */.
f8f60 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 winceMutexReleas
f8f70 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
f8f80 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 ; . CloseH
f8f90 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 andle(pFile->hMu
f8fa0 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d tex);. pFile-
f8fb0 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a >hMutex = NULL;.
f8fc0 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e }.}../* .** An
f8fd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
f8fe0 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 of the LockFile(
f8ff0 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 ) API of windows
f9000 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 for wince.*/.st
f9010 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c atic BOOL winceL
f9020 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c ockFile(. HANDL
f9030 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f E *phFile,. DWO
f9040 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c RD dwFileOffsetL
f9050 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 ow,. DWORD dwFi
f9060 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 leOffsetHigh,.
f9070 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 DWORD nNumberOfB
f9080 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 ytesToLockLow,.
f9090 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
f90a0 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a BytesToLockHigh.
f90b0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 ){. winFile *pF
f90c0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f ile = HANDLE_TO_
f90d0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b WINFILE(phFile);
f90e0 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 . BOOL bReturn
f90f0 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 = FALSE;.. UNUS
f9100 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46 ED_PARAMETER(dwF
f9110 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a ileOffsetHigh);.
f9120 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f9130 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 ER(nNumberOfByte
f9140 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 sToLockHigh);..
f9150 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 if (!pFile->hMu
f9160 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 tex) return TRUE
f9170 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 ;. winceMutexAc
f9180 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 quire(pFile->hMu
f9190 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74 tex);.. /* Want
f91a0 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ing an exclusive
f91b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 28 lock? */. if (
f91c0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f91d0 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f91e0 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 _FIRST. &&
f91f0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f9200 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f oLockLow == (DWO
f9210 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b RD)SHARED_SIZE){
f9220 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9230 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 shared->nReaders
f9240 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d 3e == 0 && pFile->
f9250 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
f9260 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 ve == 0){.
f9270 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f9280 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 bExclusive = TRU
f9290 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d E;. pFile-
f92a0 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 >local.bExclusiv
f92b0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 e = TRUE;.
f92c0 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f92d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f92e0 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e 6c Want a read-onl
f92f0 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 y lock? */. els
f9300 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 e if (dwFileOffs
f9310 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 etLow == (DWORD)
f9320 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a SHARED_FIRST &&.
f9330 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 nNumb
f9340 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f9350 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
f9360 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
f9370 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 >bExclusive == 0
f9380 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f9390 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b local.nReaders +
f93a0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 +;. if (pFi
f93b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 le->local.nReade
f93c0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 rs == 1){.
f93d0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f93e0 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 >nReaders ++;.
f93f0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
f9400 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f9410 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
f9420 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f a pending lock?
f9430 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f9440 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f9450 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 = (DWORD)PENDING
f9460 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 _BYTE && nNumber
f9470 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 OfBytesToLockLow
f9480 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20 49 == 1){. /* I
f9490 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f 63 f no pending loc
f94a0 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 69 k has been acqui
f94b0 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 72 red, then acquir
f94c0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 e it */. if (
f94d0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f94e0 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b 0a Pending == 0) {.
f94f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f9500 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
f9510 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c TRUE;. pFil
f9520 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
f9530 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 g = TRUE;.
f9540 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a bReturn = TRUE;.
f9550 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
f9560 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64 20 Want a reserved
f9570 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 lock? */. else
f9580 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f9590 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 Low == (DWORD)RE
f95a0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e SERVED_BYTE && n
f95b0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f95c0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 ockLow == 1){.
f95d0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 if (pFile->sha
f95e0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d red->bReserved =
f95f0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 = 0) {. pFi
f9600 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 le->shared->bRes
f9610 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 erved = TRUE;.
f9620 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
f9630 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 .bReserved = TRU
f9640 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f9650 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f9660 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 }.. winceMutex
f9670 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
f9680 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
f9690 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a bReturn;.}../*.
f96a0 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ** An implementa
f96b0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f tion of the Unlo
f96c0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69 ckFile API of wi
f96d0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
f96e0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
f96f0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a inceUnlockFile(.
f9700 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 HANDLE *phFile
f9710 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 ,. DWORD dwFile
f9720 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f OffsetLow,. DWO
f9730 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 RD dwFileOffsetH
f9740 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 igh,. DWORD nNu
f9750 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c mberOfBytesToUnl
f9760 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 ockLow,. DWORD
f9770 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9780 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 UnlockHigh.){.
f9790 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
f97a0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 HANDLE_TO_WINFI
f97b0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f LE(phFile);. BO
f97c0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c OL bReturn = FAL
f97d0 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 SE;.. UNUSED_PA
f97e0 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 RAMETER(dwFileOf
f97f0 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 fsetHigh);. UNU
f9800 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e SED_PARAMETER(nN
f9810 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9820 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 lockHigh);.. if
f9830 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 (!pFile->hMutex
f9840 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 ) return TRUE;.
f9850 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 winceMutexAcqui
f9860 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 re(pFile->hMutex
f9870 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 );.. /* Releasi
f9880 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b ng a reader lock
f9890 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 or an exclusive
f98a0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 lock */. if (d
f98b0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f98c0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f98d0 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20 44 FIRST){. /* D
f98e0 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65 78 id we have an ex
f98f0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f clusive lock? */
f9900 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9910 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 local.bExclusive
f9920 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
f9930 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9940 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 UnlockLow == (DW
f9950 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 ORD)SHARED_SIZE)
f9960 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
f9970 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 ocal.bExclusive
f9980 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 = FALSE;. p
f9990 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 File->shared->bE
f99a0 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 xclusive = FALSE
f99b0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
f99c0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 = TRUE;. }..
f99d0 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 /* Did we jus
f99e0 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72 20 t have a reader
f99f0 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 lock? */. els
f9a00 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 e if (pFile->loc
f9a10 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 al.nReaders){.
f9a20 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 assert(nNumb
f9a30 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 erOfBytesToUnloc
f9a40 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 kLow == (DWORD)S
f9a50 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e 4e HARED_SIZE || nN
f9a60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9a70 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a 20 lockLow == 1);.
f9a80 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f9a90 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 l.nReaders --;.
f9aa0 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e if (pFile->
f9ab0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d local.nReaders =
f9ac0 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 = 0). {.
f9ad0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f9ae0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b ed->nReaders --;
f9af0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
f9b00 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
f9b10 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
f9b20 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 69 eleasing a pendi
f9b30 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 ng lock */. els
f9b40 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 e if (dwFileOffs
f9b50 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 etLow == (DWORD)
f9b60 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 PENDING_BYTE &&
f9b70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9b80 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b UnlockLow == 1){
f9b90 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9ba0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b local.bPending){
f9bb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
f9bc0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46 cal.bPending = F
f9bd0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c ALSE;. pFil
f9be0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
f9bf0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ing = FALSE;.
f9c00 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 bReturn = TRU
f9c10 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f E;. }. }. /
f9c20 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 * Releasing a re
f9c30 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 served lock */.
f9c40 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 else if (dwFile
f9c50 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 OffsetLow == (DW
f9c60 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54 ORD)RESERVED_BYT
f9c70 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
f9c80 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f9c90 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 = 1){. if (pF
f9ca0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 ile->local.bRese
f9cb0 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 rved) {. pF
f9cc0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 ile->local.bRese
f9cd0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 rved = FALSE;.
f9ce0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f9cf0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 d->bReserved = F
f9d00 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 ALSE;. bRet
f9d10 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f9d20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 }. }.. winceMu
f9d30 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 texRelease(pFile
f9d40 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 ->hMutex);. ret
f9d50 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a urn bReturn;.}..
f9d60 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 /*.** An impleme
f9d70 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c ntation of the L
f9d80 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 ockFileEx() API
f9d90 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 of windows for w
f9da0 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 ince.*/.static B
f9db0 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c OOL winceLockFil
f9dc0 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 eEx(. HANDLE *p
f9dd0 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 hFile,. DWORD d
f9de0 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 wFlags,. DWORD
f9df0 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 dwReserved,. DW
f9e00 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
f9e10 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 esToLockLow,. D
f9e20 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
f9e30 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 tesToLockHigh,.
f9e40 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 LPOVERLAPPED lp
f9e50 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 Overlapped.){.
f9e60 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f9e70 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20 20 (dwReserved);.
f9e80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f9e90 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 (nNumberOfBytesT
f9ea0 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 2f oLockHigh);.. /
f9eb0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
f9ec0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 wants a shared r
f9ed0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 ead lock, forwar
f9ee0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a d this call. **
f9ef0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c to winceLockFil
f9f00 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 e */. if (lpOve
f9f10 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 rlapped->Offset
f9f20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f9f30 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 _FIRST &&.
f9f40 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0a dwFlags == 1 &&.
f9f50 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 nNumberOfB
f9f60 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d ytesToLockLow ==
f9f70 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53 (DWORD)SHARED_S
f9f80 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e IZE){. return
f9f90 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 winceLockFile(p
f9fa0 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 hFile, SHARED_FI
f9fb0 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 RST, 0, 1, 0);.
f9fc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 }. return FALS
f9fd0 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f E;.}./*.** End o
f9fe0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f f the special co
f9ff0 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a de for wince.***
fa000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 **********/.#end
fa050 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
fa060 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a WINCE */../*****
fa070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa0b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
fa0c0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f next group of ro
fa0d0 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 utines implement
fa0e0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
fa0f0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 specified.** by
fa100 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
fa110 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
fa120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fa160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
fa170 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 ./*.** Close a f
fa180 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 ile..**.** It is
fa190 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 reported that a
fa1a0 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f n attempt to clo
fa1b0 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 se a handle migh
fa1c0 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 t sometimes.** f
fa1d0 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 ail. This is a
fa1e0 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c very unreasonabl
fa1f0 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 e result, but wi
fa200 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f ndows is notorio
fa210 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 us.** for being
fa220 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 unreasonable so
fa230 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 I do not doubt t
fa240 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 hat it might hap
fa250 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 pen. If.** the
fa260 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 close fails, we
fa270 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 pause for 100 mi
fa280 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 lliseconds and t
fa290 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a ry again. As.**
fa2a0 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 many as MX_CLOS
fa2b0 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 E_ATTEMPT attemp
fa2c0 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 ts to close the
fa2d0 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 handle are made
fa2e0 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 before.** giving
fa2f0 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e up and returnin
fa300 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 g an error..*/.#
fa310 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f define MX_CLOSE_
fa320 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 ATTEMPT 3.static
fa330 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 int winClose(sq
fa340 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
fa350 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d . int rc, cnt =
fa360 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 0;. winFile *p
fa370 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
fa380 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 )id;.. assert(
fa390 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 id!=0 );. OSTRA
fa3a0 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 CE2("CLOSE %d\n"
fa3b0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 , pFile->h);. d
fa3c0 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 o{. rc = Clos
fa3d0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 eHandle(pFile->h
fa3e0 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d );. }while( rc=
fa3f0 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 =0 && ++cnt < MX
fa400 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 _CLOSE_ATTEMPT &
fa410 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 & (Sleep(100), 1
fa420 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f ) );.#if SQLITE_
fa430 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 OS_WINCE.#define
fa440 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f WINCE_DELETION_
fa450 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e ATTEMPTS 3. win
fa460 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 ceDestroyLock(pF
fa470 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c ile);. if( pFil
fa480 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 e->zDeleteOnClos
fa490 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 e ){. int cnt
fa4a0 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 = 0;. while(
fa4b0 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c 65 . Dele
fa4c0 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a teFileW(pFile->z
fa4d0 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d DeleteOnClose)==
fa4e0 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65 74 0. && Get
fa4f0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 FileAttributesW(
fa500 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e pFile->zDeleteOn
fa510 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66 Close)!=0xffffff
fa520 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20 63 ff . && c
fa530 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c nt++ < WINCE_DEL
fa540 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 ETION_ATTEMPTS.
fa550 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 ){. Sle
fa560 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 ep(100); /* Wai
fa570 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 t a little befor
fa580 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a e trying again *
fa590 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 /. }. free
fa5a0 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f (pFile->zDeleteO
fa5b0 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e nClose);. }.#en
fa5c0 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 dif. OpenCounte
fa5d0 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 r(-1);. return
fa5e0 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a rc ? SQLITE_OK :
fa5f0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d SQLITE_IOERR;.}
fa600 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 ../*.** Some mic
fa610 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 rosoft compilers
fa620 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e lack this defin
fa630 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ition..*/.#ifnde
fa640 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 f INVALID_SET_FI
fa650 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 LE_POINTER.# def
fa660 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f ine INVALID_SET_
fa670 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 FILE_POINTER ((D
fa680 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a WORD)-1).#endif.
fa690 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
fa6a0 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 from a file int
fa6b0 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 o a buffer. Ret
fa6c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
fa6d0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 all.** bytes we
fa6e0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 re read successf
fa6f0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f ully and SQLITE_
fa700 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e IOERR if anythin
fa710 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
fa720 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fa730 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 inRead(. sqlite
fa740 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
fa750 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
fa760 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 read from */. v
fa770 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
fa780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
fa790 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 te content into
fa7a0 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 this buffer */.
fa7b0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 int amt,
fa7c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
fa7d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
fa7e0 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 o read */. sqli
fa7f0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
fa800 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 /* Begin
fa810 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 reading at this
fa820 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c offset */.){. L
fa830 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 ONG upperBits =
fa840 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e (LONG)((offset>>
fa850 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 32) & 0x7fffffff
fa860 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 );. LONG lowerB
fa870 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 its = (LONG)(off
fa880 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66 set & 0xffffffff
fa890 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 );. DWORD rc;.
fa8a0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fa8b0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fa8c0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 DWORD error;.
fa8d0 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61 DWORD got;.. a
fa8e0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
fa8f0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
fa900 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
fa910 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f IOERR_READ);. O
fa920 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25 64 STRACE3("READ %d
fa930 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
fa940 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
fa950 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 cktype);. rc =
fa960 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 SetFilePointer(p
fa970 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 File->h, lowerBi
fa980 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 ts, &upperBits,
fa990 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 FILE_BEGIN);. i
fa9a0 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 f( rc==INVALID_S
fa9b0 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 ET_FILE_POINTER
fa9c0 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 && (error=GetLas
fa9d0 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 tError())!=NO_ER
fa9e0 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 ROR ){. pFile
fa9f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
faa00 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ror;. return
faa10 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d SQLITE_FULL;. }
faa20 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c 65 . if( !ReadFile
faa30 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c (pFile->h, pBuf,
faa40 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29 amt, &got, 0) )
faa50 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
faa60 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
faa70 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 Error();. ret
faa80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
faa90 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 _READ;. }. if(
faaa0 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 got==(DWORD)amt
faab0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
faac0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
faad0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 e{. /* Unread
faae0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 parts of the bu
faaf0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 ffer must be zer
fab00 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 o-filled */.
fab10 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 memset(&((char*)
fab20 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 pBuf)[got], 0, a
fab30 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 mt-got);. ret
fab40 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fab50 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d _SHORT_READ;. }
fab60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
fab70 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 data from a buff
fab80 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 er into a file.
fab90 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
faba0 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 K on success.**
fabb0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 or some other er
fabc0 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c ror code on fail
fabd0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
fabe0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 nt winWrite(. s
fabf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fac00 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
fac10 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a to write into *
fac20 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
fac30 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a pBuf, /*
fac40 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 The bytes to be
fac50 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
fac60 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
fac70 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
fac80 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
fac90 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ite */. sqlite3
faca0 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
facb0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
facc0 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 o the file to be
facd0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a gin writing at *
face0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 /.){. LONG uppe
facf0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 rBits = (LONG)((
fad00 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 offset>>32) & 0x
fad10 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 7fffffff);. LON
fad20 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c G lowerBits = (L
fad30 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 ONG)(offset & 0x
fad40 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f ffffffff);. DWO
fad50 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 RD rc;. winFile
fad60 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
fad70 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 le*)id;. DWORD
fad80 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77 error;. DWORD w
fad90 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 rote = 0;.. ass
fada0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fadb0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
fadc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
fadd0 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 ERR_WRITE);. Si
fade0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
fadf0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
fae00 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 E_FULL);. OSTRA
fae10 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f CE3("WRITE %d lo
fae20 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
fae30 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
fae40 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ype);. rc = Set
fae50 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
fae60 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
fae70 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
fae80 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
fae90 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
faea0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
faeb0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 (error=GetLastEr
faec0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 ror())!=NO_ERROR
faed0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
faee0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 astErrno = error
faef0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
faf00 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 ITE_FULL;. }.
faf10 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b assert( amt>0 );
faf20 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61 . while(. a
faf30 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63 mt>0. && (rc
faf40 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 69 = WriteFile(pFi
faf50 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 le->h, pBuf, amt
faf60 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30 , &wrote, 0))!=0
faf70 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30 . && wrote>0
faf80 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d . ){. amt -=
faf90 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 wrote;. pBuf
fafa0 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 = &((char*)pBuf
fafb0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 )[wrote];. }.
fafc0 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28 if( !rc || amt>(
fafd0 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20 int)wrote ){.
fafe0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
faff0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
fb000 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ();. return S
fb010 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a QLITE_FULL;. }.
fb020 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fb030 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
fb040 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 ncate an open fi
fb050 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 le to a specifie
fb060 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 d size.*/.static
fb070 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 int winTruncate
fb080 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
fb090 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
fb0a0 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20 nByte){. LONG
fb0b0 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e upperBits = (LON
fb0c0 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 G)((nByte>>32) &
fb0d0 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 0x7fffffff);.
fb0e0 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d LONG lowerBits =
fb0f0 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 (LONG)(nByte &
fb100 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 0xffffffff);. D
fb110 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 WORD rc;. winFi
fb120 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fb130 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fb140 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 D error;.. asse
fb150 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f rt( id!=0 );. O
fb160 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54 STRACE3("TRUNCAT
fb170 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
fb180 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a ile->h, nByte);.
fb190 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
fb1a0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
fb1b0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b IOERR_TRUNCATE);
fb1c0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 . rc = SetFileP
fb1d0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c ointer(pFile->h,
fb1e0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 lowerBits, &upp
fb1f0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 erBits, FILE_BEG
fb200 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 IN);. if( rc==I
fb210 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f NVALID_SET_FILE_
fb220 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f POINTER && (erro
fb230 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 r=GetLastError()
fb240 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 )!=NO_ERROR ){.
fb250 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
fb260 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 rno = error;.
fb270 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
fb280 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 OERR_TRUNCATE;.
fb290 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66 }. /* SetEndOf
fb2a0 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69 File will fail i
fb2b0 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74 f nByte is negat
fb2c0 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65 ive */. if( !Se
fb2d0 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 tEndOfFile(pFile
fb2e0 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c ->h) ){. pFil
fb2f0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
fb300 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fb310 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb320 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
fb330 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
fb340 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 LITE_OK;.}..#ifd
fb350 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
fb360 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e *.** Count the n
fb370 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e umber of fullsyn
fb380 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 cs and normal sy
fb390 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 ncs. This is us
fb3a0 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 ed to test.** th
fb3b0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c at syncs and ful
fb3c0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 lsyncs are occur
fb3d0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74 ing at the right
fb3e0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 times..*/.SQLIT
fb3f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
fb400 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3_sync_count = 0
fb410 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
fb420 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e sqlite3_fullsyn
fb430 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e c_count = 0;.#en
fb440 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 dif../*.** Make
fb450 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
fb460 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
fb470 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
fb480 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 ed to disk..*/.s
fb490 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e tatic int winSyn
fb4a0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a c(sqlite3_file *
fb4b0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a id, int flags){.
fb4c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e #ifndef SQLITE_N
fb4d0 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 O_SYNC. winFile
fb4e0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
fb4f0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
fb500 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 t( id!=0 );. OS
fb510 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 TRACE3("SYNC %d
fb520 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c lock=%d\n", pFil
fb530 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 e->h, pFile->loc
fb540 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20 ktype);.#else.
fb550 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
fb560 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 (id);.#endif.#if
fb570 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 ndef SQLITE_TEST
fb580 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
fb590 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73 TER(flags);.#els
fb5a0 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 e. if( flags &
fb5b0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
fb5c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
fb5d0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
fb5e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
fb5f0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 sync_count++;.#e
fb600 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20 ndif. /* If we
fb610 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
fb620 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 e SQLITE_NO_SYNC
fb630 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 flag, then sync
fb640 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f ing is a. ** no
fb650 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 -op. */.#ifdef
fb660 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 SQLITE_NO_SYNC.
fb670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb680 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 _OK;.#else. if(
fb690 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 FlushFileBuffer
fb6a0 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 s(pFile->h) ){.
fb6b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb6c0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
fb6d0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
fb6e0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
fb6f0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 r();. return
fb700 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
fb710 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
fb720 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 * Determine the
fb730 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 current size of
fb740 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a a file in bytes.
fb750 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fb760 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 nFileSize(sqlite
fb770 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 3_file *id, sqli
fb780 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 te3_int64 *pSize
fb790 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72 ){. DWORD upper
fb7a0 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f Bits;. DWORD lo
fb7b0 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69 werBits;. winFi
fb7c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fb7d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fb7e0 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 D error;.. asse
fb7f0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 rt( id!=0 );. S
fb800 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 imulateIOError(r
fb810 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
fb820 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77 RR_FSTAT);. low
fb830 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65 erBits = GetFile
fb840 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26 Size(pFile->h, &
fb850 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66 upperBits);. if
fb860 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d ( (lowerBits =
fb870 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53 = INVALID_FILE_S
fb880 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28 65 IZE). && ((e
fb890 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
fb8a0 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52 ror()) != NO_ERR
fb8b0 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46 OR) ). {. pF
fb8c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
fb8d0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 error;. retu
fb8e0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fb8f0 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 FSTAT;. }. *pS
fb900 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33 ize = (((sqlite3
fb910 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74 73 _int64)upperBits
fb920 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 )<<32) + lowerBi
fb930 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ts;. return SQL
fb940 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
fb950 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
fb960 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e MMEDIATELY is un
fb970 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 defined on some
fb980 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e Windows systems.
fb990 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b .*/.#ifndef LOCK
fb9a0 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 FILE_FAIL_IMMEDI
fb9b0 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c ATELY.# define L
fb9c0 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d OCKFILE_FAIL_IMM
fb9d0 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 EDIATELY 1.#endi
fb9e0 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 f../*.** Acquire
fb9f0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a a reader lock..
fba00 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50 49 ** Different API
fba10 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 routines are ca
fba20 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f lled depending o
fba30 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 n whether or not
fba40 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39 this.** is Win9
fba50 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 5 or WinNT..*/.s
fba60 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61 tatic int getRea
fba70 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 dLock(winFile *p
fba80 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 File){. int res
fba90 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
fbaa0 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 {. OVERLAPPED
fbab0 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e ovlp;. ovlp.
fbac0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
fbad0 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e FIRST;. ovlp.
fbae0 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a OffsetHigh = 0;.
fbaf0 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20 ovlp.hEvent
fbb00 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c = 0;. res = L
fbb10 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d ockFileEx(pFile-
fbb20 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 >h, LOCKFILE_FAI
fbb30 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20 L_IMMEDIATELY,.
fbb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbb50 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49 0, SHARED_SI
fbb60 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f ZE, 0, &ovlp);./
fbb70 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 * isNT() is 1 if
fbb80 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fbb90 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 ==1, so this els
fbba0 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 e is never execu
fbbb0 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c ted. .*/.#if SQL
fbbc0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
fbbd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
fbbe0 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 lk;. sqlite3
fbbf0 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 _randomness(size
fbc00 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 of(lk), &lk);.
fbc10 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c pFile->sharedL
fbc20 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74 ockByte = (short
fbc30 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 )((lk & 0x7fffff
fbc40 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 ff)%(SHARED_SIZE
fbc50 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20 - 1));. res
fbc60 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fbc70 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fbc80 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c T+pFile->sharedL
fbc90 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 ockByte, 0, 1, 0
fbca0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
fbcb0 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a if( res == 0 ){.
fbcc0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
fbcd0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
fbce0 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ror();. }. ret
fbcf0 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a urn res;.}../*.*
fbd00 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 * Undo a readloc
fbd10 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 k.*/.static int
fbd20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 unlockReadLock(w
fbd30 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
fbd40 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 int res;. if(
fbd50 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 isNT() ){. r
fbd60 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 es = UnlockFile(
fbd70 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fbd80 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 _FIRST, 0, SHARE
fbd90 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 D_SIZE, 0);./* i
fbda0 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
fbdb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
fbdc0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
fbdd0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
fbde0 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 . .*/.#if SQLITE
fbdf0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
fbe00 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 else{. res =
fbe10 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
fbe20 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fbe30 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 T + pFile->share
fbe40 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c dLockByte, 0, 1,
fbe50 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0);.#endif. }.
fbe60 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 if( res == 0 )
fbe70 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
fbe80 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
fbe90 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 Error();. }. r
fbea0 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
fbeb0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c .** Lock the fil
fbec0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 e with the lock
fbed0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
fbee0 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 ameter locktype
fbef0 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 - one.** of the
fbf00 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
fbf10 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f (1) SHARED_
fbf20 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 LOCK.** (2)
fbf30 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a RESERVED_LOCK.**
fbf40 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 (3) PENDING
fbf50 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 _LOCK.** (4)
fbf60 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a EXCLUSIVE_LOCK.
fbf70 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 **.** Sometimes
fbf80 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 when requesting
fbf90 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 one lock state,
fbfa0 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 additional lock
fbfb0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e states.** are in
fbfc0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 serted in betwee
fbfd0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 n. The locking
fbfe0 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e might fail on on
fbff0 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a e of the later.*
fc000 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 * transitions le
fc010 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 aving the lock s
fc020 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 tate different f
fc030 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 rom what it star
fc040 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c ted but.** still
fc050 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f short of its go
fc060 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 al. The followi
fc070 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 ng chart shows t
fc080 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 he allowed.** tr
fc090 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 ansitions and th
fc0a0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 e inserted inter
fc0b0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a mediate states:.
fc0c0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 **.** UNLOCKE
fc0d0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 D -> SHARED.**
fc0e0 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 SHARED -> RESE
fc0f0 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 RVED.** SHARE
fc100 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
fc110 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
fc120 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 RESERVED -> (P
fc130 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
fc140 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 SIVE.** PENDI
fc150 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a NG -> EXCLUSIVE.
fc160 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
fc170 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 ne will only inc
fc180 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 rease a lock. T
fc190 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 he winUnlock() r
fc1a0 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 outine.** erases
fc1b0 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e all locks at on
fc1c0 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 ce and returns u
fc1d0 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f s immediately to
fc1e0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 locking level 0
fc1f0 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 ..** It is not p
fc200 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 ossible to lower
fc210 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
fc220 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 el one step at a
fc230 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d time. You.** m
fc240 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 ust go straight
fc250 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c to locking level
fc260 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 0..*/.static in
fc270 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 t winLock(sqlite
fc280 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fc290 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
fc2a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
fc2b0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
fc2c0 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 de from subrouti
fc2d0 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 nes */. int res
fc2e0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 = 1;
fc2f0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 /* Result of a w
fc300 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c indows lock call
fc310 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 */. int newLoc
fc320 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 ktype; /*
fc330 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 Set pFile->lockt
fc340 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 ype to this valu
fc350 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 e before exiting
fc360 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e */. int gotPen
fc370 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 dingLock = 0;/*
fc380 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 True if we acqui
fc390 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f red a PENDING lo
fc3a0 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a ck this time */.
fc3b0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
fc3c0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
fc3d0 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d . DWORD error =
fc3e0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73 NO_ERROR;.. as
fc3f0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
fc400 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 OSTRACE5("LOCK
fc410 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 29 %d %d was %d(%d)
fc420 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 \n",. p
fc430 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
fc440 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
fc450 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pe, pFile->share
fc460 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f dLockByte);.. /
fc470 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c * If there is al
fc480 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 ready a lock of
fc490 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 this type or mor
fc4a0 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e e restrictive on
fc4b0 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 the. ** OsFile
fc4c0 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f , do nothing. Do
fc4d0 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f n't use the end_
fc4e0 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c lock: exit path,
fc4f0 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 as. ** sqlite3
fc500 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 OsEnterMutex() h
fc510 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 asn't been calle
fc520 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 d yet.. */. if
fc530 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
fc540 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e>=locktype ){.
fc550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fc560 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
fc570 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
fc580 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
fc590 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
fc5a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
fc5b0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
fc5c0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
fc5d0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
fc5e0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
fc5f0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
fc600 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
fc610 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
fc620 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
fc630 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
fc640 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b CK );.. /* Lock
fc650 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
fc660 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 K byte if we nee
fc670 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 d to acquire a P
fc680 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 ENDING lock or.
fc690 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ** a SHARED loc
fc6a0 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 k. If we are ac
fc6b0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
fc6c0 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 lock, the acqui
fc6d0 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 sition of. ** t
fc6e0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
fc6f0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 byte is temporar
fc700 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 y.. */. newLoc
fc710 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c ktype = pFile->l
fc720 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20 ocktype;. if(
fc730 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
fc740 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 e==NO_LOCK).
fc750 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70 || ( (locktyp
fc760 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
fc770 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 K). && (
fc780 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
fc790 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29 =RESERVED_LOCK))
fc7a0 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e . ){. int cn
fc7b0 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65 t = 3;. while
fc7c0 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65 ( cnt-->0 && (re
fc7d0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 s = LockFile(pFi
fc7e0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 le->h, PENDING_B
fc7f0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d YTE, 0, 1, 0))==
fc800 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 0 ){. /* Tr
fc810 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 y 3 times to get
fc820 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 the pending loc
fc830 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 k. The pending
fc840 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20 lock might be.
fc850 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61 ** held by a
fc860 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70 72 nother reader pr
fc870 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72 ocess who will r
fc880 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74 elease it moment
fc890 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a arily.. */.
fc8a0 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 OSTRACE2("
fc8b0 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 could not get a
fc8c0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e PENDING lock. cn
fc8d0 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20 t=%d\n", cnt);.
fc8e0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 Sleep(1);.
fc8f0 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 }. gotPend
fc900 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 ingLock = res;.
fc910 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20 if( !res ){.
fc920 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 error = Get
fc930 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
fc940 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
fc950 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f uire a shared lo
fc960 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
fc970 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc980 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 OCK && res ){.
fc990 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
fc9a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f >locktype==NO_LO
fc9b0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
fc9c0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
fc9d0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 e);. if( res
fc9e0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
fc9f0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
fca00 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
fca10 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 error = Get
fca20 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
fca30 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
fca40 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
fca50 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
fca60 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
fca70 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 ED_LOCK && res )
fca80 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
fca90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
fcaa0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
fcab0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 res = LockFile
fcac0 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 (pFile->h, RESER
fcad0 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 VED_BYTE, 0, 1,
fcae0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 0);. if( res
fcaf0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
fcb00 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f type = RESERVED_
fcb10 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
fcb20 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
fcb30 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fcb40 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
fcb50 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 cquire a PENDING
fcb60 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
fcb70 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
fcb80 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
fcb90 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 ){. newLockt
fcba0 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ype = PENDING_LO
fcbb0 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 CK;. gotPendi
fcbc0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a ngLock = 0;. }.
fcbd0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e . /* Acquire an
fcbe0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a EXCLUSIVE lock.
fcbf0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
fcc00 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
fcc10 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 OCK && res ){.
fcc20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
fcc30 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 >locktype>=SHARE
fcc40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 D_LOCK );. re
fcc50 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
fcc60 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
fcc70 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c STRACE2("unreadl
fcc80 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 ock = %d\n", res
fcc90 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
fcca0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fccb0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
fccc0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 SHARED_SIZE, 0)
fccd0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
fcce0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
fccf0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c pe = EXCLUSIVE_L
fcd00 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a OCK;. }else{.
fcd10 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 error = Ge
fcd20 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fcd30 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 OSTRACE2("er
fcd40 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 ror-code = %d\n"
fcd50 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 , error);.
fcd60 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
fcd70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 e);. }. }..
fcd80 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f /* If we are ho
fcd90 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 lding a PENDING
fcda0 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 lock that ought
fcdb0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 to be released,
fcdc0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 then. ** releas
fcdd0 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 e it now.. */.
fcde0 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c if( gotPendingL
fcdf0 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d ock && locktype=
fce00 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
fce10 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 UnlockFile(p
fce20 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
fce30 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fce40 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 . }.. /* Updat
fce50 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
fce60 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 he lock has held
fce70 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 in the file des
fce80 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a criptor then. *
fce90 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 * return the app
fcea0 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 ropriate result
fceb0 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 code.. */. if(
fcec0 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d res ){. rc =
fced0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
fcee0 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 lse{. OSTRACE
fcef0 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 4("LOCK FAILED %
fcf00 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 d trying for %d
fcf10 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 but got %d\n", p
fcf20 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
fcf30 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 locktype, ne
fcf40 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 wLocktype);.
fcf50 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
fcf60 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63 = error;. rc
fcf70 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
fcf80 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
fcf90 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c ktype = (u8)newL
fcfa0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 ocktype;. retur
fcfb0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
fcfc0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
fcfd0 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ks if there is a
fcfe0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 RESERVED lock h
fcff0 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 eld on the speci
fd000 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 fied.** file by
fd010 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
fd020 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 r process. If su
fd030 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c ch a lock is hel
fd040 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e d, return.** non
fd050 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 -zero, otherwise
fd060 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 zero..*/.static
fd070 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73 int winCheckRes
fd080 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
fd090 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fd0a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
fd0b0 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a rc;. winFile *
fd0c0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
fd0d0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
fd0e0 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 id!=0 );. if(
fd0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
fd100 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 =RESERVED_LOCK )
fd110 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
fd120 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 OSTRACE3("TEST
fd130 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
fd140 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 local)\n", pFile
fd150 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 ->h, rc);. }els
fd160 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b e{. rc = Lock
fd170 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 File(pFile->h, R
fd180 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c ESERVED_BYTE, 0,
fd190 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 1, 0);. if(
fd1a0 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f rc ){. Unlo
fd1b0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fd1c0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
fd1d0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0, 1, 0);. }.
fd1e0 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 rc = !rc;.
fd1f0 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 OSTRACE3("TEST
fd200 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
fd210 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c remote)\n", pFil
fd220 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 e->h, rc);. }.
fd230 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a *pResOut = rc;.
fd240 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fd250 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 OK;.}../*.** Low
fd260 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
fd270 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
fd280 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f criptor id to lo
fd290 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
fd2a0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
fd2b0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
fd2c0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
fd2d0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
fd2e0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
fd2f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
fd300 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
fd310 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
fd320 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
fd330 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
fd340 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a e is a no-op..**
fd350 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
fd360 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 ssible for this
fd370 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 routine to fail
fd380 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 if the second ar
fd390 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f gument.** is NO_
fd3a0 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 LOCK. If the se
fd3b0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
fd3c0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 SHARED_LOCK the
fd3d0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
fd3e0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 * might return S
fd3f0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a QLITE_IOERR;.*/.
fd400 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e static int winUn
fd410 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
fd420 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
fd430 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 ype){. int type
fd440 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 ;. winFile *pFi
fd450 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
fd460 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 d;. int rc = SQ
fd470 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 LITE_OK;. asser
fd480 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 t( pFile!=0 );.
fd490 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
fd4a0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
fd4b0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
fd4c0 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 LOCK %d to %d wa
fd4d0 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 s %d(%d)\n", pFi
fd4e0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
fd4f0 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
fd500 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
fd510 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
fd520 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 e);. type = pFi
fd530 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 le->locktype;.
fd540 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 if( type>=EXCLUS
fd550 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
fd560 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
fd570 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fd580 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a T, 0, SHARED_SIZ
fd590 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c E, 0);. if( l
fd5a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
fd5b0 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 LOCK && !getRead
fd5c0 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 Lock(pFile) ){.
fd5d0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f /* This sho
fd5e0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e uld never happen
fd5f0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 . We should alw
fd600 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 ays be able to.
fd610 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 ** reacquir
fd620 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 e the read lock
fd630 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 */. rc = SQ
fd640 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
fd650 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 K;. }. }. i
fd660 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 f( type>=RESERVE
fd670 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e D_LOCK ){. Un
fd680 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
fd690 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 h, RESERVED_BYTE
fd6a0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a , 0, 1, 0);. }.
fd6b0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
fd6c0 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e NO_LOCK && type>
fd6d0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
fd6e0 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f unlockReadLo
fd6f0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 ck(pFile);. }.
fd700 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 if( type>=PENDI
fd710 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 NG_LOCK ){. U
fd720 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d nlockFile(pFile-
fd730 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 >h, PENDING_BYTE
fd740 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a , 0, 1, 0);. }.
fd750 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
fd760 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 e = (u8)locktype
fd770 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
fd780 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 ../*.** Control
fd790 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 and query of the
fd7a0 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
fd7b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
fd7c0 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 winFileControl(
fd7d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fd7e0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
fd7f0 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 pArg){. switch(
fd800 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
fd810 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
fd820 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
fd830 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
fd840 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f winFile*)id)->lo
fd850 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 cktype;. re
fd860 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fd870 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 }. case S
fd880 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f QLITE_LAST_ERRNO
fd890 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a : {. *(int*
fd8a0 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77 )pArg = (int)((w
fd8b0 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 inFile*)id)->las
fd8c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 tErrno;. re
fd8d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fd8e0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
fd8f0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
fd900 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
fd910 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
fd920 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
fd930 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 underlying bloc
fd940 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 k device for.**
fd950 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 the specified fi
fd960 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f le. This is almo
fd970 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 st always 512 by
fd980 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a tes, but may be.
fd990 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f ** larger for so
fd9a0 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a me devices..**.*
fd9b0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 * SQLite code as
fd9c0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 sumes this funct
fd9d0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
fd9e0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 It also assumes
fd9f0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 that.** if two
fda00 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 files are create
fda10 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 d in the same fi
fda20 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 le-system direct
fda30 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 ory (i.e..** a d
fda40 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 atabase and its
fda50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 journal file) th
fda60 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 at the sector si
fda70 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a ze will be the.*
fda80 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e * same for both.
fda90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fdaa0 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c inSectorSize(sql
fdab0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
fdac0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fdad0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 );. return (int
fdae0 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 )(((winFile*)id)
fdaf0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d ->sectorSize);.}
fdb00 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
fdb10 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 vector of devic
fdb20 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
fdb30 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
fdb40 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 winDeviceCharac
fdb50 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
fdb60 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 3_file *id){. U
fdb70 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
fdb80 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b id);. return 0;
fdb90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 .}../*.** This v
fdba0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c ector defines al
fdbb0 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 l the methods th
fdbc0 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f at can operate o
fdbd0 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f n an.** sqlite3_
fdbe0 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a file for win32..
fdbf0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
fdc00 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
fdc10 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d ds winIoMethod =
fdc20 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 {. 1,
fdc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fdc40 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 * iVersion */.
fdc50 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52 winClose,. winR
fdc60 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c ead,. winWrite,
fdc70 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a . winTruncate,.
fdc80 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e winSync,. win
fdc90 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c FileSize,. winL
fdca0 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b ock,. winUnlock
fdcb0 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65 ,. winCheckRese
fdcc0 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46 rvedLock,. winF
fdcd0 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69 ileControl,. wi
fdce0 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77 nSectorSize,. w
fdcf0 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 inDeviceCharacte
fdd00 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a ristics.};../***
fdd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdd50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 ********.** Here
fdd60 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 ends the I/O me
fdd70 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 thods that form
fdd80 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
fdd90 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a ethods object..*
fdda0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c *.** The next bl
fddb0 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c ock of code impl
fddc0 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d ements the VFS m
fddd0 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a ethods..********
fdde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fddf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fde00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fde10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fde20 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e ****/../*.** Con
fde30 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c vert a UTF-8 fil
fde40 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65 ename into whate
fde50 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64 ver form the und
fde60 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 erlying.** opera
fde70 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74 ting system want
fde80 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 s filenames in.
fde90 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
fdea0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 he result.** is
fdeb0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
fdec0 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 lloc and must be
fded0 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 freed by the ca
fdee0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f lling.** functio
fdef0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 n..*/.static voi
fdf00 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 d *convertUtf8Fi
fdf10 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 lename(const cha
fdf20 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
fdf30 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
fdf40 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e d = 0;. if( isN
fdf50 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 T() ){. zConv
fdf60 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e erted = utf8ToUn
fdf70 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
fdf80 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 ;./* isNT() is 1
fdf90 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if SQLITE_OS_WI
fdfa0 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 NCE==1, so this
fdfb0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 else is never ex
fdfc0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 ecuted. .*/.#if
fdfd0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fdfe0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
fdff0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 zConverted = utf
fe000 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 8ToMbcs(zFilenam
fe010 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 e);.#endif. }.
fe020 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 /* caller will
fe030 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65 handle out of me
fe040 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e mory */. return
fe050 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a zConverted;.}..
fe060 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 /*.** Create a t
fe070 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 emporary file na
fe080 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 me in zBuf. zBu
fe090 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e f must be big en
fe0a0 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 ough to.** hold
fe0b0 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e at pVfs->mxPathn
fe0c0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a ame characters..
fe0d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
fe0e0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 tTempname(int nB
fe0f0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
fe100 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a . static char z
fe110 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 Chars[] =. "a
fe120 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 bcdefghijklmnopq
fe130 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 rstuvwxyz". "
fe140 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGHIJKLMNOP
fe150 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 QRSTUVWXYZ".
fe160 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 "0123456789";.
fe170 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63 size_t i, j;. c
fe180 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41 har zTempPath[MA
fe190 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28 X_PATH+1];. if(
fe1a0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 sqlite3_temp_di
fe1b0 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73 rectory ){. s
fe1c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
fe1d0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 MAX_PATH-30, zTe
fe1e0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71 mpPath, "%s", sq
fe1f0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
fe200 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69 tory);. }else i
fe210 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
fe220 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 char *zMulti;.
fe230 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 WCHAR zWidePa
fe240 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 th[MAX_PATH];.
fe250 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d GetTempPathW(M
fe260 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64 AX_PATH-30, zWid
fe270 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c ePath);. zMul
fe280 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 ti = unicodeToUt
fe290 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 f8(zWidePath);.
fe2a0 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b if( zMulti ){
fe2b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
fe2c0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
fe2d0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 -30, zTempPath,
fe2e0 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 "%s", zMulti);.
fe2f0 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69 free(zMulti
fe300 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
fe310 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fe320 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f E_NOMEM;. }./
fe330 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 * isNT() is 1 if
fe340 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fe350 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 ==1, so this els
fe360 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 e is never execu
fe370 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 ted. .** Since t
fe380 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e he ASCII version
fe390 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 of these Window
fe3a0 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 s API do not exi
fe3b0 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a st for WINCE,.**
fe3c0 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 it's important
fe3d0 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 to not reference
fe3e0 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 them for WINCE
fe3f0 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 builds..*/.#if S
fe400 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fe410 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 0. }else{. c
fe420 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20 har *zUtf8;.
fe430 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d char zMbcsPath[M
fe440 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 AX_PATH];. Ge
fe450 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 tTempPathA(MAX_P
fe460 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 ATH-30, zMbcsPat
fe470 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 h);. zUtf8 =
fe480 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 sqlite3_win32_mb
fe490 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 cs_to_utf8(zMbcs
fe4a0 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a Path);. if( z
fe4b0 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71 Utf8 ){. sq
fe4c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d lite3_snprintf(M
fe4d0 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d AX_PATH-30, zTem
fe4e0 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 pPath, "%s", zUt
fe4f0 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 f8);. free(
fe500 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73 zUtf8);. }els
fe510 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
fe520 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fe530 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
fe540 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74 for(i=sqlite3St
fe550 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 rlen30(zTempPath
fe560 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 ); i>0 && zTempP
fe570 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 ath[i-1]=='\\';
fe580 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 i--){}. zTempPa
fe590 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c th[i] = 0;. sql
fe5a0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 ite3_snprintf(nB
fe5b0 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 uf-30, zBuf,.
fe5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe5d0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d "%s\\"SQLITE_TEM
fe5e0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
fe5f0 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d TempPath);. j =
fe600 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
fe610 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 (zBuf);. sqlite
fe620 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 3_randomness(20,
fe630 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f &zBuf[j]);. fo
fe640 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b r(i=0; i<20; i++
fe650 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 , j++){. zBuf
fe660 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 [j] = (char)zCha
fe670 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 rs[ ((unsigned c
fe680 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 har)zBuf[j])%(si
fe690 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 zeof(zChars)-1)
fe6a0 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d ];. }. zBuf[j]
fe6b0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 = 0;. OSTRACE2
fe6c0 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a ("TEMP FILENAME:
fe6d0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 %s\n", zBuf);.
fe6e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fe6f0 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 K; .}../*.** The
fe700 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
fe710 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
fe720 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 .** is zero if t
fe730 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
fe740 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 fits in the buf
fe750 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f fer, or non-zero
fe760 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 .** otherwise (i
fe770 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 f the message wa
fe780 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f s truncated)..*/
fe790 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c .static int getL
fe7a0 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 astErrorMsg(int
fe7b0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
fe7c0 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 ){. /* FormatMe
fe7d0 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 ssage returns 0
fe7e0 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 on failure. Oth
fe7f0 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 erwise it. ** r
fe800 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
fe810 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 r of TCHARs writ
fe820 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ten to the outpu
fe830 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 t. ** buffer, e
fe840 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 xcluding the ter
fe850 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 minating null ch
fe860 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44 ar.. */. DWORD
fe870 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 error = GetLast
fe880 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52 44 Error();. DWORD
fe890 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 dwLen = 0;. ch
fe8a0 61 72 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20 ar *zOut = 0;..
fe8b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
fe8c0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 57 WCHAR *zTempW
fe8d0 69 64 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 ide = NULL;.
fe8e0 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 dwLen = FormatMe
fe8f0 73 73 61 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45 ssageW(FORMAT_ME
fe900 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 SSAGE_ALLOCATE_B
fe910 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d UFFER | FORMAT_M
fe920 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 ESSAGE_FROM_SYST
fe930 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 EM | FORMAT_MESS
fe940 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 AGE_IGNORE_INSER
fe950 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 TS,.
fe960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
fe970 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ULL,.
fe980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe990 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 error,.
fe9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe9b0 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 0,.
fe9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe9d0 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d 70 57 (LPWSTR) &zTempW
fe9e0 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ide,.
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fea00 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 0)
fea20 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20 ;. if( dwLen
fea30 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 > 0 ){. /*
fea40 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65 allocate a buffe
fea50 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f r and convert to
fea60 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a UTF8 */. z
fea70 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 Out = unicodeToU
fea80 74 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a tf8(zTempWide);.
fea90 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68 /* free th
feaa0 65 20 73 79 73 74 65 6d 20 62 75 66 66 65 72 20 e system buffer
feab0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46 6f 72 allocated by For
feac0 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20 matMessage */.
fead0 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54 LocalFree(zT
feae0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a empWide);. }.
feaf0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
feb00 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
feb10 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
feb20 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
feb30 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
feb40 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
feb50 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
feb60 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
feb70 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
feb80 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
feb90 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
feba0 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
febb0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
febc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
febd0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
febe0 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55 char *zTemp = NU
febf0 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 LL;. dwLen =
fec00 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46 FormatMessageA(F
fec10 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c ORMAT_MESSAGE_AL
fec20 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20 LOCATE_BUFFER |
fec30 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 FORMAT_MESSAGE_F
fec40 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 ROM_SYSTEM | FOR
fec50 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f MAT_MESSAGE_IGNO
fec60 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 RE_INSERTS,.
fec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fec80 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 NULL,.
fec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feca0 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 error,.
fecb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fecc0 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
fecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fece0 20 20 20 20 20 20 20 20 28 4c 50 53 54 52 29 20 (LPSTR)
fecf0 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20 20 20 &zTemp,.
fed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fed10 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 0,.
fed20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fed30 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 0);. if( dwL
fed40 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 en > 0 ){.
fed50 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 /* allocate a bu
fed60 66 66 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 ffer and convert
fed70 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 to UTF8 */.
fed80 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 zOut = sqlite3
fed90 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 _win32_mbcs_to_u
feda0 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 tf8(zTemp);.
fedb0 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79 /* free the sy
fedc0 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f stem buffer allo
fedd0 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d cated by FormatM
fede0 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 essage */.
fedf0 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 29 LocalFree(zTemp)
fee00 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
fee10 20 7d 0a 20 20 69 66 28 20 30 20 3d 3d 20 64 77 }. if( 0 == dw
fee20 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Len ){. sqlit
fee30 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
fee40 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 , zBuf, "OsError
fee50 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 0x%x (%u)", err
fee60 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65 or, error);. }e
fee70 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 lse{. /* copy
fee80 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42 a maximum of nB
fee90 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75 74 70 uf chars to outp
feea0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 ut buffer */.
feeb0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
feec0 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 25 f(nBuf, zBuf, "%
feed0 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f s", zOut);. /
feee0 2a 20 66 72 65 65 20 74 68 65 20 55 54 46 38 20 * free the UTF8
feef0 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 66 72 buffer */. fr
fef00 65 65 28 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 ee(zOut);. }.
fef10 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
fef20 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a ** Open a file..
fef30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fef40 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 nOpen(. sqlite3
fef50 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
fef60 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a /* Not used *
fef70 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
fef80 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a zName, /*
fef90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
fefa0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 e (UTF-8) */. s
fefb0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fefc0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
fefd0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c e the SQLite fil
fefe0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f e handle here */
feff0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
ff000 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff010 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 Open mode flags
ff020 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c */. int *pOutFl
ff030 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
ff040 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 * Status return
ff050 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 flags */.){. HA
ff060 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 NDLE h;. DWORD
ff070 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b dwDesiredAccess;
ff080 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 . DWORD dwShare
ff090 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 Mode;. DWORD dw
ff0a0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
ff0b0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 ion;. DWORD dwF
ff0c0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
ff0d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
ff0e0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 E_OS_WINCE. int
ff0f0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e isTemp = 0;.#en
ff100 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 dif. winFile *p
ff110 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
ff120 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f )id;. void *zCo
ff130 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 nverted;
ff140 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
ff150 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 name in OS encod
ff160 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ing */. const c
ff170 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d har *zUtf8Name =
ff180 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 zName; /* Fi
ff190 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 lename in UTF-8
ff1a0 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 encoding */. ch
ff1b0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f ar zTmpname[MAX_
ff1c0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 PATH+1];
ff1d0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74 /* Buffer used t
ff1e0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69 o create temp fi
ff1f0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 lename */.. ass
ff200 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
ff210 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ff220 28 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 (pVfs);.. /* If
ff230 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
ff240 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
ff250 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 ction is NULL, g
ff260 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 enerate a . **
ff270 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
ff280 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f ame to use . */
ff290 0a 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d . if( !zUtf8Nam
ff2a0 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 e ){. int rc
ff2b0 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 = getTempname(MA
ff2c0 58 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 X_PATH+1, zTmpna
ff2d0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
ff2e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ff2f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
ff300 20 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 }. zUtf8Na
ff310 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 me = zTmpname;.
ff320 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 }.. /* Convert
ff330 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f the filename to
ff340 20 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f the system enco
ff350 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 ding. */. zConv
ff360 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
ff370 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 tf8Filename(zUtf
ff380 38 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 8Name);. if( zC
ff390 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
ff3a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ff3b0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 _NOMEM;. }.. i
ff3c0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
ff3d0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
ff3e0 20 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 ){. dwDesire
ff3f0 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 dAccess = GENERI
ff400 43 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 C_READ | GENERIC
ff410 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b _WRITE;. }else{
ff420 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 . dwDesiredAc
ff430 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 cess = GENERIC_R
ff440 45 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 EAD;. }. /* SQ
ff450 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
ff460 49 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d IVE is used to m
ff470 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20 ake sure that a
ff480 6e 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a new file is . *
ff490 2a 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 * created. SQLit
ff4a0 65 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 e doesn't use it
ff4b0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 to indicate "ex
ff4c0 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20 clusive access"
ff4d0 0a 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 . ** as it is u
ff4e0 73 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f sually understoo
ff4f0 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
ff500 28 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 (!(flags & SQLIT
ff510 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
ff520 29 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 ) || (flags & SQ
ff530 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
ff540 29 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 ));. if( flags
ff550 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 & SQLITE_OPEN_EX
ff560 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f CLUSIVE ){. /
ff570 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20 * Creates a new
ff580 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 file, only if it
ff590 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 does not alread
ff5a0 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 y exist. */.
ff5b0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65 /* If the file e
ff5c0 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e xists, it fails.
ff5d0 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 */. dwCreati
ff5e0 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 onDisposition =
ff5f0 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 CREATE_NEW;. }e
ff600 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
ff610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
ff620 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 TE ){. /* Ope
ff630 6e 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c n existing file,
ff640 20 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74 or create if it
ff650 20 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a doesn't exist *
ff660 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e /. dwCreation
ff670 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 Disposition = OP
ff680 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c EN_ALWAYS;. }el
ff690 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 se{. /* Opens
ff6a0 20 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 a file, only if
ff6b0 20 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 it exists. */.
ff6c0 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
ff6d0 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f position = OPEN_
ff6e0 45 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 EXISTING;. }.
ff6f0 64 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 dwShareMode = FI
ff700 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 LE_SHARE_READ |
ff710 46 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 FILE_SHARE_WRITE
ff720 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
ff730 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
ff740 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 TEONCLOSE ){.#if
ff750 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff760 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 . dwFlagsAndA
ff770 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 ttributes = FILE
ff780 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 _ATTRIBUTE_HIDDE
ff790 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20 N;. isTemp =
ff7a0 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46 1;.#else. dwF
ff7b0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
ff7c0 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 s = FILE_ATTRIBU
ff7d0 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 TE_TEMPORARY.
ff7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff7f0 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 | FI
ff800 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 LE_ATTRIBUTE_HID
ff810 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 DEN.
ff820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff830 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 | FILE_FLAG_D
ff840 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a ELETE_ON_CLOSE;.
ff850 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a #endif. }else{.
ff860 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
ff870 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f tributes = FILE_
ff880 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c ATTRIBUTE_NORMAL
ff890 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 ;. }. /* Repor
ff8a0 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 ts from the inte
ff8b0 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65 rnet are that pe
ff8c0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 rformance is alw
ff8d0 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 ays. ** better
ff8e0 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e if FILE_FLAG_RAN
ff8f0 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73 DOM_ACCESS is us
ff900 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39 ed. Ticket #269
ff910 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 9. */.#if SQLITE
ff920 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c _OS_WINCE. dwFl
ff930 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
ff940 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 |= FILE_FLAG_RA
ff950 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e NDOM_ACCESS;.#en
ff960 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29 dif. if( isNT()
ff970 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 ){. h = Crea
ff980 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29 teFileW((WCHAR*)
ff990 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 zConverted,.
ff9a0 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
ff9b0 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 ss,. dwSha
ff9c0 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e reMode,. N
ff9d0 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 ULL,. dwCr
ff9e0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
ff9f0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 n,. dwFlag
ffa00 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a sAndAttributes,.
ffa10 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 NULL.
ffa20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
ffa30 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
ffa40 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
ffa50 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
ffa60 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
ffa70 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
ffa80 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
ffa90 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
ffaa0 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
ffab0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
ffac0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
ffad0 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
ffae0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
ffaf0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ffb00 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
ffb10 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c h = CreateFil
ffb20 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 eA((char*)zConve
ffb30 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 rted,. dwD
ffb40 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 esiredAccess,.
ffb50 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 dwShareMode
ffb60 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 ,. NULL,.
ffb70 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e dwCreation
ffb80 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 Disposition,.
ffb90 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
ffba0 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 tributes,.
ffbb0 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e NULL. );.#en
ffbc0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d dif. }. if( h=
ffbd0 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f =INVALID_HANDLE_
ffbe0 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65 VALUE ){. fre
ffbf0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ffc00 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 if( flags & S
ffc10 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
ffc20 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65 RITE ){. re
ffc30 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 turn winOpen(pVf
ffc40 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 s, zName, id, .
ffc50 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c ((fl
ffc60 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ags|SQLITE_OPEN_
ffc70 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 READONLY)&~SQLIT
ffc80 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
ffc90 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 ), pOutFlags);.
ffca0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
ffcb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
ffcc0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
ffcd0 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 }. if( pOutFlag
ffce0 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 s ){. if( fla
ffcf0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ffd00 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
ffd10 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d *pOutFlags =
ffd20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
ffd30 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 DWRITE;. }els
ffd40 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c e{. *pOutFl
ffd50 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 ags = SQLITE_OPE
ffd60 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 N_READONLY;.
ffd70 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 }. }. memset(p
ffd80 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 File, 0, sizeof(
ffd90 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c *pFile));. pFil
ffda0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 e->pMethod = &wi
ffdb0 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 nIoMethod;. pFi
ffdc0 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 le->h = h;. pFi
ffdd0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ffde0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c NO_ERROR;. pFil
ffdf0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 e->sectorSize =
ffe00 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56 getSectorSize(pV
ffe10 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a fs, zUtf8Name);.
ffe20 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
ffe30 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 NCE. if( (flags
ffe40 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f & (SQLITE_OPEN_
ffe50 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 READWRITE|SQLITE
ffe60 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 _OPEN_MAIN_DB))
ffe70 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ==.
ffe80 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 (SQLITE_OPEN_R
ffe90 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
ffea0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 OPEN_MAIN_DB).
ffeb0 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 && !winceCr
ffec0 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 eateLock(zName,
ffed0 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 pFile). ){.
ffee0 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a CloseHandle(h);.
ffef0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
fff00 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ted);. return
fff10 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
fff20 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 ;. }. if( isTe
fff30 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d mp ){. pFile-
fff40 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 >zDeleteOnClose
fff50 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 = zConverted;.
fff60 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b }else.#endif. {
fff70 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
fff80 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 rted);. }. Ope
fff90 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
fffa0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
fffb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
fffc0 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 e the named file
fffd0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
fffe0 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e t windows does n
ffff0 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 ot allow a file
10000 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 to be deleted if
10001 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 some other.** p
10002 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 rocess has it op
10003 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 en. Sometimes a
10004 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f virus scanner o
10005 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 r indexing progr
10006 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 am.** will open
10007 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 a journal file s
10008 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 hortly after it
10009 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 is created in or
1000a 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 der to do.** wha
1000b 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 tever it does.
1000c 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 While this other
1000d 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
1000e 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 ing the.** file
1000f 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 open, we will be
10010 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 unable to delet
10011 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 e it. To work a
10012 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 round this.** pr
10013 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 oblem, we delay
10014 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 100 milliseconds
10015 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 and try to dele
10016 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a te again. Up.**
10017 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f to MX_DELETION_
10018 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f ATTEMPTs deletio
10019 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 n attempts are r
1001a 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 un before giving
1001b 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 .** up and retur
1001c 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a ning an error..*
1001d 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c /.#define MX_DEL
1001e 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 ETION_ATTEMPTS 5
1001f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 .static int winD
10020 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 elete(. sqlite3
10021 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
10022 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
10023 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 on win32 */. c
10024 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
10025 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 name, /* Na
10026 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 me of file to de
10027 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 lete */. int sy
10028 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 ncDir
10029 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
1002a 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b d on win32 */.){
1002b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a . int cnt = 0;.
1002c 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 DWORD rc;. DW
1002d 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 ORD error = 0;.
1002e 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
1002f 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 d = convertUtf8F
10030 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d ilename(zFilenam
10031 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e);. UNUSED_PAR
10032 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
10033 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10034 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 (syncDir);. if(
10035 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 zConverted==0 )
10036 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
10037 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
10038 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
10039 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
1003a 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 OERR_DELETE);.
1003b 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
1003c 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 do{. Dele
1003d 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 teFileW(zConvert
1003e 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 ed);. }while(
1003f 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 ( ((rc = Ge
10040 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 tFileAttributesW
10041 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d (zConverted)) !=
10042 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 INVALID_FILE_AT
10043 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 TRIBUTES).
10044 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 || ((er
10045 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 ror = GetLastErr
10046 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 or()) == ERROR_A
10047 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 CCESS_DENIED)).
10048 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b && (++
10049 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f cnt < MX_DELETIO
1004a 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 N_ATTEMPTS).
1004b 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 && (Sleep
1004c 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 (100), 1) );./*
1004d 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
1004e 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
1004f 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
10050 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
10051 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
10052 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
10053 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
10054 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
10055 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
10056 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
10057 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
10058 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
10059 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
1005a 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
1005b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b }else{. do{
1005c 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c . DeleteFil
1005d 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a eA(zConverted);.
1005e 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 }while( (
1005f 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 ((rc = GetFile
10060 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e AttributesA(zCon
10061 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 verted)) != INVA
10062 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
10063 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 TES).
10064 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d || ((error =
10065 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 GetLastError())
10066 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 == ERROR_ACCESS
10067 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 _DENIED)).
10068 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c && (++cnt <
10069 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 MX_DELETION_ATT
1006a 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 EMPTS).
1006b 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 && (Sleep(100)
1006c 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 , 1) );.#endif.
1006d 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 }. free(zConve
1006e 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 rted);. OSTRACE
1006f 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 2("DELETE \"%s\"
10070 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b \n", zFilename);
10071 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 . return ( (r
10072 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c c == INVALID_FIL
10073 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 E_ATTRIBUTES) .
10074 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72 && (err
10075 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 or == ERROR_FILE
10076 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 _NOT_FOUND)) ? S
10077 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
10078 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a E_IOERR_DELETE;.
10079 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 }../*.** Check t
1007a 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 he existance and
1007b 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c status of a fil
1007c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1007d 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 winAccess(. sq
1007e 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
1007f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
10080 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f used on win32 */
10081 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
10082 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a Filename, /*
10083 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
10084 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 check */. int
10085 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
10086 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
10087 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f f test to make o
10088 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 n this file */.
10089 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 int *pResOut
1008a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1008b 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b UT: Result */.){
1008c 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 . DWORD attr;.
1008d 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 int rc = 0;. v
1008e 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
1008f 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
10090 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
10091 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
10092 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 ETER(pVfs);. if
10093 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 ( zConverted==0
10094 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
10095 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
10096 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
10097 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 attr = GetFi
10098 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57 leAttributesW((W
10099 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
1009a 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
1009b 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
1009c 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
1009d 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
1009e 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
1009f 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
100a0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
100a1 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
100a2 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
100a3 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
100a4 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
100a5 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
100a6 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
100a7 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
100a8 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
100a9 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c attr = GetFil
100aa 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68 eAttributesA((ch
100ab 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b ar*)zConverted);
100ac 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 .#endif. }. fr
100ad 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
100ae 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 switch( flags
100af 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
100b0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a TE_ACCESS_READ:.
100b1 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
100b2 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 ACCESS_EXISTS:.
100b3 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d rc = attr!=
100b4 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
100b5 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 RIBUTES;. b
100b6 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
100b7 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
100b8 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 DWRITE:. rc
100b9 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f = (attr & FILE_
100ba 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e ATTRIBUTE_READON
100bb 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 LY)==0;. br
100bc 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
100bd 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 :. assert(!
100be 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 "Invalid flags a
100bf 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 rgument");. }.
100c0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a *pResOut = rc;.
100c1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
100c2 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 OK;.}.../*.** Tu
100c3 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 rn a relative pa
100c4 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 thname into a fu
100c5 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 ll pathname. Wr
100c6 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 ite the full.**
100c7 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f pathname into zO
100c8 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 ut[]. zOut[] wi
100c9 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 ll be at least p
100ca 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a Vfs->mxPathname.
100cb 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
100cc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
100cd 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 winFullPathname(
100ce 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
100cf 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
100d0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
100d1 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 fs object */. c
100d2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 onst char *zRela
100d3 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 tive, /*
100d4 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
100d5 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
100d6 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 int nFull,
100d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100d8 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 /* Size of outpu
100d9 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 t buffer in byte
100da 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 s */. char *zFu
100db 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
100dc 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
100dd 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 uffer */.){. .#
100de 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 if defined(__CYG
100df 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f WIN__). UNUSED_
100e0 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 PARAMETER(nFull)
100e1 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f ;. cygwin_conv_
100e2 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 to_full_win32_pa
100e3 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 th(zRelative, zF
100e4 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ull);. return S
100e5 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 QLITE_OK;.#endif
100e6 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
100e7 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 WINCE. UNUSED_P
100e8 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b ARAMETER(nFull);
100e9 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 . /* WinCE has
100ea 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 no concept of a
100eb 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
100ec 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f e, or so I am to
100ed 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ld. */. sqlite3
100ee 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e _snprintf(pVfs->
100ef 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c mxPathname, zFul
100f0 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 l, "%s", zRelati
100f1 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ve);. return SQ
100f2 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a LITE_OK;.#endif.
100f3 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
100f4 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 WINCE && !define
100f5 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 d(__CYGWIN__).
100f6 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 int nByte;. voi
100f7 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 d *zConverted;.
100f8 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 char *zOut;. U
100f9 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
100fa 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 nFull);. zConve
100fb 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 rted = convertUt
100fc 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 f8Filename(zRela
100fd 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e tive);. if( isN
100fe 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 T() ){. WCHAR
100ff 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 *zTemp;. nBy
10100 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 te = GetFullPath
10101 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 NameW((WCHAR*)zC
10102 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 onverted, 0, 0,
10103 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 0) + 3;. zTem
10104 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 p = malloc( nByt
10105 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 e*sizeof(zTemp[0
10106 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 ]) );. if( zT
10107 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 emp==0 ){.
10108 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
10109 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
1010a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1010b 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 }. GetFullPa
1010c 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 thNameW((WCHAR*)
1010d 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 zConverted, nByt
1010e 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 e, zTemp, 0);.
1010f 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
10110 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 d);. zOut = u
10111 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 nicodeToUtf8(zTe
10112 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 mp);. free(zT
10113 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 emp);./* isNT()
10114 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f is 1 if SQLITE_O
10115 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 S_WINCE==1, so t
10116 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 his else is neve
10117 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 r executed. .**
10118 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 Since the ASCII
10119 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 version of these
1011a 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 Windows API do
1011b 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 not exist for WI
1011c 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 NCE,.** it's imp
1011d 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 ortant to not re
1011e 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 ference them for
1011f 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a WINCE builds..*
10120 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
10121 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
10122 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d {. char *zTem
10123 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 p;. nByte = G
10124 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 etFullPathNameA(
10125 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 (char*)zConverte
10126 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b d, 0, 0, 0) + 3;
10127 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c . zTemp = mal
10128 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f loc( nByte*sizeo
10129 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 f(zTemp[0]) );.
1012a 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 if( zTemp==0
1012b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 ){. free(zC
1012c 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 onverted);.
1012d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1012e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
1012f 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 GetFullPathNameA
10130 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 ((char*)zConvert
10131 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 ed, nByte, zTemp
10132 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a , 0);. free(z
10133 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
10134 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 zOut = sqlite3_w
10135 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 in32_mbcs_to_utf
10136 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 8(zTemp);. fr
10137 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 ee(zTemp);.#endi
10138 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 f. }. if( zOut
10139 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1013a 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d snprintf(pVfs->m
1013b 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c xPathname, zFull
1013c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 , "%s", zOut);.
1013d 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 free(zOut);.
1013e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1013f 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
10140 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10141 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 NOMEM;. }.#endi
10142 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 f.}../*.** Get t
10143 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f he sector size o
10144 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65 f the device use
10145 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 d to store.** fi
10146 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
10147 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
10148 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
10149 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
1014a 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
1014b 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
1014c 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 ile name */.){.
1014d 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53 DWORD bytesPerS
1014e 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 ector = SQLITE_D
1014f 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
10150 5a 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b ZE;. /* GetDisk
10151 46 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74 FreeSpace is not
10152 20 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72 supported under
10153 20 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 WINCE */.#if SQ
10154 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
10155 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10156 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 (pVfs);. UNUSED
10157 5f 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 _PARAMETER(zRela
10158 74 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 tive);.#else. c
10159 68 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 har zFullpath[MA
1015a 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 X_PATH+1];. int
1015b 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 rc;. DWORD dwR
1015c 65 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 et = 0;. DWORD
1015d 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 dwDummy;.. /*.
1015e 20 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67 ** We need to g
1015f 65 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 et the full path
10160 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c name of the fil
10161 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 e. ** to get th
10162 65 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74 e drive letter t
10163 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 o look up the se
10164 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a ctor. ** size..
10165 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 */. rc = winF
10166 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
10167 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 , zRelative, MAX
10168 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 _PATH, zFullpath
10169 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53 );. if( rc == S
1016a 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 QLITE_OK ). {.
1016b 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 void *zConver
1016c 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
1016d 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 8Filename(zFullp
1016e 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 ath);. if( zC
1016f 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20 onverted ){.
10170 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
10171 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 /* trim
10172 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69 path to just dri
10173 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a ve reference */.
10174 20 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70 WCHAR *p
10175 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 = zConverted;.
10176 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 for(;*p;p
10177 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
10178 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b f( *p == '\\' ){
10179 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 . *p
1017a 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 = '\0';.
1017b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1017c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1017d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d . dwRet =
1017e 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 GetDiskFreeSpac
1017f 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
10180 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 erted,.
10181 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10182 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d &dwDumm
10183 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
10184 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10185 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 &bytesPerSe
10186 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 ctor,.
10187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10188 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 &dwDummy
10189 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1018a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1018b 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 &dwDummy);.
1018c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1018d 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 /* trim path
1018e 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 to just drive r
1018f 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 eference */.
10190 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63 char *p = (c
10191 68 61 72 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 har *)zConverted
10192 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a ;. for(;*
10193 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 p;p++){.
10194 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 if( *p == '\\'
10195 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
10196 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 *p = '\0';.
10197 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
10198 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10199 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 }. dwRe
1019a 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 t = GetDiskFreeS
1019b 70 61 63 65 41 28 28 63 68 61 72 2a 29 7a 43 6f paceA((char*)zCo
1019c 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 nverted,.
1019d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1019e 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 &dwDu
1019f 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 mmy,.
101a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
101a1 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72 &bytesPer
101a2 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 Sector,.
101a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
101a4 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d &dwDum
101a5 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 my,.
101a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
101a7 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b &dwDummy);
101a8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
101a9 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
101aa 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
101ab 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62 dwRet ){. b
101ac 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 ytesPerSector =
101ad 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 SQLITE_DEFAULT_S
101ae 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 ECTOR_SIZE;.
101af 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
101b0 65 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 eturn (int) byte
101b1 73 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a sPerSector; .}..
101b2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
101b3 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
101b4 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 ON./*.** Interfa
101b5 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
101b6 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
101b7 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
101b8 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
101b9 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
101ba 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
101bb 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
101bc 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e ary..*/./*.** In
101bd 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 terfaces for ope
101be 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 ning a shared li
101bf 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 brary, finding e
101c0 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 ntry points.** w
101c1 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 ithin the shared
101c2 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c library, and cl
101c3 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 osing the shared
101c4 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 library..*/.sta
101c5 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f tic void *winDlO
101c6 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 pen(sqlite3_vfs
101c7 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 *pVfs, const cha
101c8 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
101c9 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 HANDLE h;. voi
101ca 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 d *zConverted =
101cb 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
101cc 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ame(zFilename);.
101cd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
101ce 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 ER(pVfs);. if(
101cf 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b zConverted==0 ){
101d0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
101d1 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 }. if( isNT()
101d2 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c ){. h = LoadL
101d3 69 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 ibraryW((WCHAR*)
101d4 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 zConverted);./*
101d5 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
101d6 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
101d7 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
101d8 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
101d9 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
101da 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
101db 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
101dc 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
101dd 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
101de 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
101df 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
101e0 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
101e1 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
101e2 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
101e3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d }else{. h =
101e4 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 LoadLibraryA((c
101e5 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 har*)zConverted)
101e6 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 ;.#endif. }. f
101e7 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
101e8 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
101e9 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 )h;.}.static voi
101ea 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c d winDlError(sql
101eb 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
101ec 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
101ed 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 zBufOut){. UNUS
101ee 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
101ef 73 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 s);. getLastErr
101f0 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 orMsg(nBuf, zBuf
101f1 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 Out);.}.void (*w
101f2 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f inDlSym(sqlite3_
101f3 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
101f4 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 *pHandle, const
101f5 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 char *zSymbol))(
101f6 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f void){. UNUSED_
101f7 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
101f8 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
101f9 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 INCE. /* The Ge
101fa 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 tProcAddressA()
101fb 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 routine is only
101fc 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e available on win
101fd 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ce. */. return
101fe 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 (void(*)(void))G
101ff 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 etProcAddressA((
10200 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 HANDLE)pHandle,
10201 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a zSymbol);.#else.
10202 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 /* All other w
10203 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 indows platforms
10204 20 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 expect GetProcA
10205 64 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 ddress() to take
10206 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 . ** an Ansi st
10207 72 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 ring regardless
10208 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 of the _UNICODE
10209 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 setting */. ret
1020a 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 urn (void(*)(voi
1020b 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 d))GetProcAddres
1020c 73 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c s((HANDLE)pHandl
1020d 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e e, zSymbol);.#en
1020e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c dif.}.void winDl
1020f 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
10210 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
10211 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 Handle){. UNUSE
10212 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
10213 29 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 );. FreeLibrary
10214 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 ((HANDLE)pHandle
10215 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 );.}.#else /* if
10216 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
10217 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 D_EXTENSION is d
10218 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 efined: */. #de
10219 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 fine winDlOpen
1021a 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 0. #define winD
1021b 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 lError 0. #defi
1021c 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a ne winDlSym 0.
1021d 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 #define winDlC
1021e 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a lose 0.#endif...
1021f 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 /*.** Write up t
10220 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 o nBuf bytes of
10221 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 randomness into
10222 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zBuf..*/.static
10223 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 int winRandomnes
10224 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 s(sqlite3_vfs *p
10225 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 Vfs, int nBuf, c
10226 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e har *zBuf){. in
10227 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 t n = 0;. UNUSE
10228 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
10229 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
1022a 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 QLITE_TEST). n
1022b 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 = nBuf;. memset
1022c 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b (zBuf, 0, nBuf);
1022d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a .#else. if( siz
1022e 65 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c eof(SYSTEMTIME)<
1022f 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 =nBuf-n ){. S
10230 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 YSTEMTIME x;.
10231 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 GetSystemTime(&
10232 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 x);. memcpy(&
10233 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a zBuf[n], &x, siz
10234 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b eof(x));. n +
10235 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d = sizeof(x);. }
10236 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 . if( sizeof(DW
10237 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a ORD)<=nBuf-n ){.
10238 20 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 DWORD pid =
10239 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 GetCurrentProces
1023a 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 sId();. memcp
1023b 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 y(&zBuf[n], &pid
1023c 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a , sizeof(pid));.
1023d 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 n += sizeof(
1023e 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pid);. }. if(
1023f 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e sizeof(DWORD)<=n
10240 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f Buf-n ){. DWO
10241 52 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b RD cnt = GetTick
10242 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d Count();. mem
10243 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 cpy(&zBuf[n], &c
10244 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 nt, sizeof(cnt))
10245 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
10246 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 f(cnt);. }. if
10247 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 ( sizeof(LARGE_I
10248 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 NTEGER)<=nBuf-n
10249 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 ){. LARGE_INT
1024a 45 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 EGER i;. Quer
1024b 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e yPerformanceCoun
1024c 74 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d ter(&i);. mem
1024d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 cpy(&zBuf[n], &i
1024e 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 , sizeof(i));.
1024f 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 n += sizeof(i)
10250 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
10251 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn n;.}.../*.
10252 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c ** Sleep for a l
10253 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 ittle while. Re
10254 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 turn the amount
10255 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a of time slept..*
10256 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
10257 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
10258 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 s *pVfs, int mic
10259 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 rosec){. Sleep(
1025a 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 (microsec+999)/1
1025b 30 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 000);. UNUSED_P
1025c 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
1025d 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f return ((micro
1025e 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 sec+999)/1000)*1
1025f 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 000;.}../*.** Th
10260 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
10261 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 able, if set to
10262 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
10263 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 , becomes the re
10264 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 sult.** returned
10265 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 from sqlite3OsC
10266 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 urrentTime(). T
10267 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
10268 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 testing..*/.#ifd
10269 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1026a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1026b 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
1026c 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a me = 0;.#endif..
1026d 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 /*.** Find the c
1026e 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 urrent time (in
1026f 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 Universal Coordi
10270 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 nated Time). Wr
10271 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 ite the.** curre
10272 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 nt time and date
10273 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 as a Julian Day
10274 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 number into *pr
10275 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 Now and.** retur
10276 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 n 0. Return 1 i
10277 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 f the time and d
10278 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f ate cannot be fo
10279 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 und..*/.int winC
1027a 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
1027b 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f e3_vfs *pVfs, do
1027c 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 uble *prNow){.
1027d 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f FILETIME ft;. /
1027e 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 * FILETIME struc
1027f 74 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 ture is a 64-bit
10280 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 value represent
10281 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
10282 66 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f f . 100-nano
10283 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 second intervals
10284 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 since January 1
10285 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 , 1601 (= JD 230
10286 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 5813.5). . */.
10287 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 sqlite3_int64 t
10288 69 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 imeW; /* Whole
10289 20 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 days */. sqlit
1028a 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 e3_int64 timeF;
1028b 20 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 /* Fractional
1028c 44 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 Days */.. /* Nu
1028d 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f mber of 100-nano
1028e 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 second intervals
1028f 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 in a single day
10290 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e */. static con
10291 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 st sqlite3_int64
10292 20 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 ntuPerDay = .
10293 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 10000000*(sq
10294 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 lite3_int64)8640
10295 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0;.. /* Number
10296 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e of 100-nanosecon
10297 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 d intervals in h
10298 61 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a alf of a day */.
10299 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
1029a 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 qlite3_int64 ntu
1029b 50 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 PerHalfDay = .
1029c 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 10000000*(sq
1029d 6c 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 lite3_int64)4320
1029e 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 0;.. /* 2^32 -
1029f 74 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 to avoid use of
102a0 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 LL and warnings
102a1 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 in gcc */. stat
102a2 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
102a3 5f 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 _int64 max32BitV
102a4 61 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 alue = . (s
102a5 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 qlite3_int64)200
102a6 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 0000000 + (sqlit
102a7 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 e3_int64)2000000
102a8 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 000 + (sqlite3_i
102a9 6e 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a nt64)294967296;.
102aa 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
102ab 49 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d INCE. SYSTEMTIM
102ac 45 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 E time;. GetSys
102ad 74 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a temTime(&time);.
102ae 20 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 /* if SystemTi
102af 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 meToFileTime() f
102b0 61 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 ails, it returns
102b1 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 zero. */. if (
102b2 21 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c !SystemTimeToFil
102b3 65 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 eTime(&time,&ft)
102b4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
102b5 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 . }.#else. Get
102b6 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 SystemTimeAsFile
102b7 54 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e Time( &ft );.#en
102b8 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 dif. UNUSED_PAR
102b9 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
102ba 74 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 timeW = (((sqlit
102bb 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 e3_int64)ft.dwHi
102bc 67 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 ghDateTime)*max3
102bd 32 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 2BitValue) + (sq
102be 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 lite3_int64)ft.d
102bf 77 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 wLowDateTime;.
102c0 74 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 timeF = timeW %
102c1 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 ntuPerDay;
102c2 20 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 /* fractiona
102c3 6c 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f l days (100-nano
102c4 73 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 seconds) */. ti
102c5 6d 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 meW = timeW / nt
102c6 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 uPerDay;
102c7 20 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 /* whole days
102c8 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d */. timeW = tim
102c9 65 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 eW + 2305813;
102ca 20 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 /* add
102cb 77 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d whole days (from
102cc 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 2305813.5) */.
102cd 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b timeF = timeF +
102ce 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 ntuPerHalfDay;
102cf 20 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 /* add half
102d0 20 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 a day (from 230
102d1 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 5813.5) */. tim
102d2 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 eW = timeW + (ti
102d3 6d 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 meF/ntuPerDay);
102d4 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 /* add whole da
102d5 79 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 y if half day ma
102d6 64 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 de one */. time
102d7 46 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 F = timeF % ntuP
102d8 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 erDay;
102d9 2f 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 /* compute new f
102da 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a ractional days *
102db 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f /. *prNow = (do
102dc 75 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 uble)timeW + ((d
102dd 6f 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 ouble)timeF / (d
102de 6f 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 ouble)ntuPerDay)
102df 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
102e0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
102e1 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
102e2 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
102e3 28 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 ((double)sqlite3
102e4 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 _current_time +
102e5 28 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f (double)43200) /
102e6 20 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b (double)86400 +
102e7 20 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 (double)2440587
102e8 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
102e9 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
102ea 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68 * The idea is th
102eb 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e at this function
102ec 20 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f works like a co
102ed 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 mbination of.**
102ee 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 GetLastError() a
102ef 6e 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 nd FormatMessage
102f0 28 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f () on windows (o
102f1 72 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 r errno and.** s
102f2 74 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 trerror_r() on u
102f3 6e 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 nix). After an e
102f4 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
102f5 20 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e by an OS.** fun
102f6 63 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 ction, SQLite ca
102f7 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
102f8 6e 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e n with zBuf poin
102f9 74 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 ting to.** a buf
102fa 66 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 fer of nBuf byte
102fb 73 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 s. The OS layer
102fc 73 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 should populate
102fd 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 the.** buffer wi
102fe 74 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 th a nul-termina
102ff 74 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 ted UTF-8 encode
10300 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a d error message.
10301 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 ** describing th
10302 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 e last IO error
10303 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 to have occurred
10304 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c within the call
10305 69 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a ing.** thread..*
10306 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f *.** If the erro
10307 72 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f r message is too
10308 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 large for the s
10309 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a upplied buffer,.
1030a 2a 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 ** it should be
1030b 74 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 truncated. The r
1030c 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 eturn value of x
1030d 47 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 GetLastError.**
1030e 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 is zero if the e
1030f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 rror message fit
10310 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c s in the buffer,
10311 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 or non-zero.**
10312 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 otherwise (if th
10313 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 e message was tr
10314 75 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e uncated). If non
10315 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 -zero is returne
10316 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 d,.** then it is
10317 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 not necessary t
10318 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 o include the nu
10319 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 l-terminator cha
1031a 72 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 racter.** in the
1031b 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a output buffer..
1031c 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 **.** Not supply
1031d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ing an error mes
1031e 73 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e sage will have n
1031f 6f 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74 o adverse effect
10320 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 .** on SQLite. I
10321 74 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 t is fine to hav
10322 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e an implementat
10323 69 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a ion that never.*
10324 2a 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 * returns an err
10325 6f 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a or message:.**.*
10326 2a 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 * int xGetLast
10327 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
10328 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
10329 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
1032a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 ** assert(zB
1032b 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a uf[0]=='\0');.**
1032c 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a return 0;.*
1032d 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 * }.**.** Howe
1032e 76 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ver if an error
1032f 6d 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c message is suppl
10330 69 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 ied, it will be
10331 69 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 incorporated.**
10332 62 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 by sqlite into t
10333 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
10334 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 available to th
10335 65 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 e user using.**
10336 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
10337 2c 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e , possibly makin
10338 67 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 g IO errors easi
10339 65 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a er to debug..*/.
1033a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 static int winGe
1033b 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
1033c 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
1033d 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
1033e 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 uf){. UNUSED_PA
1033f 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
10340 20 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 return getLastE
10341 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 rrorMsg(nBuf, zB
10342 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e uf);.}../*.** In
10343 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
10344 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
10345 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
10346 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c nterface..*/.SQL
10347 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
10348 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
10349 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
1034a 74 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d te3_vfs winVfs =
1034b 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
1034c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
1034d 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a rsion */. siz
1034e 65 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 eof(winFile),
1034f 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 /* szOsFile */.
10350 20 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 MAX_PATH,
10351 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
10352 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
10353 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10354 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 pNext */. "w
10355 69 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 in32",
10356 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
10357 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
10358 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
10359 2a 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e */. . winOpen
1035a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
1035b 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 Open */. winD
1035c 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f elete, /
1035d 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 * xDelete */.
1035e 20 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 winAccess,
1035f 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a /* xAccess *
10360 2f 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 /. winFullPat
10361 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c hname, /* xFul
10362 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 lPathname */.
10363 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 winDlOpen,
10364 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a /* xDlOpen *
10365 2f 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 /. winDlError
10366 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 , /* xDlE
10367 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 rror */. winD
10368 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f lSym, /
10369 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 * xDlSym */.
1036a 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 winDlClose,
1036b 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a /* xDlClose *
1036c 2f 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e /. winRandomn
1036d 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e ess, /* xRan
1036e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 domness */. w
1036f 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 inSleep,
10370 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 /* xSleep */.
10371 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d winCurrentTim
10372 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e e, /* xCurren
10373 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e tTime */. win
10374 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 GetLastError
10375 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 /* xGetLastError
10376 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 */. };.. sqli
10377 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 te3_vfs_register
10378 28 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 (&winVfs, 1);.
10379 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1037a 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 ; .}.SQLITE_API
1037b 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 int sqlite3_os_e
1037c 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 nd(void){ . ret
1037d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1037e 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
1037f 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a TE_OS_WIN */../*
10380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
10381 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a d of os_win.c **
10382 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
10386 67 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e gin file bitvec.
10387 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
10388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1038a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72 .** 2008 Februar
1038b 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 16.**.** The a
1038c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1038d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1038e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1038f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10390 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10391 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10392 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10393 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10394 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10395 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10396 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10397 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10398 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10399 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1039a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1039b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1039c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1039d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1039e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1039f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
103a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
103a1 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
103a2 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f implements an o
103a3 62 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65 bject that repre
103a4 73 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65 sents a fixed-le
103a5 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 ngth.** bitmap.
103a6 20 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72 Bits are number
103a7 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 ed starting with
103a8 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 1..**.** A bitm
103a9 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ap is used to re
103aa 63 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73 cord which pages
103ab 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 of a database f
103ac 69 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a ile have been.**
103ad 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 journalled duri
103ae 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ng a transaction
103af 2c 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73 , or which pages
103b0 20 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d have the "dont-
103b1 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 write".** proper
103b2 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c ty. Usually onl
103b3 79 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72 y a few pages ar
103b4 65 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f e meet either co
103b5 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 ndition..** So t
103b6 68 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75 he bitmap is usu
103b7 61 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20 ally sparse and
103b8 68 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c has low cardinal
103b9 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 ity..** But some
103ba 74 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 times (for examp
103bb 6c 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61 le when during a
103bc 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 DROP of a large
103bd 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 table) most.**
103be 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 or all of the pa
103bf 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 ges in a databas
103c0 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 e can get journa
103c1 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 lled. In those
103c2 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 cases, .** the b
103c3 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65 itmap becomes de
103c4 6e 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61 nse with high ca
103c5 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 rdinality. The
103c6 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 algorithm needs
103c7 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f .** to handle bo
103c8 74 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a th cases well..*
103c9 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
103ca 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66 the bitmap is f
103cb 69 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62 ixed when the ob
103cc 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e ject is created.
103cd 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 .**.** All bits
103ce 61 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74 are clear when t
103cf 68 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65 he bitmap is cre
103d0 61 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61 ated. Individua
103d1 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 l bits.** may be
103d2 20 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20 set or cleared
103d3 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a one at a time..*
103d4 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74 *.** Test operat
103d5 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31 ions are about 1
103d6 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 00 times more co
103d7 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70 mmon that set op
103d8 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 erations..** Cle
103d9 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 ar operations ar
103da 65 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61 e exceedingly ra
103db 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75 re. There are u
103dc 73 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a sually between.*
103dd 2a 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20 * 5 and 500 set
103de 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42 operations per B
103df 69 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 itvec object, th
103e0 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20 ough the number
103e1 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 of sets can.** s
103e2 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e ometimes grow in
103e3 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 to tens of thous
103e4 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 ands or larger.
103e5 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
103e6 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 .** Bitvec objec
103e7 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
103e8 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
103e9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 database file at
103ea 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 the.** start of
103eb 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 a transaction,
103ec 61 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61 and is thus usua
103ed 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20 lly less than a
103ee 66 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a few thousand,.**
103ef 20 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c but can be as l
103f0 61 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f arge as 2 billio
103f1 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 n for a really b
103f2 69 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a ig database..*/.
103f3 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 ./* Size of the
103f4 42 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 Bitvec structure
103f5 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64 in bytes. */.#d
103f6 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 20 efine BITVEC_SZ
103f7 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 76 (sizeof(v
103f8 6f 69 64 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35 oid*)*128) /* 5
103f9 31 32 20 6f 6e 20 33 32 62 69 74 2e 20 20 31 30 12 on 32bit. 10
103fa 32 34 20 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a 24 on 64bit */..
103fb 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 /* Round the uni
103fc 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 on size down to
103fd 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e the nearest poin
103fe 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 ter boundary, si
103ff 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a nce that's how .
10400 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c ** it will be al
10401 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 igned within the
10402 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20 Bitvec struct.
10403 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
10404 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42 C_USIZE (((B
10405 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 ITVEC_SZ-(3*size
10406 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 of(u32)))/sizeof
10407 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f (Bitvec*))*sizeo
10408 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 f(Bitvec*))../*
10409 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61 Type of the arra
1040a 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 y "element" for
1040b 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65 the bitmap repre
1040c 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 sentation. .** S
1040d 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72 hould be a power
1040e 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c of 2, and ideal
1040f 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64 ly, evenly divid
10410 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 e into BITVEC_US
10411 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 IZE. .** Setting
10412 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61 this to the "na
10413 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 tural word" size
10414 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79 of your CPU may
10415 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 improve.** perf
10416 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 ormance. */.#def
10417 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d ine BITVEC_TELEM
10418 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c u8./* Size,
10419 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65 in bits, of the
1041a 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e bitmap element.
1041b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
1041c 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f EC_SZELEM 8./
1041d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
1041e 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70 ents in a bitmap
1041f 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 array. */.#defi
10420 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 ne BITVEC_NELEM
10421 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a (BITVEC_USIZ
10422 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f E/sizeof(BITVEC_
10423 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 TELEM))./* Numbe
10424 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 r of bits in the
10425 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a bitmap array. *
10426 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
10427 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56 _NBIT (BITV
10428 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f EC_NELEM*BITVEC_
10429 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 SZELEM)../* Numb
1042a 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73 er of u32 values
1042b 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 in hash table.
1042c 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
1042d 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 C_NINT (BIT
1042e 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 VEC_USIZE/sizeof
1042f 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 (u32))./* Maximu
10430 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 m number of entr
10431 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c ies in hash tabl
10432 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 e before .** sub
10433 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 -dividing and re
10434 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 -hashing. */.#de
10435 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 fine BITVEC_MXHA
10436 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 SH (BITVEC_NI
10437 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 NT/2)./* Hashing
10438 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 function for th
10439 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e e aHash represen
1043a 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 tation..** Empir
1043b 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f ical testing sho
1043c 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37 wed that the *37
1043d 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 multiplier .**
1043e 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72 (an arbitrary pr
1043f 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20 ime)in the hash
10440 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 function provide
10441 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 d .** no fewer c
10442 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 ollisions than t
10443 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a he no-op *1. */.
10444 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 #define BITVEC_H
10445 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31 ASH(X) (((X)*1
10446 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a )%BITVEC_NINT)..
10447 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e #define BITVEC_N
10448 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43 PTR (BITVEC
10449 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 _USIZE/sizeof(Bi
1044a 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a tvec *)).../*.**
1044b 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 A bitmap is an
1044c 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
1044d 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
1044e 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ure..**.** This
1044f 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74 bitmap records t
10450 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 he existance of
10451 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 zero or more bit
10452 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73 s.** with values
10453 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 between 1 and i
10454 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e Size, inclusive.
10455 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
10456 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 three possible
10457 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
10458 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a of the bitmap..*
10459 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 * If iSize<=BITV
1045a 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 EC_NBIT, then Bi
1045b 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d tvec.u.aBitmap[]
1045c 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a is a straight.*
1045d 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c * bitmap. The l
1045e 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 east significant
1045f 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a bit is bit 1..*
10460 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 *.** If iSize>BI
10461 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 TVEC_NBIT and iD
10462 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 ivisor==0 then B
10463 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 itvec.u.aHash[]
10464 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62 is.** a hash tab
10465 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c le that will hol
10466 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d d up to BITVEC_M
10467 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76 XHASH distinct v
10468 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 alues..**.** Oth
10469 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 erwise, the valu
1046a 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65 e i is redirecte
1046b 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 d into one of BI
1046c 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 TVEC_NPTR.** sub
1046d 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 -bitmaps pointed
1046e 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e to by Bitvec.u.
1046f 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 apSub[]. Each s
10470 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 ubbitmap.** hand
10471 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73 les up to iDivis
10472 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75 or separate valu
10473 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b es of i. apSub[
10474 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 0] holds.** valu
10475 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 es between 1 and
10476 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 iDivisor. apSu
10477 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 b[1] holds value
10478 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 s between.** iDi
10479 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 visor+1 and 2*iD
1047a 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e ivisor. apSub[N
1047b 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 ] holds values b
1047c 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 etween.** N*iDiv
1047d 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 isor+1 and (N+1)
1047e 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68 *iDivisor. Each
1047f 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f subbitmap is no
10480 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 rmalized.** to h
10481 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61 old deal with va
10482 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 lues between 1 a
10483 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a nd iDivisor..*/.
10484 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a struct Bitvec {.
10485 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20 u32 iSize;
10486 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 /* Maximum bit
10487 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69 index. Max iSi
10488 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c ze is 4,294,967,
10489 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 296. */. u32 nS
1048a 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d et; /* Num
1048b 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74 ber of bits that
1048c 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 are set - only
1048d 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a valid for aHash.
1048e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1048f 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d ** element. M
10490 61 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e ax is BITVEC_NIN
10491 54 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53 T. For BITVEC_S
10492 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20 Z of 512,.
10493 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
10494 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35 his would be 125
10495 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69 . */. u32 iDivi
10496 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 sor; /* Number
10497 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64 of bits handled
10498 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d by each apSub[]
10499 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 entry. */.
1049a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1049b 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 Should >=0 for a
1049c 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f pSub element. */
1049d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1049e 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 /* Max iDivis
1049f 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f or is max(u32) /
104a0 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 BITVEC_NPTR + 1
104a1 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 . */.
104a2 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 /* For a
104a3 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 BITVEC_SZ of 51
104a4 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 2, this would be
104a5 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 34,359,739. */.
104a6 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 union {. BI
104a7 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d TVEC_TELEM aBitm
104a8 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d ap[BITVEC_NELEM]
104a9 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 ; /* Bitmap r
104aa 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f epresentation */
104ab 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 . u32 aHash[B
104ac 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 ITVEC_NINT];
104ad 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
104ae 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
104af 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 /. Bitvec *ap
104b0 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d Sub[BITVEC_NPTR]
104b1 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 ; /* Recursive
104b2 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
104b3 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a /. } u;.};../*.
104b4 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
104b5 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 bitmap object ab
104b6 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 le to handle bit
104b7 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 s between 0 and
104b8 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 iSize,.** inclus
104b9 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 ive. Return a p
104ba 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
104bb 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72 w object. Retur
104bc 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 n NULL if .** ma
104bd 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 lloc fails..*/.S
104be 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 QLITE_PRIVATE Bi
104bf 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 tvec *sqlite3Bit
104c0 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53 vecCreate(u32 iS
104c1 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a ize){. Bitvec *
104c2 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a p;. assert( siz
104c3 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f eof(*p)==BITVEC_
104c4 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 SZ );. p = sqli
104c5 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
104c6 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 izeof(*p) );. i
104c7 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 f( p ){. p->i
104c8 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 Size = iSize;.
104c9 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
104ca 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
104cb 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20 see if the i-th
104cc 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74 bit is set. Ret
104cd 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73 urn true or fals
104ce 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 e..** If p is NU
104cf 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61 LL (if the bitma
104d0 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 p has not been c
104d1 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a reated) or if.**
104d2 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e i is out of ran
104d3 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 ge, then return
104d4 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
104d5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
104d6 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 ite3BitvecTest(B
104d7 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 itvec *p, u32 i)
104d8 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
104d9 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 eturn 0;. if( i
104da 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d >p->iSize || i==
104db 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
104dc 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d i--;. while( p-
104dd 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 >iDivisor ){.
104de 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e u32 bin = i/p->
104df 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 iDivisor;. i
104e0 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i%p->iDivisor;
104e1 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 . p = p->u.ap
104e2 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 Sub[bin];. if
104e3 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 (!p) {. re
104e4 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
104e5 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 }. if( p->iSize
104e6 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b <=BITVEC_NBIT ){
104e7 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e . return (p->
104e8 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 u.aBitmap[i/BITV
104e9 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c EC_SZELEM] & (1<
104ea 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c <(i&(BITVEC_SZEL
104eb 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d EM-1))))!=0;. }
104ec 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 else{. u32 h
104ed 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 = BITVEC_HASH(i
104ee 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ++);. while(
104ef 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b p->u.aHash[h] ){
104f0 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e . if( p->u.
104f1 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 aHash[h]==i ) re
104f2 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 turn 1;. h
104f3 3d 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43 = (h+1) % BITVEC
104f4 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _NINT;. }.
104f5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d return 0;. }.}
104f6 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
104f7 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 i-th bit. Retur
104f8 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 n 0 on success a
104f9 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
104fa 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 if.** anything
104fb 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a goes wrong..**.*
104fc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
104fd 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 ight cause sub-b
104fe 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c itmaps to be all
104ff 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 ocated. Failing
10500 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d .** to get the m
10501 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 emory needed to
10502 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 hold the sub-bit
10503 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a map is the only.
10504 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 ** that can go w
10505 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 rong with an ins
10506 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 ert, assuming p
10507 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e and i are valid.
10508 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 .**.** The calli
10509 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 ng function must
1050a 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 ensure that p i
1050b 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 s a valid Bitvec
1050c 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 object.** and t
1050d 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f hat the value fo
1050e 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 r "i" is within
1050f 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 range of the Bit
10510 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f vec object..** O
10511 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 therwise the beh
10512 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
10513 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
10514 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
10515 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 3BitvecSet(Bitve
10516 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 c *p, u32 i){.
10517 75 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d u32 h;. if( p==
10518 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
10519 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1051a 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 i>0 );. assert(
1051b 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a i<=p->iSize );.
1051c 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 i--;. while((
1051d 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45 p->iSize > BITVE
1051e 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 C_NBIT) && p->iD
1051f 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 ivisor) {. u3
10520 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 2 bin = i/p->iDi
10521 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 visor;. i = i
10522 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 %p->iDivisor;.
10523 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 if( p->u.apSub
10524 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 [bin]==0 ){.
10525 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e p->u.apSub[bin
10526 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 ] = sqlite3Bitve
10527 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76 cCreate( p->iDiv
10528 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66 isor );. if
10529 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e ( p->u.apSub[bin
1052a 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 ]==0 ) return SQ
1052b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
1052c 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 }. p = p->u.a
1052d 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 pSub[bin];. }.
1052e 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 if( p->iSize<=B
1052f 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 ITVEC_NBIT ){.
10530 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 p->u.aBitmap[i
10531 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 /BITVEC_SZELEM]
10532 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56 |= 1 << (i&(BITV
10533 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 EC_SZELEM-1));.
10534 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
10535 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 _OK;. }. h = B
10536 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b ITVEC_HASH(i++);
10537 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77 . /* if there w
10538 61 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c asn't a hash col
10539 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 lision, and this
1053a 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a doesn't */. /*
1053b 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c completely fill
1053c 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20 the hash, then
1053d 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68 just add it with
1053e 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 out */. /* worr
1053f 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69 ing about sub-di
10540 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 viding and re-ha
10541 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 shing. */. if(
10542 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 !p->u.aHash[h] )
10543 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 {. if (p->nSe
10544 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 t<(BITVEC_NINT-1
10545 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 )) {. goto
10546 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a bitvec_set_end;.
10547 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
10548 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 goto bitvec_s
10549 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d et_rehash;. }
1054a 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 . }. /* there
1054b 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c was a collision,
1054c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 check to see if
1054d 20 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f it's already */
1054e 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 . /* in hash, i
1054f 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 f not, try to fi
10550 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74 nd a spot for it
10551 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 */. do {. i
10552 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d f( p->u.aHash[h]
10553 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==i ) return SQL
10554 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b ITE_OK;. h++;
10555 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 . if( h>=BITV
10556 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b EC_NINT ) h = 0;
10557 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 . } while( p->u
10558 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f .aHash[h] );. /
10559 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64 * we didn't find
1055a 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e it in the hash.
1055b 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 h points to th
1055c 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 e first */. /*
1055d 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73 available free s
1055e 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 pot. check to se
1055f 65 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69 e if this is goi
10560 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 ng to */. /* ma
10561 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 ke our hash too
10562 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 "full". */.bitv
10563 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 ec_set_rehash:.
10564 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 if( p->nSet>=BI
10565 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 TVEC_MXHASH ){.
10566 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
10567 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 j;. int rc;.
10568 20 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 u32 *aiValues
10569 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 = sqlite3StackA
1056a 6c 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f llocRaw(0, sizeo
1056b 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a f(p->u.aHash));.
1056c 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 if( aiValues
1056d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
1056e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1056f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
10570 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 memcpy(aiValu
10571 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 es, p->u.aHash,
10572 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 sizeof(p->u.aHas
10573 68 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 h));. memse
10574 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c t(p->u.apSub, 0,
10575 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 sizeof(p->u.apS
10576 75 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 ub));. p->i
10577 44 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 Divisor = (p->iS
10578 69 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 ize + BITVEC_NPT
10579 52 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 R - 1)/BITVEC_NP
1057a 54 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 TR;. rc = s
1057b 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
1057c 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 p, i);. for
1057d 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e (j=0; j<BITVEC_N
1057e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 INT; j++){.
1057f 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b if( aiValues[
10580 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 j] ) rc |= sqlit
10581 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61 e3BitvecSet(p, a
10582 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 iValues[j]);.
10583 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
10584 65 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61 e3StackFree(0, a
10585 69 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 iValues);.
10586 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
10587 0a 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f . }.bitvec_set_
10588 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b end:. p->nSet++
10589 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 ;. p->u.aHash[h
1058a 5d 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 ] = i;. return
1058b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1058c 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d .** Clear the i-
1058d 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 th bit..**.** pB
1058e 75 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 uf must be a poi
1058f 6e 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74 nter to at least
10590 20 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73 BITVEC_SZ bytes
10591 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74 of temporary st
10592 6f 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 orage.** that Bi
10593 74 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73 tvecClear can us
10594 65 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73 e to rebuilt its
10595 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
10596 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10597 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
10598 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 cClear(Bitvec *p
10599 2c 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 , u32 i, void *p
1059a 42 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 Buf){. if( p==0
1059b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
1059c 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d ert( i>0 );. i-
1059d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 -;. while( p->i
1059e 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 Divisor ){. u
1059f 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 32 bin = i/p->iD
105a0 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 ivisor;. i =
105a1 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 i%p->iDivisor;.
105a2 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 p = p->u.apSu
105a3 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 b[bin];. if (
105a4 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 !p) {. retu
105a5 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
105a6 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 if( p->iSize<=BI
105a7 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 TVEC_NBIT ){.
105a8 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f p->u.aBitmap[i/
105a9 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 BITVEC_SZELEM] &
105aa 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 = ~(1 << (i&(BIT
105ab 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b VEC_SZELEM-1)));
105ac 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e . }else{. un
105ad 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 signed int j;.
105ae 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 u32 *aiValues
105af 3d 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 = pBuf;. memc
105b0 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e py(aiValues, p->
105b1 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 u.aHash, sizeof(
105b2 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 p->u.aHash));.
105b3 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 memset(p->u.aH
105b4 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 ash, 0, sizeof(p
105b5 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 ->u.aHash));.
105b6 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 p->nSet = 0;.
105b7 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 for(j=0; j<BIT
105b8 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a VEC_NINT; j++){.
105b9 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 if( aiValu
105ba 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 es[j] && aiValue
105bb 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 s[j]!=(i+1) ){.
105bc 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42 u32 h = B
105bd 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c ITVEC_HASH(aiVal
105be 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 ues[j]-1);.
105bf 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 p->nSet++;.
105c0 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e while( p->
105c1 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 u.aHash[h] ){.
105c2 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 h++;.
105c3 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49 if( h>=BI
105c4 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 TVEC_NINT ) h =
105c5 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
105c6 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b p->u.aHash[
105c7 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d h] = aiValues[j]
105c8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
105c9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 }.}../*.** Des
105ca 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62 troy a bitmap ob
105cb 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 ject. Reclaim a
105cc 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a ll memory used..
105cd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
105ce 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 E void sqlite3Bi
105cf 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 tvecDestroy(Bitv
105d0 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d ec *p){. if( p=
105d1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
105d2 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 f( p->iDivisor )
105d3 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 {. unsigned i
105d4 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
105d5 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 0; i<BITVEC_NPTR
105d6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
105d7 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
105d8 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d oy(p->u.apSub[i]
105d9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 );. }. }. s
105da 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
105db 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
105dc 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
105dd 20 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72 iSize parameter
105de 20 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 specified when
105df 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 Bitvec *p.** was
105e0 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c created..*/.SQL
105e1 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
105e2 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
105e3 65 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 e(Bitvec *p){.
105e4 72 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b return p->iSize;
105e5 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
105e6 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
105e7 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 TEST./*.** Let V
105e8 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f [] be an array o
105e9 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 f unsigned chara
105ea 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74 cters sufficient
105eb 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 to hold.** up t
105ec 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49 o N bits. Let I
105ed 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 be an integer b
105ee 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 etween 0 and N.
105ef 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 0<=I<N..** Then
105f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
105f1 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 acros can be use
105f2 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c d to set, clear,
105f3 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 or test.** indi
105f4 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 vidual bits with
105f5 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 in V..*/.#define
105f6 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20 SETBIT(V,I)
105f7 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c V[I>>3] |= (1<
105f8 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
105f9 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20 CLEARBIT(V,I)
105fa 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c V[I>>3] &= ~(1<
105fb 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
105fc 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20 TESTBIT(V,I)
105fd 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 (V[I>>3]&(1<<(I
105fe 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 &7)))!=0../*.**
105ff 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e This routine run
10600 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74 s an extensive t
10601 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65 est of the Bitve
10602 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 c code..**.** Th
10603 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72 e input is an ar
10604 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 ray of integers
10605 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70 that acts as a p
10606 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 rogram.** to tes
10607 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54 t the Bitvec. T
10608 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 he integers are
10609 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 opcodes followed
1060a 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 .** by 0, 1, or
1060b 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65 3 operands, depe
1060c 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 nding on the opc
1060d 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a ode. Another.**
1060e 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 opcode follows
1060f 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 immediately afte
10610 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61 r the last opera
10611 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 nd..**.** There
10612 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 are 6 opcodes nu
10613 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68 mbered from 0 th
10614 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74 rough 5. 0 is t
10615 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 he.** "halt" opc
10616 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 ode and causes t
10617 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a he test to end..
10618 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 **.** 0
10619 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74 Halt and ret
1061a 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1061b 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 f errors.** 1
1061c 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 N S X Set N
1061d 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 bits beginning w
1061e 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d ith S and increm
1061f 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 enting by X.**
10620 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65 2 N S X Cle
10621 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e ar N bits beginn
10622 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 ing with S and i
10623 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 ncrementing by X
10624 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20 .** 3 N
10625 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 Set N randomly
10626 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 chosen bits.**
10627 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c 4 N Cl
10628 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 ear N randomly c
10629 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 hosen bits.**
1062a 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 5 N S X Set
1062b 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e N bits from S in
1062c 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 crement X in arr
1062d 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 ay only, not in
1062e 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 bitvec.**.** The
1062f 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75 opcodes 1 throu
10630 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74 gh 4 perform set
10631 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61 and clear opera
10632 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72 tions are perfor
10633 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 med.** on both a
10634 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61 Bitvec object a
10635 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 nd on a linear a
10636 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74 rray of bits obt
10637 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
10638 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 c..** Opcode 5 w
10639 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 orks on the line
1063a 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e ar array only, n
1063b 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63 ot on the Bitvec
1063c 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 ..** Opcode 5 is
1063d 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72 used to deliber
1063e 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66 ately induce a f
1063f 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f ault in order to
10640 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 .** confirm that
10641 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e error detection
10642 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 works..**.** At
10643 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
10644 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20 of the test the
10645 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20 linear array is
10646 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 compared.** agai
10647 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f nst the Bitvec o
10648 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 bject. If there
10649 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 are any differe
1064a 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f nces,.** an erro
1064b 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 r is returned.
1064c 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 If they are the
1064d 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 same, zero is re
1064e 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
1064f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
10650 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
10651 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f s, return -1..*/
10652 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10653 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
10654 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 cBuiltinTest(int
10655 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a sz, int *aOp){.
10656 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65 Bitvec *pBitve
10657 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 c = 0;. unsigne
10658 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a d char *pV = 0;.
10659 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 int rc = -1;.
1065a 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 int i, nx, pc,
1065b 6f 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 op;. void *pTmp
1065c 53 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c Space;.. /* All
1065d 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63 ocate the Bitvec
1065e 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e to be tested an
1065f 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 d a linear array
10660 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f of. ** bits to
10661 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65 act as the refe
10662 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 rence */. pBitv
10663 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 ec = sqlite3Bitv
10664 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a ecCreate( sz );.
10665 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d pV = sqlite3_m
10666 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 alloc( (sz+7)/8
10667 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53 70 61 + 1 );. pTmpSpa
10668 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ce = sqlite3_mal
10669 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a loc(BITVEC_SZ);.
1066a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d 30 if( pBitvec==0
1066b 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d || pV==0 || pTm
1066c 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67 6f 74 pSpace==0 ) got
1066d 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 o bitvec_end;.
1066e 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73 memset(pV, 0, (s
1066f 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20 z+7)/8 + 1);..
10670 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 65 63 20 /* NULL pBitvec
10671 74 65 73 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tests */. sqlit
10672 65 33 42 69 74 76 65 63 53 65 74 28 30 2c 20 31 e3BitvecSet(0, 1
10673 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 );. sqlite3Bitv
10674 65 63 43 6c 65 61 72 28 30 2c 20 31 2c 20 70 54 ecClear(0, 1, pT
10675 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20 mpSpace);.. /*
10676 52 75 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 Run the program
10677 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 */. pc = 0;. w
10678 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b hile( (op = aOp[
10679 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 pc])!=0 ){. s
1067a 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
1067b 20 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 case 1:.
1067c 20 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63 case 2:. c
1067d 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 ase 5: {.
1067e 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 nx = 4;.
1067f 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d i = aOp[pc+2] -
10680 20 31 3b 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 1;. aOp[
10681 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b pc+2] += aOp[pc+
10682 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 3];. brea
10683 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
10684 20 63 61 73 65 20 33 3a 0a 20 20 20 20 20 20 63 case 3:. c
10685 61 73 65 20 34 3a 20 0a 20 20 20 20 20 20 64 65 ase 4: . de
10686 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 fault: {.
10687 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 nx = 2;.
10688 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
10689 65 73 73 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 ess(sizeof(i), &
1068a 69 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 i);. brea
1068b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1068c 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b . if( (--aOp[
1068d 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 pc+1]) > 0 ) nx
1068e 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e = 0;. pc += n
1068f 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 20 26 20 x;. i = (i &
10690 30 78 37 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 0x7fffffff)%sz;.
10691 20 20 20 20 69 66 28 20 28 6f 70 20 26 20 31 29 if( (op & 1)
10692 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 45 54 !=0 ){. SET
10693 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a BIT(pV, (i+1));.
10694 20 20 20 20 20 20 69 66 28 20 6f 70 21 3d 35 20 if( op!=5
10695 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 ){. if( s
10696 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
10697 70 42 69 74 76 65 63 2c 20 69 2b 31 29 20 29 20 pBitvec, i+1) )
10698 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b goto bitvec_end;
10699 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1069a 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 41 52 42 se{. CLEARB
1069b 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 IT(pV, (i+1));.
1069c 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
1069d 65 63 43 6c 65 61 72 28 70 42 69 74 76 65 63 2c ecClear(pBitvec,
1069e 20 69 2b 31 2c 20 70 54 6d 70 53 70 61 63 65 29 i+1, pTmpSpace)
1069f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
106a0 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 * Test to make s
106a1 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61 ure the linear a
106a2 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74 rray exactly mat
106a3 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 ches the. ** Bi
106a4 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 tvec object. St
106a5 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 73 art with the ass
106a6 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65 umption that the
106a7 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 y do. ** match
106a8 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 (rc==0). Change
106a9 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 rc to non-zero
106aa 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 79 if a discrepancy
106ab 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a . ** is found..
106ac 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
106ad 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 2c te3BitvecTest(0,
106ae 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 0) + sqlite3Bitv
106af 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 ecTest(pBitvec,
106b0 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20 sz+1).
106b1 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 + sqlite3BitvecT
106b2 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a est(pBitvec, 0).
106b3 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c + (sql
106b4 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 ite3BitvecSize(p
106b5 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 Bitvec) - sz);.
106b6 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b for(i=1; i<=sz;
106b7 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 i++){. if(
106b8 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 29 21 (TESTBIT(pV,i))!
106b9 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 =sqlite3BitvecTe
106ba 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b st(pBitvec,i) ){
106bb 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 . rc = i;.
106bc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
106bd 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 }. }.. /* Free
106be 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63 allocated struc
106bf 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 ture */.bitvec_e
106c0 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 nd:. sqlite3_fr
106c1 65 65 28 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 ee(pTmpSpace);.
106c2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 sqlite3_free(pV
106c3 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 );. sqlite3Bitv
106c4 65 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65 ecDestroy(pBitve
106c5 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
106c6 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
106c7 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
106c8 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a _TEST */../*****
106c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
106ca 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a bitvec.c ******
106cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106cd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
106ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
106cf 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a file pcache.c **
106d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106d2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
106d3 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2008 August 05.*
106d4 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
106d5 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
106d6 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
106d7 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
106d8 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
106d9 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
106da 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
106db 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
106dc 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
106dd 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
106de 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
106df 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
106e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
106e1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
106e2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
106e3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
106e4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
106e5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
106e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
106ea 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
106eb 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20 ments that page
106ec 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a cache..*/../*.**
106ed 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 A complete page
106ee 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 cache is an ins
106ef 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
106f0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
106f1 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 ct PCache {. Pg
106f2 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 Hdr *pDirty, *pD
106f3 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 irtyTail;
106f4 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 /* List of dir
106f5 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 ty pages in LRU
106f6 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 order */. PgHdr
106f7 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 *pSynced;
106f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106f9 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 * Last synced pa
106fa 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 ge in dirty page
106fb 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e list */. int n
106fc 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
106fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106fe 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 * Number of refe
106ff 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a renced pages */.
10700 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 int nMax;
10701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10702 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
10703 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a red cache size *
10704 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 /. int szPage;
10705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10706 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
10707 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e of every page in
10708 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
10709 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 int szExtra;
1070a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1070b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1070c 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 extra space for
1070d 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 each page */. i
1070e 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 nt bPurgeable;
1070f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10710 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 /* True if pa
10711 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 ges are on backi
10712 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e ng store */. in
10713 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 t (*xStress)(voi
10714 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 d*,PgHdr*);
10715 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 /* Call to try
10716 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 make a page cle
10717 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 an */. void *pS
10718 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 tress;
10719 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1071a 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
1071b 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ss */. sqlite3_
1071c 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 pcache *pCache;
1071d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1071e 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d luggable cache m
1071f 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 odule */. PgHdr
10720 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 *pPage1;
10721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10722 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 * Reference to p
10723 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a age 1 */.};../*.
10724 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 ** Some of the a
10725 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 ssert() macros i
10726 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 n this code are
10727 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f too expensive to
10728 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 run.** even dur
10729 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 ing normal debug
1072a 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 ging. Use them
1072b 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c only rarely on l
1072c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 ong-running.** t
1072d 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 ests. Enable th
1072e 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 e expensive asse
1072f 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a rts using the.**
10730 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 -DSQLITE_ENABLE
10731 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
10732 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 T=1 compile-time
10733 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 option..*/.#ifd
10734 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
10735 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
10736 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e T.# define expen
10737 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 sive_assert(X)
10738 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a assert(X).#else.
10739 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 # define expensi
1073a 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e ve_assert(X).#en
1073b 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
1073c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1073d 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 ******** Linked
1073e 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 List Management
1073f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10740 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 ****/..#if !defi
10741 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 ned(NDEBUG) && d
10742 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
10743 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 ABLE_EXPENSIVE_A
10744 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 SSERT)./*.** Che
10745 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 ck that the pCac
10746 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 he->pSynced vari
10747 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 able is set corr
10748 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 ectly. If it.**
10749 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 is not, either f
1074a 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 ail an assert or
1074b 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 return zero. Ot
1074c 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a herwise, return.
1074d 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 ** non-zero. Thi
1074e 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 s is only used i
1074f 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c n debugging buil
10750 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ds, as follows:.
10751 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 **.** expensiv
10752 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 e_assert( pcache
10753 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 CheckSynced(pCac
10754 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 he) );.*/.static
10755 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b int pcacheCheck
10756 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 Synced(PCache *p
10757 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 Cache){. PgHdr
10758 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 *p;. for(p=pCac
10759 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 he->pDirtyTail;
1075a 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 p!=pCache->pSync
1075b 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 ed; p=p->pDirtyP
1075c 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 rev){. assert
1075d 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d ( p->nRef || (p-
1075e 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
1075f 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 D_SYNC) );. }.
10760 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c return (p==0 ||
10761 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e p->nRef || (p->
10762 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
10763 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 _SYNC)==0);.}.#e
10764 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 ndif /* !NDEBUG
10765 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
10766 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
10767 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f T */../*.** Remo
10768 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 ve page pPage fr
10769 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 om the list of d
1076a 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 irty pages..*/.s
1076b 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
1076c 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
1076d 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 List(PgHdr *pPag
1076e 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 e){. PCache *p
1076f 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b = pPage->pCache;
10770 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
10771 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c e->pDirtyNext ||
10772 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 pPage==p->pDirt
10773 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 yTail );. asser
10774 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 t( pPage->pDirty
10775 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 Prev || pPage==p
10776 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f ->pDirty );.. /
10777 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 * Update the PCa
10778 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 che1.pSynced var
10779 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 iable if necessa
1077a 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e ry. */. if( p->
1077b 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 pSynced==pPage )
1077c 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 {. PgHdr *pSy
1077d 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 nced = pPage->pD
1077e 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 irtyPrev;. wh
1077f 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 ile( pSynced &&
10780 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 (pSynced->flags&
10781 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
10782 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 ){. pSynce
10783 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 d = pSynced->pDi
10784 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 rtyPrev;. }.
10785 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 p->pSynced =
10786 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 pSynced;. }..
10787 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 if( pPage->pDirt
10788 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 yNext ){. pPa
10789 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e ge->pDirtyNext->
1078a 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 pDirtyPrev = pPa
1078b 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a ge->pDirtyPrev;.
1078c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1078d 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 ert( pPage==p->p
1078e 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 DirtyTail );.
1078f 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d p->pDirtyTail =
10790 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10791 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 ev;. }. if( pP
10792 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 age->pDirtyPrev
10793 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 ){. pPage->pD
10794 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 irtyPrev->pDirty
10795 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 Next = pPage->pD
10796 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 irtyNext;. }els
10797 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
10798 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 Page==p->pDirty
10799 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 );. p->pDirty
1079a 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 = pPage->pDirty
1079b 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 Next;. }. pPag
1079c 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 e->pDirtyNext =
1079d 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 0;. pPage->pDir
1079e 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 tyPrev = 0;.. e
1079f 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 xpensive_assert(
107a0 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 pcacheCheckSync
107a1 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ed(p) );.}../*.*
107a2 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 * Add page pPage
107a3 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
107a4 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 the dirty list (
107a5 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 PCache1.pDirty i
107a6 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 s set to.** pPag
107a7 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e)..*/.static vo
107a8 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 id pcacheAddToDi
107a9 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 rtyList(PgHdr *p
107aa 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 Page){. PCache
107ab 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 *p = pPage->pCac
107ac 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 he;.. assert( p
107ad 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
107ae 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 ==0 && pPage->pD
107af 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 irtyPrev==0 && p
107b0 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 ->pDirty!=pPage
107b1 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 );.. pPage->pDi
107b2 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 rtyNext = p->pDi
107b3 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 rty;. if( pPage
107b4 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a ->pDirtyNext ){.
107b5 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
107b6 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 e->pDirtyNext->p
107b7 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a DirtyPrev==0 );.
107b8 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 pPage->pDirt
107b9 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 yNext->pDirtyPre
107ba 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 v = pPage;. }.
107bb 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 p->pDirty = pPa
107bc 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 ge;. if( !p->pD
107bd 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 irtyTail ){.
107be 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 p->pDirtyTail =
107bf 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 pPage;. }. if(
107c0 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 !p->pSynced &&
107c1 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 0==(pPage->flags
107c2 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
107c3 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e ) ){. p->pSyn
107c4 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d ced = pPage;. }
107c5 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 . expensive_ass
107c6 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b ert( pcacheCheck
107c7 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a Synced(p) );.}..
107c8 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 /*.** Wrapper ar
107c9 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 ound the pluggab
107ca 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e le caches xUnpin
107cb 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 method. If the
107cc 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e cache is.** bein
107cd 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e g used for an in
107ce 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
107cf 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
107d0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
107d1 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
107d2 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 eUnpin(PgHdr *p)
107d3 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
107d4 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
107d5 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 if( pCache->bP
107d6 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 urgeable ){.
107d7 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 if( p->pgno==1 )
107d8 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e {. pCache->
107d9 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 pPage1 = 0;.
107da 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f }. sqlite3Glo
107db 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
107dc 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e .xUnpin(pCache->
107dd 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 pCache, p, 0);.
107de 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
107df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 ********** Gener
107e2 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a al Interfaces **
107e3 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 ****.**.** Initi
107e4 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f alize and shutdo
107e5 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 wn the page cach
107e6 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 e subsystem. Nei
107e7 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a ther of these .*
107e8 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 * functions are
107e9 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 threadsafe..*/.S
107ea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
107eb 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 t sqlite3PcacheI
107ec 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b nitialize(void){
107ed 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
107ee 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
107ef 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 e.xInit==0 ){.
107f0 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 sqlite3PCacheS
107f1 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d etDefault();. }
107f2 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
107f3 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
107f4 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 ache.xInit(sqlit
107f5 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
107f6 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 cache.pArg);.}.S
107f7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107f8 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107f9 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a Shutdown(void){.
107fa 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
107fb 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
107fc 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 .xShutdown ){.
107fd 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
107fe 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 onfig.pcache.xSh
107ff 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c utdown(sqlite3Gl
10800 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
10801 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a e.pArg);. }.}..
10802 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
10803 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
10804 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 f a PCache objec
10805 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
10806 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
10807 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 PcacheSize(void)
10808 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 { return sizeof(
10809 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a PCache); }../*.*
1080a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 * Create a new P
1080b 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 Cache object. St
1080c 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 orage space to h
1080d 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a old the object.*
1080e 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
1080f 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
10810 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 is passed in as
10811 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 the p pointer.
10812 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 .** The caller d
10813 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 iscovers how muc
10814 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f h space needs to
10815 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 be allocated by
10816 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c .** calling sql
10817 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 ite3PcacheSize()
10818 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10819 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1081a 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e PcacheOpen(. in
1081b 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 t szPage,
1081c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1081d 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 ze of every page
1081e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 */. int szExtr
1081f 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a,
10820 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 /* Extra spac
10821 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
10822 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 h each page */.
10823 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c int bPurgeable,
10824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10825 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 True if pages a
10826 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 re on backing st
10827 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ore */. int (*x
10828 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 Stress)(void*,Pg
10829 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f Hdr*),/* Call to
1082a 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 try to make pag
1082b 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f es clean */. vo
1082c 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 id *pStress,
1082d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
1082e 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
1082f 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
10830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10831 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 /* Prealloca
10832 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 ted space for th
10833 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 e PCache */.){.
10834 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 memset(p, 0, si
10835 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 zeof(PCache));.
10836 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 p->szPage = szP
10837 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 age;. p->szExtr
10838 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 a = szExtra;. p
10839 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 ->bPurgeable = b
1083a 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e Purgeable;. p->
1083b 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 xStress = xStres
1083c 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 s;. p->pStress
1083d 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e = pStress;. p->
1083e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f nMax = 100;.}../
1083f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
10840 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 page size for PC
10841 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 ache object. The
10842 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 caller must ens
10843 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a ure that there.*
10844 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e * are no outstan
10845 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 ding page refere
10846 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 nces when this f
10847 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10848 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
10849 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1084a 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
1084b 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 ze(PCache *pCach
1084c 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a e, int szPage){.
1084d 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
1084e 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 ->nRef==0 && pCa
1084f 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 che->pDirty==0 )
10850 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e ;. if( pCache->
10851 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 pCache ){. sq
10852 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
10853 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f g.pcache.xDestro
10854 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 y(pCache->pCache
10855 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 );. pCache->p
10856 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 Cache = 0;. }.
10857 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 pCache->szPage
10858 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a = szPage;.}../*.
10859 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e ** Try to obtain
1085a 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
1085b 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
1085c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1085d 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
1085e 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 (. PCache *pCac
1085f 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 he, /* Obt
10860 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f ain the page fro
10861 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a m this cache */.
10862 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 Pgno pgno,
10863 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
10864 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e number to obtain
10865 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 */. int create
10866 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 Flag, /* I
10867 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 f true, create p
10868 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e age if it does n
10869 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 ot exist already
1086a 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 */. PgHdr **pp
1086b 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 Page /* W
1086c 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 rite the page he
1086d 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 re */.){. PgHdr
1086e 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 *pPage = 0;. i
1086f 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 nt eCreate;.. a
10870 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 ssert( pCache!=0
10871 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 );. assert( cr
10872 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 eateFlag==1 || c
10873 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a reateFlag==0 );.
10874 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 assert( pgno>0
10875 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
10876 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 pluggable cache
10877 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 (sqlite3_pcache
10878 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 *) has not been
10879 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 allocated,. **
1087a 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e allocate it now.
1087b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 . */. if( !pCa
1087c 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 che->pCache && c
1087d 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 reateFlag ){.
1087e 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
1087f 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 *p;. int nByt
10880 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 e;. nByte = p
10881 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 Cache->szPage +
10882 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 pCache->szExtra
10883 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b + sizeof(PgHdr);
10884 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
10885 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10886 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 che.xCreate(nByt
10887 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 e, pCache->bPurg
10888 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 eable);. if(
10889 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 !p ){. retu
1088a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1088b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1088c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
1088d 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 cache.xCachesize
1088e 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 (p, pCache->nMax
1088f 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 );. pCache->p
10890 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a Cache = p;. }..
10891 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 eCreate = crea
10892 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 teFlag * (1 + (!
10893 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10894 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 le || !pCache->p
10895 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 Dirty));. if( p
10896 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
10897 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c . pPage = sql
10898 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10899 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 .pcache.xFetch(p
1089a 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
1089b 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 gno, eCreate);.
1089c 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
1089d 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 && eCreate==1 )
1089e 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 {. PgHdr *pPg
1089f 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 ;.. /* Find a
108a0 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 dirty page to w
108a1 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 rite-out and rec
108a2 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 ycle. First try
108a3 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a to find a . *
108a4 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 * page that does
108a5 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a not require a j
108a6 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 ournal-sync (one
108a7 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 with PGHDR_NEED
108a8 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 _SYNC. ** cle
108a9 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 ared), but if th
108aa 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 at is not possib
108ab 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e le settle for an
108ac 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 y other . **
108ad 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 unreferenced dir
108ae 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a ty page.. */.
108af 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 expensive_as
108b0 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 sert( pcacheChec
108b1 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 kSynced(pCache)
108b2 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 );. for(pPg=p
108b3 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 Cache->pSynced;
108b4 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 . pPg &&
108b5 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 (pPg->nRef || (p
108b6 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
108b7 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 NEED_SYNC)); .
108b8 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 pPg=pPg->p
108b9 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b DirtyPrev. );
108ba 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b . if( !pPg ){
108bb 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 . for(pPg=p
108bc 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
108bd 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e l; pPg && pPg->n
108be 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 Ref; pPg=pPg->pD
108bf 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d irtyPrev);. }
108c0 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a . if( pPg ){.
108c1 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 int rc;.
108c2 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d rc = pCache-
108c3 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d >xStress(pCache-
108c4 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a >pStress, pPg);.
108c5 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
108c6 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 LITE_OK && rc!=S
108c7 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
108c8 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
108c9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
108ca 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 pPage = sqli
108cb 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
108cc 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 pcache.xFetch(pC
108cd 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 ache->pCache, pg
108ce 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 no, 2);. }.. i
108cf 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
108d0 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 if( !pPage->pDat
108d1 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 a ){. memse
108d2 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 t(pPage, 0, size
108d3 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 of(PgHdr) + pCac
108d4 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 he->szExtra);.
108d5 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 pPage->pExtr
108d6 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 a = (void*)&pPag
108d7 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 e[1];. pPag
108d8 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 e->pData = (void
108d9 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 *)&((char *)pPa
108da 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 ge)[sizeof(PgHdr
108db 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 ) + pCache->szEx
108dc 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 tra];. pPag
108dd 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 e->pCache = pCac
108de 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d he;. pPage-
108df 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 >pgno = pgno;.
108e0 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
108e1 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 pPage->pCache==p
108e2 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 Cache );. ass
108e3 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f ert( pPage->pgno
108e4 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 ==pgno );. as
108e5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 sert( pPage->pEx
108e6 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 tra==(void *)&pP
108e7 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 age[1] );.. i
108e8 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 f( 0==pPage->nRe
108e9 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 f ){. pCach
108ea 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d e->nRef++;. }
108eb 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 . pPage->nRef
108ec 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f ++;. if( pgno
108ed 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ==1 ){. pCa
108ee 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 che->pPage1 = pP
108ef 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 age;. }. }.
108f0 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 *ppPage = pPage
108f1 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 ;. return (pPag
108f2 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 e==0 && eCreate)
108f3 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ? SQLITE_NOMEM
108f4 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a : SQLITE_OK;.}..
108f5 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 /*.** Decrement
108f6 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
108f7 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 unt on a page. I
108f8 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c f the page is cl
108f9 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 ean and the.** r
108fa 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 eference count d
108fb 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 rops to 0, then
108fc 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c it is made elibl
108fd 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e e for recycling.
108fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
108ff 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10900 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 cacheRelease(PgH
10901 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 dr *p){. assert
10902 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
10903 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 p->nRef--;. if
10904 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
10905 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 PCache *pCac
10906 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
10907 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 pCache->nRef
10908 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e --;. if( (p->
10909 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
1090a 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 Y)==0 ){. p
1090b 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 cacheUnpin(p);.
1090c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1090d 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 /* Move the page
1090e 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
1090f 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 the dirty list.
10910 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 */. pcacheR
10911 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 emoveFromDirtyLi
10912 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 st(p);. pca
10913 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 cheAddToDirtyLis
10914 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a t(p);. }. }.
10915 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 }../*.** Increas
10916 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 e the reference
10917 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c count of a suppl
10918 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a ied page by 1..*
10919 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1091a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
1091b 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 cheRef(PgHdr *p)
1091c 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 {. assert(p->nR
1091d 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 ef>0);. p->nRef
1091e 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f ++;.}../*.** Dro
1091f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 p a page from th
10920 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d e cache. There m
10921 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f ust be exactly o
10922 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 ne reference to
10923 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 the.** page. Thi
10924 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 s function delet
10925 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 es that referenc
10926 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 e, so after it r
10927 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 eturns the.** pa
10928 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ge pointed to by
10929 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a p is invalid..*
1092a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1092b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
1092c 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 cheDrop(PgHdr *p
1092d 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
1092e 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 che;. assert( p
1092f 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 ->nRef==1 );. i
10930 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 f( p->flags&PGHD
10931 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 R_DIRTY ){. p
10932 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 cacheRemoveFromD
10933 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d irtyList(p);. }
10934 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 . pCache = p->p
10935 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d Cache;. pCache-
10936 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
10937 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ->pgno==1 ){.
10938 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 pCache->pPage1
10939 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 0;. }. sqlit
1093a 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
1093b 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 cache.xUnpin(pCa
1093c 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 che->pCache, p,
1093d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 1);.}../*.** Mak
1093e 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 e sure the page
1093f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 is marked as dir
10940 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 ty. If it isn't
10941 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a dirty already,.*
10942 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f * make it so..*/
10943 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10944 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10945 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 heMakeDirty(PgHd
10946 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 r *p){. p->flag
10947 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 s &= ~PGHDR_DONT
10948 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 _WRITE;. assert
10949 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
1094a 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 if( 0==(p->flag
1094b 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 s & PGHDR_DIRTY)
1094c 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 ){. p->flags
1094d 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b |= PGHDR_DIRTY;
1094e 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f . pcacheAddTo
1094f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 DirtyList( p);.
10950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
10951 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 sure the page i
10952 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 s marked as clea
10953 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 n. If it isn't c
10954 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a lean already,.**
10955 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a make it so..*/.
10956 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10957 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10958 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 eMakeClean(PgHdr
10959 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e *p){. if( (p->
1095a 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 flags & PGHDR_DI
1095b 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 RTY) ){. pcac
1095c 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
1095d 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d yList(p);. p-
1095e 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 >flags &= ~(PGHD
1095f 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 R_DIRTY|PGHDR_NE
10960 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 ED_SYNC);. if
10961 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
10962 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 pcacheUnpi
10963 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n(p);. }. }.
10964 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 }../*.** Make ev
10965 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
10966 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a cache clean..*/.
10967 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10968 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10969 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 eCleanAll(PCache
1096a 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
1096b 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 dr *p;. while(
1096c 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 (p = pCache->pDi
1096d 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 rty)!=0 ){. s
1096e 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1096f 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a Clean(p);. }.}.
10970 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
10971 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
10972 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 flag from all d
10973 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 irty pages..*/.S
10974 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
10975 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10976 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 ClearSyncFlags(P
10977 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
10978 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f PgHdr *p;. fo
10979 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 r(p=pCache->pDir
1097a 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 ty; p; p=p->pDir
1097b 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e tyNext){. p->
1097c 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f flags &= ~PGHDR_
1097d 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 NEED_SYNC;. }.
1097e 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 pCache->pSynced
1097f 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 = pCache->pDirt
10980 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 yTail;.}../*.**
10981 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 Change the page
10982 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 number of page p
10983 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f to newPgno. .*/
10984 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10985 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10986 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c heMove(PgHdr *p,
10987 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a Pgno newPgno){.
10988 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 PCache *pCache
10989 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 = p->pCache;.
1098a 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
1098b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
1098c 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 ewPgno>0 );. sq
1098d 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1098e 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 g.pcache.xRekey(
1098f 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
10990 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 p, p->pgno, newP
10991 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 gno);. p->pgno
10992 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 = newPgno;. if(
10993 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 (p->flags&PGHDR
10994 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 _DIRTY) && (p->f
10995 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
10996 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 SYNC) ){. pca
10997 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 cheRemoveFromDir
10998 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 tyList(p);. p
10999 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c cacheAddToDirtyL
1099a 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ist(p);. }.}../
1099b 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 *.** Drop every
1099c 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 cache entry whos
1099d 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 e page number is
1099e 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 greater than "p
1099f 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c gno". The.** cal
109a0 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 ler must ensure
109a1 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e that there are n
109a2 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 o outstanding re
109a3 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 ferences to any
109a4 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 pages.** other t
109a5 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 han page 1 with
109a6 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 a page number gr
109a7 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e eater than pgno.
109a8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
109a9 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 is a reference t
109aa 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 o page 1 and the
109ab 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 pgno parameter
109ac 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a passed to this.*
109ad 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c * function is 0,
109ae 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 then the data a
109af 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 rea associated w
109b0 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 ith page 1 is ze
109b1 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 roed, but.** the
109b2 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
109b3 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a not dropped..*/.
109b4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
109b5 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
109b6 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 eTruncate(PCache
109b7 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 *pCache, Pgno p
109b8 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 gno){. if( pCac
109b9 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 he->pCache ){.
109ba 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 PgHdr *p;.
109bb 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 PgHdr *pNext;.
109bc 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e for(p=pCache->
109bd 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 pDirty; p; p=pNe
109be 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 xt){. pNext
109bf 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 = p->pDirtyNext
109c0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 ;. if( p->p
109c1 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 gno>pgno ){.
109c2 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 assert( p->f
109c3 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
109c4 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
109c5 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
109c6 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 an(p);. }.
109c7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e }. if( pgn
109c8 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e o==0 && pCache->
109c9 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 pPage1 ){.
109ca 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 memset(pCache->p
109cb 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c Page1->pData, 0,
109cc 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 pCache->szPage)
109cd 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 ;. pgno = 1
109ce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
109cf 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
109d0 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 pcache.xTruncate
109d1 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
109d2 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a pgno+1);. }.}.
109d3 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 ./*.** Close a c
109d4 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
109d5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
109d6 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
109d7 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
109d8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
109d9 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
109da 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
109db 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 .pcache.xDestroy
109dc 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
109dd 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 ;. }.}../* .**
109de 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 Discard the cont
109df 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 ents of the cach
109e0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
109e1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
109e2 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 3PcacheClear(PCa
109e3 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
109e4 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 sqlite3PcacheTru
109e5 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 ncate(pCache, 0)
109e6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 ;.}../*.** Merge
109e7 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 two lists of pa
109e8 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ges connected by
109e9 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 pDirty and in p
109ea 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f gno order..** Do
109eb 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 not both fixing
109ec 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 the pDirtyPrev
109ed 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 pointers..*/.sta
109ee 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
109ef 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 eMergeDirtyList(
109f0 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 PgHdr *pA, PgHdr
109f1 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 *pB){. PgHdr r
109f2 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 esult, *pTail;.
109f3 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 pTail = &result
109f4 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 ;. while( pA &&
109f5 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 pB ){. if( p
109f6 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f A->pgno<pB->pgno
109f7 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d ){. pTail-
109f8 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 >pDirty = pA;.
109f9 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a pTail = pA;.
109fa 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
109fb 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 Dirty;. }else
109fc 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
109fd 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 Dirty = pB;.
109fe 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 pTail = pB;.
109ff 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 pB = pB->pDi
10a00 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rty;. }. }.
10a01 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 if( pA ){. p
10a02 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 Tail->pDirty = p
10a03 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 A;. }else if( p
10a04 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e B ){. pTail->
10a05 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d pDirty = pB;. }
10a06 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d else{. pTail-
10a07 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d >pDirty = 0;. }
10a08 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 . return result
10a09 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a .pDirty;.}../*.*
10a0a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 * Sort the list
10a0b 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 of pages in acce
10a0c 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 nding order by p
10a0d 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a gno. Pages are.
10a0e 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
10a0f 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e pDirty pointers.
10a10 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 The pDirtyPrev
10a11 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a pointers are.**
10a12 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 corrupted by th
10a13 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 is sort..**.** S
10a14 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f ince there canno
10a15 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 t be more than 2
10a16 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 ^31 distinct pag
10a17 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 es in a database
10a18 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f ,.** there canno
10a19 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 t be more than 3
10a1a 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 1 buckets requir
10a1b 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 ed by the merge
10a1c 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 sorter..** One e
10a1d 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 xtra bucket is a
10a1e 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 dded to catch ov
10a1f 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 erflow in case s
10a20 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 omething.** ever
10a21 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 changes to make
10a22 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
10a23 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 ntence incorrect
10a24 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 ..*/.#define N_S
10a25 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 ORT_BUCKET 32.s
10a26 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
10a27 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 cheSortDirtyList
10a28 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 (PgHdr *pIn){.
10a29 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f PgHdr *a[N_SORT_
10a2a 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 BUCKET], *p;. i
10a2b 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 nt i;. memset(a
10a2c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b , 0, sizeof(a));
10a2d 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b . while( pIn ){
10a2e 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 . p = pIn;.
10a2f 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 pIn = p->pDirt
10a30 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 y;. p->pDirty
10a31 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d = 0;. for(i=
10a32 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 0; ALWAYS(i<N_SO
10a33 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b RT_BUCKET-1); i+
10a34 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b +){. if( a[
10a35 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 i]==0 ){.
10a36 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 a[i] = p;.
10a37 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
10a38 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
10a39 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
10a3a 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 rtyList(a[i], p)
10a3b 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d ;. a[i] =
10a3c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
10a3d 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 }. if( NEVER(
10a3e 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 i==N_SORT_BUCKET
10a3f 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 -1) ){. /*
10a40 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 To get here, the
10a41 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e re need to be 2^
10a42 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 (N_SORT_BUCKET)
10a43 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 elements in.
10a44 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c ** the input l
10a45 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 ist. But that i
10a46 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 s impossible..
10a47 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 */. a[i
10a48 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 ] = pcacheMergeD
10a49 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 irtyList(a[i], p
10a4a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 );. }. }. p
10a4b 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 = a[0];. for(i
10a4c 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 =1; i<N_SORT_BUC
10a4d 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 KET; i++){. p
10a4e 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
10a4f 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 rtyList(p, a[i])
10a50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
10a51 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
10a52 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 n a list of all
10a53 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 dirty pages in t
10a54 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 he cache, sorted
10a55 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e by page number.
10a56 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10a57 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 TE PgHdr *sqlite
10a58 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 3PcacheDirtyList
10a59 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
10a5a 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
10a5b 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
10a5c 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 irty; p; p=p->pD
10a5d 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 irtyNext){. p
10a5e 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 ->pDirty = p->pD
10a5f 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 irtyNext;. }.
10a60 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 return pcacheSor
10a61 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 tDirtyList(pCach
10a62 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f e->pDirty);.}../
10a63 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
10a64 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
10a65 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 referenced page
10a66 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 s held by the ca
10a67 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
10a68 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
10a69 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
10a6a 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
10a6b 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 {. return pCach
10a6c 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a e->nRef;.}../*.*
10a6d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
10a6e 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
10a6f 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 s to the page su
10a70 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
10a71 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
10a72 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10a73 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
10a74 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 fcount(PgHdr *p)
10a75 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 {. return p->nR
10a76 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 ef;.}../* .** Re
10a77 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e turn the total n
10a78 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10a79 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a n the cache..*/.
10a7a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
10a7b 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
10a7c 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 Pagecount(PCache
10a7d 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 *pCache){. int
10a7e 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 nPage = 0;. if
10a7f 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
10a80 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 ){. nPage =
10a81 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
10a82 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 fig.pcache.xPage
10a83 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 count(pCache->pC
10a84 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ache);. }. ret
10a85 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 urn nPage;.}..#i
10a86 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
10a87 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 ./*.** Get the s
10a88 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 uggested cache-s
10a89 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ize value..*/.SQ
10a8a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10a8b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 sqlite3PcacheGe
10a8c 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 tCachesize(PCach
10a8d 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 e *pCache){. re
10a8e 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 turn pCache->nMa
10a8f 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a x;.}.#endif../*.
10a90 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 ** Set the sugge
10a91 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 sted cache-size
10a92 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 value..*/.SQLITE
10a93 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10a94 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 lite3PcacheSetCa
10a95 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a chesize(PCache *
10a96 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 pCache, int mxPa
10a97 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e ge){. pCache->n
10a98 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 Max = mxPage;.
10a99 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 if( pCache->pCac
10a9a 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 he ){. sqlite
10a9b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10a9c 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 ache.xCachesize(
10a9d 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
10a9e 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a mxPage);. }.}..
10a9f 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
10aa0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 TE_CHECK_PAGES)
10aa1 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
10aa2 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 E_DEBUG)./*.** F
10aa3 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 or all dirty pag
10aa4 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 es currently in
10aa5 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b the cache, invok
10aa6 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a e the specified.
10aa7 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 ** callback. Thi
10aa8 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 s is only used i
10aa9 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 f the SQLITE_CHE
10aaa 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 CK_PAGES macro i
10aab 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f s.** defined..*/
10aac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10aad 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10aae 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 heIterateDirty(P
10aaf 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 Cache *pCache, v
10ab0 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 oid (*xIter)(PgH
10ab1 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 dr *)){. PgHdr
10ab2 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 *pDirty;. for(p
10ab3 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 Dirty=pCache->pD
10ab4 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 irty; pDirty; pD
10ab5 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 irty=pDirty->pDi
10ab6 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 rtyNext){. xI
10ab7 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d ter(pDirty);. }
10ab8 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a .}.#endif../****
10ab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
10aba 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a f pcache.c *****
10abb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10abc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10abd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
10abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
10abf 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 file pcache1.c
10ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
10ac3 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 2008 November 0
10ac4 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
10ac5 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
10ac6 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
10ac7 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
10ac8 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
10ac9 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
10aca 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
10acb 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
10acc 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
10acd 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
10ace 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
10acf 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
10ad0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
10ad1 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
10ad2 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
10ad3 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
10ad4 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
10ad5 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
10ad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ad7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ada 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
10adb 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
10adc 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 default page cac
10add 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
10ade 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 n (the.** sqlite
10adf 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 3_pcache interfa
10ae0 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e ce). It also con
10ae1 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 tains part of th
10ae2 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
10ae3 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 .** of the SQLIT
10ae4 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10ae5 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 HE and sqlite3_r
10ae6 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
10ae7 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 features..** If
10ae8 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
10ae9 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
10aea 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 ation is overrid
10aeb 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 en, then neither
10aec 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f of.** these two
10aed 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 features are av
10aee 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 0a 0a 74 79 ailable..*/...ty
10aef 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61 pedef struct PCa
10af0 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 che1 PCache1;.ty
10af1 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
10af2 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 dr1 PgHdr1;.type
10af3 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65 def struct PgFre
10af4 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 eslot PgFreeslot
10af5 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 ;../* Pointers t
10af6 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 o structures of
10af7 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61 this type are ca
10af8 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 st and returned
10af9 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 as .** opaque sq
10afa 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 lite3_pcache* ha
10afb 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 ndles.*/.struct
10afc 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 PCache1 {. /* C
10afd 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 ache configurati
10afe 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 on parameters. P
10aff 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 age size (szPage
10b00 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 ) and the purgea
10b01 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 ble. ** flag (b
10b02 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 Purgeable) are s
10b03 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 et when the cach
10b04 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d e is created. nM
10b05 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 ax may be . **
10b06 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 modified at any
10b07 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 time by a call t
10b08 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63 o the pcache1Cac
10b09 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e heSize() method.
10b0a 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c . ** The global
10b0b 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
10b0c 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 eld when accessi
10b0d 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 ng nMax.. */.
10b0e 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 int szPage;
10b0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b10 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 /* Size of a
10b11 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 llocated pages i
10b12 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 n bytes */. int
10b13 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 bPurgeable;
10b14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b15 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 /* True if cach
10b16 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a e is purgeable *
10b17 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10b18 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 nMin;
10b19 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
10b1a 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 um number of pag
10b1b 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 es reserved */.
10b1c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d unsigned int nM
10b1d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ax;
10b1e 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
10b1f 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 ed "cache_size"
10b20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 value */.. /* H
10b21 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c ash table of all
10b22 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c pages. The foll
10b23 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 owing variables
10b24 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 may only be acce
10b25 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 ssed. ** when t
10b26 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 he accessor is h
10b27 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 olding the globa
10b28 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 l mutex (see pca
10b29 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10b2a 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 . ** and pcach
10b2b 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e e1LeaveMutex()).
10b2c 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 . */. unsigned
10b2d 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 int nRecyclable
10b2e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
10b2f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10b30 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a n the LRU list *
10b31 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10b32 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
10b33 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
10b34 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10b35 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 in apHash */.
10b36 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 unsigned int nHa
10b37 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sh;
10b38 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10b39 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 slots in apHash
10b3a 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a [] */. PgHdr1 *
10b3b 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 *apHash;
10b3c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
10b3d 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 ash table for fa
10b3e 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 st lookup by key
10b3f 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 */.. unsigned
10b40 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 int iMaxKey;
10b41 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
10b42 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 rgest key seen s
10b43 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 ince xTruncate()
10b44 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
10b45 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 ch cache entry i
10b46 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 s represented by
10b47 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
10b48 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a the following .*
10b49 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 * structure. A b
10b4a 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e uffer of PgHdr1.
10b4b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 pCache->szPage b
10b4c 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 ytes is allocate
10b4d 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 d .** directly b
10b4e 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 efore this struc
10b4f 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 ture in memory (
10b50 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 see the PGHDR1_T
10b51 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 O_PAGE() .** mac
10b52 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 ro below)..*/.st
10b53 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 ruct PgHdr1 {.
10b54 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 unsigned int iKe
10b55 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
10b56 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 * Key value (pag
10b57 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 e number) */. P
10b58 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 gHdr1 *pNext;
10b59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b5a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 Next in hash ta
10b5b 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 ble chain */. P
10b5c 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 Cache1 *pCache;
10b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b5e 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72 Cache that curr
10b5f 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 ently owns this
10b60 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 page */. PgHdr1
10b61 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 *pLruNext;
10b62 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
10b63 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 in LRU list of
10b64 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
10b65 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10b66 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 Prev;
10b67 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 /* Previous i
10b68 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e n LRU list of un
10b69 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
10b6a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 };../*.** Free s
10b6b 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f lots in the allo
10b6c 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 cator used to di
10b6d 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66 vide up the buff
10b6e 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e er provided usin
10b6f 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f g.** the SQLITE_
10b70 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10b71 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 mechanism..*/.s
10b72 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 truct PgFreeslot
10b73 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 {. PgFreeslot
10b74 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
10b75 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b free slot */.};
10b76 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 ../*.** Global d
10b77 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73 ata used by this
10b78 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
10b79 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
10b7a 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c uct PCacheGlobal
10b7b 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 {. sqlite3_mut
10b7c 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
10b7d 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 /* stat
10b7e 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 ic mutex MUTEX_S
10b7f 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 TATIC_LRU */..
10b80 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 int nMaxPage;
10b81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b82 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d /* Sum of nM
10b83 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 axPage for purge
10b84 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 able caches */.
10b85 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 int nMinPage;
10b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b87 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e /* Sum of n
10b88 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 MinPage for purg
10b89 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a eable caches */.
10b8a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 int nCurrentPa
10b8b 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
10b8c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10b8d 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 of purgeable pag
10b8e 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a es allocated */.
10b8f 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 PgHdr1 *pLruHe
10b90 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 ad, *pLruTail;
10b91 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 /* LRU lis
10b92 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 t of unpinned pa
10b93 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 ges */.. /* Var
10b94 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 iables related t
10b95 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f o SQLITE_CONFIG_
10b96 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e PAGECACHE settin
10b97 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 gs. */. int szS
10b98 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 lot;
10b99 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b9a 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 Size of each fre
10b9b 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 e slot */. void
10b9c 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b *pStart, *pEnd;
10b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b9e 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 /* Bounds of pag
10b9f 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 ecache malloc ra
10ba0 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 nge */. PgFrees
10ba1 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 lot *pFree;
10ba2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10ba3 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 Free page blocks
10ba4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 */. int isInit
10ba5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10ba6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
10ba7 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 e if initialized
10ba8 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b */.} pcache1_g;
10ba9 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 ../*.** All code
10baa 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 in this file sh
10bab 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 ould access the
10bac 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 global structure
10bad 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a above via the.*
10bae 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 * alias "pcache1
10baf 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 ". This ensures
10bb0 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 that the WSD emu
10bb1 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 lation is used w
10bb2 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 hen.** compiling
10bb3 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 for systems tha
10bb4 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 t do not support
10bb5 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 real WSD..*/.#d
10bb6 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 efine pcache1 (G
10bb7 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 LOBAL(struct PCa
10bb8 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 cheGlobal, pcach
10bb9 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 e1_g))../*.** Wh
10bba 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75 en a PgHdr1 stru
10bbb 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 cture is allocat
10bbc 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 ed, the associat
10bbd 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 ed PCache1.szPag
10bbe 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 e.** bytes of da
10bbf 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 ta are located d
10bc0 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 irectly before i
10bc1 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 t in memory (i.e
10bc2 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 . the total.** s
10bc3 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
10bc4 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 ation is sizeof(
10bc5 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e PgHdr1)+PCache1.
10bc6 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 szPage byte). Th
10bc7 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 e.** PGHDR1_TO_P
10bc8 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 AGE() macro take
10bc9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
10bca 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72 PgHdr1 structur
10bcb 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d e as.** an argum
10bcc 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ent and returns
10bcd 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
10bce 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 associated bloc
10bcf 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 k of szPage.** b
10bd0 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 ytes. The PAGE_T
10bd1 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f O_PGHDR1() macro
10bd2 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 does the opposi
10bd3 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 te: its argument
10bd4 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 is.** a pointer
10bd5 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 to a block of s
10bd6 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 zPage bytes of d
10bd7 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 ata and the retu
10bd8 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 rn value is.** a
10bd9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
10bda 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 associated PgHdr
10bdb 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 1 structure..**.
10bdc 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48 ** assert( PGH
10bdd 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 DR1_TO_PAGE(PAGE
10bde 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 _TO_PGHDR1(pCach
10bdf 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a e, X))==X );.*/.
10be0 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 #define PGHDR1_T
10be1 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f O_PAGE(p) (vo
10be2 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 id*)(((char*)p)
10be3 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 - p->pCache->szP
10be4 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 age).#define PAG
10be5 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 E_TO_PGHDR1(c, p
10be6 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 ) (PgHdr1*)(((ch
10be7 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 ar*)p) + c->szPa
10be8 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f ge)../*.** Macro
10be9 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c s to enter and l
10bea 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 eave the global
10beb 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 LRU mutex..*/.#d
10bec 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 efine pcache1Ent
10bed 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 erMutex() sqlite
10bee 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 3_mutex_enter(pc
10bef 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 ache1.mutex).#de
10bf0 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 fine pcache1Leav
10bf1 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 eMutex() sqlite3
10bf2 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 _mutex_leave(pca
10bf3 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a che1.mutex)../**
10bf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10bf9 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c ******* Page All
10bfa 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 ocation/SQLITE_C
10bfb 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c ONFIG_PCACHE Rel
10bfc 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a ated Functions *
10bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10bfe 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10bff 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 ion is called du
10c00 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
10c01 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 ion if a static
10c02 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 buffer is .** su
10c03 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f pplied to use fo
10c04 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 r the page-cache
10c05 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 by passing the
10c06 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
10c07 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 GECACHE.** verb
10c08 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 to sqlite3_confi
10c09 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 g(). Parameter p
10c0a 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e Buf points to an
10c0b 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
10c0c 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 e.** enough to c
10c0d 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 ontain 'n' buffe
10c0e 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 rs of 'sz' bytes
10c0f 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 each..*/.SQLITE
10c10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10c11 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 lite3PCacheBuffe
10c12 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 rSetup(void *pBu
10c13 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e f, int sz, int n
10c14 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 ){. if( pcache1
10c15 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 .isInit ){. P
10c16 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 gFreeslot *p;.
10c17 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e sz = ROUNDDOWN
10c18 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 8(sz);. pcach
10c19 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a e1.szSlot = sz;.
10c1a 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 pcache1.pSta
10c1b 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 rt = pBuf;. p
10c1c 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 cache1.pFree = 0
10c1d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d ;. while( n--
10c1e 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 ){. p = (P
10c1f 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b gFreeslot*)pBuf;
10c20 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 . p->pNext
10c21 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10c22 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10c23 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 Free = p;.
10c24 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 pBuf = (void*)&(
10c25 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d (char*)pBuf)[sz]
10c26 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
10c27 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b he1.pEnd = pBuf;
10c28 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 . }.}../*.** Ma
10c29 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 lloc function us
10c2a 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 ed within this f
10c2b 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ile to allocate
10c2c 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10c2d 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 uffer.** configu
10c2e 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 red using sqlite
10c2f 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10c30 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10c31 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10c32 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 .** such buffer
10c33 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 exists or there
10c34 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 is no space left
10c35 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e in it, this fun
10c36 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 ction falls .**
10c37 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d back to sqlite3M
10c38 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
10c39 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 ic void *pcache1
10c3a 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 Alloc(int nByte)
10c3b 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 {. void *p;. a
10c3c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
10c3d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
10c3e 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 1.mutex) );. if
10c3f 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 ( nByte<=pcache1
10c40 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 .szSlot && pcach
10c41 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 e1.pFree ){.
10c42 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e assert( pcache1.
10c43 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 isInit );. p
10c44 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 = (PgHdr1 *)pcac
10c45 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
10c46 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
10c47 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e cache1.pFree->pN
10c48 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ext;. sqlite3
10c49 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
10c4a 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
10c4b 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a E_SIZE, nByte);.
10c4c 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10c4d 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10c4e 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
10c4f 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a D, 1);. }else{.
10c50 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
10c51 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 a new buffer us
10c52 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ing sqlite3Mallo
10c53 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 c. Before doing
10c54 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 so, exit the.
10c55 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 ** global pcach
10c56 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f e mutex and unlo
10c57 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 ck the pager-cac
10c58 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 he object pCache
10c59 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a . This is . *
10c5a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 * so that if the
10c5b 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f attempt to allo
10c5c 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 cate a new buffe
10c5d 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65 r causes the the
10c5e 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 . ** configu
10c5f 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 red soft-heap-li
10c60 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 mit to be breach
10c61 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 ed, it will be p
10c62 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a ossible to. *
10c63 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 * reclaim memory
10c64 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 from this pager
10c65 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 -cache.. */.
10c66 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
10c67 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 utex();. p =
10c68 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 sqlite3Malloc(nB
10c69 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 yte);. pcache
10c6a 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10c6b 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
10c6c 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 int sz = sqlit
10c6d 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
10c6e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
10c6f 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
10c70 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10c71 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 OVERFLOW, sz);.
10c72 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
10c73 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 n p;.}../*.** Fr
10c74 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 ee an allocated
10c75 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 buffer obtained
10c76 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f from pcache1Allo
10c77 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
10c78 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28 oid pcache1Free(
10c79 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
10c7a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10c7b 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
10c7c 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
10c7d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
10c7e 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 if( p>=pcache1.p
10c7f 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 Start && p<pcach
10c80 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 e1.pEnd ){. P
10c81 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 gFreeslot *pSlot
10c82 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c83 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c84 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
10c85 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 SED, -1);. pS
10c86 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f lot = (PgFreeslo
10c87 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d t*)p;. pSlot-
10c88 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 >pNext = pcache1
10c89 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 .pFree;. pcac
10c8a 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f he1.pFree = pSlo
10c8b 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
10c8c 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
10c8d 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
10c8e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c8f 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c90 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
10c91 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 VERFLOW, -iSize)
10c92 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
10c93 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ee(p);. }.}../*
10c94 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
10c95 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
10c96 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 nitially associa
10c97 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70 ted with cache p
10c98 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cache..*/.static
10c99 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 PgHdr1 *pcache1
10c9a 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 AllocPage(PCache
10c9b 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 1 *pCache){. in
10c9c 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 t nByte = sizeof
10c9d 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 (PgHdr1) + pCach
10c9e 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 e->szPage;. voi
10c9f 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 d *pPg = pcache1
10ca0 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 Alloc(nByte);.
10ca1 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 PgHdr1 *p;. if(
10ca2 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 pPg ){. p =
10ca3 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10ca4 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 Cache, pPg);.
10ca5 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10ca6 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 rgeable ){.
10ca7 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10ca8 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 tPage++;. }.
10ca9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
10caa 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
10cab 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10cac 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 a page object a
10cad 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 llocated by pcac
10cae 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a he1AllocPage()..
10caf 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
10cb0 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 r is allowed to
10cb1 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 be NULL, which i
10cb2 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 s prudent. But
10cb3 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 it turns out.**
10cb4 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 that the current
10cb5 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10cb6 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 happens to never
10cb7 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
10cb8 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c ne.** with a NUL
10cb9 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 L pointer, so we
10cba 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 mark the NULL t
10cbb 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 est with ALWAYS(
10cbc 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10cbd 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 d pcache1FreePag
10cbe 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 e(PgHdr1 *p){.
10cbf 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b if( ALWAYS(p) ){
10cc0 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 . if( p->pCac
10cc1 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
10cc2 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10cc3 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a nCurrentPage--;.
10cc4 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10cc5 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 1Free(PGHDR1_TO_
10cc6 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a PAGE(p));. }.}.
10cc7 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
10cc8 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 nction used by S
10cc9 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 QLite to obtain
10cca 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10ccb 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 uffer configured
10ccc 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 .** using sqlite
10ccd 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10cce 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10ccf 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10cd0 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 such buffer.** e
10cd1 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 xists, this func
10cd2 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 tion falls back
10cd3 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 to sqlite3Malloc
10cd4 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
10cd5 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
10cd6 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
10cd7 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 t sz){. void *p
10cd8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10cd9 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 Mutex();. p = p
10cda 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b cache1Alloc(sz);
10cdb 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10cdc 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
10cdd 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
10cde 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 e an allocated b
10cdf 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 uffer obtained f
10ce0 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d rom sqlite3PageM
10ce1 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
10ce2 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10ce3 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
10ce4 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 void *p){. pcac
10ce5 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10ce6 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70 . pcache1Free(p
10ce7 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
10ce8 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a eMutex();.}../**
10ce9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ceb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ced 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10cee 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 ******* General
10cef 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 Implementation F
10cf0 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
10cf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10cf3 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10cf4 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
10cf5 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 esize the hash t
10cf6 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 able used by the
10cf7 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a cache passed.**
10cf8 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
10cf9 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
10cfa 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
10cfb 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
10cfc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10cfd 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
10cfe 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 tic int pcache1R
10cff 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 esizeHash(PCache
10d00 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 1 *p){. PgHdr1
10d01 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 **apNew;. unsig
10d02 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 ned int nNew;.
10d03 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
10d04 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10d05 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10d06 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10d07 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 . nNew = p->nHa
10d08 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 sh*2;. if( nNew
10d09 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 <256 ){. nNew
10d0a 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 = 256;. }.. p
10d0b 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10d0c 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 ();. if( p->nHa
10d0d 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 sh ){ sqlite3Beg
10d0e 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
10d0f 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 ; }. apNew = (P
10d10 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 gHdr1 **)sqlite3
10d11 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
10d12 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a gHdr1 *)*nNew);.
10d13 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 if( p->nHash )
10d14 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 { sqlite3EndBeni
10d15 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
10d16 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10d17 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 x();. if( apNew
10d18 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 ){. memset(a
10d19 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pNew, 0, sizeof(
10d1a 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b PgHdr1 *)*nNew);
10d1b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
10d1c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a p->nHash; i++){.
10d1d 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 PgHdr1 *pP
10d1e 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 age;. PgHdr
10d1f 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 1 *pNext = p->ap
10d20 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 Hash[i];. w
10d21 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 hile( (pPage = p
10d22 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 Next)!=0 ){.
10d23 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
10d24 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 h = pPage->iKey
10d25 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 % nNew;.
10d26 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e pNext = pPage->
10d27 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 pNext;. p
10d28 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 Page->pNext = ap
10d29 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 New[h];.
10d2a 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 apNew[h] = pPage
10d2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10d2c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10d2d 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 (p->apHash);.
10d2e 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e p->apHash = apN
10d2f 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 ew;. p->nHash
10d30 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 = nNew;. }..
10d31 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 return (p->apHas
10d32 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 h ? SQLITE_OK :
10d33 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
10d34 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10d35 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e ction is used in
10d36 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f ternally to remo
10d37 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 ve the page pPag
10d38 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 e from the .** g
10d39 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 lobal LRU list,
10d3a 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 if is part of it
10d3b 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f . If pPage is no
10d3c 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c t part of the gl
10d3d 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 obal.** LRU list
10d3e 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 , then this func
10d3f 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
10d40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10d41 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10d42 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10d43 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10d44 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10d45 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 d pcache1PinPage
10d46 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10d47 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10d48 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10d49 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10d4a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 if( pPage && (
10d4b 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10d4c 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 || pPage==pcache
10d4d 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 1.pLruTail) ){.
10d4e 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c if( pPage->pL
10d4f 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 ruPrev ){.
10d50 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d pPage->pLruPrev-
10d51 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 >pLruNext = pPag
10d52 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 e->pLruNext;.
10d53 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
10d54 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 ->pLruNext ){.
10d55 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e pPage->pLruN
10d56 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 ext->pLruPrev =
10d57 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
10d58 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
10d59 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d cache1.pLruHead=
10d5a 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 =pPage ){.
10d5b 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10d5c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 = pPage->pLruNe
10d5d 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
10d5e 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 ( pcache1.pLruTa
10d5f 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 il==pPage ){.
10d60 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 pcache1.pLruT
10d61 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ail = pPage->pLr
10d62 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
10d63 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10d64 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
10d65 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 >pLruPrev = 0;.
10d66 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 pPage->pCache
10d67 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b ->nRecyclable--;
10d68 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 . }.}.../*.** R
10d69 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 emove the page s
10d6a 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
10d6b 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 gument from the
10d6c 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 hash table .** (
10d6d 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 PCache1.apHash s
10d6e 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 tructure) that i
10d6f 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 t is currently s
10d70 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 tored in..**.**
10d71 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10d72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10d73 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10d74 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10d75 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10d76 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
10d77 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10d78 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10d79 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 h;. PCache1 *pC
10d7a 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 ache = pPage->pC
10d7b 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a ache;. PgHdr1 *
10d7c 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 *pp;.. h = pPag
10d7d 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 e->iKey % pCache
10d7e 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 ->nHash;. for(p
10d7f 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 p=&pCache->apHas
10d80 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 h[h]; (*pp)!=pPa
10d81 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 ge; pp=&(*pp)->p
10d82 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 Next);. *pp = (
10d83 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 *pp)->pNext;..
10d84 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b pCache->nPage--;
10d85 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
10d86 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
10d87 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 more than pcach
10d88 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 e.nMaxPage pages
10d89 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a allocated, try.
10d8a 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 ** to recycle pa
10d8b 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 ges to reduce th
10d8c 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 e number allocat
10d8d 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 ed to pcache.nMa
10d8e 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xPage..*/.static
10d8f 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 void pcache1Enf
10d90 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 orceMaxPage(void
10d91 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10d92 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10d93 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10d94 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 ;. while( pcach
10d95 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e e1.nCurrentPage>
10d96 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
10d97 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 && pcache1.pLru
10d98 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 Tail ){. PgHd
10d99 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e r1 *p = pcache1.
10d9a 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 pLruTail;. pc
10d9b 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b ache1PinPage(p);
10d9c 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f . pcache1Remo
10d9d 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
10d9e 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
10d9f 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ge(p);. }.}../*
10da0 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 .** Discard all
10da1 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 pages from cache
10da2 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 pCache with a p
10da3 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 age number (key
10da4 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 value) .** great
10da5 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
10da6 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 to iLimit. Any
10da7 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 pinned pages tha
10da8 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 t meet this .**
10da9 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 criteria are unp
10daa 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 inned before the
10dab 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e y are discarded.
10dac 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10dad 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10dae 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10daf 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10db0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10db1 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 d pcache1Truncat
10db2 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 eUnsafe(. PCach
10db3 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 e1 *pCache, . u
10db4 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d nsigned int iLim
10db5 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c it .){. TESTONL
10db6 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 Y( unsigned int
10db7 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 nPage = 0; )
10db8 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 /* Used to ass
10db9 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 ert pCache->nPag
10dba 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a e is correct */.
10dbb 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10dbc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
10dbd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
10dbe 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b cache1.mutex) );
10dbf 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 . for(h=0; h<pC
10dc0 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b ache->nHash; h++
10dc1 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a ){. PgHdr1 **
10dc2 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10dc3 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 Hash[h]; . Pg
10dc4 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 Hdr1 *pPage;.
10dc5 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d while( (pPage =
10dc6 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 *pp)!=0 ){.
10dc7 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 if( pPage->iKe
10dc8 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 y>=iLimit ){.
10dc9 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 pCache->nPa
10dca 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 ge--;. *p
10dcb 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
10dcc 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 ;. pcache
10dcd 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 1PinPage(pPage);
10dce 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 . pcache1
10dcf 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
10dd0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
10dd1 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 pp = &pPag
10dd2 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 e->pNext;.
10dd3 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 TESTONLY( nPag
10dd4 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 e++; ). }.
10dd5 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
10dd6 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 t( pCache->nPage
10dd7 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a ==nPage );.}../*
10dd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ddb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ddc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10ddd 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
10dde 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 _pcache Methods
10ddf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10de1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10de2 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10de3 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10de4 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 ite3_pcache.xIni
10de5 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 t method..*/.sta
10de6 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 tic int pcache1I
10de7 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 nit(void *NotUse
10de8 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
10de9 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
10dea 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 . assert( pcach
10deb 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a e1.isInit==0 );.
10dec 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 memset(&pcache
10ded 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 1, 0, sizeof(pca
10dee 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 che1));. if( sq
10def 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
10df0 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
10df1 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 pcache1.mute
10df2 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x = sqlite3_mute
10df3 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d x_alloc(SQLITE_M
10df4 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 UTEX_STATIC_LRU)
10df5 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e ;. }. pcache1.
10df6 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 isInit = 1;. re
10df7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10df8 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10df9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10dfa 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 qlite3_pcache.xS
10dfb 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a hutdown method..
10dfc 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
10dfd 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c static mutex al
10dfe 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 located in xInit
10dff 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 does .** not ne
10e00 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a ed to be freed..
10e01 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10e02 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 cache1Shutdown(v
10e03 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
10e04 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10e05 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 R(NotUsed);. as
10e06 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 sert( pcache1.is
10e07 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d Init!=0 );. mem
10e08 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c set(&pcache1, 0,
10e09 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 sizeof(pcache1)
10e0a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
10e0b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10e0c 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10e0d 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e .xCreate method.
10e0e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
10e0f 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a a new cache..*/.
10e10 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 static sqlite3_p
10e11 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 cache *pcache1Cr
10e12 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c eate(int szPage,
10e13 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 int bPurgeable)
10e14 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
10e15 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d che;.. pCache =
10e16 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 (PCache1 *)sqli
10e17 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f te3_malloc(sizeo
10e18 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 f(PCache1));. i
10e19 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 f( pCache ){.
10e1a 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 memset(pCache,
10e1b 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 0, sizeof(PCache
10e1c 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 1));. pCache-
10e1d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 >szPage = szPage
10e1e 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 ;. pCache->bP
10e1f 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 urgeable = (bPur
10e20 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b geable ? 1 : 0);
10e21 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61 . if( bPurgea
10e22 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ble ){. pCa
10e23 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a che->nMin = 10;.
10e24 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 pcache1Ent
10e25 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 erMutex();.
10e26 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 pcache1.nMinPag
10e27 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 e += pCache->nMi
10e28 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 n;. pcache1
10e29 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10e2a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
10e2b 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 (sqlite3_pcache
10e2c 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a *)pCache;.}../*
10e2d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10e2e 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10e2f 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 3_pcache.xCaches
10e30 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a ize method. .**.
10e31 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 ** Configure the
10e32 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 cache_size limi
10e33 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a t for a cache..*
10e34 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10e35 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 ache1Cachesize(s
10e36 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
10e37 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 , int nMax){. P
10e38 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10e39 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
10e3a 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10e3b 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 rgeable ){. p
10e3c 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10e3d 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ();. pcache1.
10e3e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 nMaxPage += (nMa
10e3f 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 x - pCache->nMax
10e40 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e );. pCache->n
10e41 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 Max = nMax;.
10e42 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
10e43 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 xPage();. pca
10e44 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10e45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
10e46 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10e47 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10e48 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d che.xPagecount m
10e49 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 ethod. .*/.stati
10e4a 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 c int pcache1Pag
10e4b 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 ecount(sqlite3_p
10e4c 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 cache *p){. int
10e4d 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 n;. pcache1Ent
10e4e 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d erMutex();. n =
10e4f 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d ((PCache1 *)p)-
10e50 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 >nPage;. pcache
10e51 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10e52 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a return n;.}../*
10e53 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10e54 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10e55 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 3_pcache.xFetch
10e56 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 method. .**.** F
10e57 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b etch a page by k
10e58 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 ey value..**.**
10e59 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 Whether or not a
10e5a 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 new page may be
10e5b 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
10e5c 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 is function depe
10e5d 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 nds on.** the va
10e5e 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 lue of the creat
10e5f 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 eFlag argument.
10e60 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 0 means do not
10e61 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a allocate a new.*
10e62 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 * page. 1 means
10e63 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
10e64 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73 page if space is
10e65 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c easily availabl
10e66 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 e. 2 .** means
10e67 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 to try really ha
10e68 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 rd to allocate a
10e69 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a new page..**.**
10e6a 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 For a non-purge
10e6b 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 able cache (a ca
10e6c 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20 che used as the
10e6d 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 storage for an i
10e6e 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 n-memory.** data
10e6f 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72 base) there is r
10e70 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 eally no differe
10e71 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 nce between crea
10e72 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 teFlag 1 and 2.
10e73 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 So.** the calli
10e74 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 ng function (pca
10e75 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 che.c) will neve
10e76 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46 r have a createF
10e77 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 lag of 1 on.** a
10e78 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 non-purgable ca
10e79 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 che..**.** There
10e7a 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65 are three diffe
10e7b 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 rent approaches
10e7c 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 to obtaining spa
10e7d 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a ce for a page,.*
10e7e 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 * depending on t
10e7f 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 he value of para
10e80 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 meter createFlag
10e81 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 (which may be 0
10e82 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a , 1 or 2)..**.**
10e83 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 1. Regardless
10e84 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 of the value of
10e85 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 createFlag, the
10e86 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68 cache is search
10e87 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 ed for a .**
10e88 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 copy of the re
10e89 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 quested page. If
10e8a 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 one is found, i
10e8b 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a t is returned..*
10e8c 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 *.** 2. If cre
10e8d 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 ateFlag==0 and t
10e8e 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
10e8f 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
10e90 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 che, NULL is.**
10e91 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a returned..*
10e92 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 *.** 3. If cre
10e93 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e ateFlag is 1, an
10e94 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
10e95 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 t already in the
10e96 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 cache,.**
10e97 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 and if either of
10e98 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
10e99 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 re true, return
10e9a 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 NULL:.**.**
10e9b 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 (a) the number
10e9c 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 of pages pinned
10e9d 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73 by the cache is
10e9e 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
10e9f 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10ea0 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 e1.nMax, or.**
10ea1 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d (b) the num
10ea2 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e ber of pages pin
10ea3 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 ned by the cache
10ea4 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
10ea5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
10ea6 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f e sum of nMax fo
10ea7 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 r all purgeable
10ea8 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 caches, less the
10ea9 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 sum of .**
10eaa 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 nMin for a
10eab 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 ll other purgeab
10eac 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a le caches. .**.*
10ead 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f * 4. If none o
10eae 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 f the first thre
10eaf 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 e conditions app
10eb0 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 ly and the cache
10eb1 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 is marked.**
10eb2 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c as purgeable,
10eb3 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 and if one of t
10eb4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
10eb5 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 true:.**.**
10eb6 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 (a) The number
10eb7 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 of pages alloca
10eb8 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 ted for the cach
10eb9 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a e is already .**
10eba 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10ebb 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a e1.nMax, or.**.*
10ebc 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20 * (b) The
10ebd 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
10ebe 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c allocated for al
10ebf 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 l purgeable cach
10ec0 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 es is.**
10ec1 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c already equal
10ec2 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 to or greater t
10ec3 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e han the sum of n
10ec4 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 Max for all.**
10ec5 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62 purgeab
10ec6 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a le caches,.**.**
10ec7 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d then attem
10ec8 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 pt to recycle a
10ec9 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 page from the LR
10eca 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 U list. If it is
10ecb 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 the right.**
10ecc 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 size, return
10ecd 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 the recycled buf
10ece 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 fer. Otherwise,
10ecf 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20 free the buffer
10ed0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 and.** proc
10ed1 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a eed to step 5. .
10ed2 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 **.** 5. Other
10ed3 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 wise, allocate a
10ed4 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 nd return a new
10ed5 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a page buffer..*/.
10ed6 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 static void *pca
10ed7 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 che1Fetch(sqlite
10ed8 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 3_pcache *p, uns
10ed9 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 igned int iKey,
10eda 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b int createFlag){
10edb 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10edc 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 nPinned;. PCach
10edd 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
10ede 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 ache1 *)p;. PgH
10edf 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a dr1 *pPage = 0;.
10ee0 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 . assert( pCach
10ee1 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c e->bPurgeable ||
10ee2 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 createFlag!=1 )
10ee3 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10ee4 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 Mutex();. if( c
10ee5 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 reateFlag==1 ) s
10ee6 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
10ee7 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a nMalloc();.. /*
10ee8 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68 Search the hash
10ee9 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 table for an ex
10eea 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f isting entry. */
10eeb 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e . if( pCache->n
10eec 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e Hash>0 ){. un
10eed 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 signed int h = i
10eee 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 Key % pCache->nH
10eef 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 ash;. for(pPa
10ef0 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ge=pCache->apHas
10ef1 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 h[h]; pPage&&pPa
10ef2 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 ge->iKey!=iKey;
10ef3 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 pPage=pPage->pNe
10ef4 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 xt);. }.. if(
10ef5 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 pPage || createF
10ef6 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 lag==0 ){. pc
10ef7 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 ache1PinPage(pPa
10ef8 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ge);. goto fe
10ef9 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10efa 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 /* Step 3 of hea
10efb 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a der comment. */.
10efc 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 nPinned = pCac
10efd 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 he->nPage - pCac
10efe 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b he->nRecyclable;
10eff 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 . if( createFla
10f00 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 g==1 && (.
10f01 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 nPinned>=(pcac
10f02 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 he1.nMaxPage+pCa
10f03 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 che->nMin-pcache
10f04 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 1.nMinPage).
10f05 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 || nPinned>=(pC
10f06 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f ache->nMax * 9 /
10f07 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 10). )){. g
10f08 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 oto fetch_out;.
10f09 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 }.. if( pCache
10f0a 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d ->nPage>=pCache-
10f0b 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 >nHash && pcache
10f0c 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 1ResizeHash(pCac
10f0d 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 he) ){. goto
10f0e 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
10f0f 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 /* Step 4. Try
10f10 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 to recycle a pa
10f11 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70 ge buffer if app
10f12 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 ropriate. */. i
10f13 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10f14 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 eable && pcache1
10f15 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 .pLruTail && (.
10f16 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 (pCache->nPa
10f17 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d ge+1>=pCache->nM
10f18 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e ax) || pcache1.n
10f19 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 CurrentPage>=pca
10f1a 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 che1.nMaxPage.
10f1b 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 )){. pPage =
10f1c 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10f1d 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d ;. pcache1Rem
10f1e 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 oveFromHash(pPag
10f1f 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 e);. pcache1P
10f20 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10f21 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 if( pPage->pC
10f22 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 ache->szPage!=pC
10f23 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a ache->szPage ){.
10f24 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 pcache1Fre
10f25 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
10f26 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 pPage = 0;.
10f27 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
10f28 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
10f29 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e Page -= (pPage->
10f2a 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10f2b 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 le - pCache->bPu
10f2c 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a rgeable);. }.
10f2d 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 }.. /* Step 5
10f2e 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 . If a usable pa
10f2f 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74 ge buffer has st
10f30 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 ill not been fou
10f31 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 nd, . ** attemp
10f32 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 t to allocate a
10f33 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 new one. . */.
10f34 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 if( !pPage ){.
10f35 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 pPage = pcach
10f36 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 e1AllocPage(pCac
10f37 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 he);. }.. if(
10f38 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 pPage ){. uns
10f39 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b igned int h = iK
10f3a 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 ey % pCache->nHa
10f3b 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e sh;. pCache->
10f3c 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 nPage++;. pPa
10f3d 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b ge->iKey = iKey;
10f3e 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 . pPage->pNex
10f3f 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10f40 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 sh[h];. pPage
10f41 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
10f42 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c e;. pPage->pL
10f43 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ruPrev = 0;.
10f44 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10f45 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 = 0;. *(void
10f46 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 **)(PGHDR1_TO_PA
10f47 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a GE(pPage)) = 0;.
10f48 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 pCache->apHa
10f49 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 sh[h] = pPage;.
10f4a 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 }..fetch_out:.
10f4b 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b if( pPage && iK
10f4c 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b ey>pCache->iMaxK
10f4d 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 ey ){. pCache
10f4e 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 ->iMaxKey = iKey
10f4f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 ;. }. if( crea
10f50 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 teFlag==1 ) sqli
10f51 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
10f52 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c oc();. pcache1L
10f53 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
10f54 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 eturn (pPage ? P
10f55 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 GHDR1_TO_PAGE(pP
10f56 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f age) : 0);.}.../
10f57 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10f58 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10f59 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e e3_pcache.xUnpin
10f5a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d method..**.** M
10f5b 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e ark a page as un
10f5c 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 pinned (eligible
10f5d 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 for asynchronou
10f5e 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f s recycling)..*/
10f5f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10f60 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 che1Unpin(sqlite
10f61 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 3_pcache *p, voi
10f62 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 d *pPg, int reus
10f63 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 eUnlikely){. PC
10f64 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
10f65 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
10f66 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 PgHdr1 *pPage =
10f67 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10f68 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 Cache, pPg);. .
10f69 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10f6a 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 pCache==pCache )
10f6b 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10f6c 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 Mutex();.. /* I
10f6d 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f t is an error to
10f6e 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
10f6f 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 ion if the page
10f70 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a is already . **
10f71 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f part of the glo
10f72 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 bal LRU list..
10f73 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
10f74 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 ge->pLruPrev==0
10f75 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 && pPage->pLruNe
10f76 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 xt==0 );. asser
10f77 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 t( pcache1.pLruH
10f78 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 ead!=pPage && pc
10f79 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d ache1.pLruTail!=
10f7a 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 pPage );.. if(
10f7b 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c reuseUnlikely ||
10f7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10f7d 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d tPage>pcache1.nM
10f7e 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 axPage ){. pc
10f7f 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10f80 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
10f81 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10f82 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b pPage);. }else{
10f83 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
10f84 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 page to the glob
10f85 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 al LRU list. Nor
10f86 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 mally, the page
10f87 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 is added to.
10f88 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 ** the head of t
10f89 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 he list (last pa
10f8a 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 ge to be recycle
10f8b 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 d). However, if
10f8c 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 the . ** reus
10f8d 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 eUnlikely flag p
10f8e 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
10f8f 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 nction is true,
10f90 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10f91 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 d. ** to the
10f92 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 tail of the list
10f93 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 (first page to
10f94 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 be recycled)..
10f95 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 */. if( pca
10f96 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b che1.pLruHead ){
10f97 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10f98 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 LruHead->pLruPre
10f99 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 v = pPage;.
10f9a 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10f9b 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 = pcache1.pLruH
10f9c 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 ead;. pcach
10f9d 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 e1.pLruHead = pP
10f9e 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a age;. }else{.
10f9f 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10fa0 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a ruTail = pPage;.
10fa1 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10fa2 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a ruHead = pPage;.
10fa3 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 }. pCache
10fa4 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b ->nRecyclable++;
10fa5 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
10fa6 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a eaveMutex();.}..
10fa7 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10fa8 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10fa9 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 te3_pcache.xReke
10faa 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 y method. .*/.st
10fab 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10fac 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 1Rekey(. sqlite
10fad 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 3_pcache *p,. v
10fae 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 oid *pPg,. unsi
10faf 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 gned int iOld,.
10fb0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e unsigned int iN
10fb1 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ew.){. PCache1
10fb2 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10fb3 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10fb4 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 *pPage = PAGE_T
10fb5 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10fb6 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 pPg);. PgHdr1
10fb7 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 **pp;. unsigned
10fb8 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 int h; . asser
10fb9 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d t( pPage->iKey==
10fba 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 iOld );. assert
10fbb 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d ( pPage->pCache=
10fbc 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 =pCache );.. pc
10fbd 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10fbe 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 );.. h = iOld%p
10fbf 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10fc0 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10fc1 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 Hash[h];. while
10fc2 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 ( (*pp)!=pPage )
10fc3 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 {. pp = &(*pp
10fc4 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 )->pNext;. }.
10fc5 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 *pp = pPage->pNe
10fc6 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 xt;.. h = iNew%
10fc7 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
10fc8 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 pPage->iKey = i
10fc9 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e New;. pPage->pN
10fca 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 ext = pCache->ap
10fcb 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 Hash[h];. pCach
10fcc 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 e->apHash[h] = p
10fcd 50 61 67 65 3b 0a 20 20 69 66 28 20 69 4e 65 77 Page;. if( iNew
10fce 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 >pCache->iMaxKey
10fcf 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ){. pCache->
10fd0 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a iMaxKey = iNew;.
10fd1 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 }.. pcache1Le
10fd2 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f aveMutex();.}../
10fd3 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10fd4 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10fd5 65 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 e3_pcache.xTrunc
10fd6 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a ate method. .**.
10fd7 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 ** Discard all u
10fd8 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e npinned pages in
10fd9 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 the cache with
10fda 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 a page number eq
10fdb 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 ual to.** or gre
10fdc 61 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 ater than parame
10fdd 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 ter iLimit. Any
10fde 70 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 pinned pages wit
10fdf 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a h a page number.
10fe0 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 ** equal to or g
10fe1 72 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d reater than iLim
10fe2 69 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c it are implicitl
10fe3 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 y unpinned..*/.s
10fe4 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10fe5 65 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 e1Truncate(sqlit
10fe6 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e e3_pcache *p, un
10fe7 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 signed int iLimi
10fe8 74 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 t){. PCache1 *p
10fe9 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10fea 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 *)p;. pcache1E
10feb 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 nterMutex();. i
10fec 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 f( iLimit<=pCach
10fed 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 e->iMaxKey ){.
10fee 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 pcache1Truncat
10fef 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 eUnsafe(pCache,
10ff0 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 iLimit);. pCa
10ff1 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 che->iMaxKey = i
10ff2 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 Limit-1;. }. p
10ff3 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10ff4 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ();.}../*.** Imp
10ff5 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10ff6 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10ff7 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f e.xDestroy metho
10ff8 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f d. .**.** Destro
10ff9 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 y a cache alloca
10ffa 74 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 ted using pcache
10ffb 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 1Create()..*/.st
10ffc 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10ffd 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 1Destroy(sqlite3
10ffe 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 _pcache *p){. P
10fff 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
11000 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
11001 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
11002 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 ex();. pcache1T
11003 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 runcateUnsafe(pC
11004 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 ache, 0);. pcac
11005 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 he1.nMaxPage -=
11006 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 pCache->nMax;.
11007 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 pcache1.nMinPage
11008 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e -= pCache->nMin
11009 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 ;. pcache1Enfor
1100a 63 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 ceMaxPage();. p
1100b 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
1100c 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 ();. sqlite3_fr
1100d 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ee(pCache->apHas
1100e 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 h);. sqlite3_fr
1100f 65 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f ee(pCache);.}../
11010 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
11011 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 on is called dur
11012 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ing initializati
11013 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 on (sqlite3_init
11014 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 ialize()) to.**
11015 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 install the defa
11016 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 ult pluggable ca
11017 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 che module, assu
11018 6d 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 ming the user ha
11019 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 s not.** already
1101a 20 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 provided an alt
1101b 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c ernative..*/.SQL
1101c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1101d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 sqlite3PCacheSe
1101e 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
1101f 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
11020 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 _pcache_methods
11021 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
11022 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 {. 0,
11023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11024 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 /* pArg */. p
11025 63 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 cache1Init,
11026 20 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 /* xInit
11027 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 */. pcache1S
11028 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 hutdown,
11029 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f /* xShutdown */
1102a 0a 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61 . pcache1Crea
1102b 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a te, /*
1102c 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 xCreate */.
1102d 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 pcache1Cachesize
1102e 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 , /* xCac
1102f 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 hesize */. pc
11030 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 ache1Pagecount,
11031 20 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 /* xPagec
11032 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 ount */. pcac
11033 68 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 he1Fetch,
11034 20 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a /* xFetch *
11035 2f 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 /. pcache1Unp
11036 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f in, /
11037 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 * xUnpin */.
11038 70 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 pcache1Rekey,
11039 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b /* xRek
1103a 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 ey */. pcache
1103b 31 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 1Truncate,
1103c 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 /* xTruncate
1103d 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 */. pcache1De
1103e 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 stroy
1103f 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 /* xDestroy */.
11040 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f };. sqlite3_co
11041 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 nfig(SQLITE_CONF
11042 49 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 IG_PCACHE, &defa
11043 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a ultMethods);.}..
11044 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
11045 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
11046 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 GEMENT./*.** Thi
11047 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
11048 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 lled to free sup
11049 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 erfluous dynamic
1104a 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d ally allocated m
1104b 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 emory.** held by
1104c 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65 the pager syste
1104d 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 m. Memory in use
1104e 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 by any SQLite p
1104f 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a ager allocated.*
11050 2a 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 * by the current
11051 20 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 thread may be s
11052 71 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e qlite3_free()ed.
11053 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 .**.** nReq is t
11054 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
11055 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 es of memory req
11056 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 uired. Once this
11057 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 much has.** bee
11058 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 n released, the
11059 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1105a 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c . The return val
1105b 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 ue is the total
1105c 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 number .** of by
1105d 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 tes of memory re
1105e 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 leased..*/.SQLIT
1105f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11060 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
11061 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 seMemory(int nRe
11062 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 q){. int nFree
11063 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 = 0;. if( pcach
11064 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a e1.pStart==0 ){.
11065 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 PgHdr1 *p;.
11066 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d pcache1EnterM
11067 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c utex();. whil
11068 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 e( (nReq<0 || nF
11069 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d ree<nReq) && (p=
1106a 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
1106b 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 ) ){. nFree
1106c 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f += sqlite3Mallo
1106d 63 53 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f cSize(PGHDR1_TO_
1106e 50 41 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20 PAGE(p));.
1106f 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
11070 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 );. pcache1
11071 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 RemoveFromHash(p
11072 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 );. pcache1
11073 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 FreePage(p);.
11074 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 }. pcache1Le
11075 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
11076 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a return nFree;.
11077 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
11078 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 TE_ENABLE_MEMORY
11079 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a _MANAGEMENT */..
1107a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1107b 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ST./*.** This fu
1107c 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 nction is used b
1107d 79 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 y test procedure
1107e 73 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 s to inspect the
1107f 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a internal state.
11080 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c ** of the global
11081 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
11082 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
11083 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 qlite3PcacheStat
11084 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 s(. int *pnCurr
11085 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 ent, /* OUT
11086 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f : Total number o
11087 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a f pages cached *
11088 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 /. int *pnMax,
11089 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1108a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 Global maximum
1108b 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 cache size */.
1108c 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 int *pnMin,
1108d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d /* OUT: Sum
1108e 20 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e of PCache1.nMin
1108f 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 for purgeable c
11090 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a aches */. int *
11091 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 pnRecyclable
11092 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 /* OUT: Total nu
11093 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 mber of pages av
11094 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 ailable for recy
11095 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 cling */.){. Pg
11096 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e Hdr1 *p;. int n
11097 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a Recyclable = 0;.
11098 20 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e for(p=pcache1.
11099 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 pLruHead; p; p=p
1109a 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 ->pLruNext){.
1109b 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a nRecyclable++;.
1109c 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 }. *pnCurrent
1109d 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 = pcache1.nCurr
1109e 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 entPage;. *pnMa
1109f 78 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 x = pcache1.nMax
110a0 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d Page;. *pnMin =
110a1 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 pcache1.nMinPag
110a2 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 e;. *pnRecyclab
110a3 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 le = nRecyclable
110a4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
110a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
110a6 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a of pcache1.c ***
110a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
110aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
110ab 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 n file rowset.c
110ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
110af 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 * 2008 December
110b0 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
110b1 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
110b2 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
110b3 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
110b4 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
110b5 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
110b6 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
110b7 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
110b8 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
110b9 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
110ba 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
110bb 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
110bc 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
110bd 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
110be 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
110bf 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
110c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
110c1 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
110c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110c6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 .**.** This modu
110c7 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e le implements an
110c8 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 object we call
110c9 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a a "RowSet"..**.*
110ca 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a * The RowSet obj
110cb 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74 ect is a collect
110cc 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20 ion of rowids.
110cd 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e Rowids.** are in
110ce 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 serted into the
110cf 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62 RowSet in an arb
110d0 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49 itrary order. I
110d1 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65 nserts.** can be
110d2 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68 intermixed with
110d3 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 69 66 tests to see if
110d4 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68 a given rowid h
110d5 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69 as been.** previ
110d6 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69 ously inserted i
110d7 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a nto the RowSet..
110d8 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 **.** After all
110d9 69 6e 73 65 72 74 73 20 61 72 65 20 66 69 6e 69 inserts are fini
110da 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73 shed, it is poss
110db 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20 ible to extract
110dc 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 the.** elements
110dd 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e of the RowSet in
110de 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 sorted order.
110df 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72 61 63 Once this extrac
110e0 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20 tion.** process
110e1 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20 has started, no
110e2 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79 new elements may
110e3 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a be inserted..**
110e4 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70 .** Hence, the p
110e5 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69 rimitive operati
110e6 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74 ons for a RowSet
110e7 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43 are:.**.** C
110e8 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45 REATE.** INSE
110e9 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a RT.** TEST.**
110ea 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 SMALLEST.**
110eb 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a DESTROY.**.**
110ec 20 54 68 65 20 43 52 45 41 54 45 20 61 6e 64 20 The CREATE and
110ed 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76 DESTROY primitiv
110ee 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74 es are the const
110ef 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72 ructor and destr
110f0 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75 uctor,.** obviou
110f1 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54 sly. The INSERT
110f2 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 73 20 primitive adds
110f3 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f a new element to
110f4 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
110f5 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73 TEST checks to s
110f6 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74 ee if an element
110f7 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
110f8 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c he RowSet. SMAL
110f9 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73 LEST.** extracts
110fa 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65 the least value
110fb 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 from the RowSet
110fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45 ..**.** The INSE
110fd 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67 RT primitive mig
110fe 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69 ht allocate addi
110ff 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20 tional memory.
11100 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c Memory is.** all
11101 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73 ocated in chunks
11102 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73 so most INSERTs
11103 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f do no allocatio
11104 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20 n. There is an
11105 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20 .** upper bound
11106 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 on the size of a
11107 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e llocated memory.
11108 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66 No memory is f
11109 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45 reed.** until DE
1110a 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 STROY..**.** The
1110b 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 20 TEST primitive
1110c 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61 74 63 includes a "batc
1110d 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 h" number. The
1110e 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a TEST primitive.*
1110f 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20 * will only see
11110 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65 elements that we
11111 72 65 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f re inserted befo
11112 72 65 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e re the last chan
11113 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74 ge.** in the bat
11114 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f ch number. In o
11115 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 ther words, if a
11116 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20 n INSERT occurs
11117 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54 between.** two T
11118 45 53 54 73 20 77 68 65 72 65 20 74 68 65 20 54 ESTs where the T
11119 45 53 54 73 20 68 61 76 65 20 74 68 65 20 73 61 ESTs have the sa
1111a 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c me batch nubmer,
1111b 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c then the.** val
1111c 75 65 20 61 64 64 65 64 20 62 79 20 74 68 65 20 ue added by the
1111d 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20 INSERT will not
1111e 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68 be visible to th
1111f 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a e second TEST..*
11120 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61 * The initial ba
11121 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65 tch number is ze
11122 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65 ro, so if the ve
11123 72 79 20 66 69 72 73 74 20 54 45 53 54 20 63 6f ry first TEST co
11124 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d ntains.** a non-
11125 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65 zero batch numbe
11126 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61 r, it will see a
11127 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73 ll prior INSERTs
11128 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52 ..**.** No INSER
11129 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66 Ts may occurs af
1112a 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20 ter a SMALLEST.
1112b 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69 An assertion wi
1112c 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68 ll fail if.** th
1112d 61 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e at is attempted.
1112e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 .**.** The cost
1112f 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20 of an INSERT is
11130 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74 roughly constant
11131 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77 . (Sometime new
11132 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74 memory.** has t
11133 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f o be allocated o
11134 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54 n an INSERT.) T
11135 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 he cost of a TES
11136 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 T with a new.**
11137 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 batch number is
11138 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e O(NlogN) where N
11139 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
1113a 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 f elements in th
1113b 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65 e RowSet..** The
1113c 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20 cost of a TEST
1113d 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62 using the same b
1113e 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f atch number is O
1113f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73 (logN). The cos
11140 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 t.** of the firs
11141 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28 t SMALLEST is O(
11142 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20 NlogN). Second
11143 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 53 and subsequent S
11144 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69 MALLEST.** primi
11145 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61 tives are consta
11146 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f nt time. The co
11147 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73 st of DESTROY is
11148 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 O(N)..**.** The
11149 72 65 20 69 73 20 61 6e 20 61 64 64 65 64 20 63 re is an added c
1114a 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e ost of O(N) when
1114b 20 73 77 69 74 63 68 69 6e 67 20 62 65 74 77 65 switching betwe
1114c 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53 en TEST and.** S
1114d 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76 MALLEST primitiv
1114e 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 es..*/.../*.** T
1114f 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 arget size for a
11150 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 llocation chunks
11151 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 ..*/.#define ROW
11152 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 SET_ALLOCATION_S
11153 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 IZE 1024../*.**
11154 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f The number of ro
11155 77 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 wset entries per
11156 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e allocation chun
11157 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f k..*/.#define RO
11158 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 WSET_ENTRY_PER_C
11159 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 HUNK \.
1115a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1115b 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 (ROWSET_ALLOCATI
1115c 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f ON_SIZE-8)/sizeo
1115d 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 f(struct RowSetE
1115e 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 ntry))../*.** Ea
1115f 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f ch entry in a Ro
11160 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 wSet is an insta
11161 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
11162 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a wing object..*/.
11163 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
11164 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 ry {
11165 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 . i64 v;
11166 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11167 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 /* ROWID value
11168 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a for this entry *
11169 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 /. struct RowSe
1116a 74 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 tEntry *pRight;
1116b 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 /* Right subtr
1116c 65 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 ee (larger entri
1116d 65 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 es) or list */.
1116e 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
1116f 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f try *pLeft; /
11170 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 * Left subtree (
11171 73 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 smaller entries)
11172 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f */.};../*.** Ro
11173 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 wSetEntry object
11174 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 s are allocated
11175 69 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 in large chunks
11176 28 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 (instances of th
11177 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 e.** following s
11178 74 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 tructure) to red
11179 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 uce memory alloc
1117a 61 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 ation overhead.
1117b 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 The.** chunks a
1117c 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e re kept on a lin
1117d 6b 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 ked list so that
1117e 20 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 they can be dea
1117f 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e llocated.** when
11180 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 the RowSet is d
11181 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 estroyed..*/.str
11182 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 uct RowSetChunk
11183 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 {. struct RowSe
11184 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 tChunk *pNextChu
11185 6e 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 nk; /* Ne
11186 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 xt chunk on list
11187 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a of them all */.
11188 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11189 6e 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 ntry aEntry[ROWS
1118a 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 ET_ENTRY_PER_CHU
1118b 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 NK]; /* Allocate
1118c 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a d entries */.};.
1118d 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 ./*.** A RowSet
1118e 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f in an instance o
1118f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
11190 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
11191 20 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 A typedef of th
11192 69 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20 is structure if
11193 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 found in sqliteI
11194 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 nt.h..*/.struct
11195 52 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 RowSet {. struc
11196 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 t RowSetChunk *p
11197 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 Chunk; /* Lis
11198 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 t of all chunk a
11199 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 llocations */.
1119a 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
1119b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1119c 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1119d 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 onnection */. s
1119e 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
1119f 79 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a y *pEntry; /*
111a0 20 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 List of entries
111a1 20 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f using pRight */
111a2 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
111a3 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 Entry *pLast;
111a4 20 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 /* Last entry
111a5 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 on the pEntry li
111a6 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 st */. struct R
111a7 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 owSetEntry *pFre
111a8 73 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 sh; /* Source
111a9 20 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 of new entry ob
111aa 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 jects */. struc
111ab 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
111ac 54 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e Tree; /* Bin
111ad 61 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 ary tree of entr
111ae 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 ies */. u16 nFr
111af 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 esh;
111b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
111b1 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 r of objects on
111b2 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 pFresh */. u8 i
111b3 73 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 sSorted;
111b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
111b5 75 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 ue if pEntry is
111b6 73 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 sorted */. u8 i
111b7 42 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 Batch;
111b8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
111b9 72 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 rrent insert bat
111ba 63 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ch */.};../*.**
111bb 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 Turn bulk memory
111bc 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f into a RowSet o
111bd 62 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 bject. N bytes
111be 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 of memory.** are
111bf 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 available at pS
111c0 70 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f pace. The db po
111c1 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
111c2 20 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 a memory contex
111c3 74 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 t.** for any sub
111c4 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 sequent allocati
111c5 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
111c6 20 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 occur..** Retur
111c7 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
111c8 68 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 he new RowSet ob
111c9 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d ject..**.** It m
111ca 75 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20 ust be the case
111cb 74 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 that N is suffic
111cc 69 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 ient to make a R
111cd 6f 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a owset. If not.*
111ce 2a 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 * an assertion f
111cf 61 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 ault occurs..**
111d0 0a 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 .** If N is larg
111d1 65 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 er than the mini
111d2 6d 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 mum, use the sur
111d3 70 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 plus as an initi
111d4 61 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e al.** allocation
111d5 20 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 of entries avai
111d6 6c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c lable to be fill
111d7 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
111d8 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 IVATE RowSet *sq
111d9 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 lite3RowSetInit(
111da 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
111db 64 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 d *pSpace, unsig
111dc 6e 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f ned int N){. Ro
111dd 77 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 wSet *p;. asser
111de 74 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 t( N >= ROUND8(s
111df 69 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 izeof(*p)) );.
111e0 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d p = pSpace;. p-
111e1 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 >pChunk = 0;. p
111e2 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e ->db = db;. p->
111e3 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d pEntry = 0;. p-
111e4 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d >pLast = 0;. p-
111e5 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d >pTree = 0;. p-
111e6 3e 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63 >pFresh = (struc
111e7 74 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 t RowSetEntry*)(
111e8 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 ROUND8(sizeof(*p
111e9 29 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a )) + (char*)p);.
111ea 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 p->nFresh = (u
111eb 31 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 16)((N - ROUND8(
111ec 73 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a sizeof(*p)))/siz
111ed 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 eof(struct RowSe
111ee 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 tEntry));. p->i
111ef 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 sSorted = 1;. p
111f0 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 ->iBatch = 0;.
111f1 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
111f2 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c ** Deallocate al
111f3 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 l chunks from a
111f4 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72 RowSet. This fr
111f5 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 ees all memory t
111f6 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 hat.** the RowSe
111f7 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 t has allocated
111f8 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d over its lifetim
111f9 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
111fa 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 is.** the destr
111fb 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f uctor for the Ro
111fc 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wSet..*/.SQLITE_
111fd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
111fe 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
111ff 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 RowSet *p){. st
11200 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
11201 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 *pChunk, *pNext
11202 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 Chunk;. for(pCh
11203 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 unk=p->pChunk; p
11204 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 Chunk; pChunk =
11205 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 pNextChunk){.
11206 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 pNextChunk = pC
11207 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b hunk->pNextChunk
11208 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
11209 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e ree(p->db, pChun
1120a 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 k);. }. p->pCh
1120b 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 unk = 0;. p->nF
1120c 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 resh = 0;. p->p
1120d 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e Entry = 0;. p->
1120e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e pLast = 0;. p->
1120f 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e pTree = 0;. p->
11210 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a isSorted = 1;.}.
11211 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
11212 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 new value into a
11213 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 RowSet..**.** T
11214 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
11215 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61 flag of the data
11216 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
11217 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d is set if a.** m
11218 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
11219 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 fails..*/.SQLIT
1121a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1121b 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
1121c 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 rt(RowSet *p, i6
1121d 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 4 rowid){. stru
1121e 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
1121f 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 pEntry; /* The
11220 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 new entry */. s
11221 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11222 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 y *pLast; /* T
11223 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e he last prior en
11224 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 try */. assert(
11225 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 p!=0 );. if( p
11226 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 ->nFresh==0 ){.
11227 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
11228 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 Chunk *pNew;.
11229 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
1122a 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 bMallocRaw(p->db
1122b 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 , sizeof(*pNew))
1122c 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d ;. if( pNew==
1122d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
1122e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 n;. }. pNe
1122f 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 w->pNextChunk =
11230 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 p->pChunk;. p
11231 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b ->pChunk = pNew;
11232 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d . p->pFresh =
11233 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 pNew->aEntry;.
11234 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 p->nFresh = R
11235 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f OWSET_ENTRY_PER_
11236 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e CHUNK;. }. pEn
11237 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b try = p->pFresh+
11238 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d +;. p->nFresh--
11239 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 ;. pEntry->v =
1123a 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d rowid;. pEntry-
1123b 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 >pRight = 0;. p
1123c 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b Last = p->pLast;
1123d 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a . if( pLast ){.
1123e 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 if( p->isSor
1123f 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c ted && rowid<=pL
11240 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 ast->v ){.
11241 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b p->isSorted = 0;
11242 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 . }. pLast
11243 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 ->pRight = pEntr
11244 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 y;. }else{.
11245 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 assert( p->pEntr
11246 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 y==0 ); /* Fires
11247 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72 if INSERT after
11248 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 SMALLEST */.
11249 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e p->pEntry = pEn
1124a 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c try;. }. p->pL
1124b 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a ast = pEntry;.}.
1124c 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f ./*.** Merge two
1124d 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74 lists of RowSet
1124e 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 Entry objects.
1124f 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65 Remove duplicate
11250 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 s..**.** The inp
11251 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e ut lists are con
11252 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68 nected via pRigh
11253 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 t pointers and a
11254 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 re .** assumed t
11255 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62 o each already b
11256 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
11257 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 r..*/.static str
11258 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11259 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 *rowSetMerge(.
1125a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1125b 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 ry *pA, /* Fi
1125c 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 rst sorted list
1125d 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a to be merged */.
1125e 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1125f 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 ntry *pB /*
11260 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 Second sorted li
11261 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 st to be merged
11262 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 */.){. struct R
11263 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b owSetEntry head;
11264 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11265 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 Entry *pTail;..
11266 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a pTail = &head;.
11267 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 while( pA && p
11268 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 B ){. assert(
11269 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c pA->pRight==0 |
1126a 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 | pA->v<=pA->pRi
1126b 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 ght->v );. as
1126c 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74 sert( pB->pRight
1126d 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 ==0 || pB->v<=pB
1126e 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 ->pRight->v );.
1126f 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d if( pA->v<pB-
11270 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 >v ){. pTai
11271 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a l->pRight = pA;.
11272 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
11273 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 Right;. pTa
11274 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 il = pTail->pRig
11275 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ht;. }else if
11276 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b ( pB->v<pA->v ){
11277 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 . pTail->pR
11278 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 ight = pB;.
11279 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 pB = pB->pRight
1127a 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 ;. pTail =
1127b 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 pTail->pRight;.
1127c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1127d 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b pA = pA->pRight;
1127e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1127f 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72 pA ){. asser
11280 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 t( pA->pRight==0
11281 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 || pA->v<=pA->p
11282 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 Right->v );.
11283 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 pTail->pRight =
11284 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 pA;. }else{.
11285 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c assert( pB==0 |
11286 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 | pB->pRight==0
11287 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 || pB->v<=pB->pR
11288 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 ight->v );. p
11289 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 Tail->pRight = p
1128a 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 B;. }. return
1128b 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a head.pRight;.}..
1128c 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 /*.** Sort all e
1128d 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 lements on the p
1128e 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68 Entry list of th
1128f 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 e RowSet into as
11290 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a cending order..*
11291 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 / .static void r
11292 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 owSetSort(RowSet
11293 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 *p){. unsigned
11294 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
11295 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
11296 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 ntry;. struct R
11297 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63 owSetEntry *aBuc
11298 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 ket[40];.. asse
11299 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d rt( p->isSorted=
1129a 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 =0 );. memset(a
1129b 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f Bucket, 0, sizeo
1129c 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 f(aBucket));. w
1129d 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 hile( p->pEntry
1129e 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 ){. pEntry =
1129f 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 p->pEntry;. p
112a0 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 ->pEntry = pEntr
112a1 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 y->pRight;. p
112a2 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 Entry->pRight =
112a3 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 0;. for(i=0;
112a4 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 aBucket[i]; i++)
112a5 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d {. pEntry =
112a6 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75 rowSetMerge(aBu
112a7 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 cket[i], pEntry)
112a8 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b ;. aBucket[
112a9 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 i] = 0;. }.
112aa 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 aBucket[i] = p
112ab 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e Entry;. }. pEn
112ac 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 try = 0;. for(i
112ad 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 =0; i<sizeof(aBu
112ae 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 cket)/sizeof(aBu
112af 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a cket[0]); i++){.
112b0 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 pEntry = row
112b1 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c SetMerge(pEntry,
112b2 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 aBucket[i]);.
112b3 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 }. p->pEntry =
112b4 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 pEntry;. p->pLa
112b5 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 st = 0;. p->isS
112b6 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f orted = 1;.}.../
112b7 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 *.** The input,
112b8 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 pIn, is a binary
112b9 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65 tree (or subtre
112ba 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 e) of RowSetEntr
112bb 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f y objects..** Co
112bc 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20 nvert this tree
112bd 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 into a linked li
112be 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 st connected by
112bf 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74 the pRight point
112c0 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 ers.** and retur
112c1 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 n pointers to th
112c2 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 e first and last
112c3 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
112c4 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 new list..*/.st
112c5 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 atic void rowSet
112c6 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 TreeToList(. st
112c7 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
112c8 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f *pIn, /
112c9 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e * Root of the in
112ca 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 put tree */. st
112cb 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
112cc 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f **ppFirst, /
112cd 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20 * Write head of
112ce 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 the output list
112cf 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 here */. struct
112d0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 RowSetEntry **p
112d1 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72 pLast /* Wr
112d2 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 ite tail of the
112d3 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 output list here
112d4 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
112d5 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 pIn!=0 );. if(
112d6 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 pIn->pLeft ){.
112d7 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
112d8 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f Entry *p;. ro
112d9 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 wSetTreeToList(p
112da 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 In->pLeft, ppFir
112db 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e st, &p);. p->
112dc 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 pRight = pIn;.
112dd 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 }else{. *ppFi
112de 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 rst = pIn;. }.
112df 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 if( pIn->pRight
112e0 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72 ){. rowSetTr
112e1 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 eeToList(pIn->pR
112e2 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 ight, &pIn->pRig
112e3 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d ht, ppLast);. }
112e4 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 else{. *ppLas
112e5 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 t = pIn;. }. a
112e6 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29 ssert( (*ppLast)
112e7 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d ->pRight==0 );.}
112e8 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 .../*.** Convert
112e9 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f a sorted list o
112ea 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e f elements (conn
112eb 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29 ected by pRight)
112ec 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a into a binary.*
112ed 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74 * tree with dept
112ee 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20 h of iDepth. A
112ef 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 depth of 1 means
112f0 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69 the tree contai
112f1 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e ns a single.** n
112f2 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 ode taken from t
112f3 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 he head of *ppLi
112f4 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 st. A depth of
112f5 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77 2 means a tree w
112f6 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 ith.** three nod
112f7 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 es. And so fort
112f8 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 h..**.** Use as
112f9 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f many entries fro
112fa 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74 m the input list
112fb 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64 as required and
112fc 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a update the.** *
112fd 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 ppList to point
112fe 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c to the unused el
112ff 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 ements of the li
11300 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 st. If the inpu
11301 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 t.** list contai
11302 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 ns too few eleme
11303 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 nts, then constr
11304 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 uct an incomplet
11305 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 e tree.** and le
11306 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20 ave *ppList set
11307 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 to NULL..**.** R
11308 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
11309 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 to the root of t
1130a 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 he constructed b
1130b 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 inary tree..*/.s
1130c 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 tatic struct Row
1130d 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 SetEntry *rowSet
1130e 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72 NDeepTree(. str
1130f 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11310 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 **ppList,. int
11311 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 iDepth.){. stru
11312 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
11313 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f p; /* Ro
11314 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72 ot of the new tr
11315 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ee */. struct R
11316 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 owSetEntry *pLef
11317 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 t; /* Left s
11318 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 ubtree */. if(
11319 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 *ppList==0 ){.
1131a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1131b 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20 if( iDepth==1
1131c 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 ){. p = *ppLi
1131d 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 st;. *ppList
1131e 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 = p->pRight;.
1131f 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 p->pLeft = p->p
11320 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 Right = 0;. r
11321 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 eturn p;. }. p
11322 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 Left = rowSetNDe
11323 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 epTree(ppList, i
11324 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 Depth-1);. p =
11325 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 *ppList;. if( p
11326 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
11327 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 n pLeft;. }. p
11328 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b ->pLeft = pLeft;
11329 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e . *ppList = p->
1132a 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 pRight;. p->pRi
1132b 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 ght = rowSetNDee
1132c 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 pTree(ppList, iD
1132d 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 epth-1);. retur
1132e 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f n p;.}../*.** Co
1132f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c nvert a sorted l
11330 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ist of elements
11331 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72 into a binary tr
11332 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65 ee. Make the tre
11333 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20 e.** as deep as
11334 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 it needs to be i
11335 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 n order to conta
11336 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 in the entire li
11337 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 st..*/.static st
11338 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11339 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 *rowSetListToTr
1133a 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 ee(struct RowSet
1133b 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 Entry *pList){.
1133c 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 int iDepth;
1133d 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 /* Depth
1133e 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66 of the tree so f
1133f 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ar */. struct R
11340 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 owSetEntry *p;
11341 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
11342 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 tree root */. s
11343 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11344 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c y *pLeft; /* L
11345 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a eft subtree */..
11346 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 assert( pList!
11347 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 =0 );. p = pLis
11348 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e t;. pList = p->
11349 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 pRight;. p->pLe
1134a 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d ft = p->pRight =
1134b 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68 0;. for(iDepth
1134c 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74 =1; pList; iDept
1134d 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 h++){. pLeft
1134e 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 = p;. p = pLi
1134f 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 st;. pList =
11350 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 p->pRight;. p
11351 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b ->pLeft = pLeft;
11352 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d . p->pRight =
11353 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 rowSetNDeepTree
11354 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29 (&pList, iDepth)
11355 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
11356 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
11357 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70 rt the list in p
11358 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 ->pEntry into a
11359 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69 sorted list if i
1135a 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 t is not.** sort
1135b 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20 ed already. If
1135c 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72 there is a binar
1135d 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 y tree on p->pTr
1135e 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 ee, then.** conv
1135f 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 ert it into a li
11360 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 st too and merge
11361 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e it into the p->
11362 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a pEntry list..*/.
11363 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 static void rowS
11364 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 etToList(RowSet
11365 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 *p){. if( !p->i
11366 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72 sSorted ){. r
11367 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 owSetSort(p);.
11368 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65 }. if( p->pTree
11369 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 ){. struct R
1136a 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61 owSetEntry *pHea
1136b 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 d, *pTail;. r
1136c 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 owSetTreeToList(
1136d 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64 p->pTree, &pHead
1136e 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 , &pTail);. p
1136f 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 ->pTree = 0;.
11370 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 p->pEntry = row
11371 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 SetMerge(p->pEnt
11372 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a ry, pHead);. }.
11373 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 }../*.** Extract
11374 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c the smallest el
11375 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 ement from the R
11376 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 owSet..** Write
11377 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f the element into
11378 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 *pRowid. Retur
11379 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 n 1 on success.
1137a 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 Return.** 0 if
1137b 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c the RowSet is al
1137c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a ready empty..**.
1137d 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f ** After this ro
1137e 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 utine has been c
1137f 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 alled, the sqlit
11380 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 e3RowSetInsert()
11381 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 .** routine may
11382 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 not be called ag
11383 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 ain. .*/.SQLITE
11384 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
11385 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 ite3RowSetNext(R
11386 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 owSet *p, i64 *p
11387 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 Rowid){. rowSet
11388 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28 ToList(p);. if(
11389 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 p->pEntry ){.
1138a 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 *pRowid = p->p
1138b 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d Entry->v;. p-
1138c 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e >pEntry = p->pEn
1138d 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 try->pRight;.
1138e 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d if( p->pEntry==
1138f 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
11390 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29 e3RowSetClear(p)
11391 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
11392 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn 1;. }else{.
11393 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
11394 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
11395 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e to see if elemen
11396 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73 t iRowid was ins
11397 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 erted into the t
11398 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 he rowset as.**
11399 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 part of any inse
1139a 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 rt batch prior t
1139b 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 o iBatch. Retur
1139c 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c n 1 or 0..*/.SQL
1139d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1139e 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
1139f 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 t(RowSet *pRowSe
113a0 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71 t, u8 iBatch, sq
113a1 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 lite3_int64 iRow
113a2 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f id){. struct Ro
113a3 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 wSetEntry *p;.
113a4 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77 if( iBatch!=pRow
113a5 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 Set->iBatch ){.
113a6 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e if( pRowSet->
113a7 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20 pEntry ){.
113a8 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f rowSetToList(pRo
113a9 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f wSet);. pRo
113aa 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f wSet->pTree = ro
113ab 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70 wSetListToTree(p
113ac 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b RowSet->pEntry);
113ad 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e . pRowSet->
113ae 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 pEntry = 0;.
113af 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 pRowSet->pLast
113b0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
113b1 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20 pRowSet->iBatch
113b2 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 = iBatch;. }.
113b3 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 p = pRowSet->pTr
113b4 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 ee;. while( p )
113b5 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 {. if( p->v<i
113b6 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 Rowid ){. p
113b7 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
113b8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 }else if( p->v
113b9 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 >iRowid ){.
113ba 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
113bb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
113bc 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
113bd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
113be 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
113bf 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 ** End of rowset
113c0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
113c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c3 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
113c4 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 ** Begin file pa
113c5 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ger.c **********
113c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c8 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
113c9 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
113ca 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
113cb 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
113cc 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
113cd 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
113ce 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
113cf 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
113d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
113d1 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
113d2 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
113d3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
113d4 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
113d5 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
113d6 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
113d7 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
113d8 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
113d9 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
113da 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
113db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113df 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
113e0 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
113e1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 ntation of the p
113e2 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 age cache subsys
113e3 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a tem or "pager"..
113e4 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 ** .** The pager
113e5 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 is used to acce
113e6 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69 ss a database di
113e7 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 sk file. It imp
113e8 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 lements.** atomi
113e9 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c c commit and rol
113ea 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 lback through th
113eb 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e e use of a journ
113ec 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 al file that.**
113ed 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d is separate from
113ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
113ef 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 le. The pager a
113f0 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 lso implements f
113f1 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 ile.** locking t
113f2 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 o prevent two pr
113f3 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 ocesses from wri
113f4 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 ting the same da
113f5 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 tabase.** file s
113f6 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f imultaneously, o
113f7 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 r one process fr
113f8 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 om reading the d
113f9 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a atabase while.**
113fa 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 another is writ
113fb 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ing..*/.#ifndef
113fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
113fd 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 IO../*.** Macros
113fe 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f for troubleshoo
113ff 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 ting. Normally
11400 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 turned off.*/.#i
11401 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 f 0.int sqlite3P
11402 61 67 65 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a agerTrace=1; /*
11403 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 True to enable
11404 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69 tracing */.#defi
11405 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ne sqlite3DebugP
11406 72 69 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65 rintf printf.#de
11407 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 28 fine PAGERTRACE(
11408 58 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 X) if( sqlit
11409 65 33 50 61 67 65 72 54 72 61 63 65 20 29 7b 20 e3PagerTrace ){
1140a 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
1140b 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64 tf X; }.#else.#d
1140c 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 efine PAGERTRACE
1140d 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (X).#endif../*.*
1140e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1140f 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75 two macros are u
11410 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50 sed within the P
11411 41 47 45 52 54 52 41 43 45 28 29 20 6d 61 63 72 AGERTRACE() macr
11412 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70 os above.** to p
11413 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 65 rint out file-de
11414 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a scriptors. .**.*
11415 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b 65 * PAGERID() take
11416 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
11417 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 73 Pager struct as
11418 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20 54 its argument. T
11419 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 he.** associated
1141a 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 file-descriptor
1141b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46 49 is returned. FI
1141c 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61 6b LEHANDLEID() tak
1141d 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 es an sqlite3_fi
1141e 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73 20 le.** struct as
1141f 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f its argument..*/
11420 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49 44 .#define PAGERID
11421 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 64 (p) ((int)(p->fd
11422 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 48 )).#define FILEH
11423 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 6e ANDLEID(fd) ((in
11424 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 t)fd)../*.** The
11425 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20 61 page cache as a
11426 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79 73 whole is always
11427 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 in one of the f
11428 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74 ollowing.** stat
11429 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 es:.**.** PAGE
1142a 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 R_UNLOCK
1142b 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
1142c 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
1142d 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20 reading or .**
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1142f 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68 65 writing the
11430 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
11431 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 There is no.**
11432 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11433 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64 20 data held
11434 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 in memory. This
11435 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c 0a is the initial.
11436 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
11437 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e 0a state..
11438 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 48 **.** PAGER_SH
11439 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65 20 ARED The
1143a 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72 65 page cache is re
1143b 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
1143c 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 se..**
1143d 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72 69 Wri
1143e 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72 6d ting is not perm
1143f 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63 61 itted. There ca
11440 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 n be.**
11441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 75 mu
11442 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 ltiple readers a
11443 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
11444 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
11445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11446 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65 20 file at the
11447 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a same time..**.**
11448 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 PAGER_RESERVE
11449 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f 63 D This proc
1144a 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65 64 ess has reserved
1144b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f the database fo
1144c 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20 r writing.**
1144d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1144e 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 but has not y
1144f 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61 6e et made any chan
11450 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70 ges. Only one p
11451 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 rocess.**
11452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11453 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72 65 at a time can re
11454 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62 61 serve the databa
11455 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 se. The origina
11456 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 l.**
11457 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 datab
11458 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 ase file has not
11459 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 been modified s
1145a 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 o other.**
1145b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1145c 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 73 processes may s
1145d 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 till be reading
1145e 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20 the on-disk.**
1145f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11460 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 database fi
11461 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 le..**.** PAGE
11462 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 R_EXCLUSIVE
11463 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
11464 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 s writing the da
11465 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 tabase..**
11466 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11467 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c 75 Access is exclu
11468 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20 sive. No other
11469 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20 processes or.**
1146a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1146b 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63 61 threads ca
1146c 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72 20 n be reading or
1146d 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e writing while on
1146e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
1146f 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 proce
11470 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a ss is writing..*
11471 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59 4e *.** PAGER_SYN
11472 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20 70 CED The p
11473 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 ager moves to th
11474 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50 41 is state from PA
11475 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a GER_EXCLUSIVE.**
11476 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11477 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c 6c after all
11478 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61 76 dirty pages hav
11479 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 e been written t
1147a 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 o the.**
1147b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
1147c 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1147d 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62 65 the file has be
1147e 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20 en synced to.**
1147f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11480 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20 disk. All
11481 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 that remains to
11482 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65 20 do is to remove
11483 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 or.**
11484 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75 6e trun
11485 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
11486 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74 72 file and the tr
11487 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 ansaction .**
11488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11489 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d will be comm
1148a 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 itted..**.** The
1148b 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d 65 page cache come
1148c 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55 4e s up in PAGER_UN
1148d 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 LOCK. The first
1148e 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74 time a.** sqlit
1148f 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63 63 e3PagerGet() occ
11490 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20 74 urs, the state t
11491 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 ransitions to PA
11492 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41 GER_SHARED..** A
11493 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20 68 fter all pages h
11494 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73 65 ave been release
11495 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f 70 d using sqlite_p
11496 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20 age_unref(),.**
11497 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 the state transi
11498 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50 41 tions back to PA
11499 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 GER_UNLOCK. The
1149a 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 first time.** t
1149b 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 hat sqlite3Pager
1149c 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 Write() is calle
1149d 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61 d, the state tra
1149e 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50 nsitions to.** P
1149f 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20 AGER_RESERVED.
114a0 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69 74 (Note that sqlit
114a1 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 63 e3PagerWrite() c
114a2 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61 an only be.** ca
114a3 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74 lled on an outst
114a4 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69 63 anding page whic
114a5 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 h means that the
114a6 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62 pager must.** b
114a7 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 e in PAGER_SHARE
114a8 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61 6e D before it tran
114a9 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 sitions to PAGER
114aa 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50 _RESERVED.).** P
114ab 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d 65 AGER_RESERVED me
114ac 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69 ans that there i
114ad 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61 s an open rollba
114ae 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 ck journal..** T
114af 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f he transition to
114b0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
114b1 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 61 occurs before a
114b2 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 ny changes.** ar
114b3 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61 e made to the da
114b4 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 6f tabase file, tho
114b5 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74 68 ugh writes to th
114b6 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f e rollback.** jo
114b7 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69 74 urnal occurs wit
114b8 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45 53 h just PAGER_RES
114b9 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61 6e ERVED. After an
114ba 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
114bb 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71 lback().** or sq
114bc 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
114bd 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65 20 PhaseTwo(), the
114be 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61 63 state can go bac
114bf 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 k to PAGER_SHARE
114c0 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20 D,.** or it can
114c1 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45 58 stay at PAGER_EX
114c2 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61 72 CLUSIVE if we ar
114c3 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 e in exclusive a
114c4 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 ccess mode..*/.#
114c5 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c define PAGER_UNL
114c6 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66 69 OCK 0.#defi
114c7 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 ne PAGER_SHARED
114c8 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d 65 1 /* same
114c9 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 as SHARED_LOCK
114ca 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
114cb 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20 20 _RESERVED 2
114cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53 45 /* same as RESE
114cd 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 RVED_LOCK */.#de
114ce 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c 55 fine PAGER_EXCLU
114cf 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73 61 SIVE 4 /* sa
114d0 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45 5f me as EXCLUSIVE_
114d1 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LOCK */.#define
114d2 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 PAGER_SYNCED
114d3 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 5../*.** A mac
114d4 72 6f 20 75 73 65 64 20 66 6f 72 20 69 6e 76 6f ro used for invo
114d5 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 63 20 69 king the codec i
114d6 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a f there is one.*
114d7 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
114d8 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 65 66 69 HAS_CODEC.# defi
114d9 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c ne CODEC1(P,D,N,
114da 58 2c 45 29 20 5c 0a 20 20 20 20 69 66 28 20 50 X,E) \. if( P
114db 2d 3e 78 43 6f 64 65 63 20 26 26 20 50 2d 3e 78 ->xCodec && P->x
114dc 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c Codec(P->pCodec,
114dd 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20 D,N,X)==0 ){ E;
114de 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 }.# define CODEC
114df 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 2(P,D,N,X,E,O) \
114e0 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 . if( P->xCod
114e1 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72 ec==0 ){ O=(char
114e2 2a 29 44 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20 *)D; }else \.
114e3 20 69 66 28 20 28 4f 3d 28 63 68 61 72 2a 29 28 if( (O=(char*)(
114e4 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f P->xCodec(P->pCo
114e5 64 65 63 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20 dec,D,N,X)))==0
114e6 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 ){ E; }.#else.#
114e7 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
114e8 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f D,N,X,E) /* NO
114e9 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 -OP */.# define
114ea 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 CODEC2(P,D,N,X,E
114eb 2c 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23 ,O) O=(char*)D.#
114ec 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
114ed 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 maximum allowed
114ee 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 36 34 sector size. 64
114ef 4b 69 42 2e 20 49 66 20 74 68 65 20 78 53 65 63 KiB. If the xSec
114f0 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 torsize() method
114f1 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 .** returns a v
114f2 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e alue larger than
114f3 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f this, then MAX_
114f4 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75 SECTOR_SIZE is u
114f5 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 sed instead..**
114f6 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65 This could conce
114f7 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72 ivably cause cor
114f8 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e ruption followin
114f9 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 g a power failur
114fa 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73 e on.** such a s
114fb 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63 ystem. This is c
114fc 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f urrently an undo
114fd 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a cumented limit..
114fe 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53 */.#define MAX_S
114ff 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 31 30 30 ECTOR_SIZE 0x100
11500 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 00../*.** An ins
11501 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
11502 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
11503 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
11504 72 20 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a r each active.**
11505 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 savepoint and s
11506 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
11507 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 tion in the syst
11508 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 em. All such str
11509 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 uctures.** are s
1150a 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 tored in the Pag
1150b 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 er.aSavepoint[]
1150c 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 array, which is
1150d 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a allocated and.**
1150e 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 resized using s
1150f 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e qlite3Realloc().
11510 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 .**.** When a sa
11511 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 vepoint is creat
11512 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 ed, the PagerSav
11513 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
11514 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 t field is.** se
11515 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 t to 0. If a jou
11516 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 rnal-header is w
11517 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
11518 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 main journal whi
11519 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f le.** the savepo
1151a 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 int is active, t
1151b 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 hen iHdrOffset i
1151c 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 s set to the byt
1151d 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d e offset .** imm
1151e 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
1151f 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 ng the last jour
11520 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 nal record writt
11521 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e en into the main
11522 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f .** journal befo
11523 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 re the journal-h
11524 65 61 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 eader. This is r
11525 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 equired during s
11526 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c avepoint.** roll
11527 62 61 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 back (see pagerP
11528 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
11529 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ())..*/.typedef
1152a 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 struct PagerSave
1152b 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 point PagerSavep
1152c 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 oint;.struct Pag
1152d 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 erSavepoint {.
1152e 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 i64 iOffset;
1152f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11530 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 Starting offset
11531 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 in main journal
11532 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
11533 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 set;
11534 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a /* See above *
11535 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 /. Bitvec *pInS
11536 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 avepoint;
11537 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 /* Set of pages
11538 20 69 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 in this savepoi
11539 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 nt */. Pgno nOr
1153a 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ig;
1153b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c /* Original
1153c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1153d 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 in file */. Pg
1153e 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 no iSubRec;
1153f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
11540 64 65 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 dex of first rec
11541 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e ord in sub-journ
11542 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 al */.};../*.**
11543 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 A open page cach
11544 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 e is an instance
11545 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
11546 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
11547 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a ** errCode.**.**
11548 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 Pager.errCode
11549 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 may be set to S
1154a 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c QLITE_IOERR, SQL
1154b 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a ITE_CORRUPT, or.
1154c 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 ** or SQLITE_F
1154d 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 ULL. Once one of
1154e 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 the first three
1154f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 errors occurs,
11550 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 it persists.**
11551 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 and is returned
11552 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f as the result o
11553 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 f every major pa
11554 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 ger API call. T
11555 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 he.** SQLITE_F
11556 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ULL return code
11557 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 is slightly diff
11558 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73 erent. It persis
11559 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 ts only until th
1155a 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63 e.** next succ
1155b 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 essful rollback
1155c 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 is performed on
1155d 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e the pager cache.
1155e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 Also,.** SQLI
1155f 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 TE_FULL does not
11560 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c 69 affect the sqli
11561 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e te3PagerGet() an
11562 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f d sqlite3PagerLo
11563 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 okup().** APIs
11564 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c , they may still
11565 20 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73 be used success
11566 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 fully..**.** dbS
11567 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 izeValid, dbSize
11568 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 , dbOrigSize, db
11569 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 FileSize.**.**
1156a 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 Managing the si
1156b 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
1156c 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 se file in pages
1156d 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d is a little com
1156e 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 plicated..** T
1156f 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 he variable Page
11570 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e r.dbSize contain
11571 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
11572 70 61 67 65 73 20 74 68 61 74 20 74 68 65 20 64 pages that the d
11573 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 atabase.** ima
11574 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e ge currently con
11575 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61 tains. As the da
11576 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f tabase image gro
11577 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 ws or shrinks th
11578 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 is.** variable
11579 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 68 65 is updated. The
1157a 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e variable Pager.
1157b 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 dbFileSize conta
1157c 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a ins the number.*
1157d 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20 * of pages in
1157e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1157f 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 20 64 e. This may be d
11580 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 ifferent from Pa
11581 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 ger.dbSize.**
11582 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61 if some pages ha
11583 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64 ve been appended
11584 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
11585 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79 image but not y
11586 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 et written.**
11587 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63 out from the cac
11588 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c he to the actual
11589 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f file on disk. O
1158a 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 20 68 r if the image h
1158b 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 as been.** tru
1158c 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63 ncated by an inc
1158d 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 remental-vacuum
1158e 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 operation. The P
1158f 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 ager.dbOrigSize
11590 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f variable.** co
11591 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 ntains the numbe
11592 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
11593 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
11594 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e when the curren
11595 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 t.** transacti
11596 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 on was opened. T
11597 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 he contents of a
11598 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 65 73 ll three of thes
11599 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a e variables is.*
1159a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 * only guarant
1159b 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63 eed to be correc
1159c 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e t if the boolean
1159d 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c Pager.dbSizeVal
1159e 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a id is true..**.*
1159f 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 * TODO: Under
115a0 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 what conditions
115a1 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73 is dbSizeValid s
115a2 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a et? Cleared?.**.
115a3 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f ** changeCountDo
115a4 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 ne.**.** This
115a5 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 boolean variable
115a6 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 is used to make
115a7 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63 sure that the c
115a8 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a hange-counter .*
115a9 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 65 20 * (the 4-byte
115aa 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 74 20 header field at
115ab 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f byte offset 24 o
115ac 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
115ad 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f ile) is .** no
115ae 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f t updated more o
115af 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 ften than necess
115b0 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 ary. .**.** It
115b1 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 is set to true
115b2 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d when the change-
115b3 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73 counter field is
115b4 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 68 20 updated, which
115b5 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 .** can only h
115b6 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c appen if an excl
115b7 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 usive lock is he
115b8 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ld on the databa
115b9 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 se file..** It
115ba 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 65 74 is cleared (set
115bb 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65 to false) whene
115bc 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ver an exclusive
115bd 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 lock is .** r
115be 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 elinquished on t
115bf 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
115c0 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 74 72 . Each time a tr
115c1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
115c2 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 mitted,.** The
115c3 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
115c4 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74 flag is inspect
115c5 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 72 75 ed. If it is tru
115c6 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a e, the work of.*
115c7 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 68 65 * updating the
115c8 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
115c9 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 is omitted for t
115ca 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
115cb 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 action..**.**
115cc 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d This mechanism m
115cd 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72 eans that when r
115ce 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
115cf 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e ive mode, a conn
115d0 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 ection .** nee
115d1 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68 d only update th
115d2 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
115d3 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66 once, for the f
115d4 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e irst transaction
115d5 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e .** committed.
115d6 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 .**.** dbModifie
115d7 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 d.**.** The db
115d8 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 Modified flag is
115d9 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 61 20 set whenever a
115da 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
115db 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 dirtied..** I
115dc 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 74 20 t is cleared at
115dd 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 the end of each
115de 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
115df 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 64 20 ** It is used
115e0 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 when committing
115e1 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64 or otherwise end
115e2 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f ing a transactio
115e3 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 n. If.** the d
115e4 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 bModified flag i
115e5 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 s clear then les
115e6 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 s work has to be
115e7 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 done..**.** jou
115e8 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a rnalStarted.**.*
115e9 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 * This flag is
115ea 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 74 68 set whenever th
115eb 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e e the main journ
115ec 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a al is synced. .*
115ed 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 *.** The point
115ee 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73 of this flag is
115ef 20 74 68 61 74 20 69 74 20 6d 75 73 74 20 62 65 that it must be
115f0 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 0a set after the .
115f1 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e ** first journ
115f2 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a al header in a j
115f3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
115f4 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 been synced to d
115f5 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 isk..** After
115f6 74 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 this has happene
115f7 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61 70 70 d, new pages app
115f8 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 ended to the dat
115f9 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e abase .** do n
115fa 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47 48 44 ot need the PGHD
115fb 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
115fc 20 73 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f set, as they do
115fd 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 not need.** t
115fe 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 o wait for a jou
115ff 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 rnal sync before
11600 20 74 68 65 79 20 63 61 6e 20 62 65 20 77 72 69 they can be wri
11601 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 tten out to.**
11602 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11603 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e le (see function
11604 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e pager_write()).
11605 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 .** .** setMas
11606 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 ter.**.** This
11607 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
11608 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
11609 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1160a 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a nal file name.**
1160b 20 20 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f (if any) is o
1160c 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f nly written into
1160d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1160e 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 e once..**.**
1160f 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 When committing
11610 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 a transaction, t
11611 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
11612 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 l file name (if
11613 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 any).** may be
11614 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
11615 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
11616 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 hile the pager i
11617 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 s still in.**
11618 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 PAGER_RESERVED s
11619 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 tate (see Commit
1161a 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 PhaseOne() for t
1161b 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a he action). It.*
1161c 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 * then attempt
1161d 73 20 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20 s to upgrade to
1161e 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
1161f 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74 65 6d k. If this attem
11620 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 pt.** fails, t
11621 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 hen SQLITE_BUSY
11622 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 may be returned
11623 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e 64 20 to the user and
11624 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 the user.** ma
11625 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d y attempt to com
11626 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 mit the transact
11627 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20 ion again later
11628 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f (calling.** Co
11629 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61 mmitPhaseOne() a
1162a 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 gain). This flag
1162b 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 is used to ensu
1162c 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 re that the .**
1162d 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c master journal
1162e 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 name is only wr
1162f 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
11630 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69 rnal file the fi
11631 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f rst.** time Co
11632 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69 mmitPhaseOne() i
11633 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 s called..**.**
11634 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 doNotSync.**.**
11635 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 This variable
11636 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 is set and clear
11637 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 ed by sqlite3Pag
11638 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a erWrite()..**.**
11639 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 needSync.**.**
1163a 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 TODO: It might
1163b 20 62 65 20 65 61 73 69 65 72 20 74 6f 20 73 65 be easier to se
1163c 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 t this variable
1163d 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 in writeJournalH
1163e 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 dr().** and wr
1163f 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c iteMasterJournal
11640 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 () only. Change
11641 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 its meaning to "
11642 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a unsynced data.**
11643 20 20 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 has been writ
11644 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
11645 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 al"..**.** subjI
11646 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 nMemory.**.**
11647 54 68 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 This is a boolea
11648 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74 n variable. If t
11649 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 rue, then any re
1164a 71 75 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e quired sub-journ
1164b 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 al.** is opene
1164c 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 d as an in-memor
1164d 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 y journal file.
1164e 49 66 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 If false, then i
1164f 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 n-memory.** su
11650 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f b-journals are o
11651 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d nly used for in-
11652 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c memory pager fil
11653 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 es..*/.struct Pa
11654 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ger {. sqlite3_
11655 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 vfs *pVfs;
11656 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 /* OS functi
11657 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 ons to use for I
11658 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 O */. u8 exclus
11659 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 iveMode;
1165a 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 /* Boolean. T
1165b 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d rue if locking_m
1165c 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a ode==EXCLUSIVE *
1165d 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f /. u8 journalMo
1165e 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
1165f 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 /* On of the PAG
11660 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a ER_JOURNALMODE_*
11661 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
11662 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 useJournal;
11663 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 /* Use
11664 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
11665 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 al on this file
11666 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f */. u8 noReadlo
11667 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ck;
11668 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 /* Do not bothe
11669 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 r to obtain read
1166a 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f locks */. u8 no
1166b 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
1166c 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
1166d 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 sync the journa
1166e 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 l if true */. u
1166f 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 8 fullSync;
11670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f /* Do
11671 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 extra syncs of
11672 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 the journal for
11673 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 robustness */.
11674 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 u8 sync_flags;
11675 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
11676 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 ne of SYNC_NORMA
11677 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a L or SYNC_FULL *
11678 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b /. u8 tempFile;
11679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1167a 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 /* zFilename is
1167b 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
1167c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c */. u8 readOnl
1167d 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
1167e 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
1167f 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 read-only databa
11680 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 se */. u8 memDb
11681 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11682 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 /* True to i
11683 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 nhibit all file
11684 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 I/O */.. /* The
11685 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
11686 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 contains those
11687 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68 class members th
11688 61 74 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c at are dynamical
11689 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 ly. ** modified
1168a 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f during normal o
1168b 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f perations. The o
1168c 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 ther variables i
1168d 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
1168e 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72 . ** are either
1168f 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 constant throug
11690 68 6f 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d hout the lifetim
11691 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20 e of the pager,
11692 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 or else. ** use
11693 64 20 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 d to store confi
11694 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 guration paramet
11695 65 72 73 20 74 68 61 74 20 61 66 66 65 63 74 20 ers that affect
11696 74 68 65 20 77 61 79 20 74 68 65 20 70 61 67 65 the way the page
11697 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 r . ** operates
11698 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 .. **. ** The
11699 27 73 74 61 74 65 27 20 76 61 72 69 61 62 6c 65 'state' variable
1169a 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e is described in
1169b 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f more detail alo
1169c 6e 67 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a ng with the. **
1169d 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 descriptions of
1169e 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d the values it m
1169f 61 79 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f ay take - PAGER_
116a0 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 UNLOCK etc. Many
116a1 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 of the. ** oth
116a2 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 er variables in
116a3 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 this block are d
116a4 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 escribed in the
116a5 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 comment directly
116a6 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 . ** above thi
116a7 73 20 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69 s class definiti
116a8 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 on.. */. u8 st
116a9 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ate;
116aa 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f /* PAGER_
116ab 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c UNLOCK, _SHARED,
116ac 20 5f 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e _RESERVED, etc.
116ad 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 */. u8 dbModif
116ae 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 ied;
116af 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
116b0 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67 re are any chang
116b1 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a es to the Db */.
116b2 20 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 u8 needSync;
116b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116b4 20 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e True if an fsyn
116b5 63 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e c() is needed on
116b6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
116b7 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 u8 journalStar
116b8 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ted; /*
116b9 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72 20 True if header
116ba 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 of journal is sy
116bb 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 nced */. u8 cha
116bc 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 ngeCountDone;
116bd 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 /* Set aft
116be 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 er incrementing
116bf 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
116c0 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 er */. u8 setMa
116c1 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ster;
116c2 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
116c3 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 m-j name has be
116c4 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 en written to jr
116c5 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 nl */. u8 doNot
116c6 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
116c7 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 /* Boolean.
116c8 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e While true, do n
116c9 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 ot spill the cac
116ca 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a he */. u8 dbSiz
116cb 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20 eValid;
116cc 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 /* Set when
116cd 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63 dbSize is correc
116ce 74 20 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e t */. u8 subjIn
116cf 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20 Memory;
116d0 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 /* True to us
116d1 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d e in-memory sub-
116d2 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 journals */. Pg
116d3 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 no dbSize;
116d4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
116d5 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
116d6 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
116d7 20 20 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a Pgno dbOrigSiz
116d8 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
116d9 20 64 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74 dbSize before t
116da 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
116db 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f action */. Pgno
116dc 20 64 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 dbFileSize;
116dd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
116de 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
116df 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
116e0 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 */. int errCode
116e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
116e2 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 /* One of sever
116e3 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f al kinds of erro
116e4 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 rs */. int nRec
116e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
116e6 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 /* Pages jou
116e7 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 rnalled since la
116e8 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74 st j-header writ
116e9 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 ten */. u32 cks
116ea 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 umInit;
116eb 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 /* Quasi-ra
116ec 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64 ndom value added
116ed 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73 to every checks
116ee 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 um */. u32 nSub
116ef 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
116f0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
116f1 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e records written
116f2 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 to sub-journal
116f3 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e */. Bitvec *pIn
116f4 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
116f5 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 /* One bit for
116f6 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 each page in the
116f7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
116f8 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
116f9 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 *fd;
116fa 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
116fb 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 or for database
116fc 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
116fd 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 e *jfd;
116fe 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 /* File descrip
116ff 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 tor for main jou
11700 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rnal */. sqlite
11701 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 3_file *sjfd;
11702 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 /* File de
11703 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62 scriptor for sub
11704 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 -journal */. i6
11705 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 4 journalOff;
11706 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
11707 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65 rent write offse
11708 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c t in the journal
11709 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a file */. i64 j
1170a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 ournalHdr;
1170b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f /* Byte o
1170c 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75 ffset to previou
1170d 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 s journal header
1170e 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 */. PagerSavep
1170f 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 oint *aSavepoint
11710 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 ; /* Array of ac
11711 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
11712 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f */. int nSavepo
11713 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
11714 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c /* Number of el
11715 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 ements in aSavep
11716 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 oint[] */. char
11717 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b dbFileVers[16];
11718 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 /* Chang
11719 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 es whenever data
1171a 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
1171b 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f s */. u32 secto
1171c 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 rSize;
1171d 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 /* Assumed se
1171e 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 ctor size during
1171f 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 rollback */..
11720 75 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 u16 nExtra;
11721 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
11722 64 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 dd this many byt
11723 65 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 es to each in-me
11724 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 mory page */. i
11725 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 16 nReserve;
11726 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
11727 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 mber of unused b
11728 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65 ytes at end of e
11729 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 ach page */. u3
1172a 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 2 vfsFlags;
1172b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
1172c 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 gs for sqlite3_v
1172d 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 fs.xOpen() */.
1172e 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 int pageSize;
1172f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
11730 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
11731 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 n a page */. Pg
11732 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 no mxPgno;
11733 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
11734 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a imum allowed siz
11735 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
11736 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 e */. char *zFi
11737 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 lename;
11738 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
11739 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1173a 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 */. char *zJour
1173b 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
1173c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1173d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1173e 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e int (*xBusyHan
1173f 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a dler)(void*); /*
11740 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c Function to cal
11741 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 l when busy */.
11742 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 void *pBusyHand
11743 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 lerArg; /*
11744 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 Context argument
11745 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 for xBusyHandle
11746 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 r */.#ifdef SQLI
11747 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 TE_TEST. int nH
11748 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 it, nMiss;
11749 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 /* Cache h
1174a 69 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 its and missing
1174b 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 */. int nRead,
1174c 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 nWrite;
1174d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 /* Database pag
1174e 65 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 es read/written
1174f 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 */.#endif. void
11750 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 (*xReiniter)(Db
11751 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 Page*); /* Call
11752 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 this routine whe
11753 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 n reloading page
11754 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 s */.#ifdef SQLI
11755 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 TE_HAS_CODEC. v
11756 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 oid *(*xCodec)(v
11757 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c oid*,void*,Pgno,
11758 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 int); /* Routine
11759 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 for en/decoding
1175a 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 data */. void
1175b 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 (*xCodecSizeChng
1175c 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 )(void*,int,int)
1175d 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 ; /* Notify of p
1175e 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73 age size changes
1175f 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f */. void (*xCo
11760 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b decFree)(void*);
11761 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11762 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 Destructor for t
11763 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f he codec */. vo
11764 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 id *pCodec;
11765 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
11766 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 st argument to x
11767 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 Codec... methods
11768 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 */.#endif. cha
11769 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 r *pTmpSpace;
1176a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1176b 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 r.pageSize bytes
1176c 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
1176d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a p use */. i64 j
1176e 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b ournalSizeLimit;
1176f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c /* Size l
11770 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 74 imit for persist
11771 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ent journal file
11772 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
11773 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 PCache;
11774 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
11775 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a 65 page cache obje
11776 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ct */. sqlite3_
11777 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b backup *pBackup;
11778 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
11779 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e o list of ongoin
1177a 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 g backup process
1177b 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 es */.};../*.**
1177c 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c The following gl
1177d 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68 obal variables h
1177e 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65 old counters use
1177f 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 d for.** testing
11780 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 purposes only.
11781 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 These variables
11782 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e do not exist in
11783 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e .** a non-testin
11784 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20 g build. These
11785 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f variables are no
11786 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a t thread-safe..*
11787 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
11788 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
11789 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 int sqlite3_page
1178a 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d r_readdb_count =
1178b 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
1178c 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72 of full pages r
1178d 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 ead from DB */.S
1178e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1178f 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 lite3_pager_writ
11790 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 edb_count = 0;
11791 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 /* Number of fu
11792 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 6e ll pages written
11793 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 to DB */.SQLITE
11794 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
11795 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f _pager_writej_co
11796 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e unt = 0; /* N
11797 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 77 umber of pages w
11798 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 ritten to journa
11799 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 l */.# define PA
1179a 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b GER_INCR(v) v++
1179b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
1179c 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 PAGER_INCR(v).#e
1179d 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f ndif..../*.** Jo
1179e 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69 urnal files begi
1179f 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f n with the follo
117a0 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e wing magic strin
117a1 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 g. The data.**
117a2 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f was obtained fro
117a3 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 m /dev/random.
117a4 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 It is used only
117a5 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 as a sanity chec
117a6 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 k..**.** Since v
117a7 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 ersion 2.8.0, th
117a8 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 e journal format
117a9 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 contains additi
117aa 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 onal sanity.** c
117ab 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 hecking informat
117ac 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 ion. If the pow
117ad 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 er fails while t
117ae 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 he journal is be
117af 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 ing.** written,
117b0 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 semi-random garb
117b1 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 61 age data might a
117b2 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 ppear in the jou
117b3 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 rnal.** file aft
117b4 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 er power is rest
117b5 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74 ored. If an att
117b6 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64 empt is then mad
117b7 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 e.** to roll the
117b8 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 journal back, t
117b9 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c he database coul
117ba 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 d be corrupted.
117bb 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a The additional.
117bc 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 ** sanity checki
117bd 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 74 ng data is an at
117be 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65 tempt to discove
117bf 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 6e r the garbage in
117c0 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
117c1 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a and ignore it..*
117c2 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20 *.** The sanity
117c3 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 checking informa
117c4 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 tion for the new
117c5 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
117c6 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 consists.** of a
117c7 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 32-bit checksum
117c8 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 on each page of
117c9 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 63 data. The chec
117ca 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 ksum covers both
117cb 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d .** the page num
117cc 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 67 ber and the pPag
117cd 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 er->pageSize byt
117ce 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74 es of data for t
117cf 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 he page..** This
117d0 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61 cksum is initia
117d1 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69 lized to a 32-bi
117d2 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 t random value t
117d3 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 74 hat appears in t
117d4 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
117d5 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 74 le right after t
117d6 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 he header. The
117d7 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a random initializ
117d8 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c er is important,
117d9 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62 .** because garb
117da 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 70 age data that ap
117db 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 pears at the end
117dc 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 of a journal is
117dd 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 likely.** data
117de 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e that was once in
117df 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 other files tha
117e0 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 t have now been
117e1 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65 deleted. If the
117e2 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 61 .** garbage data
117e3 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 came from an ob
117e4 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 solete journal f
117e5 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75 ile, the checksu
117e6 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 ms might.** be c
117e7 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 orrect. But by
117e8 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
117e9 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e checksum to ran
117ea 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a dom value which.
117eb 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 ** is different
117ec 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 for every journa
117ed 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 l, we minimize t
117ee 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 hat risk..*/.sta
117ef 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
117f0 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c ed char aJournal
117f1 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 Magic[] = {. 0x
117f2 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 d9, 0xd5, 0x05,
117f3 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31 0xf9, 0x20, 0xa1
117f4 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b , 0x63, 0xd7,.};
117f5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ../*.** The size
117f6 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 20 of the of each
117f7 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74 page record in t
117f8 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 he journal is gi
117f9 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f ven by.** the fo
117fa 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a llowing macro..*
117fb 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 /.#define JOURNA
117fc 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 L_PG_SZ(pPager)
117fd 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ((pPager->pageS
117fe 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a ize) + 8)../*.**
117ff 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 The journal hea
11800 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 der size for thi
11801 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 73 s pager. This is
11802 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d usually the sam
11803 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 e .** size as a
11804 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 single disk sect
11805 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74 or. See also set
11806 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f SectorSize()..*/
11807 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c .#define JOURNAL
11808 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
11809 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 (pPager->sectorS
1180a 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ize)../*.** The
1180b 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 macro MEMDB is t
1180c 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65 rue if we are de
1180d 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e aling with an in
1180e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1180f 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20 ..** We do this
11810 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 as a macro so th
11811 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 at if the SQLITE
11812 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d _OMIT_MEMORYDB m
11813 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 acro is set,.**
11814 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d the value of MEM
11815 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e DB will be a con
11816 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f stant and the co
11817 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 mpiler will opti
11818 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 mize.** out code
11819 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 that would neve
1181a 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 r execute..*/.#i
1181b 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1181c 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 _MEMORYDB.# defi
1181d 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 ne MEMDB 0.#else
1181e 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 .# define MEMDB
1181f 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 pPager->memDb.#e
11820 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
11821 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 maximum legal pa
11822 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e ge number is (2^
11823 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 31 - 1)..*/.#def
11824 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 ine PAGER_MAX_PG
11825 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 23 NO 2147483647..#
11826 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a 2f ifndef NDEBUG ./
11827 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a *.** Usage:.**.*
11828 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65 * assert( asse
11829 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 rt_pager_state(p
1182a 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61 Pager) );.*/.sta
1182b 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f 70 tic int assert_p
1182c 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 72 ager_state(Pager
1182d 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a *pPager){.. /*
1182e 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 20 A temp-file is
1182f 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 5f always in PAGER_
11830 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 47 EXCLUSIVE or PAG
11831 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2e ER_SYNCED state.
11832 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
11833 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d ager->tempFile==
11834 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 0 || pPager->sta
11835 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
11836 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 IVE );.. /* The
11837 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
11838 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 flag is always
11839 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c set for temp-fil
1183a 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
1183b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
1183c 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63 ==0 || pPager->c
1183d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29 hangeCountDone )
1183e 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d ;.. return 1;.}
1183f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
11840 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 eturn true if it
11841 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f is necessary to
11842 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 67 write page *pPg
11843 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f into the sub-jo
11844 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65 urnal..** A page
11845 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69 needs to be wri
11846 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 75 tten into the su
11847 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 b-journal if the
11848 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a re exists one.**
11849 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61 or more open sa
1184a 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 69 vepoints for whi
1184b 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 ch:.**.** * Th
1184c 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
1184d 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
1184e 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 65 ual to PagerSave
1184f 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 point.nOrig, and
11850 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20 .** * The bit
11851 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
11852 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 the page-number
11853 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a is not set in.*
11854 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 * PagerSavep
11855 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
11856 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
11857 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
11858 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
11859 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 Pgno pgno = pPg
1185a 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 ->pgno;. Pager
1185b 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
1185c 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a Pager;. int i;.
1185d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
1185e 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
1185f 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 i++){. Pager
11860 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 Savepoint *p = &
11861 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
11862 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 nt[i];. if( p
11863 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26 ->nOrig>=pgno &&
11864 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 65 0==sqlite3Bitve
11865 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 65 cTest(p->pInSave
11866 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a point, pgno) ){.
11867 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
11868 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
11869 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 0;.}../*.** R
1186a 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
1186b 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 e page is alread
1186c 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c y in the journal
1186d 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1186e 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e int pageInJourn
1186f 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a al(PgHdr *pPg){.
11870 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
11871 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3e BitvecTest(pPg->
11872 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
11873 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
11874 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 }../*.** Read a
11875 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 66 32-bit integer f
11876 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 69 rom the given fi
11877 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
11878 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65 Store the intege
11879 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 61 r.** that is rea
1187a 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 74 d in *pRes. Ret
1187b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
1187c 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
1187d 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 ed, or an.** err
1187e 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 or code is somet
1187f 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
11880 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 .**.** All value
11881 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 s are stored on
11882 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 69 disk as big-endi
11883 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e an..*/.static in
11884 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 6c t read32bits(sql
11885 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 ite3_file *fd, i
11886 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 2a 64 offset, u32 *
11887 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 pRes){. unsigne
11888 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 d char ac[4];.
11889 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
1188a 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73 OsRead(fd, ac, s
1188b 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 izeof(ac), offse
1188c 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
1188d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a LITE_OK ){. *
1188e 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65 pRes = sqlite3Ge
1188f 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a t4byte(ac);. }.
11890 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11891 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 /*.** Write a 32
11892 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 -bit integer int
11893 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 o a string buffe
11894 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20 r in big-endian
11895 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 byte order..*/.#
11896 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 73 define put32bits
11897 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 75 (A,B) sqlite3Pu
11898 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 29 t4byte((u8*)A,B)
11899 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ../*.** Write a
1189a 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 32-bit integer i
1189b 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 69 nto the given fi
1189c 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
1189d 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1189e 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 6f .** on success o
1189f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 r an error code
118a0 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 is something goe
118a1 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 s wrong..*/.stat
118a2 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 69 ic int write32bi
118a3 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ts(sqlite3_file
118a4 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c *fd, i64 offset,
118a5 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 61 u32 val){. cha
118a6 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32 r ac[4];. put32
118a7 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20 bits(ac, val);.
118a8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f return sqlite3O
118a9 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 34 sWrite(fd, ac, 4
118aa 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a , offset);.}../*
118ab 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 .** The argument
118ac 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 to this macro i
118ad 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 s a file descrip
118ae 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65 tor (type sqlite
118af 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 3_file*)..** Ret
118b0 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e urn 0 if it is n
118b1 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d ot open, or non-
118b2 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 zero (but not 1)
118b3 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a if it is..**.**
118b4 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 This is so that
118b5 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e expressions can
118b6 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a be written as:.
118b7 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 **.** if( isOp
118b8 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
118b9 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 ){ ....**.** ins
118ba 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 tead of.**.**
118bb 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d if( pPager->jfd-
118bc 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e >pMethods ){ ...
118bd 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 .*/.#define isOp
118be 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e en(pFd) ((pFd)->
118bf 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a pMethods)../*.**
118c0 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20 If file pFd is
118c1 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 open, call sqlit
118c2 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 e3OsUnlock() on
118c3 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e it..*/.static in
118c4 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 t osUnlock(sqlit
118c5 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e e3_file *pFd, in
118c6 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 t eLock){. if(
118c7 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a !isOpen(pFd) ){.
118c8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
118c9 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 E_OK;. }. retu
118ca 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f rn sqlite3OsUnlo
118cb 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a ck(pFd, eLock);.
118cc 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
118cd 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 nction determine
118ce 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
118cf 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 the atomic-writ
118d0 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a e optimization.*
118d1 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 * can be used wi
118d2 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 th this pager. T
118d3 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
118d4 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a can be used if:.
118d5 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76 **.** (a) the v
118d6 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
118d7 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 OsDeviceCharact
118d8 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63 eristics() indic
118d9 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 ates that.**
118da 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 a database pag
118db 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e e may be written
118dc 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64 atomically, and
118dd 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c .** (b) the val
118de 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f ue returned by O
118df 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73 sSectorSize() is
118e0 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
118e1 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 ual.** to t
118e2 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a he page size..**
118e3 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 .** The optimiza
118e4 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77 tion is also alw
118e5 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 20 ays enabled for
118e6 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e temporary files.
118e7 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72 It is.** an err
118e8 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 or to call this
118e9 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 67 function if pPag
118ea 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 er is opened on
118eb 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 an in-memory.**
118ec 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
118ed 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 If the optimizat
118ee 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 ion cannot be us
118ef 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 ed, 0 is returne
118f0 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20 d. If it can be
118f1 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 used,.** then th
118f2 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
118f3 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 is the size of
118f4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
118f5 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 when it.** cont
118f6 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 ains rollback da
118f7 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f ta for exactly o
118f8 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 ne page..*/.#ifd
118f9 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
118fa 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 _ATOMIC_WRITE.st
118fb 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 atic int jrnlBuf
118fc 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 ferSize(Pager *p
118fd 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 Pager){. assert
118fe 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 ( !MEMDB );. if
118ff 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
11900 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 64 ile ){. int d
11901 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11902 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11903 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 Device character
11904 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e istics */. in
11905 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 20 t nSector;
11906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11907 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 2a /* Sector size *
11908 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65 /. int szPage
11909 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1190a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
1190b 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73 size */.. ass
1190c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
1190d 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 64 er->fd) );. d
1190e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 c = sqlite3OsDev
1190f 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
11910 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a cs(pPager->fd);.
11911 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 50 nSector = pP
11912 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
11913 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 70 ;. szPage = p
11914 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
11915 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c .. assert(SQL
11916 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
11917 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 512==(512>>8));.
11918 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 assert(SQLIT
11919 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 E_IOCAP_ATOMIC64
1191a 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a K==(65536>>8));.
1191b 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 28 if( 0==(dc&(
1191c 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
1191d 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29 MIC|(szPage>>8))
1191e 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61 || nSector>szPa
1191f 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ge) ){. ret
11920 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
11921 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e .. return JOURN
11922 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11923 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 ) + JOURNAL_PG_S
11924 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e Z(pPager);.}.#en
11925 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 dif../*.** If SQ
11926 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
11927 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e is defined then
11928 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69 we do some sani
11929 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f ty checking.** o
1192a 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e n the cache usin
1192b 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 6f g a hash functio
1192c 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 n. This is used
1192d 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 for testing.**
1192e 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e and debugging on
1192f 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ly..*/.#ifdef SQ
11930 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
11931 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
11932 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 74 32-bit hash of t
11933 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f 72 he page data for
11934 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 pPage..*/.stati
11935 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 61 c u32 pager_data
11936 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c 20 hash(int nByte,
11937 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
11938 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 73 Data){. u32 has
11939 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a h = 0;. int i;.
1193a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79 for(i=0; i<nBy
1193b 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61 te; i++){. ha
1193c 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 29 sh = (hash*1039)
1193d 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d + pData[i];. }
1193e 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b 0a . return hash;.
1193f 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 }.static u32 pag
11940 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 64 er_pagehash(PgHd
11941 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 r *pPage){. ret
11942 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 61 urn pager_dataha
11943 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 72 sh(pPage->pPager
11944 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e 73 ->pageSize, (uns
11945 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 61 igned char *)pPa
11946 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74 ge->pData);.}.st
11947 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
11948 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 48 set_pagehash(PgH
11949 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 50 dr *pPage){. pP
1194a 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 age->pageHash =
1194b 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
1194c 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Page);.}../*.**
1194d 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 6d The CHECK_PAGE m
1194e 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 48 acro takes a PgH
1194f 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d 65 dr* as an argume
11950 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 48 nt. If SQLITE_CH
11951 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20 ECK_PAGES.** is
11952 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45 defined, and NDE
11953 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e BUG is not defin
11954 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20 ed, an assert()
11955 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b 73 statement checks
11956 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 67 .** that the pag
11957 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 74 e is either dirt
11958 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 68 y or still match
11959 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 65 es the calculate
1195a 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a d page-hash..*/.
1195b 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 #define CHECK_PA
1195c 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 28 GE(x) checkPage(
1195d 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 x).static void c
1195e 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 2a heckPage(PgHdr *
1195f 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
11960 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
11961 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ger;. assert( !
11962 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c pPg->pageHash ||
11963 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11964 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e . || (pPg->
11965 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
11966 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48 Y) || pPg->pageH
11967 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 68 ash==pager_pageh
11968 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23 ash(pPg) );.}..#
11969 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 67 else.#define pag
1196a 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29 er_datahash(X,Y)
1196b 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65 0.#define page
1196c 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 30 r_pagehash(X) 0
1196d 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 .#define CHECK_P
1196e 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f AGE(x).#endif /
1196f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 * SQLITE_CHECK_P
11970 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 AGES */../*.** W
11971 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c hen this is call
11972 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ed the journal f
11973 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 ile for pager pP
11974 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65 ager must be ope
11975 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 n..** This funct
11976 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ion attempts to
11977 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f read a master jo
11978 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
11979 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 from the .** end
1197a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 of the file and
1197b 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c , if successful,
1197c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20 copies it into
1197d 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20 memory supplied
1197e 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65 .** by the calle
1197f 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 r. See comments
11980 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65 above writeMaste
11981 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 rJournal() for t
11982 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 he format.** use
11983 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 d to store a mas
11984 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
11985 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64 name at the end
11986 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
11987 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 le..**.** zMaste
11988 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 r must point to
11989 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c a buffer of at l
1198a 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 east nMaster byt
1198b 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
1198c 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 ** the caller. T
1198d 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 his should be sq
1198e 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 lite3_vfs.mxPath
1198f 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 name+1 (to ensur
11990 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e e there is.** en
11991 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 ough space to wr
11992 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
11993 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 ournal name). If
11994 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11995 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 nal.** name in t
11996 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f he journal is lo
11997 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 nger than nMaste
11998 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 r bytes (includi
11999 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d ng a.** nul-term
1199a 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 inator), then th
1199b 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 is is handled as
1199c 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f if no master jo
1199d 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 urnal name.** we
1199e 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 re present in th
1199f 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a e journal..**.**
119a0 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 If a master jou
119a1 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 rnal file name i
119a2 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65 s present at the
119a3 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 end of the jour
119a4 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 nal.** file, the
119a5 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 n it is copied i
119a6 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 nto the buffer p
119a7 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 ointed to by zMa
119a8 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 ster. A.** nul-t
119a9 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69 erminator byte i
119aa 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 s appended to th
119ab 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 e buffer followi
119ac 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a ng the master.**
119ad 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
119ae 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 me..**.** If it
119af 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 is determined th
119b0 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 at no master jou
119b1 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 rnal file name i
119b2 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d s present .** zM
119b3 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20 aster[0] is set
119b4 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f to 0 and SQLITE_
119b5 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a OK returned..**.
119b6 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
119b7 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64 ccurs while read
119b8 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ing from the jou
119b9 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 rnal file, an SQ
119ba 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f Lite.** error co
119bb 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
119bc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
119bd 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 adMasterJournal(
119be 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
119bf 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 rnl, char *zMast
119c0 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29 er, u32 nMaster)
119c1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
119c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
119c3 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
119c4 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 . u32 len;
119c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
119c6 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 Length in bytes
119c7 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
119c8 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 al name */. i64
119c9 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
119ca 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
119cb 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
119cc 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 f journal file p
119cd 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b Jrnl */. u32 ck
119ce 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 sum;
119cf 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b /* MJ check
119d0 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66 sum value read f
119d1 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 rom journal */.
119d2 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 u32 u;
119d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
119d4 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 nsigned loop cou
119d5 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e nter */. unsign
119d6 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 ed char aMagic[8
119d7 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 ]; /* A buffer
119d8 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 to hold the mag
119d9 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a ic header */. z
119da 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 Master[0] = '\0'
119db 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f ;.. if( SQLITE_
119dc 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK!=(rc = sqlite
119dd 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 3OsFileSize(pJrn
119de 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 l, &szJ)). ||
119df 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c szJ<16. || SQL
119e0 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
119e1 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 ad32bits(pJrnl,
119e2 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 szJ-16, &len)).
119e3 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 || len>=nMaste
119e4 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f r . || SQLITE_
119e5 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
119e6 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d bits(pJrnl, szJ-
119e7 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 12, &cksum)).
119e8 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 || SQLITE_OK!=(r
119e9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
119ea 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c d(pJrnl, aMagic,
119eb 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 8, szJ-8)). |
119ec 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c | memcmp(aMagic,
119ed 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
119ee 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 8). || SQLITE_
119ef 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK!=(rc = sqlite
119f0 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 3OsRead(pJrnl, z
119f1 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a Master, len, szJ
119f2 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 -16-len)). ){.
119f3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
119f4 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 }.. /* See if t
119f5 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 he checksum matc
119f6 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a hes the master j
119f7 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 ournal name */.
119f8 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b for(u=0; u<len;
119f9 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d u++){. cksum
119fa 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a -= zMaster[u];.
119fb 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 }. if( cksum
119fc 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
119fd 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 checksum doesn'
119fe 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f t add up, then o
119ff 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
11a00 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 e disk sectors.
11a01 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 ** containing
11a02 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11a03 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 nal filename is
11a04 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 corrupted. This
11a05 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 means. ** def
11a06 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 initely roll bac
11a07 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 k, so just retur
11a08 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 n SQLITE_OK and
11a09 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 report a (nul).
11a0a 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 ** master-jou
11a0b 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 rnal filename..
11a0c 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 */. len =
11a0d 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 0;. }. zMaster
11a0e 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 [len] = '\0';.
11a0f 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 . return SQLIT
11a10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
11a11 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74 eturn the offset
11a12 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62 of the sector b
11a13 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d oundary at or im
11a14 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f mediately .** fo
11a15 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 llowing the valu
11a16 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 e in pPager->jou
11a17 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e rnalOff, assumin
11a18 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 g a sector .** s
11a19 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 ize of pPager->s
11a1a 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e ectorSize bytes.
11a1b 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 .**.** i.e for a
11a1c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 sector size of
11a1d 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 512:.**.** Pag
11a1e 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 er.journalOff
11a1f 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61 Return va
11a20 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d lue.** -------
11a21 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a22 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a23 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 .** 0
11a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a25 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20 0.** 512
11a26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a27 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 512.** 100
11a28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a29 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 512.** 200
11a2a 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
11a2b 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2048.** .
11a2c 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f */.static i64 jo
11a2d 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50 urnalHdrOffset(P
11a2e 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11a2f 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b i64 offset = 0;
11a30 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65 . i64 c = pPage
11a31 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 r->journalOff;.
11a32 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 if( c ){. of
11a33 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f fset = ((c-1)/JO
11a34 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11a35 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 ger) + 1) * JOUR
11a36 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
11a37 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 r);. }. assert
11a38 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c ( offset%JOURNAL
11a39 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
11a3a 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
11a3b 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 offset>=c );. a
11a3c 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63 ssert( (offset-c
11a3d 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a )<JOURNAL_HDR_SZ
11a3e 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 (pPager) );. re
11a3f 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a turn offset;.}..
11a40 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
11a41 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
11a42 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 pen when this fu
11a43 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
11a44 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
11a45 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
11a46 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
11a47 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 file has not bee
11a48 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 n written to.**
11a49 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65 within the curre
11a4a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 nt transaction (
11a4b 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f i.e. if Pager.jo
11a4c 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a urnalOff==0)..**
11a4d 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74 .** If doTruncat
11a4e 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 e is non-zero or
11a4f 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e the Pager.journ
11a50 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 alSizeLimit vari
11a51 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 able is.** set t
11a52 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 o 0, then trunca
11a53 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 te the journal f
11a54 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 ile to zero byte
11a55 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 s in size. Other
11a56 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 wise,.** zero th
11a57 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72 e 28-byte header
11a58 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
11a59 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11a5a 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 e. In either cas
11a5b 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 e, .** if the pa
11a5c 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f ger is not in no
11a5d 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 -sync mode, sync
11a5e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11a5f 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a e immediately .*
11a60 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20 * after writing
11a61 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74 or truncating it
11a62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 ..**.** If Pager
11a63 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 .journalSizeLimi
11a64 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f t is set to a po
11a65 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f sitive, non-zero
11a66 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 value, and.** f
11a67 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75 ollowing the tru
11a68 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 ncation or zeroi
11a69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f ng described abo
11a6a 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ve the size of t
11a6b 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 he .** journal f
11a6c 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20 ile in bytes is
11a6d 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 larger than this
11a6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 value, then tru
11a6f 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 ncate the.** jou
11a70 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 rnal file to Pag
11a71 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 er.journalSizeLi
11a72 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a mit bytes. The j
11a73 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 ournal file does
11a74 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 .** not need to
11a75 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 be synced follow
11a76 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 ing this operati
11a77 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 on..**.** If an
11a78 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c IO error occurs,
11a79 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 abandon process
11a7a 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ing and return t
11a7b 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
11a7c 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
11a7d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
11a7e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11a7f 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 zeroJournalHdr(P
11a80 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
11a81 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 t doTruncate){.
11a82 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11a83 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
11a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a85 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
11a86 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 e */. assert( i
11a87 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
11a88 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 d) );. if( pPag
11a89 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 er->journalOff )
11a8a 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 {. const i64
11a8b 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d iLimit = pPager-
11a8c 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 >journalSizeLimi
11a8d 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 t; /* Local c
11a8e 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a ache of jsl */..
11a8f 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a IOTRACE(("JZ
11a90 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 EROHDR %p\n", pP
11a91 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64 ager)). if( d
11a92 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 oTruncate || iLi
11a93 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 mit==0 ){.
11a94 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
11a95 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
11a96 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 fd, 0);. }els
11a97 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 e{. static
11a98 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 const char zeroH
11a99 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 dr[28] = {0};.
11a9a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11a9b 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
11a9c 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 jfd, zeroHdr, si
11a9d 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 zeof(zeroHdr), 0
11a9e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
11a9f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11aa0 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
11aa1 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 c ){. rc =
11aa2 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
11aa3 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 ager->jfd, SQLIT
11aa4 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c E_SYNC_DATAONLY|
11aa5 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
11aa6 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 gs);. }..
11aa7 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 /* At this point
11aa8 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
11aa9 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 is committed bu
11aaa 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b t the write lock
11aab 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c . ** is stil
11aac 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 l held on the fi
11aad 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 le. If there is
11aae 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e a size limit con
11aaf 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20 figured for .
11ab0 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 ** the persiste
11ab1 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 nt journal and t
11ab2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11ab3 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d currently consum
11ab4 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 es more. ** s
11ab5 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c pace than that l
11ab6 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c imit allows for,
11ab7 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 truncate it now
11ab8 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 . There is no ne
11ab9 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e ed. ** to syn
11aba 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f c the file follo
11abb 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 wing this operat
11abc 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
11abd 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11abe 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b K && iLimit>0 ){
11abf 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 . i64 sz;.
11ac0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11ac1 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
11ac2 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 er->jfd, &sz);.
11ac3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
11ac4 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 ITE_OK && sz>iLi
11ac5 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 mit ){. r
11ac6 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
11ac7 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 ncate(pPager->jf
11ac8 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 d, iLimit);.
11ac9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
11aca 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
11acb 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
11acc 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 file must be ope
11acd 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 n when this rout
11ace 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 ine is called. A
11acf 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 journal.** head
11ad0 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f er (JOURNAL_HDR_
11ad1 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 69 SZ bytes) is wri
11ad2 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
11ad3 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68 urnal file at th
11ad4 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 e.** current loc
11ad5 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
11ad6 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20 format for the
11ad7 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
11ad8 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
11ad9 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69 - 8 bytes: Magi
11ada 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f c identifying jo
11adb 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a urnal format..**
11adc 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 - 4 bytes: Numb
11add 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e er of records in
11ade 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 journal, or -1
11adf 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 no-sync mode is
11ae0 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 on..** - 4 bytes
11ae1 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 : Random number
11ae2 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 61 used for page ha
11ae3 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 sh..** - 4 bytes
11ae4 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61 : Initial databa
11ae5 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a se page count..*
11ae6 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63 * - 4 bytes: Sec
11ae7 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 79 tor size used by
11ae8 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
11ae9 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 t wrote this jou
11aea 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 rnal..** - 4 byt
11aeb 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 67 es: Database pag
11aec 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 e size..** .** F
11aed 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 ollowed by (JOUR
11aee 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 NAL_HDR_SZ - 28)
11aef 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 bytes of unused
11af0 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 space..*/.stati
11af1 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e c int writeJourn
11af2 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 alHdr(Pager *pPa
11af3 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
11af4 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
11af5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11af6 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
11af7 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 char *zHeader =
11af8 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
11af9 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 e; /* Temporary
11afa 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 62 space used to b
11afb 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 uild header */.
11afc 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 70 u32 nHeader = p
11afd 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
11afe 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
11aff 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
11b00 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a o by zHeader */.
11b01 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20 u32 nWrite;
11b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11b03 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
11b04 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 20 f header sector
11b05 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 written */. int
11b06 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
11b07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11b08 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
11b09 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
11b0a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
11b0b 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f d) ); /* Jo
11b0c 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
11b0d 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 be open. */.. i
11b0e 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e f( nHeader>JOURN
11b0f 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11b10 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 ) ){. nHeader
11b11 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 = JOURNAL_HDR_S
11b12 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a Z(pPager);. }..
11b13 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
11b14 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 e active savepoi
11b15 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 nts and any of t
11b16 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 64 hem were created
11b17 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 . ** since the
11b18 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 most recent jou
11b19 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 20 rnal header was
11b1a 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 written, update
11b1b 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 the . ** PagerS
11b1c 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
11b1d 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a set fields now..
11b1e 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b */. for(ii=0;
11b1f 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 ii<pPager->nSav
11b20 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
11b21 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61 if( pPager->a
11b22 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 Savepoint[ii].iH
11b23 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 drOffset==0 ){.
11b24 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 pPager->aSa
11b25 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 vepoint[ii].iHdr
11b26 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
11b27 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 >journalOff;.
11b28 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 }. }.. pPager
11b29 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 ->journalHdr = p
11b2a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11b2b 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 f = journalHdrOf
11b2c 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 fset(pPager);..
11b2d 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 /* . ** Write
11b2e 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d the nRec Field -
11b2f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
11b30 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 age records that
11b31 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a follow this. *
11b32 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 * journal header
11b33 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f . Normally, zero
11b34 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
11b35 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 69 his value at thi
11b36 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 s time.. ** Aft
11b37 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 61 er the records a
11b38 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 re added to the
11b39 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 journal (and the
11b3a 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c journal synced,
11b3b 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c . ** if in ful
11b3c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 l-sync mode), th
11b3d 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72 e zero is overwr
11b3e 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 74 itten with the t
11b3f 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 rue number. **
11b40 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20 of records (see
11b41 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a syncJournal())..
11b42 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 **. ** A fast
11b43 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 er alternative i
11b44 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 46 s to write 0xFFF
11b45 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65 FFFFF to the nRe
11b46 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 c field. When.
11b47 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a ** reading the j
11b48 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 ournal this valu
11b49 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 e tells SQLite t
11b4a 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 o assume that th
11b4b 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 e. ** rest of t
11b4c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11b4d 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70 contains valid p
11b4e 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69 age records. Thi
11b4f 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a s assumption. *
11b50 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 * is dangerous,
11b51 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 as if a failure
11b52 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 20 occurred whilst
11b53 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a writing to the j
11b54 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
11b55 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 it may contain
11b56 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 74 some garbage dat
11b57 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f a. There are two
11b58 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 scenarios. **
11b59 77 68 65 72 65 20 74 68 69 73 20 72 69 73 6b 20 where this risk
11b5a 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a can be ignored:.
11b5b 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 **. ** * Wh
11b5c 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 en the pager is
11b5d 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e in no-sync mode.
11b5e 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 Corruption can
11b5f 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 follow a. **
11b60 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 power failure
11b61 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 79 in this case any
11b62 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 way.. **. **
11b63 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 * When the SQLI
11b64 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
11b65 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74 PEND flag is set
11b66 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 . This guarantee
11b67 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20 s. ** that
11b68 67 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20 garbage data is
11b69 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 74 never appended t
11b6a 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
11b6b 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 le.. */. asser
11b6c 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11b6d 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d ->fd) || pPager-
11b6e 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 >noSync );. if(
11b6f 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 (pPager->noSync
11b70 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f ) || (pPager->jo
11b71 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
11b72 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
11b73 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69 ORY). || (sqli
11b74 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
11b75 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
11b76 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f r->fd)&SQLITE_IO
11b77 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 CAP_SAFE_APPEND)
11b78 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 . ){. memcp
11b79 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 y(zHeader, aJour
11b7a 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 nalMagic, sizeof
11b7b 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 (aJournalMagic))
11b7c 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 ;. put32bits(
11b7d 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
11b7e 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c aJournalMagic)],
11b7f 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 0xffffffff);.
11b80 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 }else{. memse
11b81 74 28 7a 48 65 61 64 65 72 2c 20 30 2c 20 73 69 t(zHeader, 0, si
11b82 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11b83 69 63 29 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f ic)+4);. }.. /
11b84 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 65 * The random che
11b85 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c 69 ck-hash initiali
11b86 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 ser */ . sqlite
11b87 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
11b88 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 eof(pPager->cksu
11b89 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 2d mInit), &pPager-
11b8a 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70 >cksumInit);. p
11b8b 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
11b8c 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11b8d 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 67 lMagic)+4], pPag
11b8e 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a er->cksumInit);.
11b8f 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 6c /* The initial
11b90 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 2a database size *
11b91 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a /. put32bits(&z
11b92 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
11b93 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c ournalMagic)+8],
11b94 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
11b95 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 ize);. /* The a
11b96 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 ssumed sector si
11b97 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 ze for this proc
11b98 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 ess */. put32bi
11b99 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 ts(&zHeader[size
11b9a 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
11b9b 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 )+12], pPager->s
11b9c 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f ectorSize);.. /
11b9d 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 * The page size
11b9e 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
11b9f 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
11ba0 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 JournalMagic)+16
11ba1 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ], pPager->pageS
11ba2 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 ize);.. /* Init
11ba3 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 69 ializing the tai
11ba4 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 l of the buffer
11ba5 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
11ba6 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 20 . Everything.
11ba7 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 66 ** works find if
11ba8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
11ba9 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 74 emset() is omitt
11baa 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 6c ed. But initial
11bab 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d izing. ** the m
11bac 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 76 emory prevents v
11bad 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d algrind from com
11bae 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20 plaining, so we
11baf 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 are willing to.
11bb0 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 72 ** take the per
11bb1 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20 formance hit..
11bb2 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 65 */. memset(&zHe
11bb3 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
11bb4 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20 rnalMagic)+20],
11bb5 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 61 0,. nHea
11bb6 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75 der-(sizeof(aJou
11bb7 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b rnalMagic)+20));
11bb8 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 .. /* In theory
11bb9 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63 , it is only nec
11bba 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 essary to write
11bbb 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 61 the 28 bytes tha
11bbc 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 t the . ** jour
11bbd 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 75 nal header consu
11bbe 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e mes to the journ
11bbf 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 68 al file here. Th
11bc0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 en increment the
11bc1 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 . ** Pager.jou
11bc2 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c 65 rnalOff variable
11bc3 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f by JOURNAL_HDR_
11bc4 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e SZ so that the n
11bc5 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64 ext . ** record
11bc6 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
11bc7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 he following sec
11bc8 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 67 tor (leaving a g
11bc9 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 20 ap in the file.
11bca 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ** that will be
11bcb 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c implicitly fill
11bcc 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 29 ed in by the OS)
11bcd 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 .. **. ** Howe
11bce 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 ver it has been
11bcf 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 discovered that
11bd0 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 on some systems
11bd1 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 6e this pattern can
11bd2 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 66 . ** be signif
11bd3 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74 icantly slower t
11bd4 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 han contiguously
11bd5 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f writing data to
11bd6 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 the file,. **
11bd7 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 even if that mea
11bd8 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 72 ns explicitly wr
11bd9 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 iting data to th
11bda 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a e block of . **
11bdb 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
11bdc 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 61 - 28) bytes tha
11bdd 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 t will not be us
11bde 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 77 ed. So that is w
11bdf 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65 hat. ** is done
11be0 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 . . **. ** The
11be1 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 65 loop is require
11be2 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 74 d here in case t
11be3 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 69 he sector-size i
11be4 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
11be5 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 e . ** database
11be6 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e 63 page size. Sinc
11be7 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 75 e the zHeader bu
11be8 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 67 ffer is only Pag
11be9 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a er.pageSize. **
11bea 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
11beb 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 61 more than one ca
11bec 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 57 ll to sqlite3OsW
11bed 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 65 rite() may be re
11bee 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70 quired. ** to p
11bef 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 69 opulate the enti
11bf0 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 re journal heade
11bf1 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a r sector.. */ .
11bf2 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20 for(nWrite=0;
11bf3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e rc==SQLITE_OK&&n
11bf4 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 Write<JOURNAL_HD
11bf5 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e 57 R_SZ(pPager); nW
11bf6 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a rite+=nHeader){.
11bf7 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 IOTRACE(("JH
11bf8 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 DR %p %lld %d\n"
11bf9 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 , pPager, pPager
11bfa 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48 ->journalHdr, nH
11bfb 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 3d eader)). rc =
11bfc 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
11bfd 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 pPager->jfd, zHe
11bfe 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 70 ader, nHeader, p
11bff 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11c00 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e f);. pPager->
11c01 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48 journalOff += nH
11c02 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 eader;. }.. re
11c03 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
11c04 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
11c05 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 le must be open
11c06 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c when this is cal
11c07 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 led. A journal h
11c08 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a eader file.** (J
11c09 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 OURNAL_HDR_SZ by
11c0a 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72 6f tes) is read fro
11c0b 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f m the current lo
11c0c 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f cation in the jo
11c0d 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54 urnal.** file. T
11c0e 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
11c0f 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ion in the journ
11c10 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e al file is given
11c11 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a by.** pPager->j
11c12 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63 ournalOff. See c
11c13 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 omments above fu
11c14 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 nction writeJour
11c15 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20 nalHdr() for.**
11c16 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 a description of
11c17 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11c18 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a der format..**.*
11c19 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72 20 * If the header
11c1a 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73 66 is read successf
11c1b 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73 20 ully, *pNRec is
11c1c 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
11c1d 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 r of.** page rec
11c1e 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 ords following t
11c1f 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20 2a his header and *
11c20 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20 74 pDbSize is set t
11c21 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 o the size of th
11c22 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62 65 e.** database be
11c23 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 fore the transac
11c24 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70 tion began, in p
11c25 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67 ages. Also, pPag
11c26 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a er->cksumInit.**
11c27 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 is set to the v
11c28 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 alue read from t
11c29 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
11c2a 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 r. SQLITE_OK is
11c2b 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 returned.** in t
11c2c 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
11c2d 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 If the journal h
11c2e 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65 61 eader file appea
11c2f 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 74 rs to be corrupt
11c30 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 ed, SQLITE_DONE
11c31 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 61 is.** returned a
11c32 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50 nd *pNRec and *P
11c33 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65 66 DbSize are undef
11c34 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41 ined. If JOURNA
11c35 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a L_HDR_SZ bytes.*
11c36 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61 64 * cannot be read
11c37 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
11c38 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72 20 l file an error
11c39 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
11c3a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11c3b 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a readJournalHdr(.
11c3c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
11c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11c3e 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a * Pager object *
11c3f 2f 0a 20 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20 /. int isHot,.
11c40 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 i64 journalSize
11c41 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
11c42 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 70 65 Size of the ope
11c43 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 n journal file i
11c44 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 n bytes */. u32
11c45 20 2a 70 4e 52 65 63 2c 20 20 20 20 20 20 20 20 *pNRec,
11c46 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
11c47 3a 20 56 61 6c 75 65 20 72 65 61 64 20 66 72 6f : Value read fro
11c48 6d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 m the nRec field
11c49 20 2a 2f 0a 20 20 75 33 32 20 2a 70 44 62 53 69 */. u32 *pDbSi
11c4a 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ze
11c4b 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 /* OUT: Value
11c4c 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 of original dat
11c4d 61 62 61 73 65 20 73 69 7a 65 20 66 69 65 6c 64 abase size field
11c4e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
11c4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c50 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11c51 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e code */. unsign
11c52 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 ed char aMagic[8
11c53 5d 3b 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 ]; /* A buff
11c54 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d er to hold the m
11c55 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 agic header */.
11c56 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 i64 iHdrOff;
11c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11c58 20 4f 66 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e Offset of journ
11c59 61 6c 20 68 65 61 64 65 72 20 62 65 69 6e 67 20 al header being
11c5a 72 65 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 read */.. asser
11c5b 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11c5c 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f ->jfd) ); /
11c5d 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d * Journal file m
11c5e 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a ust be open. */.
11c5f 0a 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 50 61 . /* Advance Pa
11c60 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74 ger.journalOff t
11c61 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
11c62 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 2e 20 he next sector.
11c63 49 66 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 If the. ** jour
11c64 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 6f 6f 20 nal file is too
11c65 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 72 65 20 small for there
11c66 74 6f 20 62 65 20 61 20 68 65 61 64 65 72 20 73 to be a header s
11c67 74 6f 72 65 64 20 61 74 20 74 68 69 73 0a 20 20 tored at this.
11c68 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e ** point, return
11c69 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20 SQLITE_DONE..
11c6a 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 */. pPager->jou
11c6b 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 rnalOff = journa
11c6c 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 lHdrOffset(pPage
11c6d 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 r);. if( pPager
11c6e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 ->journalOff+JOU
11c6f 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11c70 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a er) > journalSiz
11c71 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
11c72 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
11c73 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 . iHdrOff = pPa
11c74 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
11c75 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 .. /* Read in t
11c76 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73 he first 8 bytes
11c77 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11c78 68 65 61 64 65 72 2e 20 49 66 20 74 68 65 79 20 header. If they
11c79 64 6f 20 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a do not match. *
11c7a 2a 20 74 68 65 20 20 6d 61 67 69 63 20 73 74 72 * the magic str
11c7b 69 6e 67 20 66 6f 75 6e 64 20 61 74 20 74 68 65 ing found at the
11c7c 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 6a start of each j
11c7d 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 72 ournal header, r
11c7e 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 eturn. ** SQLIT
11c7f 45 5f 44 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f E_DONE. If an IO
11c80 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 error occurs, r
11c81 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
11c82 6f 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a ode. Otherwise,.
11c83 20 20 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20 ** proceed..
11c84 2a 2f 0a 20 20 69 66 28 20 69 73 48 6f 74 20 7c */. if( isHot |
11c85 7c 20 69 48 64 72 4f 66 66 21 3d 70 50 61 67 65 | iHdrOff!=pPage
11c86 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b r->journalHdr ){
11c87 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11c88 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 3OsRead(pPager->
11c89 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a jfd, aMagic, siz
11c8a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 69 48 64 eof(aMagic), iHd
11c8b 72 4f 66 66 29 3b 0a 20 20 20 20 69 66 28 20 72 rOff);. if( r
11c8c 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 c ){. retur
11c8d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
11c8e 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 if( memcmp(aMagi
11c8f 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 c, aJournalMagic
11c90 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 , sizeof(aMagic)
11c91 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )!=0 ){. re
11c92 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
11c93 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
11c94 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74 * Read the first
11c95 20 74 68 72 65 65 20 33 32 2d 62 69 74 20 66 69 three 32-bit fi
11c96 65 6c 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 elds of the jour
11c97 6e 61 6c 20 68 65 61 64 65 72 3a 20 54 68 65 20 nal header: The
11c98 6e 52 65 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c nRec. ** field,
11c99 20 74 68 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e the checksum-in
11c9a 69 74 69 61 6c 69 7a 65 72 20 61 6e 64 20 74 68 itializer and th
11c9b 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
11c9c 61 74 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a at the start. *
11c9d 2a 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 * of the transac
11c9e 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20 tion. Return an
11c9f 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
11ca0 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ything goes wron
11ca1 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 g.. */. if( SQ
11ca2 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
11ca3 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 ead32bits(pPager
11ca4 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 38 ->jfd, iHdrOff+8
11ca5 2c 20 70 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20 , pNRec)). ||
11ca6 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11ca7 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11ca8 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11ca9 2b 31 32 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b +12, &pPager->ck
11caa 73 75 6d 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20 sumInit)). ||
11cab 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11cac 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11cad 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11cae 2b 31 36 2c 20 70 44 62 53 69 7a 65 29 29 0a 20 +16, pDbSize)).
11caf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
11cb0 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 c;. }.. if( pP
11cb1 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11cb2 3d 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 69 ==0 ){. u32 i
11cb3 50 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 PageSize;
11cb4 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 2d /* Page-
11cb5 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f size field of jo
11cb6 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a urnal header */.
11cb7 20 20 20 20 75 33 32 20 69 53 65 63 74 6f 72 53 u32 iSectorS
11cb8 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
11cb9 20 2f 2a 20 53 65 63 74 6f 72 2d 73 69 7a 65 20 /* Sector-size
11cba 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c field of journal
11cbb 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 header */. u
11cbc 31 36 20 69 50 61 67 65 53 69 7a 65 31 36 3b 20 16 iPageSize16;
11cbd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
11cbe 6f 70 79 20 6f 66 20 69 50 61 67 65 53 69 7a 65 opy of iPageSize
11cbf 20 69 6e 20 31 36 2d 62 69 74 20 76 61 72 69 61 in 16-bit varia
11cc0 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 ble */.. /* R
11cc1 65 61 64 20 74 68 65 20 70 61 67 65 2d 73 69 7a ead the page-siz
11cc2 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a e and sector-siz
11cc3 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11cc4 20 66 69 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20 fields. */.
11cc5 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
11cc6 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11cc7 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11cc8 72 4f 66 66 2b 32 30 2c 20 26 69 53 65 63 74 6f rOff+20, &iSecto
11cc9 72 53 69 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20 rSize)). ||
11cca 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11ccb 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11ccc 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11ccd 2b 32 34 2c 20 26 69 50 61 67 65 53 69 7a 65 29 +24, &iPageSize)
11cce 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 ). ){. r
11ccf 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
11cd0 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 . /* Check th
11cd1 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 at the values re
11cd2 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ad from the page
11cd3 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
11cd4 2d 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 -size fields.
11cd5 20 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72 ** are within r
11cd6 61 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 ange. To be 'in
11cd7 72 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c range', both val
11cd8 75 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 ues need to be a
11cd9 20 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 power. ** of
11cda 20 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61 two greater tha
11cdb 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31 n or equal to 51
11cdc 32 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74 2 or 32, and not
11cdd 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
11cde 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 eir . ** resp
11cdf 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 ective compile t
11ce0 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 ime maximum limi
11ce1 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
11ce2 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 f( iPageSize<512
11ce3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ce4 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 || iSectorSize
11ce5 3c 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 <32. || iPag
11ce6 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 eSize>SQLITE_MAX
11ce7 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 _PAGE_SIZE || iS
11ce8 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
11ce9 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c CTOR_SIZE. |
11cea 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 | ((iPageSize-1)
11ceb 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 &iPageSize)!=0
11cec 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a || ((iSectorSiz
11ced 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 e-1)&iSectorSize
11cee 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )!=0 . ){.
11cef 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 /* If the eit
11cf0 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a her the page-siz
11cf1 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 e or sector-size
11cf2 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d in the journal-
11cf3 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 header is .
11cf4 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 ** invalid, the
11cf5 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 n the process th
11cf6 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 at wrote the jou
11cf7 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 rnal-header must
11cf8 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 have . **
11cf9 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 crashed before t
11cfa 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 he header was sy
11cfb 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 nced. In this ca
11cfc 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 se stop reading
11cfd 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f . ** the jo
11cfe 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e urnal file here.
11cff 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
11d00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
11d01 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f NE;. }.. /
11d02 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 * Update the pag
11d03 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 e-size to match
11d04 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 the value read f
11d05 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e rom the journal.
11d06 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 . ** Use a t
11d07 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
11d08 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
11d09 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
11d0a 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 within . **
11d0b 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
11d0c 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 () is tested..
11d0d 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 */. iPageSi
11d0e 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 ze16 = (u16)iPag
11d0f 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 eSize;. rc =
11d10 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
11d11 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 agesize(pPager,
11d12 26 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31 &iPageSize16, -1
11d13 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
11d14 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11d15 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
11d16 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 !=SQLITE_OK || i
11d17 50 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 PageSize16==(u16
11d18 29 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 )iPageSize );..
11d19 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
11d1a 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d assumed sector-
11d1b 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 size to match th
11d1c 65 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 e value used by
11d1d 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 . ** the proc
11d1e 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 ess that created
11d1f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 this journal. I
11d20 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 f this journal w
11d21 61 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 as. ** create
11d22 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f d by a process o
11d23 74 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f ther than this o
11d24 6e 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f ne, then this ro
11d25 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 utine. ** is
11d26 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f being called fro
11d27 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 m within pager_p
11d28 6c 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c layback(). The l
11d29 6f 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a ocal value. *
11d2a 2a 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f * of Pager.secto
11d2b 72 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 rSize is restore
11d2c 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 d at the end of
11d2d 74 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 that routine..
11d2e 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
11d2f 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 >sectorSize = iS
11d30 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a ectorSize;. }..
11d31 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11d32 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f lOff += JOURNAL_
11d33 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a HDR_SZ(pPager);.
11d34 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11d35 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 ./*.** Write the
11d36 20 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 supplied master
11d37 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e journal name in
11d38 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
11d39 69 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a ile for pager.**
11d3a 20 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 pPager at the c
11d3b 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e urrent location.
11d3c 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 The master jour
11d3d 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 nal name must be
11d3e 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 the last.** thi
11d3f 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 ng written to a
11d40 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
11d41 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
11d42 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c full-sync mode,
11d43 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
11d44 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
11d45 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 is advanced to t
11d46 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 he next sector b
11d47 6f 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a oundary before.*
11d48 2a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 * anything is wr
11d49 69 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 itten. The forma
11d4a 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 t is:.**.** +
11d4b 34 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4 bytes: PAGER_M
11d4c 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e J_PGNO..** + N
11d4d 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a bytes: Master j
11d4e 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 ournal filename
11d4f 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b in utf-8..** +
11d50 20 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 4 bytes: N (len
11d51 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f gth of master jo
11d52 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 urnal name in by
11d53 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d tes, no nul-term
11d54 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 inator)..** +
11d55 34 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 4 bytes: Master
11d56 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 journal name che
11d57 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 cksum..** + 8
11d58 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d bytes: aJournalM
11d59 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 agic[]..**.** Th
11d5a 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11d5b 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 page checksum i
11d5c 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 s the sum of the
11d5d 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 bytes in the ma
11d5e 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ster.** journal
11d5f 6e 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 name, where each
11d60 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 byte is interpr
11d61 65 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 eted as a signed
11d62 20 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 8-bit integer..
11d63 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 **.** If zMaster
11d64 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
11d65 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 er (occurs for a
11d66 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
11d67 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a transaction), .
11d68 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 ** this call is
11d69 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
11d6a 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 ic int writeMast
11d6b 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 erJournal(Pager
11d6c 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 *pPager, const c
11d6d 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 har *zMaster){.
11d6e 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
11d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d70 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
11d71 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 */. int nMaste
11d72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
11d73 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
11d74 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 of string zMast
11d75 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 er */. i64 iHdr
11d76 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Off;
11d77 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
11d78 65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 et of header in
11d79 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
11d7a 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 i64 jrnlSize;
11d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d7c 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f /* Size of jo
11d7d 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 urnal file on di
11d7e 73 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 sk */. u32 cksu
11d7f 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 m = 0;
11d80 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
11d81 6b 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a ksum of string z
11d82 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 Master */.. if(
11d83 20 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 !zMaster || pPa
11d84 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 ger->setMaster.
11d85 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 || pPager->jou
11d86 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
11d87 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
11d88 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 RY . || pPager
11d89 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
11d8a 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
11d8b 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 _OFF . ){. r
11d8c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
11d8d 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 . }. pPager->s
11d8e 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 etMaster = 1;.
11d8f 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
11d90 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a Pager->jfd) );..
11d91 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 /* Calculate t
11d92 68 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 he length in byt
11d93 65 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b es and the check
11d94 73 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a sum of zMaster *
11d95 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d /. for(nMaster=
11d96 30 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 0; zMaster[nMast
11d97 65 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b er]; nMaster++){
11d98 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d . cksum += zM
11d99 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a aster[nMaster];.
11d9a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 }.. /* If in
11d9b 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 full-sync mode,
11d9c 61 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e advance to the n
11d9d 65 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 ext disk sector
11d9e 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 before writing.
11d9f 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a ** the master j
11da0 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 ournal name. Thi
11da1 73 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 s is in case the
11da2 20 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 previous page w
11da3 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 ritten to. ** t
11da4 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 he journal has a
11da5 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 lready been sync
11da6 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ed.. */. if( p
11da7 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
11da8 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a ){. pPager->j
11da9 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 ournalOff = jour
11daa 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 nalHdrOffset(pPa
11dab 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 ger);. }. iHdr
11dac 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
11dad 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
11dae 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 Write the master
11daf 20 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f journal data to
11db0 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
11db1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
11db2 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f . ** an error o
11db3 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 ccurs, return th
11db4 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 e error code to
11db5 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f the caller.. */
11db6 0a 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 . if( (0 != (rc
11db7 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
11db8 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
11db9 4f 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 Off, PAGER_MJ_PG
11dba 4e 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 NO(pPager)))).
11dbb 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 || (0 != (rc =
11dbc 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
11dbd 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 Pager->jfd, zMas
11dbe 74 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 ter, nMaster, iH
11dbf 64 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c drOff+4))). ||
11dc0 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 (0 != (rc = wri
11dc1 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
11dc2 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b >jfd, iHdrOff+4+
11dc3 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 nMaster, nMaster
11dc4 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 ))). || (0 !=
11dc5 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 (rc = write32bit
11dc6 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
11dc7 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 HdrOff+4+nMaster
11dc8 2b 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 +4, cksum))).
11dc9 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 || (0 != (rc = s
11dca 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
11dcb 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 ager->jfd, aJour
11dcc 6e 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 nalMagic, 8, iHd
11dcd 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 rOff+4+nMaster+8
11dce 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 ))). ){. ret
11dcf 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 urn rc;. }. pP
11dd0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11dd1 20 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 += (nMaster+20)
11dd2 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 ;. pPager->need
11dd3 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e Sync = !pPager->
11dd4 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 noSync;.. /* If
11dd5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
11dd6 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e peristent-journ
11dd7 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 al mode, then th
11dd8 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a e physical . **
11dd9 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 journal-file ma
11dda 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 y extend past th
11ddb 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 e end of the mas
11ddc 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter-journal name
11ddd 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 . ** and 8 byte
11dde 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 s of magic data
11ddf 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 just written to
11de0 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 the file. This i
11de1 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 s . ** dangerou
11de2 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f s because the co
11de3 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 de to rollback a
11de4 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c hot-journal fil
11de5 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 e. ** will not
11de6 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 be able to find
11de7 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e the master-journ
11de8 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 al name to deter
11de9 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 mine . ** wheth
11dea 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f er or not the jo
11deb 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 urnal is hot. .
11dec 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 **. ** Easiest
11ded 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 thing to do in
11dee 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 this scenario is
11def 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 to truncate the
11df0 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 journal . ** f
11df1 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 ile to the requi
11df2 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a red size.. */ .
11df3 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
11df4 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
11df5 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
11df6 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 >jfd, &jrnlSize)
11df7 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 ). && jrnlSize
11df8 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c >pPager->journal
11df9 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 Off. ){. rc
11dfa 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
11dfb 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
11dfc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11dfd 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Off);. }. retu
11dfe 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11dff 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 Find a page in t
11e00 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 he hash table gi
11e01 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d ven its page num
11e02 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 ber. Return.** a
11e03 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
11e04 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 page or NULL if
11e05 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
11e06 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c ge is not .** al
11e07 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e ready in memory.
11e08 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 .*/.static PgHdr
11e09 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 *pager_lookup(P
11e0a 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
11e0b 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 no pgno){. PgHd
11e0c 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 r *p;
11e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11e0e 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f Return value */
11e0f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 .. /* It is not
11e10 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 possible for a
11e11 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 call to PcacheFe
11e12 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 tch() with creat
11e13 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a eFlag==0 to. **
11e14 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 fail, since no
11e15 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 attempt to alloc
11e16 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f ate dynamic memo
11e17 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e ry will be made.
11e18 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 . */. (void)sq
11e19 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
11e1a 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
11e1b 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a , pgno, 0, &p);.
11e1c 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
11e1d 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 *.** Unless the
11e1e 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f pager is in erro
11e1f 72 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 r-state, discard
11e20 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 all in-memory p
11e21 61 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 ages. If.** the
11e22 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f pager is in erro
11e23 72 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 r-state, then th
11e24 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
11e25 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 op..**.** TODO:
11e26 57 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 Why can we not r
11e27 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77 eset the pager w
11e28 68 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74 hile in error st
11e29 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ate?.*/.static v
11e2a 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 oid pager_reset(
11e2b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11e2c 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
11e2d 3d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 =pPager->errCode
11e2e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
11e2f 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 ackupRestart(pPa
11e30 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 ger->pBackup);.
11e31 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
11e32 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50 Clear(pPager->pP
11e33 43 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67 Cache);. pPag
11e34 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
11e35 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
11e36 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 * Free all struc
11e37 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 tures in the Pag
11e38 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 er.aSavepoint[]
11e39 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f array and set bo
11e3a 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 th.** Pager.aSav
11e3b 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 epoint and Pager
11e3c 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a .nSavepoint to z
11e3d 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 ero. Close the s
11e3e 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 ub-journal.** if
11e3f 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 it is open and
11e40 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 the pager is not
11e41 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
11e42 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f de..*/.static vo
11e43 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 id releaseAllSav
11e44 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 epoints(Pager *p
11e45 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 Pager){. int ii
11e46 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11e47 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 /* Iterator for
11e48 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 looping through
11e49 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
11e4a 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 */. for(ii=0;
11e4b 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
11e4c 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
11e4d 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
11e4e 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 estroy(pPager->a
11e4f 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 Savepoint[ii].pI
11e50 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
11e51 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
11e52 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
11e53 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
11e54 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 rnal(pPager->sjf
11e55 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
11e56 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11e57 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 >sjfd);. }. sq
11e58 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 lite3_free(pPage
11e59 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a r->aSavepoint);.
11e5a 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
11e5b 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 oint = 0;. pPag
11e5c 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d er->nSavepoint =
11e5d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 0;. pPager->nS
11e5e 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ubRec = 0;.}../*
11e5f 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 .** Set the bit
11e60 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 number pgno in t
11e61 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
11e62 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a t.pInSavepoint .
11e63 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c ** bitvecs of al
11e64 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 l open savepoint
11e65 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 s. Return SQLITE
11e66 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 _OK if successfu
11e67 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e l.** or SQLITE_N
11e68 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 OMEM if a malloc
11e69 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e failure occurs.
11e6a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
11e6b 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
11e6c 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 vecs(Pager *pPag
11e6d 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
11e6e 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
11e6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
11e70 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
11e71 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11e72 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 _OK; /* Re
11e73 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 sult code */..
11e74 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
11e75 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
11e76 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 ii++){. Page
11e77 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 rSavepoint *p =
11e78 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
11e79 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 int[ii];. if(
11e7a 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 pgno<=p->nOrig
11e7b 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 ){. rc |= s
11e7c 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
11e7d 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c p->pInSavepoint,
11e7e 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 pgno);. te
11e7f 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
11e80 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
11e81 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
11e82 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 LITE_OK || rc==S
11e83 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
11e84 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
11e85 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 n rc;.}../*.** U
11e86 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
11e87 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75 se file. This fu
11e88 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
11e89 70 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a p if the pager.*
11e8a 2a 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 * is in exclusiv
11e8b 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e mode..**.** If
11e8c 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75 the pager is cu
11e8d 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 rrently in error
11e8e 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 state, discard
11e8f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11e90 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e .** the cache an
11e91 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65 d reset the Page
11e92 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65 r structure inte
11e93 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74 rnal state. If t
11e94 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 here is.** an op
11e95 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c en journal-file,
11e96 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74 then the next t
11e97 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 ime a shared-loc
11e98 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a k is obtained.**
11e99 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 on the pager fi
11e9a 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61 le (by this or a
11e9b 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
11e9c 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a ), it will be.**
11e9d 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f treated as a ho
11e9e 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f t-journal and ro
11e9f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 lled back..*/.st
11ea0 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
11ea1 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 unlock(Pager *pP
11ea2 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50 ager){. if( !pP
11ea3 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
11ea4 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ode ){. int r
11ea5 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11ea6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11ea7 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f n code */.. /
11ea8 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 * Always close t
11ea9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11eaa 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 when dropping th
11eab 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e e database lock.
11eac 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 . ** Otherwis
11ead 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 e, another conne
11eae 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e ction with journ
11eaf 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d al_mode=delete m
11eb0 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 ight. ** dele
11eb1 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 te the file out
11eb2 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 from under us..
11eb3 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
11eb4 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11eb5 3e 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 >jfd);. sqlit
11eb6 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
11eb7 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
11eb8 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
11eb9 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
11eba 0a 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 . releaseAllS
11ebb 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 avepoints(pPager
11ebc 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
11ebd 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b e file is unlock
11ebe 65 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 ed, somebody els
11ebf 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 e might change i
11ec0 74 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 t. The. ** va
11ec1 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50 lues stored in P
11ec2 61 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e ager.dbSize etc.
11ec3 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e might become in
11ec4 76 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 valid if. **
11ec5 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f this happens. TO
11ec6 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 DO: Really, this
11ec7 20 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f doesn't need to
11ec8 20 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20 be cleared.
11ec9 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61 ** until the cha
11eca 6e 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 nge-counter chec
11ecb 6b 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72 k fails in Pager
11ecc 53 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 SharedLock()..
11ecd 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
11ece 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 >dbSizeValid = 0
11ecf 3b 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e ;.. rc = osUn
11ed0 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c lock(pPager->fd,
11ed1 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 NO_LOCK);. i
11ed2 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70 f( rc ){. p
11ed3 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d Pager->errCode =
11ed4 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 rc;. }. I
11ed5 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 OTRACE(("UNLOCK
11ed6 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
11ed7 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72 . /* If Pager
11ed8 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c .errCode is set,
11ed9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
11eda 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
11edb 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a cannot be. *
11edc 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 * trusted. Now t
11edd 68 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69 hat the pager fi
11ede 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 le is unlocked,
11edf 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11ee0 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 the. ** cache
11ee1 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 can be discarde
11ee2 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 d and the error
11ee3 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61 code safely clea
11ee4 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 red.. */.
11ee5 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
11ee6 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ode ){. if(
11ee7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
11ee8 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 {. pPager
11ee9 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 ->errCode = SQLI
11eea 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 TE_OK;. }.
11eeb 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 pager_reset
11eec 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
11eed 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 . pPager->cha
11eee 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 ngeCountDone = 0
11eef 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 ;. pPager->st
11ef0 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f ate = PAGER_UNLO
11ef1 43 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e CK;. pPager->
11ef2 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a dbModified = 0;.
11ef3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
11ef4 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c s function shoul
11ef5 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e d be called when
11ef6 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 55 an IOERR, CORRU
11ef7 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72 PT or FULL error
11ef8 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 63 .** may have occ
11ef9 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 74 urred. The first
11efa 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
11efb 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 ointer to the pa
11efc 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 ger .** structur
11efd 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 68 e, the second th
11efe 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f e error-code abo
11eff 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 ut to be returne
11f00 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a 2a d by a pager .**
11f01 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 API function. T
11f02 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
11f03 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 d is a copy of t
11f04 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
11f05 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 nt .** to this f
11f06 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 unction. .**.**
11f07 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
11f08 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 gument is SQLITE
11f09 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 _IOERR, SQLITE_C
11f0a 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 ORRUPT, or SQLIT
11f0b 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 E_FULL.** the er
11f0c 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 ror becomes pers
11f0d 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 istent. Until th
11f0e 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f e persisten erro
11f0f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a r is cleared,.**
11f10 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 subsequent API
11f11 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 calls on this Pa
11f12 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 ger will immedia
11f13 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 tely return the
11f14 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 same .** error c
11f15 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 ode..**.** A per
11f16 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e sistent error in
11f17 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 dicates that the
11f18 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
11f19 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a pager-cache .**
11f1a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 cannot be trust
11f1b 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63 ed. This state c
11f1c 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 an be cleared by
11f1d 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 completely disc
11f1e 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 arding .** the c
11f1f 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
11f20 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61 ager-cache. If a
11f21 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
11f22 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 active when.**
11f23 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65 the persistent e
11f24 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 rror occurred, t
11f25 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b hen the rollback
11f26 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 journal may nee
11f27 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 d.** to be repla
11f28 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74 yed to restore t
11f29 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11f2a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11f2b 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 (as if.** it we
11f2c 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c re a hot-journal
11f2d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
11f2e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67 pager_error(Pag
11f2f 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
11f30 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d rc){. int rc2 =
11f31 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73 rc & 0xff;. as
11f32 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
11f33 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b _OK || !MEMDB );
11f34 0a 20 20 61 73 73 65 72 74 28 0a 20 20 20 20 20 . assert(.
11f35 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 pPager->errCod
11f36 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c e==SQLITE_FULL |
11f37 7c 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 2d |. pPager-
11f38 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 >errCode==SQLITE
11f39 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 _OK ||. (p
11f3a 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 Pager->errCode &
11f3b 20 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 0xff)==SQLITE_I
11f3c 4f 45 52 52 0a 20 20 29 3b 0a 20 20 69 66 28 20 OERR. );. if(
11f3d 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c rc2==SQLITE_FULL
11f3e 20 7c 7c 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f || rc2==SQLITE_
11f3f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 70 50 61 IOERR ){. pPa
11f40 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 ger->errCode = r
11f41 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 c;. }. return
11f42 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 rc;.}../*.** Exe
11f43 63 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 cute a rollback
11f44 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e if a transaction
11f45 20 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 is active and u
11f46 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 nlock the .** da
11f47 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a tabase file. .**
11f48 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
11f49 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 has already ent
11f4a 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 ered the error s
11f4b 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 tate, do not att
11f4c 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c empt .** the rol
11f4d 6c 62 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 lback at this ti
11f4e 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 me. Instead, pag
11f4f 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 er_unlock() is c
11f50 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 alled. The.** ca
11f51 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f ll to pager_unlo
11f52 63 6b 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 ck() will discar
11f53 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 d all in-memory
11f54 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a pages, unlock.**
11f55 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11f56 6c 65 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 le and clear the
11f57 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 error state. If
11f58 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 this means that
11f59 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 .** there is a h
11f5a 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 ot-journal left
11f5b 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
11f5c 65 6d 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e em, the next con
11f5d 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 nection.** to ob
11f5e 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f tain a shared lo
11f5f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 ck on the pager
11f60 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 (which may be th
11f61 69 73 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 is one) will.**
11f62 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a roll it back..**
11f63 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
11f64 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 has not already
11f65 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 entered the err
11f66 6f 72 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e or state, but an
11f67 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 IO or.** malloc
11f68 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
11f69 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c ring a rollback,
11f6a 20 74 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 then this will
11f6b 69 74 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a itself cause .**
11f6c 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e the pager to en
11f6d 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ter the error st
11f6e 61 74 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 ate. Which will
11f6f 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 be cleared by th
11f70 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 e.** call to pag
11f71 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 er_unlock(), as
11f72 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e described above.
11f73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
11f74 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f pagerUnlockAndRo
11f75 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 llback(Pager *pP
11f76 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 ager){. if( pPa
11f77 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
11f78 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
11f79 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
11f7a 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 RESERVED ){.
11f7b 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
11f7c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
11f7d 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
11f7e 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 back(pPager);.
11f7f 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
11f80 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a gnMalloc();. }.
11f81 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
11f82 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Pager);.}../*.**
11f83 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e This routine en
11f84 64 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ds a transaction
11f85 2e 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 . A transaction
11f86 69 73 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 is usually ended
11f87 20 62 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 by .** either a
11f88 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c COMMIT or a ROL
11f89 4c 42 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e LBACK operation.
11f8a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 This routine ma
11f8b 79 20 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 y be called .**
11f8c 61 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f after rollback o
11f8d 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c f a hot-journal,
11f8e 20 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 or if an error
11f8f 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 occurs while ope
11f90 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 ning.** the jour
11f91 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 nal file or writ
11f92 69 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72 ing the very fir
11f93 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 st journal-heade
11f94 72 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 r of a.** databa
11f95 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a se transaction..
11f96 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 ** .** If the pa
11f97 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f ger is in PAGER_
11f98 53 48 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f SHARED or PAGER_
11f99 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 UNLOCK state whe
11f9a 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e n this.** routin
11f9b 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 e is called, it
11f9c 69 73 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 is a no-op (retu
11f9d 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a rns SQLITE_OK)..
11f9e 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
11f9f 20 61 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 any active save
11fa0 70 6f 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 points are relea
11fa1 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 sed..**.** If th
11fa2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
11fa3 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 s open, then it
11fa4 69 73 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 is "finalized".
11fa5 4f 6e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a Once a journal .
11fa6 2a 2a 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e ** file has been
11fa7 20 66 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 finalized it is
11fa8 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
11fa9 20 75 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 use it to roll
11faa 62 61 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 back a .** trans
11fab 61 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c action. Nor will
11fac 20 69 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 it be considere
11fad 64 20 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f d to be a hot-jo
11fae 75 72 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a urnal by this.**
11faf 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 or any other da
11fb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
11fb1 6e 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 n. Exactly how a
11fb2 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 journal is fina
11fb3 6c 69 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 lized.** depends
11fb4 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
11fb5 6f 74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 ot the pager is
11fb6 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
11fb7 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a sive mode and.**
11fb8 20 74 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 the current jou
11fb9 72 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 rnal-mode (Pager
11fba 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c .journalMode val
11fbb 75 65 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a ue), as follows:
11fbc 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c .**.** journal
11fbd 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 Mode==MEMORY.**
11fbe 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 Journal file
11fbf 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 descriptor is s
11fc0 69 6d 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 imply closed. Th
11fc1 69 73 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a is destroys an .
11fc2 2a 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 ** in-memory
11fc3 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 journal..**.**
11fc4 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 journalMode==T
11fc5 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a RUNCATE.** J
11fc6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 ournal file is t
11fc7 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f runcated to zero
11fc8 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a bytes in size..
11fc9 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d **.** journalM
11fca 6f 64 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 ode==PERSIST.**
11fcb 20 20 20 20 54 68 65 20 66 69 72 73 74 20 32 38 The first 28
11fcc 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f bytes of the jo
11fcd 75 72 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a urnal file are z
11fce 65 72 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 eroed. This inva
11fcf 6c 69 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 lidates.** t
11fd0 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c he first journal
11fd1 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 header in the f
11fd2 69 6c 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 ile, and hence t
11fd3 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 he entire journa
11fd4 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 l.** file. A
11fd5 6e 20 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 n invalid journa
11fd6 6c 20 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 l file cannot be
11fd7 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
11fd8 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 .** journalMod
11fd9 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 e==DELETE.**
11fda 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
11fdb 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 e is closed and
11fdc 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 deleted using sq
11fdd 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e lite3OsDelete().
11fde 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 .**.** If th
11fdf 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
11fe0 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 ng in exclusive
11fe1 6d 6f 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f mode, this metho
11fe2 64 20 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a d of finalizing.
11fe3 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e ** the journ
11fe4 61 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 al file is never
11fe5 20 75 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 used. Instead,
11fe6 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f if the journalMo
11fe7 64 65 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c de is.** DEL
11fe8 45 54 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 ETE and the page
11fe9 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 r is in exclusiv
11fea 65 20 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 e mode, the meth
11feb 6f 64 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 od described und
11fec 65 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 er.** journa
11fed 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 lMode==PERSIST i
11fee 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a s used instead..
11fef 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 **.** After the
11ff0 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c journal is final
11ff1 69 7a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 ized, if running
11ff2 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 in non-exclusiv
11ff3 65 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 e mode, the.** p
11ff4 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 ager moves to PA
11ff5 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 GER_SHARED state
11ff6 20 28 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 (and downgrades
11ff7 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 the lock on the
11ff8 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
11ff9 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a e accordingly)..
11ffa 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
11ffb 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e er is running in
11ffc 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 exclusive mode
11ffd 61 6e 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f and is in PAGER_
11ffe 53 59 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a SYNCED state,.**
11fff 20 69 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 it moves to PAG
12000 45 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f ER_EXCLUSIVE. No
12001 20 6c 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 locks are downg
12002 72 61 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 raded when runni
12003 6e 67 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 ng in.** exclusi
12004 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 ve mode..**.** S
12005 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
12006 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 rned if no error
12007 20 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 occurs. If an e
12008 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
12009 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 ng.** any of the
1200a 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 IO operations t
1200b 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a o finalize the j
1200c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 ournal file or u
1200d 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 nlock the.** dat
1200e 61 62 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 abase then the I
1200f 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12010 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
12011 75 73 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a user. If the .**
12012 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 operation to fi
12013 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e nalize the journ
12014 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 al file fails, t
12015 68 65 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 hen the code sti
12016 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 ll.** tries to u
12017 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
12018 73 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 se file if not i
12019 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
1201a 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f . If the.** unlo
1201b 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 ck operation fai
1201c 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e ls as well, then
1201d 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 the first error
1201e 20 63 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a code related.**
1201f 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 to the first er
12020 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 ror encountered
12021 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e (the journal fin
12022 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 alization one) i
12023 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a s.** returned..*
12024 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12025 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
12026 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 on(Pager *pPager
12027 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 , int hasMaster)
12028 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
12029 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 ITE_OK; /*
1202a 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 Error code from
1202b 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 journal finaliza
1202c 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a tion operation *
1202d 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 /. int rc2 = SQ
1202e 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 LITE_OK; /*
1202f 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 Error code from
12030 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f db file unlock o
12031 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 peration */.. i
12032 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
12033 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 <PAGER_RESERVED
12034 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
12035 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 LITE_OK;. }. r
12036 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 eleaseAllSavepoi
12037 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 nts(pPager);..
12038 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
12039 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 Pager->jfd) || p
1203a 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1203b 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 l==0 );. if( is
1203c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1203d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e ) ){.. /* Fin
1203e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 alize the journa
1203f 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 l file. */. i
12040 66 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a f( sqlite3IsMemJ
12041 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a ournal(pPager->j
12042 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 fd) ){. ass
12043 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
12044 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
12045 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
12046 52 59 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 RY );. sqli
12047 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
12048 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c r->jfd);. }el
12049 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a se if( pPager->j
1204a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
1204b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 R_JOURNALMODE_TR
1204c 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 20 20 20 UNCATE ){.
1204d 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
1204e 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 nalOff==0 ){.
1204f 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
12050 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 _OK;. }else
12051 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
12052 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
12053 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 (pPager->jfd, 0)
12054 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
12055 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12056 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ff = 0;. pP
12057 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
12058 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 rted = 0;. }e
12059 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e lse if( pPager->
1205a 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 exclusiveMode .
1205b 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a || pPager->j
1205c 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
1205d 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
1205e 52 53 49 53 54 0a 20 20 20 20 29 7b 0a 20 20 20 RSIST. ){.
1205f 20 20 20 72 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 rc = zeroJour
12060 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 68 nalHdr(pPager, h
12061 61 73 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 asMaster);.
12062 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
12063 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 ger, rc);.
12064 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12065 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ff = 0;. pP
12066 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
12067 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 rted = 0;. }e
12068 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 lse{. /* Th
12069 69 73 20 62 72 61 6e 63 68 20 6d 61 79 20 62 65 is branch may be
1206a 20 65 78 65 63 75 74 65 64 20 77 69 74 68 20 50 executed with P
1206b 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ager.journalMode
1206c 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a 20 20 20 20 ==MEMORY if.
1206d 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e ** a hot-journ
1206e 61 6c 20 77 61 73 20 6a 75 73 74 20 72 6f 6c 6c al was just roll
1206f 65 64 20 62 61 63 6b 2e 20 49 6e 20 74 68 69 73 ed back. In this
12070 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 case the journa
12071 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 l. ** file
12072 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 should be closed
12073 20 61 6e 64 20 64 65 6c 65 74 65 64 2e 20 49 66 and deleted. If
12074 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e this connection
12075 20 77 72 69 74 65 73 20 74 6f 0a 20 20 20 20 20 writes to.
12076 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
12077 20 66 69 6c 65 2c 20 69 74 20 77 69 6c 6c 20 64 file, it will d
12078 6f 20 73 6f 20 75 73 69 6e 67 20 61 6e 20 69 6e o so using an in
12079 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e -memory journal.
1207a 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 */. asser
1207b 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t( pPager->journ
1207c 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
1207d 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 URNALMODE_DELETE
1207e 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1207f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
12080 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
12081 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 ALMODE_MEMORY .
12082 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 73 71 );. sq
12083 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
12084 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
12085 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 if( !pPager->te
12086 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 mpFile ){.
12087 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12088 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 Delete(pPager->p
12089 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f Vfs, pPager->zJo
1208a 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 urnal, 0);.
1208b 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 }. }..#ifdef
1208c 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
1208d 47 45 53 0a 20 20 20 20 73 71 6c 69 74 65 33 50 GES. sqlite3P
1208e 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 cacheIterateDirt
1208f 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 y(pPager->pPCach
12090 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 e, pager_set_pag
12091 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66 0a 0a ehash);.#endif..
12092 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
12093 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 eCleanAll(pPager
12094 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 ->pPCache);.
12095 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
12096 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e troy(pPager->pIn
12097 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 Journal);. pP
12098 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
12099 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
1209a 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 0a ->nRec = 0;. }.
1209b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
1209c 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b exclusiveMode ){
1209d 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 55 6e 6c . rc2 = osUnl
1209e 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
1209f 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
120a0 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
120a1 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a = PAGER_SHARED;.
120a2 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e pPager->chan
120a3 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b geCountDone = 0;
120a4 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 . }else if( pPa
120a5 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
120a6 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 20 20 R_SYNCED ){.
120a7 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
120a8 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b PAGER_EXCLUSIVE;
120a9 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 . }. pPager->s
120aa 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 etMaster = 0;.
120ab 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
120ac 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
120ad 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a dbModified = 0;.
120ae 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 74 . /* TODO: Is t
120af 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20 57 68 79 his optimal? Why
120b0 20 69 73 20 74 68 65 20 64 62 20 73 69 7a 65 20 is the db size
120b1 69 6e 76 61 6c 69 64 61 74 65 64 20 68 65 72 65 invalidated here
120b2 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 . ** when the
120b3 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
120b4 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64 3f 20 2a not unlocked? *
120b5 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 /. pPager->dbOr
120b6 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 20 73 71 igSize = 0;. sq
120b7 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
120b8 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ate(pPager->pPCa
120b9 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 che, pPager->dbS
120ba 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d 45 4d ize);. if( !MEM
120bb 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 DB ){. pPager
120bc 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 ->dbSizeValid =
120bd 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 0;. }.. return
120be 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f (rc==SQLITE_OK?
120bf 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a rc2:rc);.}../*.*
120c0 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44 61 74 * Parameter aDat
120c1 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 a must point to
120c2 61 20 62 75 66 66 65 72 20 6f 66 20 70 50 61 67 a buffer of pPag
120c3 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 er->pageSize byt
120c4 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e 20 43 es.** of data. C
120c5 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74 75 72 ompute and retur
120c6 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62 61 73 n a checksum bas
120c7 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e 74 65 ed ont the conte
120c8 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 nts of the .** p
120c9 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e 64 20 age of data and
120ca 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
120cb 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63 6b 73 e of pPager->cks
120cc 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 umInit..**.** Th
120cd 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 61 6c is is not a real
120ce 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20 69 73 checksum. It is
120cf 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 really just the
120d0 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a 2a 20 sum of the .**
120d1 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 20 76 random initial v
120d2 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e 63 6b alue (pPager->ck
120d3 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65 76 65 sumInit) and eve
120d4 72 79 20 32 30 30 74 68 20 62 79 74 65 0a 2a 2a ry 200th byte.**
120d5 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 of the page dat
120d6 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 a, starting with
120d7 20 62 79 74 65 20 6f 66 66 73 65 74 20 28 70 50 byte offset (pP
120d8 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 25 32 ager->pageSize%2
120d9 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62 79 74 00)..** Each byt
120da 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 e is interpreted
120db 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 as an 8-bit uns
120dc 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a igned integer..*
120dd 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
120de 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64 20 74 e formula used t
120df 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73 20 63 o compute this c
120e0 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74 73 20 hecksum results
120e1 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d 70 61 in an.** incompa
120e2 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 tible journal fi
120e3 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a le format..**.**
120e4 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f 72 72 If journal corr
120e5 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 64 75 uption occurs du
120e6 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 61 69 e to a power fai
120e7 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 20 6c lure, the most l
120e8 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e 61 72 ikely .** scenar
120e9 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65 20 65 io is that one e
120ea 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 nd or the other
120eb 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77 69 of the record wi
120ec 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20 0a ll be changed. .
120ed 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20 6c 65 ** It is much le
120ee 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20 74 ss likely that t
120ef 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20 74 he two ends of t
120f0 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 he journal recor
120f1 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72 d will be.** cor
120f2 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69 64 rect and the mid
120f3 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e 20 dle be corrupt.
120f4 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68 65 Thus, this "che
120f5 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a cksum" scheme,.*
120f6 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61 6e * though fast an
120f7 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68 65 d simple, catche
120f8 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b s the mostly lik
120f9 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72 ely kind of corr
120fa 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 uption..*/.stati
120fb 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b 73 75 c u32 pager_cksu
120fc 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c m(Pager *pPager,
120fd 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 const u8 *aData
120fe 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d ){. u32 cksum =
120ff 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e pPager->cksumIn
12100 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 it; /* C
12101 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 74 6f hecksum value to
12102 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 return */. int
12103 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 i = pPager->pag
12104 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20 20 20 eSize-200;
12105 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
12106 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 ter */. while(
12107 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73 75 6d i>0 ){. cksum
12108 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a 20 20 += aData[i];.
12109 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a i -= 200;. }.
1210a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d 3b 0a return cksum;.
1210b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 }../*.** Read a
1210c 73 69 6e 67 6c 65 20 70 61 67 65 20 66 72 6f 6d single page from
1210d 20 65 69 74 68 65 72 20 74 68 65 20 6a 6f 75 72 either the jour
1210e 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 73 4d nal file (if isM
1210f 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a ainJrnl==1) or.*
12110 2a 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a * from the sub-j
12111 6f 75 72 6e 61 6c 20 28 69 66 20 69 73 4d 61 69 ournal (if isMai
12112 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64 20 70 6c nJrnl==0) and pl
12113 61 79 62 61 63 6b 20 74 68 61 74 20 70 61 67 65 ayback that page
12114 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 62 65 ..** The page be
12115 67 69 6e 73 20 61 74 20 6f 66 66 73 65 74 20 2a gins at offset *
12116 70 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 pOffset into the
12117 20 66 69 6c 65 2e 20 54 68 65 20 2a 70 4f 66 66 file. The *pOff
12118 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 set.** value is
12119 69 6e 63 72 65 61 73 65 64 20 74 6f 20 74 68 65 increased to the
1211a 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 start of the ne
1211b 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a xt page in the j
1211c 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 ournal..**.** Th
1211d 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61 e isMainJrnl fla
1211e 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 69 g is true if thi
1211f 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 72 6f s is the main ro
12120 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 llback journal a
12121 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 nd.** false for
12122 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
12123 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 69 6e urnal. The main
12124 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
12125 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63 6b 73 l uses.** checks
12126 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d ums - the statem
12127 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 ent journal does
12128 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 not..**.** If t
12129 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1212a 66 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 f the page recor
1212b 64 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 d read from the
1212c 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 (sub-)journal fi
1212d 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 le.** is greater
1212e 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e than the curren
1212f 74 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 t value of Pager
12130 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e 20 70 6c .dbSize, then pl
12131 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 73 6b 69 ayback is.** ski
12132 70 70 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f pped and SQLITE_
12133 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
12134 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e 65 20 69 **.** If pDone i
12135 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
12136 20 69 74 20 69 73 20 61 20 72 65 63 6f 72 64 20 it is a record
12137 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 68 61 of pages that ha
12138 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 ve already.** be
12139 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 en played back.
1213a 20 49 66 20 74 68 65 20 70 61 67 65 20 61 74 20 If the page at
1213b 2a 70 4f 66 66 73 65 74 20 68 61 73 20 61 6c 72 *pOffset has alr
1213c 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79 65 64 eady been played
1213d 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 74 68 65 back.** (if the
1213e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 corresponding p
1213f 44 6f 6e 65 20 62 69 74 20 69 73 20 73 65 74 29 Done bit is set)
12140 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65 20 70 then skip the p
12141 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d 61 6b 65 layback..** Make
12142 20 73 75 72 65 20 74 68 65 20 70 44 6f 6e 65 20 sure the pDone
12143 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e bit correspondin
12144 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 66 73 65 g to the *pOffse
12145 74 20 70 61 67 65 20 69 73 20 73 65 74 0a 2a 2a t page is set.**
12146 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e prior to return
12147 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ing..**.** If th
12148 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 73 e page record is
12149 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 successfully re
1214a 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 ad from the (sub
1214b 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a -)journal file.*
1214c 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 62 61 63 * and played bac
1214d 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f k, then SQLITE_O
1214e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
1214f 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
12150 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 20 72 65 curs.** while re
12151 61 64 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 ading the record
12152 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 from the (sub-)
12153 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 journal file or
12154 77 68 69 6c 65 20 77 72 69 74 69 6e 67 0a 2a 2a while writing.**
12155 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
12156 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 file, then the
12157 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 IO error code is
12158 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 64 61 returned. If da
12159 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 ta.** is success
1215a 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 fully read from
1215b 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
1215c 6c 20 66 69 6c 65 20 62 75 74 20 61 70 70 65 61 l file but appea
1215d 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 72 72 rs to be.** corr
1215e 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f upted, SQLITE_DO
1215f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 NE is returned.
12160 44 61 74 61 20 69 73 20 63 6f 6e 73 69 64 65 72 Data is consider
12161 65 64 20 63 6f 72 72 75 70 74 65 64 20 69 6e 0a ed corrupted in.
12162 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d 73 74 61 ** two circumsta
12163 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 2a nces:.** .** *
12164 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 70 If the record p
12165 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 69 6c age-number is il
12166 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 41 47 45 legal (0 or PAGE
12167 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72 0a 2a R_MJ_PGNO), or.*
12168 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 * * If the rec
12169 6f 72 64 20 69 73 20 62 65 69 6e 67 20 72 6f 6c ord is being rol
1216a 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 led back from th
1216b 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
1216c 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74 ile.** and t
1216d 68 65 20 63 68 65 63 6b 73 75 6d 20 66 69 65 6c he checksum fiel
1216e 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 d does not match
1216f 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 the record cont
12170 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 69 74 68 ent..**.** Neith
12171 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 er of these two
12172 73 63 65 6e 61 72 69 6f 73 20 61 72 65 20 70 6f scenarios are po
12173 73 73 69 62 6c 65 20 64 75 72 69 6e 67 20 61 20 ssible during a
12174 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 savepoint rollba
12175 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ck..**.** If thi
12176 73 20 69 73 20 61 20 73 61 76 65 70 6f 69 6e 74 s is a savepoint
12177 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 rollback, then
12178 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 76 65 20 memory may have
12179 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c to be dynamicall
1217a 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 y.** allocated b
1217b 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
1217c 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
1217d 63 61 73 65 20 61 6e 64 20 61 6e 20 61 6c 6c 6f case and an allo
1217e 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a cation fails,.**
1217f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 SQLITE_NOMEM is
12180 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
12181 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 atic int pager_p
12182 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 layback_one_page
12183 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 (. Pager *pPage
12184 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
12185 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 62 /* The pager b
12186 65 69 6e 67 20 70 6c 61 79 65 64 20 62 61 63 6b eing played back
12187 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e */. int isMain
12188 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Jrnl,
12189 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e /* 1 -> main
1218a 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 journal. 0 -> s
1218b 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 ub-journal. */.
1218c 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 2c 20 20 int isUnsync,
1218d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1218e 2a 20 54 72 75 65 20 69 66 20 72 65 61 64 69 6e * True if readin
1218f 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63 65 64 20 g from unsynced
12190 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a main journal */.
12191 20 20 69 36 34 20 2a 70 4f 66 66 73 65 74 2c 20 i64 *pOffset,
12192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12193 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 72 65 63 /* Offset of rec
12194 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 ord to playback
12195 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76 65 70 */. int isSavep
12196 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nt,
12197 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 /* True for a
12198 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 savepoint rollb
12199 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 ack */. Bitvec
1219a 2a 70 44 6f 6e 65 20 20 20 20 20 20 20 20 20 20 *pDone
1219b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 /* Bitvec
1219c 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 64 of pages alread
1219d 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f y played back */
1219e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
1219f 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20 20 20 PgHdr *pPg;
121a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
121a1 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 An existing pag
121a2 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a e in the cache *
121a3 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 /. Pgno pgno;
121a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121a5 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 /* The page nu
121a6 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65 20 69 mber of a page i
121a7 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 n journal */. u
121a8 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 32 cksum;
121a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
121aa 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20 66 6f Checksum used fo
121ab 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e r sanity checkin
121ac 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 61 g */. char *aDa
121ad 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
121ae 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
121af 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20 74 68 y storage for th
121b0 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71 6c 69 e page */. sqli
121b1 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 te3_file *jfd;
121b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
121b3 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
121b4 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c for the journal
121b5 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 file */.. asse
121b6 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 26 rt( (isMainJrnl&
121b7 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f ~1)==0 ); /
121b8 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 69 73 20 * isMainJrnl is
121b9 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 65 0 or 1 */. asse
121ba 72 74 28 20 28 69 73 53 61 76 65 70 6e 74 26 7e rt( (isSavepnt&~
121bb 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 1)==0 ); /
121bc 2a 20 69 73 53 61 76 65 70 6e 74 20 69 73 20 30 * isSavepnt is 0
121bd 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 or 1 */. asser
121be 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c t( isMainJrnl ||
121bf 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 20 2f 2a pDone ); /*
121c0 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 20 75 73 pDone always us
121c1 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 ed on sub-journa
121c2 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ls */. assert(
121c3 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 44 6f isSavepnt || pDo
121c4 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 70 44 ne==0 ); /* pD
121c5 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 64 20 6f one never used o
121c6 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e 74 20 n non-savepoint
121c7 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d 20 70 50 */.. aData = pP
121c8 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
121c9 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74 61 . assert( aData
121ca 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ); /* T
121cb 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73 74 emp storage must
121cc 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
121cd 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a en allocated */.
121ce 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 . /* Read the p
121cf 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 age number and p
121d0 61 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 age data from th
121d1 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 e journal or sub
121d2 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 -journal. ** fi
121d3 6c 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 le. Return an er
121d4 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 ror code to the
121d5 63 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 caller if an IO
121d6 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 error occurs..
121d7 2a 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 */. jfd = isMai
121d8 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e nJrnl ? pPager->
121d9 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a jfd : pPager->sj
121da 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 fd;. rc = read3
121db 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 2bits(jfd, *pOff
121dc 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 set, &pgno);. i
121dd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
121de 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
121df 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
121e0 61 64 28 6a 66 64 2c 20 28 75 38 2a 29 61 44 61 ad(jfd, (u8*)aDa
121e1 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
121e2 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73 65 74 29 Size, (*pOffset)
121e3 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 +4);. if( rc!=S
121e4 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
121e5 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66 73 65 74 n rc;. *pOffset
121e6 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 += pPager->page
121e7 53 69 7a 65 20 2b 20 34 20 2b 20 69 73 4d 61 69 Size + 4 + isMai
121e8 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 nJrnl*4;.. /* S
121e9 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f anity checking o
121ea 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69 n the page. Thi
121eb 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 s is more import
121ec 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 67 69 ant that I origi
121ed 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 nally. ** thoug
121ee 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20 ht. If a power
121ef 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 failure occurs w
121f0 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c hile the journal
121f1 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 is being writte
121f2 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 n,. ** it could
121f3 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64 cause invalid d
121f4 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 ata to be writte
121f5 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
121f6 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a al. We need to.
121f7 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73 ** detect this
121f8 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77 invalid data (w
121f9 69 74 68 20 68 69 67 68 20 70 72 6f 62 61 62 69 ith high probabi
121fa 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 lity) and ignore
121fb 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
121fc 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d pgno==0 || pgno=
121fd 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 =PAGER_MJ_PGNO(p
121fe 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 61 73 Pager) ){. as
121ff 73 65 72 74 28 20 21 69 73 53 61 76 65 70 6e 74 sert( !isSavepnt
12200 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 );. return S
12201 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a QLITE_DONE;. }.
12202 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67 6e 6f if( pgno>(Pgno
12203 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 )pPager->dbSize
12204 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 || sqlite3Bitvec
12205 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f Test(pDone, pgno
12206 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
12207 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
12208 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 if( isMainJrnl
12209 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 ){. rc = read
1220a 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f 32bits(jfd, (*pO
1220b 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d ffset)-4, &cksum
1220c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
1220d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 return rc;. i
1220e 66 28 20 21 69 73 53 61 76 65 70 6e 74 20 26 26 f( !isSavepnt &&
1220f 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 pager_cksum(pPa
12210 67 65 72 2c 20 28 75 38 2a 29 61 44 61 74 61 29 ger, (u8*)aData)
12211 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 !=cksum ){.
12212 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
12213 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ONE;. }. }..
12214 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26 20 28 if( pDone && (
12215 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 rc = sqlite3Bitv
12216 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e ecSet(pDone, pgn
12217 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 o))!=SQLITE_OK )
12218 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
12219 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1221a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
1221b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c AGER_RESERVED ||
1221c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
1221d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
1221e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1221f 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53 45 pager is in RESE
12220 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e RVED state, then
12221 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 there must be a
12222 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20 copy of this.
12223 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 70 ** page in the p
12224 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74 ager cache. In t
12225 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75 70 his case just up
12226 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 63 date the pager c
12227 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 ache,. ** not t
12228 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12229 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c 65 . The page is le
1222a 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 ft marked dirty
1222b 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 in this case..
1222c 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 **. ** An excep
1222d 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 tion to the abov
1222e 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64 e rule: If the d
1222f 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f atabase is in no
12230 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 -sync mode. **
12231 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f and a page is mo
12232 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e ved during an in
12233 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
12234 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d then the page m
12235 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 ay. ** not be i
12236 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
12237 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d e. Later: if a m
12238 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 alloc() or IO er
12239 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 ror occurs. **
1223a 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 during a Movepag
1223b 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 e() call, then t
1223c 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 he page may not
1223d 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a be in the cache.
1223e 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 ** either. So
1223f 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 the condition de
12240 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 61 scribed in the a
12241 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20 69 bove paragraph i
12242 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 s not. ** asser
12243 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 t()able.. **.
12244 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49 ** If in EXCLUSI
12245 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77 VE state, then w
12246 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 e update the pag
12247 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20 65 er cache if it e
12248 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 xists. ** and t
12249 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 he main file. Th
1224a 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d e page is then m
1224b 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e arked not dirty.
1224c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 . **. ** Ticke
1224d 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 74 t #1171: The st
1224e 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
1224f 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 might contain pa
12250 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 ge content that
12251 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e is. ** differen
12252 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 t from the page
12253 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73 content at the s
12254 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
12255 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 saction.. ** Th
12256 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 is occurs when a
12257 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 page is changed
12258 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 prior to the st
12259 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65 art of a stateme
1225a 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 nt. ** then cha
1225b 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 69 nged again withi
1225c 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e n the statement.
1225d 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 When rolling b
1225e 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 ack such a. **
1225f 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 statement we mus
12260 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 t not write to t
12261 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
12262 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b base unless we k
12263 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 now. ** for cer
12264 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e tain that origin
12265 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 al page contents
12266 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f are synced into
12267 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 the main rollba
12268 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e ck. ** journal.
12269 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 Otherwise, a p
1226a 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 ower loss might
1226b 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64 leave modified d
1226c 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 ata in the. **
1226d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
1226e 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69 thout an entry i
1226f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
12270 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a ournal that can.
12271 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65 ** restore the
12272 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 database to its
12273 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 original form.
12274 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 Two conditions
12275 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 must be. ** met
12276 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 before writing
12277 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
12278 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64 files. (1) the d
12279 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a atabase must be.
1227a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 ** locked. (2
1227b 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 ) we know that t
1227c 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
1227d 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c content is full
1227e 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e y synced. ** in
1227f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12280 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 l either because
12281 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
12282 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73 in cache or els
12283 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 e. ** the page
12284 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 is marked as nee
12285 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 dSync==0.. **.
12286 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 ** 2008-04-14:
12287 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 When attempting
12288 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72 to vacuum a cor
12289 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1228a 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 le, it. ** is p
1228b 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 ossible to fail
1228c 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 a statement on a
1228d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 64 database that d
1228e 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73 oes not yet exis
1228f 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 t.. ** Do not a
12290 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
12291 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 if database file
12292 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 has never been
12293 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 opened.. */. p
12294 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 Pg = pager_looku
12295 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b p(pPager, pgno);
12296 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 20 7c . assert( pPg |
12297 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41 | !MEMDB );. PA
12298 47 45 52 54 52 41 43 45 28 28 22 50 4c 41 59 42 GERTRACE(("PLAYB
12299 41 43 4b 20 25 64 20 70 61 67 65 20 25 64 20 68 ACK %d page %d h
1229a 61 73 68 28 25 30 38 78 29 20 25 73 5c 6e 22 2c ash(%08x) %s\n",
1229b 0a 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 . PAGE
1229c 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e RID(pPager), pgn
1229d 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 o, pager_datahas
1229e 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 h(pPager->pageSi
1229f 7a 65 2c 20 28 75 38 2a 29 61 44 61 74 61 29 2c ze, (u8*)aData),
122a0 0a 20 20 20 20 20 20 20 20 20 20 20 28 69 73 4d . (isM
122a1 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f ainJrnl?"main-jo
122a2 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75 72 urnal":"sub-jour
122a3 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69 66 nal"). ));. if
122a4 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 ( (pPager->state
122a5 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
122a6 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d 30 E). && (pPg==0
122a7 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 || 0==(pPg->fla
122a8 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
122a9 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70 65 NC)). && isOpe
122aa 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 20 n(pPager->fd).
122ab 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20 20 && !isUnsync.
122ac 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 20 ){. i64 ofst
122ad 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 = (pgno-1)*(i64)
122ae 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
122af 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
122b0 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
122b1 2d 3e 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61 ->fd, (u8*)aData
122b2 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
122b3 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 69 ze, ofst);. i
122b4 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e f( pgno>pPager->
122b5 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 dbFileSize ){.
122b6 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
122b7 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
122b8 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 }. if( pPa
122b9 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b 0a ger->pBackup ){.
122ba 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 CODEC1(pPa
122bb 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f ger, aData, pgno
122bc 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e , 3, rc=SQLITE_N
122bd 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71 6c OMEM);. sql
122be 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 ite3BackupUpdate
122bf 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 (pPager->pBackup
122c0 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 61 44 61 , pgno, (u8*)aDa
122c1 74 61 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43 ta);. CODEC
122c2 32 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 2(pPager, aData,
122c3 20 70 67 6e 6f 2c 20 37 2c 20 72 63 3d 53 51 4c pgno, 7, rc=SQL
122c4 49 54 45 5f 4e 4f 4d 45 4d 2c 20 61 44 61 74 61 ITE_NOMEM, aData
122c5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
122c6 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c if( !isMainJrnl
122c7 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 && pPg==0 ){.
122c8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
122c9 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 a rollback of a
122ca 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61 savepoint and da
122cb 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74 ta was not writt
122cc 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 en to. ** the
122cd 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 database and th
122ce 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
122cf 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69 -memory, there i
122d0 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20 s a potential.
122d1 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68 ** problem. Wh
122d2 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20 6e en the page is n
122d3 65 78 74 20 66 65 74 63 68 65 64 20 62 79 20 74 ext fetched by t
122d4 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c he b-tree layer,
122d5 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c it . ** will
122d6 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 be read from th
122d7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
122d8 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 which may or ma
122d9 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a y not be . **
122da 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a current. . *
122db 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 *. ** There a
122dc 72 65 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 re a couple of d
122dd 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 74 68 ifferent ways th
122de 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41 is can happen. A
122df 6c 6c 20 61 72 65 20 71 75 69 74 65 0a 20 20 20 ll are quite.
122e0 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65 ** obscure. Whe
122e1 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e n running in syn
122e2 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74 chronous mode, t
122e3 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 his can only hap
122e4 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74 pen . ** if t
122e5 68 65 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68 he page is on th
122e6 65 20 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74 e free-list at t
122e7 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
122e8 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
122e9 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74 n. ** populat
122ea 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75 ed, then moved u
122eb 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 sing sqlite3Page
122ec 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20 rMovepage()..
122ed 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
122ee 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64 olution is to ad
122ef 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 d an in-memory p
122f0 61 67 65 20 74 6f 20 74 68 65 20 63 61 63 68 65 age to the cache
122f1 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20 containing.
122f2 2a 2a 20 74 68 65 20 64 61 74 61 20 6a 75 73 74 ** the data just
122f3 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 read from the s
122f4 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b ub-journal. Mark
122f5 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 the page as dir
122f6 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 ty . ** and i
122f7 66 20 74 68 65 20 70 61 67 65 72 20 72 65 71 75 f the pager requ
122f8 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 ires a journal-s
122f9 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 ync, then mark t
122fa 68 65 20 70 61 67 65 20 61 73 20 0a 20 20 20 20 he page as .
122fb 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a ** requiring a j
122fc 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f ournal-sync befo
122fd 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e re it is written
122fe 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
122ff 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 29 ert( isSavepnt )
12300 3b 0a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 ;. if( (rc =
12301 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
12302 69 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f ire(pPager, pgno
12303 2c 20 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c , &pPg, 1))!=SQL
12304 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12305 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
12306 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 . pPg->flags
12307 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52 &= ~PGHDR_NEED_R
12308 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 EAD;. sqlite3
12309 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 PcacheMakeDirty(
1230a 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pPg);. }. if(
1230b 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f pPg ){. /* No
1230c 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65 page should eve
1230d 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20 r be explicitly
1230e 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74 rolled back that
1230f 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65 is in use, exce
12310 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61 pt. ** for pa
12311 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 65 ge 1 which is he
12312 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64 ld in use in ord
12313 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c er to keep the l
12314 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a ock on the. *
12315 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 76 * database activ
12316 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 20 e. However such
12317 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f a page may be ro
12318 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 lled back as a r
12319 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20 esult. ** of
1231a 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f an internal erro
1231b 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 r resulting in a
1231c 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c n automatic call
1231d 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 to. ** sqlit
1231e 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
1231f 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f ).. */. vo
12320 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70 id *pData;. p
12321 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 Data = pPg->pDat
12322 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44 a;. memcpy(pD
12323 61 74 61 2c 20 28 75 38 2a 29 61 44 61 74 61 2c ata, (u8*)aData,
12324 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
12325 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e e);. pPager->
12326 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a xReiniter(pPg);.
12327 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 if( isMainJr
12328 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e nl && (!isSavepn
12329 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 t || *pOffset<=p
1232a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
1232b 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 r) ){. /* I
1232c 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f f the contents o
1232d 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65 f this page were
1232e 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 just restored f
1232f 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20 rom the main .
12330 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
12331 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f ile, then its co
12332 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73 ntent must be as
12333 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20 they were when
12334 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72 the . ** tr
12335 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69 ansaction was fi
12336 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 rst opened. In t
12337 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20 his case we can
12338 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20 mark the page.
12339 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c ** as clean,
1233a 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c since there wil
1233b 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20 l be no need to
1233c 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 write it out to
1233d 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 the.. **.
1233e 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 ** There is
1233f 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f one exception to
12340 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 this rule. If t
12341 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 he page is being
12342 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a rolled. **
12343 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 back as part of
12344 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 a savepoint (or
12345 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c statement) roll
12346 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 back from an .
12347 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 ** unsynced
12348 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d portion of the m
12349 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
1234a 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 , then it is not
1234b 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 safe. ** t
1234c 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 o mark the page
1234d 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 as clean. This i
1234e 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e s because markin
1234f 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 g the page as.
12350 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c ** clean wil
12351 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 l clear the PGHD
12352 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
12353 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 . Since the page
12354 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 is. ** alr
12355 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 eady in the jour
12356 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 nal file (record
12357 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a ed in Pager.pInJ
12358 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 ournal) and.
12359 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e ** the PGHDR_N
1235a 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 EED_SYNC flag is
1235b 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 cleared, if the
1235c 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
1235d 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 to. ** aga
1235e 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 in within this t
1235f 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 ransaction, it w
12360 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ill be marked as
12361 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 dirty but.
12362 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 ** the PGHDR_NE
12363 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c ED_SYNC flag wil
12364 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 l not be set. It
12365 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 could then pote
12366 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a ntially. **
12367 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 be written out
12368 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12369 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 e file before it
1236a 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 s journal file.
1236b 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 ** segment
1236c 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 is synced. If a
1236d 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 crash occurs dur
1236e 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 ing or following
1236f 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 this,. **
12370 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
12371 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 ion may ensue..
12372 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
12373 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
12374 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d lean(pPg);. }
12375 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
12376 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
12377 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
12378 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
12379 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f g);.#endif. /
1237a 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61 * If this was pa
1237b 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f ge 1, then resto
1237c 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 re the value of
1237d 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
1237e 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 .. ** Do this
1237f 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f before any deco
12380 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 ding. */. if(
12381 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
12382 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 memcpy(&pPager
12383 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 ->dbFileVers, &(
12384 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c (u8*)pData)[24],
12385 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
12386 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 bFileVers));.
12387 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 }.. /* Decod
12388 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 e the page just
12389 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a read from disk *
1238a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 /. CODEC1(pPa
1238b 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
1238c 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c >pgno, 3, rc=SQL
1238d 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
1238e 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
1238f 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 ease(pPg);. }.
12390 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12391 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
12392 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 Master is the na
12393 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
12394 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 ournal file. A s
12395 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ingle journal.**
12396 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 file that refer
12397 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 red to the maste
12398 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 r journal file h
12399 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c as just been rol
1239a 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 led back..** Thi
1239b 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
1239c 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
1239d 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 le to delete the
1239e 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1239f 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 file,.** and doe
123a0 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a s so if it is..*
123a1 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d *.** Argument zM
123a2 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 aster may point
123a3 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 to Pager.pTmpSpa
123a4 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 ce. So that buff
123a5 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 er is not .** av
123a6 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 ailable for use
123a7 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 within this func
123a8 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e tion..**.** When
123a9 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
123aa 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 l file is create
123ab 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74 d, it is populat
123ac 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 ed with the name
123ad 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 s .** of all of
123ae 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 its child journa
123af 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e ls, one after an
123b0 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 other, formatted
123b1 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e as utf-8 .** en
123b2 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20 coded text. The
123b3 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c end of each chil
123b4 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 d journal file i
123b5 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20 s marked with a
123b6 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
123b7 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20 or byte (0x00).
123b8 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20 i.e. the entire
123b9 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 contents of a ma
123ba 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
123bb 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73 file for a trans
123bc 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 action involving
123bd 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d two databases m
123be 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 ight be:.**.**
123bf 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 "/home/bill/a.d
123c0 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f b-journal\x00/ho
123c1 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 me/bill/b.db-jou
123c2 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 rnal\x00".**.**
123c3 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c A master journal
123c4 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 file may only b
123c5 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 e deleted once a
123c6 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 ll of its child
123c7 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 .** journals hav
123c8 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 e been rolled ba
123c9 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ck..**.** This f
123ca 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68 unction reads th
123cb 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
123cc 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c e master-journal
123cd 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d file into .** m
123ce 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 emory and loops
123cf 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20 through each of
123d0 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 the child journa
123d1 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 l names. For.**
123d2 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e each child journ
123d3 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66 al, it checks if
123d4 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 :.**.** * if t
123d5 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c he child journal
123d6 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20 exists, and if
123d7 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 so.** * if the
123d8 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
123d9 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65 ontains a refere
123da 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f nce to master jo
123db 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 urnal .** fi
123dc 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a le zMaster.**.**
123dd 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 If a child jour
123de 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 nal can be found
123df 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f that matches bo
123e0 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72 th of the criter
123e1 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 ia.** above, thi
123e2 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
123e3 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 ns without doing
123e4 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 anything. Other
123e5 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 wise, if.** no s
123e6 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 uch child journa
123e7 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 l can be found,
123e8 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20 file zMaster is
123e9 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 deleted from.**
123ea 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
123eb 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
123ec 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 elete()..**.** I
123ed 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 f an IO error wi
123ee 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 thin this functi
123ef 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 on, an error cod
123f0 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 e is returned. T
123f1 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
123f2 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 allocates memory
123f3 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 by calling sqli
123f4 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 te3Malloc(). If
123f5 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a an allocation.**
123f6 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e fails, SQLITE_N
123f7 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
123f8 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
123f9 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 no IO or malloc
123fa 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 errors .** occur
123fb 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
123fc 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
123fd 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69 ODO: This functi
123fe 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 on allocates a s
123ff 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d ingle block of m
12400 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a emory to load.**
12401 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
12402 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
12403 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
12404 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a This could be.*
12405 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 * a couple of ki
12406 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 lobytes or so -
12407 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 potentially larg
12408 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 er than the page
12409 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 .** size..*/.st
1240a 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64 atic int pager_d
1240b 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a elmaster(Pager *
1240c 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
1240d 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
1240e 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1240f 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
12410 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 ;. int rc;
12411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12412 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12413 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
12414 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d pMaster; /* M
12415 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a alloc'd master-j
12416 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
12417 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 riptor */. sqli
12418 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e te3_file *pJourn
12419 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 al; /* Malloc'
1241a 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 d child-journal
1241b 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
1241c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
1241d 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f erJournal = 0; /
1241e 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 * Contents of ma
1241f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
12420 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 e */. i64 nMast
12421 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 erJournal;
12422 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 /* Size of mast
12423 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
12424 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 */.. /* Allocat
12425 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68 e space for both
12426 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e the pJournal an
12427 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64 d pMaster file d
12428 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a escriptors.. **
12429 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
1242a 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 open the master
1242b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
1242c 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 reading.. */.
1242d 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 pMaster = (sqli
1242e 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
1242f 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 e3MallocZero(pVf
12430 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 s->szOsFile * 2)
12431 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 ;. pJournal = (
12432 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 sqlite3_file *)(
12433 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 ((u8 *)pMaster)
12434 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 + pVfs->szOsFile
12435 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 );. if( !pMaste
12436 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 r ){. rc = SQ
12437 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
12438 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 lse{. const i
12439 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 nt flags = (SQLI
1243a 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
1243b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
1243c 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 TER_JOURNAL);.
1243d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1243e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74 Open(pVfs, zMast
1243f 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61 er, pMaster, fla
12440 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 gs, 0);. }. if
12441 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12442 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
12443 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 _out;.. rc = sq
12444 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
12445 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 pMaster, &nMaste
12446 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 rJournal);. if(
12447 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12448 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
12449 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 out;.. if( nMas
1244a 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a terJournal>0 ){.
1244b 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e char *zJourn
1244c 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d al;. char *zM
1244d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 asterPtr = 0;.
1244e 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 int nMasterPtr
1244f 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e = pVfs->mxPathn
12450 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c ame+1;.. /* L
12451 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d oad the entire m
12452 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
12453 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 le into space ob
12454 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20 tained from.
12455 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
12456 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20 c() and pointed
12457 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75 to by zMasterJou
12458 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 rnal. . */.
12459 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c zMasterJournal
1245a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
1245b 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 ((int)nMasterJou
1245c 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 rnal + nMasterPt
1245d 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 r + 1);. if(
1245e 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 !zMasterJournal
1245f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
12460 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
12461 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
12462 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
12463 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d zMasterPtr = &zM
12464 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 asterJournal[nMa
12465 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a sterJournal+1];.
12466 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12467 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 OsRead(pMaster,
12468 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 zMasterJournal,
12469 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 (int)nMasterJour
1246a 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 nal, 0);. if(
1246b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1246c 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
1246d 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 out;. zMaster
1246e 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
1246f 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20 ournal] = 0;..
12470 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 zJournal = zMa
12471 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 sterJournal;.
12472 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 while( (zJourna
12473 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l-zMasterJournal
12474 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c )<nMasterJournal
12475 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 ){. int ex
12476 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d ists;. rc =
12477 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
12478 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c (pVfs, zJournal,
12479 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
1247a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b XISTS, &exists);
1247b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1247c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1247d 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
1247e 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
1247f 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 if( exists
12480 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f ){. /* O
12481 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ne of the journa
12482 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ls pointed to by
12483 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
12484 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 nal exists..
12485 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 ** Open it a
12486 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 nd check if it p
12487 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 oints at the mas
12488 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a ter journal. If.
12489 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 ** so, r
1248a 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 eturn without de
1248b 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 leting the maste
1248c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
1248d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1248e 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 int c;.
1248f 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 int flags = (S
12490 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
12491 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f NLY|SQLITE_OPEN_
12492 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 MAIN_JOURNAL);.
12493 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
12494 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
12495 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e zJournal, pJourn
12496 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 al, flags, 0);.
12497 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
12498 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12499 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
1249a 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
1249b 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 }.. rc
1249c 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
1249d 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d nal(pJournal, zM
1249e 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 asterPtr, nMaste
1249f 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 rPtr);. s
124a0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a qlite3OsClose(pJ
124a1 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 ournal);.
124a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
124a3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
124a4 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
124a5 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
124a6 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 c = zMast
124a7 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 erPtr[0]!=0 && s
124a8 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 trcmp(zMasterPtr
124a9 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 , zMaster)==0;.
124aa 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a if( c ){.
124ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 /* We
124ac 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f have a match. Do
124ad 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 not delete the
124ae 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
124af 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ile. */.
124b0 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
124b1 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
124b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a }. zJ
124b3 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 ournal += (sqlit
124b4 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 e3Strlen30(zJour
124b5 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 nal)+1);. }.
124b6 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c }. . rc = sql
124b7 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
124b8 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a s, zMaster, 0);.
124b9 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a .delmaster_out:.
124ba 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 if( zMasterJou
124bb 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 rnal ){. sqli
124bc 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 te3_free(zMaster
124bd 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a Journal);. } .
124be 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b if( pMaster ){
124bf 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
124c0 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
124c1 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
124c2 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 n(pJournal) );.
124c3 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
124c4 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 e(pMaster);. re
124c5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
124c6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
124c7 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
124c8 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 ge the actual si
124c9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
124ca 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 se .** file in t
124cb 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 he file-system.
124cc 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
124cd 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e s when committin
124ce 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
124cf 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 .** or rolling b
124d0 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f ack a transactio
124d1 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c n (including rol
124d2 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d ling back a hot-
124d3 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 journal)..**.**
124d4 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 If the main data
124d5 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 base file is not
124d6 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 open, or an exc
124d7 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e lusive lock is n
124d8 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 ot.** held, this
124d9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
124da 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
124db 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
124dc 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e file is.** chan
124dd 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 ged to nPage pag
124de 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 es (nPage*pPager
124df 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
124e0 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a ). If the file.*
124e1 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 * on disk is cur
124e2 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 rently larger th
124e3 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 an nPage pages,
124e4 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 then use the VFS
124e5 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 .** xTruncate()
124e6 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 method to trunca
124e7 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c te it..**.** Or,
124e8 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 it might might
124e9 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
124ea 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
124eb 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 k is smaller tha
124ec 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 n .** nPage page
124ed 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e s. Some operatin
124ee 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 g system impleme
124ef 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 ntations can get
124f0 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a confused if .**
124f1 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e you try to trun
124f2 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 cate a file to s
124f3 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 ome size that is
124f4 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 larger than it
124f5 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 .** currently is
124f6 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 , so detect this
124f7 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 case and write
124f8 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 a single zero by
124f9 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e te to .** the en
124fa 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c d of the new fil
124fb 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a e instead..**.**
124fc 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
124fd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
124fe 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
124ff 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f occurs while mo
12500 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 difying.** the d
12501 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 atabase file, re
12502 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 turn the error c
12503 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ode to the calle
12504 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
12505 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
12506 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
12507 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e gno nPage){. in
12508 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
12509 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
1250a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
1250b 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e LUSIVE && isOpen
1250c 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
1250d 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 i64 currentS
1250e 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 ize, newSize;.
1250f 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 /* TODO: Is it
12510 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 safe to use Pag
12511 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 er.dbFileSize he
12512 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 re? */. rc =
12513 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
12514 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 e(pPager->fd, &c
12515 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 urrentSize);.
12516 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 newSize = pPage
12517 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 r->pageSize*(i64
12518 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 )nPage;. if(
12519 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1251a 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 currentSize!=ne
1251b 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 wSize ){. i
1251c 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e f( currentSize>n
1251d 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 ewSize ){.
1251e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1251f 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
12520 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 >fd, newSize);.
12521 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12522 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12523 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
12524 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 fd, "", 1, newSi
12525 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ze-1);. }.
12526 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
12527 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12528 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
12529 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
1252a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1252b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1252c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 /*.** Set the va
1252d 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 lue of the Pager
1252e 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 .sectorSize vari
1252f 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 able for the giv
12530 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 en.** pager base
12531 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 d on the value r
12532 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 eturned by the x
12533 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f SectorSize metho
12534 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e d.** of the open
12535 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12536 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 The sector size
12537 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 will be used use
12538 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 d .** to determi
12539 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 ne the size and
1253a 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 alignment of jou
1253b 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 rnal header and
1253c 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
1253d 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 al pointers with
1253e 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e in created journ
1253f 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 al files..**.**
12540 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 For temporary fi
12541 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 les the effectiv
12542 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 e sector size is
12543 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
12544 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 s..**.** Otherwi
12545 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 se, for non-temp
12546 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 orary files, the
12547 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
12548 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 r size is.** the
12549 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
1254a 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 by the xSectorSi
1254b 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e ze() method roun
1254c 64 65 64 20 75 70 20 74 6f 20 33 32 20 69 66 0a ded up to 32 if.
1254d 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 ** it is less th
1254e 61 6e 20 33 32 2c 20 6f 72 20 72 6f 75 6e 64 65 an 32, or rounde
1254f 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45 d down to MAX_SE
12550 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 74 0a CTOR_SIZE if it.
12551 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ** is greater th
12552 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 an MAX_SECTOR_SI
12553 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ZE..*/.static vo
12554 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 id setSectorSize
12555 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
12556 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 . assert( isOpe
12557 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c n(pPager->fd) ||
12558 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
12559 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 e );.. if( !pPa
1255a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
1255b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 . /* Sector s
1255c 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 ize doesn't matt
1255d 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 er for temporary
1255e 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 68 files. Also, th
1255f 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6d 61 e file. ** ma
12560 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 y not have been
12561 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20 77 opened yet, in w
12562 68 69 63 68 20 63 61 73 65 20 74 68 65 20 4f 73 hich case the Os
12563 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 SectorSize().
12564 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 73 65 ** call will se
12565 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 gfault.. */.
12566 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
12567 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f rSize = sqlite3O
12568 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 sSectorSize(pPag
12569 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 20 69 er->fd);. }. i
1256a 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f f( pPager->secto
1256b 72 53 69 7a 65 3c 33 32 20 29 7b 0a 20 20 20 20 rSize<32 ){.
1256c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
1256d 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20 ze = 512;. }.
1256e 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 if( pPager->sect
1256f 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f orSize>MAX_SECTO
12570 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 R_SIZE ){. as
12571 73 65 72 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 sert( MAX_SECTOR
12572 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 _SIZE>=512 );.
12573 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 pPager->sector
12574 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f Size = MAX_SECTO
12575 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f R_SIZE;. }.}../
12576 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 *.** Playback th
12577 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 e journal and th
12578 75 73 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 us restore the d
12579 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 0a atabase file to.
1257a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 69 74 20 ** the state it
1257b 77 61 73 20 69 6e 20 62 65 66 6f 72 65 20 77 65 was in before we
1257c 20 73 74 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 started making
1257d 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a changes. .**.**
1257e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
1257f 65 20 66 6f 72 6d 61 74 20 69 73 20 61 73 20 66 e format is as f
12580 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 ollows: .**.**
12581 28 31 29 20 20 38 20 62 79 74 65 20 70 72 65 66 (1) 8 byte pref
12582 69 78 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 61 ix. A copy of a
12583 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a JournalMagic[]..
12584 2a 2a 20 20 28 32 29 20 20 34 20 62 79 74 65 20 ** (2) 4 byte
12585 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
12586 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
12587 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20 number of valid
12588 70 61 67 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 page records.**
12589 20 20 20 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 in the jou
1258a 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 73 20 76 rnal. If this v
1258b 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66 66 alue is 0xffffff
1258c 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 ff, then compute
1258d 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 the.** nu
1258e 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 mber of page rec
1258f 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f ords from the jo
12590 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 urnal size..**
12591 28 33 29 20 20 34 20 62 79 74 65 20 62 69 67 2d (3) 4 byte big-
12592 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 endian integer w
12593 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 69 74 hich is the init
12594 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ial value for th
12595 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 e .** sani
12596 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 ty checksum..**
12597 20 28 34 29 20 20 34 20 62 79 74 65 20 69 6e 74 (4) 4 byte int
12598 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
12599 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
1259a 73 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 s to truncate th
1259b 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62 e.** datab
1259c 61 73 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20 ase to during a
1259d 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 rollback..** (5
1259e 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
1259f 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
125a0 63 68 20 69 73 20 74 68 65 20 73 65 63 74 6f 72 ch is the sector
125a1 20 73 69 7a 65 2e 20 20 54 68 65 20 68 65 61 64 size. The head
125a2 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 73 20 74 er.** is t
125a3 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 69 his many bytes i
125a4 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20 n size..** (6)
125a5 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 4 byte big-endi
125a6 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
125a7 20 69 73 20 74 68 65 20 70 61 67 65 20 73 69 7a is the page siz
125a8 65 2e 0a 2a 2a 20 20 28 37 29 20 20 7a 65 72 6f e..** (7) zero
125a9 20 70 61 64 64 69 6e 67 20 6f 75 74 20 74 6f 20 padding out to
125aa 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 the next sector
125ab 73 69 7a 65 2e 0a 2a 2a 20 20 28 38 29 20 20 5a size..** (8) Z
125ac 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 ero or more page
125ad 73 20 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 s instances, eac
125ae 68 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a h as follows:.**
125af 20 20 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 + 4 byt
125b0 65 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a e page number..*
125b1 2a 20 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 * + pPag
125b2 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 er->pageSize byt
125b3 65 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 es of data..**
125b4 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 + 4 byte
125b5 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 checksum.**.** W
125b6 68 65 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 hen we speak of
125b7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
125b8 65 72 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 er, we mean the
125b9 66 69 72 73 74 20 37 20 69 74 65 6d 73 20 61 62 first 7 items ab
125ba 6f 76 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 ove..** Each ent
125bb 72 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ry in the journa
125bc 6c 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 l is an instance
125bd 20 6f 66 20 74 68 65 20 38 74 68 20 69 74 65 6d of the 8th item
125be 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 ..**.** Call the
125bf 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 value from the
125c0 73 65 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e second bullet "n
125c1 52 65 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 Rec". nRec is t
125c2 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
125c3 76 61 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 valid page entri
125c4 65 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 es in the journa
125c5 6c 2e 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 l. In most case
125c6 73 2c 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 s, you can compu
125c7 74 65 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 te the.** value
125c8 6f 66 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 of nRec from the
125c9 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 size of the jou
125ca 72 6e 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 rnal file. But
125cb 69 66 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 if a power.** fa
125cc 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 ilure occurred w
125cd 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c hile the journal
125ce 20 77 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 was being writt
125cf 65 6e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 en, it could be
125d0 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 the.** case that
125d1 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
125d2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
125d3 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 d already been i
125d4 6e 63 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 ncreased but.**
125d5 74 68 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 the extra entrie
125d6 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 s had not yet ma
125d7 64 65 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 de it safely to
125d8 64 69 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 disk. In such a
125d9 20 63 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 case,.** the va
125da 6c 75 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 lue of nRec comp
125db 75 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 uted from the fi
125dc 6c 65 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 le size would be
125dd 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 too large. For
125de 0a 2a 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c .** that reason,
125df 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 we always use t
125e0 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e he nRec value in
125e1 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a the header..**.
125e2 2a 2a 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 ** If the nRec v
125e3 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66 66 alue is 0xffffff
125e4 66 66 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 ff it means that
125e5 20 6e 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 nRec should be
125e6 63 6f 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d computed.** from
125e7 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 the file size.
125e8 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 This value is u
125e9 73 65 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 sed when the use
125ea 72 20 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a r selects the.**
125eb 20 6e 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 no-sync option
125ec 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e for the journal.
125ed 20 20 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 A power failur
125ee 65 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 e could lead to
125ef 63 6f 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e corruption.** in
125f0 20 74 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 this case. But
125f1 20 66 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 for things like
125f2 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
125f3 20 28 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a (which will be.
125f4 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 ** deleted when
125f5 74 68 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 the power is res
125f6 74 6f 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 tored) we don't
125f7 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 care. .**.** If
125f8 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 the file opened
125f9 20 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 as the journal
125fa 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 file is not a we
125fb 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 ll-formed.** jou
125fc 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 rnal file then a
125fd 6c 6c 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 ll pages up to t
125fe 68 65 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 he first corrupt
125ff 65 64 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c ed page are roll
12600 65 64 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e ed.** back (or n
12601 6f 20 70 61 67 65 73 20 69 66 20 74 68 65 20 6a o pages if the j
12602 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 ournal header is
12603 20 63 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 corrupted). The
12604 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
12605 20 69 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 is then deleted
12606 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 and SQLITE_OK r
12607 65 74 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 eturned, just as
12608 20 69 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f if no corruptio
12609 6e 20 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e n had.** been en
1260a 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a countered..**.**
1260b 20 49 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 If an I/O or ma
1260c 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 lloc() error occ
1260d 75 72 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c urs, the journal
1260e 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c -file is not del
1260f 65 74 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 eted.** and an e
12610 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
12611 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
12612 20 69 73 48 6f 74 20 70 61 72 61 6d 65 74 65 72 isHot parameter
12613 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
12614 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f we are trying to
12615 20 72 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75 72 rollback a jour
12616 6e 61 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67 68 nal.** that migh
12617 74 20 62 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e t be a hot journ
12618 61 6c 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75 6c al. Or, it coul
12619 64 20 62 65 20 74 68 61 74 20 74 68 65 20 6a 6f d be that the jo
1261a 75 72 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72 65 urnal is .** pre
1261b 73 65 72 76 65 64 20 62 65 63 61 75 73 65 20 6f served because o
1261c 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 f JOURNALMODE_PE
1261d 52 53 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41 4c RSIST or JOURNAL
1261e 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a 2a MODE_TRUNCATE..*
1261f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
12620 20 72 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c 20 really is hot,
12621 72 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20 reset the pager
12622 63 61 63 68 65 20 70 72 69 6f 72 20 72 6f 6c 6c cache prior roll
12623 69 6e 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 ing.** back any
12624 63 6f 6e 74 65 6e 74 2e 20 20 49 66 20 74 68 65 content. If the
12625 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72 65 journal is mere
12626 6c 79 20 70 65 72 73 69 73 74 65 6e 74 2c 20 6e ly persistent, n
12627 6f 20 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e 65 o reset is.** ne
12628 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
12629 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 int pager_playba
1262a 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
1262b 2c 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 , int isHot){.
1262c 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1262d 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
1262e 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 ;. i64 szJ;
1262f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12630 53 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 Size of the jour
12631 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 nal file in byte
12632 73 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b s */. u32 nRec;
12633 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12634 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 /* Number of Rec
12635 6f 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 ords in the jour
12636 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 nal */. u32 u;
12637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12638 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f /* Unsigned lo
12639 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1263a 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 Pgno mxPg = 0;
1263b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
1263c 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c of the original
1263d 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a file in pages *
1263e 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
1263f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12640 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 Result code of a
12641 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 subroutine */.
12642 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 int res = 1;
12643 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
12644 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
12645 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 qlite3OsAccess()
12646 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 */. char *zMas
12647 74 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f ter = 0; /
12648 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 * Name of master
12649 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 journal file if
1264a 20 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 any */. int ne
1264b 65 64 50 61 67 65 72 52 65 73 65 74 3b 20 20 20 edPagerReset;
1264c 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 65 /* True to re
1264d 73 65 74 20 70 61 67 65 20 70 72 69 6f 72 20 74 set page prior t
1264e 6f 20 66 69 72 73 74 20 70 61 67 65 20 72 6f 6c o first page rol
1264f 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 46 lback */.. /* F
12650 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 igure out how ma
12651 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 ny records are i
12652 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 n the journal.
12653 41 62 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20 Abort early if.
12654 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
12655 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 is empty.. */.
12656 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
12657 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
12658 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12659 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
1265a 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 >jfd, &szJ);. i
1265b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1265c 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 || szJ==0 ){.
1265d 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 goto end_playb
1265e 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 ack;. }.. /* R
1265f 65 61 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a ead the master j
12660 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d ournal name from
12661 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 the journal, if
12662 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a it is present..
12663 20 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 ** If a master
12664 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
12665 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c me is specified,
12666 20 62 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 but the file is
12667 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e not. ** presen
12668 74 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 t on disk, then
12669 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e the journal is n
1266a 6f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 ot hot and does
1266b 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 not need to be.
1266c 20 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e ** played back.
1266d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a . **. ** TODO:
1266e 20 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 Technically the
1266f 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e following is an
12670 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 69 error because i
12671 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20 t assumes that.
12672 20 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65 72 ** buffer Pager
12673 2e 70 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d .pTmpSpace is (m
12674 78 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74 xPathname+1) byt
12675 65 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e es or larger. i.
12676 65 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50 e. that. ** (pP
12677 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e ager->pageSize >
12678 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e = pPager->pVfs->
12679 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55 mxPathname+1). U
1267a 73 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a sing os_unix.c,.
1267b 20 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65 ** mxPathname
1267c 20 69 73 20 35 31 32 2c 20 77 68 69 63 68 20 69 is 512, which i
1267d 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
1267e 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 e minimum allowa
1267f 62 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66 ble value. ** f
12680 6f 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a or pageSize.. *
12681 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 /. zMaster = pP
12682 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
12683 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 . rc = readMast
12684 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 erJournal(pPager
12685 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 ->jfd, zMaster,
12686 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 pPager->pVfs->mx
12687 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 Pathname+1);. i
12688 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12689 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 && zMaster[0] )
1268a 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1268b 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
1268c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 zMaster, SQLITE
1268d 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
1268e 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 &res);. }. zMa
1268f 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 ster = 0;. if(
12690 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
12691 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 !res ){. got
12692 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
12693 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }. pPager->jo
12694 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 urnalOff = 0;.
12695 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d needPagerReset =
12696 20 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68 isHot;.. /* Th
12697 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 is loop terminat
12698 65 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 61 es either when a
12699 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 readJournalHdr(
1269a 29 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72 ) or . ** pager
1269b 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
1269c 67 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e ge() call return
1269d 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 s SQLITE_DONE or
1269e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20 an IO error .
1269f 2a 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f ** occurs. . */
126a0 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 . while( 1 ){.
126a1 20 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 20 int isUnsync
126a2 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 = 0;.. /* Rea
126a3 64 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e d the next journ
126a4 61 6c 20 68 65 61 64 65 72 20 66 72 6f 6d 20 74 al header from t
126a5 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
126a6 20 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 20 If there are.
126a7 20 20 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 ** not enough
126a8 20 62 79 74 65 73 20 6c 65 66 74 20 69 6e 20 74 bytes left in t
126a9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
126aa 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 20 68 for a complete h
126ab 65 61 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a eader, or. **
126ac 20 69 74 20 69 73 20 63 6f 72 72 75 70 74 65 64 it is corrupted
126ad 2c 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73 , then a process
126ae 20 6d 75 73 74 20 6f 66 20 66 61 69 6c 65 64 20 must of failed
126af 77 68 69 6c 65 20 77 72 69 74 69 6e 67 20 69 74 while writing it
126b0 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 6e .. ** This in
126b1 64 69 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 dicates nothing
126b2 6d 6f 72 65 20 6e 65 65 64 73 20 74 6f 20 62 65 more needs to be
126b3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 rolled back..
126b4 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 */. rc = re
126b5 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 adJournalHdr(pPa
126b6 67 65 72 2c 20 69 73 48 6f 74 2c 20 73 7a 4a 2c ger, isHot, szJ,
126b7 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a &nRec, &mxPg);.
126b8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
126b9 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 TE_OK ){ .
126ba 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 if( rc==SQLITE_D
126bb 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ONE ){. r
126bc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
126bd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
126be 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
126bf 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
126c0 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66 nRec is 0xfffff
126c1 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a fff, then this j
126c2 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 ournal was creat
126c3 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a ed by a process.
126c4 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 ** working i
126c5 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 n no-sync mode.
126c6 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
126c7 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 the rest of the
126c8 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 journal. ** f
126c9 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ile consists of
126ca 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 pages, there are
126cb 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c no more journal
126cc 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 headers. Comput
126cd 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c e. ** the val
126ce 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 ue of nRec based
126cf 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 on this assumpt
126d0 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
126d1 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 if( nRec==0xffff
126d2 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 ffff ){. as
126d3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f sert( pPager->jo
126d4 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 urnalOff==JOURNA
126d5 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
126d6 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d );. nRec =
126d7 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f (int)((szJ - JO
126d8 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
126d9 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 ger))/JOURNAL_PG
126da 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 _SZ(pPager));.
126db 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e }.. /* If n
126dc 52 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 Rec is 0 and thi
126dd 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 s rollback is of
126de 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 a transaction c
126df 72 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 reated by this.
126e0 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e ** process an
126e1 64 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 d if this is the
126e2 20 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e final header in
126e3 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 the journal, th
126e4 65 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 en it means.
126e5 2a 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 ** that this par
126e6 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
126e7 20 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 was being fille
126e8 64 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 d but has not ye
126e9 74 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 t been. ** sy
126ea 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 nced to disk. C
126eb 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 ompute the numbe
126ec 72 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 r of pages based
126ed 20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e on the remainin
126ee 67 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 g. ** size of
126ef 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a the file.. *
126f0 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 *. ** The thi
126f1 72 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 rd term of the t
126f2 65 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f est was added to
126f3 20 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 fix ticket #256
126f4 35 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 5.. ** When r
126f5 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f olling back a ho
126f6 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d t journal, nRec=
126f7 3d 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 =0 always means
126f8 74 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 that the next.
126f9 20 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 ** chunk of th
126fa 65 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 e journal contai
126fb 6e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f ns zero pages to
126fc 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
126fd 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 But. ** whe
126fe 6e 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 n doing a ROLLBA
126ff 43 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d CK and the nRec=
12700 3d 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 =0 chunk is the
12701 6c 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 last chunk in.
12702 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
12703 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
12704 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 the journal migh
12705 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 t contain additi
12706 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 onal. ** page
12707 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 s that need to b
12708 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e e rolled back an
12709 64 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 d that the numbe
1270a 72 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 r of pages .
1270b 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d ** should be com
1270c 70 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 puted based on t
1270d 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1270e 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 size.. */.
1270f 20 69 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 if( nRec==0 &&
12710 21 69 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20 !isHot &&.
12711 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12712 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 lHdr+JOURNAL_HDR
12713 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 _SZ(pPager)==pPa
12714 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
12715 29 7b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 ){. nRec =
12716 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61 (int)((szJ - pPa
12717 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 ger->journalOff)
12718 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a / JOURNAL_PG_SZ
12719 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 (pPager));.
1271a 20 69 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20 isUnsync = 1;.
1271b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1271c 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
1271d 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66 72 t header read fr
1271e 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 om the journal,
1271f 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 truncate the.
12720 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
12721 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 e back to its or
12722 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 iginal size..
12723 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 */. if( pPag
12724 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
12725 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
12726 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 Pager) ){.
12727 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 rc = pager_trunc
12728 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 ate(pPager, mxPg
12729 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1272a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1272b 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 goto end_p
1272c 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d layback;. }
1272d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
1272e 62 53 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 bSize = mxPg;.
1272f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 }.. /* Copy
12730 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 original pages
12731 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e out of the journ
12732 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f al and back into
12733 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 the . ** dat
12734 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f abase file and/o
12735 72 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 r page cache..
12736 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 */. for(u=0
12737 3b 20 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a ; u<nRec; u++){.
12738 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 if( needPa
12739 67 65 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 gerReset ){.
1273a 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 pager_reset(
1273b 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 pPager);.
1273c 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 needPagerReset
1273d 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1273e 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
1273f 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
12740 70 50 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e pPager,1,isUnsyn
12741 63 2c 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e c,&pPager->journ
12742 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20 alOff,0,0);.
12743 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12744 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 _OK ){. i
12745 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f f( rc==SQLITE_DO
12746 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 NE ){.
12747 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
12748 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
12749 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 ->journalOff = s
1274a 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 zJ;. br
1274b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c eak;. }el
1274c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
1274d 20 49 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c If we are unabl
1274e 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 71 e to rollback, q
1274f 75 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 uit and return t
12750 68 65 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20 he error.
12751 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20 54 68 69 ** code. Thi
12752 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 s will cause the
12753 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 pager to enter
12754 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 0a the error state.
12755 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 ** so
12756 74 68 61 74 20 6e 6f 20 66 75 72 74 68 65 72 20 that no further
12757 68 61 72 6d 20 77 69 6c 6c 20 62 65 20 64 6f 6e harm will be don
12758 65 2e 20 20 50 65 72 68 61 70 73 20 74 68 65 20 e. Perhaps the
12759 6e 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a next. *
1275a 2a 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 6d * process to com
1275b 65 20 61 6c 6f 6e 67 20 77 69 6c 6c 20 62 65 20 e along will be
1275c 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b able to rollback
1275d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 the database..
1275e 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
1275f 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 goto end_p
12760 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 layback;.
12761 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
12762 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 . }. /*NOTREAC
12763 48 45 44 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 HED*/. assert(
12764 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 0 );..end_playba
12765 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 ck:. /* Followi
12766 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 ng a rollback, t
12767 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12768 20 73 68 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 should be back
12769 69 6e 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a in its original.
1276a 20 20 2a 2a 20 73 74 61 74 65 20 70 72 69 6f 72 ** state prior
1276b 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
1276c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1276d 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a , so invoke the.
1276e 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54 ** SQLITE_FCNT
1276f 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 66 L_DB_UNCHANGED f
12770 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68 ile-control meth
12771 6f 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 od to disable th
12772 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e e. ** assertion
12773 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73 61 that the transa
12774 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 ction counter wa
12775 73 20 6d 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f s modified.. */
12776 0a 20 20 61 73 73 65 72 74 28 0a 20 20 20 20 70 . assert(. p
12777 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 Pager->fd->pMeth
12778 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 ods==0 ||. sq
12779 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
1277a 6f 6c 28 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 ol(pPager->fd,SQ
1277b 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e LITE_FCNTL_DB_UN
1277c 43 48 41 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49 CHANGED,0)>=SQLI
1277d 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a TE_OK. );.. /*
1277e 20 49 66 20 74 68 69 73 20 70 6c 61 79 62 61 63 If this playbac
1277f 6b 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 k is happening a
12780 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 utomatically as
12781 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 a result of an I
12782 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f O or . ** mallo
12783 63 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 c error that occ
12784 75 72 72 65 64 20 61 66 74 65 72 20 74 68 65 20 urred after the
12785 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 change-counter w
12786 61 73 20 75 70 64 61 74 65 64 20 62 75 74 20 0a as updated but .
12787 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 ** before the
12788 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
12789 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 committed, then
1278a 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
1278b 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 er . ** modific
1278c 61 74 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 ation may just h
1278d 61 76 65 20 62 65 65 6e 20 72 65 76 65 72 74 65 ave been reverte
1278e 64 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65 d. If this happe
1278f 6e 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 ns in exclusive
12790 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e . ** mode, then
12791 20 73 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e subsequent tran
12792 73 61 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d sactions perform
12793 65 64 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 ed by the connec
12794 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 tion will not.
12795 2a 2a 20 75 70 64 61 74 65 20 74 68 65 20 63 68 ** update the ch
12796 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 ange-counter at
12797 61 6c 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 all. This may le
12798 61 64 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f ad to cache inco
12799 6e 73 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 nsistency. ** p
1279a 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 roblems for othe
1279b 72 20 70 72 6f 63 65 73 73 65 73 20 61 74 20 73 r processes at s
1279c 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 ome point in the
1279d 20 66 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 future. So, jus
1279e 74 0a 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 t. ** in case t
1279f 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 his has happened
127a0 2c 20 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e , clear the chan
127a1 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
127a2 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 now.. */. pPa
127a3 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 ger->changeCount
127a4 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 Done = pPager->t
127a5 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 empFile;.. if(
127a6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
127a7 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 . zMaster = p
127a8 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
127a9 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d ;. rc = readM
127aa 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 asterJournal(pPa
127ab 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 ger->jfd, zMaste
127ac 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d r, pPager->pVfs-
127ad 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a >mxPathname+1);.
127ae 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
127af 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
127b0 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
127b1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
127b2 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
127b3 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
127b4 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30 zMaster[0]!='\0
127b5 27 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 ');. testcase
127b6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
127b7 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
127b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d =SQLITE_OK && zM
127b9 61 73 74 65 72 5b 30 5d 20 26 26 20 72 65 73 20 aster[0] && res
127ba 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
127bb 72 65 20 77 61 73 20 61 20 6d 61 73 74 65 72 20 re was a master
127bc 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 69 73 journal and this
127bd 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 routine will re
127be 74 75 72 6e 20 73 75 63 63 65 73 73 2c 0a 20 20 turn success,.
127bf 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 ** see if it i
127c0 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 s possible to de
127c1 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
127c2 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a journal.. */.
127c3 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 64 rc = pager_d
127c4 65 6c 6d 61 73 74 65 72 28 70 50 61 67 65 72 2c elmaster(pPager,
127c5 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 74 zMaster);. t
127c6 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
127c7 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 ITE_OK );. }..
127c8 20 2f 2a 20 54 68 65 20 50 61 67 65 72 2e 73 65 /* The Pager.se
127c9 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c ctorSize variabl
127ca 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 e may have been
127cb 75 70 64 61 74 65 64 20 77 68 69 6c 65 20 72 6f updated while ro
127cc 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 lling. ** back
127cd 61 20 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 a journal create
127ce 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 77 d by a process w
127cf 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 ith a different
127d0 73 65 63 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a sector size. **
127d1 20 76 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74 value. Reset it
127d2 20 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 to the correct
127d3 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70 value for this p
127d4 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 rocess.. */. s
127d5 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 etSectorSize(pPa
127d6 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ger);. return r
127d7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 c;.}../*.** Play
127d8 62 61 63 6b 20 73 61 76 65 70 6f 69 6e 74 20 70 back savepoint p
127d9 53 61 76 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 Savepoint. Or, i
127da 66 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 f pSavepoint==NU
127db 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 LL, then playbac
127dc 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 k.** the entire
127dd 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
127de 69 6c 65 2e 20 54 68 65 20 63 61 73 65 20 70 53 ile. The case pS
127df 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f avepoint==NULL o
127e0 63 63 75 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61 ccurs when .** a
127e1 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d ROLLBACK TO com
127e2 6d 61 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 mand is invoked
127e3 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e 54 20 74 on a SAVEPOINT t
127e4 68 61 74 20 69 73 20 61 20 74 72 61 6e 73 61 63 hat is a transac
127e5 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69 tion .** savepoi
127e6 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 nt..**.** When p
127e7 53 61 76 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74 Savepoint is not
127e8 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61 NULL (meaning a
127e9 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e non-transaction
127ea 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a savepoint is .*
127eb 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 * being rolled b
127ec 61 63 6b 29 2c 20 74 68 65 6e 20 74 68 65 20 72 ack), then the r
127ed 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69 73 74 73 ollback consists
127ee 20 6f 66 20 75 70 20 74 6f 20 74 68 72 65 65 20 of up to three
127ef 73 74 61 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f stages,.** perfo
127f0 72 6d 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65 rmed in the orde
127f1 72 20 73 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a r specified:.**.
127f2 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 ** * Pages are
127f3 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f played back fro
127f4 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e m the main journ
127f5 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 20 62 al starting at b
127f6 79 74 65 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65 yte.** offse
127f7 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 t PagerSavepoint
127f8 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e .iOffset and con
127f9 74 69 6e 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20 tinuing to .**
127fa 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
127fb 74 2e 69 48 64 72 4f 66 66 73 65 74 2c 20 6f 72 t.iHdrOffset, or
127fc 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 to the end of t
127fd 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a he main journal.
127fe 2a 2a 20 20 20 20 20 66 69 6c 65 20 69 66 20 50 ** file if P
127ff 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 agerSavepoint.iH
12800 64 72 4f 66 66 73 65 74 20 69 73 20 7a 65 72 6f drOffset is zero
12801 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50 ..**.** * If P
12802 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 agerSavepoint.iH
12803 64 72 4f 66 66 73 65 74 20 69 73 20 6e 6f 74 20 drOffset is not
12804 7a 65 72 6f 2c 20 74 68 65 6e 20 70 61 67 65 73 zero, then pages
12805 20 61 72 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20 are played.**
12806 20 20 20 62 61 63 6b 20 73 74 61 72 74 69 6e 67 back starting
12807 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
12808 6c 20 68 65 61 64 65 72 20 69 6d 6d 65 64 69 61 l header immedia
12809 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a tely following .
1280a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 ** PagerSave
1280b 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
1280c 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 to the end of t
1280d 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
1280e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 file..**.** *
1280f 50 61 67 65 73 20 61 72 65 20 74 68 65 6e 20 70 Pages are then p
12810 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 layed back from
12811 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
12812 66 69 6c 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a file, starting.*
12813 2a 20 20 20 20 20 77 69 74 68 20 74 68 65 20 50 * with the P
12814 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 agerSavepoint.iS
12815 75 62 52 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e ubRec and contin
12816 75 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 uing to the end
12817 6f 66 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f of.** the jo
12818 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a urnal file..**.*
12819 2a 20 54 68 72 6f 75 67 68 6f 75 74 20 74 68 65 * Throughout the
1281a 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73 rollback proces
1281b 73 2c 20 65 61 63 68 20 74 69 6d 65 20 61 20 70 s, each time a p
1281c 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 age is rolled ba
1281d 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 ck, the.** corre
1281e 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 sponding bit is
1281f 73 65 74 20 69 6e 20 61 20 62 69 74 76 65 63 20 set in a bitvec
12820 73 74 72 75 63 74 75 72 65 20 28 76 61 72 69 61 structure (varia
12821 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20 74 68 65 ble pDone in the
12822 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
12823 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 68 69 73 20 on below). This
12824 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 is used to ensur
12825 65 20 74 68 61 74 20 61 20 70 61 67 65 20 69 73 e that a page is
12826 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 only.** rolled
12827 62 61 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 back the first t
12828 69 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e ime it is encoun
12829 74 65 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 tered in either
1282a 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 journal..**.** I
1282b 66 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 f pSavepoint is
1282c 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 61 67 65 73 NULL, then pages
1282d 20 61 72 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64 are only played
1282e 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d back from the m
1282f 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ain.** journal f
12830 69 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f ile. There is no
12831 20 6e 65 65 64 20 66 6f 72 20 61 20 62 69 74 76 need for a bitv
12832 65 63 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ec in this case.
12833 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72 .**.** In either
12834 20 63 61 73 65 2c 20 62 65 66 6f 72 65 20 70 6c case, before pl
12835 61 79 62 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73 ayback commences
12836 20 74 68 65 20 50 61 67 65 72 2e 64 62 53 69 7a the Pager.dbSiz
12837 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 e variable.** is
12838 20 72 65 73 65 74 20 74 6f 20 74 68 65 20 76 61 reset to the va
12839 6c 75 65 20 74 68 61 74 20 69 74 20 68 65 6c 64 lue that it held
1283a 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
1283b 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a the savepoint .
1283c 2a 2a 20 28 6f 72 20 74 72 61 6e 73 61 63 74 69 ** (or transacti
1283d 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 20 77 69 74 on). No page wit
1283e 68 20 61 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 h a page-number
1283f 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 greater than thi
12840 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c s value.** is pl
12841 61 79 65 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e ayed back. If on
12842 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 e is encountered
12843 20 69 74 20 69 73 20 73 69 6d 70 6c 79 20 73 6b it is simply sk
12844 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 ipped..*/.static
12845 20 69 6e 74 20 70 61 67 65 72 50 6c 61 79 62 61 int pagerPlayba
12846 63 6b 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 ckSavepoint(Page
12847 72 20 2a 70 50 61 67 65 72 2c 20 50 61 67 65 72 r *pPager, Pager
12848 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
12849 70 6f 69 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a point){. i64 sz
1284a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 J;
1284b 20 20 20 2f 2a 20 45 66 66 65 63 74 69 76 65 20 /* Effective
1284c 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e size of the main
1284d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 journal */. i6
1284e 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 4 iHdrOff;
1284f 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 /* End of
12850 20 66 69 72 73 74 20 73 65 67 6d 65 6e 74 20 6f first segment o
12851 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72 f main-journal r
12852 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 ecords */. int
12853 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
12854 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
12855 6f 64 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 ode */. Bitvec
12856 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20 *pDone = 0;
12857 20 20 2f 2a 20 42 69 74 76 65 63 20 74 6f 20 65 /* Bitvec to e
12858 6e 73 75 72 65 20 70 61 67 65 73 20 70 6c 61 79 nsure pages play
12859 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63 ed back only onc
1285a 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
1285b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
1285c 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a AGER_SHARED );..
1285d 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 /* Allocate a
1285e 62 69 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f bitvec to use to
1285f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f store the set o
12860 66 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 f pages rolled b
12861 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 ack */. if( pSa
12862 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 vepoint ){. p
12863 44 6f 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69 Done = sqlite3Bi
12864 74 76 65 63 43 72 65 61 74 65 28 70 53 61 76 65 tvecCreate(pSave
12865 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 point->nOrig);.
12866 20 20 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b if( !pDone ){
12867 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
12868 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
12869 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 }. }.. /* Set
1286a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a the database siz
1286b 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 e back to the va
1286c 6c 75 65 20 69 74 20 77 61 73 20 62 65 66 6f 72 lue it was befor
1286d 65 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 e the savepoint
1286e 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72 65 76 65 . ** being reve
1286f 72 74 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e rted was opened.
12870 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e . */. pPager->
12871 64 62 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f dbSize = pSavepo
12872 69 6e 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 int ? pSavepoint
12873 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72 ->nOrig : pPager
12874 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20 ->dbOrigSize;..
12875 20 2f 2a 20 55 73 65 20 70 50 61 67 65 72 2d 3e /* Use pPager->
12876 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 journalOff as th
12877 65 20 65 66 66 65 63 74 69 76 65 20 73 69 7a 65 e effective size
12878 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c of the main rol
12879 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e lback. ** journ
1287a 61 6c 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20 al. The actual
1287b 66 69 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 61 file might be la
1287c 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 rger than this i
1287d 6e 0a 20 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 n. ** PAGER_JOU
1287e 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
1287f 45 20 6f 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e E or PAGER_JOURN
12880 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e 20 ALMODE_PERSIST.
12881 20 42 75 74 20 61 6e 79 74 68 69 6e 67 0a 20 20 But anything.
12882 2a 2a 20 70 61 73 74 20 70 50 61 67 65 72 2d 3e ** past pPager->
12883 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66 journalOff is of
12884 66 2d 6c 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a f-limits to us..
12885 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61 */. szJ = pPa
12886 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
12887 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 .. /* Begin by
12888 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 rolling back rec
12889 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 ords from the ma
1288a 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 in journal start
1288b 69 6e 67 20 61 74 0a 20 20 2a 2a 20 50 61 67 65 ing at. ** Page
1288c 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 rSavepoint.iOffs
1288d 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e et and continuin
1288e 67 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f g to the next jo
1288f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 urnal header..
12890 2a 2a 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 ** There might b
12891 65 20 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 e records in the
12892 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68 main journal th
12893 61 74 20 68 61 76 65 20 61 20 70 61 67 65 20 6e at have a page n
12894 75 6d 62 65 72 0a 20 20 2a 2a 20 67 72 65 61 74 umber. ** great
12895 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 er than the curr
12896 65 6e 74 20 64 61 74 61 62 61 73 65 20 73 69 7a ent database siz
12897 65 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a e (pPager->dbSiz
12898 65 29 20 62 75 74 20 74 68 6f 73 65 0a 20 20 2a e) but those. *
12899 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 * will be skippe
1289a 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e d automatically.
1289b 20 20 50 61 67 65 73 20 61 72 65 20 61 64 64 65 Pages are adde
1289c 64 20 74 6f 20 70 44 6f 6e 65 20 61 73 20 74 68 d to pDone as th
1289d 65 79 0a 20 20 2a 2a 20 61 72 65 20 70 6c 61 79 ey. ** are play
1289e 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 ed back.. */.
1289f 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 if( pSavepoint )
128a0 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20 {. iHdrOff =
128a1 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 pSavepoint->iHdr
128a2 4f 66 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f Offset ? pSavepo
128a3 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 int->iHdrOffset
128a4 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 : szJ;. pPage
128a5 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
128a6 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 pSavepoint->iOff
128a7 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 set;. while(
128a8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
128a9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
128aa 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 Off<iHdrOff ){.
128ab 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
128ac 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
128ad 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 e(pPager, 1, 0,
128ae 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c &pPager->journal
128af 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a Off, 1, pDone);.
128b0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
128b1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc!=SQLITE_DON
128b2 45 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E );. }else{.
128b3 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
128b4 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 lOff = 0;. }..
128b5 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c /* Continue rol
128b6 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 ling back record
128b7 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 s out of the mai
128b8 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 n journal starti
128b9 6e 67 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 ng at. ** the f
128ba 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 irst journal hea
128bb 64 65 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e der seen and con
128bc 74 69 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 tinuing until th
128bd 65 20 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a e effective end.
128be 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e ** of the main
128bf 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 journal file.
128c0 43 6f 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 Continue to skip
128c1 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 out-of-range pa
128c2 67 65 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e ges and. ** con
128c3 74 69 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 tinue adding pag
128c4 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 es rolled back t
128c5 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 o pDone.. */.
128c6 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
128c7 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
128c8 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 journalOff<szJ )
128c9 7b 0a 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 {. u32 ii;
128ca 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
128cb 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
128cc 75 33 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 u32 nJRec = 0;
128cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
128ce 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 Journal Records
128cf 2a 2f 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 */. u32 dummy
128d0 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a ;. rc = readJ
128d1 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 ournalHdr(pPager
128d2 2c 20 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 , 0, szJ, &nJRec
128d3 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 , &dummy);. a
128d4 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
128d5 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f E_DONE );.. /
128d6 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 *. ** The "pP
128d7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
128d8 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
128d9 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d pPager)==pPager-
128da 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 >journalOff".
128db 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 ** test is rela
128dc 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 ted to ticket #2
128dd 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 565. See the di
128de 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a scussion in the.
128df 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 ** pager_pla
128e0 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e yback() function
128e1 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
128e2 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 information..
128e3 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 */. if( nJRe
128e4 63 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50 c==0 . && pP
128e5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
128e6 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
128e7 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d pPager)==pPager-
128e8 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 >journalOff.
128e9 29 7b 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d ){. nJRec =
128ea 20 28 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 (u32)((szJ - pP
128eb 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
128ec 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 )/JOURNAL_PG_SZ(
128ed 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a pPager));. }.
128ee 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 for(ii=0; rc
128ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
128f0 69 3c 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 i<nJRec && pPage
128f1 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a r->journalOff<sz
128f2 4a 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 J; ii++){.
128f3 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
128f4 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
128f5 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 ger, 1, 0, &pPag
128f6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
128f7 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 1, pDone);. }
128f8 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 . assert( rc!
128f9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a =SQLITE_DONE );.
128fa 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 72 63 }. assert( rc
128fb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 !=SQLITE_OK || p
128fc 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
128fd 66 3d 3d 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 f==szJ );.. /*
128fe 46 69 6e 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 Finally, rollba
128ff 63 6b 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 ck pages from th
12900 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 e sub-journal.
12901 50 61 67 65 20 74 68 61 74 20 77 65 72 65 0a 20 Page that were.
12902 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 ** previously r
12903 6f 6c 6c 65 64 20 62 61 63 6b 20 6f 75 74 20 6f olled back out o
12904 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
12905 61 6c 20 28 61 6e 64 20 61 72 65 20 68 65 6e 63 al (and are henc
12906 65 20 69 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a e in pDone). **
12907 20 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 will be skipped
12908 2e 20 20 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 . Out-of-range
12909 70 61 67 65 73 20 61 72 65 20 61 6c 73 6f 20 73 pages are also s
1290a 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 kipped.. */. i
1290b 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b f( pSavepoint ){
1290c 0a 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 . u32 ii;
1290d 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1290e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 counter */. i
1290f 36 34 20 6f 66 66 73 65 74 20 3d 20 70 53 61 76 64 offset = pSav
12910 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a epoint->iSubRec*
12911 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 (4+pPager->pageS
12912 69 7a 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 ize);. for(ii
12913 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 =pSavepoint->iSu
12914 62 52 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 bRec; rc==SQLITE
12915 5f 4f 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72 _OK && ii<pPager
12916 2d 3e 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 ->nSubRec; ii++)
12917 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
12918 6f 66 66 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50 offset==ii*(4+pP
12919 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 ager->pageSize)
1291a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 );. rc = pa
1291b 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 ger_playback_one
1291c 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 30 2c _page(pPager, 0,
1291d 20 30 2c 20 26 6f 66 66 73 65 74 2c 20 31 2c 20 0, &offset, 1,
1291e 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pDone);. }.
1291f 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
12920 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d LITE_DONE );. }
12921 0a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 .. sqlite3Bitve
12922 63 44 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b cDestroy(pDone);
12923 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
12924 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 E_OK ){. pPag
12925 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
12926 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 szJ;. }. retu
12927 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12928 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d Change the maxim
12929 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
1292a 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61 memory pages tha
1292b 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a t are allowed..*
1292c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1292d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
1292e 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 erSetCachesize(P
1292f 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
12930 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c t mxPage){. sql
12931 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 ite3PcacheSetCac
12932 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 hesize(pPager->p
12933 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b PCache, mxPage);
12934 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 .}../*.** Adjust
12935 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20 the robustness
12936 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
12937 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f to damage due to
12938 20 4f 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f OS crashes.** o
12939 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73 r power failures
1293a 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 by changing the
1293b 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 number of syncs
1293c 28 29 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67 ()s when writing
1293d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b .** the rollback
1293e 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 journal. There
1293f 20 61 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c are three level
12940 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 s:.**.** OFF
12941 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 sqlite3OsS
12942 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 63 ync() is never c
12943 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20 alled. This is
12944 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 the default.**
12945 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 for
12946 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72 temporary and tr
12947 61 6e 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a ansient files..*
12948 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 *.** NORMAL
12949 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 The journal is
1294a 20 73 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 synced once bef
1294b 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e ore writes begin
1294c 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 on the.**
1294d 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
1294e 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 . This is norma
1294f 6c 6c 79 20 61 64 65 71 75 61 74 65 20 70 72 6f lly adequate pro
12950 74 65 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 tection, but.**
12951 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20 it
12952 69 73 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 is theoretically
12953 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 possible, thoug
12954 68 20 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c h very unlikely,
12955 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
12956 20 74 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 that an inopert
12957 75 6e 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72 une power failur
12958 65 20 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 e could leave th
12959 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 e journal.**
1295a 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 20 73 in a s
1295b 74 61 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64 tate which would
1295c 20 63 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f cause damage to
1295d 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
1295e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 wh
1295f 65 6e 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 en it is rolled
12960 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 back..**.** F
12961 55 4c 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75 ULL The jou
12962 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74 rnal is synced t
12963 77 69 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 wice before writ
12964 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a es begin on the.
12965 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
12966 64 61 74 61 62 61 73 65 20 28 77 69 74 68 20 73 database (with s
12967 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 ome additional i
12968 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 nformation - the
12969 20 6e 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 nRec field.**
1296a 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 of t
1296b 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
1296c 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65 r - being writte
1296d 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 n in between the
1296e 20 74 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 two.**
1296f 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66 syncs). If
12970 20 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 we assume that
12971 77 72 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 writing a.**
12972 20 20 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65 single
12973 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20 disk sector is
12974 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 atomic, then thi
12975 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a s mode provides.
12976 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
12977 61 73 73 75 72 61 6e 63 65 20 74 68 61 74 20 74 assurance that t
12978 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 he journal will
12979 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 not be corrupted
1297a 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 to the.**
1297b 20 20 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 point of
1297c 20 63 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20 causing damage
1297d 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1297e 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e during rollback.
1297f 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 .**.** Numeric v
12980 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64 alues associated
12981 20 77 69 74 68 20 74 68 65 73 65 20 73 74 61 74 with these stat
12982 65 73 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e es are OFF==1, N
12983 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 ORMAL=2,.** and
12984 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 FULL=3..*/.#ifnd
12985 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ef SQLITE_OMIT_P
12986 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c AGER_PRAGMAS.SQL
12987 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12988 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
12989 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 SafetyLevel(Page
1298a 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c r *pPager, int l
1298b 65 76 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 evel, int bFullF
1298c 73 79 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d sync){. pPager-
1298d 3e 6e 6f 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 >noSync = (leve
1298e 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e l==1 || pPager->
1298f 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a tempFile) ?1:0;.
12990 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 pPager->fullSy
12991 6e 63 20 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26 nc = (level==3 &
12992 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 & !pPager->tempF
12993 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 ile) ?1:0;. pPa
12994 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 ger->sync_flags
12995 3d 20 28 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51 = (bFullFsync?SQ
12996 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 LITE_SYNC_FULL:S
12997 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 QLITE_SYNC_NORMA
12998 4c 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 L);. if( pPager
12999 2d 3e 6e 6f 53 79 6e 63 20 29 20 70 50 61 67 65 ->noSync ) pPage
1299a 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b r->needSync = 0;
1299b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1299c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 The following g
1299d 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 lobal variable i
1299e 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 s incremented wh
1299f 65 6e 65 76 65 72 20 74 68 65 20 6c 69 62 72 61 enever the libra
129a0 72 79 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 ry.** attempts t
129a1 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 o open a tempora
129a2 72 79 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 ry file. This i
129a3 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 nformation is us
129a4 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e ed for.** testin
129a5 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
129a6 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 nly. .*/.#ifdef
129a7 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
129a8 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
129a9 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 te3_opentemp_cou
129aa 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
129ab 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d /*.** Open a tem
129ac 70 6f 72 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a porary file..**.
129ad 2a 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c ** Write the fil
129ae 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 6e 74 e descriptor int
129af 6f 20 2a 70 46 69 6c 65 2e 20 52 65 74 75 72 6e o *pFile. Return
129b0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
129b1 63 63 65 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d ccess .** or som
129b2 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f e other error co
129b3 64 65 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 de if we fail. T
129b4 68 65 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d he OS will autom
129b5 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c atically .** del
129b6 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 ete the temporar
129b7 79 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 y file when it i
129b8 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 s closed..**.**
129b9 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 The flags passed
129ba 20 74 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 to the VFS laye
129bb 72 20 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 r xOpen() call a
129bc 72 65 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 re those specifi
129bd 65 64 0a 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 ed.** by paramet
129be 65 72 20 76 66 73 46 6c 61 67 73 20 4f 52 65 64 er vfsFlags ORed
129bf 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 with the follow
129c0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 ing:.**.** S
129c1 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
129c2 52 49 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 RITE.** SQLI
129c3 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a TE_OPEN_CREATE.*
129c4 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 * SQLITE_OPE
129c5 4e 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 N_EXCLUSIVE.**
129c6 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 SQLITE_OPEN_D
129c7 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a ELETEONCLOSE.*/.
129c8 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
129c9 4f 70 65 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 Opentemp(. Page
129ca 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
129cb 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f /* The pager o
129cc 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 bject */. sqlit
129cd 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
129ce 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 /* Write the fi
129cf 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 68 65 le descriptor he
129d0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 re */. int vfsF
129d1 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 2f 2a lags /*
129d2 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 Flags passed th
129d3 72 6f 75 67 68 20 74 6f 20 74 68 65 20 56 46 53 rough to the VFS
129d4 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
129d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
129d6 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
129d7 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
129d8 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f TEST. sqlite3_o
129d9 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b pentemp_count++;
129da 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 /* Used for te
129db 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 sting and analys
129dc 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 is only */.#endi
129dd 66 0a 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d f.. vfsFlags |=
129de 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 SQLITE_OPEN_RE
129df 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 ADWRITE | SQLITE
129e0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 _OPEN_CREATE |.
129e1 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
129e2 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
129e3 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 | SQLITE_OPEN_D
129e4 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 ELETEONCLOSE;.
129e5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
129e6 65 6e 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c en(pPager->pVfs,
129e7 20 30 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 0, pFile, vfsFl
129e8 61 67 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 ags, 0);. asser
129e9 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b t( rc!=SQLITE_OK
129ea 20 7c 7c 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 || isOpen(pFile
129eb 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
129ec 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
129ed 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 he busy handler
129ee 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
129ef 54 68 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 The pager invoke
129f0 73 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c s the busy-handl
129f1 65 72 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c er if sqlite3OsL
129f2 6f 63 6b 28 29 20 72 65 74 75 72 6e 73 20 0a 2a ock() returns .*
129f3 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68 * SQLITE_BUSY wh
129f4 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 en trying to upg
129f5 72 61 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 rade from no-loc
129f6 6b 20 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f k to a SHARED lo
129f7 63 6b 2c 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 ck,.** or when t
129f8 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 rying to upgrade
129f9 20 66 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44 from a RESERVED
129fa 20 6c 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c lock to an EXCL
129fb 55 53 49 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 USIVE .** lock.
129fc 49 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e It does *not* in
129fd 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 voke the busy ha
129fe 6e 64 6c 65 72 20 77 68 65 6e 20 75 70 67 72 61 ndler when upgra
129ff 64 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 ding from.** SHA
12a00 52 45 44 20 74 6f 20 52 45 53 45 52 56 45 44 2c RED to RESERVED,
12a01 20 6f 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 or when upgradi
12a02 6e 67 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 ng from SHARED t
12a03 6f 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 o EXCLUSIVE.** (
12a04 77 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 which occurs dur
12a05 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ing hot-journal
12a06 72 6f 6c 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 rollback). Summa
12a07 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e ry:.**.** Tran
12a08 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 sition
12a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
12a0a 49 6e 76 6f 6b 65 73 20 78 42 75 73 79 48 61 6e Invokes xBusyHan
12a0b 64 6c 65 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d dler.** ------
12a0c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12a0d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12a0e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12a0f 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 --.** NO_LOCK
12a10 20 20 20 20 20 20 2d 3e 20 53 48 41 52 45 44 5f -> SHARED_
12a11 4c 4f 43 4b 20 20 20 20 20 20 7c 20 59 65 73 0a LOCK | Yes.
12a12 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b ** SHARED_LOCK
12a13 20 20 20 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c -> RESERVED_L
12a14 4f 43 4b 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 OCK | No.**
12a15 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d SHARED_LOCK -
12a16 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b > EXCLUSIVE_LOCK
12a17 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 | No.** RES
12a18 45 52 56 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 ERVED_LOCK -> EX
12a19 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c CLUSIVE_LOCK |
12a1a 20 59 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 Yes.**.** If th
12a1b 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 e busy-handler c
12a1c 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
12a1d 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f non-zero, the lo
12a1e 63 6b 20 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 ck is .** retrie
12a1f 64 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 d. If it returns
12a20 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
12a21 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f SQLITE_BUSY erro
12a22 72 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 r is.** returned
12a23 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f to the caller o
12a24 66 20 74 68 65 20 70 61 67 65 72 20 41 50 49 20 f the pager API
12a25 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c function..*/.SQL
12a26 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12a27 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
12a28 42 75 73 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 Busyhandler(. P
12a29 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
12a2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12a2b 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a /* Pager obj
12a2c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ect */. int (*x
12a2d 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 BusyHandler)(voi
12a2e 64 20 2a 29 2c 20 20 20 20 20 20 20 20 20 2f 2a d *), /*
12a2f 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 73 79 Pointer to busy
12a30 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f -handler functio
12a31 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 n */. void *pBu
12a32 73 79 48 61 6e 64 6c 65 72 41 72 67 20 20 20 20 syHandlerArg
12a33 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
12a34 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 rgument to pass
12a35 74 6f 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 to xBusyHandler
12a36 2a 2f 0a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 */.){ . pPager
12a37 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d ->xBusyHandler =
12a38 20 78 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 xBusyHandler;.
12a39 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
12a3a 6e 64 6c 65 72 41 72 67 20 3d 20 70 42 75 73 79 ndlerArg = pBusy
12a3b 48 61 6e 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f HandlerArg;.}../
12a3c 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 *.** Report the
12a3d 63 75 72 72 65 6e 74 20 70 61 67 65 20 73 69 7a current page siz
12a3e 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 e and number of
12a3f 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 62 reserved bytes b
12a40 61 63 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f ack.** to the co
12a41 64 65 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 dec..*/.#ifdef S
12a42 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
12a43 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
12a44 72 52 65 70 6f 72 74 53 69 7a 65 28 50 61 67 65 rReportSize(Page
12a45 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
12a46 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ( pPager->xCodec
12a47 53 69 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 20 SizeChng ){.
12a48 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 pPager->xCodecSi
12a49 7a 65 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 zeChng(pPager->p
12a4a 43 6f 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 Codec, pPager->p
12a4b 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 ageSize,.
12a4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12a4d 20 20 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d (int)pPager-
12a4e 3e 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a >nReserve);. }.
12a4f 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
12a50 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 pagerReportSize
12a51 28 58 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 (X) /* No-op
12a52 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 if we do not su
12a53 70 70 6f 72 74 20 61 20 63 6f 64 65 63 20 2a 2f pport a codec */
12a54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
12a55 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 hange the page s
12a56 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 ize used by the
12a57 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 Pager object. Th
12a58 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 e new page size
12a59 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e .** is passed in
12a5a 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a *pPageSize..**.
12a5b 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
12a5c 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 is in the error
12a5d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 state when this
12a5e 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
12a5f 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e ed, it.** is a n
12a60 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20 o-op. The value
12a61 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 returned is the
12a62 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f error state erro
12a63 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a r code (i.e. .**
12a64 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 one of SQLITE_I
12a65 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 OERR, SQLITE_COR
12a66 52 55 50 54 20 6f 72 20 53 51 4c 49 54 45 5f 46 RUPT or SQLITE_F
12a67 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 ULL)..**.** Othe
12a68 72 77 69 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 rwise, if all of
12a69 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
12a6a 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 re true:.**.**
12a6b 20 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 * the new page
12a6c 73 69 7a 65 20 28 76 61 6c 75 65 20 6f 66 20 2a size (value of *
12a6d 70 50 61 67 65 53 69 7a 65 29 20 69 73 20 76 61 pPageSize) is va
12a6e 6c 69 64 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a lid (a power .**
12a6f 20 20 20 20 20 6f 66 20 74 77 6f 20 62 65 74 77 of two betw
12a70 65 65 6e 20 35 31 32 20 61 6e 64 20 53 51 4c 49 een 512 and SQLI
12a71 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
12a72 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e , inclusive), an
12a73 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 d.**.** * ther
12a74 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e e are no outstan
12a75 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 ding page refere
12a76 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 nces, and.**.**
12a77 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
12a78 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74 20 61 is either not a
12a79 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
12a7a 62 61 73 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a base or it is.**
12a7b 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 an in-memor
12a7c 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 y database that
12a7d 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 currently consis
12a7e 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 ts of zero pages
12a7f 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ..**.** then the
12a80 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 pager object pa
12a81 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 ge size is set t
12a82 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a o *pPageSize..**
12a83 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
12a84 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65 64 2c size is changed,
12a85 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
12a86 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74 65 33 ion uses sqlite3
12a87 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a PagerMalloc() .*
12a88 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 * to obtain a ne
12a89 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 w Pager.pTmpSpac
12a8a 65 20 62 75 66 66 65 72 2e 20 49 66 20 74 68 69 e buffer. If thi
12a8b 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 s allocation att
12a8c 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 empt .** fails,
12a8d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
12a8e 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
12a8f 20 70 61 67 65 20 73 69 7a 65 20 72 65 6d 61 69 page size remai
12a90 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a ns unchanged. .*
12a91 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 * In all other c
12a92 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ases, SQLITE_OK
12a93 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
12a94 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 73 ** If the page s
12a95 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 ize is not chang
12a96 65 64 2c 20 65 69 74 68 65 72 20 62 65 63 61 75 ed, either becau
12a97 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e se one of the en
12a98 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 umerated.** cond
12a99 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 itions above is
12a9a 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 20 70 61 not true, the pa
12a9b 67 65 72 20 77 61 73 20 69 6e 20 65 72 72 6f 72 ger was in error
12a9c 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
12a9d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 .** function was
12a9e 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 called, or beca
12a9f 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 use the memory a
12aa0 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
12aa1 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 t failed, .** th
12aa2 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 20 69 73 en *pPageSize is
12aa3 20 73 65 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c set to the old,
12aa4 20 72 65 74 61 69 6e 65 64 20 70 61 67 65 20 73 retained page s
12aa5 69 7a 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ize before retur
12aa6 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ning..*/.SQLITE_
12aa7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12aa8 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
12aa9 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ize(Pager *pPage
12aaa 72 2c 20 75 31 36 20 2a 70 50 61 67 65 53 69 7a r, u16 *pPageSiz
12aab 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 e, int nReserve)
12aac 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 {. int rc = pPa
12aad 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 ger->errCode;..
12aae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
12aaf 4f 4b 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 OK ){. u16 pa
12ab0 67 65 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 geSize = *pPageS
12ab1 69 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ize;. assert(
12ab2 20 70 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 pageSize==0 ||
12ab3 28 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 (pageSize>=512 &
12ab4 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 & pageSize<=SQLI
12ab5 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
12ab6 29 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 ) );. if( (pP
12ab7 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c ager->memDb==0 |
12ab8 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 | pPager->dbSize
12ab9 3d 3d 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c ==0). && sql
12aba 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
12abb 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
12abc 68 65 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 he)==0 . &&
12abd 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 pageSize && page
12abe 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61 Size!=pPager->pa
12abf 67 65 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a 20 geSize . ){.
12ac0 20 20 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 char *pNew
12ac1 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
12ac2 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 3PageMalloc(page
12ac3 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Size);. if(
12ac4 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 !pNew ){.
12ac5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
12ac6 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 MEM;. }else
12ac7 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f {. pager_
12ac8 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
12ac9 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 pPager->p
12aca 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 ageSize = pageSi
12acb 7a 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ze;. sqli
12acc 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67 te3PageFree(pPag
12acd 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a er->pTmpSpace);.
12ace 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
12acf 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 77 pTmpSpace = pNew
12ad0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
12ad1 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
12ad2 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ze(pPager->pPCac
12ad3 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 he, pageSize);.
12ad4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
12ad5 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 28 75 *pPageSize = (u
12ad6 31 36 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 16)pPager->pageS
12ad7 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 ize;. if( nRe
12ad8 73 65 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 serve<0 ) nReser
12ad9 76 65 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 ve = pPager->nRe
12ada 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 serve;. asser
12adb 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 t( nReserve>=0 &
12adc 26 20 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 & nReserve<1000
12add 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e );. pPager->n
12ade 52 65 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e Reserve = (i16)n
12adf 52 65 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 Reserve;. pag
12ae0 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 erReportSize(pPa
12ae1 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ger);. }. retu
12ae2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12ae3 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
12ae4 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 to the "tempora
12ae5 72 79 20 70 61 67 65 22 20 62 75 66 66 65 72 20 ry page" buffer
12ae6 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a held internally.
12ae7 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 72 2e ** by the pager.
12ae8 20 20 54 68 69 73 20 69 73 20 61 20 62 75 66 66 This is a buff
12ae9 65 72 20 74 68 61 74 20 69 73 20 62 69 67 20 65 er that is big e
12aea 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 nough to hold th
12aeb 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 e.** entire cont
12aec 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 ent of a databas
12aed 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 e page. This bu
12aee 66 66 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 ffer is used int
12aef 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e ernally.** durin
12af0 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 g rollback and w
12af1 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 ill be overwritt
12af2 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f en whenever a ro
12af3 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 llback.** occurs
12af4 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 . But other mod
12af5 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 74 6f ules are free to
12af6 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 use it too, as
12af7 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f long as.** no ro
12af8 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 llbacks are happ
12af9 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ening..*/.SQLITE
12afa 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
12afb 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 qlite3PagerTempS
12afc 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 pace(Pager *pPag
12afd 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
12afe 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
12aff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
12b00 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 t to set the max
12b01 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 imum database pa
12b02 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 ge count if mxPa
12b03 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 ge is positive.
12b04 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e .** Make no chan
12b05 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 ges if mxPage is
12b06 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 zero or negativ
12b07 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 e. And never re
12b08 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 duce the.** maxi
12b09 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 mum page count b
12b0a 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 elow the current
12b0b 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
12b0c 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 abase..**.** Reg
12b0d 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 ardless of mxPag
12b0e 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 e, return the cu
12b0f 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 rrent maximum pa
12b10 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c ge count..*/.SQL
12b11 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12b12 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 sqlite3PagerMaxP
12b13 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a ageCount(Pager *
12b14 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 pPager, int mxPa
12b15 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67 ge){. if( mxPag
12b16 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 e>0 ){. pPage
12b17 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 r->mxPgno = mxPa
12b18 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ge;. }. sqlite
12b19 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
12b1a 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 72 65 pPager, 0);. re
12b1b 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50 turn pPager->mxP
12b1c 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 gno;.}../*.** Th
12b1d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 e following set
12b1e 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 of routines are
12b1f 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c 65 20 used to disable
12b20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a the simulated.**
12b21 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 61 I/O error mecha
12b22 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 6f 75 nism. These rou
12b23 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 tines are used t
12b24 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 65 o avoid simulate
12b25 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 70 d.** errors in p
12b26 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 20 64 laces where we d
12b27 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 o not care about
12b28 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 errors..**.** U
12b29 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 nless -DSQLITE_T
12b2a 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74 EST=1 is used, t
12b2b 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
12b2c 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 e all no-ops.**
12b2d 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 and generate no
12b2e 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 code..*/.#ifdef
12b2f 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
12b30 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e TE_API extern in
12b31 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
12b32 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 or_pending;.SQLI
12b33 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e TE_API extern in
12b34 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
12b35 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63 20 69 or_hit;.static i
12b36 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76 6f nt saved_cnt;.vo
12b37 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c id disable_simul
12b38 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 ated_io_errors(v
12b39 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f 63 6e oid){. saved_cn
12b3a 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 t = sqlite3_io_e
12b3b 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 rror_pending;.
12b3c 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
12b3d 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d _pending = -1;.}
12b3e 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d .void enable_sim
12b3f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
12b40 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 (void){. sqlite
12b41 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
12b42 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b 0a ng = saved_cnt;.
12b43 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
12b44 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
12b45 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 ed_io_errors().#
12b46 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 define enable_s
12b47 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12b48 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a rs().#endif../*.
12b49 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 ** Read the firs
12b4a 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 t N bytes from t
12b4b 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
12b4c 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d 65 the file into me
12b4d 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70 44 65 mory.** that pDe
12b4e 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a st points to. .*
12b4f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
12b50 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
12b51 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 a transient file
12b52 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 (zFilename=="")
12b53 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f , or.** opened o
12b54 6e 20 61 20 66 69 6c 65 20 6c 65 73 73 20 74 68 n a file less th
12b55 61 6e 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 an N bytes in si
12b56 7a 65 2c 20 74 68 65 20 6f 75 74 70 75 74 20 62 ze, the output b
12b57 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f uffer is.** zero
12b58 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b ed and SQLITE_OK
12b59 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 72 returned. The r
12b5a 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20 74 68 69 ationale for thi
12b5b 73 20 69 73 20 74 68 61 74 20 74 68 69 73 20 0a s is that this .
12b5c 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 ** function is u
12b5d 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 sed to read data
12b5e 62 61 73 65 20 68 65 61 64 65 72 73 2c 20 61 6e base headers, an
12b5f 64 20 61 20 6e 65 77 20 74 72 61 6e 73 69 65 6e d a new transien
12b60 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a t or.** zero siz
12b61 65 64 20 64 61 74 61 62 61 73 65 20 68 61 73 20 ed database has
12b62 61 20 68 65 61 64 65 72 20 74 68 61 6e 20 63 6f a header than co
12b63 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20 nsists entirely
12b64 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a of zeroes..**.**
12b65 20 49 66 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 If any IO error
12b66 20 61 70 61 72 74 20 66 72 6f 6d 20 53 51 4c 49 apart from SQLI
12b67 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
12b68 45 41 44 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 EAD is encounter
12b69 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 ed,.** the error
12b6a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12b6b 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 d to the caller
12b6c 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 and the contents
12b6d 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 of the.** outpu
12b6e 74 20 62 75 66 66 65 72 20 75 6e 64 65 66 69 6e t buffer undefin
12b6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
12b70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12b71 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
12b72 61 64 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 ader(Pager *pPag
12b73 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 er, int N, unsig
12b74 6e 65 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 ned char *pDest)
12b75 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
12b76 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 ITE_OK;. memset
12b77 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 (pDest, 0, N);.
12b78 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
12b79 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 pPager->fd) || p
12b7a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
12b7b 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 );. if( isOpen(
12b7c 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 pPager->fd) ){.
12b7d 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 IOTRACE(("DBH
12b7e 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 DR %p 0 %d\n", p
12b7f 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 Pager, N)). r
12b80 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
12b81 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 d(pPager->fd, pD
12b82 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 est, N, 0);.
12b83 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 if( rc==SQLITE_I
12b84 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
12b85 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
12b86 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
12b87 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12b88 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
12b89 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
12b8a 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
12b8b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
12b8c 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 ssociated .** wi
12b8d 74 68 20 70 50 61 67 65 72 2e 20 4e 6f 72 6d 61 th pPager. Norma
12b8e 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c lly, this is cal
12b8f 63 75 6c 61 74 65 64 20 61 73 20 28 3c 64 62 20 culated as (<db
12b90 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 67 65 file size>/<page
12b91 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 -size>)..** Howe
12b92 76 65 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65 ver, if the file
12b93 20 69 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e is between 1 an
12b94 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79 d <page-size> by
12b95 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 tes in size, the
12b96 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f n .** this is co
12b97 6e 73 69 64 65 72 65 64 20 61 20 31 20 70 61 67 nsidered a 1 pag
12b98 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
12b99 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
12b9a 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 error state whe
12b9b 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
12b9c 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
12b9d 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 61 the.** error sta
12b9e 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
12b9f 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 returned and *p
12ba0 6e 50 61 67 65 20 6c 65 66 74 20 75 6e 63 68 61 nPage left uncha
12ba1 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 nged. Or,.** if
12ba2 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 the file system
12ba3 68 61 73 20 74 6f 20 62 65 20 71 75 65 72 69 65 has to be querie
12ba4 64 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f d for the size o
12ba5 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 0a 2a f the file and.*
12ba6 2a 20 74 68 65 20 71 75 65 72 79 20 61 74 74 65 * the query atte
12ba7 6d 70 74 20 72 65 74 75 72 6e 73 20 61 6e 20 49 mpt returns an I
12ba8 4f 20 65 72 72 6f 72 2c 20 74 68 65 20 49 4f 20 O error, the IO
12ba9 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
12baa 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 turned.** and *p
12bab 6e 50 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e nPage is left un
12bac 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f changed..**.** O
12bad 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 therwise, if eve
12bae 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
12baf 73 73 66 75 6c 2c 20 74 68 65 6e 20 53 51 4c 49 ssful, then SQLI
12bb0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
12bb1 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 d.** and *pnPage
12bb2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e is set to the n
12bb3 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
12bb4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
12bb5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12bb6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
12bb7 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 erPagecount(Page
12bb8 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a r *pPager, int *
12bb9 70 6e 50 61 67 65 29 7b 0a 20 20 50 67 6e 6f 20 pnPage){. Pgno
12bba 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 nPage;
12bbb 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
12bbc 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 return via *pnP
12bbd 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 age */.. /* If
12bbe 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 the pager is alr
12bbf 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f eady in the erro
12bc0 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 r state, return
12bc1 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 the error code.
12bc2 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
12bc3 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
12bc4 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
12bc5 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f rrCode;. }.. /
12bc6 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 * Determine the
12bc7 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
12bc8 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 53 74 6f in the file. Sto
12bc9 72 65 20 74 68 69 73 20 69 6e 20 6e 50 61 67 65 re this in nPage
12bca 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 . */. if( pPage
12bcb 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 r->dbSizeValid )
12bcc 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50 {. nPage = pP
12bcd 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 ager->dbSize;.
12bce 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 }else{. int r
12bcf 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
12bd0 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 75 /* Error retu
12bd1 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 rned by OsFileSi
12bd2 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 20 ze() */. i64
12bd3 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 n = 0;
12bd4 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a 65 /* File size
12bd5 20 69 6e 20 62 79 74 65 73 20 72 65 74 75 72 6e in bytes return
12bd6 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 ed by OsFileSize
12bd7 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 () */.. asser
12bd8 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
12bd9 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d ->fd) || pPager-
12bda 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 >tempFile );.
12bdb 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
12bdc 65 72 2d 3e 66 64 29 20 26 26 20 28 30 20 21 3d er->fd) && (0 !=
12bdd 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 (rc = sqlite3Os
12bde 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
12bdf 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b 0a 20 20 >fd, &n))) ){.
12be0 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 pager_error(
12be1 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
12be2 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
12be3 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 }. if( n>0
12be4 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 && n<pPager->pag
12be5 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e eSize ){. n
12be6 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 Page = 1;. }e
12be7 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 lse{. nPage
12be8 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f 20 70 50 = (Pgno)(n / pP
12be9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
12bea 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
12beb 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 Pager->state!=PA
12bec 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 GER_UNLOCK ){.
12bed 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
12bee 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 ze = nPage;.
12bef 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
12bf0 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
12bf1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
12bf2 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 zeValid = 1;.
12bf3 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
12bf4 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 the current numb
12bf5 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
12bf6 68 65 20 66 69 6c 65 20 69 73 20 67 72 65 61 74 he file is great
12bf7 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a er than the . *
12bf8 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78 * configured max
12bf9 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65 imum pager numbe
12bfa 72 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 r, increase the
12bfb 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f allowed limit so
12bfc 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66 . ** that the f
12bfd 69 6c 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e ile can be read.
12bfe 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67 . */. if( nPag
12bff 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f e>pPager->mxPgno
12c00 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
12c01 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e mxPgno = (Pgno)n
12c02 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Page;. }.. /*
12c03 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 Set the output v
12c04 61 72 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75 ariable and retu
12c05 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a rn SQLITE_OK */.
12c06 20 20 69 66 28 20 70 6e 50 61 67 65 20 29 7b 0a if( pnPage ){.
12c07 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 *pnPage = nP
12c08 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 age;. }. retur
12c09 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
12c0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 ./*.** Try to ob
12c0b 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 tain a lock of t
12c0c 79 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 ype locktype on
12c0d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12c0e 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c e. If.** a simil
12c0f 61 72 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f ar or greater lo
12c10 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
12c11 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f ld, this functio
12c12 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 n is a no-op.**
12c13 28 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 (returning SQLIT
12c14 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 E_OK immediately
12c15 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 )..**.** Otherwi
12c16 73 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f se, attempt to o
12c17 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 btain the lock u
12c18 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f sing sqlite3OsLo
12c19 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a ck(). Invoke .**
12c1a 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 the busy callba
12c1b 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 ck if the lock i
12c1c 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 s currently not
12c1d 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 available. Repea
12c1e 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 t .** until the
12c1f 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 busy callback re
12c20 74 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 turns false or u
12c21 6e 74 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 ntil the attempt
12c22 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 to .** obtain t
12c23 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 he lock succeeds
12c24 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
12c25 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
12c26 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ess and an error
12c27 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e code if we cann
12c28 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 ot obtain.** the
12c29 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f lock. If the lo
12c2a 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 ck is obtained s
12c2b 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 uccessfully, set
12c2c 20 74 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 the Pager.state
12c2d 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f .** variable to
12c2e 20 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 locktype before
12c2f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
12c30 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
12c31 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 wait_on_lock(Pag
12c32 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
12c33 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
12c34 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c36 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
12c37 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53 */.. /* The OS
12c38 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73 lock values mus
12c39 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 t be the same as
12c3a 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20 the Pager lock
12c3b 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65 values */. asse
12c3c 72 74 28 20 50 41 47 45 52 5f 53 48 41 52 45 44 rt( PAGER_SHARED
12c3d 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
12c3e 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 . assert( PAGER
12c3f 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52 _RESERVED==RESER
12c40 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 VED_LOCK );. as
12c41 73 65 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c sert( PAGER_EXCL
12c42 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45 USIVE==EXCLUSIVE
12c43 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 _LOCK );.. /* I
12c44 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 75 f the file is cu
12c45 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 rrently unlocked
12c46 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d then the size m
12c47 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 2e 20 ust be unknown.
12c48 49 74 0a 20 20 2a 2a 20 6d 75 73 74 20 6e 6f 74 It. ** must not
12c49 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
12c4a 69 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e ied at this poin
12c4b 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 t.. */. assert
12c4c 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12c4d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c =PAGER_SHARED ||
12c4e 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
12c4f 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 alid==0 );. ass
12c50 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12c51 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 te>=PAGER_SHARED
12c52 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f || pPager->dbMo
12c53 64 69 66 69 65 64 3d 3d 30 20 29 3b 0a 0a 20 20 dified==0 );..
12c54 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 /* Check that th
12c55 69 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e is is either a n
12c56 6f 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68 o-op (because th
12c57 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
12c58 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 is . ** alread
12c59 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f y held, or one o
12c5a 66 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f f the transistio
12c5b 6e 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 ns that the busy
12c5c 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 -handler. ** ma
12c5d 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 y be invoked dur
12c5e 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 ing, according t
12c5f 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 o the comment ab
12c60 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 ove. ** sqlite3
12c61 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 PagerSetBusyhand
12c62 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 ler().. */. as
12c63 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 sert( (pPager->s
12c64 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a tate>=locktype).
12c65 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 || (pPage
12c66 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
12c67 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79 UNLOCK && lockty
12c68 70 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 pe==PAGER_SHARED
12c69 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 ). || (pPa
12c6a 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
12c6b 52 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f R_RESERVED && lo
12c6c 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58 cktype==PAGER_EX
12c6d 43 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20 CLUSIVE). );..
12c6e 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
12c6f 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a te>=locktype ){.
12c70 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
12c71 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
12c72 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d do {. rc =
12c73 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
12c74 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 Pager->fd, lockt
12c75 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 ype);. }while
12c76 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
12c77 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 Y && pPager->xBu
12c78 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 syHandler(pPager
12c79 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
12c7a 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 g) );. if( rc
12c7b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12c7c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 pPager->sta
12c7d 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 te = (u8)locktyp
12c7e 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 e;. IOTRACE
12c7f 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 (("LOCK %p %d\n"
12c80 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 , pPager, lockty
12c81 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 pe)). }. }.
12c82 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12c83 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73 *.** Function as
12c84 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
12c85 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63 traint(pPager) c
12c86 68 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f hecks that one o
12c87 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 f the .** follow
12c88 69 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20 ing is true for
12c89 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 all dirty pages
12c8a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
12c8b 20 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a page-cache:.**.
12c8c 2a 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65 ** a) The page
12c8d 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 number is less
12c8e 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
12c8f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
12c90 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e .** curren
12c91 74 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 t database image
12c92 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a , in pages, OR.*
12c93 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65 *.** b) if the
12c94 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65 page content we
12c95 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 re written at th
12c96 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c is time, it woul
12c97 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65 d not.** be
12c98 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 necessary to wr
12c99 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
12c9a 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74 content out to t
12c9b 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a he sub-journal.*
12c9c 2a 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72 * (as deter
12c9d 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f mined by functio
12c9e 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 n subjRequiresPa
12c9f 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ge())..**.** If
12ca0 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73 the condition as
12ca1 73 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66 serted by this f
12ca2 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 unction were not
12ca3 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a true, and the.*
12ca4 2a 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72 * dirty page wer
12ca5 65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65 e to be discarde
12ca6 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 d from the cache
12ca7 20 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74 via the pagerSt
12ca8 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e ress().** routin
12ca9 65 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29 e, pagerStress()
12caa 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65 would not write
12cab 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 the current pag
12cac 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 e content to.**
12cad 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12cae 65 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e e. If a savepoin
12caf 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65 t transaction we
12cb0 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 re rolled back a
12cb1 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70 fter.** this hap
12cb2 70 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65 pened, the corre
12cb3 63 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75 ct behaviour wou
12cb4 6c 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65 ld be to restore
12cb5 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
12cb6 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 content of the p
12cb7 61 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 age. However, si
12cb8 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 nce this content
12cb9 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 is not present
12cba 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65 in either.** the
12cbb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f database file o
12cbc 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66 r the portion of
12cbd 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
12cbe 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 urnal and .** su
12cbf 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 b-journal rolled
12cc0 20 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e back the conten
12cc1 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 t could not be r
12cc2 65 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a estored and the.
12cc3 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 ** database imag
12cc4 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63 e would become c
12cc5 6f 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68 orrupt. It is th
12cc6 65 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74 erefore fortunat
12cc7 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20 e that .** this
12cc8 63 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e circumstance can
12cc9 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 not arise..*/.#i
12cca 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
12ccb 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 _DEBUG).static v
12ccc 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 oid assertTrunca
12ccd 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50 teConstraintCb(P
12cce 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73 gHdr *pPg){. as
12ccf 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 sert( pPg->flags
12cd0 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a &PGHDR_DIRTY );.
12cd1 20 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52 assert( !subjR
12cd2 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 equiresPage(pPg)
12cd3 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 || pPg->pgno<=p
12cd4 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 Pg->pPager->dbSi
12cd5 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 ze );.}.static v
12cd6 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 oid assertTrunca
12cd7 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67 teConstraint(Pag
12cd8 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 er *pPager){. s
12cd9 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
12cda 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d ateDirty(pPager-
12cdb 3e 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74 >pPCache, assert
12cdc 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12cdd 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 ntCb);.}.#else.#
12cde 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72 define assertTr
12cdf 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 uncateConstraint
12ce0 28 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a (pPager).#endif.
12ce1 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
12ce2 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 the in-memory da
12ce3 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67 tabase file imag
12ce4 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 e to nPage pages
12ce5 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 . This .** funct
12ce6 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 ion does not act
12ce7 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 ually modify the
12ce8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f database file o
12ce9 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a n disk. It .** j
12cea 75 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 ust sets the int
12ceb 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 ernal state of t
12cec 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 he pager object
12ced 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 so that the .**
12cee 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 truncation will
12cef 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 be done when the
12cf0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
12cf1 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
12cf2 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
12cf3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
12cf4 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
12cf5 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 age(Pager *pPage
12cf6 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a r, Pgno nPage){.
12cf7 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12cf8 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b ->dbSizeValid );
12cf9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12cfa 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 r->dbSize>=nPage
12cfb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
12cfc 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
12cfd 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
12cfe 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
12cff 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72 = nPage;. asser
12d00 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
12d01 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a int(pPager);.}..
12d02 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 /*.** Shutdown t
12d03 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 he page cache.
12d04 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 Free all memory
12d05 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 and close all fi
12d06 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 les..**.** If a
12d07 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
12d08 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e in progress when
12d09 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
12d0a 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a called, that.**
12d0b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
12d0c 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c rolled back. Al
12d0d 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 l outstanding pa
12d0e 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 ges are invalida
12d0f 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 ted.** and their
12d10 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 memory is freed
12d11 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
12d12 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73 o use a page ass
12d13 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 ociated.** with
12d14 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20 this page cache
12d15 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
12d16 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c ion returns will
12d17 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c likely.** resul
12d18 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e t in a coredump.
12d19 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
12d1a 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 tion always succ
12d1b 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 eeds. If a trans
12d1c 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
12d1d 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 an attempt.** i
12d1e 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 s made to roll i
12d1f 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 t back. If an er
12d20 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
12d21 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a g the rollback .
12d22 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c ** a hot journal
12d23 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 may be left in
12d24 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 the filesystem b
12d25 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 ut no error is r
12d26 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 eturned.** to th
12d27 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c e caller..*/.SQL
12d28 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12d29 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 sqlite3PagerClos
12d2a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
12d2b 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 {. disable_simu
12d2c 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
12d2d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 );. sqlite3Begi
12d2e 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
12d2f 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f . pPager->errCo
12d30 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 de = 0;. pPager
12d31 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
12d32 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 = 0;. pager_res
12d33 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 et(pPager);. if
12d34 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 ( MEMDB ){. p
12d35 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 ager_unlock(pPag
12d36 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 er);. }else{.
12d37 20 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a /* Set Pager.j
12d38 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 ournalHdr to -1
12d39 66 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 for the benefit
12d3a 6f 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 of the pager_pla
12d3b 79 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 yback() . **
12d3c 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 call which may b
12d3d 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 e made from with
12d3e 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e in pagerUnlockAn
12d3f 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 dRollback(). If
12d40 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 it. ** is not
12d41 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e -1, then the un
12d42 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f synced portion o
12d43 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 f an open journa
12d44 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a l file may. *
12d45 2a 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b * be played back
12d46 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
12d47 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 se. If a power f
12d48 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 ailure occurs wh
12d49 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 ile. ** this
12d4a 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 is happening, th
12d4b 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 e database may b
12d4c 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 ecome corrupt..
12d4d 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 */. pPager
12d4e 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d ->journalHdr = -
12d4f 31 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 1;. pagerUnlo
12d50 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 ckAndRollback(pP
12d51 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c ager);. }. sql
12d52 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
12d53 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f loc();. enable_
12d54 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
12d55 6f 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 ors();. PAGERTR
12d56 41 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e ACE(("CLOSE %d\n
12d57 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
12d58 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 r)));. IOTRACE(
12d59 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 ("CLOSE %p\n", p
12d5a 50 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 Pager)). sqlite
12d5b 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
12d5c 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 >fd);. sqlite3P
12d5d 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e ageFree(pPager->
12d5e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 pTmpSpace);. sq
12d5f 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 lite3PcacheClose
12d60 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12d61 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
12d62 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 E_HAS_CODEC. if
12d63 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ( pPager->xCodec
12d64 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 Free ) pPager->x
12d65 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 CodecFree(pPager
12d66 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 ->pCodec);.#endi
12d67 66 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 f.. assert( !pP
12d68 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
12d69 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e && !pPager->pIn
12d6a 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 Journal );. ass
12d6b 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 ert( !isOpen(pPa
12d6c 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 ger->jfd) && !is
12d6d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 Open(pPager->sjf
12d6e 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 d) );.. sqlite3
12d6f 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 _free(pPager);.
12d70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
12d71 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e K;.}..#if !defin
12d72 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 ed(NDEBUG) || de
12d73 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
12d74 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 T)./*.** Return
12d75 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
12d76 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f for page pPg..*/
12d77 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12d78 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 Pgno sqlite3Page
12d79 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 rPagenumber(DbPa
12d7a 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 ge *pPg){. retu
12d7b 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a rn pPg->pgno;.}.
12d7c 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
12d7d 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
12d7e 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 rence count for
12d7f 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c page pPg..*/.SQL
12d80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12d81 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
12d82 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
12d83 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
12d84 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a f(pPg);.}../*.**
12d85 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 Sync the journa
12d86 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 l. In other word
12d87 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c s, make sure all
12d88 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 the pages that
12d89 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 have.** been wri
12d8a 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 tten to the jour
12d8b 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c nal have actuall
12d8c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 y reached the su
12d8d 72 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 rface of the.**
12d8e 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 disk and can be
12d8f 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 restored in the
12d90 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a event of a hot-j
12d91 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e ournal rollback.
12d92 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 .**.** If the Pa
12d93 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 ger.needSync fla
12d94 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 g is not set, th
12d95 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
12d96 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 is a.** no-op.
12d97 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 Otherwise, the a
12d98 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 ctions required
12d99 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f depend on the jo
12d9a 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e urnal-mode.** an
12d9b 64 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 d the device cha
12d9c 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 racteristics of
12d9d 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 the the file-sys
12d9e 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a tem, as follows:
12d9f 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 .**.** * If th
12da0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
12da1 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a s an in-memory j
12da2 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 ournal file, no
12da3 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 action need.**
12da4 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a be taken..**.
12da5 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 ** * Otherwise
12da6 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 , if the device
12da7 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
12da8 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 the SAFE_APPEND
12da9 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 property,.**
12daa 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 then the nRec
12dab 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 field of the mos
12dac 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 t recently writt
12dad 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 en journal heade
12dae 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 r.** is upda
12daf 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 ted to contain t
12db0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 he number of jou
12db1 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 rnal records tha
12db2 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 t have.** be
12db3 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f en written follo
12db4 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 wing it. If the
12db5 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 pager is operati
12db6 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a ng in full-sync.
12db7 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 ** mode, the
12db8 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
12db9 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 le is synced bef
12dba 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 ore this field i
12dbb 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a s updated..**.**
12dbc 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 * If the devi
12dbd 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 ce does not supp
12dbe 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 ort the SEQUENTI
12dbf 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 AL property, the
12dc0 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 n .** journa
12dc1 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 l file is synced
12dc2 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 ..**.** Or, in p
12dc3 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a seudo-code:.**.*
12dc4 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d * if( NOT <in-
12dc5 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 memory journal>
12dc6 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f ){.** if( NO
12dc7 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b T SAFE_APPEND ){
12dc8 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 .** if( <f
12dc9 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 ull-sync mode> )
12dca 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 xSync(<journal
12dcb 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 file>);.**
12dcc 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 <update nRec fi
12dcd 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a eld>.** } .*
12dce 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 * if( NOT SE
12dcf 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 QUENTIAL ) xSync
12dd0 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 (<journal file>)
12dd1 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 ;.** }.**.** T
12dd2 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e he Pager.needSyn
12dd3 63 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 c flag is never
12dd4 62 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f be set for tempo
12dd5 72 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 rary files, or a
12dd6 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 ny.** file opera
12dd7 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 ting in no-sync
12dd8 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 mode (Pager.noSy
12dd9 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 nc set to non-ze
12dda 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ro)..**.** If su
12ddb 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 ccessful, this r
12ddc 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 outine clears th
12ddd 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
12dde 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 C flag of every
12ddf 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 .** page current
12de0 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 ly held in memor
12de1 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 y before returni
12de2 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 ng SQLITE_OK. If
12de3 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 an IO.** error
12de4 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 is encountered,
12de5 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
12de6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
12de7 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
12de8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12de9 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 syncJournal(Page
12dea 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
12deb 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ( pPager->needSy
12dec 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 nc ){. assert
12ded 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
12dee 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ile );. if( p
12def 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
12df0 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
12df1 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a LMODE_MEMORY ){.
12df2 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 int rc;
12df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12df4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
12df5 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 turn code */.
12df6 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 const int iDc
12df7 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 = sqlite3OsDevi
12df8 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
12df9 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 s(pPager->fd);.
12dfa 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f assert( isO
12dfb 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
12dfc 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 );.. if( 0
12dfd 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f ==(iDc&SQLITE_IO
12dfe 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 CAP_SAFE_APPEND)
12dff 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
12e00 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 his block deals
12e01 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20 with an obscure
12e02 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20 problem. If the
12e03 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a last connection.
12e04 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ** that
12e05 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61 wrote to this da
12e06 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61 tabase was opera
12e07 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65 ting in persiste
12e08 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 nt-journal.
12e09 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e ** mode, then
12e0a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12e0b 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f e may at this po
12e0c 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 int actually be
12e0d 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a larger. *
12e0e 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75 * than Pager.jou
12e0f 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49 rnalOff bytes. I
12e10 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67 f the next thing
12e11 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a in the journal.
12e12 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 ** file
12e13 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 happens to be a
12e14 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28 journal-header (
12e15 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20 written as part
12e16 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a of the. *
12e17 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65 * previous conne
12e18 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 ctions transacti
12e19 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68 on), and a crash
12e1a 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 or power-failur
12e1b 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 e . ** oc
12e1c 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 curs after nRec
12e1d 69 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62 is updated but b
12e1e 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 efore this conne
12e1f 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20 ction writes .
12e20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e ** anythin
12e21 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f g else to the jo
12e22 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 urnal file (or c
12e23 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 ommits/rolls bac
12e24 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a k its . *
12e25 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 * transaction),
12e26 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20 then SQLite may
12e27 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20 become confused
12e28 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a when doing the .
12e29 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a ** hot-j
12e2a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 ournal rollback
12e2b 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65 following recove
12e2c 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20 ry. It may roll
12e2d 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20 back all.
12e2e 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e ** of this conn
12e2f 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68 ections data, th
12e30 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f en proceed to ro
12e31 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f lling back the o
12e32 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f ld,. ** o
12e33 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20 ut-of-date data
12e34 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e that follows it.
12e35 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 Database corrup
12e36 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a tion.. **
12e37 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77 . ** To w
12e38 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c ork around this,
12e39 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
12e3a 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72 file does appear
12e3b 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 to contain.
12e3c 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68 ** a valid h
12e3d 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 eader following
12e3e 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 Pager.journalOff
12e3f 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30 , then write a 0
12e40 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 x00. ** b
12e41 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74 yte to the start
12e42 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e of it to preven
12e43 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 t it from being
12e44 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20 recognized..
12e45 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
12e46 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74 * Variable iNext
12e47 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 HdrOffset is set
12e48 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61 to the offset a
12e49 74 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20 t which this.
12e4a 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 ** problema
12e4b 74 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20 tic header will
12e4c 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69 occur, if it exi
12e4d 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75 sts. aMagic is u
12e4e 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 sed . **
12e4f 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62 as a temporary b
12e50 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74 uffer to inspect
12e51 20 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c the first coupl
12e52 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20 e of bytes of.
12e53 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74 ** the pot
12e54 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 ential journal h
12e55 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a eader.. *
12e56 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e /. i64 iN
12e57 65 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20 extHdrOffset;.
12e58 20 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b u8 aMagic[
12e59 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b 8];..u8 zHeader[
12e5a 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
12e5b 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 agic)+4];...memc
12e5c 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 py(zHeader, aJou
12e5d 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
12e5e 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
12e5f 29 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a );..put32bits(&z
12e60 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
12e61 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 ournalMagic)], p
12e62 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 Pager->nRec);..
12e63 20 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f iNextHdrO
12e64 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 ffset = journalH
12e65 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 drOffset(pPager)
12e66 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
12e67 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
12e68 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 ger->jfd, aMagic
12e69 2c 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 , 8, iNextHdrOff
12e6a 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 set);. if
12e6b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12e6c 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 && 0==memcmp(aMa
12e6d 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 gic, aJournalMag
12e6e 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 ic, 8) ){.
12e6f 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
12e70 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 u8 zerobyte = 0
12e71 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
12e72 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
12e73 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 pPager->jfd, &ze
12e74 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 robyte, 1, iNext
12e75 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 HdrOffset);.
12e76 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
12e77 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12e78 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f && rc!=SQLITE_IO
12e79 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
12e7a 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
12e7b 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
12e7c 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 .. /* Wri
12e7d 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 te the nRec valu
12e7e 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e e into the journ
12e7f 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 al file header.
12e80 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a If in. **
12e81 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 full-synchronou
12e82 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 s mode, sync the
12e83 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 journal first.
12e84 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 This ensures tha
12e85 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c t. ** all
12e86 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 data has really
12e87 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 hit the disk be
12e88 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 fore nRec is upd
12e89 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 ated to mark.
12e8a 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 ** it as a
12e8b 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f candidate for ro
12e8c 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 llback..
12e8d 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 **. ** Th
12e8e 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 is is not requir
12e8f 65 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 ed if the persis
12e90 74 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f tent media suppo
12e91 72 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 rts the.
12e92 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 ** SAFE_APPEND p
12e93 72 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 roperty. Because
12e94 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 in this case it
12e95 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
12e96 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 . ** for
12e97 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f garbage data to
12e98 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 be appended to
12e99 74 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 the file, the nR
12e9a 65 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 ec field.
12e9b 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 ** is populated
12e9c 20 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 with 0xFFFFFFFF
12e9d 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 when the journa
12e9e 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 l header is writ
12e9f 74 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ten. ** a
12ea0 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 nd never needs t
12ea1 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 o be updated..
12ea2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
12ea3 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c if( pPager->ful
12ea4 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 lSync && 0==(iDc
12ea5 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 &SQLITE_IOCAP_SE
12ea6 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 QUENTIAL) ){.
12ea7 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
12ea8 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c E(("SYNC journal
12ea9 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 of %d\n", PAGER
12eaa 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
12eab 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 IOTRACE(
12eac 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 ("JSYNC %p\n", p
12ead 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 Pager)).
12eae 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12eaf 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 Sync(pPager->jfd
12eb0 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 , pPager->sync_f
12eb1 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 lags);.
12eb2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12eb3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
12eb4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12eb5 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 IOTRACE(("JHDR
12eb6 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 %p %lld\n", pPa
12eb7 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ger, pPager->jou
12eb8 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20 rnalHdr));.
12eb9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12eba 73 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20 sWrite(.
12ebb 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c pPager->jfd,
12ebc 20 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 zHeader, sizeof
12ebd 28 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65 (zHeader), pPage
12ebe 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29 r->journalHdr..)
12ebf 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
12ec0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
12ec1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
12ec2 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 . if( 0==(i
12ec3 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f Dc&SQLITE_IOCAP_
12ec4 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 SEQUENTIAL) ){.
12ec5 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
12ec6 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c E(("SYNC journal
12ec7 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 of %d\n", PAGER
12ec8 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
12ec9 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
12eca 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 JSYNC %p\n", pPa
12ecb 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 ger)). rc
12ecc 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
12ecd 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
12ece 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
12ecf 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 | . (pP
12ed0 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
12ed1 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 ==SQLITE_SYNC_FU
12ed2 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 LL?SQLITE_SYNC_D
12ed3 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 ATAONLY:0).
12ed4 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 );. if
12ed5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12ed6 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
12ed7 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
12ed8 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
12ed9 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 ile was just suc
12eda 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 cessfully synced
12edb 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 . Set Pager.need
12edc 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 Sync . ** to
12edd 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 zero and clear t
12ede 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
12edf 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 NC flag on all p
12ee0 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 agess.. */.
12ee1 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
12ee2 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 nc = 0;. pPag
12ee3 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 er->journalStart
12ee4 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 ed = 1;. sqli
12ee5 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 te3PcacheClearSy
12ee6 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e ncFlags(pPager->
12ee7 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 pPCache);. }..
12ee8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
12ee9 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
12eea 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
12eeb 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 first in a linke
12eec 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 d list of dirty
12eed 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a pages connected.
12eee 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e ** by the PgHdr.
12eef 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 pDirty pointer.
12ef0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 This function wr
12ef1 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 ites each one of
12ef2 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 the.** in-memor
12ef3 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c y pages in the l
12ef4 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 ist to the datab
12ef5 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 ase file. The ar
12ef6 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 gument may.** be
12ef7 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 NULL, represent
12ef8 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 ing an empty lis
12ef9 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 t. In this case
12efa 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
12efb 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a .** a no-op..**.
12efc 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 ** The pager mus
12efd 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 t hold at least
12efe 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
12eff 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
12f00 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e on.** is called.
12f01 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 Before writing
12f02 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 anything to the
12f03 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
12f04 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 his lock.** is u
12f05 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 pgraded to an EX
12f06 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 CLUSIVE lock. If
12f07 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 the lock cannot
12f08 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a be obtained,.**
12f09 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
12f0a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
12f0b 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 data is written
12f0c 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
12f0d 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 file..** .** If
12f0e 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 the pager is a t
12f0f 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 emp-file pager a
12f10 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 nd the actual fi
12f11 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a le-system file.*
12f12 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 * is not yet ope
12f13 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 n, it is created
12f14 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f and opened befo
12f15 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a re any data is .
12f16 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a ** written out..
12f17 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c **.** Once the l
12f18 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 ock has been upg
12f19 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 raded and, if ne
12f1a 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c cessary, the fil
12f1b 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 e opened,.** the
12f1c 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 pages are writt
12f1d 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 en out to the da
12f1e 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c tabase file in l
12f1f 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 ist order. Writi
12f20 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 ng.** a page is
12f21 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 skipped if it me
12f22 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 ets either of th
12f23 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 e following crit
12f24 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 eria:.**.** *
12f25 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 The page number
12f26 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
12f27 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 Pager.dbSize, or
12f28 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 .** * The PGHD
12f29 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 R_DONT_WRITE fla
12f2a 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 g is set on the
12f2b 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 page..**.** If w
12f2c 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 riting out a pag
12f2d 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 e causes the dat
12f2e 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 abase file to gr
12f2f 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 ow, Pager.dbFile
12f30 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 Size.** is updat
12f31 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 ed accordingly.
12f32 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 If page 1 is wri
12f33 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 tten out, then t
12f34 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a he value cached.
12f35 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 ** in Pager.dbFi
12f36 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 leVers[] is upda
12f37 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 ted to match the
12f38 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 new value store
12f39 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 d in.** the data
12f3a 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
12f3b 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 If everything i
12f3c 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 s successful, SQ
12f3d 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
12f3e 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 ned. If an IO er
12f3f 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 ror .** occurs,
12f40 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
12f41 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 is returned. Or
12f42 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 , if the EXCLUSI
12f43 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a VE lock cannot.*
12f44 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 * be obtained, S
12f45 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 QLITE_BUSY is re
12f46 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
12f47 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 c int pager_writ
12f48 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 e_pagelist(PgHdr
12f49 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 *pList){. Page
12f4a 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
12f4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f4c 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
12f4d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
12f4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f4f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
12f50 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 turn code */..
12f51 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d if( NEVER(pList=
12f52 3d 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c =0) ) return SQL
12f53 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 ITE_OK;. pPager
12f54 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 = pList->pPager
12f55 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 ;.. /* At this
12f56 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 point there may
12f57 62 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45 be either a RESE
12f58 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 RVED or EXCLUSIV
12f59 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 E lock on the.
12f5a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
12f5b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c . If there is al
12f5c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 ready an EXCLUSI
12f5d 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c VE lock, the fol
12f5e 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c lowing. ** call
12f5f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a is a no-op.. *
12f60 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 *. ** Moving th
12f61 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 e lock from RESE
12f62 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 RVED to EXCLUSIV
12f63 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c E actually invol
12f64 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 ves going. ** t
12f65 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d hrough an interm
12f66 65 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e ediate state PEN
12f67 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e DING. A PENDIN
12f68 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 G lock prevents
12f69 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 new. ** readers
12f6a 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 from attaching
12f6b 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
12f6c 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69 but is unsuffici
12f6d 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 ent for us to.
12f6e 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69 ** write. The i
12f6f 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 dea of a PENDING
12f70 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76 lock is to prev
12f71 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20 ent new readers
12f72 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 from. ** coming
12f73 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69 in while we wai
12f74 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72 t for existing r
12f75 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e eaders to clear.
12f76 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 . **. ** While
12f77 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
12f78 20 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74 the RESERVED st
12f79 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 ate, the origina
12f7a 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a l database file.
12f7b 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 ** is unchange
12f7c 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c d and we can rol
12f7d 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 lback without ha
12f7e 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b ving to playback
12f7f 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 the. ** journa
12f80 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 l into the origi
12f81 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c nal database fil
12f82 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e e. Once we tran
12f83 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 sition to. ** E
12f84 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61 XCLUSIVE, it mea
12f85 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
12f86 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68 file has been ch
12f87 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f anged and any ro
12f88 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c llback. ** will
12f89 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e require a journ
12f8a 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a al playback.. *
12f8b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
12f8c 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
12f8d 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72 _RESERVED );. r
12f8e 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f c = pager_wait_o
12f8f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 n_lock(pPager, E
12f90 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a XCLUSIVE_LOCK);.
12f91 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c . /* If the fil
12f92 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 e is a temp-file
12f93 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
12f94 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 n opened, open i
12f95 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69 t now. It. ** i
12f96 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 s not possible f
12f97 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65 or rc to be othe
12f98 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b r than SQLITE_OK
12f99 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a if this branch.
12f9a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61 ** is taken, a
12f9b 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f s pager_wait_on_
12f9c 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f lock() is a no-o
12f9d 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 p for temp-files
12f9e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 .. */. if( !is
12f9f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
12fa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
12fa1 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
12fa2 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f && rc==SQLITE_O
12fa3 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 K );. rc = pa
12fa4 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 gerOpentemp(pPag
12fa5 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 er, pPager->fd,
12fa6 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 pPager->vfsFlags
12fa7 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 );. }.. while(
12fa8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12fa9 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50 & pList ){. P
12faa 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74 gno pgno = pList
12fab 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 ->pgno;.. /*
12fac 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72 If there are dir
12fad 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
12fae 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 page cache with
12faf 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 page numbers gre
12fb0 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e ater. ** than
12fb1 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 Pager.dbSize, t
12fb2 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 his means sqlite
12fb3 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
12fb4 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 age() was called
12fb5 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 to. ** make
12fb6 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 the file smaller
12fb7 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 (presumably by
12fb8 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 auto-vacuum code
12fb9 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a ). Do not write.
12fba 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 ** any such
12fbb 70 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c pages to the fil
12fbc 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
12fbd 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 Also, do not wr
12fbe 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65 ite out any page
12fbf 20 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47 that has the PG
12fc0 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 HDR_DONT_WRITE f
12fc1 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28 lag. ** set (
12fc2 73 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61 set by sqlite3Pa
12fc3 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e gerDontWrite()).
12fc4 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 63 Note that if c
12fc5 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 20 20 20 ompiled with.
12fc6 20 2a 2a 20 53 51 4c 49 54 45 5f 53 45 43 55 52 ** SQLITE_SECUR
12fc7 45 5f 44 45 4c 45 54 45 20 74 68 65 20 50 47 48 E_DELETE the PGH
12fc8 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 62 69 DR_DONT_WRITE bi
12fc9 74 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 61 t is never set a
12fca 6e 64 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68 65 nd so. ** the
12fcb 20 73 65 63 6f 6e 64 20 74 65 73 74 20 69 73 20 second test is
12fcc 61 6c 77 61 79 73 20 74 72 75 65 2e 0a 20 20 20 always true..
12fcd 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f */. if( pgno
12fce 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 <=pPager->dbSize
12fcf 20 26 26 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 && 0==(pList->f
12fd0 6c 61 67 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f lags&PGHDR_DONT_
12fd1 57 52 49 54 45 29 20 29 7b 0a 20 20 20 20 20 20 WRITE) ){.
12fd2 69 36 34 20 6f 66 66 73 65 74 20 3d 20 28 70 67 i64 offset = (pg
12fd3 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 no-1)*(i64)pPage
12fd4 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 2f r->pageSize; /
12fd5 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 72 69 74 * Offset to writ
12fd6 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 e */. char
12fd7 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *pData;
12fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
12fda 61 20 74 6f 20 77 72 69 74 65 20 2a 2f 20 20 20 a to write */
12fdb 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f .. /* Enco
12fdc 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 de the database
12fdd 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28 */. CODEC2(
12fde 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70 pPager, pList->p
12fdf 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 2c 20 72 Data, pgno, 6, r
12fe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
12fe1 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a 20 20 20 EM, pData);..
12fe2 20 20 20 2f 2a 20 57 72 69 74 65 20 6f 75 74 20 /* Write out
12fe3 74 68 65 20 70 61 67 65 20 64 61 74 61 2e 20 2a the page data. *
12fe4 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c /. rc = sql
12fe5 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
12fe6 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 er->fd, pData, p
12fe7 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
12fe8 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 20 offset);..
12fe9 20 2f 2a 20 49 66 20 70 61 67 65 20 31 20 77 61 /* If page 1 wa
12fea 73 20 6a 75 73 74 20 77 72 69 74 74 65 6e 2c 20 s just written,
12feb 75 70 64 61 74 65 20 50 61 67 65 72 2e 64 62 46 update Pager.dbF
12fec 69 6c 65 56 65 72 73 20 74 6f 20 6d 61 74 63 68 ileVers to match
12fed 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 . ** the va
12fee 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65 64 20 69 lue now stored i
12fef 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
12ff0 69 6c 65 2e 20 49 66 20 77 72 69 74 69 6e 67 20 ile. If writing
12ff1 74 68 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 70 this . ** p
12ff2 61 67 65 20 63 61 75 73 65 64 20 74 68 65 20 64 age caused the d
12ff3 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
12ff4 67 72 6f 77 2c 20 75 70 64 61 74 65 20 64 62 46 grow, update dbF
12ff5 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20 20 20 20 ileSize. .
12ff6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e */. if( pgn
12ff7 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 o==1 ){.
12ff8 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
12ff9 64 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 dbFileVers, &pDa
12ffa 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 ta[24], sizeof(p
12ffb 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
12ffc 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 s));. }.
12ffd 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 if( pgno>pPag
12ffe 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 er->dbFileSize )
12fff 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 {. pPager
13000 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 ->dbFileSize = p
13001 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 gno;. }..
13002 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6e /* Update an
13003 79 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 73 y backup objects
13004 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e copying the con
13005 74 65 6e 74 73 20 6f 66 20 74 68 69 73 20 70 61 tents of this pa
13006 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 ger. */. sq
13007 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 lite3BackupUpdat
13008 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 e(pPager->pBacku
13009 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 70 4c p, pgno, (u8*)pL
1300a 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a 0a 20 20 ist->pData);..
1300b 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
1300c 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 "STORE %d page %
1300d 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c d hash(%08x)\n",
1300e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1300f 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
13010 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 er), pgno, pager
13011 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 _pagehash(pList)
13012 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 ));. IOTRAC
13013 45 28 28 22 50 47 4f 55 54 20 25 70 20 25 64 5c E(("PGOUT %p %d\
13014 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f n", pPager, pgno
13015 29 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f ));. PAGER_
13016 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
13017 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 er_writedb_count
13018 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 );. PAGER_I
13019 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 NCR(pPager->nWri
1301a 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a te);. }else{.
1301b 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
1301c 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61 (("NOSTORE %d pa
1301d 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 ge %d\n", PAGERI
1301e 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29 D(pPager), pgno)
1301f 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 );. }.#ifdef
13020 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
13021 45 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 ES. pList->pa
13022 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
13023 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a agehash(pList);.
13024 23 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 #endif. pList
13025 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 = pList->pDirty
13026 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
13027 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 rc;.}../*.** App
13028 65 6e 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20 end a record of
13029 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 the current stat
1302a 65 20 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f e of page pPg to
1302b 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
1302c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 . .** It is the
1302d 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 callers responsi
1302e 62 69 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75 bility to use su
1302f 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29 bjRequiresPage()
13030 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68 to check .** th
13031 61 74 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 at it is really
13032 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 required before
13033 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e calling this fun
13034 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
13035 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 successful, set
13036 74 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f the bit correspo
13037 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67 nding to pPg->pg
13038 6e 6f 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 no in the bitvec
13039 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 s.** for all ope
1303a 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66 n savepoints bef
1303b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
1303c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1303d 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 on returns SQLIT
1303e 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 E_OK if everythi
1303f 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
13040 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 , an IO.** error
13041 20 63 6f 64 65 20 69 66 20 74 68 65 20 61 74 74 code if the att
13042 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f empt to write to
13043 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
13044 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 fails, or .** S
13045 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 QLITE_NOMEM if a
13046 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 malloc fails wh
13047 69 6c 65 20 73 65 74 74 69 6e 67 20 61 20 62 69 ile setting a bi
13048 74 20 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 t in a savepoint
13049 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 .** bitvec..*/.s
1304a 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75 tatic int subjou
1304b 72 6e 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a rnalPage(PgHdr *
1304c 70 50 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pPg){. int rc =
1304d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 SQLITE_OK;. Pa
1304e 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
1304f 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 g->pPager;. if(
13050 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
13051 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 76 6f 69 sjfd) ){. voi
13052 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e d *pData = pPg->
13053 70 44 61 74 61 3b 0a 20 20 20 20 69 36 34 20 6f pData;. i64 o
13054 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e ffset = pPager->
13055 6e 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 nSubRec*(4+pPage
13056 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 r->pageSize);.
13057 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a char *pData2;.
13058 0a 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 . CODEC2(pPag
13059 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e er, pData, pPg->
1305a 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 pgno, 7, return
1305b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 SQLITE_NOMEM, pD
1305c 61 74 61 32 29 3b 0a 20 20 20 20 50 41 47 45 52 ata2);. PAGER
1305d 54 52 41 43 45 28 28 22 53 54 4d 54 2d 4a 4f 55 TRACE(("STMT-JOU
1305e 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c RNAL %d page %d\
1305f 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
13060 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 er), pPg->pgno))
13061 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 ;. . assert(
13062 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 pageInJournal(p
13063 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f Pg) || pPg->pgno
13064 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 >pPager->dbOrigS
13065 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ize );. rc =
13066 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
13067 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 74 er->sjfd, offset
13068 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
13069 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1306a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
1306b 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
1306c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 70 (pPager->sjfd, p
1306d 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
1306e 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b ageSize, offset+
1306f 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4);. }. }.
13070 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13071 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d K ){. pPager-
13072 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 >nSubRec++;.
13073 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13074 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a nSavepoint>0 );.
13075 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61 rc = addToSa
13076 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
13077 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
13078 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
13079 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}.../*.** Th
1307a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1307b 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 alled by the pca
1307c 63 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 che layer when i
1307d 74 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f t has reached so
1307e 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 me.** soft memor
1307f 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 y limit. The fir
13080 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 st argument is a
13081 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 pointer to a Pa
13082 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 ger object.** (c
13083 61 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e ast as a void*).
13084 20 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c The pager is al
13085 77 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 ways 'purgeable'
13086 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f (not an in-memo
13087 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e ry.** database).
13088 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
13089 6d 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 ment is a refere
1308a 6e 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 nce to a page th
1308b 61 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e at is .** curren
1308c 74 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61 tly dirty but ha
1308d 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 s no outstanding
1308e 20 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 references. The
1308f 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 page.** is alwa
13090 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ys associated wi
13091 74 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a th the Pager obj
13092 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 ect passed as th
13093 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 e first .** argu
13094 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
13095 6a 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 job of this func
13096 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 tion is to make
13097 70 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 pPg clean by wri
13098 74 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 ting its content
13099 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 s.** out to the
1309a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 database file, i
1309b 66 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 f possible. This
1309c 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e may involve syn
1309d 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 cing the.** jour
1309e 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a nal file. .**.**
1309f 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
130a0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
130a1 65 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c eClean() is call
130a2 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 ed on the page a
130a3 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 nd.** SQLITE_OK
130a4 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
130a5 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 IO error occurs
130a6 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 while trying to
130a7 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 make the.** page
130a8 20 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 clean, the IO e
130a9 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
130aa 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 urned. If the pa
130ab 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 ge cannot be.**
130ac 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 made clean for s
130ad 6f 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e ome other reason
130ae 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f , but no error o
130af 63 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 ccurs, then SQLI
130b0 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 TE_OK.** is retu
130b1 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 rned by sqlite3P
130b2 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 cacheMakeClean()
130b3 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a is not called..
130b4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
130b5 67 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a gerStress(void *
130b6 70 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a p, PgHdr *pPg){.
130b7 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
130b8 3d 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 = (Pager *)p;.
130b9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
130ba 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 OK;.. assert( p
130bb 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 Pg->pPager==pPag
130bc 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 er );. assert(
130bd 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
130be 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 _DIRTY );.. /*
130bf 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c The doNotSync fl
130c0 61 67 20 69 73 20 73 65 74 20 62 79 20 74 68 65 ag is set by the
130c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
130c2 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 68 te() function wh
130c3 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69 73 20 6a ile it. ** is j
130c4 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74 ournalling a set
130c5 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 of two or more
130c6 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 database pages t
130c7 68 61 74 20 61 72 65 20 73 74 6f 72 65 64 0a 20 hat are stored.
130c8 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 ** on the same
130c9 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e disk sector. Syn
130ca 63 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c cing the journal
130cb 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 is not allowed
130cc 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 69 73 20 while. ** this
130cd 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 20 is happening as
130ce 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 it is important
130cf 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 73 that all members
130d0 20 6f 66 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 of such a. **
130d1 73 65 74 20 6f 66 20 70 61 67 65 73 20 61 72 65 set of pages are
130d2 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 synced to disk
130d3 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c 20 69 66 together. So, if
130d4 20 74 68 65 20 70 61 67 65 20 74 68 69 73 20 66 the page this f
130d5 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 unction. ** is
130d6 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 63 trying to make c
130d7 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71 75 69 72 lean will requir
130d8 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 e a journal sync
130d9 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f 74 53 79 and the doNotSy
130da 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 nc. ** flag is
130db 73 65 74 2c 20 72 65 74 75 72 6e 20 77 69 74 68 set, return with
130dc 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 out doing anythi
130dd 6e 67 2e 20 54 68 65 20 70 63 61 63 68 65 20 6c ng. The pcache l
130de 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a ayer will. ** j
130df 75 73 74 20 68 61 76 65 20 74 6f 20 67 6f 20 61 ust have to go a
130e0 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f 63 61 74 head and allocat
130e1 65 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 e a new page buf
130e2 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 fer instead of.
130e3 20 2a 2a 20 72 65 75 73 69 6e 67 20 70 50 67 2e ** reusing pPg.
130e4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c . **. ** Simil
130e5 61 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67 arly, if the pag
130e6 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 er has already e
130e7 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 ntered the error
130e8 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 state, do not.
130e9 20 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 65 ** try to write
130ea 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
130eb 20 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 20 pPg to disk..
130ec 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
130ed 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 0a Pager->errCode).
130ee 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 64 || (pPager->d
130ef 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70 50 67 2d oNotSync && pPg-
130f0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
130f1 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a 20 20 20 D_SYNC). ){.
130f2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
130f3 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 79 6e K;. }.. /* Syn
130f4 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 c the journal fi
130f5 6c 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 le if required.
130f6 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c */. if( pPg->fl
130f7 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
130f8 59 4e 43 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 YNC ){. rc =
130f9 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 syncJournal(pPag
130fa 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d er);. if( rc=
130fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
130fc 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 ager->fullSync &
130fd 26 20 0a 20 20 20 20 20 20 21 28 70 50 61 67 65 & . !(pPage
130fe 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
130ff 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
13100 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a 20 20 20 E_MEMORY) &&.
13101 20 20 20 21 28 73 71 6c 69 74 65 33 4f 73 44 65 !(sqlite3OsDe
13102 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
13103 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26 ics(pPager->fd)&
13104 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 SQLITE_IOCAP_SAF
13105 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 20 29 7b E_APPEND). ){
13106 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e . pPager->n
13107 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 Rec = 0;. r
13108 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c c = writeJournal
13109 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 Hdr(pPager);.
1310a 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1310b 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1310c 6f 66 20 74 68 69 73 20 70 61 67 65 20 69 73 20 of this page is
1310d 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
1310e 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 0a current size of.
1310f 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
13110 65 20 69 6d 61 67 65 2c 20 69 74 20 6d 61 79 20 e image, it may
13111 6e 65 65 64 20 74 6f 20 62 65 20 77 72 69 74 74 need to be writt
13112 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f en to the sub-jo
13113 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 68 69 73 urnal.. ** This
13114 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20 is because the
13115 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 77 72 call to pager_wr
13116 69 74 65 5f 70 61 67 65 6c 69 73 74 28 29 20 62 ite_pagelist() b
13117 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 elow will not.
13118 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 ** actually writ
13119 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 e data to the fi
1311a 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e le in this case.
1311b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f 6e 73 69 . **. ** Consi
1311c 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e der the followin
1311d 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 65 76 g sequence of ev
1311e 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ents:. **. **
1311f 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 BEGIN;. **
13120 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 <journal page
13121 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c 6d 6f 64 X>. ** <mod
13122 69 66 79 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a ify page X>. **
13123 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 73 SAVEPOINT s
13124 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 73 p;. ** <s
13125 68 72 69 6e 6b 20 64 61 74 61 62 61 73 65 20 66 hrink database f
13126 69 6c 65 20 74 6f 20 59 20 70 61 67 65 73 3e 0a ile to Y pages>.
13127 20 20 2a 2a 20 20 20 20 20 20 20 70 61 67 65 72 ** pager
13128 53 74 72 65 73 73 28 70 61 67 65 20 58 29 0a 20 Stress(page X).
13129 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b ** ROLLBACK
1312a 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a 20 20 2a TO sp;. **. *
1312b 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65 6e * If (X>Y), then
1312c 20 77 68 65 6e 20 70 61 67 65 72 53 74 72 65 73 when pagerStres
1312d 73 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 s is called page
1312e 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 X will not be w
1312f 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f 75 74 20 ritten. ** out
13130 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
13131 66 69 6c 65 2c 20 62 75 74 20 77 69 6c 6c 20 62 file, but will b
13132 65 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20 74 e dropped from t
13133 68 65 20 63 61 63 68 65 2e 20 54 68 65 6e 2c 0a he cache. Then,.
13134 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 ** following t
13135 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 he "ROLLBACK TO
13136 73 70 22 20 73 74 61 74 65 6d 65 6e 74 2c 20 72 sp" statement, r
13137 65 61 64 69 6e 67 20 70 61 67 65 20 58 20 77 69 eading page X wi
13138 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20 64 61 74 ll read. ** dat
13139 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 a from the datab
1313a 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 77 ase file. This w
1313b 69 6c 6c 20 62 65 20 74 68 65 20 63 6f 70 79 20 ill be the copy
1313c 6f 66 20 70 61 67 65 20 58 20 61 73 20 69 74 0a of page X as it.
1313d 20 20 2a 2a 20 77 61 73 20 77 68 65 6e 20 74 68 ** was when th
1313e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 e transaction st
1313f 61 72 74 65 64 2c 20 6e 6f 74 20 61 73 20 69 74 arted, not as it
13140 20 77 61 73 20 77 68 65 6e 20 22 53 41 56 45 50 was when "SAVEP
13141 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a 20 77 61 OINT sp". ** wa
13142 73 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a s executed.. **
13143 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 . ** The soluti
13144 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 65 20 74 on is to write t
13145 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 20 he current data
13146 66 6f 72 20 70 61 67 65 20 58 20 69 6e 74 6f 20 for page X into
13147 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f the . ** sub-jo
13148 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f 77 20 28 urnal file now (
13149 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 if it is not alr
1314a 65 61 64 79 20 74 68 65 72 65 29 2c 20 73 6f 20 eady there), so
1314b 74 68 61 74 20 69 74 20 77 69 6c 6c 0a 20 20 2a that it will. *
1314c 2a 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f * be restored to
1314d 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c its current val
1314e 75 65 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c ue when the "ROL
1314f 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 69 73 20 LBACK TO sp" is
13150 0a 20 20 2a 2a 20 65 78 65 63 75 74 65 64 2e 0a . ** executed..
13151 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
13152 28 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 (. rc==SQLI
13153 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67 TE_OK && pPg->pg
13154 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a no>pPager->dbSiz
13155 65 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 e && subjRequire
13156 73 50 61 67 65 28 70 50 67 29 0a 20 20 29 20 29 sPage(pPg). ) )
13157 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f {. rc = subjo
13158 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a urnalPage(pPg);.
13159 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 }.. /* Write
1315a 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1315b 74 68 65 20 70 61 67 65 20 6f 75 74 20 74 6f 20 the page out to
1315c 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1315d 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d e. */. if( rc==
1315e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1315f 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 pPg->pDirty = 0
13160 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 ;. rc = pager
13161 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 _write_pagelist(
13162 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 pPg);. }.. /*
13163 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
13164 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69 66 28 clean. */. if(
13165 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13166 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 {. PAGERTRACE
13167 28 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67 (("STRESS %d pag
13168 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 e %d\n", PAGERID
13169 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
1316a 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 gno));. sqlit
1316b 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
1316c 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 n(pPg);. }.. r
1316d 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f eturn pager_erro
1316e 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d r(pPager, rc);.}
1316f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 .../*.** Allocat
13170 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 e and initialize
13171 20 61 20 6e 65 77 20 50 61 67 65 72 20 6f 62 6a a new Pager obj
13172 65 63 74 20 61 6e 64 20 70 75 74 20 61 20 70 6f ect and put a po
13173 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a 20 69 inter to it.** i
13174 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 68 65 20 n *ppPager. The
13175 70 61 67 65 72 20 73 68 6f 75 6c 64 20 65 76 65 pager should eve
13176 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 65 65 64 ntually be freed
13177 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 0a 2a by passing it.*
13178 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 * to sqlite3Page
13179 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 rClose()..**.**
1317a 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 61 72 The zFilename ar
1317b 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 61 gument is the pa
1317c 74 68 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 th to the databa
1317d 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 2e se file to open.
1317e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 .** If zFilename
1317f 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 20 is NULL then a
13180 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 randomly-named t
13181 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 emporary file is
13182 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 created.** and
13183 75 73 65 64 20 61 73 20 74 68 65 20 66 69 6c 65 used as the file
13184 20 74 6f 20 62 65 20 63 61 63 68 65 64 2e 20 54 to be cached. T
13185 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61 emporary files a
13186 72 65 20 62 65 20 64 65 6c 65 74 65 64 0a 2a 2a re be deleted.**
13187 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 automatically w
13188 68 65 6e 20 74 68 65 79 20 61 72 65 20 63 6c 6f hen they are clo
13189 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e 61 6d sed. If zFilenam
1318a 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 e is ":memory:"
1318b 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66 then .** all inf
1318c 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 ormation is held
1318d 20 69 6e 20 63 61 63 68 65 2e 20 49 74 20 69 73 in cache. It is
1318e 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74 never written t
1318f 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68 69 73 o disk. .** This
13190 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
13191 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d implement an in-
13192 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e memory database.
13193 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78 74 72 .**.** The nExtr
13194 61 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 a parameter spec
13195 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 ifies the number
13196 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 of bytes of spa
13197 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 ce allocated.**
13198 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 63 68 20 along with each
13199 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 page reference.
1319a 54 68 69 73 20 73 70 61 63 65 20 69 73 20 61 76 This space is av
1319b 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 ailable to the u
1319c 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 65 20 73 ser.** via the s
1319d 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
1319e 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a tra() API..**.**
1319f 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d The flags argum
131a0 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 ent is used to s
131a1 70 65 63 69 66 79 20 70 72 6f 70 65 72 74 69 65 pecify propertie
131a2 73 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 s that affect th
131a3 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f e.** operation o
131a4 66 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20 f the pager. It
131a5 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 should be passed
131a6 20 73 6f 6d 65 20 62 69 74 77 69 73 65 20 63 6f some bitwise co
131a7 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 mbination.** of
131a8 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a the PAGER_OMIT_J
131a9 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 47 45 52 OURNAL and PAGER
131aa 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66 6c 61 _NO_READLOCK fla
131ab 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 66 gs..**.** The vf
131ac 73 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 sFlags parameter
131ad 20 69 73 20 61 20 62 69 74 6d 61 73 6b 20 74 6f is a bitmask to
131ae 20 70 61 73 73 20 74 6f 20 74 68 65 20 66 6c 61 pass to the fla
131af 67 73 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 gs parameter.**
131b0 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 29 20 6d of the xOpen() m
131b1 65 74 68 6f 64 20 6f 66 20 74 68 65 20 73 75 70 ethod of the sup
131b2 70 6c 69 65 64 20 56 46 53 20 77 68 65 6e 20 6f plied VFS when o
131b3 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20 0a 2a pening files. .*
131b4 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
131b5 72 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f r object is allo
131b6 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 73 70 cated and the sp
131b7 65 63 69 66 69 65 64 20 66 69 6c 65 20 6f 70 65 ecified file ope
131b8 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 ned .** successf
131b9 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ully, SQLITE_OK
131ba 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
131bb 2a 70 70 50 61 67 65 72 20 73 65 74 20 74 6f 20 *ppPager set to
131bc 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 point to.** the
131bd 6e 65 77 20 70 61 67 65 72 20 6f 62 6a 65 63 74 new pager object
131be 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
131bf 63 75 72 73 2c 20 2a 70 70 50 61 67 65 72 20 69 curs, *ppPager i
131c0 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a s set to NULL.**
131c1 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 65 20 and error code
131c2 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 66 returned. This f
131c3 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
131c4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a rn SQLITE_NOMEM.
131c5 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ** (sqlite3Mallo
131c6 63 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 c() is used to a
131c7 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 29 2c llocate memory),
131c8 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
131c9 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75 73 20 or .** various
131ca 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20 65 72 SQLITE_IO_XXX er
131cb 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rors..*/.SQLITE_
131cc 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
131cd 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20 te3PagerOpen(.
131ce 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
131cf 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 s, /* The
131d0 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 virtual file sys
131d1 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 tem to use */.
131d2 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c Pager **ppPager,
131d3 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
131d4 20 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 Return the Page
131d5 72 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 r structure here
131d6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
131d7 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f *zFilename, /
131d8 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * Name of the da
131d9 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f tabase file to o
131da 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 pen */. int nEx
131db 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 tra,
131dc 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 /* Extra bytes
131dd 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 append to each
131de 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a in-memory page *
131df 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
131e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
131e1 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e flags controllin
131e2 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 g this file */.
131e3 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20 20 int vfsFlags,
131e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 /* fla
131e5 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
131e6 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 h to sqlite3_vfs
131e7 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 76 6f .xOpen() */. vo
131e8 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62 id (*xReinit)(Db
131e9 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e 63 74 69 Page*) /* Functi
131ea 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 61 6c 69 on to reinitiali
131eb 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 ze pages */.){.
131ec 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 u8 *pPtr;. Pag
131ed 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 20 er *pPager = 0;
131ee 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f /* Pager o
131ef 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61 74 bject to allocat
131f0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a e and return */.
131f1 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
131f2 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 E_OK; /* Re
131f3 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
131f4 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b nt tempFile = 0;
131f5 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
131f6 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 20 28 for temp files (
131f7 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20 incl. in-memory
131f8 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e 74 20 files) */. int
131f9 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 20 20 memDb = 0;
131fa 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
131fb 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 this is an in-me
131fc 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 69 mory file */. i
131fd 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b nt readOnly = 0;
131fe 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
131ff 69 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61 if this is a rea
13200 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f 0a 20 d-only file */.
13201 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 int journalFile
13202 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42 79 74 Size; /* Byt
13203 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 es to allocate f
13204 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 or each journal
13205 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 fd */. char *zP
13206 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 athname = 0;
13207 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20 74 6f /* Full path to
13208 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
13209 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d /. int nPathnam
1320a 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 e = 0; /*
1320b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1320c 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a 2f 0a in zPathname */.
1320d 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61 6c int useJournal
1320e 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45 = (flags & PAGE
1320f 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29 3d R_OMIT_JOURNAL)=
13210 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74 6f 20 =0; /* False to
13211 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a omit journal */.
13212 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f 63 6b int noReadlock
13213 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45 = (flags & PAGE
13214 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29 21 3d R_NO_READLOCK)!=
13215 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 0; /* True to o
13216 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f mit read-lock */
13217 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 69 7a . int pcacheSiz
13218 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 e = sqlite3Pcach
13219 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 20 2f eSize(); /
1321a 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 * Bytes to alloc
1321b 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 20 2a ate for PCache *
1321c 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65 44 66 /. u16 szPageDf
1321d 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 lt = SQLITE_DEFA
1321e 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 20 ULT_PAGE_SIZE;
1321f 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 65 20 /* Default page
13220 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 size */.. /* Fi
13221 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 75 63 gure out how muc
13222 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69 h space is requi
13223 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 red for each jou
13224 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 rnal file-handle
13225 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61 72 65 . ** (there are
13226 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20 74 68 two of them, th
13227 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 e main journal a
13228 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e nd the sub-journ
13229 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69 al). This. ** i
1322a 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 70 s the maximum sp
1322b 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
1322c 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f an in-memory jo
1322d 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c urnal file handl
1322e 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 e . ** and a re
1322f 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 66 69 gular journal fi
13230 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 65 20 le-handle. Note
13231 74 68 61 74 20 61 20 22 72 65 67 75 6c 61 72 20 that a "regular
13232 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a journal-handle".
13233 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20 77 72 ** may be a wr
13234 61 70 70 65 72 20 63 61 70 61 62 6c 65 20 6f 66 apper capable of
13235 20 63 61 63 68 69 6e 67 20 74 68 65 20 66 69 72 caching the fir
13236 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 st portion of th
13237 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 e journal. ** f
13238 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20 74 6f ile in memory to
13239 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 61 implement the a
1323a 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
1323b 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20 0a 20 mization (see .
1323c 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c 65 20 ** source file
1323d 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f journal.c).. */
1323e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f . if( sqlite3Jo
1323f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e urnalSize(pVfs)>
13240 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
13241 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a lSize() ){. j
13242 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d ournalFileSize =
13243 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4a ROUND8(sqlite3J
13244 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 ournalSize(pVfs)
13245 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
13246 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
13247 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 = ROUND8(sqlite3
13248 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 MemJournalSize()
13249 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 );. }.. /* Set
1324a 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 the output vari
1324b 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 able to NULL in
1324c 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20 6f 63 case an error oc
1324d 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 50 61 curs. */. *ppPa
1324e 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 ger = 0;.. /* C
1324f 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72 65 ompute and store
13250 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 the full pathna
13251 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 me in an allocat
13252 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 ed buffer pointe
13253 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 61 d. ** to by zPa
13254 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e thname, length n
13255 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 Pathname. Or, if
13256 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f this is a tempo
13257 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 rary file,. **
13258 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74 68 leave both nPath
13259 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e 61 name and zPathna
1325a 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a me set to 0.. *
1325b 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d /. if( zFilenam
1325c 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 e && zFilename[0
1325d 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e 61 ] ){. nPathna
1325e 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
1325f 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 61 hname+1;. zPa
13260 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
13261 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 65 Malloc(nPathname
13262 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50 61 *2);. if( zPa
13263 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 thname==0 ){.
13264 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13265 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 _NOMEM;. }.#i
13266 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
13267 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 69 T_MEMORYDB. i
13268 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e f( strcmp(zFilen
13269 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d ame,":memory:")=
1326a 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 44 =0 ){. memD
1326b 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50 61 b = 1;. zPa
1326c 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 thname[0] = 0;.
1326d 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a }else.#endif.
1326e 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50 61 74 {. zPat
1326f 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a hname[0] = 0; /*
13270 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 69 Make sure initi
13271 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 46 alized even if F
13272 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 61 ullPathname() fa
13273 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 ils */. rc
13274 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 = sqlite3OsFullP
13275 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 athname(pVfs, zF
13276 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 ilename, nPathna
13277 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a me, zPathname);.
13278 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61 74 68 }.. nPath
13279 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 name = sqlite3St
1327a 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 rlen30(zPathname
1327b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1327c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 74 QLITE_OK && nPat
1327d 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 hname+8>pVfs->mx
1327e 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 Pathname ){.
1327f 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
13280 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 is taken when t
13281 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 20 he journal path
13282 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 required by.
13283 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
13284 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 77 e being opened w
13285 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 6e ill be more than
13286 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
13287 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 e. ** bytes
13288 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 73 in length. This
13289 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 means the datab
1328a 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 ase cannot be op
1328b 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 61 ened,. ** a
1328c 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s it will not be
1328d 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 possible to ope
1328e 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
1328f 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 20 le or even.
13290 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 20 ** check for a
13291 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 6f hot-journal befo
13292 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 20 re reading..
13293 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
13294 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
13295 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
13296 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13297 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
13298 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
13299 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1329a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1329b 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 Allocate memory
1329c 66 6f 72 20 74 68 65 20 50 61 67 65 72 20 73 74 for the Pager st
1329d 72 75 63 74 75 72 65 2c 20 50 43 61 63 68 65 20 ructure, PCache
1329e 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 20 2a 2a object, the. **
1329f 20 74 68 72 65 65 20 66 69 6c 65 20 64 65 73 63 three file desc
132a0 72 69 70 74 6f 72 73 2c 20 74 68 65 20 64 61 74 riptors, the dat
132a1 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 abase file name
132a2 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 and the journal
132a3 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 6d 65 2e . ** file name.
132a4 20 54 68 65 20 6c 61 79 6f 75 74 20 69 6e 20 6d The layout in m
132a5 65 6d 6f 72 79 20 69 73 20 61 73 20 66 6f 6c 6c emory is as foll
132a6 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 ows:. **. **
132a7 20 20 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 Pager object
132a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132a9 20 20 20 28 73 69 7a 65 6f 66 28 50 61 67 65 72 (sizeof(Pager
132aa 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 ) bytes). **
132ab 20 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20 PCache object
132ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132ad 20 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 (sqlite3Pcache
132ae 53 69 7a 65 28 29 20 62 79 74 65 73 29 0a 20 20 Size() bytes).
132af 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 65 20 ** Database
132b0 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 file handle
132b1 20 20 20 20 20 20 20 28 70 56 66 73 2d 3e 73 7a (pVfs->sz
132b2 4f 73 46 69 6c 65 20 62 79 74 65 73 29 0a 20 20 OsFile bytes).
132b3 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75 72 6e ** Sub-journ
132b4 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 al file handle
132b5 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 (journalF
132b6 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a 20 ileSize bytes).
132b7 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a 6f 75 ** Main jou
132b8 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 rnal file handle
132b9 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c (journal
132ba 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a FileSize bytes).
132bb 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 ** Databas
132bc 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 e file name
132bd 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e (nPathn
132be 61 6d 65 2b 31 20 62 79 74 65 73 29 0a 20 20 2a ame+1 bytes). *
132bf 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 * Journal fi
132c0 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 le name
132c1 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 (nPathname
132c2 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 20 2a 2f +8+1 bytes). */
132c3 0a 20 20 70 50 74 72 20 3d 20 28 75 38 20 2a 29 . pPtr = (u8 *)
132c4 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
132c5 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38 28 73 69 o(. ROUND8(si
132c6 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 20 2b zeof(*pPager)) +
132c7 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 /* Pager s
132c8 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 tructure */.
132c9 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a ROUND8(pcacheSiz
132ca 65 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f e) + /
132cb 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20 * PCache object
132cc 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 56 */. ROUND8(pV
132cd 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 20 2b 20 fs->szOsFile) +
132ce 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 /* The mai
132cf 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 20 20 n db file */.
132d0 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
132d1 20 2a 20 32 20 2b 20 20 20 20 20 20 20 20 20 20 * 2 +
132d2 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f 75 72 6e /* The two journ
132d3 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a 20 20 20 al files */ .
132d4 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 20 2b nPathname + 1 +
132d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132d6 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a /* zFilename */.
132d7 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 nPathname +
132d8 38 20 2b 20 31 20 20 20 20 20 20 20 20 20 20 20 8 + 1
132d9 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a /* zJournal *
132da 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 /. );. assert(
132db 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
132dc 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f 49 4e 54 NMENT(SQLITE_INT
132dd 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e 61 6c 46 _TO_PTR(journalF
132de 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a 20 20 69 ileSize)) );. i
132df 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20 20 f( !pPtr ){.
132e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 sqlite3_free(zPa
132e1 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 thname);. ret
132e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
132e3 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 20 3d ;. }. pPager =
132e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 (P
132e5 61 67 65 72 2a 29 28 70 50 74 72 29 3b 0a 20 20 ager*)(pPtr);.
132e6 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 20 pPager->pPCache
132e7 3d 20 20 20 20 28 50 43 61 63 68 65 2a 29 28 70 = (PCache*)(p
132e8 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 73 69 Ptr += ROUND8(si
132e9 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 29 3b zeof(*pPager)));
132ea 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 . pPager->fd =
132eb 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a (sqlite3_file*
132ec 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 )(pPtr += ROUND8
132ed 28 70 63 61 63 68 65 53 69 7a 65 29 29 3b 0a 20 (pcacheSize));.
132ee 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 20 3d 20 pPager->sjfd =
132ef 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 (sqlite3_file*)(
132f0 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 pPtr += ROUND8(p
132f1 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 29 3b Vfs->szOsFile));
132f2 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3d . pPager->jfd =
132f3 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a (sqlite3_file*
132f4 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 )(pPtr += journa
132f5 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 lFileSize);. pP
132f6 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 ager->zFilename
132f7 3d 20 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 = (char*)(pPt
132f8 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 r += journalFile
132f9 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 72 74 28 Size);. assert(
132fa 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
132fb 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d 3e 6a 66 NMENT(pPager->jf
132fc 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c d) );.. /* Fill
132fd 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 7a 46 in the Pager.zF
132fe 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 61 67 65 ilename and Page
132ff 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 66 66 65 r.zJournal buffe
13300 72 73 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e rs, if required.
13301 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 74 68 6e */. if( zPathn
13302 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ame ){. pPage
13303 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 20 20 r->zJournal =
13304 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 (char*)(pPtr +=
13305 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 29 3b 0a nPathname + 1);.
13306 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 memcpy(pPage
13307 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 r->zFilename, zP
13308 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 athname, nPathna
13309 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 me);. memcpy(
1330a 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
1330b 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 , zPathname, nPa
1330c 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d thname);. mem
1330d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f cpy(&pPager->zJo
1330e 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d urnal[nPathname]
1330f 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 38 29 , "-journal", 8)
13310 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 ;. if( pPager
13311 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d ->zFilename[0]==
13312 30 20 29 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 0 ) pPager->zJou
13313 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 rnal[0] = 0;.
13314 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 sqlite3_free(zP
13315 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 athname);. }.
13316 70 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 pPager->pVfs = p
13317 56 66 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 Vfs;. pPager->v
13318 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 fsFlags = vfsFla
13319 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 gs;.. /* Open t
1331a 68 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 he pager file..
1331b 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e */. if( zFilen
1331c 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
1331d 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62 20 29 7b [0] && !memDb ){
1331e 0a 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 . int fout =
1331f 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
13320 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 /* VFS fla
13321 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 78 gs returned by x
13322 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 Open() */. rc
13323 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
13324 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
13325 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 Filename, pPager
13326 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 ->fd, vfsFlags,
13327 26 66 6f 75 74 29 3b 0a 20 20 20 20 72 65 61 64 &fout);. read
13328 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c Only = (fout&SQL
13329 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
1332a 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 Y);.. /* If t
1332b 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 he file was succ
1332c 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 essfully opened
1332d 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 for read/write a
1332e 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 ccess,. ** ch
1332f 6f 6f 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 oose a default p
13330 61 67 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 age size in case
13331 20 77 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 we have to crea
13332 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 te the. ** da
13333 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 tabase file. The
13334 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 default page si
13335 7a 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 ze is the maximu
13336 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 m of:. **.
13337 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f ** + SQLITE_
13338 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
13339 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 E,. ** + T
1333a 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1333b 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 d by sqlite3OsSe
1333c 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a ctorSize(). *
1333d 2a 20 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 * + The large
1333e 73 74 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 st page size tha
1333f 74 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e t can be written
13340 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 atomically..
13341 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
13342 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 SQLITE_OK && !re
13343 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 adOnly ){.
13344 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 setSectorSize(pP
13345 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 ager);. ass
13346 65 72 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55 ert(SQLITE_DEFAU
13347 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 LT_PAGE_SIZE<=SQ
13348 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
13349 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 _PAGE_SIZE);.
1334a 20 20 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c if( szPageDfl
1334b 74 3c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 t<pPager->sector
1334c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
1334d 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 if( pPager->sect
1334e 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 orSize>SQLITE_MA
1334f 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
13350 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 IZE ){.
13351 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 szPageDflt = SQ
13352 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
13353 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 _PAGE_SIZE;.
13354 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13355 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 szPageDflt
13356 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 73 = (u16)pPager->s
13357 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 ectorSize;.
13358 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 }. }.#if
13359 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
1335a 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
1335b 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 {. i
1335c 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 nt iDc = sqlite3
1335d 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
1335e 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
1335f 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 fd);. int
13360 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ii;. ass
13361 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
13362 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 _ATOMIC512==(512
13363 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 >>8));. a
13364 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
13365 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 AP_ATOMIC64K==(6
13366 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 5536>>8));.
13367 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
13368 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
13369 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a E_SIZE<=65536);.
1336a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73 for(ii=s
1336b 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 zPageDflt; ii<=S
1336c 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
1336d 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d T_PAGE_SIZE; ii=
1336e 69 69 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20 ii*2){.
1336f 20 69 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 if( iDc&(SQLITE
13370 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 _IOCAP_ATOMIC|(i
13371 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20 i>>8)) ){.
13372 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
13373 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 = ii;.
13374 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
13375 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
13376 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
13377 2a 20 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79 * If a temporary
13378 20 66 69 6c 65 20 69 73 20 72 65 71 75 65 73 74 file is request
13379 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 ed, it is not op
1337a 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 ened immediately
1337b 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 .. ** In this
1337c 20 63 61 73 65 20 77 65 20 61 63 63 65 70 74 20 case we accept
1337d 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
1337e 20 73 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 size and delay
1337f 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 actually. **
13380 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 opening the file
13381 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 until the first
13382 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 call to OsWrite
13383 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ().. **. *
13384 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 * This branch is
13385 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e also run for an
13386 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
13387 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 ase. An in-memor
13388 79 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 y. ** databas
13389 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
1338a 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 a temp-file tha
1338b 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 t is never writt
1338c 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a en out to. **
1338d 20 64 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61 disk and uses a
1338e 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c n in-memory roll
1338f 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 back journal..
13390 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 */ . tempFi
13391 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 le = 1;. pPag
13392 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
13393 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 R_EXCLUSIVE;.
13394 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 76 66 73 readOnly = (vfs
13395 46 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 Flags&SQLITE_OPE
13396 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d N_READONLY);. }
13397 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f .. /* The follo
13398 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 wing call to Pag
13399 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 29 20 erSetPagesize()
1339a 73 65 72 76 65 73 20 74 6f 20 73 65 74 20 74 68 serves to set th
1339b 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a e value of . **
1339c 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 Pager.pageSize
1339d 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 and to allocate
1339e 74 68 65 20 50 61 67 65 72 2e 70 54 6d 70 53 70 the Pager.pTmpSp
1339f 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f ace buffer.. */
133a0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
133a1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
133a2 72 74 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 rt( pPager->memD
133a3 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d b==0 );. rc =
133a4 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
133a5 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c Pagesize(pPager,
133a6 20 26 73 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31 &szPageDflt, -1
133a7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
133a8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
133a9 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 ;. }.. /* If a
133aa 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 n error occurred
133ab 20 69 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 in either of th
133ac 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20 e blocks above,
133ad 66 72 65 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 free the . ** P
133ae 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61 ager structure a
133af 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c nd close the fil
133b0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 e.. */. if( rc
133b1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
133b2 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 assert( !pPag
133b3 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 3b er->pTmpSpace );
133b4 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
133b5 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ose(pPager->fd);
133b6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
133b7 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 72 e(pPager);. r
133b8 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
133b9 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
133ba 68 65 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 he PCache object
133bb 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e . */. assert( n
133bc 45 78 74 72 61 3c 31 30 30 30 20 29 3b 0a 20 20 Extra<1000 );.
133bd 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 nExtra = ROUND8(
133be 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 74 nExtra);. sqlit
133bf 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50 e3PcacheOpen(szP
133c0 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c ageDflt, nExtra,
133c1 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 20 !memDb,.
133c2 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d 65 !me
133c3 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 3a mDb?pagerStress:
133c4 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65 0, (void *)pPage
133c5 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 r, pPager->pPCac
133c6 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 he);.. PAGERTRA
133c7 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 5c CE(("OPEN %d %s\
133c8 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 n", FILEHANDLEID
133c9 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 (pPager->fd), pP
133ca 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 ager->zFilename)
133cb 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f );. IOTRACE(("O
133cc 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 PEN %p %s\n", pP
133cd 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 ager, pPager->zF
133ce 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61 ilename)).. pPa
133cf 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
133d0 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c = (u8)useJournal
133d1 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 ;. pPager->noRe
133d2 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 64 adlock = (noRead
133d3 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c 79 lock && readOnly
133d4 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 61 ) ?1:0;. /* pPa
133d5 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 ger->stmtOpen =
133d6 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
133d7 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 r->stmtInUse = 0
133d8 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
133d9 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 ->nRef = 0; */.
133da 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
133db 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 62 alid = (u8)memDb
133dc 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 ;. /* pPager->s
133dd 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a tmtSize = 0; */.
133de 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
133df 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 tJSize = 0; */.
133e0 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 /* pPager->nPag
133e1 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 e = 0; */. pPag
133e2 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c er->mxPgno = SQL
133e3 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 ITE_MAX_PAGE_COU
133e4 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d NT;. /* pPager-
133e5 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55 >state = PAGER_U
133e6 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 NLOCK; */. asse
133e7 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
133e8 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f e == (tempFile ?
133e9 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
133ea 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 : PAGER_UNLOCK)
133eb 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d );. /* pPager-
133ec 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f >errMask = 0; */
133ed 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 . pPager->tempF
133ee 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 ile = (u8)tempFi
133ef 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 65 le;. assert( te
133f0 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f mpFile==PAGER_LO
133f1 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
133f2 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74 . || t
133f3 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c empFile==PAGER_L
133f4 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
133f5 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 SIVE );. assert
133f6 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ( PAGER_LOCKINGM
133f7 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d 31 ODE_EXCLUSIVE==1
133f8 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 );. pPager->ex
133f9 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 clusiveMode = (u
133fa 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70 8)tempFile; . p
133fb 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
133fc 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d ntDone = pPager-
133fd 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 >tempFile;. pPa
133fe 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 38 ger->memDb = (u8
133ff 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 )memDb;. pPager
13400 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 38 ->readOnly = (u8
13401 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 )readOnly;. /*
13402 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
13403 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73 73 65 72 = 0; */. asser
13404 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c 20 7c 7c t( useJournal ||
13405 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
13406 65 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e e );. pPager->n
13407 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e oSync = pPager->
13408 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 tempFile;. pPag
13409 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 70 er->fullSync = p
1340a 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3f 30 Pager->noSync ?0
1340b 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 :1;. pPager->sy
1340c 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 nc_flags = SQLIT
1340d 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 E_SYNC_NORMAL;.
1340e 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 /* pPager->pFir
1340f 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 st = 0; */. /*
13410 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79 pPager->pFirstSy
13411 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f nced = 0; */. /
13412 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 20 * pPager->pLast
13413 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 = 0; */. pPager
13414 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75 31 36 29 ->nExtra = (u16)
13415 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 72 nExtra;. pPager
13416 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
13417 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 it = SQLITE_DEFA
13418 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 ULT_JOURNAL_SIZE
13419 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 74 _LIMIT;. assert
1341a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1341b 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65 >fd) || tempFile
1341c 20 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 );. setSectorS
1341d 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 ize(pPager);. i
1341e 66 28 20 21 75 73 65 4a 6f 75 72 6e 61 6c 20 29 f( !useJournal )
1341f 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
13420 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
13421 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13422 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6d F;. }else if( m
13423 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 50 61 67 emDb ){. pPag
13424 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 er->journalMode
13425 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d = PAGER_JOURNALM
13426 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a ODE_MEMORY;. }.
13427 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 78 42 75 /* pPager->xBu
13428 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 3b 20 2a syHandler = 0; *
13429 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 /. /* pPager->p
1342a 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d BusyHandlerArg =
1342b 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 0; */. pPager-
1342c 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78 52 65 >xReiniter = xRe
1342d 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65 6d 73 65 init;. /* memse
1342e 74 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 2c t(pPager->aHash,
1342f 20 30 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 0, sizeof(pPage
13430 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20 r->aHash)); */.
13431 20 2a 70 70 50 61 67 65 72 20 3d 20 70 50 61 67 *ppPager = pPag
13432 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c er;. return SQL
13433 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a ITE_OK;.}..../*.
13434 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
13435 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
13436 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 transitioning f
13437 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b rom PAGER_UNLOCK
13438 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 53 48 41 to.** PAGER_SHA
13439 52 45 44 20 73 74 61 74 65 2e 20 49 74 20 74 65 RED state. It te
1343a 73 74 73 20 69 66 20 74 68 65 72 65 20 69 73 20 sts if there is
1343b 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 70 72 a hot journal pr
1343c 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 esent in.** the
1343d 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20 file-system for
1343e 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e the given pager.
1343f 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 A hot journal i
13440 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a 2a 20 6e s one that .** n
13441 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 eeds to be playe
13442 64 20 62 61 63 6b 2e 20 41 63 63 6f 72 64 69 6e d back. Accordin
13443 67 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 g to this functi
13444 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 on, a hot-journa
13445 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69 73 74 73 l.** file exists
13446 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e if the followin
13447 67 20 63 72 69 74 65 72 69 61 20 61 72 65 20 6d g criteria are m
13448 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 et:.**.** * Th
13449 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
1344a 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c xists in the fil
1344b 65 20 73 79 73 74 65 6d 2c 20 61 6e 64 0a 2a 2a e system, and.**
1344c 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65 73 73 20 * No process
1344d 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 holds a RESERVED
1344e 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
1344f 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13450 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 file, and.**
13451 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
13452 69 6c 65 20 69 74 73 65 6c 66 20 69 73 20 67 72 ile itself is gr
13453 65 61 74 65 72 20 74 68 61 6e 20 30 20 62 79 74 eater than 0 byt
13454 65 73 20 69 6e 20 73 69 7a 65 2c 20 61 6e 64 0a es in size, and.
13455 2a 2a 20 20 20 2a 20 54 68 65 20 66 69 72 73 74 ** * The first
13456 20 62 79 74 65 20 6f 66 20 74 68 65 20 6a 6f 75 byte of the jou
13457 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
13458 20 61 6e 64 20 69 73 20 6e 6f 74 20 30 78 30 30 and is not 0x00
13459 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
1345a 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 urrent size of t
1345b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1345c 20 69 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 is 0 but a jour
1345d 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 nal file.** exis
1345e 74 73 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 ts, that is prob
1345f 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 ably an old jour
13460 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 nal left over fr
13461 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 om a prior.** da
13462 74 61 62 61 73 65 20 77 69 74 68 20 74 68 65 20 tabase with the
13463 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e 20 74 68 same name. In th
13464 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 is case the jour
13465 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6a nal file is.** j
13466 75 73 74 20 64 65 6c 65 74 65 64 20 75 73 69 6e ust deleted usin
13467 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a 70 45 78 g OsDelete, *pEx
13468 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 30 ists is set to 0
13469 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a and SQLITE_OK.*
1346a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a * is returned..*
1346b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1346c 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 65 63 6b e does not check
1346d 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 6d if there is a m
1346e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1346f 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20 74 68 65 lename.** at the
13470 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
13471 2e 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 61 . If there is, a
13472 6e 64 20 74 68 61 74 20 6d 61 73 74 65 72 20 6a nd that master j
13473 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 64 ournal file.** d
13474 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 oes not exist, t
13475 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
13476 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 6c file is not real
13477 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68 69 73 0a ly hot. In this.
13478 2a 2a 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 ** case this rou
13479 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e tine will return
1347a 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76 a false-positiv
1347b 65 2e 20 54 68 65 20 70 61 67 65 72 5f 70 6c 61 e. The pager_pla
1347c 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 yback().** routi
1347d 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f 76 65 72 ne will discover
1347e 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
1347f 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 l file is not re
13480 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20 0a 2a 2a ally hot and .**
13481 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 69 will not roll i
13482 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 t back. .**.** I
13483 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 f a hot-journal
13484 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 20 74 6f file is found to
13485 20 65 78 69 73 74 2c 20 2a 70 45 78 69 73 74 73 exist, *pExists
13486 20 69 73 20 73 65 74 20 74 6f 20 31 20 61 6e 64 is set to 1 and
13487 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 .** SQLITE_OK r
13488 65 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 20 68 eturned. If no h
13489 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ot-journal file
1348a 69 73 20 70 72 65 73 65 6e 74 2c 20 2a 70 45 78 is present, *pEx
1348b 69 73 74 73 20 69 73 0a 2a 2a 20 73 65 74 20 74 ists is.** set t
1348c 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f o 0 and SQLITE_O
1348d 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 K returned. If a
1348e 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
1348f 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 0a 2a s while trying.*
13490 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 * to determine w
13491 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
13492 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot-journal file
13493 20 65 78 69 73 74 73 2c 20 74 68 65 20 49 4f 20 exists, the IO
13494 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
13495 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
13496 65 20 76 61 6c 75 65 20 6f 66 20 2a 70 45 78 69 e value of *pExi
13497 73 74 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 sts is undefined
13498 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13499 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 hasHotJournal(Pa
1349a 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
1349b 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71 *pExists){. sq
1349c 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 lite3_vfs * cons
1349d 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d t pVfs = pPager-
1349e 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b >pVfs;. int rc;
1349f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
134a0 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
134a1 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 65 code */. int e
134a2 78 69 73 74 73 3b 20 20 20 20 20 20 20 20 20 20 xists;
134a3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
134a4 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 if a journal fi
134a5 6c 65 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f le is present */
134a6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
134a7 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
134a8 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f t( pPager->useJo
134a9 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 urnal );. asser
134aa 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
134ab 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 ->fd) );. asser
134ac 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 t( !isOpen(pPage
134ad 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 61 73 73 r->jfd) );. ass
134ae 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
134af 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 52 te <= PAGER_SHAR
134b0 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 ED );.. *pExist
134b1 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 s = 0;. rc = sq
134b2 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
134b3 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
134b4 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 rnal, SQLITE_ACC
134b5 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 ESS_EXISTS, &exi
134b6 73 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d sts);. if( rc==
134b7 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 SQLITE_OK && exi
134b8 73 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c sts ){. int l
134b9 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20 ocked;
134ba 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
134bb 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 f some process h
134bc 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20 olds a RESERVED
134bd 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 lock */.. /*
134be 52 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 Race condition h
134bf 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 20 70 72 ere: Another pr
134c0 6f 63 65 73 73 20 6d 69 67 68 74 20 68 61 76 65 ocess might have
134c1 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 been holding th
134c2 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 52 45 53 e. ** the RES
134c3 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e 64 20 68 ERVED lock and h
134c4 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 70 ave a journal op
134c5 65 6e 20 61 74 20 74 68 65 20 73 71 6c 69 74 65 en at the sqlite
134c6 33 4f 73 41 63 63 65 73 73 28 29 20 0a 20 20 20 3OsAccess() .
134c7 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65 2c 20 ** call above,
134c8 62 75 74 20 74 68 65 6e 20 64 65 6c 65 74 65 20 but then delete
134c9 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 the journal and
134ca 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b 20 62 65 drop the lock be
134cb 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 65 20 67 fore. ** we g
134cc 65 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 et to the follow
134cd 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 43 68 65 ing sqlite3OsChe
134ce 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 ckReservedLock()
134cf 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 61 74 0a call. If that.
134d0 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 ** is the ca
134d1 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 se, this routine
134d2 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 74 68 65 might think the
134d3 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 re is a hot jour
134d4 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 nal when. **
134d5 69 6e 20 66 61 63 74 20 74 68 65 72 65 20 69 73 in fact there is
134d6 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 72 65 73 none. This res
134d7 75 6c 74 73 20 69 6e 20 61 20 66 61 6c 73 65 2d ults in a false-
134d8 70 6f 73 69 74 69 76 65 20 77 68 69 63 68 20 77 positive which w
134d9 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 ill. ** be de
134da 61 6c 74 20 77 69 74 68 20 62 79 20 74 68 65 20 alt with by the
134db 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 69 6e 65 playback routine
134dc 2e 20 20 54 69 63 6b 65 74 20 23 33 38 38 33 2e . Ticket #3883.
134dd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
134de 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
134df 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67 eservedLock(pPag
134e0 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29 er->fd, &locked)
134e1 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
134e2 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b LITE_OK && !lock
134e3 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ed ){. int
134e4 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a nPage;.. /*
134e5 20 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 Check the size
134e6 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
134e7 66 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 file. If it cons
134e8 69 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c ists of 0 pages,
134e9 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 . ** then d
134ea 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
134eb 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 l file. See the
134ec 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 header comment a
134ed 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 bove for .
134ee 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 ** the reasoning
134ef 20 68 65 72 65 2e 20 20 44 65 6c 65 74 65 20 74 here. Delete t
134f0 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 he obsolete jour
134f1 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65 72 0a 20 nal file under.
134f2 20 20 20 20 20 2a 2a 20 61 20 52 45 53 45 52 56 ** a RESERV
134f3 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76 6f 69 64 ED lock to avoid
134f4 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 race conditions
134f5 20 61 6e 64 20 74 6f 20 61 76 6f 69 64 20 76 69 and to avoid vi
134f6 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20 20 2a 2a olating. **
134f7 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20 20 20 20 [H33020]..
134f8 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 */. rc = s
134f9 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
134fa 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 ount(pPager, &nP
134fb 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
134fc 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
134fd 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 50 61 . if( nPa
134fe 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
134ff 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
13500 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
13501 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
13502 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
13503 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f r->fd, RESERVED_
13504 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b LOCK)==SQLITE_OK
13505 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13506 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
13507 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
13508 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 ournal, 0);.
13509 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
1350a 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e sUnlock(pPager->
1350b 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 fd, SHARED_LOCK)
1350c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1350d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1350e 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
1350f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
13510 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
13511 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
13512 78 69 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 xists and no oth
13513 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 er connection ha
13514 73 20 61 20 72 65 73 65 72 76 65 64 0a 20 20 20 s a reserved.
13515 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 ** or gre
13516 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ater lock on the
13517 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
13518 4e 6f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74 Now check that t
13519 68 65 72 65 20 69 73 0a 20 20 20 20 20 20 20 20 here is.
1351a 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e ** at least on
1351b 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 e non-zero bytes
1351c 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
1351d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1351e 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 e.. **
1351f 49 66 20 74 68 65 72 65 20 69 73 2c 20 74 68 65 If there is, the
13520 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 20 74 68 n we consider th
13521 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 is journal to be
13522 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 hot. If not, .
13523 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63 ** it c
13524 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 an be ignored..
13525 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
13526 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 int f = SQ
13527 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
13528 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d LY|SQLITE_OPEN_M
13529 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 AIN_JOURNAL;.
1352a 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1352b 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
1352c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
1352d 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 , pPager->jfd, f
1352e 2c 20 26 66 29 3b 0a 20 20 20 20 20 20 20 20 20 , &f);.
1352f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13530 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
13531 20 20 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a u8 first = 0;.
13532 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
13533 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
13534 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 Pager->jfd, (voi
13535 64 20 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30 d *)&first, 1, 0
13536 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
13537 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
13538 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
13539 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1353a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1353b 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1353c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1353d 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
1353e 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 >jfd);.
1353f 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 28 66 *pExists = (f
13540 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 irst!=0);.
13541 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 }else if( rc
13542 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 ==SQLITE_CANTOPE
13543 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 N ){.
13544 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 /* If we cannot
13545 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 open the rollba
13546 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ck journal file
13547 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 65 20 in order to see
13548 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a if. *
13549 2a 20 69 74 73 20 68 61 73 20 61 20 7a 65 72 6f * its has a zero
1354a 20 68 65 61 64 65 72 2c 20 74 68 61 74 20 6d 69 header, that mi
1354b 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 61 6e ght be due to an
1354c 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f 72 0a 20 I/O error, or.
1354d 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 ** it
1354e 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f might be due to
1354f 20 74 68 65 20 72 61 63 65 20 63 6f 6e 64 69 74 the race condit
13550 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 61 62 ion described ab
13551 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 20 20 20 ove and in.
13552 20 20 20 20 20 20 20 2a 2a 20 74 69 63 6b 65 74 ** ticket
13553 20 23 33 38 38 33 2e 20 20 45 69 74 68 65 72 20 #3883. Either
13554 77 61 79 2c 20 61 73 73 75 6d 65 20 74 68 61 74 way, assume that
13555 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
13556 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 hot..
13557 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 62 ** This might b
13558 65 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 e a false positi
13559 76 65 2e 20 20 42 75 74 20 69 66 20 69 74 20 69 ve. But if it i
1355a 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 s, then the.
1355b 20 20 20 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d ** autom
1355c 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 atic journal pla
1355d 79 62 61 63 6b 20 61 6e 64 20 72 65 63 6f 76 65 yback and recove
1355e 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c ry mechanism wil
1355f 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 20 20 20 l deal.
13560 20 20 20 2a 2a 20 77 69 74 68 20 69 74 20 75 6e ** with it un
13561 64 65 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 der an EXCLUSIVE
13562 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 65 20 64 lock where we d
13563 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 20 20 o not need to.
13564 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 6f 72 ** wor
13565 72 79 20 73 6f 20 6d 75 63 68 20 77 69 74 68 20 ry so much with
13566 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 2e race conditions.
13567 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a . */.
13568 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 *pEx
13569 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 ists = 1;.
1356a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1356b 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 E_OK;.
1356c 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1356d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
1356e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1356f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f *.** Read the co
13570 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 ntent for page p
13571 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 Pg out of the da
13572 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
13573 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 into .** pPg->pD
13574 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f ata. A shared lo
13575 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75 ck or greater mu
13576 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 st be held on th
13577 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
13578 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 le before this f
13579 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1357a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65 d..**.** If page
1357b 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e 1 is read, then
1357c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 the value of Pa
1357d 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d ger.dbFileVers[]
1357e 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 is set to.** th
1357f 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f e value read fro
13580 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
13581 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ile..**.** If an
13582 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
13583 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 , then the IO er
13584 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
13585 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a to the caller..*
13586 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c * Otherwise, SQL
13587 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
13588 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
13589 74 20 72 65 61 64 44 62 50 61 67 65 28 50 67 48 t readDbPage(PgH
1358a 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
1358b 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
1358c 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65 >pPager; /* Page
1358d 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 r object associa
1358e 74 65 64 20 77 69 74 68 20 70 61 67 65 20 70 50 ted with page pP
1358f 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f g */. Pgno pgno
13590 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 = pPg->pgno;
13591 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
13592 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 er to read */.
13593 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
13594 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13595 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
13596 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 i64 iOffset;
13597 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13598 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 Byte offset of
13599 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f file to read fro
1359a 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 m */.. assert(
1359b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
1359c 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 20 21 AGER_SHARED && !
1359d 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72 MEMDB );. asser
1359e 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
1359f 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69 66 28 20 ->fd) );.. if(
135a0 4e 45 56 45 52 28 21 69 73 4f 70 65 6e 28 70 50 NEVER(!isOpen(pP
135a1 61 67 65 72 2d 3e 66 64 29 29 20 29 7b 0a 20 20 ager->fd)) ){.
135a2 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
135a3 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
135a4 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 memset(pPg->pD
135a5 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ata, 0, pPager->
135a6 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 72 pageSize);. r
135a7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
135a8 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65 74 20 3d . }. iOffset =
135a9 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 (pgno-1)*(i64)p
135aa 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
135ab 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
135ac 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
135ad 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50 , pPg->pData, pP
135ae 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
135af 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 iOffset);. if(
135b0 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
135b1 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 _SHORT_READ ){.
135b2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
135b3 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e K;. }. if( pgn
135b4 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75 38 20 2a o==1 ){. u8 *
135b5 64 62 46 69 6c 65 56 65 72 73 20 3d 20 26 28 28 dbFileVers = &((
135b6 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b u8*)pPg->pData)[
135b7 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 24];. memcpy(
135b8 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 &pPager->dbFileV
135b9 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c ers, dbFileVers,
135ba 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
135bb 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
135bc 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 }. CODEC1(pPage
135bd 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 r, pPg->pData, p
135be 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 53 51 4c gno, 3, rc = SQL
135bf 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50 ITE_NOMEM);.. P
135c0 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 AGER_INCR(sqlite
135c1 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 3_pager_readdb_c
135c2 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f 49 ount);. PAGER_I
135c3 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65 61 NCR(pPager->nRea
135c4 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 d);. IOTRACE(("
135c5 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 PGIN %p %d\n", p
135c6 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 Pager, pgno));.
135c7 20 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45 PAGERTRACE(("FE
135c8 54 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 TCH %d page %d h
135c9 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 ash(%08x)\n",.
135ca 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
135cb 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
135cc 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 no, pager_pageha
135cd 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 72 65 sh(pPg)));.. re
135ce 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
135cf 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
135d0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 is called to obt
135d1 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ain a shared loc
135d2 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
135d3 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 e file..** It is
135d4 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c illegal to call
135d5 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
135d6 75 69 72 65 28 29 20 75 6e 74 69 6c 20 61 66 74 uire() until aft
135d7 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
135d8 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 73 75 63 .** has been suc
135d9 63 65 73 73 66 75 6c 6c 79 20 63 61 6c 6c 65 64 cessfully called
135da 2e 20 49 66 20 61 20 73 68 61 72 65 64 2d 6c 6f . If a shared-lo
135db 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
135dc 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20 ld when.** this
135dd 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
135de 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f ed, it is a no-o
135df 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c p..**.** The fol
135e0 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e lowing operation
135e1 73 20 61 72 65 20 61 6c 73 6f 20 70 65 72 66 6f s are also perfo
135e2 72 6d 65 64 20 62 79 20 74 68 69 73 20 66 75 6e rmed by this fun
135e3 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 ction..**.** 1
135e4 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 ) If the pager i
135e5 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50 s currently in P
135e6 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 AGER_UNLOCK stat
135e7 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a e (no lock held.
135e8 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 ** on the d
135e9 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 atabase file), t
135ea 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 hen an attempt i
135eb 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e s made to obtain
135ec 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 a.** SHARE
135ed 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 D lock on the da
135ee 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d tabase file. Imm
135ef 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f ediately after o
135f0 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 btaining.**
135f1 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b the SHARED lock
135f2 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 , the file-syste
135f3 6d 20 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 m is checked for
135f4 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a a hot-journal,.
135f5 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 69 73 ** which is
135f6 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 played back if
135f7 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 present. Followi
135f8 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e ng any hot-journ
135f9 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c al .** roll
135fa 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e back, the conten
135fb 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 ts of the cache
135fc 61 72 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 are validated by
135fd 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 checking.**
135fe 20 20 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f the 'change-co
135ff 75 6e 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 unter' field of
13600 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13601 65 20 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 e header and.**
13602 20 20 20 20 20 64 69 73 63 61 72 64 65 64 20 69 discarded i
13603 66 20 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 f they are found
13604 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a to be invalid..
13605 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 **.** 2) If th
13606 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
13607 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ng in exclusive-
13608 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 mode, and there
13609 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a are currently.**
1360a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e no outstan
1360b 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 ding references
1360c 74 6f 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e to any pages, an
1360d 64 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f d is in the erro
1360e 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 r state,.**
1360f 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 then an attempt
13610 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 is made to clea
13611 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
13612 65 20 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a e by discarding.
13613 2a 2a 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 ** the cont
13614 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
13615 20 63 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 cache and rolli
13616 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e ng back any open
13617 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 journal.**
13618 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
13619 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 64 65 the operation de
1361a 73 63 72 69 62 65 64 20 62 79 20 28 32 29 20 61 scribed by (2) a
1361b 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 74 74 65 bove is not atte
1361c 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 20 74 68 mpted, and if th
1361d 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e e.** pager is in
1361e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 an error state
1361f 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 other than SQLIT
13620 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 E_FULL when this
13621 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 is called,.** t
13622 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 he error state e
13623 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
13624 75 72 6e 65 64 2e 20 49 74 20 69 73 20 70 65 72 urned. It is per
13625 6d 69 74 74 65 64 20 74 6f 20 72 65 61 64 20 74 mitted to read t
13626 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 he.** database w
13627 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 5f 46 55 hen in SQLITE_FU
13628 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 65 2e 0a LL error state..
13629 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1362a 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
1362b 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 s successful, SQ
1362c 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
1362d 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 49 4f ned. If an.** IO
1362e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
1362f 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 ile locking the
13630 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69 database, checki
13631 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 ng for a hot-jou
13632 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 rnal.** file or
13633 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a rolling back a j
13634 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
13635 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
13636 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 s returned..*/.S
13637 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13638 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 t sqlite3PagerSh
13639 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a aredLock(Pager *
1363a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
1363b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
1363c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1363d 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
1363e 20 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73 int isErrorRes
1363f 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 et = 0;
13640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13641 72 65 63 6f 76 65 72 69 6e 67 20 66 72 6f 6d 20 recovering from
13642 65 72 72 6f 72 20 73 74 61 74 65 20 2a 2f 0a 0a error state */..
13643 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e /* This routin
13644 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 e is only called
13645 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 61 6e 64 from b-tree and
13646 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 65 72 65 only when there
13647 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74 are no. ** out
13648 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 2a standing pages *
13649 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
1364a 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
1364b 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
1364c 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e e)==0 );. if( N
1364d 45 56 45 52 28 4d 45 4d 44 42 20 26 26 20 70 50 EVER(MEMDB && pP
1364e 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
1364f 7b 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d { return pPager-
13650 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a 20 20 2f >errCode; }.. /
13651 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61 * If this databa
13652 73 65 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f se is in an erro
13653 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20 r-state, now is
13654 61 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61 a chance to clea
13655 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 r. ** the error
13656 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f . Discard the co
13657 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
13658 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 72 6f ger-cache and ro
13659 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61 6e 79 20 llback. ** any
1365a 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 6e 20 74 hot journal in t
1365b 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 0a he file-system..
1365c 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
1365d 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 r->errCode ){.
1365e 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
1365f 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 ger->jfd) || pPa
13660 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 29 7b ger->zJournal ){
13661 0a 20 20 20 20 20 20 69 73 45 72 72 6f 72 52 65 . isErrorRe
13662 73 65 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 set = 1;. }.
13663 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
13664 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a de = SQLITE_OK;.
13665 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 pager_reset(
13666 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 pPager);. }..
13667 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
13668 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 e==PAGER_UNLOCK
13669 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 || isErrorReset
1366a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
1366b 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 fs * const pVfs
1366c 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a = pPager->pVfs;.
1366d 20 20 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f 75 int isHotJou
1366e 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73 rnal = 0;. as
1366f 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a sert( !MEMDB );.
13670 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
13671 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
13672 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
13673 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 e)==0 );. if(
13674 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c pPager->noReadl
13675 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ock ){. ass
13676 65 72 74 28 20 70 50 61 67 65 72 2d 3e 72 65 61 ert( pPager->rea
13677 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 20 20 70 dOnly );. p
13678 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
13679 41 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 AGER_SHARED;.
1367a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
1367b 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e = pager_wait_on
1367c 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48 _lock(pPager, SH
1367d 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 ARED_LOCK);.
1367e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1367f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 _OK ){. a
13680 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13681 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
13682 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 CK );. re
13683 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 turn pager_error
13684 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
13685 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
13686 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13687 73 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f state>=SHARED_LO
13688 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 CK );.. /* If
13689 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 a journal file
1368a 65 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 exists, and ther
1368b 65 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 e is no RESERVED
1368c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 lock on the.
1368d 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
1368e 65 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65 e, then it eithe
1368f 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c r needs to be pl
13690 61 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c ayed back or del
13691 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eted.. */.
13692 20 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 if( !isErrorRes
13693 65 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 et ){. asse
13694 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
13695 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 52 45 e <= PAGER_SHARE
13696 44 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 D );. rc =
13697 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 hasHotJournal(pP
13698 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72 ager, &isHotJour
13699 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nal);. if(
1369a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1369b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
1369c 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 iled;. }.
1369d 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72 }. if( isEr
1369e 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f rorReset || isHo
1369f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 tJournal ){.
136a0 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c /* Get an EXCL
136a1 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
136a2 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
136a3 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 At this point i
136a4 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d t is. ** im
136a5 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52 portant that a R
136a6 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 ESERVED lock is
136a7 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 not obtained on
136a8 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20 the way to the.
136a9 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 ** EXCLUSIV
136aa 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65 E lock. If it we
136ab 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 re, another proc
136ac 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74 ess might open t
136ad 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 he. ** data
136ae 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63 base file, detec
136af 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c t the RESERVED l
136b0 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64 ock, and conclud
136b1 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 e that the.
136b2 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 ** database is
136b3 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69 safe to read whi
136b4 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 le this process
136b5 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67 is still rolling
136b6 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 68 the . ** h
136b7 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2e ot-journal back.
136b8 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 . ** .
136b9 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 ** Because the
136ba 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45 53 intermediate RES
136bb 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f ERVED lock is no
136bc 74 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 79 t requested, any
136bd 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 . ** other
136be 70 72 6f 63 65 73 73 20 61 74 74 65 6d 70 74 69 process attempti
136bf 6e 67 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ng to access the
136c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
136c1 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 20 20 20 ill get to .
136c2 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 20 ** this point
136c3 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 in the code and
136c4 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 6e 20 69 fail to obtain i
136c5 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45 ts own EXCLUSIVE
136c6 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 2a 2a 20 lock . **
136c7 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
136c8 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 file.. */.
136c9 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
136ca 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 56 45 >state<EXCLUSIVE
136cb 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 _LOCK ){.
136cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c rc = sqlite3OsL
136cd 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
136ce 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
136cf 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
136d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
136d1 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
136d2 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
136d3 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 rc);.
136d4 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
136d5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
136d6 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
136d7 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a AGER_EXCLUSIVE;.
136d8 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 }. .
136d9 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 /* Open the jour
136da 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69 nal for read/wri
136db 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 73 20 te access. This
136dc 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 0a 20 is because in .
136dd 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 76 ** exclusiv
136de 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 e-access mode th
136df 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
136e0 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f r will be kept o
136e1 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a pen and. **
136e2 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 20 66 possibly used f
136e3 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
136e4 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f later on. On so
136e5 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 0a me systems, the.
136e6 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63 ** OsTrunc
136e7 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 64 20 ate() call used
136e8 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 in exclusive-acc
136e9 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65 ess mode also re
136ea 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 quires. **
136eb 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 69 6c a read/write fil
136ec 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20 e handle..
136ed 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 */. if( !is
136ee 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
136ef 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) ){. int
136f0 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63 res;. rc
136f1 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
136f2 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e ss(pVfs,pPager->
136f3 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f zJournal,SQLITE_
136f4 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72 ACCESS_EXISTS,&r
136f5 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 es);. if(
136f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
136f7 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
136f8 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 res ){.
136f9 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b int fout = 0;
136fa 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
136fb 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e f = SQLITE_OPEN
136fc 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
136fd 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
136fe 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 NAL;.
136ff 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 assert( !pPager
13700 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
13701 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13702 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
13703 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
13704 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
13705 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 , f, &fout);.
13706 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
13707 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
13708 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
13709 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 20 20 20 >jfd) );.
1370a 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1370b 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53 ITE_OK && fout&S
1370c 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
1370d 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 NLY ){.
1370e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1370f 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 _CANTOPEN;.
13710 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
13711 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
13712 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 jfd);.
13713 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 }. }e
13714 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
13715 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e /* If the journ
13716 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 al does not exis
13717 74 2c 20 69 74 20 75 73 75 61 6c 6c 79 20 6d 65 t, it usually me
13718 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 20 0a 20 ans that some .
13719 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 ** ot
1371a 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d her connection m
1371b 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 69 6e anaged to get in
1371c 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 62 61 63 and roll it bac
1371d 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 20 20 20 k before .
1371e 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f ** this co
1371f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 69 6e 65 nnection obtaine
13720 64 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 d the exclusive
13721 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f 72 2c 20 lock above. Or,
13722 69 74 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 it .
13723 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74 68 61 74 ** may mean that
13724 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 the pager was i
13725 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74 n the error-stat
13726 65 20 77 68 65 6e 20 74 68 69 73 0a 20 20 20 20 e when this.
13727 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
13728 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 20 61 ion was called a
13729 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nd the journal f
1372a 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 ile does not exi
1372b 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 st. */.
1372c 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
1372d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
1372e 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 Pager, 0);.
1372f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
13730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
13731 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13732 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
13733 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d failed;. }
13734 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 3a .. /* TODO:
13735 20 57 68 79 20 61 72 65 20 74 68 65 73 65 20 63 Why are these c
13736 6c 65 61 72 65 64 20 68 65 72 65 3f 20 49 73 20 leared here? Is
13737 69 74 20 6e 65 63 65 73 73 61 72 79 3f 20 2a 2f it necessary? */
13738 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
13739 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 ournalStarted =
1373a 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
1373b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
1373c 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 . pPager->s
1373d 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 etMaster = 0;.
1373e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1373f 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20 nalHdr = 0;. .
13740 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20 /* Playback
13741 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6a and delete the j
13742 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68 ournal. Drop th
13743 65 20 64 61 74 61 62 61 73 65 20 77 72 69 74 65 e database write
13744 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 . ** lock a
13745 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 68 65 nd reacquire the
13746 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 72 67 read lock. Purg
13747 65 20 74 68 65 20 63 61 63 68 65 20 62 65 66 6f e the cache befo
13748 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 re. ** play
13749 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 6f 74 ing back the hot
1374a 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 61 74 -journal so that
1374b 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 75 70 we don't end up
1374c 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20 61 with. ** a
1374d 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 63 n inconsistent c
1374e 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ache.. */.
1374f 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 if( isOpen(
13750 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a pPager->jfd) ){.
13751 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
13752 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
13753 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 er, 1);.
13754 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13755 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
13756 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 c = pager_error(
13757 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
13758 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
13759 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
1375a 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
1375b 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 rt( (pPager->sta
1375c 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 te==PAGER_SHARED
1375d 29 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ). ||
1375e 28 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 (pPager->exclusi
1375f 76 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 veMode && pPager
13760 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48 ->state>PAGER_SH
13761 41 52 45 44 29 0a 20 20 20 20 20 20 29 3b 0a 20 ARED). );.
13762 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 50 }.. if( pP
13763 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 7c 7c ager->pBackup ||
13764 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
13765 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e gecount(pPager->
13766 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20 20 pPCache)>0 ){.
13767 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72 65 /* The share
13768 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 74 20 d-lock has just
13769 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 6f 6e been acquired on
1376a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1376b 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 le. ** and
1376c 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 64 there are alread
1376d 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 y pages in the c
1376e 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 72 65 ache (from a pre
1376f 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20 72 vious. ** r
13770 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 ead or write tra
13771 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 65 63 nsaction). Chec
13772 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 k to see if the
13773 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a database. *
13774 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 * has been modif
13775 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 74 ied. If the dat
13776 61 62 61 73 65 20 68 61 73 20 63 68 61 6e 67 65 abase has change
13777 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20 20 d, flush the.
13778 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 20 ** cache..
13779 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 44 **. ** D
1377a 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 20 atabase changes
1377b 69 73 20 64 65 74 65 63 74 65 64 20 62 79 20 6c is detected by l
1377c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 79 74 ooking at 15 byt
1377d 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 es beginning.
1377e 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 20 ** at offset
1377f 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 24 into the file
13780 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 20 6f . The first 4 o
13781 66 20 74 68 65 73 65 20 31 36 20 62 79 74 65 73 f these 16 bytes
13782 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 20 are. ** a
13783 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20 74 32-bit counter t
13784 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 hat is increment
13785 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68 61 ed with each cha
13786 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 20 20 nge. The.
13787 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 63 ** other bytes c
13788 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20 77 hange randomly w
13789 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63 68 ith each file ch
1378a 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 20 20 ange when.
1378b 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 69 6e ** a codec is in
1378c 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20 0a use.. ** .
1378d 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 ** There i
1378e 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 s a vanishingly
1378f 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 61 small chance tha
13790 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c 20 t a change will
13791 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 2a 2a not be . **
13792 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65 20 detected. The
13793 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e 64 chance of an und
13794 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20 69 etected change i
13795 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 0a s so small that.
13796 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 ** it can
13797 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a 20 20 be neglected..
13798 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 */. cha
13799 72 20 64 62 46 69 6c 65 56 65 72 73 5b 73 69 7a r dbFileVers[siz
1379a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 eof(pPager->dbFi
1379b 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 20 20 20 leVers)];.
1379c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
1379d 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 count(pPager, 0)
1379e 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 ;.. if( pPa
1379f 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
137a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 50 61 rc = pPa
137a1 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 ger->errCode;.
137a2 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 goto faile
137a3 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 d;. }..
137a4 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
137a5 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b ->dbSizeValid );
137a6 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
137a7 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 r->dbSize>0 ){.
137a8 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
137a9 22 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22 "CKVERS %p %d\n"
137aa 2c 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 , pPager, sizeof
137ab 28 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0a (dbFileVers)));.
137ac 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
137ad 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
137ae 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 r->fd, &dbFileVe
137af 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c rs, sizeof(dbFil
137b0 65 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20 eVers), 24);.
137b1 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
137b2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
137b3 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b goto failed;
137b4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
137b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
137b6 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72 memset(dbFileVer
137b7 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46 s, 0, sizeof(dbF
137b8 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 ileVers));.
137b9 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 65 }.. if( me
137ba 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 46 mcmp(pPager->dbF
137bb 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 ileVers, dbFileV
137bc 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 ers, sizeof(dbFi
137bd 6c 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a 20 leVers))!=0 ){.
137be 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 pager_res
137bf 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
137c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 }. }. as
137c1 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 78 sert( pPager->ex
137c2 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 clusiveMode || p
137c3 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
137c4 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 GER_SHARED );.
137c5 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69 66 }.. failed:. if
137c6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
137c7 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f ){. /* pager_
137c8 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f unlock() is a no
137c9 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69 76 -op for exclusiv
137ca 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d 65 e mode and in-me
137cb 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e 20 mory databases.
137cc 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c */. pager_unl
137cd 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ock(pPager);. }
137ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
137cf 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 ./*.** If the re
137d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68 61 ference count ha
137d1 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c 20 s reached zero,
137d2 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 rollback any act
137d3 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ive.** transacti
137d4 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 on and unlock th
137d5 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 e pager..**.** E
137d6 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69 6e xcept, in lockin
137d7 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 g_mode=EXCLUSIVE
137d8 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e when there is n
137d9 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a 20 othing to in.**
137da 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
137db 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63 6b rnal, the unlock
137dc 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d 65 is not performe
137dd 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a 2a d and there is.*
137de 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f 6c * nothing to rol
137df 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20 72 lback, so this r
137e0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
137e1 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f p..*/ .static vo
137e2 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 id pagerUnlockIf
137e3 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70 50 Unused(Pager *pP
137e4 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73 71 ager){. if( (sq
137e5 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
137e6 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
137e7 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 26 20 28 che)==0). && (
137e8 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 !pPager->exclusi
137e9 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 veMode || pPager
137ea 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 ->journalOff>0)
137eb 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 65 72 55 . ){. pagerU
137ec 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
137ed 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a (pPager);. }.}.
137ee 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 ./*.** Acquire a
137ef 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 reference to pa
137f0 67 65 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 ge number pgno i
137f1 6e 20 70 61 67 65 72 20 70 50 61 67 65 72 20 28 n pager pPager (
137f2 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 65 72 65 a page.** refere
137f3 6e 63 65 20 68 61 73 20 74 79 70 65 20 44 62 50 nce has type DbP
137f4 61 67 65 2a 29 2e 20 49 66 20 74 68 65 20 72 65 age*). If the re
137f5 71 75 65 73 74 65 64 20 72 65 66 65 72 65 6e 63 quested referenc
137f6 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 65 73 73 e is .** success
137f7 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 fully obtained,
137f8 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 it is copied to
137f9 2a 70 70 50 61 67 65 20 61 6e 64 20 53 51 4c 49 *ppPage and SQLI
137fa 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a TE_OK returned..
137fb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 **.** If the req
137fc 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 61 uested page is a
137fd 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
137fe 63 68 65 2c 20 69 74 20 69 73 20 72 65 74 75 72 che, it is retur
137ff 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 ned. .** Otherwi
13800 73 65 2c 20 61 20 6e 65 77 20 70 61 67 65 20 6f se, a new page o
13801 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 bject is allocat
13802 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 ed and populated
13803 20 77 69 74 68 20 64 61 74 61 0a 2a 2a 20 72 65 with data.** re
13804 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
13805 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 73 6f base file. In so
13806 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 70 63 me cases, the pc
13807 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d 61 79 0a ache module may.
13808 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 20 74 6f ** choose not to
13809 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
1380a 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 page object and
1380b 6d 61 79 20 72 65 75 73 65 20 61 6e 20 65 78 69 may reuse an exi
1380c 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 63 74 20 sting.** object
1380d 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 61 6e 64 with no outstand
1380e 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 0a ing references..
1380f 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 72 61 20 **.** The extra
13810 64 61 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f data appended to
13811 20 61 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 a page is alway
13812 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f s initialized to
13813 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a 2a 20 66 zeros the .** f
13814 69 72 73 74 20 74 69 6d 65 20 61 20 70 61 67 65 irst time a page
13815 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 is loaded into
13816 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 65 20 70 memory. If the p
13817 61 67 65 20 72 65 71 75 65 73 74 65 64 20 69 73 age requested is
13818 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 .** already in
13819 74 68 65 20 63 61 63 68 65 20 77 68 65 6e 20 74 the cache when t
1381a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1381b 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 called, then the
1381c 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 61 20 69 extra.** data i
1381d 73 20 6c 65 66 74 20 61 73 20 69 74 20 77 61 73 s left as it was
1381e 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 20 6f when the page o
1381f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 74 20 75 bject was last u
13820 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 sed..**.** If th
13821 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
13822 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
13823 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 the requested p
13824 61 67 65 20 6f 72 20 69 66 20 61 20 0a 2a 2a 20 age or if a .**
13825 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69 non-zero value i
13826 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
13827 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 61 6d 65 noContent parame
13828 74 65 72 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20 ter and the .**
13829 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
1382a 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 73 74 s not already st
1382b 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 ored in the cach
1382c 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 61 e, then no .** a
1382d 63 74 75 61 6c 20 64 69 73 6b 20 72 65 61 64 20 ctual disk read
1382e 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 69 73 20 occurs. In this
1382f 63 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 case the memory
13830 69 6d 61 67 65 20 6f 66 20 74 68 65 20 0a 2a 2a image of the .**
13831 20 70 61 67 65 20 69 73 20 69 6e 69 74 69 61 6c page is initial
13832 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a 65 72 6f ized to all zero
13833 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 s. .**.** If noC
13834 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 ontent is true,
13835 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 it means that we
13836 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
13837 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a ut the contents.
13838 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 ** of the page.
13839 54 68 69 73 20 6f 63 63 75 72 73 20 69 6e 20 74 This occurs in t
1383a 77 6f 20 73 65 70 65 72 61 74 65 20 73 63 65 6e wo seperate scen
1383b 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 arios:.**.** a
1383c 29 20 57 68 65 6e 20 72 65 61 64 69 6e 67 20 61 ) When reading a
1383d 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
1383e 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61 page from the da
1383f 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a 2a 0a 2a tabase, and.**.*
13840 2a 20 20 20 62 29 20 57 68 65 6e 20 61 20 73 61 * b) When a sa
13841 76 65 70 6f 69 6e 74 20 69 73 20 62 65 69 6e 67 vepoint is being
13842 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 rolled back and
13843 20 77 65 20 6e 65 65 64 20 74 6f 20 6c 6f 61 64 we need to load
13844 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 77 20 70 .** a new p
13845 61 67 65 20 69 6e 74 6f 20 74 68 65 20 63 61 63 age into the cac
13846 68 65 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 77 he to populate w
13847 69 74 68 20 74 68 65 20 64 61 74 61 20 72 65 61 ith the data rea
13848 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 74 d.** from t
13849 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6a 6f 75 he savepoint jou
1384a 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e rnal..**.** If n
1384b 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65 oContent is true
1384c 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 , then the data
1384d 72 65 74 75 72 6e 65 64 20 69 73 20 7a 65 72 6f returned is zero
1384e 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a ed instead of.**
1384f 20 62 65 69 6e 67 20 72 65 61 64 20 66 72 6f 6d being read from
13850 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 41 the database. A
13851 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 74 68 65 dditionally, the
13852 20 62 69 74 73 20 63 6f 72 72 65 73 70 6f 6e 64 bits correspond
13853 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e 6f 20 69 ing.** to pgno i
13854 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e n Pager.pInJourn
13855 61 6c 20 28 62 69 74 76 65 63 20 6f 66 20 70 61 al (bitvec of pa
13856 67 65 73 20 61 6c 72 65 61 64 79 20 77 72 69 74 ges already writ
13857 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 6a 6f ten to the.** jo
13858 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 6e 64 20 urnal file) and
13859 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 the PagerSavepoi
1385a 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 nt.pInSavepoint
1385b 62 69 74 76 65 63 73 20 6f 66 20 61 6e 79 20 6f bitvecs of any o
1385c 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 pen.** savepoint
1385d 73 20 61 72 65 20 73 65 74 2e 20 54 68 69 73 20 s are set. This
1385e 6d 65 61 6e 73 20 69 66 20 74 68 65 20 70 61 67 means if the pag
1385f 65 20 69 73 20 6d 61 64 65 20 77 72 69 74 61 62 e is made writab
13860 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 70 6f 69 le at any.** poi
13861 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 nt in the future
13862 2c 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 , using a call t
13863 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 o sqlite3PagerWr
13864 69 74 65 28 29 2c 20 69 74 73 20 63 6f 6e 74 65 ite(), its conte
13865 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 nts.** will not
13866 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 54 68 be journaled. Th
13867 69 73 20 73 61 76 65 73 20 49 4f 2e 0a 2a 2a 0a is saves IO..**.
13868 2a 2a 20 54 68 65 20 61 63 71 75 69 73 69 74 69 ** The acquisiti
13869 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 66 6f on might fail fo
1386a 72 20 73 65 76 65 72 61 6c 20 72 65 61 73 6f 6e r several reason
1386b 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 s. In all cases
1386c 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 ,.** an appropri
1386d 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ate error code i
1386e 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a s returned and *
1386f 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f ppPage is set to
13870 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 NULL..**.** See
13871 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 also sqlite3Pag
13872 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42 6f 74 erLookup(). Bot
13873 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 h this routine a
13874 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74 74 65 nd Lookup() atte
13875 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 61 mpt.** to find a
13876 20 70 61 67 65 20 69 6e 20 74 68 65 20 69 6e 2d page in the in-
13877 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 66 69 72 memory cache fir
13878 73 74 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 st. If the page
13879 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a is not already.
1387a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 74 68 ** in memory, th
1387b 69 73 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20 is routine goes
1387c 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 61 64 20 to disk to read
1387d 69 74 20 69 6e 20 77 68 65 72 65 61 73 20 4c 6f it in whereas Lo
1387e 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 74 20 72 okup().** just r
1387f 65 74 75 72 6e 73 20 30 2e 20 20 54 68 69 73 20 eturns 0. This
13880 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 routine acquires
13881 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 74 68 65 a read-lock the
13882 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 0a 2a first time it.*
13883 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 6f 20 64 * has to go to d
13884 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c 64 20 61 isk, and could a
13885 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 61 6e 20 lso playback an
13886 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 66 20 6e old journal if n
13887 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 53 69 6e ecessary..** Sin
13888 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e 65 76 65 ce Lookup() neve
13889 72 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 2c 20 r goes to disk,
1388a 69 74 20 6e 65 76 65 72 20 68 61 73 20 74 6f 20 it never has to
1388b 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 6b 73 0a deal with locks.
1388c 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 ** or journal fi
1388d 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
1388e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1388f 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 0a e3PagerAcquire(.
13890 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
13891 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
13892 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64 er open on the d
13893 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
13894 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 Pgno pgno,
13895 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
13896 6d 62 65 72 20 74 6f 20 66 65 74 63 68 20 2a 2f mber to fetch */
13897 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 . DbPage **ppPa
13898 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 ge, /* Write
13899 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1389a 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20 page here */.
1389b 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 int noContent
1389c 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f /* Do not bo
1389d 74 68 65 72 20 72 65 61 64 69 6e 67 20 63 6f 6e ther reading con
1389e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 6b 20 69 tent from disk i
1389f 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 f true */.){. i
138a0 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a nt rc;. PgHdr *
138a1 70 50 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pPg;.. assert(
138a2 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 assert_pager_sta
138a3 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 te(pPager) );.
138a4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
138a5 73 74 61 74 65 3e 50 41 47 45 52 5f 55 4e 4c 4f state>PAGER_UNLO
138a6 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 67 6e CK );.. if( pgn
138a7 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 o==0 ){. retu
138a8 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
138a9 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f T_BKPT;. }.. /
138aa 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
138ab 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 s in the error s
138ac 74 61 74 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 tate, return an
138ad 65 72 72 6f 72 20 69 6d 6d 65 64 69 61 74 65 6c error immediatel
138ae 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 y. . ** Otherwi
138af 73 65 2c 20 72 65 71 75 65 73 74 20 74 68 65 20 se, request the
138b0 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 50 43 page from the PC
138b1 61 63 68 65 20 6c 61 79 65 72 2e 20 2a 2f 0a 20 ache layer. */.
138b2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
138b3 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code!=SQLITE_OK
138b4 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f && pPager->errCo
138b5 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 de!=SQLITE_FULL
138b6 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 ){. rc = pPag
138b7 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
138b8 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 else{. rc = s
138b9 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 qlite3PcacheFetc
138ba 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 h(pPager->pPCach
138bb 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 70 50 61 e, pgno, 1, ppPa
138bc 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 ge);. }.. if(
138bd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
138be 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 . /* Either t
138bf 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
138c0 65 33 50 63 61 63 68 65 46 65 74 63 68 28 29 20 e3PcacheFetch()
138c1 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f returned an erro
138c2 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 r or the. **
138c3 70 61 67 65 72 20 77 61 73 20 61 6c 72 65 61 64 pager was alread
138c4 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 y in the error-s
138c5 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 tate when this f
138c6 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c unction was call
138c7 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65 74 20 70 ed.. ** Set p
138c8 50 67 20 74 6f 20 30 20 61 6e 64 20 6a 75 6d 70 Pg to 0 and jump
138c9 20 74 6f 20 74 68 65 20 65 78 63 65 70 74 69 6f to the exceptio
138ca 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a 2f 0a 20 n handler. */.
138cb 20 20 20 70 50 67 20 3d 20 30 3b 0a 20 20 20 20 pPg = 0;.
138cc 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
138cd 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 20 61 73 re_err;. }. as
138ce 73 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d sert( (*ppPage)-
138cf 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 >pgno==pgno );.
138d0 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67 assert( (*ppPag
138d1 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 e)->pPager==pPag
138d2 65 72 20 7c 7c 20 28 2a 70 70 50 61 67 65 29 2d er || (*ppPage)-
138d3 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b 0a 0a 20 >pPager==0 );..
138d4 20 69 66 28 20 28 2a 70 70 50 61 67 65 29 2d 3e if( (*ppPage)->
138d5 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 2f 2a pPager ){. /*
138d6 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
138d7 65 20 70 63 61 63 68 65 20 61 6c 72 65 61 64 79 e pcache already
138d8 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 69 contains an ini
138d9 74 69 61 6c 69 7a 65 64 20 63 6f 70 79 20 6f 66 tialized copy of
138da 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 61 67 65 . ** the page
138db 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 . Return without
138dc 20 66 75 72 74 68 65 72 20 61 64 6f 2e 20 20 2a further ado. *
138dd 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 /. assert( pg
138de 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 5f 50 47 no<=PAGER_MAX_PG
138df 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50 41 47 45 NO && pgno!=PAGE
138e0 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
138e1 29 20 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 ) );. PAGER_I
138e2 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 74 NCR(pPager->nHit
138e3 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
138e4 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 LITE_OK;.. }els
138e5 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 e{. /* The pa
138e6 67 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72 ger cache has cr
138e7 65 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65 eated a new page
138e8 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 . Its content ne
138e9 65 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 eds to . ** b
138ea 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 e initialized.
138eb 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b */. int nMax;
138ec 0a 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 .. PAGER_INCR
138ed 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29 3b (pPager->nMiss);
138ee 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70 50 61 . pPg = *ppPa
138ef 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 ge;. pPg->pPa
138f0 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20 ger = pPager;..
138f1 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 /* The maximu
138f2 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 m page number is
138f3 20 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 2^31. Return SQ
138f4 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 LITE_CORRUPT if
138f5 61 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 6e 75 a page. ** nu
138f6 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 mber greater tha
138f7 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 65 20 75 n this, or the u
138f8 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 2d 70 61 nused locking-pa
138f9 67 65 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 ge, is requested
138fa 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e . */. if( pgn
138fb 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f o>PAGER_MAX_PGNO
138fc 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f || pgno==PAGER_
138fd 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
138fe 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
138ff 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
13900 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 T;. goto pa
13901 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
13902 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d . }.. rc =
13903 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
13904 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 ecount(pPager, &
13905 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66 28 20 72 nMax);. if( r
13906 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13907 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 goto pager
13908 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 _acquire_err;.
13909 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 4d 45 4d }.. if( MEM
1390a 44 42 20 7c 7c 20 6e 4d 61 78 3c 28 69 6e 74 29 DB || nMax<(int)
1390b 70 67 6e 6f 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e pgno || noConten
1390c 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 t ){. if( p
1390d 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 gno>pPager->mxPg
1390e 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53 51 4c 49 no ){..rc = SQLI
1390f 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74 6f 20 70 TE_FULL;..goto p
13910 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
13911 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
13912 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b if( noContent ){
13913 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 69 6c . /* Fail
13914 75 72 65 20 74 6f 20 73 65 74 20 74 68 65 20 62 ure to set the b
13915 69 74 73 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75 its in the InJou
13916 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 6f 72 73 rnal bit-vectors
13917 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 is benign..
13918 20 20 20 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 ** It merely
13919 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 6d means that we m
1391a 69 67 68 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 ight do some ext
1391b 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e ra work to journ
1391c 61 6c 20 61 20 0a 20 20 20 20 20 20 20 20 2a 2a al a . **
1391d 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 page that does
1391e 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6a not need to be j
1391f 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72 ournaled. Never
13920 74 68 65 6c 65 73 73 2c 20 62 65 20 73 75 72 65 theless, be sure
13921 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 . ** to
13922 74 65 73 74 20 74 68 65 20 63 61 73 65 20 77 68 test the case wh
13923 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 ere a malloc err
13924 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
13925 74 72 79 69 6e 67 20 74 6f 20 73 65 74 20 0a 20 trying to set .
13926 20 20 20 20 20 20 20 2a 2a 20 61 20 62 69 74 20 ** a bit
13927 69 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 2e in a bit vector.
13928 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
13929 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
1392a 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1392b 20 20 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f if( pgno
1392c 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 <=pPager->dbOrig
1392d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
1392e 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d TESTONLY( rc =
1392f 20 29 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 ) sqlite3Bitvec
13930 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a Set(pPager->pInJ
13931 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 ournal, pgno);.
13932 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
13933 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f e( rc==SQLITE_NO
13934 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 7d MEM );. }
13935 0a 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c . TESTONL
13936 59 28 20 72 63 20 3d 20 29 20 61 64 64 54 6f 53 Y( rc = ) addToS
13937 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 avepointBitvecs(
13938 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 pPager, pgno);.
13939 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
1393a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
1393b 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c M );. sql
1393c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
1393d 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 7d 65 6c loc();. }el
1393e 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 se{. mems
1393f 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 et(pPg->pData, 0
13940 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
13941 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ze);. }.
13942 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52 IOTRACE(("ZER
13943 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 O %p %d\n", pPag
13944 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 er, pgno));.
13945 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
13946 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 ert( pPg->pPager
13947 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 20 20 ==pPager );.
13948 20 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 rc = readDbPag
13949 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 e(pPg);. if
1394a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1394b 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1394c 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 pager_acquire_er
1394d 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d r;. }. }
1394e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
1394f 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
13950 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
13951 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
13952 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a g);.#endif. }..
13953 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13954 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63 71 75 69 OK;..pager_acqui
13955 72 65 5f 65 72 72 3a 0a 20 20 61 73 73 65 72 74 re_err:. assert
13956 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13957 29 3b 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a );. if( pPg ){.
13958 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
13959 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 20 7d 0a eDrop(pPg);. }.
1395a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 pagerUnlockIfU
1395b 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 0a nused(pPager);..
1395c 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 *ppPage = 0;.
1395d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1395e 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 *.** Acquire a p
1395f 61 67 65 20 69 66 20 69 74 20 69 73 20 61 6c 72 age if it is alr
13960 65 61 64 79 20 69 6e 20 74 68 65 20 69 6e 2d 6d eady in the in-m
13961 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20 44 6f emory cache. Do
13962 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 74 68 65 .** not read the
13963 20 70 61 67 65 20 66 72 6f 6d 20 64 69 73 6b 2e page from disk.
13964 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
13965 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 2c 0a er to the page,.
13966 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 65 20 70 ** or 0 if the p
13967 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 age is not in ca
13968 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65 74 75 72 che. Also, retur
13969 6e 20 30 20 69 66 20 74 68 65 20 0a 2a 2a 20 70 n 0 if the .** p
1396a 61 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 ager is in PAGER
1396b 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 _UNLOCK state wh
1396c 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1396d 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f is called,.** o
1396e 72 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 r if the pager i
1396f 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 s in an error st
13970 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 ate other than S
13971 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a QLITE_FULL..**.*
13972 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 * See also sqlit
13973 65 33 50 61 67 65 72 47 65 74 28 29 2e 20 20 54 e3PagerGet(). T
13974 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 he difference be
13975 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69 tween this routi
13976 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 ne.** and sqlite
13977 33 50 61 67 65 72 47 65 74 28 29 20 69 73 20 74 3PagerGet() is t
13978 68 61 74 20 5f 67 65 74 28 29 20 77 69 6c 6c 20 hat _get() will
13979 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61 go to the disk a
1397a 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e 20 74 68 nd read.** in th
1397b 65 20 70 61 67 65 20 69 66 20 74 68 65 20 70 61 e page if the pa
1397c 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 ge is not alread
1397d 79 20 69 6e 20 63 61 63 68 65 2e 20 20 54 68 69 y in cache. Thi
1397e 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 s routine.** ret
1397f 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 urns NULL if the
13980 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
13981 63 61 63 68 65 20 6f 72 20 69 66 20 61 20 64 69 cache or if a di
13982 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a sk I/O error .**
13983 20 68 61 73 20 65 76 65 72 20 68 61 70 70 65 6e has ever happen
13984 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
13985 49 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 IVATE DbPage *sq
13986 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 lite3PagerLookup
13987 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13988 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 Pgno pgno){. Pg
13989 48 64 72 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 Hdr *pPg = 0;.
1398a 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d assert( pPager!=
1398b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1398c 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 gno!=0 );. asse
1398d 72 74 28 20 70 50 61 67 65 72 2d 3e 70 50 43 61 rt( pPager->pPCa
1398e 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 che!=0 );. asse
1398f 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
13990 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b e > PAGER_UNLOCK
13991 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 );. sqlite3Pca
13992 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d cheFetch(pPager-
13993 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 >pPCache, pgno,
13994 30 2c 20 26 70 50 67 29 3b 0a 20 20 72 65 74 75 0, &pPg);. retu
13995 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn pPg;.}../*.**
13996 20 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 20 Release a page
13997 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a reference..**.**
13998 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f If the number o
13999 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
1399a 74 68 65 20 70 61 67 65 20 64 72 6f 70 20 74 6f the page drop to
1399b 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 0a zero, then the.
1399c 2a 2a 20 70 61 67 65 20 69 73 20 61 64 64 65 64 ** page is added
1399d 20 74 6f 20 74 68 65 20 4c 52 55 20 6c 69 73 74 to the LRU list
1399e 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 65 66 65 . When all refe
1399f 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70 61 rences to all pa
139a0 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 6c 65 61 ges.** are relea
139a1 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 sed, a rollback
139a2 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c occurs and the l
139a3 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
139a4 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 ase is.** remove
139a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
139a6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
139a7 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 3PagerUnref(DbPa
139a8 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 ge *pPg){. if(
139a9 70 50 67 20 29 7b 0a 20 20 20 20 50 61 67 65 72 pPg ){. Pager
139aa 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
139ab 70 50 61 67 65 72 3b 0a 20 20 20 20 73 71 6c 69 pPager;. sqli
139ac 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 te3PcacheRelease
139ad 28 70 50 67 29 3b 0a 20 20 20 20 70 61 67 65 72 (pPg);. pager
139ae 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 UnlockIfUnused(p
139af 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f Pager);. }.}../
139b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e *.** If the main
139b1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
139b2 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f s already been o
139b3 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 20 74 68 pened, ensure th
139b4 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 2d 6a 6f at the.** sub-jo
139b5 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 urnal file is op
139b6 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 65 20 6d en too. If the m
139b7 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e ain journal is n
139b8 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68 69 73 ot open,.** this
139b9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
139ba 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 o-op..**.** SQLI
139bb 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
139bc 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 d if everything
139bd 67 6f 65 73 20 61 63 63 6f 72 64 69 6e 67 20 74 goes according t
139be 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 6e 20 53 o plan. .** An S
139bf 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 QLITE_IOERR_XXX
139c0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
139c1 74 75 72 6e 65 64 20 69 66 20 61 20 63 61 6c 6c turned if a call
139c2 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 4f to .** sqlite3O
139c3 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 2e 0a 2a sOpen() fails..*
139c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 /.static int ope
139c5 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 61 67 65 nSubJournal(Page
139c6 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
139c7 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
139c8 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 ;. if( isOpen(p
139c9 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 Pager->jfd) && !
139ca 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 isOpen(pPager->s
139cb 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 jfd) ){. if(
139cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
139cd 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
139ce 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c ALMODE_MEMORY ||
139cf 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d pPager->subjInM
139d0 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20 20 73 emory ){. s
139d1 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
139d2 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 Open(pPager->sjf
139d3 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
139d4 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f rc = pagerO
139d5 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 pentemp(pPager,
139d6 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53 51 pPager->sjfd, SQ
139d7 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 LITE_OPEN_SUBJOU
139d8 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d RNAL);. }. }
139d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
139da 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
139db 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 tion is called a
139dc 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 t the start of e
139dd 76 65 72 79 20 77 72 69 74 65 20 74 72 61 6e 73 very write trans
139de 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 action..** There
139df 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 62 65 must already be
139e0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 a RESERVED or E
139e1 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
139e2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a the database .*
139e3 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 * file when this
139e4 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
139e5 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 ed..**.** Open t
139e6 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
139e7 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72 for pager pPager
139e8 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a 6f 75 and write a jou
139e9 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 74 rnal header.** t
139ea 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 o the start of i
139eb 74 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 t. If there are
139ec 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 active savepoint
139ed 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75 62 2d s, open the sub-
139ee 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 journal.** as we
139ef 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ll. This functio
139f0 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 n is only used w
139f1 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
139f2 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20 0a 2a file is being .*
139f3 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72 69 74 * opened to writ
139f4 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 e a rollback log
139f5 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 for a transacti
139f6 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73 on. It is not us
139f7 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 65 6e ed .** when open
139f8 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 ing a hot journa
139f9 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 l file to roll i
139fa 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 t back..**.** If
139fb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
139fc 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 e is already ope
139fd 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 n (as it may be
139fe 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
139ff 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 e),.** then this
13a00 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74 20 77 function just w
13a01 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 rites a journal
13a02 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 header to the st
13a03 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c art of the.** al
13a04 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c 65 2e ready open file.
13a05 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 .**.** Whether
13a06 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e or not the journ
13a07 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 al file is opene
13a08 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
13a09 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 on, the.** Pager
13a0a 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 .pInJournal bitv
13a0b 65 63 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ec structure is
13a0c 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a allocated..**.**
13a0d 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
13a0e 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 K if everything
13a0f 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 4f is successful. O
13a10 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e therwise, return
13a11 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 .** SQLITE_NOME
13a12 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 M if the attempt
13a13 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50 61 67 to allocate Pag
13a14 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 er.pInJournal fa
13a15 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 ils, or .** an I
13a16 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 O error code if
13a17 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69 74 69 opening or writi
13a18 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ng the journal f
13a19 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 ile fails..*/.st
13a1a 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f atic int pager_o
13a1b 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65 pen_journal(Page
13a1c 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
13a1d 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
13a1e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13a1f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
13a20 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c rn code */. sql
13a21 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 ite3_vfs * const
13a22 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e pVfs = pPager->
13a23 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c pVfs; /* Local
13a24 20 63 61 63 68 65 20 6f 66 20 76 66 73 20 70 6f cache of vfs po
13a25 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 inter */.. asse
13a26 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
13a27 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e>=PAGER_RESERVE
13a28 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 D );. assert( p
13a29 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
13a2a 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 l );. assert( p
13a2b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
13a2c 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
13a2d 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 61 LMODE_OFF );. a
13a2e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
13a2f 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
13a30 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c 72 65 61 . /* If alrea
13a31 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 dy in the error
13a32 73 74 61 74 65 2c 20 74 68 69 73 20 66 75 6e 63 state, this func
13a33 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
13a34 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a 20 74 68 But on. ** th
13a35 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 e other hand, th
13a36 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 is routine is ne
13a37 76 65 72 20 63 61 6c 6c 65 64 20 69 66 20 77 65 ver called if we
13a38 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 0a are already in.
13a39 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 73 74 ** an error st
13a3a 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 ate. */. if( NE
13a3b 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 VER(pPager->errC
13a3c 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 50 ode) ) return pP
13a3d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a ager->errCode;..
13a3e 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 /* TODO: Is it
13a3f 20 72 65 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 really possible
13a40 20 74 6f 20 67 65 74 20 68 65 72 65 20 77 69 74 to get here wit
13a41 68 20 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 h dbSizeValid==0
13a42 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a 2a 20 74 ? If not,. ** t
13a43 68 65 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 he call to Pager
13a44 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61 6e 20 Pagecount() can
13a45 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f be removed.. */
13a46 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 . testcase( pPa
13a47 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
13a48 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ==0 );. sqlite3
13a49 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
13a4a 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 70 50 Pager, 0);.. pP
13a4b 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13a4c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
13a4d 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64 Create(pPager->d
13a4e 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 50 bSize);. if( pP
13a4f 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13a50 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
13a51 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
13a52 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 }.. /* Open t
13a53 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13a54 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 if it is not alr
13a55 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f 0a 20 20 eady open. */.
13a56 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 if( !isOpen(pPag
13a57 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
13a58 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
13a59 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
13a5a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
13a5b 59 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 Y ){. sqlit
13a5c 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
13a5d 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
13a5e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
13a5f 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 const int flags
13a60 3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =
13a61 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 /* VFS flags
13a62 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c to open journal
13a63 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 file */.
13a64 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
13a65 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
13a66 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 EN_CREATE|.
13a67 20 20 20 28 70 50 61 67 65 72 2d 3e 74 65 6d 70 (pPager->temp
13a68 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 20 20 20 File ? .
13a69 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 (SQLITE_OPEN_D
13a6a 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c ELETEONCLOSE|SQL
13a6b 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f ITE_OPEN_TEMP_JO
13a6c 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20 20 20 20 URNAL):.
13a6d 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d (SQLITE_OPEN_M
13a6e 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 20 AIN_JOURNAL).
13a6f 20 20 20 20 20 29 3b 0a 23 69 66 64 65 66 20 53 );.#ifdef S
13a70 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
13a71 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 MIC_WRITE.
13a72 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 rc = sqlite3Jour
13a73 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 20 nalOpen(.
13a74 20 20 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d pVfs, pPager-
13a75 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 >zJournal, pPage
13a76 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a r->jfd, flags, j
13a77 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 rnlBufferSize(pP
13a78 61 67 65 72 29 0a 20 20 20 20 20 20 29 3b 0a 23 ager). );.#
13a79 65 6c 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20 else. rc =
13a7a 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
13a7b 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
13a7c 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 rnal, pPager->jf
13a7d 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 23 65 d, flags, 0);.#e
13a7e 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 61 ndif. }. a
13a7f 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
13a80 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 E_OK || isOpen(p
13a81 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
13a82 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 }... /* Write
13a83 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 the first journa
13a84 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 l header to the
13a85 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 journal file and
13a86 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74 68 65 20 open . ** the
13a87 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 6e sub-journal if n
13a88 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 ecessary.. */.
13a89 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13a8a 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 OK ){. /* TOD
13a8b 4f 3a 20 43 68 65 63 6b 20 69 66 20 61 6c 6c 20 O: Check if all
13a8c 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 61 of these are rea
13a8d 6c 6c 79 20 72 65 71 75 69 72 65 64 2e 20 2a 2f lly required. */
13a8e 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4f . pPager->dbO
13a8f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 67 65 72 rigSize = pPager
13a90 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 70 50 ->dbSize;. pP
13a91 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
13a92 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 70 50 rted = 0;. pP
13a93 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d ager->needSync =
13a94 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13a95 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 nRec = 0;. pP
13a96 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13a97 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
13a98 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b ->setMaster = 0;
13a99 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
13a9a 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20 rnalHdr = 0;.
13a9b 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e rc = writeJourn
13a9c 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 alHdr(pPager);.
13a9d 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
13a9e 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
13a9f 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a ->nSavepoint ){.
13aa0 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 rc = openSub
13aa1 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b Journal(pPager);
13aa2 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d . }.. if( rc!=
13aa3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13aa4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
13aa5 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 stroy(pPager->pI
13aa6 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 nJournal);. p
13aa7 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
13aa8 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 l = 0;. }. ret
13aa9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13aaa 20 42 65 67 69 6e 20 61 20 77 72 69 74 65 2d 74 Begin a write-t
13aab 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
13aac 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 e specified page
13aad 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 20 0a r object. If a .
13aae 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ** write-transac
13aaf 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 tion has already
13ab0 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 74 68 been opened, th
13ab1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
13ab2 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 no-op..**.** If
13ab3 20 74 68 65 20 65 78 46 6c 61 67 20 61 72 67 75 the exFlag argu
13ab4 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2c 20 74 ment is false, t
13ab5 68 65 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c hen acquire at l
13ab6 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44 0a east a RESERVED.
13ab7 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ** lock on the d
13ab8 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
13ab9 20 65 78 46 6c 61 67 20 69 73 20 74 72 75 65 2c exFlag is true,
13aba 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61 74 then acquire at
13abb 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20 45 58 43 least.** an EXC
13abc 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
13abd 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 61 such a lock is a
13abe 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6e 6f 20 lready held, no
13abf 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66 75 6e 63 locking .** func
13ac0 74 69 6f 6e 73 20 6e 65 65 64 20 62 65 20 63 61 tions need be ca
13ac1 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lled..**.** If t
13ac2 68 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d his is not a tem
13ac3 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d 65 6d porary or in-mem
13ac4 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c 20 74 68 ory file and, th
13ac5 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13ac6 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 69 66 20 s .** opened if
13ac7 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 it has not been
13ac8 61 6c 72 65 61 64 79 2e 20 46 6f 72 20 61 20 74 already. For a t
13ac9 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 20 74 emporary file, t
13aca 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a 2a 20 6f he opening .** o
13acb 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
13acc 6c 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 le is deferred u
13acd 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20 61 6e ntil there is an
13ace 20 61 63 74 75 61 6c 20 6e 65 65 64 20 74 6f 20 actual need to
13acf 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20 74 68 65 .** write to the
13ad0 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44 4f 3a 20 journal. TODO:
13ad1 57 68 79 20 68 61 6e 64 6c 65 20 74 65 6d 70 6f Why handle tempo
13ad2 72 61 72 79 20 66 69 6c 65 73 20 64 69 66 66 65 rary files diffe
13ad3 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a 20 49 66 rently?.**.** If
13ad4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13ad5 65 20 69 73 20 6f 70 65 6e 65 64 20 28 6f 72 20 e is opened (or
13ad6 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 if it is already
13ad7 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20 61 0a 2a open), then a.*
13ad8 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 * journal-header
13ad9 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
13ada 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e 0a he start of it..
13adb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 75 62 **.** If the sub
13adc 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 jInMemory argume
13add 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 nt is non-zero,
13ade 74 68 65 6e 20 61 6e 79 20 73 75 62 2d 6a 6f 75 then any sub-jou
13adf 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a 2a 20 77 rnal opened.** w
13ae0 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 ithin this trans
13ae1 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 6f action will be o
13ae2 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d pened as an in-m
13ae3 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54 68 69 73 emory file. This
13ae4 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66 66 65 63 .** has no effec
13ae5 74 20 69 66 20 74 68 65 20 73 75 62 2d 6a 6f 75 t if the sub-jou
13ae6 72 6e 61 6c 20 69 73 20 61 6c 72 65 61 64 79 20 rnal is already
13ae7 6f 70 65 6e 65 64 20 28 61 73 20 69 74 20 6d 61 opened (as it ma
13ae8 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20 72 75 6e y be when.** run
13ae9 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 ning in exclusiv
13aea 65 20 6d 6f 64 65 29 20 6f 72 20 69 66 20 74 68 e mode) or if th
13aeb 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 64 6f e transaction do
13aec 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 es not require a
13aed 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e .** sub-journal.
13aee 20 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 If the subjInMe
13aef 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 mory argument is
13af0 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 zero, then any
13af1 72 65 71 75 69 72 65 64 0a 2a 2a 20 73 75 62 2d required.** sub-
13af2 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d 70 6c 65 journal is imple
13af3 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d 6f 72 79 mented in-memory
13af4 20 69 66 20 70 50 61 67 65 72 20 69 73 20 61 6e if pPager is an
13af5 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
13af6 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75 73 69 6e ase, .** or usin
13af7 67 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 g a temporary fi
13af8 6c 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f le otherwise..*/
13af9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13afa 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
13afb 42 65 67 69 6e 28 50 61 67 65 72 20 2a 70 50 61 Begin(Pager *pPa
13afc 67 65 72 2c 20 69 6e 74 20 65 78 46 6c 61 67 2c ger, int exFlag,
13afd 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 int subjInMemor
13afe 79 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 y){. int rc = S
13aff 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
13b00 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
13b01 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 e!=PAGER_UNLOCK
13b02 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 75 62 );. pPager->sub
13b03 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28 75 38 29 jInMemory = (u8)
13b04 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b 0a 20 20 subjInMemory;.
13b05 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
13b06 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e==PAGER_SHARED
13b07 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
13b08 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
13b09 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 l==0 );. asse
13b0a 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 21 70 rt( !MEMDB && !p
13b0b 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
13b0c 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69 );.. /* Obtai
13b0d 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 n a RESERVED loc
13b0e 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
13b0f 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 65 e file. If the e
13b10 78 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 0a xFlag parameter.
13b11 20 20 20 20 2a 2a 20 69 73 20 74 72 75 65 2c 20 ** is true,
13b12 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 then immediately
13b13 20 75 70 67 72 61 64 65 20 74 68 69 73 20 74 6f upgrade this to
13b14 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
13b15 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 62 ck. The. ** b
13b16 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c usy-handler call
13b17 62 61 63 6b 20 63 61 6e 20 62 65 20 75 73 65 64 back can be used
13b18 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 when upgrading
13b19 74 6f 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 to the EXCLUSIVE
13b1a 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c 20 62 75 . ** lock, bu
13b1b 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62 74 61 69 t not when obtai
13b1c 6e 69 6e 67 20 74 68 65 20 52 45 53 45 52 56 45 ning the RESERVE
13b1d 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 D lock.. */.
13b1e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
13b1f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 sLock(pPager->fd
13b20 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 , RESERVED_LOCK)
13b21 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
13b22 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13b23 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
13b24 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 3b PAGER_RESERVED;
13b25 0a 20 20 20 20 20 20 69 66 28 20 65 78 46 6c 61 . if( exFla
13b26 67 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 g ){. rc
13b27 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f = pager_wait_on_
13b28 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 lock(pPager, EXC
13b29 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 LUSIVE_LOCK);.
13b2a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
13b2b 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 69 /* If the requi
13b2c 72 65 64 20 6c 6f 63 6b 73 20 77 65 72 65 20 73 red locks were s
13b2d 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 uccessfully obta
13b2e 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68 65 20 6a ined, open the j
13b2f 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
13b30 6c 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 le and write the
13b31 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 first journal-h
13b32 65 61 64 65 72 20 74 6f 20 69 74 2e 0a 20 20 20 eader to it..
13b33 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
13b34 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
13b35 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
13b36 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
13b37 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 ODE_OFF ){.
13b38 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e rc = pager_open
13b39 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 _journal(pPager)
13b3a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
13b3b 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
13b3c 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 r->jfd) && pPage
13b3d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 r->journalOff==0
13b3e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
13b3f 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65 happens when the
13b40 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 78 pager was in ex
13b41 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d clusive-access m
13b42 6f 64 65 20 74 68 65 20 6c 61 73 74 0a 20 20 20 ode the last.
13b43 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 65 61 64 ** time a (read
13b44 20 6f 72 20 77 72 69 74 65 29 20 74 72 61 6e 73 or write) trans
13b45 61 63 74 69 6f 6e 20 77 61 73 20 73 75 63 63 65 action was succe
13b46 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 ssfully conclude
13b47 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 69 73 d. ** by this
13b48 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 connection. Ins
13b49 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69 6e 67 tead of deleting
13b4a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13b4b 65 20 69 74 20 77 61 73 20 0a 20 20 20 20 2a 2a e it was . **
13b4c 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 20 65 kept open and e
13b4d 69 74 68 65 72 20 77 61 73 20 74 72 75 6e 63 61 ither was trunca
13b4e 74 65 64 20 74 6f 20 30 20 62 79 74 65 73 20 6f ted to 0 bytes o
13b4f 72 20 69 74 73 20 68 65 61 64 65 72 20 77 61 73 r its header was
13b50 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 72 69 74 . ** overwrit
13b51 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a ten with zeros..
13b52 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
13b53 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d t( pPager->nRec=
13b54 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
13b55 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 ( pPager->dbOrig
13b56 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 Size==0 );. a
13b57 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
13b58 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
13b59 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f rc = pager_o
13b5a 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 pen_journal(pPag
13b5b 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 er);. }.. PAGE
13b5c 52 54 52 41 43 45 28 28 22 54 52 41 4e 53 41 43 RTRACE(("TRANSAC
13b5d 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45 TION %d\n", PAGE
13b5e 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 RID(pPager)));.
13b5f 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e assert( !isOpen
13b60 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c (pPager->jfd) ||
13b61 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13b62 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c Off>0 || rc!=SQL
13b63 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 ITE_OK );. if(
13b64 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13b65 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 . assert( !pP
13b66 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13b67 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67 6e 6f 72 );. /* Ignor
13b68 65 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 74 e any IO error t
13b69 68 61 74 20 6f 63 63 75 72 73 20 77 69 74 68 69 hat occurs withi
13b6a 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e n pager_end_tran
13b6b 73 61 63 74 69 6f 6e 28 29 2e 20 54 68 65 0a 20 saction(). The.
13b6c 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 ** purpose of
13b6d 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 74 6f this call is to
13b6e 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 reset the inter
13b6f 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65 nal state of the
13b70 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 73 75 pager. ** su
13b71 62 2d 73 79 73 74 65 6d 2e 20 49 74 20 64 6f 65 b-system. It doe
13b72 73 6e 27 74 20 6d 61 74 74 65 72 20 69 66 20 74 sn't matter if t
13b73 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 he journal-file
13b74 69 73 20 6e 6f 74 20 70 72 6f 70 65 72 6c 79 0a is not properly.
13b75 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 ** finalized
13b76 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 28 at this point (
13b77 73 69 6e 63 65 20 69 74 20 69 73 20 6e 6f 74 20 since it is not
13b78 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 a valid journal
13b79 66 69 6c 65 20 61 6e 79 77 61 79 29 2e 0a 20 20 file anyway)..
13b7a 20 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 65 */. pager_e
13b7b 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
13b7c 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 Pager, 0);. }.
13b7d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
13b7e 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 *.** Mark a sing
13b7f 6c 65 20 64 61 74 61 20 70 61 67 65 20 61 73 20 le data page as
13b80 77 72 69 74 65 61 62 6c 65 2e 20 54 68 65 20 70 writeable. The p
13b81 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 age is written i
13b82 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d 61 69 6e nto the .** main
13b83 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d journal or sub-
13b84 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 71 75 69 journal as requi
13b85 72 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 red. If the page
13b86 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
13b87 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 6a .** one of the j
13b88 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 63 6f 72 ournals, the cor
13b89 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 responding bit i
13b8a 73 20 73 65 74 20 69 6e 20 74 68 65 20 0a 2a 2a s set in the .**
13b8b 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 Pager.pInJourna
13b8c 6c 20 62 69 74 76 65 63 20 61 6e 64 20 74 68 65 l bitvec and the
13b8d 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
13b8e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 pInSavepoint bit
13b8f 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e 79 20 6f vecs.** of any o
13b90 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 61 pen savepoints a
13b91 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a s appropriate..*
13b92 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
13b93 65 72 5f 77 72 69 74 65 28 50 67 48 64 72 20 2a er_write(PgHdr *
13b94 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 44 pPg){. void *pD
13b95 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 ata = pPg->pData
13b96 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
13b97 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b r = pPg->pPager;
13b98 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
13b99 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 54 68 69 TE_OK;.. /* Thi
13b9a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 s routine is not
13b9b 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73 73 20 61 called unless a
13b9c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 transaction has
13b9d 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 already been.
13b9e 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20 20 2a 2f ** started.. */
13b9f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13ba0 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
13ba1 52 45 53 45 52 56 45 44 20 29 3b 0a 0a 20 20 2f RESERVED );.. /
13ba2 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 68 61 * If an error ha
13ba3 73 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c s been previousl
13ba4 79 20 64 65 74 65 63 74 65 64 2c 20 77 65 20 73 y detected, we s
13ba5 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 20 20 2a hould not be. *
13ba6 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 * calling this r
13ba7 6f 75 74 69 6e 65 2e 20 20 52 65 70 65 61 74 20 outine. Repeat
13ba8 74 68 65 20 65 72 72 6f 72 20 66 6f 72 20 72 6f the error for ro
13ba9 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a 2f 0a 20 bustness.. */.
13baa 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
13bab 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 20 72 r->errCode) ) r
13bac 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
13bad 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 48 69 67 rCode;.. /* Hig
13bae 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e her-level routin
13baf 65 73 20 6e 65 76 65 72 20 63 61 6c 6c 20 74 68 es never call th
13bb0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 64 is function if d
13bb1 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 20 atabase is not.
13bb2 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e 20 20 42 ** writable. B
13bb3 75 74 20 63 68 65 63 6b 20 61 6e 79 77 61 79 2c ut check anyway,
13bb4 20 6a 75 73 74 20 66 6f 72 20 72 6f 62 75 73 74 just for robust
13bb5 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66 28 20 4e ness. */. if( N
13bb6 45 56 45 52 28 70 50 61 67 65 72 2d 3e 72 65 61 EVER(pPager->rea
13bb7 64 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 6e 20 dOnly) ) return
13bb8 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 0a 20 20 SQLITE_PERM;..
13bb9 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
13bba 3e 73 65 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20 >setMaster );..
13bbb 20 43 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29 CHECK_PAGE(pPg)
13bbc 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 ;.. /* Mark the
13bbd 20 70 61 67 65 20 61 73 20 64 69 72 74 79 2e 20 page as dirty.
13bbe 20 49 66 20 74 68 65 20 70 61 67 65 20 68 61 73 If the page has
13bbf 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 already been wr
13bc0 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 itten. ** to th
13bc1 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20 77 e journal then w
13bc2 65 20 63 61 6e 20 72 65 74 75 72 6e 20 72 69 67 e can return rig
13bc3 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 ht away.. */.
13bc4 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
13bc5 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 69 eDirty(pPg);. i
13bc6 66 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c f( pageInJournal
13bc7 28 70 50 67 29 20 26 26 20 21 73 75 62 6a 52 65 (pPg) && !subjRe
13bc8 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
13bc9 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 ){. pPager->d
13bca 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 bModified = 1;.
13bcb 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 }else{.. /*
13bcc 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 If we get this f
13bcd 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 ar, it means tha
13bce 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 t the page needs
13bcf 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 77 72 to be. ** wr
13bd0 69 74 74 65 6e 20 74 6f 20 74 68 65 20 74 72 61 itten to the tra
13bd1 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c nsaction journal
13bd2 20 6f 72 20 74 68 65 20 63 6b 65 63 6b 70 6f 69 or the ckeckpoi
13bd3 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a nt journal. *
13bd4 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20 20 20 2a * or both.. *
13bd5 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68 65 72 20 *. ** Higher
13bd6 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 73 level routines s
13bd7 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 hould have alrea
13bd8 64 79 20 73 74 61 72 74 65 64 20 61 20 74 72 61 dy started a tra
13bd9 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a nsaction,. **
13bda 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 65 which means the
13bdb 79 20 68 61 76 65 20 61 63 71 75 69 72 65 64 20 y have acquired
13bdc 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6c 6f the necessary lo
13bdd 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65 64 0a 20 cks and opened.
13bde 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b ** a rollback
13bdf 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f 75 62 6c journal. Doubl
13be0 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 73 e-check to makes
13be1 20 73 75 72 65 20 74 68 69 73 20 69 73 20 74 68 sure this is th
13be2 65 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 e case.. */.
13be3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
13be4 61 67 65 72 42 65 67 69 6e 28 70 50 61 67 65 72 agerBegin(pPager
13be5 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 73 75 62 , 0, pPager->sub
13be6 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20 20 jInMemory);.
13be7 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 if( NEVER(rc!=SQ
13be8 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 LITE_OK) ){.
13be9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
13bea 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 4f 70 }. if( !isOp
13beb 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
13bec 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
13bed 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
13bee 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b URNALMODE_OFF ){
13bef 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
13bf0 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
13bf1 6c 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 l );. rc =
13bf2 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
13bf3 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
13bf4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13bf5 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
13bf6 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
13bf7 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 r->dbModified =
13bf8 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 1;. . /* The
13bf9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 transaction jou
13bfa 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 74 73 20 rnal now exists
13bfb 61 6e 64 20 77 65 20 68 61 76 65 20 61 20 52 45 and we have a RE
13bfc 53 45 52 56 45 44 20 6f 72 20 61 6e 0a 20 20 20 SERVED or an.
13bfd 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f ** EXCLUSIVE lo
13bfe 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 ck on the main d
13bff 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 57 atabase file. W
13c00 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
13c01 20 70 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20 page to. **
13c02 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
13c03 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 20 69 73 journal if it is
13c04 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 not there alrea
13c05 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 dy.. */. i
13c06 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 72 6e 61 f( !pageInJourna
13c07 6c 28 70 50 67 29 20 26 26 20 69 73 4f 70 65 6e l(pPg) && isOpen
13c08 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
13c09 0a 20 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e . if( pPg->
13c0a 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 pgno<=pPager->db
13c0b 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 OrigSize ){.
13c0c 20 20 20 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20 u32 cksum;.
13c0d 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 char *pDa
13c0e 74 61 32 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a ta2;.. /*
13c0f 20 57 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 We should never
13c10 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f write to the jo
13c11 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70 urnal file the p
13c12 61 67 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 age that.
13c13 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ** contains the
13c14 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e database locks.
13c15 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
13c16 61 73 73 65 72 74 20 76 65 72 69 66 69 65 73 0a assert verifies.
13c17 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ** that
13c18 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 we do not. */.
13c19 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
13c1a 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d g->pgno!=PAGER_M
13c1b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
13c1c 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 45 43 32 ;. CODEC2
13c1d 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
13c1e 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 pPg->pgno, 7, re
13c1f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
13c20 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 M, pData2);.
13c21 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 cksum = page
13c22 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 r_cksum(pPager,
13c23 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 (u8*)pData2);.
13c24 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
13c25 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
13c26 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 fd, pPager->jour
13c27 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e nalOff, pPg->pgn
13c28 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
13c29 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13c2a 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
13c2b 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
13c2c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 Pager->jfd, pDat
13c2d 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 a2, pPager->page
13c2e 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Size,.
13c2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c30 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
13c31 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20 nalOff + 4);.
13c32 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
13c33 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 ournalOff += pPa
13c34 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b ger->pageSize+4;
13c35 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13c36 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13c37 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13c38 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 rc = write32bi
13c39 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
13c3a 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13c3b 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 ff, cksum);.
13c3c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
13c3d 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 urnalOff += 4;.
13c3e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
13c3f 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20 IOTRACE(("JOUT
13c40 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 %p %d %lld %d\n"
13c41 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 , pPager, pPg->p
13c42 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 gno, .
13c43 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
13c44 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65 ournalOff, pPage
13c45 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20 r->pageSize));.
13c46 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 PAGER_INC
13c47 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f R(sqlite3_pager_
13c48 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 writej_count);.
13c49 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
13c4a 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70 E(("JOURNAL %d p
13c4b 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d age %d needSync=
13c4c 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 %d hash(%08x)\n"
13c4d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50 ,. P
13c4e 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
13c4f 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 pPg->pgno, .
13c50 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e ((pPg->
13c51 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
13c52 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67 _SYNC)?1:0), pag
13c53 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
13c54 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 ));.. /*
13c55 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72 Even if an IO or
13c56 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20 diskfull error
13c57 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a occurred while j
13c58 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20 ournalling the.
13c59 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 ** page i
13c5a 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 n the block abov
13c5b 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d e, set the need-
13c5c 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68 sync flag for th
13c5d 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 e page..
13c5e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 ** Otherwise, wh
13c5f 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 en the transacti
13c60 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 on is rolled bac
13c61 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a k, the logic in.
13c62 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62 ** playb
13c63 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77 ack_one_page() w
13c64 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74 ill think that t
13c65 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f he page needs to
13c66 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20 be restored.
13c67 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64 ** in the d
13c68 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e atabase file. An
13c69 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 d if an IO error
13c6a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f occurs while do
13c6b 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 ing so,.
13c6c 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69 ** then corrupti
13c6d 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 on may follow..
13c6e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
13c6f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e if( !pPager->n
13c70 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 oSync ){.
13c71 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
13c72 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13c73 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 ;. pPag
13c74 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 er->needSync = 1
13c75 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
13c76 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 /* An error
13c77 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 77 72 has occurred wr
13c78 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 iting to the jou
13c79 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a rnal file. The .
13c7a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 ** trans
13c7b 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 action will be r
13c7c 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 olled back by th
13c7d 65 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 e layer above..
13c7e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
13c7f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13c80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
13c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
13c82 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 }.. p
13c83 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 Pager->nRec++;.
13c84 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
13c85 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
13c86 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 l!=0 );.
13c87 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 rc = sqlite3Bitv
13c88 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 ecSet(pPager->pI
13c89 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 nJournal, pPg->p
13c8a 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 gno);. te
13c8b 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
13c8c 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
13c8d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
13c8e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
13c8f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
13c90 0a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61 . rc |= a
13c91 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
13c92 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 vecs(pPager, pPg
13c93 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
13c94 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13c95 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
13c96 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
13c97 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
13c98 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
13c99 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13c9a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
13c9b 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 if( !pPager->jou
13c9c 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21 rnalStarted && !
13c9d 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 pPager->noSync )
13c9e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d {. pPg-
13c9f 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f >flags |= PGHDR_
13ca0 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 NEED_SYNC;.
13ca1 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 pPager->nee
13ca2 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 dSync = 1;.
13ca3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 }. PAG
13ca4 45 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44 ERTRACE(("APPEND
13ca5 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 %d page %d need
13ca6 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 Sync=%d\n",.
13ca7 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 PAGE
13ca8 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
13ca9 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 ->pgno,.
13caa 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c ((pPg->fl
13cab 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
13cac 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20 YNC)?1:0)));.
13cad 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
13cae 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 /* If the stat
13caf 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 ement journal is
13cb0 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 open and the pa
13cb1 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c ge is not in it,
13cb2 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 . ** then wri
13cb3 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 te the current p
13cb4 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 age to the state
13cb5 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e ment journal. N
13cb6 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ote that. **
13cb7 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
13cb8 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 urnal format dif
13cb9 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 fers from the st
13cba 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 andard journal f
13cbb 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 ormat. ** in
13cbc 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 that it omits th
13cbd 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 e checksums and
13cbe 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 the header..
13cbf 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 */. if( subjR
13cc0 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 equiresPage(pPg)
13cc1 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
13cc2 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 ubjournalPage(pP
13cc3 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 g);. }. }..
13cc4 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 /* Update the d
13cc5 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64 atabase size and
13cc6 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 return.. */.
13cc7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13cc8 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 state>=PAGER_SHA
13cc9 52 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61 RED );. if( pPa
13cca 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d ger->dbSize<pPg-
13ccb 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 >pgno ){. pPa
13ccc 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 ger->dbSize = pP
13ccd 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 g->pgno;. }. r
13cce 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
13ccf 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70 ** Mark a data p
13cd0 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 age as writeable
13cd1 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d . This routine m
13cd2 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 ust be called be
13cd3 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 fore .** making
13cd4 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67 changes to a pag
13cd5 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 e. The caller mu
13cd6 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 st check the ret
13cd7 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 urn value .** of
13cd8 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
13cd9 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f nd be careful no
13cda 74 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 20 t to change any
13cdb 70 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 73 page data unless
13cdc 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e .** this routin
13cdd 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
13cde 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 _OK..**.** The d
13cdf 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
13ce0 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
13ce1 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 and pager_write(
13ce2 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a ) is that this.*
13ce3 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 * function also
13ce4 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 deals with the s
13ce5 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 pecial case wher
13ce6 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 e 2 or more page
13ce7 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 s.** fit on a si
13ce8 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 ngle disk sector
13ce9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 . In this case a
13cea 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 ll co-resident p
13ceb 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 ages.** must hav
13cec 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 e been written t
13ced 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
13cee 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e le before return
13cef 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ing..**.** If an
13cf0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 error occurs, S
13cf1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 QLITE_NOMEM or a
13cf2 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
13cf3 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 is returned.** a
13cf4 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 4f s appropriate. O
13cf5 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 therwise, SQLITE
13cf6 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 _OK..*/.SQLITE_P
13cf7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13cf8 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50 e3PagerWrite(DbP
13cf9 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 age *pDbPage){.
13cfa 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13cfb 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 _OK;.. PgHdr *p
13cfc 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 Pg = pDbPage;.
13cfd 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
13cfe 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 pPg->pPager;. P
13cff 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 gno nPagePerSect
13d00 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 or = (pPager->se
13d01 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d ctorSize/pPager-
13d02 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69 >pageSize);.. i
13d03 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f f( nPagePerSecto
13d04 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 r>1 ){. Pgno
13d05 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 nPageCount;
13d06 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
13d07 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
13d08 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
13d09 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 /. Pgno pg1;
13d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13d0b 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 /* First page of
13d0c 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 the sector pPg
13d0d 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a is located on. *
13d0e 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b /. int nPage;
13d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13d10 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
13d11 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 es starting at p
13d12 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f g1 to journal */
13d13 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 . int ii;
13d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13d15 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
13d16 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 /. int needSy
13d17 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 nc = 0;
13d18 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 /* True if any p
13d19 61 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45 age has PGHDR_NE
13d1a 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 ED_SYNC */..
13d1b 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 /* Set the doNot
13d1c 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e 20 Sync flag to 1.
13d1d 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 This is because
13d1e 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 we cannot allow
13d1f 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a a journal. **
13d20 20 68 65 61 64 65 72 20 74 6f 20 62 65 20 77 72 header to be wr
13d21 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74 68 itten between th
13d22 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c 65 e pages journale
13d23 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
13d24 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 on.. */. a
13d25 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b ssert( !MEMDB );
13d26 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13d27 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d ger->doNotSync==
13d28 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 0 );. pPager-
13d29 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b 0a >doNotSync = 1;.
13d2a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 69 . /* This tri
13d2b 63 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ck assumes that
13d2c 62 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 69 both the page-si
13d2d 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 ze and sector-si
13d2e 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6e ze are. ** an
13d2f 20 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 6f integer power o
13d30 66 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 72 f 2. It sets var
13d31 69 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 65 iable pg1 to the
13d32 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 20 identifier.
13d33 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 ** of the first
13d34 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 page of the sect
13d35 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 or pPg is locate
13d36 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 d on.. */.
13d37 20 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67 pg1 = ((pPg->pg
13d38 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50 no-1) & ~(nPageP
13d39 65 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31 erSector-1)) + 1
13d3a 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 ;.. sqlite3Pa
13d3b 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 gerPagecount(pPa
13d3c 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 ger, (int *)&nPa
13d3d 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69 66 geCount);. if
13d3e 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 ( pPg->pgno>nPag
13d3f 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 eCount ){.
13d40 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 nPage = (pPg->pg
13d41 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20 no - pg1)+1;.
13d42 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 2b }else if( (pg1+
13d43 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 nPagePerSector-1
13d44 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a )>nPageCount ){.
13d45 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 nPage = nP
13d46 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a ageCount+1-pg1;.
13d47 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13d48 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 65 nPage = nPagePe
13d49 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 rSector;. }.
13d4a 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 3e assert(nPage>
13d4b 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 0);. assert(p
13d4c 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a g1<=pPg->pgno);.
13d4d 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31 2b assert((pg1+
13d4e 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f nPage)>pPg->pgno
13d4f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 );.. for(ii=0
13d50 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 63 ; ii<nPage && rc
13d51 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b ==SQLITE_OK; ii+
13d52 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 +){. Pgno p
13d53 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 20 g = pg1+ii;.
13d54 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 0a PgHdr *pPage;.
13d55 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 50 if( pg==pP
13d56 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 g->pgno || !sqli
13d57 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 te3BitvecTest(pP
13d58 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13d59 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 20 , pg) ){.
13d5a 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f 4d if( pg!=PAGER_M
13d5b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
13d5c 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
13d5d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
13d5e 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 50 (pPager, pg, &pP
13d5f 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
13d60 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13d61 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
13d62 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 rc = pager_writ
13d63 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
13d64 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d if( pPage-
13d65 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
13d66 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 D_SYNC ){.
13d67 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 needSync
13d68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
13d69 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 assert(pPage
13d6a 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 r->needSync);.
13d6b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
13d6c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
13d6d 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 agerUnref(pPage)
13d6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
13d6f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
13d70 6c 73 65 20 69 66 28 20 28 70 50 61 67 65 20 3d lse if( (pPage =
13d71 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 pager_lookup(pP
13d72 61 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b ager, pg))!=0 ){
13d73 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
13d74 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ge->flags&PGHDR_
13d75 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 NEED_SYNC ){.
13d76 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 needSync
13d77 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
13d78 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
13d79 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b gerUnref(pPage);
13d7a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
13d7b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 47 /* If the PG
13d7c 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
13d7d 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 6e ag is set for an
13d7e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 70 y of the nPage p
13d7f 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 61 ages . ** sta
13d80 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 68 rting at pg1, th
13d81 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 en it needs to b
13d82 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66 e set for all of
13d83 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a 20 them. Because.
13d84 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f ** writing to
13d85 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e 50 any of these nP
13d86 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 61 age pages may da
13d87 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 2c mage the others,
13d88 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 the. ** jour
13d89 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 6f nal file must co
13d8a 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 63 ntain sync()ed c
13d8b 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20 opies of all of
13d8c 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f them. ** befo
13d8d 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 re any of them c
13d8e 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 an be written ou
13d8f 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
13d90 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
13d91 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13d92 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53 79 6e 63 E_OK && needSync
13d93 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
13d94 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 ( !MEMDB && pPag
13d95 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b er->noSync==0 );
13d96 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b . for(ii=0;
13d97 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 2b 2b 29 ii<nPage; ii++)
13d98 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 20 {. PgHdr
13d99 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c *pPage = pager_l
13d9a 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 ookup(pPager, pg
13d9b 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20 20 69 1+ii);. i
13d9c 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
13d9d 20 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61 pPage->fla
13d9e 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 gs |= PGHDR_NEED
13d9f 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 _SYNC;.
13da0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
13da1 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
13da2 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
13da3 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 assert(pPager
13da4 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 ->needSync);.
13da5 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 }.. assert(
13da6 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
13da7 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 50 61 67 c==1 );. pPag
13da8 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 er->doNotSync =
13da9 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
13daa 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
13dab 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 (pDbPage);. }.
13dac 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
13dad 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
13dae 20 69 66 20 74 68 65 20 70 61 67 65 20 67 69 76 if the page giv
13daf 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 en in the argume
13db0 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c nt was previousl
13db1 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 73 y passed.** to s
13db2 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13db3 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f (). In other wo
13db4 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 45 rds, return TRUE
13db5 20 69 66 20 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 if it is ok.**
13db6 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 63 6f to change the co
13db7 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 ntent of the pag
13db8 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 e..*/.#ifndef ND
13db9 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
13dba 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
13dbb 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
13dbc 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
13dbd 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 6c 61 67 return pPg->flag
13dbe 73 26 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 7d s&PGHDR_DIRTY;.}
13dbf 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
13dc0 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
13dc1 45 4c 45 54 45 0a 2f 2a 0a 2a 2a 20 41 20 63 61 ELETE./*.** A ca
13dc2 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 ll to this routi
13dc3 6e 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67 ne tells the pag
13dc4 65 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f er that it is no
13dc5 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 2a t necessary to.*
13dc6 2a 20 77 72 69 74 65 20 74 68 65 20 69 6e 66 6f * write the info
13dc7 72 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 rmation on page
13dc8 70 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20 pPg back to the
13dc9 64 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 disk, even thoug
13dca 68 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 6d h.** that page m
13dcb 69 67 68 74 20 62 65 20 6d 61 72 6b 65 64 20 61 ight be marked a
13dcc 73 20 64 69 72 74 79 2e 20 20 54 68 69 73 20 68 s dirty. This h
13dcd 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d appens, for exam
13dce 70 6c 65 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 ple, when.** the
13dcf 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 61 page has been a
13dd0 64 64 65 64 20 61 73 20 61 20 6c 65 61 66 20 6f dded as a leaf o
13dd1 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 61 f the freelist a
13dd2 6e 64 20 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e nd so its.** con
13dd3 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d tent no longer m
13dd4 61 74 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 atters..**.** Th
13dd5 65 20 6f 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 e overlying soft
13dd6 77 61 72 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 ware layer calls
13dd7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 this routine wh
13dd8 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 en all of the da
13dd9 74 61 0a 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 ta.** on the giv
13dda 65 6e 20 70 61 67 65 20 69 73 20 75 6e 75 73 65 en page is unuse
13ddb 64 2e 20 54 68 65 20 70 61 67 65 72 20 6d 61 72 d. The pager mar
13ddc 6b 73 20 74 68 65 20 70 61 67 65 20 61 73 20 63 ks the page as c
13ddd 6c 65 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 lean so.** that
13dde 69 74 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 it does not get
13ddf 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e written to disk.
13de0 0a 2a 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f .**.** Tests sho
13de1 77 20 74 68 61 74 20 74 68 69 73 20 6f 70 74 69 w that this opti
13de2 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 mization can qua
13de3 64 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 druple the speed
13de4 20 6f 66 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 of large .** DE
13de5 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e LETE operations.
13de6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13de7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
13de8 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 50 67 agerDontWrite(Pg
13de9 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 Hdr *pPg){. Pag
13dea 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
13deb 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 ->pPager;. if(
13dec 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
13ded 52 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 67 R_DIRTY) && pPag
13dee 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d er->nSavepoint==
13def 30 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 0 ){. PAGERTR
13df0 41 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 ACE(("DONT_WRITE
13df1 20 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e page %d of %d\n
13df2 22 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 ", pPg->pgno, PA
13df3 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
13df4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 . IOTRACE(("C
13df5 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 LEAN %p %d\n", p
13df6 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
13df7 29 29 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 )). pPg->flag
13df8 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f s |= PGHDR_DONT_
13df9 57 52 49 54 45 3b 0a 23 69 66 64 65 66 20 53 51 WRITE;.#ifdef SQ
13dfa 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
13dfb 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 . pPg->pageHa
13dfc 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 sh = pager_pageh
13dfd 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 ash(pPg);.#endif
13dfe 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
13dff 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
13e00 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 20 _SECURE_DELETE)
13e01 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 */../*.** This r
13e02 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
13e03 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 to increment th
13e04 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 e value of the d
13e05 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a 2a atabase file .**
13e06 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c change-counter,
13e07 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62 stored as a 4-b
13e08 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
13e09 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 20 nteger starting
13e0a 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 at .** byte offs
13e0b 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61 67 et 24 of the pag
13e0c 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 er file..**.** I
13e0d 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f f the isDirectMo
13e0e 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c de flag is zero,
13e0f 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f then this is do
13e10 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a ne by calling .*
13e11 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 * sqlite3PagerWr
13e12 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c ite() on page 1,
13e13 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 then modifying
13e14 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
13e15 74 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 the.** page data
13e16 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
13e17 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 he file will be
13e18 75 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 updated when the
13e19 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e current.** tran
13e1a 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
13e1b 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tted..**.** The
13e1c 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
13e1d 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f g may only be no
13e1e 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69 n-zero if the li
13e1f 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c brary was compil
13e20 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 ed.** with the S
13e21 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
13e22 4d 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20 MIC_WRITE macro
13e23 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 defined. In this
13e24 20 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44 case,.** if isD
13e25 69 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 irect is non-zer
13e26 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 o, then the data
13e27 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 70 64 base file is upd
13e28 61 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a ated directly.**
13e29 20 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75 by writing an u
13e2a 70 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f pdated version o
13e2b 66 20 70 61 67 65 20 31 20 75 73 69 6e 67 20 61 f page 1 using a
13e2c 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a call to the .**
13e2d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
13e2e 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ) function..*/.s
13e2f 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
13e30 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13e31 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
13e32 2c 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f , int isDirectMo
13e33 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 de){. int rc =
13e34 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
13e35 20 44 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69 Declare and ini
13e36 74 69 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74 tialize constant
13e37 20 69 6e 74 65 67 65 72 20 27 69 73 44 69 72 65 integer 'isDire
13e38 63 74 27 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a ct'. If the. **
13e39 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
13e3a 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e timization is en
13e3b 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 abled in this bu
13e3c 69 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65 ild, then isDire
13e3d 63 74 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 ct. ** is initi
13e3e 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61 alized to the va
13e3f 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 lue passed as th
13e40 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70 e isDirectMode p
13e41 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 6f arameter. ** to
13e42 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 this function.
13e43 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 Otherwise, it is
13e44 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a always set to z
13e45 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ero.. **. ** T
13e46 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20 he idea is that
13e47 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 if the atomic-wr
13e48 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
13e49 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 is not. ** ena
13e4a 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 bled at compile
13e4b 74 69 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 6c time, the compil
13e4c 65 72 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 20 er can omit the
13e4d 74 65 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 69 tests of. ** 'i
13e4e 73 44 69 72 65 63 74 27 20 62 65 6c 6f 77 2c 20 sDirect' below,
13e4f 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 as well as the b
13e50 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 6e lock enclosed in
13e51 20 74 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 69 the. ** "if( i
13e52 73 44 69 72 65 63 74 20 29 22 20 63 6f 6e 64 69 sDirect )" condi
13e53 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 tion.. */.#ifnd
13e54 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
13e55 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 20 _ATOMIC_WRITE.#
13e56 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f define DIRECT_MO
13e57 44 45 20 30 0a 20 20 61 73 73 65 72 74 28 20 69 DE 0. assert( i
13e58 73 44 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 sDirectMode==0 )
13e59 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
13e5a 45 54 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 ETER(isDirectMod
13e5b 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 e);.#else.# defi
13e5c 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 69 ne DIRECT_MODE i
13e5d 73 44 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e 64 sDirectMode.#end
13e5e 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 if.. assert( pP
13e5f 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13e60 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
13e61 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 if( !pPager->ch
13e62 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 angeCountDone &&
13e63 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e pPager->dbSize>
13e64 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 0 ){. PgHdr *
13e65 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 20 pPgHdr;
13e66 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65 /* Refere
13e67 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f nce to page 1 */
13e68 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 65 5f . u32 change_
13e69 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20 20 counter;
13e6a 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76 61 /* Initial va
13e6b 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63 6f lue of change-co
13e6c 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a 0a unter field */..
13e6d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
13e6e 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 ger->tempFile &&
13e6f 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
13e70 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f fd) );.. /* O
13e71 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74 68 pen page 1 of th
13e72 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74 69 e file for writi
13e73 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ng. */. rc =
13e74 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
13e75 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67 48 pPager, 1, &pPgH
13e76 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 dr);. assert(
13e77 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 63 pPgHdr==0 || rc
13e78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a ==SQLITE_OK );..
13e79 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 6f /* If page o
13e7a 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 20 73 ne was fetched s
13e7b 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e 64 uccessfully, and
13e7c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13e7d 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70 65 s not. ** ope
13e7e 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63 74 rating in direct
13e7f 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67 65 -mode, make page
13e80 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20 57 68 1 writable. Wh
13e81 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20 20 2a en not in . *
13e82 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 70 * direct mode, p
13e83 61 67 65 20 31 20 69 73 20 61 6c 77 61 79 73 20 age 1 is always
13e84 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20 61 6e held in cache an
13e85 64 20 68 65 6e 63 65 20 74 68 65 20 50 61 67 65 d hence the Page
13e86 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20 61 62 rGet(). ** ab
13e87 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20 73 75 ove is always su
13e88 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e 63 65 ccessful - hence
13e89 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e 20 72 the ALWAYS on r
13e8a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 c==SQLITE_OK..
13e8b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 44 49 */. if( !DI
13e8c 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41 4c 57 RECT_MODE && ALW
13e8d 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f AYS(rc==SQLITE_O
13e8e 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d K) ){. rc =
13e8f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
13e90 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 te(pPgHdr);.
13e91 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
13e92 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13e93 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 /* Increment t
13e94 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20 72 65 he value just re
13e95 61 64 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 ad and write it
13e96 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32 34 2e back to byte 24.
13e97 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e 67 65 */. change
13e98 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c 69 74 _counter = sqlit
13e99 65 33 47 65 74 34 62 79 74 65 28 28 75 38 2a 29 e3Get4byte((u8*)
13e9a 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 pPager->dbFileVe
13e9b 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61 6e 67 rs);. chang
13e9c 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 20 20 e_counter++;.
13e9d 20 20 20 70 75 74 33 32 62 69 74 73 28 28 28 63 put32bits(((c
13e9e 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70 44 61 har*)pPgHdr->pDa
13e9f 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65 5f 63 ta)+24, change_c
13ea0 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20 20 20 ounter);..
13ea1 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e /* If running in
13ea2 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 72 direct mode, wr
13ea3 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ite the contents
13ea4 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 74 68 of page 1 to th
13ea5 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 e file. */.
13ea6 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f 44 45 if( DIRECT_MODE
13ea7 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
13ea8 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d 20 70 t void *zBuf = p
13ea9 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a 20 20 PgHdr->pData;.
13eaa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
13eab 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
13eac 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 >0 );. rc
13ead 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
13eae 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42 e(pPager->fd, zB
13eaf 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 uf, pPager->page
13eb0 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Size, 0);.
13eb1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13eb2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
13eb3 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
13eb4 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 ountDone = 1;.
13eb5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
13eb6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 50 61 lse{. pPa
13eb7 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 ger->changeCount
13eb8 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 Done = 1;.
13eb9 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
13eba 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 67 65 Release the page
13ebb 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 reference. */.
13ebc 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
13ebd 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 nref(pPgHdr);.
13ebe 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13ebf 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 ../*.** Sync the
13ec0 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f 20 64 pager file to d
13ec1 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61 20 6e isk. This is a n
13ec2 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f o-op for in-memo
13ec3 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72 20 70 ry files.** or p
13ec4 61 67 65 73 20 77 69 74 68 20 74 68 65 20 50 61 ages with the Pa
13ec5 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 67 20 ger.noSync flag
13ec6 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 set..**.** If su
13ec7 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63 61 6c ccessful, or cal
13ec8 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72 20 66 led on a pager f
13ec9 6f 72 20 77 68 69 63 68 20 69 74 20 69 73 20 61 or which it is a
13eca 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a 2a 20 no-op, this.**
13ecb 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
13ecc 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 SQLITE_OK. Othe
13ecd 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65 72 72 rwise, an IO err
13ece 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
13ecf 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
13ed0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13ed1 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 67 65 e3PagerSync(Page
13ed2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
13ed3 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
13ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13ed5 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
13ed6 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 e */. assert( !
13ed7 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 70 MEMDB );. if( p
13ed8 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b Pager->noSync ){
13ed9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
13eda 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
13edb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13edc 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c Sync(pPager->fd,
13edd 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
13ede 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ags);. }. retu
13edf 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
13ee0 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 Sync the databas
13ee1 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 e file for the p
13ee2 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 ager pPager. zMa
13ee3 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ster points to t
13ee4 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 he name.** of a
13ee5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
13ee6 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ile that should
13ee7 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
13ee8 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a the individual.*
13ee9 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 * journal file.
13eea 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e zMaster may be N
13eeb 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e ULL, which is in
13eec 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 terpreted as no
13eed 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 master.** journa
13eee 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 l (a single data
13eef 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
13ef0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f )..**.** This ro
13ef1 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 utine ensures th
13ef2 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 at:.**.** * Th
13ef3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13ef4 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 change-counter i
13ef5 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20 s updated,.**
13ef6 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 * the journal is
13ef7 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20 synced (unless
13ef8 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 the atomic-write
13ef9 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
13efa 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 used),.** * a
13efb 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 61 ll dirty pages a
13efc 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 re written to th
13efd 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
13efe 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 .** * the dat
13eff 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 72 abase file is tr
13f00 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 71 75 uncated (if requ
13f01 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 ired), and.**
13f02 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
13f03 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a ile synced. .**.
13f04 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e ** The only thin
13f05 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 g that remains t
13f06 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 o commit the tra
13f07 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66 nsaction is to f
13f08 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c inalize .** (del
13f09 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72 ete, truncate or
13f0a 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 74 20 zero the first
13f0b 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75 part of) the jou
13f0c 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a rnal file (or .*
13f0d 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 * delete the mas
13f0e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
13f0f 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a if specified)..
13f10 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
13f11 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c if zMaster==NULL
13f12 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 , this does not
13f13 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 76 overwrite a prev
13f14 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61 ious value.** pa
13f15 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74 ssed to an sqlit
13f16 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
13f17 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a seOne() call..**
13f18 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c .** If the final
13f19 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 parameter - noS
13f1a 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 ync - is true, t
13f1b 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
13f1c 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 file itself.**
13f1d 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 is not synced. T
13f1e 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 he caller must c
13f1f 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 all sqlite3Pager
13f20 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 Sync() directly
13f21 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 to.** sync the d
13f22 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 atabase file bef
13f23 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d ore calling Comm
13f24 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 itPhaseTwo() to
13f25 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f delete the.** jo
13f26 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 urnal file in th
13f27 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 is case..*/.SQLI
13f28 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13f29 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
13f2a 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 tPhaseOne(. Pag
13f2b 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
13f2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13f2d 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
13f2e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d const char *zM
13f2f 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 aster,
13f30 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c /* If not NULL
13f31 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 , the master jou
13f32 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 rnal name */. i
13f33 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20 nt noSync
13f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13f35 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74 * True to omit t
13f36 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20 he xSync on the
13f37 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 db file */.){.
13f38 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13f39 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
13f3a 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
13f3b 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62 4f 72 /.. /* The dbOr
13f3c 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65 72 20 igSize is never
13f3d 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d set if journal_m
13f3e 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61 73 73 ode=OFF */. ass
13f3f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
13f40 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
13f41 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
13f42 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 || pPager->dbOri
13f43 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20 20 2f gSize==0 );.. /
13f44 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65 72 72 * If a prior err
13f45 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 69 or occurred, thi
13f46 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 s routine should
13f47 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 2e 20 not be called.
13f48 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a 20 69 ROLLBACK. ** i
13f49 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 s the appropriat
13f4a 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e e response to an
13f4b 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f 4d 4d error, not COMM
13f4c 49 54 2e 20 20 47 75 61 72 64 20 61 67 61 69 6e IT. Guard again
13f4d 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67 20 65 st. ** coding e
13f4e 72 72 6f 72 73 20 62 79 20 72 65 70 65 61 74 69 rrors by repeati
13f4f 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65 72 72 ng the prior err
13f50 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 or. */. if( NEV
13f51 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f ER(pPager->errCo
13f52 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 50 61 de) ) return pPa
13f53 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 ger->errCode;..
13f54 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 41 PAGERTRACE(("DA
13f55 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c TABASE SYNC: Fil
13f56 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 e=%s zMaster=%s
13f57 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 nSize=%d\n", .
13f58 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c pPager->zFil
13f59 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 ename, zMaster,
13f5a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29 pPager->dbSize))
13f5b 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 26 ;.. if( MEMDB &
13f5c 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 & pPager->dbModi
13f5d 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 49 fied ){. /* I
13f5e 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d f this is an in-
13f5f 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e 6f memory db, or no
13f60 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
13f61 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 20 written to, or
13f62 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 6e 63 this. ** func
13f63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 tion has already
13f64 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 74 been called, it
13f65 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e 6f 2d is mostly a no-
13f66 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 61 6e op. However, an
13f67 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 70 20 y. ** backup
13f68 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 65 64 in progress need
13f69 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74 65 s to be restarte
13f6a 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 d.. */. sq
13f6b 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 lite3BackupResta
13f6c 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b rt(pPager->pBack
13f6d 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 up);. }else if(
13f6e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
13f6f 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20 PAGER_SYNCED &&
13f70 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
13f71 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 68 ed ){.. /* Th
13f72 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
13f73 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 k updates the ch
13f74 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 45 78 ange-counter. Ex
13f75 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 20 20 actly how it.
13f76 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 64 65 ** does this de
13f77 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 pends on whether
13f78 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d or not the atom
13f79 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
13f7a 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 77 61 zation. ** wa
13f7b 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d s enabled at com
13f7c 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69 pile time, and i
13f7d 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 f this transacti
13f7e 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20 on meets the .
13f7f 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 69 ** runtime cri
13f80 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 65 teria to use the
13f81 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20 20 operation: .
13f82 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 **. ** *
13f83 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 The file-system
13f84 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f supports the ato
13f85 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72 mic-write proper
13f86 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20 ty for. **
13f87 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a blocks of siz
13f88 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64 e page-size, and
13f89 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 . ** * Th
13f8a 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 is commit is not
13f8b 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 part of a multi
13f8c 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f -file transactio
13f8d 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20 n, and. **
13f8e 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 * Exactly one p
13f8f 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 age has been mod
13f90 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 ified and store
13f91 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
13f92 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
13f93 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 ** If the optimi
13f94 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65 zation was not e
13f95 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c nabled at compil
13f96 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65 e time, then the
13f97 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e . ** pager_in
13f98 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
13f99 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 () function is c
13f9a 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65 20 alled to update
13f9b 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20 2a the change. *
13f9c 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 69 6e * counter in 'in
13f9d 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 49 66 direct-mode'. If
13f9e 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f the optimizatio
13f9f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e n is compiled in
13fa0 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e but. ** is n
13fa1 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 74 6f ot applicable to
13fa2 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
13fa3 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4a n, call sqlite3J
13fa4 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 0a 20 ournalCreate().
13fa5 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 75 ** to make su
13fa6 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 re the journal f
13fa7 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c 6c 79 ile has actually
13fa8 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 74 been created, t
13fa9 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 hen call. **
13faa 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 pager_incr_chang
13fab 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70 ecounter() to up
13fac 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
13fad 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 69 72 counter in indir
13fae 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e ect. ** mode.
13faf 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 . **. **
13fb0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
13fb1 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
13fb2 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61 s both enabled a
13fb3 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20 nd applicable,.
13fb4 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 ** then call
13fb5 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 pager_incr_chang
13fb6 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70 ecounter() to up
13fb7 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
13fb8 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 69 counter. ** i
13fb9 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 65 2e n 'direct' mode.
13fba 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
13fbb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
13fbc 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 20 20 ill never be.
13fbd 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f 72 20 ** created for
13fbe 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13fbf 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 .. */.#ifdef
13fc0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
13fc1 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 50 OMIC_WRITE. P
13fc2 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 61 gHdr *pPg;. a
13fc3 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
13fc4 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
13fc5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13fc6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13fc7 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20 MODE_OFF );.
13fc8 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 26 20 if( !zMaster &&
13fc9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13fca 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 fd) . && pPa
13fcb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
13fcc 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 =jrnlBufferSize(
13fcd 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 26 26 pPager) . &&
13fce 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e pPager->dbSize>
13fcf 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 =pPager->dbFileS
13fd0 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 3d 3d ize. && (0==
13fd1 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 (pPg = sqlite3Pc
13fd2 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 acheDirtyList(pP
13fd3 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 20 ager->pPCache))
13fd4 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 72 74 || 0==pPg->pDirt
13fd5 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 y). ){.
13fd6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62 /* Update the db
13fd7 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 file change cou
13fd8 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 69 72 nter via the dir
13fd9 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 6f 64 ect-write method
13fda 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 . The . **
13fdb 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 77 following call w
13fdc 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 69 ill modify the i
13fdd 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 n-memory represe
13fde 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20 ntation of page
13fdf 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 69 1 . ** to i
13fe0 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74 nclude the updat
13fe1 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 ed change counte
13fe2 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 65 r and then write
13fe3 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a page 1 . *
13fe4 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 * directly to th
13fe5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13fe6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 Because of the
13fe7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a 20 20 atomic-write .
13fe8 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20 ** property
13fe9 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 of the host file
13fea 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73 -system, this is
13feb 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a safe.. */.
13fec 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
13fed 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
13fee 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a ter(pPager, 1);.
13fef 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13ff0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 rc = sqlite3Jou
13ff1 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 67 65 rnalCreate(pPage
13ff2 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 r->jfd);. i
13ff3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13ff4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
13ff5 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
13ff6 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 gecounter(pPager
13ff7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
13ff8 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 }.#else. rc
13ff9 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
13ffa 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
13ffb 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 er, 0);.#endif.
13ffc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13ffd 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d E_OK ) goto comm
13ffe 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 it_phase_one_exi
13fff 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 t;.. /* If th
14000 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 is transaction h
14001 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 74 61 as made the data
14002 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 74 68 base smaller, th
14003 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 20 en all pages.
14004 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 61 72 ** being discar
14005 64 65 64 20 62 79 20 74 68 65 20 74 72 75 6e 63 ded by the trunc
14006 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 77 72 ation must be wr
14007 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
14008 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 rnal. ** file
14009 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 . This can only
1400a 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 happen in auto-v
1400b 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 20 20 acuum mode..
1400c 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f 72 65 **. ** Before
1400d 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 61 67 reading the pag
1400e 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d es with page num
1400f 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 61 6e bers larger than
14010 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 the . ** cur
14011 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 rent value of Pa
14012 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 74 20 ger.dbSize, set
14013 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 dbSize back to t
14014 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 he value. **
14015 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 74 20 that it took at
14016 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
14017 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 transaction. Ot
14018 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 20 20 herwise, the.
14019 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ** calls to sql
1401a 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 72 ite3PagerGet() r
1401b 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 eturn zeroed pag
1401c 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20 es instead of .
1401d 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61 ** reading da
1401e 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ta from the data
1401f 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a base file.. *
14020 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 6a 6f *. ** When jo
14021 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46 46 20 urnal_mode==OFF
14022 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20 69 the dbOrigSize i
14023 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c 20 73 s always zero, s
14024 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 62 6c o this. ** bl
14025 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73 20 69 ock never runs i
14026 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f f journal_mode=O
14027 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 FF.. */.#ifnd
14028 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
14029 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
1402a 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
1402b 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 <pPager->dbOrigS
1402c 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41 4c 57 ize . && ALW
1402d 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 AYS(pPager->jour
1402e 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
1402f 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a OURNALMODE_OFF).
14030 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67 6e ){. Pgn
14031 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 o i;
14032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14033 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
14034 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
14035 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 const Pgno
14036 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a iSkip = PAGER_MJ
14037 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20 2f _PGNO(pPager); /
14038 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 * Pending lock p
14039 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e age */. con
1403a 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d st Pgno dbSize =
1403b 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b pPager->dbSize;
1403c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
1403d 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f se image size */
1403e 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
1403f 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d dbSize = pPager-
14040 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20 >dbOrigSize;.
14041 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a 65 for( i=dbSize
14042 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 64 +1; i<=pPager->d
14043 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 29 bOrigSize; i++ )
14044 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 {. if( !s
14045 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
14046 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
14047 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53 nal, i) && i!=iS
14048 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 kip ){.
14049 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20 20 PgHdr *pPage;
1404a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
1404b 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f ge to journal */
1404c 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1404d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
1404e 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 67 pPager, i, &pPag
1404f 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 e);. if
14050 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
14051 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
14052 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
14053 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
14054 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
14055 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
14056 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
14057 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
14058 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
14059 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
1405a 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
1405b 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 xit;. }.
1405c 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70 50 } . pP
1405d 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 64 ager->dbSize = d
1405e 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65 6e bSize;. }.#en
1405f 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 dif.. /* Writ
14060 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
14061 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 rnal name into t
14062 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
14063 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20 20 If a master .
14064 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
14065 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61 e name has alrea
14066 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 dy been written
14067 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
14068 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72 20 ile, . ** or
14069 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e 55 if zMaster is NU
1406a 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a 6f LL (no master jo
1406b 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 69 urnal), then thi
1406c 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
1406d 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 p.. */. rc
1406e 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f = writeMasterJo
1406f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d urnal(pPager, zM
14070 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 aster);. if(
14071 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
14072 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
14073 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 e_one_exit;..
14074 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 /* Sync the jou
14075 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 rnal file. If th
14076 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 e atomic-update
14077 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 optimization is
14078 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65 being. ** use
14079 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69 6c d, this call wil
1407a 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68 65 l not create the
1407b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 journal file or
1407c 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20 20 perform any.
1407d 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20 20 ** real IO..
1407e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79 6e */. rc = syn
1407f 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 cJournal(pPager)
14080 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
14081 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
14082 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
14083 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 exit;.. /* Wr
14084 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 ite all dirty pa
14085 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 ges to the datab
14086 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 ase file. */.
14087 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 rc = pager_writ
14088 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69 74 e_pagelist(sqlit
14089 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
1408a 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
1408b 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 e));. if( rc!
1408c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1408d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
1408e 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
1408f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67 6f CKED );. go
14090 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f to commit_phase_
14091 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a one_exit;. }.
14092 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
14093 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 eCleanAll(pPager
14094 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 20 ->pPCache);..
14095 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 /* If the file
14096 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 on disk is not t
14097 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 he same size as
14098 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
14099 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 ge,. ** then
1409a 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61 use pager_trunca
1409b 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68 te to grow or sh
1409c 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65 rink the file he
1409d 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 re.. */. i
1409e 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a f( pPager->dbSiz
1409f 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c e!=pPager->dbFil
140a0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 50 eSize ){. P
140a1 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67 65 gno nNew = pPage
140a2 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50 61 r->dbSize - (pPa
140a3 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41 47 ger->dbSize==PAG
140a4 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
140a5 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 r));. asser
140a6 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
140a7 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
140a8 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 E );. rc =
140a9 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 pager_truncate(p
140aa 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 Pager, nNew);.
140ab 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
140ac 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
140ad 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
140ae 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f it;. }.. /
140af 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 20 * Finally, sync
140b0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
140b1 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 e. */. if( !p
140b2 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 Pager->noSync &&
140b3 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 !noSync ){.
140b4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
140b5 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c Sync(pPager->fd,
140b6 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
140b7 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ags);. }.
140b8 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43 IOTRACE(("DBSYNC
140b9 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
140ba 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 .. pPager->st
140bb 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43 ate = PAGER_SYNC
140bc 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f ED;. }..commit_
140bd 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a phase_one_exit:.
140be 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
140bf 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 ./*.** When this
140c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
140c1 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62 61 73 led, the databas
140c2 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 e file has been
140c3 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 completely.** up
140c4 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 dated to reflect
140c5 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 the changes mad
140c6 65 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 e by the current
140c7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
140c8 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 .** synced to di
140c9 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 sk. The journal
140ca 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69 73 74 file still exist
140cb 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 s in the file-sy
140cc 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 68 2c stem .** though,
140cd 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c 75 72 and if a failur
140ce 65 20 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 e occurs at this
140cf 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c 20 65 point it will e
140d0 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 ventually.** be
140d1 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f used as a hot-jo
140d2 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 63 75 urnal and the cu
140d3 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
140d4 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a n rolled back..*
140d5 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
140d6 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 on finalizes the
140d7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 65 journal file, e
140d8 69 74 68 65 72 20 62 79 20 64 65 6c 65 74 69 6e ither by deletin
140d9 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6e g, .** truncatin
140da 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 7a g or partially z
140db 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20 74 68 eroing it, so th
140dc 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 at it cannot be
140dd 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 used .** for hot
140de 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
140df 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 k. Once this is
140e0 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73 61 63 done the transac
140e1 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 65 76 tion is.** irrev
140e2 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74 65 64 ocably committed
140e3 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 ..**.** If an er
140e4 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 ror occurs, an I
140e5 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
140e6 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
140e7 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 pager.** moves
140e8 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 into the error s
140e9 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c tate. Otherwise,
140ea 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
140eb 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
140ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
140ed 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
140ee 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 20 2a PhaseTwo(Pager *
140ef 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
140f0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
140f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140f2 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
140f3 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 /.. /* This rou
140f4 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 tine should not
140f5 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61 20 70 be called if a p
140f6 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73 20 6f rior error has o
140f7 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20 42 75 ccurred.. ** Bu
140f8 74 20 69 66 20 28 64 75 65 20 74 6f 20 61 20 63 t if (due to a c
140f9 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c 73 65 oding error else
140fa 77 68 65 72 65 20 69 6e 20 74 68 65 20 73 79 73 where in the sys
140fb 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67 65 74 tem) it does get
140fc 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20 6a 75 . ** called, ju
140fd 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 st return the sa
140fe 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77 69 me error code wi
140ff 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 thout doing anyt
14100 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 4e hing. */. if( N
14101 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 EVER(pPager->err
14102 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 Code) ) return p
14103 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
14104 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 . /* This funct
14105 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 ion should not b
14106 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 e called if the
14107 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 pager is not in
14108 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20 50 41 at least. ** PA
14109 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 GER_RESERVED sta
1410a 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64 20 53 te. And indeed S
1410b 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f 65 73 QLite never does
1410c 20 74 68 69 73 2e 20 42 75 74 20 69 74 20 69 73 this. But it is
1410d 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20 68 61 . ** nice to ha
1410e 76 65 20 74 68 69 73 20 64 65 66 65 6e 73 69 76 ve this defensiv
1410f 65 20 74 65 73 74 20 68 65 72 65 20 61 6e 79 77 e test here anyw
14110 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e ay.. */. if( N
14111 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73 74 61 EVER(pPager->sta
14112 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 te<PAGER_RESERVE
14113 44 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 D) ) return SQLI
14114 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 TE_ERROR;.. /*
14115 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e An optimization.
14116 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
14117 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c was not actuall
14118 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e y modified durin
14119 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e g. ** this tran
1411a 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 61 67 saction, the pag
1411b 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e er is running in
1411c 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 exclusive-mode
1411d 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 69 6e and is. ** usin
1411e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 g persistent jou
1411f 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 69 73 rnals, then this
14120 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
14121 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 o-op.. **. **
14122 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 The start of the
14123 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 journal file cu
14124 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 rrently contains
14125 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 a single journa
14126 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 l . ** header w
14127 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 69 65 ith the nRec fie
14128 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 ld set to 0. If
14129 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c 20 69 such a journal i
1412a 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a 20 61 s used as. ** a
1412b 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 75 72 hot-journal dur
1412c 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ing hot-journal
1412d 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 61 6e rollback, 0 chan
1412e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 ges will be made
1412f 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 . ** to the dat
14130 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20 74 abase file. So t
14131 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
14132 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75 72 to zero the jour
14133 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 nal . ** header
14134 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 . Since the page
14135 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 r is in exclusiv
14136 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 69 73 e mode, there is
14137 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f no need. ** to
14138 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b 73 20 drop any locks
14139 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 either.. */. i
1413a 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 f( pPager->dbMod
1413b 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 61 67 ified==0 && pPag
1413c 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
1413d 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 72 2d e . && pPager-
1413e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
1413f 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
14140 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 20 20 PERSIST. ){.
14141 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14142 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 >journalOff==JOU
14143 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
14144 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 er) );. retur
14145 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
14146 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 .. PAGERTRACE((
14147 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 "COMMIT %d\n", P
14148 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
14149 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
1414a 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
1414b 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d 44 42 _SYNCED || MEMDB
1414c 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 62 4d || !pPager->dbM
1414d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 63 20 odified );. rc
1414e 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e = pager_end_tran
1414f 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 saction(pPager,
14150 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
14151 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 61 67 r);. return pag
14152 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
14153 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rc);.}../*.** R
14154 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e ollback all chan
14155 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 61 73 ges. The databas
14156 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 e falls back to
14157 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f 64 PAGER_SHARED mod
14158 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 e..**.** This fu
14159 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
1415a 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a two tasks:.**.**
1415b 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 20 62 1) It rolls b
1415c 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ack the journal
1415d 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e 67 20 file, restoring
1415e 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c all database fil
1415f 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 69 e and .** i
14160 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70 n-memory cache p
14161 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 61 74 ages to the stat
14162 65 20 74 68 65 79 20 77 65 72 65 20 69 6e 20 77 e they were in w
14163 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 hen the transact
14164 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 73 20 ion.** was
14165 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 opened, and.**
14166 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 65 73 2) It finalizes
14167 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
14168 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 e, so that it is
14169 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 68 6f not used for ho
1416a 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 t.** rollba
1416b 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 ck at any point
1416c 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a in the future..*
1416d 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 6f 20 *.** subject to
1416e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 the following qu
1416f 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a alifications:.**
14170 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a 6f 75 .** * If the jou
14171 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
14172 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e 20 74 yet open when t
14173 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
14174 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 68 65 called,.** the
14175 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 70 65 n only (2) is pe
14176 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 69 73 rformed. In this
14177 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e case there is n
14178 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a o journal file.*
14179 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b * to roll back
1417a 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 6e 20 ..**.** * If in
1417b 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f an error state o
1417c 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
1417d 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 73 6b _FULL, then task
1417e 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 70 65 (1) is .** pe
1417f 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 63 63 rformed. If succ
14180 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 32 29 essful, task (2)
14181 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 . Regardless of
14182 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20 the outcome.**
14183 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 65 20 of either, the
14184 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f error state erro
14185 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
14186 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
14187 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 74 68 .** (i.e. eith
14188 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 er SQLITE_IOERR
14189 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 or SQLITE_CORRUP
1418a 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 T)..**.** * If t
1418b 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 he pager is in P
1418c 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 AGER_RESERVED st
1418d 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 ate, then attemp
1418e 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 0a 2a t (1). Whether.*
1418f 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 20 69 * or not (1) i
14190 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 61 6c s succussful, al
14191 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 2e 20 so attempt (2).
14192 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
14193 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c 49 54 eturn.** SQLIT
14194 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c E_OK. Otherwise,
14195 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
14196 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 state and retur
14197 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 n the first .**
14198 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 6e 63 error code enc
14199 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a ountered. .**.**
1419a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 In this case
1419b 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e there is no chan
1419c 63 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ce that the data
1419d 62 61 73 65 20 77 61 73 20 77 72 69 74 74 65 6e base was written
1419e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 69 73 to. .** So is
1419f 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a safe to finaliz
141a0 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
141a1 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 70 le even if the p
141a2 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 28 6f layback .** (o
141a3 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 69 6c peration 1) fail
141a4 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 ed. However the
141a5 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 65 72 pager must enter
141a6 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
141a7 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 6f 6e .** as the con
141a8 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 2d tents of the in-
141a9 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 72 65 memory cache are
141aa 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a 2a 2a now suspect..**
141ab 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c 20 69 .** * Finally, i
141ac 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 f in PAGER_EXCLU
141ad 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e SIVE state, then
141ae 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 4f 6e attempt (1). On
141af 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 74 20 ly.** attempt
141b0 28 32 29 20 69 66 20 28 31 29 20 69 73 20 73 75 (2) if (1) is su
141b1 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 72 6e ccessful. Return
141b2 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 SQLITE_OK if su
141b3 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f ccessful,.** o
141b4 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 20 74 therwise enter t
141b5 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 he error state a
141b6 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 nd return the er
141b7 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ror code from th
141b8 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e 67 20 e .** failing
141b9 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a operation..**.**
141ba 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 In this case
141bb 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
141bc 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 e may have been
141bd 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f 20 69 written to. So i
141be 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 79 62 f the.** playb
141bf 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 64 69 ack operation di
141c0 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 69 74 d not succeed it
141c1 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 would not be sa
141c2 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a fe to finalize.*
141c3 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 * the journal
141c4 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 20 74 file. It needs t
141c5 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 o be left in the
141c6 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 6f 20 file-system so
141c7 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f that.** some o
141c8 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e ther process can
141c9 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 74 6f use it to resto
141ca 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
141cb 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 20 68 state (by.** h
141cc 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
141cd 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ack)..*/.SQLITE_
141ce 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
141cf 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
141d0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
141d1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
141d2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
141d3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
141d4 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45 n code */. PAGE
141d5 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43 RTRACE(("ROLLBAC
141d6 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 K %d\n", PAGERID
141d7 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 69 66 (pPager)));. if
141d8 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 ( !pPager->dbMod
141d9 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 65 6e ified || !isOpen
141da 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
141db 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
141dc 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
141dd 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
141de 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65 setMaster);. }e
141df 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e lse if( pPager->
141e0 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 errCode && pPage
141e1 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 r->errCode!=SQLI
141e2 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69 TE_FULL ){. i
141e3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
141e4 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 >=PAGER_EXCLUSIV
141e5 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 E ){. pager
141e6 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
141e7 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
141e8 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 rc = pPager->err
141e9 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Code;. }else{.
141ea 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
141eb 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
141ec 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e RVED ){. in
141ed 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 t rc2;. rc
141ee 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
141ef 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
141f0 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 rc2 = pager_e
141f1 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
141f2 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 Pager, pPager->s
141f3 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 etMaster);.
141f4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
141f5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
141f6 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a = rc2;. }.
141f7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
141f8 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
141f9 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b back(pPager, 0);
141fa 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
141fb 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 !MEMDB ){.
141fc 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
141fd 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a lid = 0;. }..
141fe 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 /* If an err
141ff 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
14200 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 a ROLLBACK, we
14201 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 can no longer tr
14202 75 73 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 ust the pager.
14203 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63 ** cache. So c
14204 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 all pager_error(
14205 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74 ) on the way out
14206 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72 to make any err
14207 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69 or . ** persi
14208 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 stent.. */.
14209 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
1420a 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
1420b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1420c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1420d 20 54 52 55 45 20 69 66 20 74 68 65 20 64 61 74 TRUE if the dat
1420e 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 abase file is op
1420f 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 ened read-only.
14210 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a Return FALSE.**
14211 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
14212 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20 is (in theory)
14213 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c writable..*/.SQL
14214 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
14215 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 qlite3PagerIsrea
14216 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61 donly(Pager *pPa
14217 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ger){. return p
14218 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b Pager->readOnly;
14219 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1421a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
1421b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
1421c 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 pager..*/.SQLIT
1421d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1421e 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
1421f 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
14220 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
14221 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
14222 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
14223 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e);.}../*.** Ret
14224 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
14225 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
14226 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 the specified pa
14227 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ge..*/.SQLITE_PR
14228 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14229 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 3PagerPageRefcou
1422a 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 nt(DbPage *pPage
1422b 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1422c 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 te3PcachePageRef
1422d 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a count(pPage);.}.
1422e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1422f 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 EST./*.** This r
14230 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 outine is used f
14231 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 or testing and a
14232 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f nalysis only..*/
14233 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14234 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 int *sqlite3Page
14235 72 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50 rStats(Pager *pP
14236 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20 ager){. static
14237 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 int a[11];. a[0
14238 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 ] = sqlite3Pcach
14239 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
1423a 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b ->pPCache);. a[
1423b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 1] = sqlite3Pcac
1423c 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 hePagecount(pPag
1423d 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
1423e 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 a[2] = sqlite3Pc
1423f 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 acheGetCachesize
14240 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
14241 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67 );. a[3] = pPag
14242 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
14243 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e ? (int) pPager->
14244 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 dbSize : -1;. a
14245 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 [4] = pPager->st
14246 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50 ate;. a[5] = pP
14247 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
14248 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[6] = pPager->
14249 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 nHit;. a[7] = p
1424a 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 Pager->nMiss;.
1424b 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73 a[8] = 0; /* Us
1424c 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d ed to be pPager-
1424d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d >nOvfl */. a[9]
1424e 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 = pPager->nRead
1424f 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67 ;. a[10] = pPag
14250 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65 er->nWrite;. re
14251 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 69 66 turn a;.}.#endif
14252 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
14253 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
14254 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 n in-memory page
14255 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
14256 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14257 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 PagerIsMemdb(Pag
14258 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
14259 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a eturn MEMDB;.}..
1425a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
1425b 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65 there are at le
1425c 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73 ast nSavepoint s
1425d 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 avepoints open.
1425e 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 If there are.**
1425f 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74 currently less t
14260 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20 han nSavepoints
14261 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20 open, then open
14262 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 one or more save
14263 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b points.** to mak
14264 65 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65 e up the differe
14265 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62 nce. If the numb
14266 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
14267 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 is already.** e
14268 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69 qual to nSavepoi
14269 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 nt, then this fu
1426a 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
1426b 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 p..**.** If a me
1426c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1426d 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f fails, SQLITE_NO
1426e 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e MEM is returned.
1426f 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a If an error .**
14270 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 occurs while op
14271 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f ening the sub-jo
14272 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e urnal file, then
14273 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
14274 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 e is.** returned
14275 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
14276 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
14277 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14278 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
14279 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
1427a 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 Pager, int nSave
1427b 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
1427c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
1427d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1427e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1427f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 de */. int nCur
14280 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e rent = pPager->n
14281 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 Savepoint;
14282 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d /* Current num
14283 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 ber of savepoint
14284 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 61 76 s */.. if( nSav
14285 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e 74 20 epoint>nCurrent
14286 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f && pPager->useJo
14287 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 urnal ){. int
14288 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
14289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1428a 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
1428b 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 variable */.
1428c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
1428d 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 *aNew;
1428e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 /* New
1428f 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
14290 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 20 2f array */.. /
14291 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 20 69 * Either there i
14292 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f 75 72 s no active jour
14293 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 2d 6a nal or the sub-j
14294 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 6f ournal is open o
14295 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f r . ** the jo
14296 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 73 20 urnal is always
14297 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
14298 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
14299 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
1429a 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 6e 28 nt==0 || isOpen(
1429b 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c pPager->sjfd) ||
1429c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 . pPa
1429d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
1429e 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
1429f 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 ODE_MEMORY );..
142a0 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 50 /* Grow the P
142a1 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 ager.aSavepoint
142a2 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 61 6c array using real
142a3 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 53 51 loc(). Return SQ
142a4 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a LITE_NOMEM. *
142a5 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 * if the allocat
142a6 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 65 72 ion fails. Other
142a7 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 20 6e wise, zero the n
142a8 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 63 61 ew portion in ca
142a9 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d 61 6c se a . ** mal
142aa 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 loc failure occu
142ab 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c 61 74 rs while populat
142ac 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 66 6f ing it in the fo
142ad 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f r(...) loop belo
142ae 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 4e w.. */. aN
142af 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 65 70 ew = (PagerSavep
142b0 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 52 65 oint *)sqlite3Re
142b1 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 70 alloc(. p
142b2 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
142b3 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53 t, sizeof(PagerS
142b4 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 65 70 avepoint)*nSavep
142b5 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 oint. );.
142b6 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20 if( !aNew ){.
142b7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
142b8 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
142b9 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 5b 6e memset(&aNew[n
142ba 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 6e 53 Current], 0, (nS
142bb 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 65 6e avepoint-nCurren
142bc 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 67 65 t) * sizeof(Page
142bd 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a 20 20 rSavepoint));.
142be 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
142bf 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20 oint = aNew;.
142c0 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
142c1 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 6e 74 int = nSavepoint
142c2 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 ;.. /* Popula
142c3 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65 te the PagerSave
142c4 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73 point structures
142c5 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e just allocated.
142c6 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e */. for(ii=n
142c7 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76 Current; ii<nSav
142c8 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
142c9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
142ca 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
142cb 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 );. aNew[i
142cc 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 67 65 i].nOrig = pPage
142cd 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 r->dbSize;.
142ce 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
142cf 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c 57 41 er->jfd) && ALWA
142d0 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e YS(pPager->journ
142d1 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20 20 20 alOff>0) ){.
142d2 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 aNew[ii].iOf
142d3 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a fset = pPager->j
142d4 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 ournalOff;.
142d5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
142d6 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 aNew[ii].iOffset
142d7 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 = JOURNAL_HDR_S
142d8 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 Z(pPager);.
142d9 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 }. aNew[ii
142da 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 ].iSubRec = pPag
142db 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 er->nSubRec;.
142dc 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 aNew[ii].pInS
142dd 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 avepoint = sqlit
142de 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 e3BitvecCreate(p
142df 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a Pager->dbSize);.
142e0 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 5b if( !aNew[
142e1 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 ii].pInSavepoint
142e2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
142e3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
142e4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
142e5 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 /* Open the
142e6 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 sub-journal, if
142e7 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 it is not alread
142e8 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 y opened. */.
142e9 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 rc = openSubJou
142ea 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
142eb 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 assertTruncate
142ec 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 Constraint(pPage
142ed 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 r);. }.. retur
142ee 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
142ef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
142f0 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c 62 61 called to rollba
142f1 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 28 63 ck or release (c
142f2 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 6f 69 ommit) a savepoi
142f3 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 nt..** The savep
142f4 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 65 20 oint to release
142f5 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 or rollback need
142f6 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f 73 74 not be the most
142f7 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 63 72 recently .** cr
142f8 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e eated savepoint.
142f9 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 .**.** Parameter
142fa 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 65 69 op is always ei
142fb 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 ther SAVEPOINT_R
142fc 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 45 50 OLLBACK or SAVEP
142fd 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a 2a 2a OINT_RELEASE..**
142fe 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f If it is SAVEPO
142ff 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 INT_RELEASE, the
14300 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 64 65 n release and de
14301 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f stroy the savepo
14302 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e 64 65 int with.** inde
14303 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 49 66 x iSavepoint. If
14304 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 it is SAVEPOINT
14305 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 _ROLLBACK, then
14306 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 rollback all cha
14307 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76 nges.** that hav
14308 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 e occurred since
14309 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 73 the specified s
1430a 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 avepoint was cre
1430b 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ated..**.** The
1430c 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 6f 6c savepoint to rol
1430d 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 lback or release
1430e 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 is identified b
1430f 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a 2a 20 y parameter .**
14310 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 76 61 iSavepoint. A va
14311 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74 lue of 0 means t
14312 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65 o operate on the
14313 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 outermost savep
14314 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 69 72 oint.** (the fir
14315 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 20 76 st created). A v
14316 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 2e 6e alue of (Pager.n
14317 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d 65 61 Savepoint-1) mea
14318 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 6f 6e ns operate.** on
14319 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1431a 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 65 70 ly created savep
1431b 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 70 6f oint. If iSavepo
1431c 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74 int is greater t
1431d 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e 6e 53 han.** (Pager.nS
1431e 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 68 65 avepoint-1), the
1431f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
14320 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
14321 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 65 20 * If a negative
14322 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 value is passed
14323 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
14324 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 , then the curre
14325 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
14326 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
14327 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 . This is differ
14328 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 20 0a ent to calling .
14329 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 ** sqlite3PagerR
1432a 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 75 73 ollback() becaus
1432b 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
1432c 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 6e 61 does not termina
1432d 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 te.** the transa
1432e 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 6b 20 ction or unlock
1432f 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 74 the database, it
14330 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 20 74 just restores t
14331 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 he .** contents
14332 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
14333 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
14334 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 6e state. .**.** In
14335 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c 20 73 any case, all s
14336 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 20 61 avepoints with a
14337 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 n index greater
14338 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e 74 20 than iSavepoint
14339 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f 79 65 .** are destroye
1433a 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 20 d. If this is a
1433b 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 69 6f release operatio
1433c 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 n (op==SAVEPOINT
1433d 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 74 68 _RELEASE),.** th
1433e 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 53 61 en savepoint iSa
1433f 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 6f 20 vepoint is also
14340 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a destroyed..**.**
14341 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
14342 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
14343 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 6d 6f _NOMEM if a memo
14344 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
14345 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 49 4f ils,.** or an IO
14346 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 error code if a
14347 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
14348 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 20 s while rolling
14349 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 65 70 back a .** savep
1434a 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f oint. If no erro
1434b 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 rs occur, SQLITE
1434c 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
1434d 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/ .SQLITE_PRIV
1434e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1434f 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 50 61 agerSavepoint(Pa
14350 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
14351 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
14352 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
14353 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
14354 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 ssert( op==SAVEP
14355 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 OINT_RELEASE ||
14356 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f op==SAVEPOINT_RO
14357 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73 73 65 LLBACK );. asse
14358 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d rt( iSavepoint>=
14359 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 0 || op==SAVEPOI
1435a 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 0a NT_ROLLBACK );..
1435b 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 6e 74 if( iSavepoint
1435c 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
1435d 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 int ){. int i
1435e 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a i; /*
1435f 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
14360 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4e le */. int nN
14361 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ew; /*
14362 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61 69 6e Number of remain
14363 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ing savepoints a
14364 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20 2a 2f fter this op. */
14365 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 .. /* Figure
14366 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73 61 76 out how many sav
14367 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73 74 69 epoints will sti
14368 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61 66 74 ll be active aft
14369 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 6f er this. ** o
1436a 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72 65 20 peration. Store
1436b 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20 6e 4e this value in nN
1436c 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20 72 65 ew. Then free re
1436d 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 sources associat
1436e 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 ed . ** with
1436f 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74 any savepoints t
14370 68 61 74 20 61 72 65 20 64 65 73 74 72 6f 79 65 hat are destroye
14371 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 d by this operat
14372 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
14373 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f 69 6e nNew = iSavepoin
14374 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 t + (op==SAVEPOI
14375 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20 NT_ROLLBACK);.
14376 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b 20 69 for(ii=nNew; i
14377 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 i<pPager->nSavep
14378 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 oint; ii++){.
14379 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 sqlite3Bitvec
1437a 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
1437b 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 aSavepoint[ii].p
1437c 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 InSavepoint);.
1437d 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
1437e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 4e 65 nSavepoint = nNe
1437f 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 w;.. /* If th
14380 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b is is a rollback
14381 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c 61 79 operation, play
14382 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 66 69 back the specifi
14383 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a 20 20 ed savepoint..
14384 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 ** If this is
14385 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 74 20 a temp-file, it
14386 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 is possible that
14387 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
14388 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 e has. ** not
14389 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 yet been opened
1438a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
1438b 68 65 72 65 20 68 61 76 65 20 62 65 65 6e 20 6e here have been n
1438c 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20 20 o changes to.
1438d 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
1438e 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 70 6c file, so the pl
1438f 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e ayback operation
14390 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e can be skipped.
14391 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
14392 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f op==SAVEPOINT_RO
14393 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70 65 6e LLBACK && isOpen
14394 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
14395 0a 20 20 20 20 20 20 50 61 67 65 72 53 61 76 65 . PagerSave
14396 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e point *pSavepoin
14397 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f 30 3a t = (nNew==0)?0:
14398 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
14399 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20 20 20 int[nNew-1];.
1439a 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 6c 61 rc = pagerPla
1439b 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 70 ybackSavepoint(p
1439c 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f 69 6e Pager, pSavepoin
1439d 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 t);. assert
1439e 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 (rc!=SQLITE_DONE
1439f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 );. }. .
143a0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
143a1 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 20 6f release of the o
143a2 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 utermost savepoi
143a3 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a 20 20 nt, truncate .
143a4 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 ** the sub-jou
143a5 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 79 74 rnal to zero byt
143a6 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 es in size. */.
143a7 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26 if( nNew==0 &
143a8 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f & op==SAVEPOINT_
143a9 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f 70 65 RELEASE && isOpe
143aa 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
143ab 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
143ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
143ad 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
143ae 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
143af 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b Pager->sjfd, 0);
143b0 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e . pPager->n
143b1 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 SubRec = 0;.
143b2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
143b3 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
143b4 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 rn the full path
143b5 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
143b6 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 base file..*/.SQ
143b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
143b8 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
143b9 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 PagerFilename(Pa
143ba 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
143bb 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a return pPager->z
143bc 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
143bd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 56 46 ** Return the VF
143be 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 S structure for
143bf 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 the pager..*/.SQ
143c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
143c1 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a st sqlite3_vfs *
143c2 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 sqlite3PagerVfs(
143c3 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
143c4 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
143c5 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 >pVfs;.}../*.**
143c6 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c 65 20 Return the file
143c7 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 64 handle for the d
143c8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 73 atabase file ass
143c9 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 ociated.** with
143ca 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73 the pager. This
143cb 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 4e 55 might return NU
143cc 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 20 68 LL if the file h
143cd 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 as.** not yet be
143ce 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 en opened..*/.SQ
143cf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
143d0 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 ite3_file *sqlit
143d1 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 e3PagerFile(Page
143d2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
143d3 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 64 3b turn pPager->fd;
143d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
143d5 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 the full pathna
143d6 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 me of the journa
143d7 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 l file..*/.SQLIT
143d8 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
143d9 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 char *sqlite3Pag
143da 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 erJournalname(Pa
143db 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
143dc 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a return pPager->z
143dd 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a Journal;.}../*.*
143de 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
143df 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 20 61 fsync() calls a
143e0 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 re disabled for
143e1 74 68 69 73 20 70 61 67 65 72 2e 20 20 52 65 74 this pager. Ret
143e2 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 urn FALSE.** if
143e3 66 73 79 6e 63 28 29 73 20 61 72 65 20 65 78 65 fsync()s are exe
143e4 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a cuted normally..
143e5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
143e6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
143e7 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 20 2a erNosync(Pager *
143e8 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
143e9 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 n pPager->noSync
143ea 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
143eb 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a TE_HAS_CODEC./*.
143ec 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72 69 65 ** Set or retrie
143ed 76 65 20 74 68 65 20 63 6f 64 65 63 20 66 6f 72 ve the codec for
143ee 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f 0a 73 this pager.*/.s
143ef 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
143f0 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 e3PagerSetCodec(
143f1 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
143f2 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 ,. void *(*xCod
143f3 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c ec)(void*,void*,
143f4 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 Pgno,int),. voi
143f5 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 d (*xCodecSizeCh
143f6 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e ng)(void*,int,in
143f7 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f t),. void (*xCo
143f8 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 2c decFree)(void*),
143f9 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 0a . void *pCodec.
143fa 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ){. if( pPager-
143fb 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 >xCodecFree ) pP
143fc 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 ager->xCodecFree
143fd 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 (pPager->pCodec)
143fe 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 ;. pPager->xCod
143ff 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6d 65 6d ec = pPager->mem
14400 44 62 20 3f 20 30 20 3a 20 78 43 6f 64 65 63 3b Db ? 0 : xCodec;
14401 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 . pPager->xCode
14402 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 cSizeChng = xCod
14403 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 ecSizeChng;. pP
14404 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 ager->xCodecFree
14405 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 = xCodecFree;.
14406 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 pPager->pCodec
14407 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 = pCodec;. page
14408 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 rReportSize(pPag
14409 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f er);.}.static vo
1440a 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
1440b 47 65 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a GetCodec(Pager *
1440c 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
1440d 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 n pPager->pCodec
1440e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
1440f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14410 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
14411 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 Move the page p
14412 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 Pg to location p
14413 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e gno in the file.
14414 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
14415 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 t be no referenc
14416 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 es to the page p
14417 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 reviously locate
14418 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 d at.** pgno (wh
14419 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f ich we call pPgO
1441a 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 ld) though that
1441b 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 page is allowed
1441c 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 to be.** in cach
1441d 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 e. If the page
1441e 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 previously locat
1441f 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f ed at pgno is no
14420 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 t already.** in
14421 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
14422 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 rnal, it is not
14423 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20 put there by by
14424 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
14425 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 .** References t
14426 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72 o the page pPg r
14427 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 emain valid. Upd
14428 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 ating any.** met
14429 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 a-data associate
1442a 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e d with pPg (i.e.
1442b 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 data stored in
1442c 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 the nExtra bytes
1442d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c .** allocated al
1442e 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67 ong with the pag
1442f 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e e) is the respon
14430 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
14431 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 caller..**.** A
14432 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
14433 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 be active when
14434 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
14435 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 called. It used
14436 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 to be.** require
14437 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 d that a stateme
14438 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 nt transaction w
14439 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 as not active, b
1443a 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74 ut this restrict
1443b 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ion.** has been
1443c 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 removed (CREATE
1443d 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d INDEX needs to m
1443e 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 ove a page when
1443f 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 a statement.** t
14440 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
14441 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tive)..**.** If
14442 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d the fourth argum
14443 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 ent, isCommit, i
14444 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
14445 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65 this page is be
14446 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 ing.** moved as
14447 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 part of a databa
14448 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f se reorganizatio
14449 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 n just before th
1444a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a e transaction .*
1444b 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 * is being commi
1444c 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 tted. In this ca
1444d 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e se, it is guaran
1444e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61 teed that the da
1444f 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 tabase page .**
14450 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 pPg refers to wi
14451 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 ll not be writte
14452 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 n to again withi
14453 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 n this transacti
14454 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 on..**.** This f
14455 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
14456 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 rn SQLITE_NOMEM
14457 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 or an IO error c
14458 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a ode if an error.
14459 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 ** occurs. Other
1445a 77 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 wise, it returns
1445b 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
1445c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1445d 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f t sqlite3PagerMo
1445e 76 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 vepage(Pager *pP
1445f 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 ager, DbPage *pP
14460 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e g, Pgno pgno, in
14461 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 t isCommit){. P
14462 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 gHdr *pPgOld;
14463 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
14464 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 he page being ov
14465 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 erwritten. */.
14466 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e Pgno needSyncPgn
14467 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 o = 0; /*
14468 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 Old value of pPg
14469 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 ->pgno, if sync
1446a 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 is required */.
1446b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
1446c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1446d 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
1446e 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b Pgno origPgno;
1446f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14470 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 * The original p
14471 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 age number */..
14472 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 assert( pPg->nR
14473 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e ef>0 );.. /* In
14474 20 6f 72 64 65 72 20 74 6f 20 62 65 20 61 62 6c order to be abl
14475 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 e to rollback, a
14476 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
14477 62 61 73 65 20 6d 75 73 74 20 6a 6f 75 72 6e 61 base must journa
14478 6c 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 l. ** the page
14479 77 65 20 61 72 65 20 6d 6f 76 69 6e 67 20 66 72 we are moving fr
1447a 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d om.. */. if( M
1447b 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20 3d EMDB ){. rc =
1447c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1447d 74 65 28 70 50 67 29 3b 0a 20 20 20 20 69 66 28 te(pPg);. if(
1447e 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1447f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
14480 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f 76 e page being mov
14481 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 ed is dirty and
14482 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 76 has not been sav
14483 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 74 ed by the latest
14484 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2c . ** savepoint,
14485 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 63 then save the c
14486 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
14487 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f of the page into
14488 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a the . ** sub-j
14489 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 73 ournal now. This
1448a 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 is required to
1448b 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c 6f handle the follo
1448c 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a 20 wing scenario:.
1448d 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e **. ** BEGIN
1448e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 ;. ** <jour
1448f 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 6e nal page X, then
14490 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d 65 modify it in me
14491 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 53 mory>. ** S
14492 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 AVEPOINT one;.
14493 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 70 ** <Move p
14494 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 6f age X to locatio
14495 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 4f n Y>. ** RO
14496 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 LLBACK TO one;.
14497 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 65 **. ** If page
14498 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 74 X were not writ
14499 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a ten to the sub-j
1449a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 20 ournal here, it
1449b 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 62 would not. ** b
1449c 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 e possible to re
1449d 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 6e store its conten
1449e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c ts when the "ROL
1449f 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 20 LBACK TO one".
144a0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 72 ** statement wer
144a1 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e 0a e is processed..
144a2 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f 75 **. ** subjou
144a3 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 6e rnalPage() may n
144a4 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 eed to allocate
144a5 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 70 space to store p
144a6 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 20 Pg->pgno into.
144a7 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 ** one or more s
144a8 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 avepoint bitvecs
144a9 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 65 . This is the re
144aa 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ason this functi
144ab 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 75 on. ** may retu
144ac 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e rn SQLITE_NOMEM.
144ad 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d . */. if( pPg-
144ae 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
144af 54 59 0a 20 20 20 26 26 20 73 75 62 6a 52 65 71 TY. && subjReq
144b0 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20 uiresPage(pPg).
144b1 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d && SQLITE_OK!=
144b2 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c (rc = subjournal
144b3 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b 0a Page(pPg)). ){.
144b4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
144b5 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
144b6 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65 20 (("MOVE %d page
144b7 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 29 %d (needSync=%d)
144b8 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 2c moves to %d\n",
144b9 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44 28 . PAGERID(
144ba 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 pPager), pPg->pg
144bb 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 no, (pPg->flags&
144bc 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
144bd 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 ?1:0, pgno));.
144be 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20 25 IOTRACE(("MOVE %
144bf 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 p %d %d\n", pPag
144c0 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 er, pPg->pgno, p
144c1 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20 74 gno)).. /* If t
144c2 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 he journal needs
144c3 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 to be sync()ed
144c4 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67 2d before page pPg-
144c5 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20 62 >pgno can. ** b
144c6 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73 74 e written to, st
144c7 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e ore pPg->pgno in
144c8 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 local variable
144c9 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 needSyncPgno..
144ca 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 69 **. ** If the i
144cb 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 sCommit flag is
144cc 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e 6f set, there is no
144cd 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 need to remembe
144ce 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 r that. ** the
144cf 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f journal needs to
144d0 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 be sync()ed bef
144d1 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 ore database pag
144d2 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a e pPg->pgno . *
144d3 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e * can be written
144d4 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 to. The caller
144d5 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d has already prom
144d6 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 ised not to writ
144d7 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 e to it.. */.
144d8 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 if( (pPg->flags&
144d9 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
144da 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29 7b && !isCommit ){
144db 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67 6e . needSyncPgn
144dc 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 o = pPg->pgno;.
144dd 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49 assert( pageI
144de 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c nJournal(pPg) ||
144df 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 pPg->pgno>pPage
144e0 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b r->dbOrigSize );
144e1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 . assert( pPg
144e2 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
144e3 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 RTY );. asser
144e4 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 t( pPager->needS
144e5 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ync );. }.. /*
144e6 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63 6f If the cache co
144e7 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77 69 ntains a page wi
144e8 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 70 th page-number p
144e9 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a 20 gno, remove it.
144ea 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 73 ** from its has
144eb 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 h chain. Also, i
144ec 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 f the PgHdr.need
144ed 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 6f 72 Sync was set for
144ee 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 6e 6f . ** page pgno
144ef 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d 6f 76 before the 'mov
144f0 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 74 e' operation, it
144f1 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 74 needs to be ret
144f2 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72 20 ained . ** for
144f3 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 20 74 the page moved t
144f4 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 here.. */. pPg
144f5 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 ->flags &= ~PGHD
144f6 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 70 R_NEED_SYNC;. p
144f7 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c 6f PgOld = pager_lo
144f8 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e okup(pPager, pgn
144f9 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 o);. assert( !p
144fa 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64 2d PgOld || pPgOld-
144fb 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 >nRef==1 );. if
144fc 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20 ( pPgOld ){.
144fd 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 pPg->flags |= (p
144fe 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47 48 PgOld->flags&PGH
144ff 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 DR_NEED_SYNC);.
14500 20 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a if( MEMDB ){.
14501 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
14502 64 69 73 63 61 72 64 20 70 61 67 65 73 20 66 72 discard pages fr
14503 6f 6d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 om an in-memory
14504 64 61 74 61 62 61 73 65 20 73 69 6e 63 65 20 77 database since w
14505 65 20 6d 69 67 68 74 0a 20 20 20 20 20 20 2a 2a e might. **
14506 20 6e 65 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 need to rollbac
14507 6b 20 6c 61 74 65 72 2e 20 20 4a 75 73 74 20 6d k later. Just m
14508 6f 76 65 20 74 68 65 20 70 61 67 65 20 6f 75 74 ove the page out
14509 20 6f 66 20 74 68 65 20 77 61 79 2e 20 2a 2f 0a of the way. */.
1450a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1450b 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
1450c 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 d );. sqlit
1450d 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 e3PcacheMove(pPg
1450e 4f 6c 64 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 Old, pPager->dbS
1450f 69 7a 65 2b 31 29 3b 0a 20 20 20 20 7d 65 6c 73 ize+1);. }els
14510 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
14511 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c PcacheDrop(pPgOl
14512 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 d);. }. }..
14513 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50 67 2d origPgno = pPg-
14514 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 >pgno;. sqlite3
14515 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 PcacheMove(pPg,
14516 70 67 6e 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 pgno);. sqlite3
14517 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 PcacheMakeDirty(
14518 70 50 67 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e pPg);. pPager->
14519 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a dbModified = 1;.
1451a 0a 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 . if( needSyncP
1451b 67 6e 6f 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 gno ){. /* If
1451c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 needSyncPgno is
1451d 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
1451e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1451f 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 needs to be .
14520 20 20 2a 2a 20 73 79 6e 63 28 29 65 64 20 62 65 ** sync()ed be
14521 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 fore any data is
14522 20 77 72 69 74 74 65 6e 20 74 6f 20 64 61 74 61 written to data
14523 62 61 73 65 20 66 69 6c 65 20 70 61 67 65 20 6e base file page n
14524 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 eedSyncPgno..
14525 20 2a 2a 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e ** Currently, n
14526 6f 20 73 75 63 68 20 70 61 67 65 20 65 78 69 73 o such page exis
14527 74 73 20 69 6e 20 74 68 65 20 70 61 67 65 2d 63 ts in the page-c
14528 61 63 68 65 20 61 6e 64 20 74 68 65 20 0a 20 20 ache and the .
14529 20 20 2a 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c ** "is journal
1452a 65 64 22 20 62 69 74 76 65 63 20 66 6c 61 67 20 ed" bitvec flag
1452b 68 61 73 20 62 65 65 6e 20 73 65 74 2e 20 54 68 has been set. Th
1452c 69 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 is needs to be r
1452d 65 6d 65 64 69 65 64 20 62 79 0a 20 20 20 20 2a emedied by. *
1452e 2a 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 70 61 * loading the pa
1452f 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 ge into the page
14530 72 2d 63 61 63 68 65 20 61 6e 64 20 73 65 74 74 r-cache and sett
14531 69 6e 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 ing the PgHdr.ne
14532 65 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 edSync . ** f
14533 6c 61 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 lag.. **.
14534 2a 2a 20 49 66 20 74 68 65 20 61 74 74 65 6d 70 ** If the attemp
14535 74 20 74 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 t to load the pa
14536 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 ge into the page
14537 2d 63 61 63 68 65 20 66 61 69 6c 73 2c 20 28 64 -cache fails, (d
14538 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d ue. ** to a m
14539 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 alloc() or IO fa
1453a 69 6c 75 72 65 29 2c 20 63 6c 65 61 72 20 74 68 ilure), clear th
1453b 65 20 62 69 74 20 69 6e 20 74 68 65 20 70 49 6e e bit in the pIn
1453c 4a 6f 75 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a Journal[]. **
1453d 20 61 72 72 61 79 2e 20 4f 74 68 65 72 77 69 73 array. Otherwis
1453e 65 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 69 e, if the page i
1453f 73 20 6c 6f 61 64 65 64 20 61 6e 64 20 77 72 69 s loaded and wri
14540 74 74 65 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 tten again in.
14541 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61 ** this transa
14542 63 74 69 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 ction, it may be
14543 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
14544 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 database file be
14545 66 6f 72 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 fore. ** it i
14546 73 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 s synced into th
14547 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
14548 54 68 69 73 20 77 61 79 2c 20 69 74 20 6d 61 79 This way, it may
14549 20 65 6e 64 20 75 70 20 69 6e 0a 20 20 20 20 2a end up in. *
1454a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
1454b 6c 65 20 74 77 69 63 65 2c 20 62 75 74 20 74 68 le twice, but th
1454c 61 74 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 at is not a prob
1454d 6c 65 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 lem.. **.
1454e 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 50 61 ** The sqlite3Pa
1454f 67 65 72 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 gerGet() call ma
14550 79 20 63 61 75 73 65 20 74 68 65 20 6a 6f 75 72 y cause the jour
14551 6e 61 6c 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 nal to sync. So
14552 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 make. ** sure
14553 20 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 the Pager.needS
14554 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 ync flag is set
14555 74 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 too.. */.
14556 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 PgHdr *pPgHdr;.
14557 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
14558 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 r->needSync );.
14559 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1455a 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 agerGet(pPager,
1455b 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 needSyncPgno, &p
1455c 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 PgHdr);. if(
1455d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1455e 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 53 . if( needS
1455f 79 6e 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d yncPgno<=pPager-
14560 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 >dbOrigSize ){.
14561 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
14562 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
14563 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 !=0 );. s
14564 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 qlite3BitvecClea
14565 72 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 r(pPager->pInJou
14566 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e 63 50 67 rnal, needSyncPg
14567 6e 6f 2c 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 no, pPager->pTmp
14568 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a Space);. }.
14569 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1456a 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
1456b 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b r->needSync = 1;
1456c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
1456d 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 ger->noSync==0 &
1456e 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 & !MEMDB );.
1456f 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d pPgHdr->flags |=
14570 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
14571 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
14572 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
14573 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Hdr);. sqlite
14574 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 3PagerUnref(pPgH
14575 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 dr);. }.. /*.
14576 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 ** For an in-me
14577 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d mory database, m
14578 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69 ake sure the ori
14579 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 ginal page conti
1457a 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 nues. ** to exi
1457b 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 st, in case the
1457c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
1457d 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 s to roll back.
1457e 20 55 73 65 20 70 50 67 4f 6c 64 0a 20 20 2a 2a Use pPgOld. **
1457f 20 61 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c as the original
14580 20 70 61 67 65 20 73 69 6e 63 65 20 69 74 20 68 page since it h
14581 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
14582 61 6c 6c 6f 63 61 74 65 64 2e 0a 20 20 2a 2f 0a allocated.. */.
14583 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 if( MEMDB ){.
14584 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
14585 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 6f 72 69 Move(pPgOld, ori
14586 67 50 67 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 gPgno);. sqli
14587 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
14588 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 gOld);. }.. re
14589 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1458a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1458b 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1458c 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72 to the data for
1458d 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
1458e 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 age..*/.SQLITE_P
1458f 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
14590 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
14591 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
14592 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 assert( pPg->nR
14593 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61 ef>0 || pPg->pPa
14594 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 ger->memDb );.
14595 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74 return pPg->pDat
14596 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 a;.}../*.** Retu
14597 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
14598 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61 the Pager.nExtra
14599 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 61 bytes of "extra
1459a 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f " space .** allo
1459b 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
1459c 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
1459d 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 age..*/.SQLITE_P
1459e 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
1459f 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
145a0 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a a(DbPage *pPg){.
145a1 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45 return pPg->pE
145a2 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 xtra;.}../*.** G
145a3 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 et/set the locki
145a4 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 ng-mode for this
145a5 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 pager. Paramete
145a6 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 r eMode must be
145a7 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f one.** of PAGER_
145a8 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 LOCKINGMODE_QUER
145a9 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 Y, PAGER_LOCKING
145aa 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a MODE_NORMAL or .
145ab 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ** PAGER_LOCKING
145ac 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 MODE_EXCLUSIVE.
145ad 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
145ae 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 is not _QUERY,
145af 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b then.** the lock
145b0 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 ing-mode is set
145b1 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 to the value spe
145b2 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 cified..**.** Th
145b3 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 e returned value
145b4 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52 is either PAGER
145b5 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
145b6 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f MAL or.** PAGER_
145b7 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c LOCKINGMODE_EXCL
145b8 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e USIVE, indicatin
145b9 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 g the current (p
145ba 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 ossibly updated)
145bb 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 .** locking-mode
145bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
145bd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
145be 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 agerLockingMode(
145bf 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
145c0 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 nt eMode){. ass
145c1 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 ert( eMode==PAGE
145c2 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 R_LOCKINGMODE_QU
145c3 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 ERY.
145c4 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
145c5 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d LOCKINGMODE_NORM
145c6 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c AL. |
145c7 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c | eMode==PAGER_L
145c8 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
145c9 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 SIVE );. assert
145ca 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ( PAGER_LOCKINGM
145cb 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 ODE_QUERY<0 );.
145cc 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c assert( PAGER_L
145cd 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
145ce 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f L>=0 && PAGER_LO
145cf 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
145d0 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 IVE>=0 );. if(
145d1 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 eMode>=0 && !pPa
145d2 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
145d3 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 . pPager->exc
145d4 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 lusiveMode = (u8
145d5 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 )eMode;. }. re
145d6 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 turn (int)pPager
145d7 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b ->exclusiveMode;
145d8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
145d9 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f t the journal-mo
145da 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 de for this page
145db 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f r. Parameter eMo
145dc 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f de must be one o
145dd 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 f:.**.** PAGE
145de 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
145df 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ERY.** PAGER_
145e0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 JOURNALMODE_DELE
145e1 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a TE.** PAGER_J
145e2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
145e3 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ATE.** PAGER_
145e4 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
145e5 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f IST.** PAGER_
145e6 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a JOURNALMODE_OFF.
145e7 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
145e8 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a NALMODE_MEMORY.*
145e9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 *.** If the para
145ea 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 meter is not _QU
145eb 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f ERY, then the jo
145ec 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 urnal_mode is se
145ed 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 t to the.** valu
145ee 65 20 73 70 65 63 69 66 69 65 64 20 69 66 20 74 e specified if t
145ef 68 65 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c he change is all
145f0 6f 77 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67 owed. The chang
145f1 65 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a e is disallowed.
145f2 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f ** for the follo
145f3 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a wing reasons:.**
145f4 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d .** * An in-m
145f5 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 63 emory database c
145f6 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 an only have its
145f7 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 journal_mode se
145f8 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 t to _OFF.**
145f9 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a or _MEMORY..**
145fa 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 .** * The jou
145fb 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f rnal mode may no
145fc 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 t be changed whi
145fd 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e le a transaction
145fe 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a is active..**.*
145ff 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 69 * The returned i
14600 6e 64 69 63 61 74 65 20 74 68 65 20 63 75 72 72 ndicate the curr
14601 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 ent (possibly up
14602 64 61 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d dated) journal-m
14603 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ode..*/.SQLITE_P
14604 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14605 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
14606 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 de(Pager *pPager
14607 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 , int eMode){.
14608 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 assert( eMode==P
14609 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
1460a 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 _QUERY.
1460b 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
1460c 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
1460d 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 ELETE.
1460e 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
1460f 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 R_JOURNALMODE_TR
14610 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20 UNCATE.
14611 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
14612 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 ER_JOURNALMODE_P
14613 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 ERSIST.
14614 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
14615 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
14616 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 FF .
14617 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
14618 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
14619 52 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 RY );. assert(
1461a 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
1461b 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 E_QUERY<0 );. i
1461c 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 f( eMode>=0. &
1461d 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f & (!MEMDB || eMo
1461e 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
1461f 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 LMODE_MEMORY .
14620 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
14621 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
14622 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 NALMODE_OFF).
14623 26 26 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f && !pPager->dbMo
14624 64 69 66 69 65 64 0a 20 20 20 26 26 20 28 21 69 dified. && (!i
14625 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
14626 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d d) || 0==pPager-
14627 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 >journalOff). )
14628 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e {. if( isOpen
14629 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
1462a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
1462b 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 Close(pPager->jf
1462c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 d);. }. pP
1462d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
1462e 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 e = (u8)eMode;.
1462f 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 }. return (int
14630 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c )pPager->journal
14631 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Mode;.}../*.** G
14632 65 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d et/set the size-
14633 6c 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 limit used for p
14634 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 ersistent journa
14635 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 l files..**.** S
14636 65 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 etting the size
14637 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e limit to -1 mean
14638 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e s no limit is en
14639 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 forced..** An at
1463a 74 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c tempt to set a l
1463b 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 imit smaller tha
1463c 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e n -1 is a no-op.
1463d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1463e 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 TE i64 sqlite3Pa
1463f 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 gerJournalSizeLi
14640 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 mit(Pager *pPage
14641 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a r, i64 iLimit){.
14642 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 if( iLimit>=-1
14643 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
14644 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
14645 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 = iLimit;. }.
14646 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
14647 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
14648 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
14649 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
1464a 68 65 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b he pPager->pBack
1464b 75 70 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65 up variable. The
1464c 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a backup module.*
1464d 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 * in backup.c ma
1464e 69 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 intains the cont
1464f 65 6e 74 20 6f 66 20 74 68 69 73 20 76 61 72 69 ent of this vari
14650 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c able. This modul
14651 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61 e.** uses it opa
14652 71 75 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75 quely as an argu
14653 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42 ment to sqlite3B
14654 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 61 ackupRestart() a
14655 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 nd.** sqlite3Bac
14656 6b 75 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 kupUpdate() only
14657 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14658 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ATE sqlite3_back
14659 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 up **sqlite3Page
1465a 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 rBackupPtr(Pager
1465b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
1465c 75 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61 urn &pPager->pBa
1465d 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 ckup;.}..#endif
1465e 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 /* SQLITE_OMIT_D
1465f 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a ISKIO */../*****
14660 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
14661 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a pager.c *******
14662 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14664 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
14665 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
14666 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a file btmutex.c *
14667 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14669 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1466a 32 30 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2007 August 27.*
1466b 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1466c 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1466d 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1466e 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1466f 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
14670 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
14671 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
14672 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
14673 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
14674 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
14675 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
14676 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
14677 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
14678 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
14679 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1467a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1467b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1467c 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1467d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1467e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1467f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
14681 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
14682 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 ntains code used
14683 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 to implement mu
14684 74 65 78 65 73 20 6f 6e 20 42 74 72 65 65 20 6f texes on Btree o
14685 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 bjects..** This
14686 63 6f 64 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f code really belo
14687 6e 67 73 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 ngs in btree.c.
14688 20 42 75 74 20 62 74 72 65 65 2e 63 20 69 73 20 But btree.c is
14689 67 65 74 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 getting too.** b
1468a 69 67 20 61 6e 64 20 77 65 20 77 61 6e 74 20 74 ig and we want t
1468b 6f 20 62 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 o break it down
1468c 73 6f 6d 65 2e 20 20 54 68 69 73 20 70 61 63 6b some. This pack
1468d 61 67 65 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 aged seemed like
1468e 0a 2a 2a 20 61 20 67 6f 6f 64 20 62 72 65 61 6b .** a good break
1468f 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a out..*/./*******
14690 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
14691 62 74 72 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 btreeInt.h in th
14692 65 20 6d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 e middle of btmu
14693 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tex.c **********
14694 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
14695 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
14696 6c 65 20 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a le btreeInt.h **
14697 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14698 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14699 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
1469a 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 04 April 6.**.**
1469b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1469c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1469d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1469e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1469f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
146a0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
146a1 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
146a2 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
146a3 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
146a4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
146a5 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
146a6 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
146a7 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
146a8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
146a9 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
146aa 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
146ab 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
146ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
146ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
146ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
146af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
146b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
146b1 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
146b2 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 s a external (di
146b3 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 sk-based) databa
146b4 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e se using BTrees.
146b5 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 6c .** For a detail
146b6 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 ed discussion of
146b7 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 74 BTrees, refer t
146b8 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 o.**.** Dona
146b9 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 45 ld E. Knuth, THE
146ba 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 52 ART OF COMPUTER
146bb 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 6f PROGRAMMING, Vo
146bc 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 22 lume 3:.** "
146bd 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 72 Sorting And Sear
146be 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 37 ching", pages 47
146bf 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 57 3-480. Addison-W
146c0 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 62 esley.** Pub
146c1 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 2c lishing Company,
146c2 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 63 Reading, Massac
146c3 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 husetts..**.** T
146c4 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73 he basic idea is
146c5 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 20 that each page
146c6 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
146c7 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 0a ains N database.
146c8 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 4e ** entries and N
146c9 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 +1 pointers to s
146ca 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 ubpages..**.**
146cb 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
146cc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146cd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146ce 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146cf 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 29 -.** | Ptr(0)
146d0 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 28 | Key(0) | Ptr(
146d1 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e 1) | Key(1) | ..
146d2 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50 . | Key(N-1) | P
146d3 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d tr(N) |.** ---
146d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
146d8 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b .** All of the k
146d9 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 eys on the page
146da 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 6e that Ptr(0) poin
146db 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 65 ts to have value
146dc 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 4b s less.** than K
146dd 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 74 ey(0). All of t
146de 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 20 he keys on page
146df 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 73 Ptr(1) and its s
146e0 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a 20 ubpages have.**
146e1 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 values greater t
146e2 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 6c han Key(0) and l
146e3 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 2e ess than Key(1).
146e4 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 All of the key
146e5 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 61 s.** on Ptr(N) a
146e6 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20 nd its subpages
146e7 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 61 have values grea
146e8 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d 31 ter than Key(N-1
146e9 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f ). And.** so fo
146ea 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 rth..**.** Findi
146eb 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 ng a particular
146ec 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 61 key requires rea
146ed 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 70 ding O(log(M)) p
146ee 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a ages from the .*
146ef 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 69 * disk where M i
146f0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
146f1 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 entries in the t
146f2 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ree..**.** In th
146f3 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
146f4 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 n, a single file
146f5 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 can hold one or
146f6 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 0a more separate .
146f7 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 68 ** BTrees. Each
146f8 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 69 BTree is identi
146f9 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 65 fied by the inde
146fa 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 61 x of its root pa
146fb 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 20 ge. The.** key
146fc 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e 79 and data for any
146fd 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 69 entry are combi
146fe 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 20 ned to form the
146ff 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a "payload". A.**
14700 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 fixed amount of
14701 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 20 payload can be
14702 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c 79 carried directly
14703 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14704 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 68 .** page. If th
14705 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 72 e payload is lar
14706 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 ger than the pre
14707 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20 set amount then
14708 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 73 surplus.** bytes
14709 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f are stored on o
1470a 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 verflow pages.
1470b 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 The payload for
1470c 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 an entry.** and
1470d 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 6f the preceding po
1470e 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 6e inter are combin
1470f 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 65 ed to form a "Ce
14710 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 70 ll". Each .** p
14711 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c 20 age has a small
14712 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f 6e header which con
14713 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e 29 tains the Ptr(N)
14714 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 68 pointer and oth
14715 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f er.** informatio
14716 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 69 n such as the si
14717 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 ze of key and da
14718 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 ta..**.** FORMAT
14719 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 DETAILS.**.** T
1471a 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 64 he file is divid
1471b 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 20 ed into pages.
1471c 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
1471d 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2c s called page 1,
1471e 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69 .** the second i
1471f 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 6f s page 2, and so
14720 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 20 forth. A page
14721 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 69 number of zero i
14722 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 ndicates.** "no
14723 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 65 such page". The
14724 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 62 page size can b
14725 65 20 61 6e 79 20 70 6f 77 65 72 20 6f 66 20 32 e any power of 2
14726 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 between 512 and
14727 20 33 32 37 36 38 2e 0a 2a 2a 20 45 61 63 68 20 32768..** Each
14728 70 61 67 65 20 63 61 6e 20 62 65 20 65 69 74 68 page can be eith
14729 65 72 20 61 20 62 74 72 65 65 20 70 61 67 65 2c er a btree page,
1472a 20 61 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 a freelist page
1472b 2c 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a , an overflow.**
1472c 20 70 61 67 65 2c 20 6f 72 20 61 20 70 6f 69 6e page, or a poin
1472d 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 0a 2a 2a ter-map page..**
1472e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
1472f 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 62 ge is always a b
14730 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 tree page. The
14731 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20 first 100 bytes
14732 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 of the first.**
14733 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 73 page contain a s
14734 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28 74 pecial header (t
14735 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72 22 he "file header"
14736 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 ) that describes
14737 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 the file..** Th
14738 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 e format of the
14739 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 61 file header is a
1473a 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
1473b 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 OFFSET SIZE
1473c 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a DESCRIPTION.
1473d 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 31 ** 0 1
1473e 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74 72 6 Header str
1473f 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f 72 ing: "SQLite for
14740 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20 20 mat 3\000".**
14741 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20 20 16 2
14742 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79 Page size in by
14743 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31 38 tes. .** 18
14744 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c 1 Fil
14745 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20 76 e format write v
14746 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 ersion.** 19
14747 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c 1 Fil
14748 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76 65 e format read ve
14749 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30 20 rsion.** 20
1474a 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74 65 1 Byte
1474b 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 s of unused spac
1474c 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 e at the end of
1474d 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20 20 each page.**
1474e 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20 20 21 1
1474f 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 Max embedded pay
14750 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
14751 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31 20 22 1
14752 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65 64 Min embedded
14753 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
14754 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20 20 n.** 23
14755 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61 66 1 Min leaf
14756 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
14757 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 20 n.** 24
14758 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 61 4 File cha
14759 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 nge counter.**
1475a 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20 20 28 4
1475b 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 Reserved for f
1475c 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 20 uture use.**
1475d 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20 20 32 4
1475e 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 70 First freelist p
1475f 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 20 age.** 36
14760 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 4 Number
14761 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 67 of freelist pag
14762 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a es in the file.*
14763 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36 30 * 40 60
14764 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d 15 4-byte m
14765 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 65 eta values passe
14766 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65 d to higher laye
14767 72 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 34 30 20 rs.**.** 40
14768 20 20 20 20 20 20 34 20 20 20 20 20 53 63 68 65 4 Sche
14769 6d 61 20 63 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 ma cookie.**
1476a 20 34 34 20 20 20 20 20 20 20 34 20 20 20 20 20 44 4
1476b 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73 File format of s
1476c 63 68 65 6d 61 20 6c 61 79 65 72 0a 2a 2a 20 20 chema layer.**
1476d 20 20 20 34 38 20 20 20 20 20 20 20 34 20 20 20 48 4
1476e 20 20 53 69 7a 65 20 6f 66 20 70 61 67 65 20 63 Size of page c
1476f 61 63 68 65 0a 2a 2a 20 20 20 20 20 35 32 20 20 ache.** 52
14770 20 20 20 20 20 34 20 20 20 20 20 4c 61 72 67 65 4 Large
14771 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 28 61 75 st root-page (au
14772 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d 29 0a to/incr_vacuum).
14773 2a 2a 20 20 20 20 20 35 36 20 20 20 20 20 20 20 ** 56
14774 34 20 20 20 20 20 31 3d 55 54 46 2d 38 20 32 3d 4 1=UTF-8 2=
14775 55 54 46 31 36 6c 65 20 33 3d 55 54 46 31 36 62 UTF16le 3=UTF16b
14776 65 0a 2a 2a 20 20 20 20 20 36 30 20 20 20 20 20 e.** 60
14777 20 20 34 20 20 20 20 20 55 73 65 72 20 76 65 72 4 User ver
14778 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 36 34 20 20 sion.** 64
14779 20 20 20 20 20 34 20 20 20 20 20 49 6e 63 72 65 4 Incre
1477a 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f mental vacuum mo
1477b 64 65 0a 2a 2a 20 20 20 20 20 36 38 20 20 20 20 de.** 68
1477c 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a 4 unused.
1477d 2a 2a 20 20 20 20 20 37 32 20 20 20 20 20 20 20 ** 72
1477e 34 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 4 unused.**
1477f 20 20 20 20 37 36 20 20 20 20 20 20 20 34 20 20 76 4
14780 20 20 20 75 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20 unused.**.**
14781 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 All of the integ
14782 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 er values are bi
14783 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 g-endian (most s
14784 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 ignificant byte
14785 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 first)..**.** Th
14786 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f e file change co
14787 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 unter is increme
14788 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 nted when the da
14789 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 tabase is change
1478a 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 d.** This counte
1478b 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 r allows other p
1478c 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 rocesses to know
1478d 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 when the file h
1478e 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e as changed.** an
1478f 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 d thus when they
14790 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 need to flush t
14791 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a heir cache..**.*
14792 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 * The max embedd
14793 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
14794 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e ion is the amoun
14795 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 t of the total u
14796 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 sable.** space i
14797 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61 n a page that ca
14798 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 n be consumed by
14799 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 a single cell f
1479a 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 or standard.** B
1479b 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 -tree (non-LEAFD
1479c 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 ATA) tables. A
1479d 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 value of 255 mea
1479e 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 ns 100%. The de
1479f 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c fault.** is to l
147a0 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d imit the maximum
147a1 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 cell size so th
147a2 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 at at least 4 ce
147a3 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 lls will fit.**
147a4 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 on one page. Th
147a5 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d us the default m
147a6 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c ax embedded payl
147a7 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
147a8 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 64..**.** If the
147a9 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 payload for a c
147aa 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 ell is larger th
147ab 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f an the max paylo
147ac 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a ad, then extra.*
147ad 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 * payload is spi
147ae 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 lled to overflow
147af 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e pages. Once an
147b0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
147b1 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 s allocated,.**
147b2 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 as many bytes as
147b3 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f possible are mo
147b4 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 ved into the ove
147b5 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 rflow pages with
147b6 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 out letting.** t
147b7 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f he cell size dro
147b8 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 p below the min
147b9 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
147ba 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a fraction..**.**
147bb 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 The min leaf pa
147bc 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 yload fraction i
147bd 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 s like the min e
147be 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
147bf 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 fraction.** exce
147c0 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 pt that it appli
147c1 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 es to leaf nodes
147c2 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 in a LEAFDATA t
147c3 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 ree. The maximu
147c4 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 m.** payload fra
147c5 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 ction for a LEAF
147c6 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 DATA tree is alw
147c7 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 ays 100% (or 255
147c8 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 ) and it.** not
147c9 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
147ca 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 header..**.** E
147cb 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20 ach btree pages
147cc 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 is divided into
147cd 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 three sections:
147ce 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 The header, the
147cf 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 .** cell pointer
147d0 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 array, and the
147d1 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
147d2 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 a. Page 1 also
147d3 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a has a 100-byte.*
147d4 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 * file header th
147d5 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 at occurs before
147d6 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
147d7 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d ..**.** |--
147d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
147d9 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 ** | file h
147da 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 eader | 100
147db 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 bytes. Page 1
147dc 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d only..** |-
147dd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
147de 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 .** | page
147df 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 header | 8
147e0 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 bytes for leaves
147e1 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 . 12 bytes for
147e2 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a interior nodes.*
147e3 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
147e4 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
147e5 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 | cell pointer
147e6 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 | | 2 byte
147e7 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 s per cell. Sor
147e8 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 ted order..**
147e9 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 | array
147ea 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 | | Grows
147eb 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 downward.**
147ec 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 |
147ed 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 | v.**
147ee 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
147ef 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e --|.** | un
147f0 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a allocated |.*
147f1 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 * | space
147f2 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 |.**
147f3 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
147f4 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 ---| ^ Grows
147f5 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 upwards.**
147f6 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 | cell content
147f7 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 | | Arbitrar
147f8 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 y order interspe
147f9 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c rsed with freebl
147fa 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 ocks..** |
147fb 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c area |
147fc 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 | and free s
147fd 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a pace fragments..
147fe 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
147ff 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a ---------|.**.**
14800 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72 The page header
14801 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 s looks like thi
14802 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 s:.**.** OFFSE
14803 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 T SIZE DES
14804 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
14805 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20 0 1
14806 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 Flags. 1: intkey
14807 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 , 2: zerodata, 4
14808 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c : leafdata, 8: l
14809 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 eaf.** 1
1480a 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20 2 byte
1480b 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 offset to the fi
1480c 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a rst freeblock.**
1480d 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20 3 2
1480e 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 number of c
1480f 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ells on this pag
14810 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 e.** 5
14811 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62 2 first b
14812 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 yte of the cell
14813 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 content area.**
14814 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20 7 1
14815 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 number of fr
14816 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 agmented free by
14817 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 tes.** 8
14818 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74 4 Right
14819 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 child (the Ptr(
1481a 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 N) value). Omit
1481b 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a ted on leaves..*
1481c 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 *.** The flags d
1481d 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 efine the format
1481e 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70 of this btree p
1481f 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 age. The leaf f
14820 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a lag means that.*
14821 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20 * this page has
14822 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 no children. Th
14823 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 e zerodata flag
14824 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 means that this
14825 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 page carries.**
14826 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f only keys and no
14827 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b data. The intk
14828 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 ey flag means th
14829 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 at the key is a
1482a 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 integer.** which
1482b 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 is stored in th
1482c 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 e key size entry
1482d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 of the cell hea
1482e 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 der rather than
1482f 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 in.** the payloa
14830 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 d area..**.** Th
14831 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
14832 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 rray begins on t
14833 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66 he first byte af
14834 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61 ter the page hea
14835 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c der..** The cell
14836 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 pointer array c
14837 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 ontains zero or
14838 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 more 2-byte numb
14839 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a ers which are.**
1483a 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 offsets from th
1483b 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
1483c 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 he page to the c
1483d 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 ell content in t
1483e 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 he cell.** conte
1483f 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 nt area. The ce
14840 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 ll pointers occu
14841 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 r in sorted orde
14842 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 r. The system s
14843 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 trives.** to kee
14844 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74 p free space aft
14845 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c er the last cell
14846 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 pointer so that
14847 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a new cells can.*
14848 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 * be easily adde
14849 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 d without having
1484a 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 to defragment t
1484b 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 he page..**.** C
1484c 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 ell content is s
1484d 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72 tored at the ver
1484e 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 y end of the pag
1484f 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 e and grows towa
14850 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e rd the.** beginn
14851 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e ing of the page.
14852 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 .**.** Unused sp
14853 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 ace within the c
14854 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
14855 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e is collected in
14856 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 to a linked list
14857 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b of.** freeblock
14858 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f s. Each freeblo
14859 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 ck is at least 4
1485a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 bytes in size.
1485b 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 The byte offset
1485c 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
1485d 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 freeblock is gi
1485e 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 ven in the heade
1485f 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f r. Freeblocks o
14860 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 ccur in.** incre
14861 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 asing order. Be
14862 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 cause a freebloc
14863 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 k must be at lea
14864 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 st 4 bytes in si
14865 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 ze,.** any group
14866 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 of 3 or fewer u
14867 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 nused bytes in t
14868 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
14869 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 area cannot.** e
1486a 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 xist on the free
1486b 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 block chain. A
1486c 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 group of 3 or fe
1486d 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69 wer free bytes i
1486e 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 s called.** a fr
1486f 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 agment. The tot
14870 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
14871 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 es in all fragme
14872 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e nts is recorded.
14873 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 .** in the page
14874 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 header at offset
14875 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 7..**.** SIZ
14876 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e E DESCRIPTION
14877 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
14878 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 yte offset of th
14879 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b e next freeblock
1487a 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 .** 2 B
1487b 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 ytes in this fre
1487c 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c eblock.**.** Cel
1487d 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 ls are of variab
1487e 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c le length. Cell
1487f 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
14880 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
14881 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 area at.** the
14882 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e end of the page.
14883 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 Pointers to th
14884 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 e cells are in t
14885 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
14886 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d array.** that im
14887 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
14888 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 s the page heade
14889 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 r. Cells is not
1488a 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 necessarily.**
1488b 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e contiguous or in
1488c 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c order, but cell
1488d 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f pointers are co
1488e 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 ntiguous and in
1488f 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c order..**.** Cel
14890 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 l content makes
14891 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 use of variable
14892 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e length integers.
14893 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 A variable.**
14894 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 length integer i
14895 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 s 1 to 9 bytes w
14896 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 here the lower 7
14897 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a bits of each .*
14898 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e * byte are used.
14899 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f The integer co
1489a 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 nsists of all by
1489b 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69 tes that have bi
1489c 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 t 8 set and.** t
1489d 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69 he first byte wi
1489e 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 th bit 8 clear.
1489f 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 The most signif
148a0 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 icant byte of th
148a1 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 e integer.** app
148a2 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 ears first. A v
148a3 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
148a4 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 nteger may not b
148a5 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 e more than 9 by
148a6 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 tes long..** As
148a7 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 a special case,
148a8 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 all 8 bytes of t
148a9 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20 he 9th byte are
148aa 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 used as data. T
148ab 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 his.** allows a
148ac 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 64-bit integer t
148ad 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 o be encoded in
148ae 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 9 bytes..**.**
148af 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 0x00
148b0 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
148b1 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a mes 0x00000000.
148b2 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20 ** 0x7f
148b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
148b4 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
148b5 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 07f.** 0x81 0
148b6 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 x00
148b7 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
148b8 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 0000080.** 0x
148b9 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20 82 0x00
148ba 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
148bb 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 0x00000100.**
148bc 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20 0x80 0x7f
148bd 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
148be 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a mes 0x0000007f.
148bf 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20 ** 0x8a 0x91
148c0 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20 0xd1 0xac 0x78
148c1 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 becomes 0x12345
148c2 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 678.** 0x81 0
148c3 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 x81 0x81 0x81 0x
148c4 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 01 becomes 0x1
148c5 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 0204081.**.** Va
148c6 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
148c7 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20 tegers are used
148c8 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 for rowids and t
148c9 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 o hold the numbe
148ca 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 r of.** bytes of
148cb 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e key and data in
148cc 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a a btree cell..*
148cd 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
148ce 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 of a cell looks
148cf 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
148d0 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
148d1 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
148d2 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
148d3 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 er of the left c
148d4 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 hild. Omitted if
148d5 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 leaf flag is se
148d6 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 t..** var
148d7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
148d8 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 of data. Omitte
148d9 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 d if the zerodat
148da 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a a flag is set..*
148db 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d * var Num
148dc 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
148dd 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 key. Or the key
148de 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 itself if intkey
148df 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a flag is set..**
148e0 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c * Payl
148e1 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 oad.** 4
148e2 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 First page of
148e3 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 the overflow cha
148e4 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 in. Omitted if
148e5 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a no overflow.**.*
148e6 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 * Overflow pages
148e7 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c form a linked l
148e8 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 ist. Each page
148e9 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 except the last
148ea 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a is completely.**
148eb 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 filled with dat
148ec 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 a (pagesize - 4
148ed 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 bytes). The las
148ee 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 t page can have
148ef 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 as little.** as
148f0 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 1 byte of data..
148f1 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
148f2 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
148f3 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
148f4 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f number of next o
148f5 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 verflow page.**
148f6 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a * Data.
148f7 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 **.** Freelist p
148f8 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f ages come in two
148f9 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b subtypes: trunk
148fa 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 pages and leaf
148fb 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 pages. The.** f
148fc 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 ile header point
148fd 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 s to the first i
148fe 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
148ff 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 of trunk page.
14900 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 Each trunk.** pa
14901 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c ge points to mul
14902 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 tiple leaf pages
14903 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f . The content o
14904 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 f a leaf page is
14905 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e .** unspecified.
14906 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c A trunk page l
14907 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a ooks like this:.
14908 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
14909 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
1490a 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 4 Page
1490b 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 number of next t
1490c 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 runk page.**
1490d 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 4 Number o
1490e 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 f leaf pointers
1490f 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 on this page.**
14910 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 * zero
14911 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 or more pages nu
14912 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a mbers of leaves.
14913 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c */.../* The foll
14914 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 owing value is t
14915 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 he maximum cell
14916 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 size assuming a
14917 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 maximum page.**
14918 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e size give above.
14919 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 .*/.#define MX_C
1491a 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 ELL_SIZE(pBt) (
1491b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 pBt->pageSize-8)
1491c 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d ../* The maximum
1491d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
1491e 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
1491f 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
14920 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 e. This.** assu
14921 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 mes a minimum ce
14922 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 ll size of 6 byt
14923 65 73 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 es (4 bytes for
14924 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 the cell itself
14925 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 .** plus 2 bytes
14926 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 for the index t
14927 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 o the cell in th
14928 65 20 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 e page header).
14929 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 Such.** small c
1492a 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 ells will be rar
1492b 65 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 20 e, but they are
1492c 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 possible..*/.#de
1492d 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 fine MX_CELL(pBt
1492e 29 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a ) ((pBt->pageSiz
1492f 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 e-8)/6)../* Forw
14930 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
14931 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 */.typedef stru
14932 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 ct MemPage MemPa
14933 67 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ge;.typedef stru
14934 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b ct BtLock BtLock
14935 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 ;../*.** This is
14936 20 61 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 a magic string
14937 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 that appears at
14938 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14939 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 every.** SQLite
1493a 20 64 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 database in ord
1493b 65 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 er to identify t
1493c 68 65 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 he file as a rea
1493d 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a l database..**.*
1493e 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 * You can change
1493f 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 this value at c
14940 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 ompile-time by s
14941 70 65 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d pecifying a.** -
14942 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 DSQLITE_FILE_HEA
14943 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 DER="..." on the
14944 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
14945 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 d-line. The.**
14946 68 65 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 header must be e
14947 78 61 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 xactly 16 bytes
14948 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 including the ze
14949 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f ro-terminator so
1494a 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 .** the string i
1494b 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 tself should be
1494c 31 35 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 15 characters lo
1494d 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e ng. If you chan
1494e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 ge.** the header
1494f 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 , then your cust
14950 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 om library will
14951 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 not be able to r
14952 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 ead .** database
14953 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 s generated by t
14954 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c he standard tool
14955 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 s and the standa
14956 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c rd tools.** will
14957 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 not be able to
14958 72 65 61 64 20 64 61 74 61 62 61 73 65 73 20 63 read databases c
14959 72 65 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 reated by your c
1495a 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a ustom library..*
1495b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1495c 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 _FILE_HEADER /*
1495d 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36 123456789 123456
1495e 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 */.# define SQ
1495f 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 LITE_FILE_HEADER
14960 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 "SQLite format
14961 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 3".#endif../*.**
14962 20 50 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 Page type flags
14963 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 . An ORed combi
14964 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 nation of these
14965 66 6c 61 67 73 20 61 70 70 65 61 72 20 61 73 20 flags appear as
14966 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 the.** first byt
14967 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 e of on-disk ima
14968 67 65 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 ge of every BTre
14969 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 e page..*/.#defi
1496a 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 ne PTF_INTKEY
1496b 20 30 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 0x01.#define PT
1496c 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 F_ZERODATA 0x02
1496d 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 .#define PTF_LEA
1496e 46 44 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 FDATA 0x04.#def
1496f 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 ine PTF_LEAF
14970 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 0x08../*.** As
14971 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 each page of th
14972 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 e file is loaded
14973 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e into memory, an
14974 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
14975 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
14976 72 75 63 74 75 72 65 20 69 73 20 61 70 70 65 6e ructure is appen
14977 64 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ded and initiali
14978 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 zed to zero. Th
14979 69 73 20 73 74 72 75 63 74 75 72 65 20 73 74 6f is structure sto
1497a 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 res.** informati
1497b 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 on about the pag
1497c 65 20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 e that is decode
1497d 64 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 d from the raw f
1497e 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 ile page..**.**
1497f 54 68 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c The pParent fiel
14980 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f d points back to
14981 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
14982 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 . This allows u
14983 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 s to.** walk up
14984 74 68 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 the BTree from a
14985 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 ny leaf to the r
14986 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 oot. Care must
14987 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 be taken to.** u
14988 6e 72 65 66 28 29 20 74 68 65 20 70 61 72 65 6e nref() the paren
14989 74 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 t page pointer w
1498a 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 hen this page is
1498b 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 no longer refer
1498c 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 enced..** The pa
1498d 67 65 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 geDestructor() r
1498e 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 outine handles t
1498f 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a hat chore..**.**
14990 20 41 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 Access to all f
14991 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 ields of this st
14992 72 75 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 ructure is contr
14993 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 olled by the mut
14994 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 ex.** stored in
14995 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 MemPage.pBt->mut
14996 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 ex..*/.struct Me
14997 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 mPage {. u8 isI
14998 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f nit; /
14999 2a 20 54 72 75 65 20 69 66 20 70 72 65 76 69 6f * True if previo
1499a 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 usly initialized
1499b 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 . MUST BE FIRST!
1499c 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c */. u8 nOverfl
1499d 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ow; /* Nu
1499e 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 mber of overflow
1499f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 cell bodies in
149a0 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 aCell[] */. u8
149a1 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 intKey;
149a2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 /* True if int
149a3 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 key flag is set
149a4 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 */. u8 leaf;
149a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
149a6 65 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 e if leaf flag i
149a7 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 s set */. u8 ha
149a8 73 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 sData;
149a9 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
149aa 70 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 page stores data
149ab 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 */. u8 hdrOffs
149ac 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 et; /* 10
149ad 30 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 0 for page 1. 0
149ae 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 otherwise */.
149af 75 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b u8 childPtrSize;
149b0 20 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 /* 0 if lea
149b1 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 f==1. 4 if leaf
149b2 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 ==0 */. u16 max
149b3 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a Local; /*
149b4 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 Copy of BtShare
149b5 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 d.maxLocal or Bt
149b6 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a Shared.maxLeaf *
149b7 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c /. u16 minLocal
149b8 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 ; /* Copy
149b9 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e of BtShared.min
149ba 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 Local or BtShare
149bb 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 d.minLeaf */. u
149bc 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 16 cellOffset;
149bd 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
149be 61 44 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 aData of first c
149bf 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
149c0 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
149c1 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
149c2 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e of free bytes on
149c3 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 the page */. u
149c4 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 16 nCell;
149c5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
149c6 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 cells on this p
149c7 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f age, local and o
149c8 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 vfl */. u16 mas
149c9 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a kPage; /*
149ca 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f Mask for page o
149cb 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 ffset */. struc
149cc 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 t _OvflCell {
149cd 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 /* Cells that wi
149ce 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 ll not fit on aD
149cf 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 ata[] */. u8
149d0 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 *pCell;
149d1 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 /* Pointers to
149d2 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 the body of the
149d3 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f overflow cell */
149d4 0a 20 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 . u16 idx;
149d5 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 /* Inse
149d6 72 74 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 rt this cell bef
149d7 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f ore idx-th non-o
149d8 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a verflow cell */.
149d9 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 } aOvfl[5];.
149da 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
149db 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
149dc 74 6f 20 42 74 53 68 61 72 65 64 20 74 68 61 74 to BtShared that
149dd 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61 this page is pa
149de 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 rt of */. u8 *a
149df 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
149e0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 /* Pointer to di
149e1 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 sk image of the
149e2 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 page data */. D
149e3 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
149e4 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 /* Pager pag
149e5 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 e handle */. Pg
149e6 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 no pgno;
149e7 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
149e8 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 r for this page
149e9 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
149ea 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 in-memory image
149eb 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 of a disk page
149ec 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 has the auxiliar
149ed 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 y information ap
149ee 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 pended.** to the
149ef 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a end. EXTRA_SIZ
149f0 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 E is the number
149f1 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
149f2 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 e needed to hold
149f3 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 .** that extra i
149f4 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 nformation..*/.#
149f5 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a define EXTRA_SIZ
149f6 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 E sizeof(MemPage
149f7 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 )../*.** A linke
149f8 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f d list of the fo
149f9 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
149fa 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 es is stored at
149fb 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a BtShared.pLock..
149fc 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 64 ** Locks are add
149fd 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 20 ed (or upgraded
149fe 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74 from READ_LOCK t
149ff 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68 o WRITE_LOCK) wh
14a00 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20 en a cursor .**
14a01 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 is opened on the
14a02 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
14a03 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e 69 page BtShared.i
14a04 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65 Table. Locks are
14a05 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d removed.** from
14a06 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20 this list when
14a07 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
14a08 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f committed or ro
14a09 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 68 lled back, or wh
14a0a 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 61 en.** a btree ha
14a0b 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a ndle is closed..
14a0c 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 6b */.struct BtLock
14a0d 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 {. Btree *pBtr
14a0e 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 ee; /* Bt
14a0f 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69 ree handle holdi
14a10 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a ng this lock */.
14a11 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20 Pgno iTable;
14a12 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
14a13 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f page of table */
14a14 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 . u8 eLock;
14a15 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 44 /* READ
14a16 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c _LOCK or WRITE_L
14a17 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 OCK */. BtLock
14a18 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 2f *pNext; /
14a19 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 72 * Next in BtShar
14a1a 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f ed.pLock list */
14a1b 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74 .};../* Candidat
14a1c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c e values for BtL
14a1d 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 ock.eLock */.#de
14a1e 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20 fine READ_LOCK
14a1f 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 49 1.#define WRI
14a20 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a TE_LOCK 2../*
14a21 20 41 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a A Btree handle.
14a22 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 **.** A database
14a23 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 connection cont
14a24 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
14a25 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 o an instance of
14a26 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 .** this object
14a27 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 for every databa
14a28 73 65 20 66 69 6c 65 20 74 68 61 74 20 69 74 20 se file that it
14a29 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 has open. This
14a2a 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 structure.** is
14a2b 6f 70 61 71 75 65 20 74 6f 20 74 68 65 20 64 61 opaque to the da
14a2c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14a2d 6e 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 n. The database
14a2e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e connection cann
14a2f 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e ot.** see the in
14a30 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 ternals of this
14a31 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e structure and on
14a32 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f ly deals with po
14a33 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 inters to.** thi
14a34 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a s structure..**.
14a35 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 ** For some data
14a36 62 61 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20 base files, the
14a37 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 same underlying
14a38 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 6d database cache m
14a39 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 ight be .** shar
14a3a 65 64 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 ed between multi
14a3b 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ple connections.
14a3c 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 In that case,
14a3d 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a each connection.
14a3e 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e 20 69 6e ** has it own in
14a3f 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
14a40 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 63 68 bject. But each
14a41 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
14a42 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e s object.** poin
14a43 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42 ts to the same B
14a44 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 tShared object.
14a45 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61 The database ca
14a46 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 che and the.** s
14a47 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 chema associated
14a48 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 with the databa
14a49 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20 se file are all
14a4a 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
14a4b 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65 64 .** the BtShared
14a4c 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 object..**.** A
14a4d 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 ll fields in thi
14a4e 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
14a4f 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 73 accessed under s
14a50 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a qlite3.mutex..**
14a51 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 65 72 The pBt pointer
14a52 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20 itself may not
14a53 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 be changed while
14a54 20 74 68 65 72 65 20 65 78 69 73 74 73 20 63 75 there exists cu
14a55 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65 rsors .** in the
14a56 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 53 68 referenced BtSh
14a57 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e 74 20 ared that point
14a58 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 74 72 back to this Btr
14a59 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a ee since those.*
14a5a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 74 * cursors have t
14a5b 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 o do go through
14a5c 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 66 69 this Btree to fi
14a5d 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 72 65 nd their BtShare
14a5e 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 d and.** they of
14a5f 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 ten do so withou
14a60 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 t holding sqlite
14a61 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 3.mutex..*/.stru
14a62 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 71 6c ct Btree {. sql
14a63 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
14a64 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
14a65 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 connection holdi
14a66 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f ng this btree */
14a67 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
14a68 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 62 6c ; /* Sharabl
14a69 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 e content of thi
14a6a 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 s btree */. u8
14a6b 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 inTrans;
14a6c 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 /* TRANS_NONE, T
14a6d 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 52 41 RANS_READ or TRA
14a6e 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 NS_WRITE */. u8
14a6f 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 20 20 sharable;
14a70 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63 /* True if we c
14a71 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 69 74 an share pBt wit
14a72 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a h another db */.
14a73 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 u8 locked;
14a74 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
14a75 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 db currently has
14a76 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 pBt locked */.
14a77 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b int wantToLock;
14a78 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14a79 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 6f nested calls to
14a7a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
14a7b 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 er() */. int nB
14a7c 61 63 6b 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 ackup; /*
14a7d 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 75 70 Number of backup
14a7e 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 65 61 64 operations read
14a7f 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a ing this btree *
14a80 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65 78 74 /. Btree *pNext
14a81 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f ; /* List o
14a82 66 20 6f 74 68 65 72 20 73 68 61 72 61 62 6c 65 f other sharable
14a83 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74 68 65 Btrees from the
14a84 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 same db */. Bt
14a85 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20 20 20 ree *pPrev;
14a86 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 /* Back pointer
14a87 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c 69 73 of the same lis
14a88 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c t */.#ifndef SQL
14a89 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14a8a 43 41 43 48 45 0a 20 20 42 74 4c 6f 63 6b 20 6c CACHE. BtLock l
14a8b 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a 20 4f 62 ock; /* Ob
14a8c 6a 65 63 74 20 75 73 65 64 20 74 6f 20 6c 6f 63 ject used to loc
14a8d 6b 20 70 61 67 65 20 31 20 2a 2f 0a 23 65 6e 64 k page 1 */.#end
14a8e 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 if.};../*.** Btr
14a8f 65 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 ee.inTrans may t
14a90 61 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 ake one of the f
14a91 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e ollowing values.
14a92 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 .**.** If the sh
14a93 61 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 ared-data extens
14a94 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 ion is enabled,
14a95 74 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c there may be mul
14a96 74 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f tiple users.** o
14a97 66 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75 f the Btree stru
14a98 63 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f cture. At most o
14a99 6e 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 ne of these may
14a9a 6f 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 open a write tra
14a9b 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 nsaction,.** but
14a9c 20 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 any number may
14a9d 68 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64 have active read
14a9e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a transactions..*
14a9f 2f 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f /.#define TRANS_
14aa0 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20 NONE 0.#define
14aa1 54 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 TRANS_READ 1.#d
14aa2 65 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 efine TRANS_WRIT
14aa3 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e E 2../*.** An in
14aa4 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
14aa5 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 bject represents
14aa6 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
14aa7 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 se file..** .**
14aa8 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 A single databas
14aa9 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e e file can be in
14aaa 20 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65 use as the same
14aab 20 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 time by two.**
14aac 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 or more database
14aad 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 connections. W
14aae 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 hen two or more
14aaf 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a connections are.
14ab0 2a 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73 ** sharing the s
14ab1 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ame database fil
14ab2 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 e, each connecti
14ab3 6f 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a on has it own.**
14ab4 20 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f private Btree o
14ab5 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69 bject for the fi
14ab6 6c 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 le and each of t
14ab7 68 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e hose Btrees poin
14ab8 74 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e ts.** to this on
14ab9 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
14aba 74 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65 t. BtShared.nRe
14abb 66 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 f is the number
14abc 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e of.** connection
14abd 73 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72 s currently shar
14abe 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73 ing this databas
14abf 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 e file..**.** Fi
14ac0 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 elds in this str
14ac1 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 ucture are acces
14ac2 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 sed under the Bt
14ac3 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 Shared.mutex.**
14ac4 6d 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f mutex, except fo
14ac5 72 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74 r nRef and pNext
14ac6 20 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73 which are acces
14ac7 73 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a sed under the.**
14ac8 20 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d global SQLITE_M
14ac9 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
14aca 45 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70 ER mutex. The p
14acb 50 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d Pager field.** m
14acc 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 ay not be modifi
14acd 65 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e ed once it is in
14ace 69 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c itially set as l
14acf 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a ong as nRef>0..*
14ad0 2a 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 * The pSchema fi
14ad1 65 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f eld may be set o
14ad2 6e 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 nce under BtShar
14ad3 65 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 ed.mutex and.**
14ad4 74 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e thereafter is un
14ad5 63 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 changed as long
14ad6 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a as nRef>0..**.**
14ad7 20 69 73 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a isPending:.**.*
14ad8 2a 20 20 20 49 66 20 61 20 42 74 53 68 61 72 65 * If a BtShare
14ad9 64 20 63 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 d client fails t
14ada 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74 65 o obtain a write
14adb 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 -lock on a datab
14adc 61 73 65 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 ase.** table (
14add 62 65 63 61 75 73 65 20 74 68 65 72 65 20 65 78 because there ex
14ade 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 ists one or more
14adf 20 72 65 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 read-locks on t
14ae0 68 65 20 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 he table),.**
14ae1 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 the shared-cache
14ae2 20 65 6e 74 65 72 73 20 27 70 65 6e 64 69 6e 67 enters 'pending
14ae3 2d 6c 6f 63 6b 27 20 73 74 61 74 65 20 61 6e 64 -lock' state and
14ae4 20 69 73 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a isPending is.**
14ae5 20 20 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a set to true..
14ae6 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 73 68 61 72 **.** The shar
14ae7 65 64 2d 63 61 63 68 65 20 6c 65 61 76 65 73 20 ed-cache leaves
14ae8 74 68 65 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 the 'pending loc
14ae9 6b 27 20 73 74 61 74 65 20 77 68 65 6e 20 65 69 k' state when ei
14aea 74 68 65 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 ther of.** the
14aeb 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 following occur
14aec 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 :.**.** 1) T
14aed 68 65 20 63 75 72 72 65 6e 74 20 77 72 69 74 65 he current write
14aee 72 20 28 42 74 53 68 61 72 65 64 2e 70 57 72 69 r (BtShared.pWri
14aef 74 65 72 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 ter) concludes i
14af0 74 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 ts transaction,
14af1 4f 52 0a 2a 2a 20 20 20 20 20 32 29 20 54 68 65 OR.** 2) The
14af2 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 number of locks
14af3 20 68 65 6c 64 20 62 79 20 6f 74 68 65 72 20 63 held by other c
14af4 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 onnections drops
14af5 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 to zero..**.**
14af6 20 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 27 while in the '
14af7 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 pending-lock' st
14af8 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 ate, no connecti
14af9 6f 6e 20 6d 61 79 20 73 74 61 72 74 20 61 20 6e on may start a n
14afa 65 77 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 ew.** transact
14afb 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 ion..**.** Thi
14afc 73 20 66 65 61 74 75 72 65 20 69 73 20 69 6e 63 s feature is inc
14afd 6c 75 64 65 64 20 74 6f 20 68 65 6c 70 20 70 72 luded to help pr
14afe 65 76 65 6e 74 20 77 72 69 74 65 72 2d 73 74 61 event writer-sta
14aff 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 rvation..*/.stru
14b00 63 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20 20 ct BtShared {.
14b01 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 Pager *pPager;
14b02 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
14b03 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c e cache */. sql
14b04 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
14b05 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 /* Database c
14b06 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e onnection curren
14b07 74 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20 42 tly using this B
14b08 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 tree */. BtCurs
14b09 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 or *pCursor;
14b0a 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c /* A list of all
14b0b 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f open cursors */
14b0c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
14b0d 65 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 e1; /* Firs
14b0e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 t page of the da
14b0f 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 tabase */. u8 r
14b10 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 eadOnly;
14b11 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
14b12 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 underlying file
14b13 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a is readonly */.
14b14 20 20 75 38 20 70 61 67 65 53 69 7a 65 46 69 78 u8 pageSizeFix
14b15 65 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ed; /* True
14b16 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 if the page size
14b17 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 can no longer b
14b18 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69 66 e changed */.#if
14b19 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14b1a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75 38 _AUTOVACUUM. u8
14b1b 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20 20 autoVacuum;
14b1c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
14b1d 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e uto-vacuum is en
14b1e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69 6e abled */. u8 in
14b1f 63 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 crVacuum;
14b20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63 72 /* True if incr
14b21 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c -vacuum is enabl
14b22 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 ed */.#endif. u
14b23 31 36 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 16 pageSize;
14b24 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
14b25 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 6e mber of bytes on
14b26 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 a page */. u16
14b27 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 usableSize;
14b28 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14b29 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 usable bytes on
14b2a 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 each page */. u
14b2b 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 16 maxLocal;
14b2c 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
14b2d 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e local payload in
14b2e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 non-LEAFDATA ta
14b2f 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 69 bles */. u16 mi
14b30 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 nLocal;
14b31 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c /* Minimum local
14b32 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d payload in non-
14b33 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 LEAFDATA tables
14b34 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 65 61 66 */. u16 maxLeaf
14b35 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 ; /* Ma
14b36 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c ximum local payl
14b37 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 oad in a LEAFDAT
14b38 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 31 36 A table */. u16
14b39 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 minLeaf;
14b3a 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f /* Minimum lo
14b3b 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 cal payload in a
14b3c 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 LEAFDATA table
14b3d 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 */. u8 inTransa
14b3e 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 ction; /* Tr
14b3f 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 ansaction state
14b40 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61 */. int nTransa
14b41 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 ction; /* Nu
14b42 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 72 61 mber of open tra
14b43 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 61 64 20 nsactions (read
14b44 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 76 6f + write) */. vo
14b45 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 id *pSchema;
14b46 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
14b47 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 o space allocate
14b48 64 20 62 79 20 73 71 6c 69 74 65 33 42 74 72 65 d by sqlite3Btre
14b49 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76 eSchema() */. v
14b4a 6f 69 64 20 28 2a 78 46 72 65 65 53 63 68 65 6d oid (*xFreeSchem
14b4b 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 a)(void*); /* D
14b4c 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 42 74 estructor for Bt
14b4d 53 68 61 72 65 64 2e 70 53 63 68 65 6d 61 20 2a Shared.pSchema *
14b4e 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
14b4f 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e x *mutex; /* Non
14b50 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
14b51 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 required to acc
14b52 65 73 73 20 74 68 69 73 20 73 74 72 75 63 74 20 ess this struct
14b53 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 48 61 */. Bitvec *pHa
14b54 73 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a 20 53 65 sContent; /* Se
14b55 74 20 6f 66 20 70 61 67 65 73 20 6d 6f 76 65 64 t of pages moved
14b56 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 74 68 to free-list th
14b57 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a is transaction *
14b58 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
14b59 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
14b5a 48 45 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 HE. int nRef;
14b5b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
14b5c 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
14b5d 65 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 es to this struc
14b5e 74 75 72 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 ture */. BtShar
14b5f 65 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ed *pNext;
14b60 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 /* Next on a lis
14b61 74 20 6f 66 20 73 68 61 72 61 62 6c 65 20 42 74 t of sharable Bt
14b62 53 68 61 72 65 64 20 73 74 72 75 63 74 73 20 2a Shared structs *
14b63 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 /. BtLock *pLoc
14b64 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 k; /* Lis
14b65 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 t of locks held
14b66 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 on this shared-b
14b67 74 72 65 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 tree struct */.
14b68 20 42 74 72 65 65 20 2a 70 57 72 69 74 65 72 3b Btree *pWriter;
14b69 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 /* Btree
14b6a 77 69 74 68 20 63 75 72 72 65 6e 74 6c 79 20 6f with currently o
14b6b 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 pen write transa
14b6c 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 69 73 ction */. u8 is
14b6d 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 20 20 Exclusive;
14b6e 20 2f 2a 20 54 72 75 65 20 69 66 20 70 57 72 69 /* True if pWri
14b6f 74 65 72 20 68 61 73 20 61 6e 20 45 58 43 4c 55 ter has an EXCLU
14b70 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
14b71 20 64 62 20 2a 2f 0a 20 20 75 38 20 69 73 50 65 db */. u8 isPe
14b72 6e 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f nding; /
14b73 2a 20 49 66 20 77 61 69 74 69 6e 67 20 66 6f 72 * If waiting for
14b74 20 72 65 61 64 2d 6c 6f 63 6b 73 20 74 6f 20 63 read-locks to c
14b75 6c 65 61 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 lear */.#endif.
14b76 20 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 u8 *pTmpSpace;
14b77 20 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 /* BtShar
14b78 65 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 ed.pageSize byte
14b79 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 s of space for t
14b7a 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a mp use */.};../*
14b7b 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
14b7c 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
14b7d 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 structure is us
14b7e 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 ed to hold infor
14b7f 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 mation.** about
14b80 61 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 a cell. The par
14b81 73 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 seCellPtr() func
14b82 74 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 tion fills in th
14b83 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
14b84 62 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 based on informa
14b85 74 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f tion extract fro
14b86 6d 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 m the raw disk p
14b87 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 age..*/.typedef
14b88 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 struct CellInfo
14b89 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 CellInfo;.struct
14b8a 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 CellInfo {. u8
14b8b 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 *pCell; /*
14b8c 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 Pointer to the s
14b8d 74 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e tart of cell con
14b8e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b tent */. i64 nK
14b8f 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ey; /* The
14b90 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 key for INTKEY t
14b91 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 ables, or number
14b92 20 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 of bytes in key
14b93 20 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b */. u32 nData;
14b94 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14b95 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
14b96 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 */. u32 nPayloa
14b97 64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f d; /* Total amo
14b98 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a unt of payload *
14b99 2f 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b /. u16 nHeader;
14b9a 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
14b9b 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 e cell content h
14b9c 65 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a eader in bytes *
14b9d 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 /. u16 nLocal;
14b9e 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 /* Amount of
14b9f 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 payload held loc
14ba0 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f ally */. u16 iO
14ba1 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 verflow; /* Offs
14ba2 65 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 et to overflow p
14ba3 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 age number. Zer
14ba4 6f 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 o if no overflow
14ba5 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b */. u16 nSize;
14ba6 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
14ba7 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
14ba8 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 on the main b-t
14ba9 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a ree page */.};..
14baa 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 /*.** Maximum de
14bab 70 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 pth of an SQLite
14bac 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 B-Tree structur
14bad 65 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 e. Any B-Tree de
14bae 65 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 eper than.** thi
14baf 73 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 s will be declar
14bb0 65 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 ed corrupt. This
14bb1 20 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c value is calcul
14bb2 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a ated based on a.
14bb3 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 ** maximum datab
14bb4 61 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 ase size of 2^31
14bb5 20 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d pages a minimum
14bb6 20 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 fanout of 2 for
14bb7 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 a.** root-node
14bb8 61 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 and 3 for all ot
14bb9 68 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 her internal nod
14bba 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 es..**.** If a t
14bbb 72 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 ree that appears
14bbc 20 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 to be taller th
14bbd 61 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 an this is encou
14bbe 6e 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a ntered, it is.**
14bbf 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
14bc0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f e database is co
14bc1 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e rrupt..*/.#defin
14bc2 65 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 e BTCURSOR_MAX_D
14bc3 45 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 EPTH 20../*.** A
14bc4 20 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 cursor is a poi
14bc5 6e 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 nter to a partic
14bc6 75 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 ular entry withi
14bc7 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a n a particular.*
14bc8 2a 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 * b-tree within
14bc9 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
14bca 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 .**.** The entry
14bcb 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 is identified b
14bcc 79 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e y its MemPage an
14bcd 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a d the index in.*
14bce 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b * MemPage.aCell[
14bcf 5d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a ] of the entry..
14bd0 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 **.** A single d
14bd1 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e atabase file can
14bd2 20 73 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d shared by two m
14bd3 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ore database con
14bd4 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 nections,.** but
14bd5 20 63 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20 cursors cannot
14bd6 62 65 20 73 68 61 72 65 64 2e 20 20 45 61 63 68 be shared. Each
14bd7 20 63 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63 cursor is assoc
14bd8 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 iated with a.**
14bd9 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 particular datab
14bda 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
14bdb 64 65 6e 74 69 66 69 65 64 20 42 74 43 75 72 73 dentified BtCurs
14bdc 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a or.pBtree.db..**
14bdd 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 .** Fields in th
14bde 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 is structure are
14bdf 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 accessed under
14be0 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 the BtShared.mut
14be1 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 ex.** found at s
14be2 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e elf->pBt->mutex.
14be3 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75 .*/.struct BtCu
14be4 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a rsor {. Btree *
14be5 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 20 pBtree;
14be6 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 /* The Btree
14be7 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 63 75 to which this cu
14be8 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a rsor belongs */.
14be9 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
14bea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
14beb 68 65 20 42 74 53 68 61 72 65 64 20 74 68 69 73 he BtShared this
14bec 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 cursor points t
14bed 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 o */. BtCursor
14bee 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 *pNext, *pPrev;
14bef 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b /* Forms a link
14bf0 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 ed list of all c
14bf1 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75 ursors */. stru
14bf2 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 ct KeyInfo *pKey
14bf3 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e Info; /* Argumen
14bf4 74 20 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70 t passed to comp
14bf5 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 arison function
14bf6 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f */. Pgno pgnoRo
14bf7 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ot; /
14bf8 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
14bf9 6f 66 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a of this tree */.
14bfa 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
14bfb 63 61 63 68 65 64 52 6f 77 69 64 3b 20 2f 2a 20 cachedRowid; /*
14bfc 4e 65 78 74 20 72 6f 77 69 64 20 63 61 63 68 65 Next rowid cache
14bfd 2e 20 20 30 20 6d 65 61 6e 73 20 6e 6f 74 20 76 . 0 means not v
14bfe 61 6c 69 64 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e alid */. CellIn
14bff 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20 20 20 20 fo info;
14c00 20 20 20 20 2f 2a 20 41 20 70 61 72 73 65 20 6f /* A parse o
14c01 66 20 74 68 65 20 63 65 6c 6c 20 77 65 20 61 72 f the cell we ar
14c02 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f e pointing at */
14c03 0a 20 20 75 38 20 77 72 46 6c 61 67 3b 20 20 20 . u8 wrFlag;
14c04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14c05 54 72 75 65 20 69 66 20 77 72 69 74 61 62 6c 65 True if writable
14c06 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61 73 74 3b */. u8 atLast;
14c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c08 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 /* Cursor pointi
14c09 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 ng to the last e
14c0a 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c ntry */. u8 val
14c0b 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 20 20 20 idNKey;
14c0c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
14c0d 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69 nfo.nKey is vali
14c0e 64 20 2a 2f 0a 20 20 75 38 20 65 53 74 61 74 65 d */. u8 eState
14c0f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14c10 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 /* One of the C
14c11 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61 URSOR_XXX consta
14c12 6e 74 73 20 28 73 65 65 20 62 65 6c 6f 77 29 20 nts (see below)
14c13 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b */. void *pKey;
14c14 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 6b /* Saved k
14c15 65 79 20 74 68 61 74 20 77 61 73 20 63 75 72 73 ey that was curs
14c16 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 or's last known
14c17 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 position */. i6
14c18 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 4 nKey; /
14c19 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20 * Size of pKey,
14c1a 6f 72 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 or last integer
14c1b 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 key */. int ski
14c1c 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 50 72 65 pNext; /* Pre
14c1d 76 28 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 6e v() is noop if n
14c1e 65 67 61 74 69 76 65 2e 20 4e 65 78 74 28 29 20 egative. Next()
14c1f 69 73 20 6e 6f 6f 70 20 69 66 20 70 6f 73 69 74 is noop if posit
14c20 69 76 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ive */.#ifndef S
14c21 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
14c22 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62 LOB. u8 isIncrb
14c23 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20 lobHandle;
14c24 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
14c25 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63 cursor is an inc
14c26 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a r. io handle */.
14c27 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f Pgno *aOverflo
14c28 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 w; /* C
14c29 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 ache of overflow
14c2a 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20 page locations
14c2b 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 */.#endif. i16
14c2c 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 iPage;
14c2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c2e 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 /* Index of cu
14c2f 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 rrent page in ap
14c30 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 Page */. MemPag
14c31 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 e *apPage[BTCURS
14c32 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 OR_MAX_DEPTH];
14c33 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f /* Pages from ro
14c34 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 ot to current pa
14c35 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 ge */. u16 aiId
14c36 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 x[BTCURSOR_MAX_D
14c37 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a EPTH]; /*
14c38 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 Current index i
14c39 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d n apPage[i] */.}
14c3a 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 ;../*.** Potenti
14c3b 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 al values for Bt
14c3c 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a Cursor.eState..*
14c3d 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 *.** CURSOR_VALI
14c3e 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 D:.** Cursor p
14c3f 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 oints to a valid
14c40 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f entry. getPaylo
14c41 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 ad() etc. may be
14c42 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 called..**.** C
14c43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a URSOR_INVALID:.*
14c44 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 * Cursor does
14c45 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 not point to a v
14c46 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 alid entry. This
14c47 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 can happen (for
14c48 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 example) .**
14c49 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c because the tabl
14c4a 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 e is empty or be
14c4b 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73 6f cause BtreeCurso
14c4c 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 rFirst() has not
14c4d 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 been.** calle
14c4e 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f d..**.** CURSOR_
14c4f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 REQUIRESEEK:.**
14c50 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 The table that
14c51 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 this cursor was
14c52 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c opened on still
14c53 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 exists, but has
14c54 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 been .** modi
14c55 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 fied since the c
14c56 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 ursor was last u
14c57 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 sed. The cursor
14c58 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 position is save
14c59 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 d.** in variab
14c5a 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 les BtCursor.pKe
14c5b 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e y and BtCursor.n
14c5c 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 Key. When a curs
14c5d 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 or is in .** t
14c5e 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f his state, resto
14c5f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
14c60 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 () can be called
14c61 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a to attempt to.*
14c62 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 * seek the cur
14c63 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 sor to the saved
14c64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a position..**.**
14c65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a CURSOR_FAULT:.*
14c66 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 * A unrecovera
14c67 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f ble error (an I/
14c68 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c O error or a mal
14c69 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 loc failure) has
14c6a 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f occurred.** o
14c6b 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f n a different co
14c6c 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 nnection that sh
14c6d 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72 65 ares the BtShare
14c6e 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68 69 d cache with thi
14c6f 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 s.** cursor.
14c70 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 The error has le
14c71 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 ft the cache in
14c72 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
14c73 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e state..** Do n
14c74 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 othing else with
14c75 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 this cursor. A
14c76 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
14c77 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 e the cursor.**
14c78 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 should return
14c79 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 the error code s
14c7a 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f tored in BtCurso
14c7b 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e r.skip.*/.#defin
14c7c 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 e CURSOR_INVALID
14c7d 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 0.#de
14c7e 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 fine CURSOR_VALI
14c7f 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a D 1.
14c80 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 #define CURSOR_R
14c81 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 EQUIRESEEK
14c82 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 2.#define CURSO
14c83 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 R_FAULT
14c84 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 3../* .** Th
14c85 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
14c86 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 the PENDING_BYTE
14c87 20 6f 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 occupies. This
14c88 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 page is never us
14c89 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 ed..*/.# define
14c8a 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
14c8b 45 28 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f E(pBt) PAGER_MJ_
14c8c 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a PGNO(pBt)../*.**
14c8d 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 These macros de
14c8e 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f fine the locatio
14c8f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 n of the pointer
14c90 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 -map entry for a
14c91 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 .** database pa
14c92 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 ge. The first ar
14c93 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 gument to each i
14c94 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
14c95 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 usable.** bytes
14c96 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 on each page of
14c97 74 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 the database (of
14c98 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 ten 1024). The s
14c99 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 econd is the.**
14c9a 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c page number to l
14c9b 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f ook up in the po
14c9c 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a inter map..**.**
14c9d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 PTRMAP_PAGENO r
14c9e 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 eturns the datab
14c9f 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ase page number
14ca0 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d of the pointer-m
14ca1 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 ap.** page that
14ca2 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 stores the requi
14ca3 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 red pointer. PTR
14ca4 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 MAP_PTROFFSET re
14ca5 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 turns.** the off
14ca6 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 set of the reque
14ca7 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a sted map entry..
14ca8 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e **.** If the pgn
14ca9 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 o argument passe
14caa 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 d to PTRMAP_PAGE
14cab 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d NO is a pointer-
14cac 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 map page,.** the
14cad 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e n pgno is return
14cae 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 ed. So (pgno==PT
14caf 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a RMAP_PAGENO(pgsz
14cb0 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a , pgno)) can be.
14cb1 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 ** used to test
14cb2 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 if pgno is a poi
14cb3 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 nter-map page. P
14cb4 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 TRMAP_ISPAGE imp
14cb5 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 lements.** this
14cb6 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 test..*/.#define
14cb7 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 PTRMAP_PAGENO(p
14cb8 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 Bt, pgno) ptrmap
14cb9 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f Pageno(pBt, pgno
14cba 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 ).#define PTRMAP
14cbb 5f 50 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 _PTROFFSET(pgptr
14cbc 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 map, pgno) (5*(p
14cbd 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 gno-pgptrmap-1))
14cbe 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
14cbf 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f ISPAGE(pBt, pgno
14cc0 29 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ) (PTRMAP_PAGENO
14cc1 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d ((pBt),(pgno))==
14cc2 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 (pgno))../*.** T
14cc3 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
14cc4 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 s a lookup table
14cc5 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 that identifies
14cc6 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
14cc7 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 for.** each chi
14cc8 6c 64 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ld page in the d
14cc9 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
14cca 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 he parent page i
14ccb 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a s the page that.
14ccc 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f ** contains a po
14ccd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 inter to the chi
14cce 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 ld. Every page
14ccf 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
14cd0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 contains.** 0 or
14cd1 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 1 parent pages.
14cd2 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 (In this conte
14cd3 78 74 20 27 64 61 74 61 62 61 73 65 20 70 61 67 xt 'database pag
14cd4 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 e' refers.** to
14cd5 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 69 73 any page that is
14cd6 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
14cd7 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 pointer map its
14cd8 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e elf.) Each poin
14cd9 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 ter map.** entry
14cda 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 consists of a s
14cdb 69 6e 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 ingle byte 'type
14cdc 27 20 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 ' and a 4 byte p
14cdd 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 arent page numbe
14cde 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 r..** The PTRMAP
14cdf 5f 58 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 _XXX identifiers
14ce0 20 62 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 below are the v
14ce1 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a alid types..**.*
14ce2 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 * The purpose of
14ce3 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
14ce4 20 69 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 is to facility
14ce5 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f moving pages fro
14ce6 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f m one.** positio
14ce7 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f n in the file to
14ce8 20 61 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 another as part
14ce9 20 6f 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 of autovacuum.
14cea 20 57 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 When a page.**
14ceb 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f is moved, the po
14cec 69 6e 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 inter in its par
14ced 65 6e 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 ent must be upda
14cee 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ted to point to
14cef 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 the.** new locat
14cf0 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 ion. The pointe
14cf1 72 20 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f r map is used to
14cf2 20 6c 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 locate the pare
14cf3 6e 74 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e nt page quickly.
14cf4 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f .**.** PTRMAP_RO
14cf5 4f 54 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 OTPAGE: The data
14cf6 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 72 base page is a r
14cf7 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 oot-page. The pa
14cf8 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 ge-number is not
14cf9 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
14cfa 20 20 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 used in thi
14cfb 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 s case..**.** PT
14cfc 52 4d 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 RMAP_FREEPAGE: T
14cfd 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
14cfe 20 69 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 is an unused (f
14cff 72 65 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 ree) page. The p
14d00 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 age-number .**
14d01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d02 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 is not used in t
14d03 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
14d04 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
14d05 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14d06 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 age is the first
14d07 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 page in a list
14d08 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of .**
14d09 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f overflo
14d0a 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 w pages. The pag
14d0b 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 e number identif
14d0c 69 65 73 20 74 68 65 20 70 61 67 65 20 74 68 61 ies the page tha
14d0d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
14d0e 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 contains
14d0f 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 the cell with a
14d10 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
14d11 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a overflow page..*
14d12 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 *.** PTRMAP_OVER
14d13 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 FLOW2: The datab
14d14 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 ase page is the
14d15 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 second or later
14d16 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f page in a list o
14d17 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 f.**
14d18 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
14d19 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d pages. The page-
14d1a 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 number identifie
14d1b 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a s the previous.*
14d1c 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
14d1d 20 20 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 page in the
14d1e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 overflow page li
14d1f 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 st..**.** PTRMAP
14d20 5f 42 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 _BTREE: The data
14d21 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 6e base page is a n
14d22 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 on-root btree pa
14d23 67 65 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d ge. The page num
14d24 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ber.**
14d25 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 identifies
14d26 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
14d27 69 6e 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f in the btree..*/
14d28 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
14d29 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 ROOTPAGE 1.#defi
14d2a 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 ne PTRMAP_FREEPA
14d2b 47 45 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 GE 2.#define PTR
14d2c 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a MAP_OVERFLOW1 3.
14d2d 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f #define PTRMAP_O
14d2e 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 VERFLOW2 4.#defi
14d2f 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 ne PTRMAP_BTREE
14d30 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 5../* A bunch of
14d31 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
14d32 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 ents to check th
14d33 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 e transaction st
14d34 61 74 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a ate variables.**
14d35 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 of handle p (ty
14d36 70 65 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 pe Btree*) are i
14d37 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 nternally consis
14d38 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tent..*/.#define
14d39 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
14d3a 70 29 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 p) \. assert( p
14d3b 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
14d3c 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 tion!=TRANS_NONE
14d3d 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 || p->pBt->nTra
14d3e 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c nsaction==0 ); \
14d3f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 . assert( p->pB
14d40 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
14d41 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 >=p->inTrans );
14d42 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 .../*.** The ISA
14d43 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 UTOVACUUM macro
14d44 69 73 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 is used within b
14d45 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
14d46 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a to determine.**
14d47 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
14d48 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 supports auto-v
14d49 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 acuum or not. Be
14d4a 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 cause it is used
14d4b 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 .** within an ex
14d4c 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
14d4d 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 an argument to
14d4e 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a another macro .*
14d4f 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 * (sqliteMallocR
14d50 61 77 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 aw), it is not p
14d51 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 ossible to use c
14d52 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 onditional compi
14d53 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 lation..** So, t
14d54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 his macro is def
14d55 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f ined instead..*/
14d56 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14d57 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
14d58 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 #define ISAUTOVA
14d59 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 CUUM (pBt->autoV
14d5a 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 acuum).#else.#de
14d5b 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 fine ISAUTOVACUU
14d5c 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a M 0.#endif.../*.
14d5d 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
14d5e 65 20 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 e is passed arou
14d5f 6e 64 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 nd through all t
14d60 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 he sanity checki
14d61 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ng routines.** i
14d62 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
14d63 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c track of some gl
14d64 6f 62 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 obal state infor
14d65 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 mation..*/.typed
14d66 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 ef struct Integr
14d67 69 74 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 ityCk IntegrityC
14d68 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 k;.struct Integr
14d69 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 ityCk {. BtShar
14d6a 65 64 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 ed *pBt; /* T
14d6b 68 65 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 he tree being ch
14d6c 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 ecked out */. P
14d6d 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 ager *pPager;
14d6e 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 /* The associat
14d6f 65 64 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 ed pager. Also
14d70 61 63 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 accessible by pB
14d71 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50 t->pPager */. P
14d72 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 gno nPage;
14d73 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
14d74 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
14d75 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e ase */. int *an
14d76 52 65 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 Ref; /* Nu
14d77 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 mber of times ea
14d78 63 68 20 70 61 67 65 20 69 73 20 72 65 66 65 72 ch page is refer
14d79 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d enced */. int m
14d7a 78 45 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 xErr; /*
14d7b 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e Stop accumulatin
14d7c 67 20 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 g errors when th
14d7d 69 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 is reaches zero
14d7e 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 */. int nErr;
14d7f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14d80 20 6f 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 of messages wri
14d81 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 tten to zErrMsg
14d82 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
14d83 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a mallocFailed; /*
14d84 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 A memory alloca
14d85 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f tion error has o
14d86 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 ccurred */. Str
14d87 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f Accum errMsg; /
14d88 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 * Accumulate the
14d89 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
14d8a 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a ext here */.};..
14d8b 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 /*.** Read or wr
14d8c 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 ite a two- and f
14d8d 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 our-byte big-end
14d8e 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ian integer valu
14d8f 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 es..*/.#define g
14d90 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28 78 et2byte(x) ((x
14d91 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d )[0]<<8 | (x)[1]
14d92 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 ).#define put2by
14d93 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 te(p,v) ((p)[0]
14d94 3d 20 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 = (u8)((v)>>8),
14d95 28 70 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 (p)[1] = (u8)(v)
14d96 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 ).#define get4by
14d97 74 65 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 te sqlite3Get4by
14d98 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 te.#define put4b
14d99 79 74 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 yte sqlite3Put4b
14d9a 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a yte../**********
14d9b 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 **** End of btre
14d9c 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
14d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d9f 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
14da0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
14da1 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
14da2 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a f in btmutex.c *
14da3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14da4 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ***/.#ifndef SQL
14da5 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14da6 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45 CACHE.#if SQLITE
14da7 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a _THREADSAFE../*.
14da8 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74 ** Obtain the Bt
14da9 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 Shared mutex ass
14daa 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 ociated with B-T
14dab 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c ree handle p. Al
14dac 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61 so,.** set BtSha
14dad 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61 red.db to the da
14dae 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73 tabase handle as
14daf 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 20 sociated with p
14db0 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f and the.** p->lo
14db1 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 cked boolean to
14db2 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 true..*/.static
14db3 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75 void lockBtreeMu
14db4 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 tex(Btree *p){.
14db5 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b assert( p->lock
14db6 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ed==0 );. asser
14db7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14db8 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d _notheld(p->pBt-
14db9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14dba 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14dbb 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
14dbc 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69 utex) );.. sqli
14dbd 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
14dbe 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a p->pBt->mutex);.
14dbf 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
14dc0 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 ->db;. p->locke
14dc1 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d = 1;.}../*.**
14dc2 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 68 Release the BtSh
14dc3 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 ared mutex assoc
14dc4 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 iated with B-Tre
14dc5 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a e handle p and.*
14dc6 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c * clear the p->l
14dc7 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a ocked boolean..*
14dc8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e /.static void un
14dc9 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 lockBtreeMutex(B
14dca 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
14dcb 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 rt( p->locked==1
14dcc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
14dcd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14dce 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 (p->pBt->mutex)
14dcf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14dd0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14dd1 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14dd2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
14dd3 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a ==p->pBt->db );.
14dd4 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
14dd5 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d _leave(p->pBt->m
14dd6 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b utex);. p->lock
14dd7 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 0;.}../*.**
14dd8 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f Enter a mutex o
14dd9 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 n the given BTre
14dda 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 e object..**.**
14ddb 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 If the object is
14ddc 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74 not sharable, t
14ddd 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 hen no mutex is
14dde 65 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a ever required.**
14ddf 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
14de0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 e is a no-op. T
14de1 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75 he underlying mu
14de2 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72 tex is non-recur
14de3 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20 sive..** But we
14de4 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e 63 65 keep a reference
14de5 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e count in Btree.
14de6 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 wantToLock so th
14de7 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 e behavior.** of
14de8 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
14de9 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a is recursive..**
14dea 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 .** To avoid dea
14deb 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 dlocks, multiple
14dec 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b Btrees are lock
14ded 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f ed in the same o
14dee 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 rder.** by all d
14def 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14df0 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65 ons. The p->pNe
14df1 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 xt is a list of
14df2 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20 other.** Btrees
14df3 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 belonging to the
14df4 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
14df5 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65 onnection as the
14df6 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63 p Btree.** whic
14df7 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 h need to be loc
14df8 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 49 66 ked after p. If
14df9 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61 we cannot get a
14dfa 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 lock on.** p, t
14dfb 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b hen first unlock
14dfc 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 all of the othe
14dfd 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 rs on p->pNext,
14dfe 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72 then wait.** for
14dff 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 the lock to bec
14e00 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e ome available on
14e01 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 p, then relock
14e02 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 all of the.** su
14e03 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 bsequent Btrees
14e04 74 68 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f that desire a lo
14e05 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ck..*/.SQLITE_PR
14e06 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14e07 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 e3BtreeEnter(Btr
14e08 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20 ee *p){. Btree
14e09 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 *pLater;.. /* S
14e0a 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 ome basic sanity
14e0b 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 checking on the
14e0c 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73 Btree. The lis
14e0d 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a t of Btrees. **
14e0e 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e connected by pN
14e0f 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 73 68 ext and pPrev sh
14e10 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65 ould be in sorte
14e11 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20 d order by. **
14e12 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e Btree.pBt value.
14e13 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 All elements of
14e14 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64 the list should
14e15 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 belong to. **
14e16 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 the same connect
14e17 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64 ion. Only shared
14e18 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74 Btrees are on t
14e19 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73 he list. */. as
14e1a 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d sert( p->pNext==
14e1b 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 0 || p->pNext->p
14e1c 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 Bt>p->pBt );. a
14e1d 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d ssert( p->pPrev=
14e1e 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e =0 || p->pPrev->
14e1f 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 pBt<p->pBt );.
14e20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 assert( p->pNext
14e21 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d ==0 || p->pNext-
14e22 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 >db==p->db );.
14e23 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 assert( p->pPrev
14e24 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d ==0 || p->pPrev-
14e25 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 >db==p->db );.
14e26 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14e27 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 ble || (p->pNext
14e28 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d ==0 && p->pPrev=
14e29 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 =0) );.. /* Che
14e2a 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 ck for locking c
14e2b 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 onsistency */.
14e2c 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b assert( !p->lock
14e2d 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c ed || p->wantToL
14e2e 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ock>0 );. asser
14e2f 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c t( p->sharable |
14e30 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d | p->wantToLock=
14e31 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 =0 );.. /* We s
14e32 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
14e33 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
14e34 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14e35 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 tion */. assert
14e36 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14e37 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
14e38 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 x) );.. /* Unle
14e39 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ss the database
14e3a 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20 is sharable and
14e3b 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42 unlocked, then B
14e3c 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20 tShared.db. **
14e3d 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 should already b
14e3e 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e e set correctly.
14e3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 */. assert( (p
14e40 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 ->locked==0 && p
14e41 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 ->sharable) || p
14e42 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 ->pBt->db==p->db
14e43 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 );.. if( !p->s
14e44 68 61 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e harable ) return
14e45 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 ;. p->wantToLoc
14e46 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f k++;. if( p->lo
14e47 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a cked ) return;..
14e48 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 /* In most cas
14e49 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 es, we should be
14e4a 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 able to acquire
14e4b 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a the lock we. *
14e4c 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 * want without h
14e4d 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f aving to go thro
14e4e 75 67 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 ught the ascendi
14e4f 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f ng lock. ** pro
14e50 63 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c cedure that foll
14e51 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 ows. Just be su
14e52 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e re not to block.
14e53 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 . */. if( sqli
14e54 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d te3_mutex_try(p-
14e55 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 >pBt->mutex)==SQ
14e56 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
14e57 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 ->pBt->db = p->d
14e58 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 b;. p->locked
14e59 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
14e5a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 ;. }.. /* To a
14e5b 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 void deadlock, f
14e5c 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c irst release all
14e5d 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 locks with a la
14e5e 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 rger. ** BtShar
14e5f 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 ed address. The
14e60 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f n acquire our lo
14e61 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 ck. Then reacqu
14e62 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 ire. ** the oth
14e63 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b er BtShared lock
14e64 73 20 74 68 61 74 20 77 65 20 75 73 65 64 20 74 s that we used t
14e65 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 o hold in ascend
14e66 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a ing. ** order..
14e67 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 */. for(pLate
14e68 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 r=p->pNext; pLat
14e69 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 er; pLater=pLate
14e6a 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 r->pNext){. a
14e6b 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 ssert( pLater->s
14e6c 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 harable );. a
14e6d 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 ssert( pLater->p
14e6e 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 Next==0 || pLate
14e6f 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c r->pNext->pBt>pL
14e70 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 ater->pBt );.
14e71 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 assert( !pLater
14e72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 ->locked || pLat
14e73 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 er->wantToLock>0
14e74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 );. if( pLat
14e75 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 er->locked ){.
14e76 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d unlockBtreeM
14e77 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 utex(pLater);.
14e78 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 }. }. lockBt
14e79 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66 reeMutex(p);. f
14e7a 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 or(pLater=p->pNe
14e7b 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 xt; pLater; pLat
14e7c 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 er=pLater->pNext
14e7d 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 ){. if( pLate
14e7e 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b r->wantToLock ){
14e7f 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 . lockBtree
14e80 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 Mutex(pLater);.
14e81 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
14e82 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 75 72 * Exit the recur
14e83 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20 sive mutex on a
14e84 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Btree..*/.SQLITE
14e85 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
14e86 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
14e87 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28 Btree *p){. if(
14e88 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
14e89 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 assert( p->w
14e8a 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 antToLock>0 );.
14e8b 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b p->wantToLock
14e8c 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 --;. if( p->w
14e8d 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a antToLock==0 ){.
14e8e 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 unlockBtre
14e8f 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d eMutex(p);. }
14e90 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
14e91 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 NDEBUG./*.** Ret
14e92 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
14e93 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 69 BtShared mutex i
14e94 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74 s held on the bt
14e95 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a ree, or if the.*
14e96 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20 * B-Tree is not
14e97 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72 61 62 marked as sharab
14e98 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 le..**.** This r
14e99 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f outine is used o
14e9a 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
14e9b 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
14e9c 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts..*/.SQLITE_P
14e9d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14e9e 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
14e9f 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 x(Btree *p){. a
14ea0 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
14ea1 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b le==0 || p->lock
14ea2 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 ed==0 || p->want
14ea3 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 ToLock>0 );. as
14ea4 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14ea5 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 e==0 || p->locke
14ea6 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 d==0 || p->db==p
14ea7 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 ->pBt->db );. a
14ea8 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
14ea9 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b le==0 || p->lock
14eaa 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 ed==0 || sqlite3
14eab 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 _mutex_held(p->p
14eac 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14ead 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14eae 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 ble==0 || p->loc
14eaf 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ked==0 || sqlite
14eb0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
14eb1 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
14eb2 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 return (p->shar
14eb3 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f able==0 || p->lo
14eb4 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a cked);.}.#endif.
14eb5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
14eb6 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f _OMIT_INCRBLOB./
14eb7 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c *.** Enter and l
14eb8 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20 eave a mutex on
14eb9 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 20 a Btree given a
14eba 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20 cursor owned by
14ebb 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20 that.** Btree.
14ebc 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e These entry poin
14ebd 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20 69 ts are used by i
14ebe 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 ncremental I/O a
14ebf 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 nd can be.** omi
14ec0 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 tted if that mod
14ec1 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e ule is not used.
14ec2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14ec3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14ec4 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 treeEnterCursor(
14ec5 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
14ec6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
14ec7 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65 nter(pCur->pBtre
14ec8 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 e);.}.SQLITE_PRI
14ec9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14eca 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
14ecb 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
14ecc 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ){. sqlite3Btre
14ecd 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74 eLeave(pCur->pBt
14ece 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ree);.}.#endif /
14ecf 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e * SQLITE_OMIT_IN
14ed0 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a CRBLOB */.../*.*
14ed1 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 * Enter the mute
14ed2 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65 x on every Btree
14ed3 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
14ed4 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 a database.** c
14ed5 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 onnection. This
14ed6 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20 is needed (for
14ed7 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74 example) prior t
14ed8 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73 o parsing.** a s
14ed9 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77 tatement since w
14eda 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72 e will be compar
14edb 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f ing table and co
14edc 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 lumn names.** ag
14edd 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 ainst all schema
14ede 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 s and we do not
14edf 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d want those schem
14ee0 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65 as being.** rese
14ee1 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 t out from under
14ee2 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 us..**.** There
14ee3 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 is a correspond
14ee4 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72 ing leave-all pr
14ee5 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 ocedures..**.**
14ee6 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 Enter the mutexe
14ee7 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f s in accending o
14ee8 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65 64 rder by BtShared
14ee9 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73 pointer address
14eea 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 .** to avoid the
14eeb 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 possibility of
14eec 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77 deadlock when tw
14eed 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a o threads with.*
14eee 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74 * two or more bt
14eef 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 rees in common b
14ef0 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20 oth try to lock
14ef1 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 73 all their btrees
14ef2 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 .** at the same
14ef3 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 instant..*/.SQLI
14ef4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
14ef5 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
14ef6 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 rAll(sqlite3 *db
14ef7 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 ){. int i;. Bt
14ef8 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b ree *p, *pLater;
14ef9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14efa 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
14efb 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f ->mutex) );. fo
14efc 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
14efd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 ; i++){. p =
14efe 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
14eff 20 20 20 20 61 73 73 65 72 74 28 20 21 70 20 7c assert( !p |
14f00 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 | (p->locked==0
14f01 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 && p->sharable)
14f02 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 || p->pBt->db==p
14f03 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20 ->db );. if(
14f04 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 p && p->sharable
14f05 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e ){. p->wan
14f06 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 tToLock++;.
14f07 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 if( !p->locked
14f08 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
14f09 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b t( p->wantToLock
14f0a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 ==1 );. w
14f0b 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 hile( p->pPrev )
14f0c 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 p = p->pPrev;.
14f0d 20 20 20 20 20 20 20 2f 2a 20 52 65 61 73 6f 6e /* Reason
14f0e 20 66 6f 72 20 41 4c 57 41 59 53 3a 20 20 54 68 for ALWAYS: Th
14f0f 65 72 65 20 6d 75 73 74 20 62 65 20 61 74 20 6c ere must be at l
14f10 65 61 73 74 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64 east on unlocked
14f11 20 42 74 72 65 65 20 69 6e 0a 20 20 20 20 20 20 Btree in.
14f12 20 20 2a 2a 20 74 68 65 20 63 68 61 69 6e 2e 20 ** the chain.
14f13 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 21 Otherwise the !
14f14 70 2d 3e 6c 6f 63 6b 65 64 20 74 65 73 74 20 61 p->locked test a
14f15 62 6f 76 65 20 77 6f 75 6c 64 20 68 61 76 65 20 bove would have
14f16 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 failed */.
14f17 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b while( p->lock
14f18 65 64 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e ed && ALWAYS(p->
14f19 70 4e 65 78 74 29 20 29 20 70 20 3d 20 70 2d 3e pNext) ) p = p->
14f1a 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 pNext;. f
14f1b 6f 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 or(pLater = p->p
14f1c 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
14f1d 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
14f1e 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 xt){. i
14f1f 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 f( pLater->locke
14f20 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 d ){.
14f21 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 unlockBtreeMute
14f22 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20 x(pLater);.
14f23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
14f24 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
14f25 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c p ){. l
14f26 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 ockBtreeMutex(p)
14f27 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 ;. p =
14f28 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 p->pNext;.
14f29 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
14f2a 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 }. }.}.SQLITE_P
14f2b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14f2c 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
14f2d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
14f2e 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 int i;. Btree
14f2f 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 *p;. assert( sq
14f30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14f31 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
14f32 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
14f33 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 nDb; i++){. p
14f34 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14f35 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
14f36 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
14f37 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14f38 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
14f39 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c p->wantToL
14f3a 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 ock--;. if(
14f3b 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
14f3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 0 ){. unl
14f3d 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 ockBtreeMutex(p)
14f3e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
14f3f 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e }.}..#ifndef N
14f40 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 DEBUG./*.** Retu
14f41 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63 rn true if the c
14f42 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f urrent thread ho
14f43 6c 64 73 20 74 68 65 20 64 61 74 61 62 61 73 65 lds the database
14f44 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d connection.** m
14f45 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 utex and all req
14f46 75 69 72 65 64 20 42 74 53 68 61 72 65 64 20 6d uired BtShared m
14f47 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 utexes..**.** Th
14f48 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
14f49 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ed inside assert
14f4a 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
14f4b 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
14f4c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14f4d 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
14f4e 74 65 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 texes(sqlite3 *d
14f4f 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 b){. int i;. i
14f50 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 f( !sqlite3_mute
14f51 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
14f52 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
14f53 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 0;. }. for(i=0
14f54 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
14f55 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b ){. Btree *p;
14f56 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 . p = db->aDb
14f57 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
14f58 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c p && p->sharabl
14f59 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 e &&. (p
14f5a 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
14f5b 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 || !sqlite3_mute
14f5c 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d x_held(p->pBt->m
14f5d 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 utex)) ){.
14f5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
14f5f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
14f60 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 }.#endif /* NDEB
14f61 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 UG */../*.** Add
14f62 20 61 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 a new Btree poi
14f63 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d nter to a BtreeM
14f64 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 utexArray. .** i
14f65 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 f the pointer ca
14f66 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 n possibly be sh
14f67 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f ared with.** ano
14f68 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
14f69 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nnection..**.**
14f6a 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 The pointers are
14f6b 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 kept in sorted
14f6c 6f 72 64 65 72 20 62 79 20 70 42 74 72 65 65 2d order by pBtree-
14f6d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 >pBt. That.** w
14f6e 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f ay when we go to
14f6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d enter all the m
14f70 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 utexes, we can e
14f71 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 nter them.** in
14f72 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 order without ev
14f73 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 ery having to ba
14f74 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 ckup and retry a
14f75 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f nd without.** wo
14f76 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 rrying about dea
14f77 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 dlock..**.** The
14f78 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 number of share
14f79 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c d btrees will al
14f7a 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 ways be small (u
14f7b 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a sually 0 or 1).*
14f7c 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f * so an insertio
14f7d 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 n sort is an ade
14f7e 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 quate algorithm
14f7f 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f here..*/.SQLITE_
14f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
14f81 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
14f82 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d rayInsert(BtreeM
14f83 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 utexArray *pArra
14f84 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 y, Btree *pBtree
14f85 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
14f86 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a BtShared *pBt;.
14f87 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 if( pBtree==0
14f88 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 || pBtree->shara
14f89 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ble==0 ) return;
14f8a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
14f8b 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b {. for(i=0;
14f8c 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
14f8d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 x; i++){. a
14f8e 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 ssert( pArray->a
14f8f 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 Btree[i]!=pBtree
14f90 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 );. }. }.#e
14f91 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 ndif. assert( p
14f92 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 Array->nMutex>=0
14f93 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 );. assert( pA
14f94 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 rray->nMutex<Arr
14f95 61 79 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 aySize(pArray->a
14f96 42 74 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 Btree)-1 );. pB
14f97 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b t = pBtree->pBt;
14f98 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 . for(i=0; i<pA
14f99 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b rray->nMutex; i+
14f9a 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
14f9b 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
14f9c 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 ]!=pBtree );.
14f9d 20 69 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 if( pArray->aBt
14f9e 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 ree[i]->pBt>pBt
14f9f 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 ){. for(j=p
14fa0 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a Array->nMutex; j
14fa1 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 >i; j--){.
14fa2 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 pArray->aBtree
14fa3 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 [j] = pArray->aB
14fa4 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 tree[j-1];.
14fa5 20 7d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d }. pArray-
14fa6 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 >aBtree[i] = pBt
14fa7 72 65 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 ree;. pArra
14fa8 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 y->nMutex++;.
14fa9 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
14faa 0a 20 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 . }. pArray->a
14fab 42 74 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d Btree[pArray->nM
14fac 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 utex++] = pBtree
14fad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 ;.}../*.** Enter
14fae 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 the mutex of ev
14faf 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 ery btree in the
14fb0 20 61 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f array. This ro
14fb1 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c utine is.** call
14fb2 65 64 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e ed at the beginn
14fb3 69 6e 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 ing of sqlite3Vd
14fb4 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d beExec(). The m
14fb5 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 utexes are.** ex
14fb6 69 74 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 ited at the end
14fb7 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 of the same func
14fb8 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
14fb9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
14fba 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
14fbb 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 rayEnter(BtreeMu
14fbc 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 texArray *pArray
14fbd 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
14fbe 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d r(i=0; i<pArray-
14fbf 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 >nMutex; i++){.
14fc0 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 Btree *p = pA
14fc1 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b rray->aBtree[i];
14fc2 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 . /* Some bas
14fc3 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 ic sanity checki
14fc4 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ng */. assert
14fc5 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 ( i==0 || pArray
14fc6 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 ->aBtree[i-1]->p
14fc7 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 Bt<p->pBt );.
14fc8 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 assert( !p->loc
14fc9 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f ked || p->wantTo
14fca 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f Lock>0 );.. /
14fcb 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 * We should alre
14fcc 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 ady hold a lock
14fcd 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
14fce 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
14fcf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14fd0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
14fd1 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
14fd2 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 /* The Btree
14fd3 69 73 20 73 68 61 72 61 62 6c 65 20 62 65 63 61 is sharable beca
14fd4 75 73 65 20 6f 6e 6c 79 20 73 68 61 72 61 62 6c use only sharabl
14fd5 65 20 42 74 72 65 65 73 20 61 72 65 20 65 6e 74 e Btrees are ent
14fd6 65 72 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f ered. ** into
14fd7 20 74 68 65 20 61 72 72 61 79 20 69 6e 20 74 68 the array in th
14fd8 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a e first place. *
14fd9 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d /. assert( p-
14fda 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 0a 20 20 >sharable );..
14fdb 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b p->wantToLock+
14fdc 2b 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c +;. if( !p->l
14fdd 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 6c ocked ){. l
14fde 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 ockBtreeMutex(p)
14fdf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
14fe0 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d *.** Leave the m
14fe1 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
14fe2 72 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 ree in the group
14fe3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14fe4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14fe5 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c BtreeMutexArrayL
14fe6 65 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 eave(BtreeMutexA
14fe7 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 rray *pArray){.
14fe8 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14fe9 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 0; i<pArray->nMu
14fea 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 tex; i++){. B
14feb 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 tree *p = pArray
14fec 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 ->aBtree[i];.
14fed 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 /* Some basic s
14fee 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
14fef 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d /. assert( i=
14ff0 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 =0 || pArray->aB
14ff1 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 tree[i-1]->pBt<p
14ff2 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
14ff3 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 ert( p->locked )
14ff4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
14ff5 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
14ff6 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 .. /* We shou
14ff7 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 ld already hold
14ff8 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 a lock on the da
14ff9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14ffa 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 n */. assert(
14ffb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14ffc 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
14ffd 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e ) );.. p->wan
14ffe 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 tToLock--;. i
14fff 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b f( p->wantToLock
15000 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c ==0 ){. unl
15001 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 ockBtreeMutex(p)
15002 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
15003 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 else.SQLITE_PRIV
15004 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
15005 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 BtreeEnter(Btree
15006 20 2a 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e *p){. p->pBt->
15007 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 db = p->db;.}.SQ
15008 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
15009 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
1500a 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a terAll(sqlite3 *
1500b 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 db){. int i;.
1500c 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
1500d 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 Db; i++){. Bt
1500e 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 ree *p = db->aDb
1500f 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
15010 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 p ){. p->p
15011 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
15012 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 }. }.}.#end
15013 69 66 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f if /* if SQLITE_
15014 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65 THREADSAFE */.#e
15015 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 ndif /* ifndef S
15016 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15017 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a D_CACHE */../***
15018 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
15019 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a of btmutex.c ***
1501a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1501b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1501c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1501d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1501e 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a n file btree.c *
1501f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
15022 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a * 2004 April 6.*
15023 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
15024 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
15025 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
15026 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
15027 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
15028 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
15029 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1502a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1502b 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1502c 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1502d 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1502e 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1502f 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
15030 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
15031 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
15032 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
15033 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
15034 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
15035 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15036 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15037 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
15039 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
1503a 6d 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c ments a external
1503b 20 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 (disk-based) da
1503c 74 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 tabase using BTr
1503d 65 65 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 ees..** See the
1503e 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f header comment o
1503f 6e 20 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66 n "btreeInt.h" f
15040 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
15041 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e formation..** In
15042 63 6c 75 64 69 6e 67 20 61 20 64 65 73 63 72 69 cluding a descri
15043 70 74 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f ption of file fo
15044 72 6d 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 rmat and an over
15045 76 69 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f view of operatio
15046 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 n..*/../*.** The
15047 20 68 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 header string t
15048 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 hat appears at t
15049 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
1504a 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 every.** SQLite
1504b 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 database..*/.sta
1504c 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a tic const char z
1504d 4d 61 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 MagicHeader[] =
1504e 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 SQLITE_FILE_HEAD
1504f 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ER;../*.** Set t
15050 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 his global varia
15051 62 6c 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 ble to 1 to enab
15052 6c 65 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 le tracing using
15053 20 74 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 the TRACE.** ma
15054 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e cro..*/.#if 0.in
15055 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 t sqlite3BtreeTr
15056 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 ace=1; /* True
15057 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e to enable tracin
15058 67 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52 g */.# define TR
15059 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 ACE(X) if(sqlit
1505a 65 33 42 74 72 65 65 54 72 61 63 65 29 7b 70 72 e3BtreeTrace){pr
1505b 69 6e 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74 intf X;fflush(st
1505c 64 6f 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 dout);}.#else.#
1505d 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a define TRACE(X).
1505e 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 #endif....#ifnde
1505f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
15060 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
15061 20 41 20 6c 69 73 74 20 6f 66 20 42 74 53 68 61 A list of BtSha
15062 72 65 64 20 6f 62 6a 65 63 74 73 20 74 68 61 74 red objects that
15063 20 61 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f are eligible fo
15064 72 20 70 61 72 74 69 63 69 70 61 74 69 6f 6e 0a r participation.
15065 2a 2a 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 ** in shared cac
15066 68 65 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 he. This variab
15067 6c 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 le has file scop
15068 65 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 e during normal
15069 62 75 69 6c 64 73 2c 0a 2a 2a 20 62 75 74 20 74 builds,.** but t
1506a 68 65 20 74 65 73 74 20 68 61 72 6e 65 73 73 20 he test harness
1506b 6e 65 65 64 73 20 74 6f 20 61 63 63 65 73 73 20 needs to access
1506c 69 74 20 73 6f 20 77 65 20 6d 61 6b 65 20 69 74 it so we make it
1506d 20 67 6c 6f 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 global for .**
1506e 74 65 73 74 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a test builds..**.
1506f 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 74 68 69 ** Access to thi
15070 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 70 72 s variable is pr
15071 6f 74 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 otected by SQLIT
15072 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
15073 41 53 54 45 52 2e 0a 2a 2f 0a 23 69 66 64 65 66 ASTER..*/.#ifdef
15074 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
15075 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 ITE_PRIVATE BtSh
15076 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 ared *SQLITE_WSD
15077 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 sqlite3SharedCa
15078 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c cheList = 0;.#el
15079 73 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 se.static BtShar
1507a 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 ed *SQLITE_WSD s
1507b 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
1507c 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 eList = 0;.#endi
1507d 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 f.#endif /* SQLI
1507e 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1507f 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ACHE */..#ifndef
15080 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
15081 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 RED_CACHE./*.**
15082 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c Enable or disabl
15083 65 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67 e the shared pag
15084 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 er and schema fe
15085 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 atures..**.** Th
15086 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e is routine has n
15087 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 o effect on exis
15088 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f ting database co
15089 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 nnections..** Th
1508a 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73 e shared cache s
1508b 65 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f etting effects o
1508c 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 nly future calls
1508d 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f to.** sqlite3_o
1508e 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f pen(), sqlite3_o
1508f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 pen16(), or sqli
15090 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a te3_open_v2()..*
15091 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
15092 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
15093 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 shared_cache(int
15094 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 enable){. sqli
15095 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
15096 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c sharedCacheEnabl
15097 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 ed = enable;. r
15098 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15099 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 .}.#endif....#if
1509a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1509b 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f SHARED_CACHE. /
1509c 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74 *. ** The funct
1509d 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72 65 64 ions queryShared
1509e 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
1509f 2c 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 , setSharedCache
150a0 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a TableLock(),. *
150a1 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53 68 * and clearAllSh
150a2 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
150a3 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 cks(). ** manip
150a4 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e ulate entries in
150a5 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c the BtShared.pL
150a6 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 ock linked list
150a7 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20 used to store.
150a8 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 ** shared-cache
150a9 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b table level lock
150aa 73 2e 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 s. If the librar
150ab 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 y is compiled wi
150ac 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72 th the. ** shar
150ad 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 ed-cache feature
150ae 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 disabled, then
150af 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 there is only ev
150b0 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a er one user. **
150b1 20 6f 66 20 65 61 63 68 20 42 74 53 68 61 72 65 of each BtShare
150b2 64 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 d structure and
150b3 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 so this locking
150b4 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
150b5 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e . . ** So defin
150b6 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74 e the lock relat
150b7 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 ed functions as
150b8 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 no-ops.. */. #
150b9 64 65 66 69 6e 65 20 71 75 65 72 79 53 68 61 72 define queryShar
150ba 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
150bb 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f (a,b,c) SQLITE_O
150bc 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 65 74 53 K. #define setS
150bd 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
150be 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 ock(a,b,c) SQLIT
150bf 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 63 E_OK. #define c
150c0 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
150c1 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a heTableLocks(a).
150c2 20 20 23 64 65 66 69 6e 65 20 64 6f 77 6e 67 72 #define downgr
150c3 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 adeAllSharedCach
150c4 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 eTableLocks(a).
150c5 20 23 64 65 66 69 6e 65 20 68 61 73 53 68 61 72 #define hasShar
150c6 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
150c7 28 61 2c 62 2c 63 2c 64 29 20 31 0a 20 20 23 64 (a,b,c,d) 1. #d
150c8 65 66 69 6e 65 20 68 61 73 52 65 61 64 43 6f 6e efine hasReadCon
150c9 66 6c 69 63 74 73 28 61 2c 20 62 29 20 30 0a 23 flicts(a, b) 0.#
150ca 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
150cb 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
150cc 44 5f 43 41 43 48 45 0a 0a 23 69 66 64 65 66 20 D_CACHE..#ifdef
150cd 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
150ce 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 **** This functi
150cf 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 on is only used
150d0 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 as part of an as
150d1 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
150d2 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 . ***.**.** Chec
150d3 6b 20 74 6f 20 73 65 65 20 69 66 20 70 42 74 72 k to see if pBtr
150d4 65 65 20 68 6f 6c 64 73 20 74 68 65 20 72 65 71 ee holds the req
150d5 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 72 uired locks to r
150d6 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f 20 ead or write to
150d7 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65 20 77 69 the .** table wi
150d8 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69 52 6f th root page iRo
150d9 6f 74 2e 20 20 20 52 65 74 75 72 6e 20 31 20 69 ot. Return 1 i
150da 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 30 20 f it does and 0
150db 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f if not..**.** Fo
150dc 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 r example, when
150dd 77 72 69 74 69 6e 67 20 74 6f 20 61 20 74 61 62 writing to a tab
150de 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 le with root-pag
150df 65 20 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 e iRoot via .**
150e0 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e Btree connection
150e1 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 pBtree:.**.**
150e2 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
150e3 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
150e4 6b 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c k(pBtree, iRoot,
150e5 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 0, WRITE_LOCK)
150e6 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 );.**.** When wr
150e7 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 iting to an inde
150e8 78 20 74 68 61 74 20 72 65 73 69 64 65 73 20 69 x that resides i
150e9 6e 20 61 20 73 68 61 72 61 62 6c 65 20 64 61 74 n a sharable dat
150ea 61 62 61 73 65 2c 20 74 68 65 20 0a 2a 2a 20 63 abase, the .** c
150eb 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76 aller should hav
150ec 65 20 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64 e first obtained
150ed 20 61 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69 a lock specifyi
150ee 6e 67 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ng the root page
150ef 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 of.** the corre
150f0 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20 sponding table.
150f1 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67 This makes thing
150f2 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d s a bit more com
150f3 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 plicated,.** as
150f4 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61 this module trea
150f5 74 73 20 65 61 63 68 20 74 61 62 6c 65 20 61 73 ts each table as
150f6 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72 75 a separate stru
150f7 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72 6d cture. To determ
150f8 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 ine.** the table
150f9 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
150fa 6f 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e o the index bein
150fb 67 20 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a g written, this.
150fc 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 ** function has
150fd 74 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 to search throug
150fe 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 h the database s
150ff 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 chema..**.** Ins
15100 74 65 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f tead of a lock o
15101 6e 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 n the table/inde
15102 78 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 x rooted at page
15103 20 69 52 6f 6f 74 2c 20 74 68 65 20 63 61 6c 6c iRoot, the call
15104 65 72 20 6d 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 er may.** hold a
15105 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 write-lock on t
15106 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 20 he schema table
15107 28 72 6f 6f 74 20 70 61 67 65 20 31 29 2e 20 54 (root page 1). T
15108 68 69 73 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 his is also.** a
15109 63 63 65 70 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 cceptable..*/.st
1510a 61 74 69 63 20 69 6e 74 20 68 61 73 53 68 61 72 atic int hasShar
1510b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1510c 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 (. Btree *pBtre
1510d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 e, /* Ha
1510e 6e 64 6c 65 20 74 68 61 74 20 6d 75 73 74 20 68 ndle that must h
1510f 6f 6c 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 old lock */. Pg
15110 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 20 20 20 20 no iRoot,
15111 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
15112 65 20 6f 66 20 62 2d 74 72 65 65 20 2a 2f 0a 20 e of b-tree */.
15113 20 69 6e 74 20 69 73 49 6e 64 65 78 2c 20 20 20 int isIndex,
15114 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
15115 69 66 20 69 52 6f 6f 74 20 69 73 20 74 68 65 20 if iRoot is the
15116 72 6f 6f 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 root of an index
15117 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 b-tree */. int
15118 20 65 4c 6f 63 6b 54 79 70 65 20 20 20 20 20 20 eLockType
15119 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65 64 20 /* Required
1511a 6c 6f 63 6b 20 74 79 70 65 20 28 52 45 41 44 5f lock type (READ_
1511b 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f LOCK or WRITE_LO
1511c 43 4b 29 20 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 CK) */.){. Sche
1511d 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 ma *pSchema = (S
1511e 63 68 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e chema *)pBtree->
1511f 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 pBt->pSchema;.
15120 50 67 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0a 20 Pgno iTab = 0;.
15121 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a BtLock *pLock;.
15122 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 . /* If this da
15123 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 73 68 tabase is not sh
15124 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 areable, or if t
15125 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 he client is rea
15126 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 ding. ** and ha
15127 73 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d s the read-uncom
15128 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c mitted flag set,
15129 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 then no lock is
1512a 20 72 65 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a required. . **
1512b 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d Return true imm
1512c 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 ediately.. */.
1512d 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 if( (pBtree->sh
1512e 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c arable==0). ||
1512f 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 (eLockType==REA
15130 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 D_LOCK && (pBtre
15131 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 e->db->flags & S
15132 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
15133 69 74 74 65 64 29 29 0a 20 20 29 7b 0a 20 20 20 itted)). ){.
15134 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a return 1;. }..
15135 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6c 69 65 /* If the clie
15136 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 20 6f nt is reading o
15137 72 20 77 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 r writing an ind
15138 65 78 20 61 6e 64 20 74 68 65 20 73 63 68 65 6d ex and the schem
15139 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f a is. ** not lo
1513a 61 64 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 aded, then it is
1513b 20 74 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74 too difficult t
1513c 6f 20 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b o actually check
1513d 20 74 6f 20 73 65 65 20 69 66 0a 20 20 2a 2a 20 to see if. **
1513e 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b the correct lock
1513f 73 20 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 s are held. So
15140 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 do not bother -
15141 6a 75 73 74 20 72 65 74 75 72 6e 20 74 72 75 65 just return true
15142 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65 .. ** This case
15143 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75 does not come u
15144 70 20 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79 p very often any
15145 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 how.. */. if(
15146 69 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 isIndex && (!pSc
15147 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 hema || (pSchema
15148 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d ->flags&DB_Schem
15149 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20 29 7b 0a aLoaded)==0) ){.
1514a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1514b 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f }.. /* Figure o
1514c 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 ut the root-page
1514d 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 73 that the lock s
1514e 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 6f 6e hould be held on
1514f 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 20 2a 2a . For table. **
15150 20 62 2d 74 72 65 65 73 2c 20 74 68 69 73 20 69 b-trees, this i
15151 73 20 6a 75 73 74 20 74 68 65 20 72 6f 6f 74 20 s just the root
15152 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 page of the b-tr
15153 65 65 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 ee being read or
15154 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e 2e 20 46 . ** written. F
15155 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 65 65 73 or index b-trees
15156 2c 20 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 , it is the root
15157 20 70 61 67 65 20 6f 66 20 74 68 65 20 61 73 73 page of the ass
15158 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 74 61 62 ociated. ** tab
15159 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 le. */. if( is
1515a 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 48 61 73 Index ){. Has
1515b 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f hElem *p;. fo
1515c 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 r(p=sqliteHashFi
1515d 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 rst(&pSchema->id
1515e 78 48 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c xHash); p; p=sql
1515f 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b iteHashNext(p)){
15160 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 . Index *pI
15161 64 78 20 3d 20 28 49 6e 64 65 78 20 2a 29 73 71 dx = (Index *)sq
15162 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b liteHashData(p);
15163 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d . if( pIdx-
15164 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f >tnum==(int)iRoo
15165 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54 61 t ){. iTa
15166 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 b = pIdx->pTable
15167 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a ->tnum;. }.
15168 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
15169 20 20 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b iTab = iRoot;
1516a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 . }.. /* Searc
1516b 68 20 66 6f 72 20 74 68 65 20 72 65 71 75 69 72 h for the requir
1516c 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20 ed lock. Either
1516d 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
1516e 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20 root-page iTab,
1516f 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f a . ** write-lo
15170 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 ck on the schema
15171 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74 table, or (if t
15172 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 he client is rea
15173 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61 ding) a. ** rea
15174 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77 d-lock on iTab w
15175 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74 ill suffice. Ret
15176 75 72 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20 urn 1 if any of
15177 74 68 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e these are found.
15178 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b */. for(pLock
15179 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c =pBtree->pBt->pL
1517a 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 ock; pLock; pLoc
1517b 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b k=pLock->pNext){
1517c 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e . if( pLock->
1517d 70 42 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a pBtree==pBtree .
1517e 20 20 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e && (pLock->
1517f 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 iTable==iTab ||
15180 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 (pLock->eLock==W
15181 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f RITE_LOCK && pLo
15182 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a ck->iTable==1)).
15183 20 20 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 && pLock->e
15184 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 Lock>=eLockType
15185 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 . ){. re
15186 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
15187 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 }.. /* Failed t
15188 6f 20 66 69 6e 64 20 74 68 65 20 72 65 71 75 69 o find the requi
15189 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 red lock. */. r
1518a 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 eturn 0;.}.#endi
1518b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
1518c 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c G */..#ifdef SQL
1518d 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a ITE_DEBUG./*.***
1518e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1518f 6d 61 79 20 62 65 20 75 73 65 64 20 61 73 20 70 may be used as p
15190 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 art of assert()
15191 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e statements only.
15192 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 ****.**.** Retu
15193 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 77 6f rn true if it wo
15194 75 6c 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66 uld be illegal f
15195 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 or pBtree to wri
15196 74 65 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 te into the.** t
15197 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f able or index ro
15198 6f 74 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65 oted at iRoot be
15199 63 61 75 73 65 20 6f 74 68 65 72 20 73 68 61 72 cause other shar
1519a 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 ed connections a
1519b 72 65 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f re.** simultaneo
1519c 75 73 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61 usly reading tha
1519d 74 20 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 t same table or
1519e 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 index..**.** It
1519f 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 is illegal for p
151a0 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 Btree to write i
151a1 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 f some other Btr
151a2 65 65 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a ee object that.*
151a3 2a 20 73 68 61 72 65 73 20 74 68 65 20 73 61 6d * shares the sam
151a4 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
151a5 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 t is currently r
151a6 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
151a7 67 0a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74 g.** the iRoot t
151a8 61 62 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69 able. Except, i
151a9 66 20 74 68 65 20 6f 74 68 65 72 20 42 74 72 65 f the other Btre
151aa 65 20 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65 e object has the
151ab 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 .** read-uncommi
151ac 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 tted flag set, t
151ad 68 65 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 hen it is OK for
151ae 20 74 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63 the other objec
151af 74 20 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72 t to.** have a r
151b0 65 61 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a ead cursor..**.*
151b1 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 * For example, b
151b2 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f efore writing to
151b3 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 any part of the
151b4 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a table or index.
151b5 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 ** rooted at pag
151b6 65 20 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f e iRoot, one sho
151b7 75 6c 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 uld call:.**.**
151b8 20 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 assert( !hasR
151b9 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74 eadConflicts(pBt
151ba 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a ree, iRoot) );.*
151bb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 /.static int has
151bc 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 ReadConflicts(Bt
151bd 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e ree *pBtree, Pgn
151be 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75 o iRoot){. BtCu
151bf 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 rsor *p;. for(p
151c0 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 =pBtree->pBt->pC
151c1 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
151c2 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
151c3 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f ->pgnoRoot==iRoo
151c4 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 t . && p->pB
151c5 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20 tree!=pBtree.
151c6 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 && 0==(p->pBtr
151c7 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 ee->db->flags &
151c8 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
151c9 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b 0a 20 mitted). ){.
151ca 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
151cb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
151cc 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 n 0;.}.#endif
151cd 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 /* #ifdef SQLIT
151ce 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a E_DEBUG */../*.*
151cf 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 * Query to see i
151d0 66 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 f Btree handle p
151d1 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f may obtain a lo
151d2 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b ck of type eLock
151d3 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 .** (READ_LOCK
151d4 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f or WRITE_LOCK) o
151d5 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 n the table with
151d6 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e root-page iTab.
151d7 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 Return.** SQLIT
151d8 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b E_OK if the lock
151d9 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 may be obtained
151da 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 (by calling.**
151db 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
151dc 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 bleLock()), or S
151dd 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 QLITE_LOCKED if
151de 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 not..*/.static i
151df 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 43 61 nt querySharedCa
151e0 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 cheTableLock(Btr
151e1 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 ee *p, Pgno iTab
151e2 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 , u8 eLock){. B
151e3 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
151e4 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 ->pBt;. BtLock
151e5 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 *pIter;.. asser
151e6 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
151e7 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
151e8 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
151e9 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c =READ_LOCK || eL
151ea 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
151eb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
151ec 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 db!=0 );. asser
151ed 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 t( !(p->db->flag
151ee 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 s&SQLITE_ReadUnc
151ef 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f 63 6b ommitted)||eLock
151f0 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 ==WRITE_LOCK||iT
151f1 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 2f 2a ab==1 );. . /*
151f2 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 If requesting a
151f3 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 write-lock, the
151f4 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 n the Btree must
151f5 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 have an open wr
151f6 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 ite. ** transac
151f7 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c tion on this fil
151f8 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c e. And, obviousl
151f9 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 y, for this to b
151fa 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a e so there . **
151fb 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e must be an open
151fc 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
151fd 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 on on the file i
151fe 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 tself.. */. as
151ff 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 sert( eLock==REA
15200 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 D_LOCK || (p==pB
15201 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d t->pWriter && p-
15202 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
15203 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 WRITE) );. asse
15204 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f rt( eLock==READ_
15205 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 LOCK || pBt->inT
15206 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
15207 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 S_WRITE );. .
15208 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 /* This routine
15209 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 is a no-op if th
1520a 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 e shared-cache i
1520b 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f s not enabled */
1520c 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 . if( !p->shara
1520d 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ble ){. retur
1520e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1520f 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f .. /* If some o
15210 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
15211 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 is holding an ex
15212 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 clusive lock, th
15213 65 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 e. ** requested
15214 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 lock may not be
15215 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a obtained.. */.
15216 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 if( pBt->pWrit
15217 65 72 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73 er!=p && pBt->is
15218 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 Exclusive ){.
15219 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
1521a 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c onBlocked(p->db,
1521b 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 pBt->pWriter->d
1521c 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 b);. return S
1521d 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
1521e 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 REDCACHE;. }..
1521f 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
15220 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
15221 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
15222 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f ){. /* The co
15223 6e 64 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e ndition (pIter->
15224 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e eLock!=eLock) in
15225 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
15226 66 28 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 f(...) . ** s
15227 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 tatement is a si
15228 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a mplification of:
15229 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
1522a 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c (eLock==WRITE_L
1522b 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c OCK || pIter->eL
1522c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 ock==WRITE_LOCK)
1522d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 . **. ** s
1522e 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 ince we know tha
1522f 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 t if eLock==WRIT
15230 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 E_LOCK, then no
15231 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
15232 0a 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 . ** may hold
15233 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e a WRITE_LOCK on
15234 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 any table in th
15235 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 is file (since t
15236 68 65 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 here can. **
15237 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
15238 20 77 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f writer).. */
15239 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 . assert( pIt
1523a 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f er->eLock==READ_
1523b 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 LOCK || pIter->e
1523c 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1523d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1523e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
1523f 20 7c 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 || pIter->pBtre
15240 65 3d 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 e==p || pIter->e
15241 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 Lock==READ_LOCK)
15242 3b 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d ;. if( pIter-
15243 3e 70 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 >pBtree!=p && pI
15244 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
15245 62 20 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 b && pIter->eLoc
15246 6b 21 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 k!=eLock ){.
15247 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 sqlite3Connect
15248 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 ionBlocked(p->db
15249 2c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d , pIter->pBtree-
1524a 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 >db);. if(
1524b 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
1524c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 K ){. ass
1524d 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 ert( p==pBt->pWr
1524e 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 iter );.
1524f 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d pBt->isPending =
15250 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
15251 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15252 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
15253 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 HE;. }. }.
15254 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15255 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
15256 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15257 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
15258 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15259 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
1525a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e ** Add a lock on
1525b 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 the table with
1525c 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 root-page iTable
1525d 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 to the shared-b
1525e 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 tree used.** by
1525f 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 Btree handle p.
15260 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 Parameter eLock
15261 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 52 must be either R
15262 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 EAD_LOCK or .**
15263 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a WRITE_LOCK..**.*
15264 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
15265 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c assumes the foll
15266 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 owing:.**.** (
15267 61 29 20 54 68 65 20 73 70 65 63 69 66 69 65 64 a) The specified
15268 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 70 20 Btree object p
15269 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 is connected to
1526a 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 20 a sharable.**
1526b 20 20 20 20 64 61 74 61 62 61 73 65 20 28 6f 6e database (on
1526c 65 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 e with the BtSha
1526d 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 6c 61 red.sharable fla
1526e 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a g set), and.**.*
1526f 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72 * (b) No other
15270 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 68 Btree objects h
15271 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 old a lock that
15272 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 20 conflicts.**
15273 20 20 20 77 69 74 68 20 74 68 65 20 72 65 71 75 with the requ
15274 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 2e ested lock (i.e.
15275 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
15276 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 73 eTableLock() has
15277 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 64 .** alread
15278 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e y been called an
15279 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 d returned SQLIT
1527a 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c E_OK)..**.** SQL
1527b 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1527c 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 ed if the lock i
1527d 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66 s added successf
1527e 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d ully. SQLITE_NOM
1527f 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e EM .** is return
15280 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 61 ed if a malloc a
15281 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a 2f ttempt fails..*/
15282 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 53 .static int setS
15283 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15284 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 ock(Btree *p, Pg
15285 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c no iTable, u8 eL
15286 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ock){. BtShared
15287 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15288 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 BtLock *pLock
15289 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 = 0;. BtLock *p
1528a 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 Iter;.. assert(
1528b 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1528c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
1528d 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
1528e 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 EAD_LOCK || eLoc
1528f 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
15290 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
15291 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 63 !=0 );.. /* A c
15292 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 74 onnection with t
15293 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 he read-uncommit
15294 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 69 6c ted flag set wil
15295 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f 0a 20 l never try to.
15296 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 61 ** obtain a rea
15297 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 69 d-lock using thi
15298 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 s function. The
15299 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f only read-lock o
1529a 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 20 btained. ** by
1529b 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 a connection in
1529c 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 read-uncommitted
1529d 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 20 mode is on the
1529e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a 20 sqlite_master .
1529f 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 ** table, and t
152a0 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 hat lock is obta
152a1 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 65 67 ined in BtreeBeg
152a2 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a 20 inTrans(). */.
152a3 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d 3e assert( 0==(p->
152a4 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
152a5 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
152a6 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 ) || eLock==WRIT
152a7 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 E_LOCK );.. /*
152a8 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
152a9 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c ould only be cal
152aa 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 62 6c led on a sharabl
152ab 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 20 69 e b-tree after i
152ac 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 6e t . ** has been
152ad 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 determined that
152ae 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 65 no other b-tree
152af 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 63 holds a conflic
152b0 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 ting lock. */.
152b1 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
152b2 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 able );. assert
152b3 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 ( SQLITE_OK==que
152b4 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
152b5 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 leLock(p, iTable
152b6 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f , eLock) );.. /
152b7 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20 74 * First search t
152b8 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65 he list for an e
152b9 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 xisting lock on
152ba 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 this table. */.
152bb 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
152bc 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
152bd 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
152be 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 ){. if( pIter
152bf 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 ->iTable==iTable
152c0 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65 && pIter->pBtre
152c1 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c e==p ){. pL
152c2 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20 ock = pIter;.
152c3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
152c4 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
152c5 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 above search di
152c6 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c d not find a BtL
152c7 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 ock struct assoc
152c8 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20 iating Btree p.
152c9 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 ** with table i
152ca 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 Table, allocate
152cb 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 one and link it
152cc 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20 into the list..
152cd 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b */. if( !pLock
152ce 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 ){. pLock =
152cf 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 (BtLock *)sqlite
152d0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
152d1 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 of(BtLock));.
152d2 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 if( !pLock ){.
152d3 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
152d4 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
152d5 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c pLock->iTabl
152d6 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 e = iTable;.
152d7 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20 pLock->pBtree =
152d8 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e p;. pLock->pN
152d9 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b ext = pBt->pLock
152da 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b ;. pBt->pLock
152db 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 = pLock;. }..
152dc 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f /* Set the BtLo
152dd 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c ck.eLock variabl
152de 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d e to the maximum
152df 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
152e0 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68 lock. ** and th
152e1 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
152e2 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 . This means if
152e3 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 a write-lock was
152e4 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20 already held.
152e5 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f ** and a read-lo
152e6 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 ck requested, we
152e7 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 don't incorrect
152e8 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 ly downgrade the
152e9 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 lock.. */. as
152ea 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b sert( WRITE_LOCK
152eb 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 >READ_LOCK );.
152ec 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d if( eLock>pLock-
152ed 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c >eLock ){. pL
152ee 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f ock->eLock = eLo
152ef 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 ck;. }.. retur
152f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
152f1 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 endif /* !SQLITE
152f2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
152f3 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 HE */..#ifndef S
152f4 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
152f5 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 D_CACHE./*.** Re
152f6 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 74 61 lease all the ta
152f7 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 ble locks (locks
152f8 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 63 61 obtained via ca
152f9 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73 65 lls to.** the se
152fa 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
152fb 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 64 75 72 eLock() procedur
152fc 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 e) held by Btree
152fd 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a object p..**.**
152fe 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
152ff 73 73 75 6d 65 73 20 74 68 61 74 20 42 74 72 65 ssumes that Btre
15300 65 20 70 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 e p has an open
15301 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 0a 2a read or write .*
15302 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 * transaction. I
15303 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 74 f it does not, t
15304 68 65 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 hen the BtShared
15305 2e 69 73 50 65 6e 64 69 6e 67 20 76 61 72 69 61 .isPending varia
15306 62 6c 65 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e ble.** may be in
15307 63 6f 72 72 65 63 74 6c 79 20 63 6c 65 61 72 65 correctly cleare
15308 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
15309 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 d clearAllShared
1530a 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 CacheTableLocks(
1530b 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
1530c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1530d 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a pBt;. BtLock **
1530e 70 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 ppIter = &pBt->p
1530f 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 Lock;.. assert(
15310 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
15311 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
15312 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
15313 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 ble || 0==*ppIte
15314 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
15315 2d 3e 69 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a ->inTrans>0 );..
15316 20 20 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72 while( *ppIter
15317 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a ){. BtLock *
15318 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b pLock = *ppIter;
15319 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
1531a 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 ->isExclusive==0
1531b 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 || pBt->pWriter
1531c 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 ==pLock->pBtree
1531d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1531e 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e Lock->pBtree->in
1531f 54 72 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c Trans>=pLock->eL
15320 6f 63 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ock );. if( p
15321 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 Lock->pBtree==p
15322 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 ){. *ppIter
15323 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b = pLock->pNext;
15324 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
15325 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 Lock->iTable!=1
15326 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f || pLock==&p->lo
15327 63 6b 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ck );. if(
15328 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 pLock->iTable!=1
15329 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1532a 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b te3_free(pLock);
1532b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1532c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 74 65 72 se{. ppIter
1532d 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 = &pLock->pNext
1532e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 ;. }. }.. a
1532f 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 50 65 ssert( pBt->isPe
15330 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70 42 74 2d nding==0 || pBt-
15331 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 69 66 >pWriter );. if
15332 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d ( pBt->pWriter==
15333 70 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 57 p ){. pBt->pW
15334 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 riter = 0;. p
15335 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 Bt->isExclusive
15336 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 = 0;. pBt->is
15337 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d Pending = 0;. }
15338 65 6c 73 65 20 69 66 28 20 70 42 74 2d 3e 6e 54 else if( pBt->nT
15339 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29 7b ransaction==2 ){
1533a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e . /* This fun
1533b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
1533c 77 68 65 6e 20 42 74 72 65 65 20 70 20 69 73 20 when Btree p is
1533d 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a concluding its .
1533e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
1533f 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 on. If there cur
15340 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 rently exists a
15341 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 writer, and p is
15342 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 not. ** that
15343 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 writer, then th
15344 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b e number of lock
15345 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 s held by connec
15346 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 tions other.
15347 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 ** than the writ
15348 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 er must be about
15349 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f to drop to zero
1534a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 . In this case.
1534b 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 ** set the is
1534c 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 Pending flag to
1534d 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 0.. **. **
1534e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 If there is not
1534f 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 currently a wri
15350 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 ter, then BtShar
15351 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 ed.isPending mus
15352 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f t. ** be zero
15353 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 already. So thi
15354 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 s next line is h
15355 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 armless in that
15356 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
15357 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
15358 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
15359 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1535a 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 changes all writ
1535b 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 e-locks held by
1535c 42 74 72 65 65 20 70 20 69 6e 74 6f 20 72 65 61 Btree p into rea
1535d 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 d-locks..*/.stat
1535e 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 ic void downgrad
1535f 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 eAllSharedCacheT
15360 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 ableLocks(Btree
15361 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
15362 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
15363 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 if( pBt->pWrite
15364 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f r==p ){. BtLo
15365 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 ck *pLock;. p
15366 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b Bt->pWriter = 0;
15367 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c . pBt->isExcl
15368 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 usive = 0;. p
15369 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 Bt->isPending =
1536a 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 0;. for(pLock
1536b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f =pBt->pLock; pLo
1536c 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d ck; pLock=pLock-
1536d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 >pNext){. a
1536e 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c ssert( pLock->eL
1536f 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
15370 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d | pLock->pBtree=
15371 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 =p );. pLoc
15372 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f k->eLock = READ_
15373 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a LOCK;. }. }.
15374 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
15375 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15376 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 CACHE */..static
15377 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 void releasePag
15378 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
15379 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 ); /* Forward r
1537a 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a eference */../*.
1537b 2a 2a 2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 ***** This routi
1537c 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 ne is used insid
1537d 65 20 6f 66 20 61 73 73 65 72 74 28 29 20 6f 6e e of assert() on
1537e 6c 79 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 ly ****.**.** Ve
1537f 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75 rify that the cu
15380 72 73 6f 72 20 68 6f 6c 64 73 20 74 68 65 20 6d rsor holds the m
15381 75 74 65 78 20 6f 6e 20 69 74 73 20 42 74 53 68 utex on its BtSh
15382 61 72 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ared.*/.#ifdef S
15383 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 QLITE_DEBUG.stat
15384 69 63 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c ic int cursorHol
15385 64 73 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 dsMutex(BtCursor
15386 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 *p){. return s
15387 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15388 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 d(p->pBt->mutex)
15389 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 ;.}.#endif...#if
1538a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1538b 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 _INCRBLOB./*.**
1538c 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f Invalidate the o
1538d 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
1538e 74 20 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 t cache for curs
1538f 6f 72 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e or pCur, if any.
15390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15391 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
15392 6f 77 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 owCache(BtCursor
15393 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 *pCur){. asser
15394 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
15395 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 tex(pCur) );. s
15396 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 qlite3_free(pCur
15397 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 ->aOverflow);.
15398 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
15399 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e = 0;.}../*.** In
1539a 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 validate the ove
1539b 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
1539c 63 61 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 cache for all cu
1539d 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 rsors opened.**
1539e 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 62 74 on the shared bt
1539f 72 65 65 20 73 74 72 75 63 74 75 72 65 20 70 42 ree structure pB
153a0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
153a1 64 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f d invalidateAllO
153a2 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 verflowCache(BtS
153a3 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 hared *pBt){. B
153a4 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 tCursor *p;. as
153a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
153a6 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
153a7 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d tex) );. for(p=
153a8 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
153a9 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
153aa 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 invalidateOver
153ab 66 6c 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 flowCache(p);.
153ac 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
153ad 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
153ae 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 ed before modify
153af 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ing the contents
153b0 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 of a table.** t
153b1 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 o invalidate any
153b2 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 incrblob cursor
153b3 73 20 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 s that are open
153b4 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 on the.** row or
153b5 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 73 one of the rows
153b6 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e being modified.
153b7 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 .**.** If argume
153b8 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 nt isClearTable
153b9 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
153ba 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 e entire content
153bb 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c s of the.** tabl
153bc 65 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 e is about to be
153bd 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 deleted. In thi
153be 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 s case invalidat
153bf 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a e all incrblob.*
153c0 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f * cursors open o
153c1 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 68 69 6e n any row within
153c2 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 the table with
153c3 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e 6f 52 6f root-page pgnoRo
153c4 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 ot..**.** Otherw
153c5 69 73 65 2c 20 69 66 20 61 72 67 75 6d 65 6e 74 ise, if argument
153c6 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 isClearTable is
153c7 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 65 false, then the
153c8 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 72 6f 77 row with.** row
153c9 69 64 20 69 52 6f 77 20 69 73 20 62 65 69 6e 67 id iRow is being
153ca 20 72 65 70 6c 61 63 65 64 20 6f 72 20 64 65 6c replaced or del
153cb 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 eted. In this ca
153cc 73 65 20 69 6e 76 61 6c 69 64 61 74 65 0a 2a 2a se invalidate.**
153cd 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 6e 63 72 only those incr
153ce 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 blob cursors ope
153cf 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 63 69 66 n on that specif
153d0 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 ic row..*/.stati
153d1 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 c void invalidat
153d2 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 eIncrblobCursors
153d3 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 (. Btree *pBtre
153d4 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e, /* T
153d5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
153d6 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 to check */. i
153d7 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 20 20 64 iRow,
153d8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f /* The ro
153d9 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20 62 wid that might b
153da 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 e changing */.
153db 69 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 int isClearTable
153dc 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
153dd 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 if all rows are
153de 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f being deleted */
153df 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a .){. BtCursor *
153e0 70 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 p;. BtShared *p
153e1 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 Bt = pBtree->pBt
153e2 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
153e3 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
153e4 65 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 ex(pBtree) );.
153e5 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 for(p=pBt->pCurs
153e6 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
153e7 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 t){. if( p->i
153e8 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
153e9 26 26 20 28 69 73 43 6c 65 61 72 54 61 62 6c 65 && (isClearTable
153ea 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 || p->info.nKey
153eb 3d 3d 69 52 6f 77 29 20 29 7b 0a 20 20 20 20 20 ==iRow) ){.
153ec 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 p->eState = CUR
153ed 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
153ee 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a }. }.}..#else.
153ef 20 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 /* Stub functi
153f0 6f 6e 73 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f ons when INCRBLO
153f1 42 20 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a B is omitted */.
153f2 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 #define invali
153f3 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
153f4 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 e(x). #define i
153f5 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 nvalidateAllOver
153f6 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23 flowCache(x). #
153f7 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 define invalidat
153f8 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 eIncrblobCursors
153f9 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 20 2f (x,y,z).#endif /
153fa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e * SQLITE_OMIT_IN
153fb 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a CRBLOB */../*.**
153fc 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66 Set bit pgno of
153fd 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 the BtShared.pH
153fe 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 asContent bitvec
153ff 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 . This is called
15400 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65 .** when a page
15401 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79 that previously
15402 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20 contained data
15403 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c becomes a free-l
15404 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67 ist leaf .** pag
15405 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53 e..**.** The BtS
15406 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
15407 74 20 62 69 74 76 65 63 20 65 78 69 73 74 73 20 t bitvec exists
15408 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 to work around a
15409 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67 n obscure.** bug
1540a 20 63 61 75 73 65 64 20 62 79 20 74 68 65 20 69 caused by the i
1540b 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77 nteraction of tw
1540c 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69 o useful IO opti
1540d 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75 mizations surrou
1540e 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 nding.** free-li
1540f 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a st leaf pages:.*
15410 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61 *.** 1) When a
15411 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 ll data is delet
15412 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61 ed from a page a
15413 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 63 6f nd the page beco
15414 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72 mes.** a fr
15415 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
15416 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e e, the page is n
15417 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 ot written to th
15418 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
15419 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74 (as free-list
1541a 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74 leaf pages cont
1541b 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 ain no meaningfu
1541c 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d l data). Sometim
1541d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20 es.** such
1541e 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76 a page is not ev
1541f 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61 en journalled (a
15420 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s it will not be
15421 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 modified,.**
15422 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f why bother jo
15423 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a urnalling it?)..
15424 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 **.** 2) When
15425 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 a free-list leaf
15426 20 70 61 67 65 20 69 73 20 72 65 75 73 65 64 2c page is reused,
15427 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 its content is
15428 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 not read.**
15429 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1542a 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f se or written to
1542b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1542c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 e (why should it
1542d 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20 .** be, if
1542e 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c it is not at all
1542f 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a meaningful?)..*
15430 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76 *.** By themselv
15431 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69 es, these optimi
15432 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e zations work fin
15433 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20 e and provide a
15434 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d handy.** perform
15435 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75 ance boost to bu
15436 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73 lk delete or ins
15437 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 ert operations.
15438 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61 However, if.** a
15439 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74 page is moved t
1543a 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 o the free-list
1543b 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 and then reused
1543c 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a within the same.
1543d 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 ** transaction,
1543e 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 a problem comes
1543f 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 up. If the page
15440 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 is not journalle
15441 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20 d when.** it is
15442 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 moved to the fre
15443 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73 e-list and it is
15444 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 also not journa
15445 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20 lled when it.**
15446 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 6f is extracted fro
15447 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 m the free-list
15448 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e and reused, then
15449 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
1544a 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73 ta.** may be los
1544b 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20 t. In the event
1544c 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 of a rollback, i
1544d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73 t may not be pos
1544e 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74 sible.** to rest
1544f 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ore the database
15450 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c to its original
15451 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a configuration..
15452 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 **.** The soluti
15453 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 61 72 on is the BtShar
15454 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 ed.pHasContent b
15455 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 itvec. Whenever
15456 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f a page is .** mo
15457 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 ved to become a
15458 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
15459 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 age, the corresp
1545a 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a onding bit is.**
1545b 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 74 76 set in the bitv
1545c 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c ec. Whenever a l
1545d 65 61 66 20 70 61 67 65 20 69 73 20 65 78 74 72 eaf page is extr
1545e 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 acted from the f
1545f 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 ree-list,.** opt
15460 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 imization 2 abov
15461 65 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20 e is omitted if
15462 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
15463 67 20 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 g bit is already
15464 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 .** set in BtSha
15465 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e red.pHasContent.
15466 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 The contents of
15467 20 74 68 65 20 62 69 74 76 65 63 20 61 72 65 20 the bitvec are
15468 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68 cleared.** at th
15469 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74 e end of every t
1546a 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
1546b 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 53 tatic int btreeS
1546c 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 etHasContent(BtS
1546d 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
1546e 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 pgno){. int rc
1546f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
15470 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f if( !pBt->pHasCo
15471 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 ntent ){. int
15472 20 6e 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20 nPage = 100;.
15473 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 sqlite3PagerPa
15474 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 gecount(pBt->pPa
15475 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 ger, &nPage);.
15476 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50 /* If sqlite3P
15477 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 agerPagecount()
15478 66 61 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e fails there is n
15479 6f 20 68 61 72 6d 20 62 65 63 61 75 73 65 20 74 o harm because t
1547a 68 65 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20 he. ** nPage
1547b 76 61 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68 variable is unch
1547c 61 6e 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64 anged from its d
1547d 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 efault value of
1547e 31 30 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 100 */. pBt->
1547f 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 pHasContent = sq
15480 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
15481 65 28 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20 e((u32)nPage);.
15482 20 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 if( !pBt->pHa
15483 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 sContent ){.
15484 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
15485 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 MEM;. }. }.
15486 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15487 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 OK && pgno<=sqli
15488 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 te3BitvecSize(pB
15489 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 t->pHasContent)
1548a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1548b 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74 te3BitvecSet(pBt
1548c 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 ->pHasContent, p
1548d 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 gno);. }. retu
1548e 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1548f 51 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72 Query the BtShar
15490 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 ed.pHasContent v
15491 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ector..**.** Thi
15492 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
15493 6c 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 lled when a free
15494 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
15495 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 is removed from
15496 74 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 the.** free-list
15497 20 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 for reuse. It r
15498 65 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 eturns false if
15499 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 it is safe to re
1549a 74 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 trieve the.** pa
1549b 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ge from the page
1549c 72 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 r layer with the
1549d 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 'no-content' fl
1549e 61 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 ag set. True oth
1549f 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 erwise..*/.stati
154a0 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 c int btreeGetHa
154a1 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 sContent(BtShare
154a2 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
154a3 6f 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 o){. Bitvec *p
154a4 3d 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 = pBt->pHasConte
154a5 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 nt;. return (p
154a6 26 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 && (pgno>sqlite3
154a7 42 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c BitvecSize(p) ||
154a8 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
154a9 73 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d st(p, pgno)));.}
154aa 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 ../*.** Clear (d
154ab 65 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 estroy) the BtSh
154ac 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 ared.pHasContent
154ad 20 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 bitvec. This sh
154ae 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b ould be.** invok
154af 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 ed at the conclu
154b0 73 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 sion of each wri
154b1 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a te-transaction..
154b2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 */.static void b
154b3 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 treeClearHasCont
154b4 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 ent(BtShared *pB
154b5 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 t){. sqlite3Bit
154b6 76 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e vecDestroy(pBt->
154b7 70 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 pHasContent);.
154b8 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
154b9 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 = 0;.}../*.** S
154ba 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ave the current
154bb 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 cursor position
154bc 69 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 in the variables
154bd 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a BtCursor.nKey .
154be 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e ** and BtCursor.
154bf 70 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 pKey. The cursor
154c0 27 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 's state is set
154c1 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 to CURSOR_REQUIR
154c2 45 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ESEEK..**.** The
154c3 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 caller must ens
154c4 75 72 65 20 74 68 61 74 20 74 68 65 20 63 75 72 ure that the cur
154c5 73 6f 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 sor is valid (ha
154c6 73 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 s eState==CURSOR
154c7 5f 56 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 _VALID).** prior
154c8 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 to calling this
154c9 20 72 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 routine. .*/.s
154ca 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 tatic int saveCu
154cb 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 rsorPosition(BtC
154cc 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
154cd 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
154ce 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d t( CURSOR_VALID=
154cf 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b =pCur->eState );
154d0 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 . assert( 0==pC
154d1 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 ur->pKey );. as
154d2 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
154d3 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
154d4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
154d5 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 treeKeySize(pCur
154d6 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a , &pCur->nKey);.
154d7 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
154d8 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b LITE_OK ); /* K
154d9 65 79 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 eySize() cannot
154da 66 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 fail */.. /* If
154db 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b this is an intK
154dc 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 ey table, then t
154dd 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f he above call to
154de 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a BtreeKeySize().
154df 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 ** stores the
154e0 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 integer key in p
154e1 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 Cur->nKey. In th
154e2 69 73 20 63 61 73 65 20 74 68 69 73 20 76 61 6c is case this val
154e3 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 ue is. ** all t
154e4 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 2e hat is required.
154e5 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 Otherwise, if p
154e6 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 Cur is not open
154e7 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a on an intKey. *
154e8 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 * table, then ma
154e9 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 lloc space for a
154ea 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 nd store the pCu
154eb 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 r->nKey bytes of
154ec 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e key . ** data.
154ed 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 . */. if( 0==p
154ee 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
154ef 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f intKey ){. vo
154f0 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 id *pKey = sqlit
154f1 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 e3Malloc( (int)p
154f2 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 Cur->nKey );.
154f3 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 if( pKey ){.
154f4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
154f5 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c treeKey(pCur, 0,
154f6 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 (int)pCur->nKey
154f7 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 , pKey);. i
154f8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
154f9 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 ){. pCur
154fa 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 ->pKey = pKey;.
154fb 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
154fc 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
154fd 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a (pKey);. }.
154fe 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
154ff 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
15500 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 EM;. }. }.
15501 61 73 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 assert( !pCur->a
15502 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
15503 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 || !pCur->pKey
15504 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );.. if( rc==SQ
15505 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
15506 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
15507 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 0; i<=pCur->iPag
15508 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 e; i++){. r
15509 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d eleasePage(pCur-
1550a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
1550b 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
1550c 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 i] = 0;. }.
1550d 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 pCur->iPage =
1550e 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 -1;. pCur->eS
1550f 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 tate = CURSOR_RE
15510 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a QUIRESEEK;. }..
15511 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 invalidateOver
15512 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b flowCache(pCur);
15513 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15514 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 ./*.** Save the
15515 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c positions of all
15516 20 63 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 cursors (except
15517 20 70 45 78 63 65 70 74 29 20 74 68 61 74 20 61 pExcept) that a
15518 72 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 re open on.** th
15519 65 20 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f e table with ro
1551a 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 ot-page iRoot. U
1551b 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 sually, this is
1551c 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f called just befo
1551d 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 re cursor.** pEx
1551e 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 cept is used to
1551f 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 modify the table
15520 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 (BtreeDelete()
15521 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 or BtreeInsert()
15522 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
15523 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
15524 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
15525 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 gno iRoot, BtCur
15526 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 sor *pExcept){.
15527 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
15528 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15529 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
1552a 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1552b 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c rt( pExcept==0 |
1552c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d | pExcept->pBt==
1552d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 pBt );. for(p=p
1552e 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
1552f 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
15530 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 if( p!=pExcept
15531 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 && (0==iRoot ||
15532 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f p->pgnoRoot==iRo
15533 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 ot) && .
15534 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f p->eState==CURSO
15535 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 R_VALID ){.
15536 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 int rc = saveCu
15537 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b rsorPosition(p);
15538 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
15539 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK!=rc ){.
1553a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1553b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1553c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1553d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
1553e 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ear the current
1553f 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e cursor position.
15540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15541 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
15542 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
15543 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
15544 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
15545 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
15546 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) );. sqlite3_f
15547 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
15548 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 . pCur->pKey =
15549 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 0;. pCur->eStat
1554a 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
1554b 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 ID;.}../*.** In
1554c 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 this version of
1554d 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 BtreeMoveto, pKe
1554e 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e y is a packed in
1554f 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 dex record.** su
15550 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 ch as is generat
15551 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b ed by the OP_Mak
15552 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 eRecord opcode.
15553 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 Unpack the.** r
15554 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 ecord and then c
15555 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 all BtreeMovetoU
15556 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 npacked() to do
15557 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 the work..*/.sta
15558 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 tic int btreeMov
15559 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 eto(. BtCursor
1555a 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 *pCur, /* Cu
1555b 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 rsor open on the
1555c 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 btree to be sea
1555d 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 rched */. const
1555e 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f void *pKey, /
1555f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 * Packed key if
15560 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 the btree is an
15561 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e index */. i64 n
15562 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f Key, /
15563 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f * Integer key fo
15564 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 r tables. Size
15565 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 of pKey for indi
15566 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 ces */. int bia
15567 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
15568 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 Bias search to t
15569 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 he high end */.
1556a 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 int *pRes
1556b 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
1556c 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
1556d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
1556e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1556f 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 /* Status c
15570 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ode */. Unpacke
15571 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
15572 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ; /* Unpacked
15573 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 index key */. c
15574 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b har aSpace[150];
15575 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d /* Tem
15576 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 p space for pIdx
15577 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 Key - to avoid a
15578 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 malloc */.. if
15579 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 ( pKey ){. as
1557a 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 sert( nKey==(i64
1557b 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 )(int)nKey );.
1557c 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
1557d 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
1557e 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e ack(pCur->pKeyIn
1557f 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 fo, (int)nKey, p
15580 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
15581 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15582 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63 aSpac
15583 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 e, sizeof(aSpace
15584 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
15585 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Key==0 ) return
15586 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15587 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
15588 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
15589 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1558a 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
1558b 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
1558c 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
1558d 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
1558e 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1558f 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
15590 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a d(pIdxKey);. }.
15591 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15592 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 /*.** Restore th
15593 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
15594 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 position it was
15595 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 in (or as close
15596 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a to as possible).
15597 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 ** when saveCurs
15598 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 orPosition() was
15599 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 called. Note th
1559a 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c at this call del
1559b 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 etes the .** sav
1559c 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f ed position info
1559d 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 stored by saveC
1559e 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c ursorPosition(),
1559f 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 so there can be
155a0 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 .** at most one
155a1 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 effective restor
155a2 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
155a3 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 ) call after eac
155a4 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 h .** saveCursor
155a5 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 Position()..*/.s
155a6 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 tatic int btreeR
155a7 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
155a8 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
155a9 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
155aa 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
155ab 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
155ac 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
155ad 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
155ae 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
155af 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
155b0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
155b1 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LT ){. return
155b2 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
155b3 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 . }. pCur->eSt
155b4 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
155b5 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 ALID;. rc = btr
155b6 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 eeMoveto(pCur, p
155b7 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d Cur->pKey, pCur-
155b8 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d >nKey, 0, &pCur-
155b9 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 >skipNext);. if
155ba 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
155bb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
155bc 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
155bd 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 . pCur->pKey
155be 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
155bf 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
155c0 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
155c1 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
155c2 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 SOR_INVALID );.
155c3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
155c4 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f }..#define resto
155c5 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
155c6 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 (p) \. (p->eSta
155c7 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 te>=CURSOR_REQUI
155c8 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 RESEEK ? \.
155c9 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 btreeRestore
155ca 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
155cb 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 ) : \. S
155cc 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a QLITE_OK)../*.**
155cd 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 Determine wheth
155ce 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 er or not a curs
155cf 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f or has moved fro
155d0 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 m the position i
155d1 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c t.** was last pl
155d2 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 aced at. Cursor
155d3 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 s can move when
155d4 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 the row they are
155d5 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 pointing.** at
155d6 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 is deleted out f
155d7 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a rom under them..
155d8 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
155d9 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 ne returns an er
155da 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 ror code if some
155db 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
155dc 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 . The.** intege
155dd 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 r *pHasMoved is
155de 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 set to one if th
155df 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 e cursor has mov
155e0 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e ed and 0 if not.
155e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
155e2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
155e3 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
155e4 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
155e5 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 , int *pHasMoved
155e6 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
155e7 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
155e8 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
155e9 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
155ea 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
155eb 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
155ec 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d . }. if( pCur-
155ed 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
155ee 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 VALID || pCur->s
155ef 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 kipNext!=0 ){.
155f0 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
155f1 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
155f2 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 pHasMoved = 0;.
155f3 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
155f4 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
155f5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
155f6 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 TOVACUUM./*.** G
155f7 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 iven a page numb
155f8 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 er of a regular
155f9 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 database page, r
155fa 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a eturn the page.*
155fb 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 * number for the
155fc 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
155fd 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
155fe 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 the entry for th
155ff 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 e.** input page
15600 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
15601 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 c Pgno ptrmapPag
15602 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 eno(BtShared *pB
15603 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 t, Pgno pgno){.
15604 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 int nPagesPerMa
15605 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 pPage;. Pgno iP
15606 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 trMap, ret;. as
15607 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15608 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15609 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 tex) );. nPages
1560a 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 PerMapPage = (pB
1560b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 t->usableSize/5)
1560c 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 +1;. iPtrMap =
1560d 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 (pgno-2)/nPagesP
1560e 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 erMapPage;. ret
1560f 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 = (iPtrMap*nPag
15610 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 esPerMapPage) +
15611 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 2; . if( ret==P
15612 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
15613 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 (pBt) ){. ret
15614 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
15615 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ret;.}../*.** W
15616 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
15617 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d to the pointer m
15618 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ap..**.** This r
15619 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 outine updates t
1561a 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
1561b 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 ntry for page nu
1561c 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f mber 'key'.** so
1561d 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f that it maps to
1561e 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e type 'eType' an
1561f 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 d parent page nu
15620 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a mber 'pgno'..**.
15621 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e ** If *pRC is in
15622 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f itially non-zero
15623 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 (non-SQLITE_OK)
15624 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
15625 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ne is.** a no-op
15626 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f . If an error o
15627 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f ccurs, the appro
15628 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
15629 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 e is written.**
1562a 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 into *pRC..*/.st
1562b 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 atic void ptrmap
1562c 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 Put(BtShared *pB
1562d 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 t, Pgno key, u8
1562e 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 eType, Pgno pare
1562f 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 nt, int *pRC){.
15630 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
15631 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 ; /* The pointe
15632 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 r map page */.
15633 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 u8 *pPtrmap;
15634 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
15635 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 map data */. P
15636 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 gno iPtrmap;
15637 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 /* The pointer
15638 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 map page number
15639 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b */. int offset;
1563a 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1563b 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 in pointer map
1563c 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 page */. int rc
1563d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 ; /* R
1563e 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
1563f 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a subfunctions */.
15640 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 . if( *pRC ) re
15641 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
15642 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15643 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15644 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 );. /* The mast
15645 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 er-journal page
15646 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 number must neve
15647 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 r be used as a p
15648 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
15649 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d */. assert( 0==
1564a 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
1564b 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t, PENDING_BYTE_
1564c 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 PAGE(pBt)) );..
1564d 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
1564e 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 toVacuum );. if
1564f 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 ( key==0 ){.
15650 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
15651 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
15652 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 return;. }. iP
15653 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 trmap = PTRMAP_P
15654 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b AGENO(pBt, key);
15655 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
15656 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
15657 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 ger, iPtrmap, &p
15658 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
15659 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1565a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 *pRC = rc;.
1565b 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
1565c 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
1565d 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
1565e 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 ap, key);. if(
1565f 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 offset<0 ){.
15660 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
15661 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
15662 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 goto ptrmap_exit
15663 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 ;. }. pPtrmap
15664 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 = (u8 *)sqlite3P
15665 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
15666 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 age);.. if( eTy
15667 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 pe!=pPtrmap[offs
15668 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 et] || get4byte(
15669 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b &pPtrmap[offset+
1566a 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 1])!=parent ){.
1566b 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 TRACE(("PTRMA
1566c 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 P_UPDATE: %d->(%
1566d 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 d,%d)\n", key, e
1566e 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a Type, parent));.
1566f 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 *pRC= rc = s
15670 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
15671 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
15672 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15673 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 ){. pPtrma
15674 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 p[offset] = eTyp
15675 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 e;. put4byt
15676 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e(&pPtrmap[offse
15677 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 t+1], parent);.
15678 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 }. }..ptrmap
15679 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 _exit:. sqlite3
1567a 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
1567b 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ge);.}../*.** Re
1567c 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d ad an entry from
1567d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1567e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1567f 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 tine retrieves t
15680 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
15681 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b ntry for page 'k
15682 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 ey', writing.**
15683 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 the type and par
15684 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ent page number
15685 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a to *pEType and *
15686 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 pPgno respective
15687 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 ly..** An error
15688 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
15689 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f if something go
1568a 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 es wrong, otherw
1568b 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
1568c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 /.static int ptr
1568d 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 mapGet(BtShared
1568e 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 *pBt, Pgno key,
1568f 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f u8 *pEType, Pgno
15690 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 *pPgno){. DbPa
15691 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f ge *pDbPage; /
15692 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
15693 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 p page */. int
15694 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f iPtrmap; /
15695 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 * Pointer map pa
15696 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 ge index */. u8
15697 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 *pPtrmap;
15698 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 /* Pointer map
15699 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 page data */. i
1569a 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
1569b 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 /* Offset of e
1569c 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 ntry in pointer
1569d 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b map */. int rc;
1569e 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1569f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
156a0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 Bt->mutex) );..
156a1 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 iPtrmap = PTRMA
156a2 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 P_PAGENO(pBt, ke
156a3 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 y);. rc = sqlit
156a4 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e e3PagerGet(pBt->
156a5 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c pPager, iPtrmap,
156a6 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 &pDbPage);. if
156a7 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 ( rc!=0 ){. r
156a8 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
156a9 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 pPtrmap = (u8 *)
156aa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
156ab 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 ata(pDbPage);..
156ac 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
156ad 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
156ae 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 ap, key);. asse
156af 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b rt( pEType!=0 );
156b0 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 . *pEType = pPt
156b1 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 rmap[offset];.
156b2 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 if( pPgno ) *pPg
156b3 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
156b4 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
156b5 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 );.. sqlite3Pag
156b6 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
156b7 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c ;. if( *pEType<
156b8 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 1 || *pEType>5 )
156b9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
156ba 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 ORRUPT_BKPT;. r
156bb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
156bc 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 .}..#else /* if
156bd 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f defined SQLITE_O
156be 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a MIT_AUTOVACUUM *
156bf 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d /. #define ptrm
156c0 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 apPut(w,x,y,z,rc
156c1 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d ). #define ptrm
156c2 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 apGet(w,x,y,z) S
156c3 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 QLITE_OK. #defi
156c4 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c ne ptrmapPutOvfl
156c5 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 Ptr(x, y, rc).#e
156c6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ndif../*.** Give
156c7 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61 n a btree page a
156c8 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 nd a cell index
156c9 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 (0 means the fir
156ca 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 st cell on.** th
156cb 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 e page, 1 means
156cc 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c the second cell,
156cd 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 and so forth) r
156ce 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a eturn a pointer.
156cf 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 ** to the cell c
156d0 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ontent..**.** Th
156d1 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 is routine works
156d2 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 only for pages
156d3 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 that do not cont
156d4 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
156d5 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 ls..*/.#define f
156d6 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 indCell(P,I) \.
156d7 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 ((P)->aData + (
156d8 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 (P)->maskPage &
156d9 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 get2byte(&(P)->a
156da 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 Data[(P)->cellOf
156db 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a fset+2*(I)])))..
156dc 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 /*.** This a mor
156dd 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f e complex versio
156de 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 n of findCell()
156df 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a that works for.*
156e0 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 * pages that do
156e1 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 contain overflow
156e2 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 cells..*/.stati
156e3 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c c u8 *findOverfl
156e4 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a owCell(MemPage *
156e5 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c pPage, int iCell
156e6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 ){. int i;. as
156e7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
156e8 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
156e9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
156ea 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f for(i=pPage->nO
156eb 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b verflow-1; i>=0;
156ec 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b i--){. int k
156ed 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 ;. struct _Ov
156ee 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 flCell *pOvfl;.
156ef 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 pOvfl = &pPag
156f0 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 e->aOvfl[i];.
156f1 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b k = pOvfl->idx;
156f2 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c . if( k<=iCel
156f3 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b l ){. if( k
156f4 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ==iCell ){.
156f5 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d return pOvfl-
156f6 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a >pCell;. }.
156f7 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 iCell--;.
156f8 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
156f9 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 n findCell(pPage
156fa 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a , iCell);.}../*.
156fb 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 ** Parse a cell
156fc 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e content block an
156fd 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 d fill in the Ce
156fe 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 llInfo structure
156ff 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 . There.** are
15700 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 two versions of
15701 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 this function.
15702 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
15703 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c takes a .** cel
15704 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 l index as the s
15705 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 econd argument a
15706 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c nd btreeParseCel
15707 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 lPtr() .** takes
15708 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
15709 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 e body of the ce
1570a 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ll as its second
1570b 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
1570c 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c Within this fil
1570d 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c e, the parseCell
1570e 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 () macro can be
1570f 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f called instead o
15710 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 f.** btreeParseC
15711 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 ellPtr(). Using
15712 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 some compilers,
15713 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 this will be fas
15714 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ter..*/.static v
15715 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
15716 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 llPtr(. MemPage
15717 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
15718 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
15719 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
1571a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
1571b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1571c 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
1571d 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c text. */. Cell
1571e 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
1571f 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
15720 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
15721 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 .){. u16 n;
15722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15723 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e Number bytes in
15724 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
15725 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ader */. u32 nP
15726 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 ayload;
15727 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
15728 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 ytes of cell pay
15729 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 load */.. asser
1572a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1572b 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
1572c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
1572d 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 Info->pCell = pC
1572e 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ell;. assert( p
1572f 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c Page->leaf==0 ||
15730 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 pPage->leaf==1
15731 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e );. n = pPage->
15732 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
15733 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 assert( n==4-4*p
15734 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
15735 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
15736 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 y ){. if( pPa
15737 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
15738 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 n += getVar
15739 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c int32(&pCell[n],
1573a 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 nPayload);.
1573b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
1573c 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d yload = 0;. }
1573d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 . n += getVar
1573e 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 int(&pCell[n], (
1573f 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 u64*)&pInfo->nKe
15740 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e y);. pInfo->n
15741 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b Data = nPayload;
15742 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
15743 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a nfo->nData = 0;.
15744 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 n += getVari
15745 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 nt32(&pCell[n],
15746 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
15747 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
15748 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
15749 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
1574a 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
1574b 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 ->nHeader = n;.
1574c 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c testcase( nPayl
1574d 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c oad==pPage->maxL
1574e 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 ocal );. testca
1574f 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 se( nPayload==pP
15750 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
15751 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 );. if( likely(
15752 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d nPayload<=pPage-
15753 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 >maxLocal) ){.
15754 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
15755 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 (easy) common c
15756 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e ase where the en
15757 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 tire payload fit
15758 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 s. ** on the
15759 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 local page. No
1575a 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 overflow is requ
1575b 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
1575c 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 int nSize;
1575d 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
1575e 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 ze of cell conte
1575f 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 nt in bytes */.
15760 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c nSize = nPayl
15761 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e oad + n;. pIn
15762 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
15763 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 6)nPayload;.
15764 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 pInfo->iOverflow
15765 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e = 0;. if( (n
15766 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b Size & ~3)==0 ){
15767 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 . nSize = 4
15768 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 ; /* Mini
15769 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 mum cell size is
1576a 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 4 */. }.
1576b 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 pInfo->nSize = (
1576c 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c u16)nSize;. }el
1576d 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 se{. /* If th
1576e 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e e payload will n
1576f 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c ot fit completel
15770 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 y on the local p
15771 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 age, we have.
15772 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f ** to decide ho
15773 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 w much to store
15774 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 locally and how
15775 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e much to spill on
15776 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c to. ** overfl
15777 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 ow pages. The s
15778 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 trategy is to mi
15779 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e nimize the amoun
1577a 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 t of unused.
1577b 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 ** space on over
1577c 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 flow pages while
1577d 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f keeping the amo
1577e 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f unt of local sto
1577f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 rage. ** in b
15780 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 etween minLocal
15781 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 and maxLocal..
15782 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e **. ** Warn
15783 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 ing: changing t
15784 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 he way overflow
15785 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 payload is distr
15786 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 ibuted in any.
15787 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 ** way will re
15788 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d sult in an incom
15789 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 patible file for
1578a 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 mat.. */.
1578b 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f int minLocal; /
1578c 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Minimum amount
1578d 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
1578e 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
1578f 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f int maxLocal; /
15790 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Maximum amount
15791 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
15792 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
15793 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f int surplus; /
15794 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f * Overflow paylo
15795 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ad available for
15796 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a local storage *
15797 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 /.. minLocal
15798 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 = pPage->minLoca
15799 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 l;. maxLocal
1579a 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 = pPage->maxLoca
1579b 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d l;. surplus =
1579c 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 minLocal + (nPa
1579d 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c yload - minLocal
1579e 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 )%(pPage->pBt->u
1579f 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a sableSize - 4);.
157a0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 testcase( su
157a1 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 rplus==maxLocal
157a2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
157a3 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 surplus==maxLoc
157a4 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
157a5 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f surplus <= maxLo
157a6 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e cal ){. pIn
157a7 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
157a8 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 6)surplus;. }
157a9 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 else{. pInf
157aa 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
157ab 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d )minLocal;. }
157ac 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
157ad 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 rflow = (u16)(pI
157ae 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 nfo->nLocal + n)
157af 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 ;. pInfo->nSi
157b0 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 ze = pInfo->iOve
157b1 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d rflow + 4;. }.}
157b2 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 .#define parseCe
157b3 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c ll(pPage, iCell,
157b4 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 pInfo) \. btre
157b5 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
157b6 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
157b7 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
157b8 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 ), (pInfo)).stat
157b9 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
157ba 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 seCell(. MemPag
157bb 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
157bc 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
157bd 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f ning the cell */
157be 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 . int iCell,
157bf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
157c0 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 e cell index. F
157c1 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a irst cell is 0 *
157c2 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
157c3 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
157c4 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
157c5 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 cture */.){. pa
157c6 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 rseCell(pPage, i
157c7 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a Cell, pInfo);.}.
157c8 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
157c9 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
157ca 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 of bytes that a
157cb 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 Cell needs in th
157cc 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 e cell.** data a
157cd 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 rea of the btree
157ce 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 -page. The retu
157cf 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 rn number includ
157d0 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 es the cell.** d
157d1 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 ata header and t
157d2 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 he local payload
157d3 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 , but not any ov
157d4 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a erflow page or.*
157d5 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 * the space used
157d6 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 by the cell poi
157d7 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
157d8 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 u16 cellSizePtr(
157d9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
157da 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 u8 *pCell){. u8
157db 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c *pIter = &pCell
157dc 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 [pPage->childPtr
157dd 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 Size];. u32 nSi
157de 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ze;..#ifdef SQLI
157df 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 TE_DEBUG. /* Th
157e0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
157e1 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
157e2 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 n should always
157e3 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 be the same as.
157e4 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 ** the (CellInf
157e5 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 o.nSize) value f
157e6 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 ound by doing a
157e7 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 full parse of th
157e8 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 e. ** cell. If
157e9 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 SQLITE_DEBUG is
157ea 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 defined, an asse
157eb 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 rt() at the bott
157ec 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 om of. ** this
157ed 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 function verifie
157ee 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 s that this inva
157ef 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f riant is not vio
157f0 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c lated. */. Cell
157f1 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a Info debuginfo;.
157f2 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
157f3 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
157f4 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 , &debuginfo);.#
157f5 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 endif.. if( pPa
157f6 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
157f7 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 u8 *pEnd;.
157f8 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
157f9 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 ta ){. pIte
157fa 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 r += getVarint32
157fb 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a (pIter, nSize);.
157fc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
157fd 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 nSize = 0;.
157fe 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 }.. /* pIter
157ff 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 now points at th
15800 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
15801 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 key value, a va
15802 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 riable length .
15803 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 ** integer. T
15804 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
15805 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 ck moves pIter t
15806 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 o point at the f
15807 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a irst byte. **
15808 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
15809 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1580a 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 */. pEnd = &p
1580b 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 Iter[9];. whi
1580c 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 le( (*pIter++)&0
1580d 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
1580e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 d );. }else{.
1580f 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 pIter += getVa
15810 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 rint32(pIter, nS
15811 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 ize);. }.. tes
15812 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
15813 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
15814 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
15815 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
15816 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e cal+1 );. if( n
15817 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c Size>pPage->maxL
15818 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 ocal ){. int
15819 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
1581a 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
1581b 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
1581c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
1581d 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
1581e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1581f 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
15820 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
15821 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
15822 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
15823 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
15824 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
15825 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
15826 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
15827 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
15828 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
15829 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 += 4;. }. nSi
1582a 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 ze += (u32)(pIte
1582b 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f r - pCell);.. /
1582c 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 * The minimum si
1582d 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 ze of any cell i
1582e 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 s 4 bytes. */.
1582f 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 if( nSize<4 ){.
15830 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 nSize = 4;.
15831 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 }.. assert( nSi
15832 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 ze==debuginfo.nS
15833 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ize );. return
15834 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 (u16)nSize;.}..#
15835 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
15836 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61 UG./* This varia
15837 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 tion on cellSize
15838 50 74 72 28 29 20 69 73 20 75 73 65 64 20 69 6e Ptr() is used in
15839 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 side of assert()
1583a 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f statements.** o
1583b 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 nly. */.static u
1583c 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 16 cellSize(MemP
1583d 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
1583e 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e iCell){. return
1583f 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
15840 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 ge, findCell(pPa
15841 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 ge, iCell));.}.#
15842 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
15843 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15844 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ACUUM./*.** If t
15845 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 he cell pCell, p
15846 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 art of page pPag
15847 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 e contains a poi
15848 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 nter.** to an ov
15849 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 erflow page, ins
1584a 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ert an entry int
1584b 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
1584c 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 p.** for the ove
1584d 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 rflow page..*/.s
1584e 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 tatic void ptrma
1584f 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 pPutOvflPtr(MemP
15850 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
15851 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 pCell, int *pRC)
15852 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 {. CellInfo inf
15853 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 o;. if( *pRC )
15854 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
15855 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 ( pCell!=0 );.
15856 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
15857 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
15858 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 &info);. assert
15859 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 ( (info.nData+(p
1585a 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 Page->intKey?0:i
1585b 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f nfo.nKey))==info
1585c 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 .nPayload );. i
1585d 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f f( info.iOverflo
1585e 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 w ){. Pgno ov
1585f 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 fl = get4byte(&p
15860 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
15861 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 low]);. ptrma
15862 70 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c pPut(pPage->pBt,
15863 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 ovfl, PTRMAP_OV
15864 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e ERFLOW1, pPage->
15865 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a pgno, pRC);. }.
15866 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a }.#endif.../*.**
15867 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 Defragment the
15868 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c page given. All
15869 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 Cells are moved
1586a 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f to the.** end o
1586b 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 f the page and a
1586c 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73 ll free space is
1586d 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 collected into
1586e 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 one.** big FreeB
1586f 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 lk that occurs i
15870 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 n between the he
15871 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a ader and cell.**
15872 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 pointer array a
15873 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 nd the cell cont
15874 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 ent area..*/.sta
15875 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 tic int defragme
15876 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a ntPage(MemPage *
15877 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b pPage){. int i;
15878 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15879 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1587a 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 nter */. int pc
1587b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1587c 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
1587d 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a of a i-th cell *
1587e 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 /. int hdr;
1587f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15880 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
15881 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 page header */.
15882 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 int size;
15883 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
15884 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f ize of a cell */
15885 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a . int usableSiz
15886 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
15887 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c Number of usabl
15888 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 e bytes on a pag
15889 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f e */. int cellO
1588a 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1588b 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
1588c 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
1588d 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 array */. int c
1588e 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 brk;
1588f 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
15890 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
15891 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
15892 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 t nCell;
15893 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
15894 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 er of cells on t
15895 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 he page */. uns
15896 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
15897 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 ; /* The p
15898 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e age data */. un
15899 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d signed char *tem
1589a 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 p; /* Temp
1589b 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 area for cell c
1589c 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 ontent */. int
1589d 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 iCellFirst;
1589e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1589f 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 allowable cell i
158a0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 ndex */. int iC
158a1 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 ellLast;
158a2 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 /* Last pos
158a3 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 sible cell index
158a4 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 */... assert(
158a5 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
158a6 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
158a7 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
158a8 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 ert( pPage->pBt!
158a9 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
158aa 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
158ab 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 leSize <= SQLITE
158ac 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 _MAX_PAGE_SIZE )
158ad 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
158ae 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 e->nOverflow==0
158af 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
158b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
158b1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
158b2 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73 x) );. temp = s
158b3 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 qlite3PagerTempS
158b4 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d pace(pPage->pBt-
158b5 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61 >pPager);. data
158b6 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
158b7 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
158b8 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c hdrOffset;. cel
158b9 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d lOffset = pPage-
158ba 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e >cellOffset;. n
158bb 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
158bc 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ell;. assert( n
158bd 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 Cell==get2byte(&
158be 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a data[hdr+3]) );.
158bf 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
158c0 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
158c1 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20 eSize;. cbrk =
158c2 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
158c3 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 dr+5]);. memcpy
158c4 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 (&temp[cbrk], &d
158c5 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c ata[cbrk], usabl
158c6 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20 eSize - cbrk);.
158c7 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69 cbrk = usableSi
158c8 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74 ze;. iCellFirst
158c9 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 = cellOffset +
158ca 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 2*nCell;. iCell
158cb 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a Last = usableSiz
158cc 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30 e - 4;. for(i=0
158cd 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ; i<nCell; i++){
158ce 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 . u8 *pAddr;
158cf 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 /* The i-th
158d0 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
158d1 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 pAddr = &dat
158d2 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 a[cellOffset + i
158d3 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 *2];. pc = ge
158d4 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 t2byte(pAddr);.
158d5 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d testcase( pc=
158d6 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 =iCellFirst );.
158d7 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d testcase( pc=
158d8 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 =iCellLast );.#i
158d9 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
158da 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a E_ENABLE_OVERSIZ
158db 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 E_CELL_CHECK).
158dc 20 20 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69 /* These condi
158dd 74 69 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61 tions have alrea
158de 64 79 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 dy been verified
158df 20 69 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 in btreeInitPag
158e0 65 28 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51 e(). ** if SQ
158e1 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 LITE_ENABLE_OVER
158e2 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 SIZE_CELL_CHECK
158e3 69 73 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20 is defined .
158e4 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43 */. if( pc<iC
158e5 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 ellFirst || pc>i
158e6 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 CellLast ){.
158e7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
158e8 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
158e9 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 }.#endif. a
158ea 73 73 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c ssert( pc>=iCell
158eb 46 69 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65 First && pc<=iCe
158ec 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69 llLast );. si
158ed 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 ze = cellSizePtr
158ee 28 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 (pPage, &temp[pc
158ef 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 ]);. cbrk -=
158f0 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65 size;.#if define
158f1 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
158f2 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 OVERSIZE_CELL_CH
158f3 45 43 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72 ECK). if( cbr
158f4 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a k<iCellFirst ){.
158f5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
158f6 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
158f7 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.#else.
158f8 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c if( cbrk<iCell
158f9 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65 First || pc+size
158fa 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 >usableSize ){.
158fb 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
158fc 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
158fd 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
158fe 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 assert( cbrk+s
158ff 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize<=usableSize
15900 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 && cbrk>=iCellFi
15901 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 rst );. testc
15902 61 73 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d ase( cbrk+size==
15903 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 usableSize );.
15904 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 testcase( pc+s
15905 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize==usableSize
15906 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
15907 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 ata[cbrk], &temp
15908 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 [pc], size);.
15909 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c put2byte(pAddr,
1590a 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 cbrk);. }. as
1590b 73 65 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c sert( cbrk>=iCel
1590c 6c 46 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32 lFirst );. put2
1590d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
1590e 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61 ], cbrk);. data
1590f 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 [hdr+1] = 0;. d
15910 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a ata[hdr+2] = 0;.
15911 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 data[hdr+7] =
15912 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 0;. memset(&dat
15913 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30 a[iCellFirst], 0
15914 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 , cbrk-iCellFirs
15915 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 t);. assert( sq
15916 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
15917 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
15918 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63 Page) );. if( c
15919 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d brk-iCellFirst!=
1591a 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a pPage->nFree ){.
1591b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1591c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1591d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1591e 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1591f 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 Allocate nByte
15920 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 bytes of space f
15921 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42 rom within the B
15922 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65 -Tree page passe
15923 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 d.** as the firs
15924 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74 t argument. Writ
15925 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65 e into *pIdx the
15926 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67 index into pPag
15927 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 e->aData[].** of
15928 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 the first byte
15929 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 of allocated spa
1592a 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65 ce. Return eithe
1592b 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a r SQLITE_OK or.*
1592c 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 * an error code
1592d 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f (usually SQLITE_
1592e 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 CORRUPT)..**.**
1592f 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 The caller guara
15930 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65 ntees that there
15931 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 73 is sufficient s
15932 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 pace to make the
15933 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 .** allocation.
15934 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 This routine mi
15935 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72 ght need to defr
15936 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20 agment in order
15937 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 to bring.** all
15938 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 68 the space togeth
15939 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68 er, however. Th
1593a 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1593b 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74 avoid using.** t
1593c 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 74 he first two byt
1593d 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c es past the cell
1593e 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69 pointer area si
1593f 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74 nce presumably t
15940 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f his.** allocatio
15941 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20 n is being made
15942 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65 in order to inse
15943 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73 rt a new cell, s
15944 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 o we will.** als
15945 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67 o end up needing
15946 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e a new cell poin
15947 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ter..*/.static i
15948 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 nt allocateSpace
15949 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1594a 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 int nByte, int
1594b 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20 *pIdx){. const
1594c 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d int hdr = pPage-
1594d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f >hdrOffset; /
1594e 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
1594f 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15950 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 t */. u8 * cons
15951 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e t data = pPage->
15952 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c aData; /* L
15953 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 ocal cache of pP
15954 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 age->aData */.
15955 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 int nFrag;
15956 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15957 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
15958 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 f fragmented byt
15959 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 es on pPage */.
1595a 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 int top;
1595b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1595c 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
1595d 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 yte of cell cont
1595e 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
1595f 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a t gap; /*
15960 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 67 First byte of g
15961 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20 ap between cell
15962 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c pointers and cel
15963 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
15964 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f nt rc; /
15965 2a 20 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e * Integer return
15966 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 75 code */. int u
15967 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 sableSize; /* Us
15968 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 74 68 65 able size of the
15969 20 70 61 67 65 20 2a 2f 0a 20 20 0a 20 20 61 73 page */. . as
1596a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1596b 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
1596c 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
1596d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1596e 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 ->pBt );. asser
1596f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15970 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
15971 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
15972 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 sert( nByte>=0 )
15973 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 ; /* Minimum ce
15974 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
15975 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15976 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b >nFree>=nByte );
15977 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
15978 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
15979 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d ;. usableSize =
1597a 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
1597b 62 6c 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 bleSize;. asser
1597c 74 28 20 6e 42 79 74 65 20 3c 20 75 73 61 62 6c t( nByte < usabl
1597d 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 eSize-8 );.. nF
1597e 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 rag = data[hdr+7
1597f 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ];. assert( pPa
15980 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d ge->cellOffset =
15981 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 = hdr + 12 - 4*p
15982 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
15983 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c gap = pPage->cel
15984 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 lOffset + 2*pPag
15985 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 e->nCell;. top
15986 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15987 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 [hdr+5]);. if(
15988 67 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 6e gap>top ) return
15989 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1598a 42 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 65 BKPT;. testcase
1598b 28 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a ( gap+2==top );.
1598c 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b testcase( gap+
1598d 31 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 1==top );. test
1598e 63 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 case( gap==top )
1598f 3b 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d ;.. if( nFrag>=
15990 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 60 ){. /* Alw
15991 61 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 68 ays defragment h
15992 69 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 ighly fragmented
15993 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 pages */. rc
15994 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 = defragmentPag
15995 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 e(pPage);. if
15996 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
15997 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 ;. top = get2
15998 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
15999 5d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ]);. }else if(
1599a 67 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 gap+2<=top ){.
1599b 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 /* Search the
1599c 66 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 freelist looking
1599d 20 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74 for a free slot
1599e 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 big enough to s
1599f 61 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 atisfy . ** t
159a0 68 65 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 he request. The
159a1 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 allocation is ma
159a2 64 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 de from the firs
159a3 74 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a t free slot in .
159a4 20 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 ** the list
159a5 74 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e that is large en
159a6 6f 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 ough to accomada
159a7 74 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 te it.. */.
159a8 20 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a int pc, addr;.
159a9 20 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72 for(addr=hdr
159aa 2b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79 +1; (pc = get2by
159ab 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 te(&data[addr]))
159ac 3e 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 >0; addr=pc){.
159ad 20 20 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 int size;
159ae 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
159af 20 6f 66 20 74 68 65 20 66 72 65 65 20 73 6c 6f of the free slo
159b0 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 t */. if( p
159b1 63 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 7c c>usableSize-4 |
159b2 7c 20 70 63 3c 61 64 64 72 2b 34 20 29 7b 0a 20 | pc<addr+4 ){.
159b3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
159b4 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
159b5 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 T;. }.
159b6 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 size = get2byte
159b7 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 (&data[pc+2]);.
159b8 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e if( size>=n
159b9 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Byte ){.
159ba 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e int x = size - n
159bb 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 Byte;. te
159bc 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a stcase( x==4 );.
159bd 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
159be 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 ( x==3 );.
159bf 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 if( x<4 ){.
159c0 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 /* Remove
159c1 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 the slot from t
159c2 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 he free-list. Up
159c3 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 date the number
159c4 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 of. **
159c5 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 fragmented bytes
159c6 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 within the page
159c7 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d . */. m
159c8 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 emcpy(&data[addr
159c9 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 ], &data[pc], 2)
159ca 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 ;. data
159cb 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e [hdr+7] = (u8)(n
159cc 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 Frag + x);.
159cd 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a }else if( siz
159ce 65 2b 70 63 20 3e 20 75 73 61 62 6c 65 53 69 7a e+pc > usableSiz
159cf 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 e ){. r
159d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
159d1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
159d2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
159d3 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 /* The slot
159d4 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 remains on the f
159d5 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 ree-list. Reduce
159d6 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 its size to acc
159d7 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a ount. *
159d8 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f * for the portio
159d9 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 n used by the ne
159da 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f w allocation. */
159db 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 . put2b
159dc 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c yte(&data[pc+2],
159dd 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 x);. }.
159de 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 *pIdx = p
159df 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 c + x;. r
159e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
159e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
159e2 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 }.. /* Check t
159e3 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 o make sure ther
159e4 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 e is enough spac
159e5 65 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 e in the gap to
159e6 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 satisfy. ** the
159e7 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 allocation. If
159e8 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 not, defragment
159e9 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 .. */. testcas
159ea 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d e( gap+2+nByte==
159eb 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 top );. if( gap
159ec 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a +2+nByte>top ){.
159ed 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d rc = defragm
159ee 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a entPage(pPage);.
159ef 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
159f0 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 urn rc;. top
159f1 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
159f2 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 [hdr+5]);. as
159f3 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c sert( gap+nByte<
159f4 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 =top );. }...
159f5 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
159f6 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 ry from the gap
159f7 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 in between the c
159f8 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
159f9 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 y. ** and the c
159fa 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
159fb 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 . The btreeInit
159fc 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 Page() call has
159fd 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c already. ** val
159fe 69 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c idated the freel
159ff 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 ist. Given that
15a00 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 the freelist is
15a01 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 valid, there.
15a02 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 ** is no way tha
15a03 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e t the allocation
15a04 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 can extend off
15a05 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 the end of the p
15a06 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 age.. ** The as
15a07 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 sert() below ver
15a08 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f ifies the previo
15a09 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a us sentence.. *
15a0a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 /. top -= nByte
15a0b 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
15a0c 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b ta[hdr+5], top);
15a0d 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e . assert( top+n
15a0e 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70 Byte <= pPage->p
15a0f 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
15a10 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b ;. *pIdx = top;
15a11 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15a12 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
15a13 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f turn a section o
15a14 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 f the pPage->aDa
15a15 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ta to the freeli
15a16 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 st..** The first
15a17 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 byte of the new
15a18 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 free block is p
15a19 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 Page->aDisk[star
15a1a 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 t].** and the si
15a1b 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 ze of the block
15a1c 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e is "size" bytes.
15a1d 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 .**.** Most of t
15a1e 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 he effort here i
15a1f 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f s involved in co
15a20 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 alesing adjacent
15a21 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 .** free blocks
15a22 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 into a single bi
15a23 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f g free block..*/
15a24 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 .static int free
15a25 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 Space(MemPage *p
15a26 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c Page, int start,
15a27 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e int size){. in
15a28 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 t addr, pbegin,
15a29 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74 hdr;. int iLast
15a2a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15a2b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
15a2c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65 est possible fre
15a2d 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f eblock offset */
15a2e 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15a2f 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e *data = pPage->
15a30 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 aData;.. assert
15a31 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 ( pPage->pBt!=0
15a32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15a33 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
15a34 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
15a35 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
15a36 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e ( start>=pPage->
15a37 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 hdrOffset+6+pPag
15a38 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 e->childPtrSize
15a39 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74 );. assert( (st
15a3a 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 art + size)<=pPa
15a3b 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
15a3c 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ize );. assert(
15a3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15a3e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15a3f 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
15a40 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 rt( size>=0 );
15a41 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
15a42 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 size is 4 */..#
15a43 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 ifdef SQLITE_SEC
15a44 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 URE_DELETE. /*
15a45 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 Overwrite delete
15a46 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 d information wi
15a47 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 th zeros when th
15a48 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 e SECURE_DELETE
15a49 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 . ** option is
15a4a 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 enabled at compi
15a4b 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d le-time */. mem
15a4c 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d set(&data[start]
15a4d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 , 0, size);.#end
15a4e 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 if.. /* Add the
15a4f 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f space back into
15a50 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
15a51 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 of freeblocks.
15a52 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 Note that. **
15a53 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 even though the
15a54 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 freeblock list w
15a55 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74 as checked by bt
15a56 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20 reeInitPage(),.
15a57 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 ** btreeInitPag
15a58 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65 e() did not dete
15a59 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63 ct overlapping c
15a5a 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65 ells or. ** fre
15a5b 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65 eblocks that ove
15a5c 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20 rlapped cells.
15a5d 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74 Nor does it det
15a5e 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a ect when the. *
15a5f 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 * cell content a
15a60 72 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20 rea exceeds the
15a61 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67 value in the pag
15a62 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68 e header. If th
15a63 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69 ese. ** situati
15a64 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20 ons arise, then
15a65 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72 subsequent inser
15a66 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67 t operations mig
15a67 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20 ht corrupt. **
15a68 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53 the freelist. S
15a69 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 o we do need to
15a6a 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70 check for corrup
15a6b 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e tion while scann
15a6c 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 ing. ** the fre
15a6d 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64 elist.. */. hd
15a6e 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
15a6f 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68 fset;. addr = h
15a70 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20 dr + 1;. iLast
15a71 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 = pPage->pBt->us
15a72 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 ableSize - 4;.
15a73 61 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69 assert( start<=i
15a74 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28 Last );. while(
15a75 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 (pbegin = get2b
15a76 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 yte(&data[addr])
15a77 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69 )<start && pbegi
15a78 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 n>0 ){. if( p
15a79 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a begin<addr+4 ){.
15a7a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a7b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a7c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 ;. }. addr
15a7d 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 = pbegin;. }.
15a7e 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73 if( pbegin>iLas
15a7f 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
15a80 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
15a81 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 KPT;. }. asser
15a82 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c t( pbegin>addr |
15a83 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 | pbegin==0 );.
15a84 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15a85 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 addr], start);.
15a86 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15a87 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b start], pbegin);
15a88 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
15a89 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 a[start+2], size
15a8a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
15a8b 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 e = pPage->nFree
15a8c 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 + (u16)size;..
15a8d 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a /* Coalesce adj
15a8e 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b acent free block
15a8f 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64 s */. addr = hd
15a90 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 r + 1;. while(
15a91 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 (pbegin = get2by
15a92 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 te(&data[addr]))
15a93 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e >0 ){. int pn
15a94 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20 ext, psize, x;.
15a95 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 assert( pbegi
15a96 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73 n>addr );. as
15a97 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 sert( pbegin<=pP
15a98 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
15a99 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e Size-4 );. pn
15a9a 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 ext = get2byte(&
15a9b 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20 data[pbegin]);.
15a9c 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62 psize = get2b
15a9d 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
15a9e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62 +2]);. if( pb
15a9f 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33 egin + psize + 3
15aa0 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65 >= pnext && pne
15aa1 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e xt>0 ){. in
15aa2 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d t frag = pnext -
15aa3 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b (pbegin+psize);
15aa4 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67 . if( (frag
15aa5 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e <0) || (frag>(in
15aa6 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29 t)data[hdr+7]) )
15aa7 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
15aa8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15aa9 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 BKPT;. }.
15aaa 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 data[hdr+7]
15aab 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 -= (u8)frag;.
15aac 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 x = get2byte(
15aad 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 &data[pnext]);.
15aae 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 put2byte(&d
15aaf 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b ata[pbegin], x);
15ab0 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 . x = pnext
15ab1 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 + get2byte(&dat
15ab2 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 a[pnext+2]) - pb
15ab3 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 egin;. put2
15ab4 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 byte(&data[pbegi
15ab5 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 n+2], x);. }e
15ab6 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 lse{. addr
15ab7 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a = pbegin;. }.
15ab8 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
15ab9 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
15aba 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 ea begins with a
15abb 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f freeblock, remo
15abc 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 ve it. */. if(
15abd 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 data[hdr+1]==dat
15abe 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 a[hdr+5] && data
15abf 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 [hdr+2]==data[hd
15ac0 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 r+6] ){. int
15ac1 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 top;. pbegin
15ac2 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15ac3 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 [hdr+1]);. me
15ac4 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 mcpy(&data[hdr+1
15ac5 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d ], &data[pbegin]
15ac6 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 , 2);. top =
15ac7 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
15ac8 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 dr+5]) + get2byt
15ac9 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 e(&data[pbegin+2
15aca 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 ]);. put2byte
15acb 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 (&data[hdr+5], t
15acc 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 op);. }. asser
15acd 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
15ace 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
15acf 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
15ad0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15ad1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 ;.}../*.** Decod
15ad2 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
15ad3 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 (the first byte
15ad4 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 of the header)
15ad5 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e for a page.** an
15ad6 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 d initialize fie
15ad7 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 lds of the MemPa
15ad8 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 ge structure acc
15ad9 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ordingly..**.**
15ada 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 Only the followi
15adb 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 ng combinations
15adc 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 are supported.
15add 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 Anything differe
15ade 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 nt.** indicates
15adf 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 a corrupt databa
15ae0 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 se files:.**.**
15ae1 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f PTF_ZERO
15ae2 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 DATA.**
15ae3 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 PTF_ZERODATA | P
15ae4 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 TF_LEAF.**
15ae5 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
15ae6 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 | PTF_INTKEY.**
15ae7 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 PTF_LEAF
15ae8 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 DATA | PTF_INTKE
15ae9 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a Y | PTF_LEAF.*/.
15aea 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 static int decod
15aeb 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a eFlags(MemPage *
15aec 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 pPage, int flagB
15aed 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 yte){. BtShared
15aee 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 *pBt; /* A
15aef 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 copy of pPage->p
15af0 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 Bt */.. assert(
15af1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15af2 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t==(pPage->pgno=
15af3 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b =1 ? 100 : 0) );
15af4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15af5 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
15af6 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
15af7 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 );. pPage->lea
15af8 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 f = (u8)(flagByt
15af9 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 e>>3); assert(
15afa 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 PTF_LEAF == 1<<3
15afb 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 );. flagByte &
15afc 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 = ~PTF_LEAF;. p
15afd 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
15afe 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e ze = 4-4*pPage->
15aff 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 leaf;. pBt = pP
15b00 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 age->pBt;. if(
15b01 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c flagByte==(PTF_L
15b02 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e EAFDATA | PTF_IN
15b03 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 TKEY) ){. pPa
15b04 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a ge->intKey = 1;.
15b05 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 pPage->hasDa
15b06 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 ta = pPage->leaf
15b07 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 ;. pPage->max
15b08 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 Local = pBt->max
15b09 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d Leaf;. pPage-
15b0a 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >minLocal = pBt-
15b0b 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 >minLeaf;. }els
15b0c 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d e if( flagByte==
15b0d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a PTF_ZERODATA ){.
15b0e 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 pPage->intKe
15b0f 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 y = 0;. pPage
15b10 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 ->hasData = 0;.
15b11 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 pPage->maxLoc
15b12 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 al = pBt->maxLoc
15b13 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d al;. pPage->m
15b14 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d inLocal = pBt->m
15b15 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 inLocal;. }else
15b16 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
15b17 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15b18 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
15b19 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
15b1a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
15b1b 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f e auxiliary info
15b1c 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 rmation for a di
15b1d 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 sk block..**.**
15b1e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
15b1f 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 on success. If
15b20 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 we see that the
15b21 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f page does.** no
15b22 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c t contain a well
15b23 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 -formed database
15b24 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 page, then retu
15b25 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f rn .** SQLITE_CO
15b26 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 RRUPT. Note tha
15b27 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 t a return of SQ
15b28 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 LITE_OK does not
15b29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 .** guarantee th
15b2a 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 at the page is w
15b2b 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 ell-formed. It
15b2c 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a only shows that.
15b2d 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 ** we failed to
15b2e 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 detect any corru
15b2f 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ption..*/.static
15b30 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61 int btreeInitPa
15b31 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
15b32 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 e){.. assert( p
15b33 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
15b34 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15b35 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15b36 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15b37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
15b38 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 ge->pgno==sqlite
15b39 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
15b3a 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15b3b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
15b3c 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 age == sqlite3Pa
15b3d 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
15b3e 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
15b3f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15b40 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 aData == sqlite3
15b41 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
15b42 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
15b43 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 . if( !pPage->i
15b44 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 sInit ){. u16
15b45 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
15b46 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 /* Address of a
15b47 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e freeblock within
15b48 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 pPage->aData[]
15b49 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 */. u8 hdr;
15b4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
15b4b 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 set to beginning
15b4c 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 of page header
15b4d 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b */. u8 *data;
15b4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 /* Equ
15b4f 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 al to pPage->aDa
15b50 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 ta */. BtShar
15b51 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
15b52 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 /* The main btre
15b53 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
15b54 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a u16 usableSiz
15b55 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 e; /* Amount
15b56 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 of usable space
15b57 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a on each page */.
15b58 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 u16 cellOffs
15b59 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 et; /* Offset
15b5a 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 from start of p
15b5b 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c age to first cel
15b5c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 l pointer */.
15b5d 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
15b5e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
15b5f 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e unused bytes on
15b60 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
15b61 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 u16 top;
15b62 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
15b63 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
15b64 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 ntent area */.
15b65 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 int iCellFirst
15b66 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c ; /* First al
15b67 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20 lowable cell or
15b68 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 freeblock offset
15b69 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c */. int iCel
15b6a 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 lLast; /* La
15b6b 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c st possible cell
15b6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 or freeblock of
15b6d 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74 fset */.. pBt
15b6e 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a = pPage->pBt;..
15b6f 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d hdr = pPage-
15b70 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 >hdrOffset;.
15b71 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
15b72 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63 ata;. if( dec
15b73 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 odeFlags(pPage,
15b74 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74 data[hdr]) ) ret
15b75 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15b76 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73 PT_BKPT;. ass
15b77 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 ert( pBt->pageSi
15b78 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e ze>=512 && pBt->
15b79 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 pageSize<=32768
15b7a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 );. pPage->ma
15b7b 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 skPage = pBt->pa
15b7c 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20 geSize - 1;.
15b7d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
15b7e 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65 = 0;. usable
15b7f 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
15b80 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 leSize;. pPag
15b81 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 e->cellOffset =
15b82 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72 cellOffset = hdr
15b83 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d + 12 - 4*pPage-
15b84 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d >leaf;. top =
15b85 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15b86 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61 hdr+5]);. pPa
15b87 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32 ge->nCell = get2
15b88 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
15b89 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ]);. if( pPag
15b8a 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c e->nCell>MX_CELL
15b8b 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f (pBt) ){. /
15b8c 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20 * To many cells
15b8d 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67 for a single pag
15b8e 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73 e. The page mus
15b8f 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a t be corrupt */.
15b90 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15b91 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15b92 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 ;. }. test
15b93 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65 case( pPage->nCe
15b94 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29 ll==MX_CELL(pBt)
15b95 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61 );.. /* A ma
15b96 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 lformed database
15b97 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73 page might caus
15b98 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73 e us to read pas
15b99 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a t the end. **
15b9a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61 of page when pa
15b9b 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a rsing a cell. .
15b9c 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
15b9d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
15b9e 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73 k of code checks
15b9f 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66 early to see if
15ba0 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a a cell extends.
15ba1 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 ** past the
15ba2 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f end of a page bo
15ba3 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 undary and cause
15ba4 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 s SQLITE_CORRUPT
15ba5 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72 to be . ** r
15ba6 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f eturned if it do
15ba7 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 es.. */. i
15ba8 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c CellFirst = cell
15ba9 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 Offset + 2*pPage
15baa 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65 ->nCell;. iCe
15bab 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 llLast = usableS
15bac 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66 ize - 4;.#if def
15bad 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
15bae 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c LE_OVERSIZE_CELL
15baf 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20 _CHECK). {.
15bb0 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
15bb1 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
15bb2 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 nto the cell poi
15bb3 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 nter array */.
15bb4 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20 int sz;
15bb5 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
15bb6 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20 a cell */..
15bb7 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
15bb8 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d af ) iCellLast--
15bb9 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
15bba 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b i<pPage->nCell;
15bbb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 i++){. p
15bbc 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 c = get2byte(&da
15bbd 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a ta[cellOffset+i*
15bbe 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 2]);. tes
15bbf 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c tcase( pc==iCell
15bc0 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 First );.
15bc1 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 testcase( pc==i
15bc2 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 CellLast );.
15bc3 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c if( pc<iCell
15bc4 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c First || pc>iCel
15bc5 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 lLast ){.
15bc6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15bc7 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15bc8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15bc9 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 sz = cellSizePt
15bca 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 r(pPage, &data[p
15bcb 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 c]);. tes
15bcc 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73 tcase( pc+sz==us
15bcd 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
15bce 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73 if( pc+sz>us
15bcf 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
15bd0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15bd1 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15bd2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15bd3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 }. if( !p
15bd4 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65 Page->leaf ) iCe
15bd5 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20 llLast++;. }
15bd6 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a .#endif.. /*
15bd7 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 Compute the tot
15bd8 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e al free space on
15bd9 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
15bda 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 pc = get2byte(&
15bdb 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 data[hdr+1]);.
15bdc 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 nFree = data[h
15bdd 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20 dr+7] + top;.
15bde 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a while( pc>0 ){.
15bdf 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20 u16 next,
15be0 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 size;. if(
15be1 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c pc<iCellFirst ||
15be2 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b pc>iCellLast ){
15be3 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 . /* Star
15be4 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20 t of free block
15be5 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 is off the page
15be6 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
15be7 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15be8 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
15be9 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
15bea 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 2byte(&data[pc])
15beb 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 ;. size = g
15bec 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
15bed 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
15bee 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c (next>0 && next<
15bef 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70 =pc+size+3) || p
15bf0 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a c+size>usableSiz
15bf1 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 e ){. /*
15bf2 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74 Free blocks must
15bf3 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 be in ascending
15bf4 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20 order. And the
15bf5 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a last byte of..**
15bf6 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20 the free-block
15bf7 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20 must lie on the
15bf8 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 database page.
15bf9 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
15bfa 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15bfb 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
15bfc 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46 nFree = nF
15bfd 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 ree + size;.
15bfe 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20 pc = next;.
15bff 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 }.. /* At th
15c00 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20 is point, nFree
15c01 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d contains the sum
15c02 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74 of the offset t
15c03 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20 o the start.
15c04 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 ** of the cell-c
15c05 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 ontent area plus
15c06 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
15c07 72 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e ree bytes within
15c08 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c . ** the cell
15c09 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49 -content area. I
15c0a 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65 f this is greate
15c0b 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c r than the usabl
15c0c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66 e-size. ** of
15c0d 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 the page, then
15c0e 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 the page must be
15c0f 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 corrupted. This
15c10 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20 check also.
15c11 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72 ** serves to ver
15c12 69 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66 ify that the off
15c13 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 set to the start
15c14 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e of the cell-con
15c15 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61 tent. ** area
15c16 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
15c17 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20 he page header,
15c18 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 lies within the
15c19 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
15c1a 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c if( nFree>usabl
15c1b 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 eSize ){. r
15c1c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15c1d 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 RUPT_BKPT; .
15c1e 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 }. pPage->nFr
15c1f 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65 ee = (u16)(nFree
15c20 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a - iCellFirst);.
15c21 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 pPage->isIni
15c22 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 t = 1;. }. ret
15c23 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
15c24 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 ../*.** Set up a
15c25 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 raw page so tha
15c26 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 t it looks like
15c27 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 a database page
15c28 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e holding.** no en
15c29 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tries..*/.static
15c2a 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d void zeroPage(M
15c2b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
15c2c 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 nt flags){. uns
15c2d 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
15c2e 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
15c2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
15c30 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
15c31 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d u8 hdr = pPage-
15c32 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 >hdrOffset;. u1
15c33 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 6 first;.. asse
15c34 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15c35 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 Pagenumber(pPage
15c36 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 ->pDbPage)==pPag
15c37 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 e->pgno );. ass
15c38 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
15c39 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d rGetExtra(pPage-
15c3a 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f >pDbPage) == (vo
15c3b 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 id*)pPage );. a
15c3c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15c3d 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 gerGetData(pPage
15c3e 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 ->pDbPage) == da
15c3f 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ta );. assert(
15c40 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
15c41 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
15c42 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
15c43 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15c44 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
15c45 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 ex) );.#ifdef SQ
15c46 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
15c47 54 45 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 TE. memset(&dat
15c48 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e a[hdr], 0, pBt->
15c49 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 usableSize - hdr
15c4a 29 3b 0a 23 65 6e 64 69 66 0a 20 20 64 61 74 61 );.#endif. data
15c4b 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c [hdr] = (char)fl
15c4c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 ags;. first = h
15c4d 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 dr + 8 + 4*((fla
15c4e 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 gs&PTF_LEAF)==0
15c4f 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 ?1:0);. memset(
15c50 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c &data[hdr+1], 0,
15c51 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 4);. data[hdr+
15c52 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 7] = 0;. put2by
15c53 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
15c54 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
15c55 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
15c56 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
15c57 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 ize - first;. d
15c58 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 ecodeFlags(pPage
15c59 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 , flags);. pPag
15c5a 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 e->hdrOffset = h
15c5b 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c dr;. pPage->cel
15c5c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b lOffset = first;
15c5d 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 . pPage->nOverf
15c5e 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 low = 0;. asser
15c5f 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 t( pBt->pageSize
15c60 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 >=512 && pBt->pa
15c61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b geSize<=32768 );
15c62 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 . pPage->maskPa
15c63 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ge = pBt->pageSi
15c64 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d ze - 1;. pPage-
15c65 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 >nCell = 0;. pP
15c66 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b age->isInit = 1;
15c67 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 .}.../*.** Conve
15c68 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 rt a DbPage obta
15c69 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 ined from the pa
15c6a 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 ger into a MemPa
15c6b 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 ge used by.** th
15c6c 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a e btree layer..*
15c6d 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 /.static MemPage
15c6e 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 *btreePageFromD
15c6f 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 bPage(DbPage *pD
15c70 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f bPage, Pgno pgno
15c71 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 , BtShared *pBt)
15c72 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
15c73 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 ge = (MemPage*)s
15c74 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
15c75 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 tra(pDbPage);.
15c76 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 pPage->aData = s
15c77 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
15c78 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ta(pDbPage);. p
15c79 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 Page->pDbPage =
15c7a 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 pDbPage;. pPage
15c7b 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 ->pBt = pBt;. p
15c7c 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e Page->pgno = pgn
15c7d 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f o;. pPage->hdrO
15c7e 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 ffset = pPage->p
15c7f 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
15c80 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ;. return pPage
15c81 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 ; .}../*.** Get
15c82 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
15c83 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 pager. Initiali
15c84 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 ze the MemPage.p
15c85 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 Bt and.** MemPag
15c86 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 e.aData elements
15c87 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a if needed..**.*
15c88 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 * If the noConte
15c89 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 nt flag is set,
15c8a 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 it means that we
15c8b 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
15c8c 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ut.** the conten
15c8d 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 t of the page at
15c8e 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 this time. So
15c8f 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 do not go to the
15c90 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 disk.** to fetc
15c91 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 h the content.
15c92 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 Just fill in the
15c93 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 content with ze
15c94 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 ros for now..**
15c95 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 If in the future
15c96 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 we call sqlite3
15c97 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 PagerWrite() on
15c98 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a this page, that.
15c99 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 ** means we have
15c9a 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 started to be c
15c9b 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 oncerned about c
15c9c 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 ontent and the d
15c9d 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 isk.** read shou
15c9e 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 ld occur at that
15c9f 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 point..*/.stati
15ca0 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 c int btreeGetPa
15ca1 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
15ca2 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 pBt, /* Th
15ca3 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e e btree */. Pgn
15ca4 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
15ca5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
15ca6 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 he page to fetch
15ca7 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
15ca8 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 ppPage, /* Re
15ca9 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e turn the page in
15caa 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 this parameter
15cab 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 */. int noConte
15cac 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 nt /* Do
15cad 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f not load page co
15cae 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f ntent if true */
15caf 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
15cb0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
15cb1 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15cb2 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15cb3 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15cb4 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15cb5 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 rAcquire(pBt->pP
15cb6 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 ager, pgno, (DbP
15cb7 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 age**)&pDbPage,
15cb8 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 noContent);. if
15cb9 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
15cba 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 ;. *ppPage = bt
15cbb 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15cbc 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
15cbd 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 pBt);. return
15cbe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15cbf 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
15cc0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
15cc1 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 er cache. If the
15cc2 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
15cc3 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 is not.** alread
15cc4 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 y in the pager c
15cc5 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c ache return NULL
15cc6 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 . Initialize the
15cc7 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
15cc8 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
15cc9 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
15cca 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
15ccb 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 MemPage *btreePa
15ccc 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 geLookup(BtShare
15ccd 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
15cce 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
15ccf 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 bPage;. assert(
15cd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15cd1 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15cd2 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 );. pDbPage = s
15cd3 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
15cd4 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 p(pBt->pPager, p
15cd5 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 gno);. if( pDbP
15cd6 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
15cd7 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 n btreePageFromD
15cd8 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
15cd9 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 gno, pBt);. }.
15cda 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
15cdb 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
15cdc 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
15cdd 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 ase file in page
15cde 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 s. If there is a
15cdf 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 ny kind of.** er
15ce0 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e ror, return ((un
15ce1 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a signed int)-1)..
15ce2 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 */.static Pgno p
15ce3 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 agerPagecount(Bt
15ce4 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15ce5 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a int nPage = -1;.
15ce6 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
15ce7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 rt( pBt->pPage1
15ce8 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
15ce9 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
15cea 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
15ceb 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 age);. assert(
15cec 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
15ced 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 nPage==-1 );.
15cee 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 return (Pgno)nPa
15cef 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 ge;.}../*.** Get
15cf0 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
15cf1 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 pager and initi
15cf2 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 alize it. This
15cf3 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 routine is just
15cf4 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 a.** convenience
15cf5 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
15cf6 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 separate calls t
15cf7 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 o btreeGetPage()
15cf8 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e and .** btreeIn
15cf9 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 itPage()..**.**
15cfa 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
15cfb 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c rs, then the val
15cfc 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 ue *ppPage is se
15cfd 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 t to is undefine
15cfe 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d d. It.** may rem
15cff 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f ain unchanged, o
15d00 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 r it may be set
15d01 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 to an invalid va
15d02 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lue..*/.static i
15d03 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 nt getAndInitPag
15d04 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
15d05 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt, /*
15d06 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
15d07 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
15d08 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
15d09 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
15d0a 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 e to get */. Me
15d0b 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 mPage **ppPage
15d0c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
15d0d 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 page pointer her
15d0e 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15d0f 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67 ;. TESTONLY( Pg
15d10 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67 no iLastPg = pag
15d11 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
15d12 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71 ; ). assert( sq
15d13 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15d14 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
15d15 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
15d16 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
15d17 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 ppPage, 0);. if
15d18 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15d19 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
15d1a 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67 eInitPage(*ppPag
15d1b 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
15d1c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15d1d 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
15d1e 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 ppPage);. }.
15d1f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
15d20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e requested page n
15d21 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72 umber was either
15d22 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 0 or greater th
15d23 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a an the page. **
15d24 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c number of the l
15d25 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ast page in the
15d26 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66 database, this f
15d27 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 unction should r
15d28 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 eturn. ** SQLIT
15d29 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d E_CORRUPT or som
15d2a 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69 e other error (i
15d2b 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 .e. SQLITE_FULL)
15d2c 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 . Check that thi
15d2d 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 s. ** is the ca
15d2e 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 se. */. assert
15d2f 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e ( (pgno>0 && pgn
15d30 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72 o<=iLastPg) || r
15d31 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
15d32 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f testcase( pgno
15d33 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==0 );. testcas
15d34 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 e( pgno==iLastPg
15d35 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 );.. return rc
15d36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
15d37 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 se a MemPage. T
15d38 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61 his should be ca
15d39 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 lled once for ea
15d3a 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c ch prior.** call
15d3b 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 to btreeGetPage
15d3c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
15d3d 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d releasePage(Mem
15d3e 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
15d3f 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 if( pPage ){.
15d40 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15d41 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 aData );. ass
15d42 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 ert( pPage->pBt
15d43 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15d44 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
15d45 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 tra(pPage->pDbPa
15d46 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 ge) == (void*)pP
15d47 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 age );. asser
15d48 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
15d49 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
15d4a 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 bPage)==pPage->a
15d4b 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 Data );. asse
15d4c 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15d4d 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
15d4e 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 t->mutex) );.
15d4f 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
15d50 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ef(pPage->pDbPag
15d51 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a e);. }.}../*.**
15d52 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 During a rollba
15d53 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 ck, when the pag
15d54 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 er reloads infor
15d55 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 mation into the
15d56 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 cache.** so that
15d57 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72 65 the cache is re
15d58 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 stored to its or
15d59 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 iginal state at
15d5a 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 the start of.**
15d5b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c the transaction,
15d5c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 for each page r
15d5d 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 estored this rou
15d5e 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a tine is called..
15d5f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
15d60 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 ne needs to rese
15d61 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74 61 t the extra data
15d62 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 section at the
15d63 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 end of the.** pa
15d64 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 ge to agree with
15d65 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 the restored da
15d66 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ta..*/.static vo
15d67 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 id pageReinit(Db
15d68 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 Page *pData){.
15d69 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
15d6a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 pPage = (MemPa
15d6b 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 ge *)sqlite3Page
15d6c 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 29 rGetExtra(pData)
15d6d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
15d6e 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
15d6f 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 3b ount(pData)>0 );
15d70 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 . if( pPage->is
15d71 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 Init ){. asse
15d72 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15d73 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
15d74 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 t->mutex) );.
15d75 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
15d76 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
15d77 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
15d78 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29 7b ount(pData)>1 ){
15d79 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 . /* pPage
15d7a 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 might not be a b
15d7b 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d tree page; it m
15d7c 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 ight be an overf
15d7d 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a low page. *
15d7e 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 * or ptrmap page
15d7f 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 2e or a free page.
15d80 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 In those cases
15d81 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a , the following.
15d82 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f ** call to
15d83 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
15d84 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 will likely ret
15d85 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15d86 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 PT.. ** But
15d87 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 no harm is done
15d88 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20 69 by this. And i
15d89 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72 74 t is very import
15d8a 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20 2a ant that. *
15d8b 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 * btreeInitPage(
15d8c 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 ) be called on e
15d8d 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65 20 very btree page
15d8e 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 so we make.
15d8f 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 ** the call for
15d90 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 74 every page that
15d91 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 comes in for re
15d92 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 -initing. */.
15d93 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 btreeInitPage
15d94 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 (pPage);. }.
15d95 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f }.}../*.** Invo
15d96 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 ke the busy hand
15d97 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 2e ler for a btree.
15d98 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
15d99 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 treeInvokeBusyHa
15d9a 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 67 ndler(void *pArg
15d9b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
15d9c 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a 29 Bt = (BtShared*)
15d9d 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 pArg;. assert(
15d9e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 pBt->db );. ass
15d9f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15da0 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d ex_held(pBt->db-
15da1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 >mutex) );. ret
15da2 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b urn sqlite3Invok
15da3 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70 42 eBusyHandler(&pB
15da4 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c t->db->busyHandl
15da5 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 er);.}../*.** Op
15da6 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 en a database fi
15da7 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 le..** .** zFile
15da8 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 name is the name
15da9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
15daa 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 file. If zFile
15dab 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 name is NULL.**
15dac 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 77 a new database w
15dad 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d ith a random nam
15dae 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 e is created. T
15daf 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d his randomly nam
15db0 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ed.** database f
15db1 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 ile will be dele
15db2 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 ted when sqlite3
15db3 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20 BtreeClose() is
15db4 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 called..** If zF
15db5 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d ilename is ":mem
15db6 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e ory:" then an in
15db7 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
15db8 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 is created.** t
15db9 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 hat is automatic
15dba 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77 ally destroyed w
15dbb 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 hen it is closed
15dbc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 ..**.** If the d
15dbd 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 61 atabase is alrea
15dbe 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 dy opened in the
15dbf 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
15dc0 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 onnection.** and
15dc1 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 65 we are in share
15dc2 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 d cache mode, th
15dc3 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c en the open will
15dc4 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a fail with an.**
15dc5 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
15dc6 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 61 NT error. We ca
15dc7 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f nnot allow two o
15dc8 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 0a r more BtShared.
15dc9 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 ** objects in th
15dca 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
15dcb 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 connection since
15dcc 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c doing so will l
15dcd 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 ead.** to proble
15dce 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e ms with locking.
15dcf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15dd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15dd1 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 reeOpen(. const
15dd2 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
15dd3 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 , /* Name of th
15dd4 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e e file containin
15dd5 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61 g the BTree data
15dd6 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 base */. sqlite
15dd7 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
15dd8 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 /* Associated
15dd9 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
15dda 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 */. Btree **ppB
15ddb 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 tree, /*
15ddc 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 Pointer to new B
15ddd 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 tree object writ
15dde 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ten here */. in
15ddf 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
15de0 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 /* Options
15de1 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 */. int vfsFla
15de2 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a gs /*
15de3 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 Flags passed th
15de4 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 rough to sqlite3
15de5 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a _vfs.xOpen() */.
15de6 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
15de7 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
15de8 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 /* The VFS t
15de9 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 62 o use for this b
15dea 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 tree */. BtShar
15deb 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 ed *pBt = 0;
15dec 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 /* Shar
15ded 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65 ed part of btree
15dee 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
15def 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20 Btree *p;
15df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15df1 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 * Handle to retu
15df2 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f rn */. sqlite3_
15df3 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e mutex *mutexOpen
15df4 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e = 0; /* Preven
15df5 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 ts a race condit
15df6 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 33 ion. Ticket #353
15df7 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 7 */. int rc =
15df8 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
15df9 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
15dfa 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 66 code from this f
15dfb 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 unction */. u8
15dfc 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 nReserve;
15dfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
15dfe 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 70 yte of unused sp
15dff 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 ace on each page
15e00 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
15e01 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 30 har zDbHeader[10
15e02 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 0]; /* Database
15e03 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 header content
15e04 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 */.. /* Set the
15e05 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 variable isMemd
15e06 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61 6e b to true for an
15e07 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
15e08 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 ase, or . ** fa
15e09 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 lse for a file-b
15e0a 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 ased database. T
15e0b 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e his symbol is on
15e0c 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a 20 ly required if.
15e0d 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74 68 ** either of th
15e0e 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f 72 e shared-data or
15e0f 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61 74 autovacuum feat
15e10 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 ures are compile
15e11 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 d . ** into the
15e12 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 library.. */.#
15e13 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
15e14 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
15e15 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 ACHE) || !define
15e16 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 d(SQLITE_OMIT_AU
15e17 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66 64 TOVACUUM). #ifd
15e18 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ef SQLITE_OMIT_M
15e19 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 EMORYDB. cons
15e1a 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 t int isMemdb =
15e1b 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 63 0;. #else. c
15e1c 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 onst int isMemdb
15e1d 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 = zFilename &&
15e1e 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d !strcmp(zFilenam
15e1f 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a e, ":memory:");.
15e20 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a #endif.#endif.
15e21 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 . assert( db!=0
15e22 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
15e23 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15e24 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a (db->mutex) );..
15e25 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 pVfs = db->pVf
15e26 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 s;. p = sqlite3
15e27 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
15e28 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 28 f(Btree));. if(
15e29 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !p ){. retur
15e2a 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
15e2b 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 }. p->inTrans
15e2c 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 = TRANS_NONE;.
15e2d 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 p->db = db;.#if
15e2e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15e2f 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
15e30 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d p->lock.pBtree =
15e31 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 p;. p->lock.iT
15e32 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 able = 1;.#endif
15e33 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
15e34 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15e35 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
15e36 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15e37 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 _DISKIO). /*.
15e38 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 ** If this Btree
15e39 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 is a candidate
15e3a 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 for shared cache
15e3b 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e , try to find an
15e3c 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 . ** existing B
15e3d 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 tShared object t
15e3e 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 hat we can share
15e3f 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 with. */. if(
15e40 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a isMemdb==0 && z
15e41 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c Filename && zFil
15e42 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 ename[0] ){.
15e43 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20 53 if( vfsFlags & S
15e44 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 QLITE_OPEN_SHARE
15e45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20 20 DCACHE ){.
15e46 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d int nFullPathnam
15e47 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 e = pVfs->mxPath
15e48 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 name+1;. ch
15e49 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d ar *zFullPathnam
15e4a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f e = sqlite3Mallo
15e4b 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 c(nFullPathname)
15e4c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
15e4d 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 mutex *mutexShar
15e4e 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 ed;. p->sha
15e4f 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 rable = 1;.
15e50 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e if( !zFullPathn
15e51 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ame ){. s
15e52 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
15e53 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
15e54 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
15e55 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
15e56 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e3OsFullPathname
15e57 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 (pVfs, zFilename
15e58 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c , nFullPathname,
15e59 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b zFullPathname);
15e5a 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 6e . mutexOpen
15e5b 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
15e5c 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
15e5d 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a X_STATIC_OPEN);.
15e5e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
15e5f 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f tex_enter(mutexO
15e60 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 pen);. mute
15e61 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 xShared = sqlite
15e62 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
15e63 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
15e64 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 73 MASTER);. s
15e65 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
15e66 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b er(mutexShared);
15e67 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d 47 . for(pBt=G
15e68 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c LOBAL(BtShared*,
15e69 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 sqlite3SharedCac
15e6a 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 42 heList); pBt; pB
15e6b 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 t=pBt->pNext){.
15e6c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
15e6d 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 Bt->nRef>0 );.
15e6e 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 72 if( 0==str
15e6f 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d cmp(zFullPathnam
15e70 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 e, sqlite3PagerF
15e71 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 ilename(pBt->pPa
15e72 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 ger)).
15e73 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
15e74 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e 70 3PagerVfs(pBt->p
15e75 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a Pager)==pVfs ){.
15e76 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 int iD
15e77 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 b;. for
15e78 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 (iDb=db->nDb-1;
15e79 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a iDb>=0; iDb--){.
15e7a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72 65 Btre
15e7b 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 64 e *pExisting = d
15e7c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b b->aDb[iDb].pBt;
15e7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
15e7e 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70 45 pExisting && pE
15e7f 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 xisting->pBt==pB
15e80 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
15e81 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15e82 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 _leave(mutexShar
15e83 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ed);.
15e84 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15e85 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e _leave(mutexOpen
15e86 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
15e87 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 sqlite3_free(zF
15e88 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 ullPathname);.
15e89 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
15e8a 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
15e8b 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
15e8c 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 n SQLITE_CONSTRA
15e8d 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 INT;.
15e8e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
15e8f 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 p->pBt
15e90 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 = pBt;.
15e91 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 pBt->nRef++;.
15e92 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
15e93 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
15e94 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15e95 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
15e96 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73 Shared);. s
15e97 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c qlite3_free(zFul
15e98 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 lPathname);.
15e99 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
15e9a 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a DEBUG. else{.
15e9b 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 /* In debu
15e9c 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 g mode, we mark
15e9d 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64 all persistent d
15e9e 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61 72 atabases as shar
15e9f 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 able. ** ev
15ea0 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 en when they are
15ea1 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72 not. This exer
15ea2 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e cises the lockin
15ea3 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 g code and.
15ea4 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f ** gives more o
15ea5 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 pportunity for a
15ea6 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d sserts(sqlite3_m
15ea7 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 utex_held()).
15ea8 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 ** statements
15ea9 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 to find locking
15eaa 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 problems..
15eab 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 */. p->sha
15eac 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d rable = 1;. }
15ead 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 .#endif. }.#end
15eae 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20 if. if( pBt==0
15eaf 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a ){. /*. **
15eb0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
15eb1 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 sserts make sure
15eb2 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65 73 that structures
15eb3 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74 72 used by the btr
15eb4 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 ee are. ** th
15eb5 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54 e right size. T
15eb6 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64 20 his is to guard
15eb7 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61 against size cha
15eb8 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c 74 nges that result
15eb9 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d . ** when com
15eba 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 piling on a diff
15ebb 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74 75 erent architectu
15ebc 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 re.. */. a
15ebd 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36 ssert( sizeof(i6
15ebe 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 4)==8 || sizeof(
15ebf 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 i64)==4 );. a
15ec0 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 36 ssert( sizeof(u6
15ec1 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 4)==8 || sizeof(
15ec2 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 u64)==4 );. a
15ec3 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33 ssert( sizeof(u3
15ec4 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 2)==4 );. ass
15ec5 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 29 ert( sizeof(u16)
15ec6 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==2 );. asser
15ec7 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d t( sizeof(Pgno)=
15ec8 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 =4 );. . pBt
15ec9 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
15eca 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 Zero( sizeof(*pB
15ecb 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42 t) );. if( pB
15ecc 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 t==0 ){. rc
15ecd 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
15ece 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 . goto btre
15ecf 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 e_open_out;.
15ed0 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
15ed1 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 73 e3PagerOpen(pVfs
15ed2 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 , &pBt->pPager,
15ed3 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 zFilename,.
15ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15ed5 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 2c EXTRA_SIZE,
15ed6 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 flags, vfsFlags
15ed7 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 , pageReinit);.
15ed8 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15ed9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
15eda 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 = sqlite3PagerR
15edb 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70 42 eadFileheader(pB
15edc 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 t->pPager,sizeof
15edd 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62 48 (zDbHeader),zDbH
15ede 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 eader);. }.
15edf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15ee0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
15ee1 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 o btree_open_out
15ee2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d ;. }. pBt-
15ee3 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 >db = db;. sq
15ee4 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 lite3PagerSetBus
15ee5 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 yhandler(pBt->pP
15ee6 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b ager, btreeInvok
15ee7 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 eBusyHandler, pB
15ee8 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d t);. p->pBt =
15ee9 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 pBt;. . pBt
15eea 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 ->pCursor = 0;.
15eeb 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d pBt->pPage1 =
15eec 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 0;. pBt->rea
15eed 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 dOnly = sqlite3P
15eee 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 agerIsreadonly(p
15eef 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
15ef0 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d pBt->pageSize =
15ef1 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48 65 get2byte(&zDbHe
15ef2 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 ader[16]);. i
15ef3 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 f( pBt->pageSize
15ef4 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 <512 || pBt->pag
15ef5 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 eSize>SQLITE_MAX
15ef6 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 _PAGE_SIZE.
15ef7 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 || ((pBt->pa
15ef8 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 geSize-1)&pBt->p
15ef9 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 ageSize)!=0 ){.
15efa 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 pBt->pageSi
15efb 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 ze = 0;.#ifndef
15efc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
15efd 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 VACUUM. /*
15efe 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d If the magic nam
15eff 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c e ":memory:" wil
15f00 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d l create an in-m
15f01 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 emory database,
15f02 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 then. ** le
15f03 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 75 ave the autoVacu
15f04 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 6f um mode at 0 (do
15f05 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d not auto-vacuum
15f06 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 20 ), even if.
15f07 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ** SQLITE_DEFAU
15f08 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 LT_AUTOVACUUM is
15f09 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 true. On the ot
15f0a 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 her hand, if.
15f0b 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 ** SQLITE_OMI
15f0c 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 62 T_MEMORYDB has b
15f0d 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 65 een defined, the
15f0e 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 n ":memory:" is
15f0f 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 just a. **
15f10 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d regular file-nam
15f11 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
15f12 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 the auto-vacuum
15f13 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 6e applies as per n
15f14 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a ormal.. */.
15f15 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 6e if( zFilen
15f16 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 20 ame && !isMemdb
15f17 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
15f18 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 autoVacuum = (SQ
15f19 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
15f1a 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 OVACUUM ? 1 : 0)
15f1b 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 ;. pBt->i
15f1c 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 4c ncrVacuum = (SQL
15f1d 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
15f1e 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 VACUUM==2 ? 1 :
15f1f 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 0);. }.#end
15f20 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 if. nReserv
15f21 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
15f22 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 {. nReserve
15f23 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 5d = zDbHeader[20]
15f24 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 ;. pBt->pag
15f25 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a eSizeFixed = 1;.
15f26 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15f27 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
15f28 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 pBt->autoVa
15f29 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
15f2a 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b (&zDbHeader[36 +
15f2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 4*4])?1:0);.
15f2c 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
15f2d 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 um = (get4byte(&
15f2e 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37 zDbHeader[36 + 7
15f2f 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 *4])?1:0);.#endi
15f30 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d f. }. rc =
15f31 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
15f32 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 Pagesize(pBt->pP
15f33 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 ager, &pBt->page
15f34 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b Size, nReserve);
15f35 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
15f36 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
15f37 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 t;. pBt->usab
15f38 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 leSize = pBt->pa
15f39 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 geSize - nReserv
15f3a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 e;. assert( (
15f3b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 pBt->pageSize &
15f3c 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 7)==0 ); /* 8-b
15f3d 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 yte alignment of
15f3e 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 pageSize */.
15f3f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
15f40 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15f41 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
15f42 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15f43 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 DISKIO). /* A
15f44 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 dd the new BtSha
15f45 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 red object to th
15f46 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 e linked list sh
15f47 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 arable BtShareds
15f48 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
15f49 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
15f4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
15f4b 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 tex *mutexShared
15f4c 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 ;. pBt->nRe
15f4d 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 f = 1;. mut
15f4e 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 exShared = sqlit
15f4f 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15f50 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
15f51 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 _MASTER);.
15f52 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41 if( SQLITE_THREA
15f53 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 33 DSAFE && sqlite3
15f54 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f GlobalConfig.bCo
15f55 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 reMutex ){.
15f56 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 pBt->mutex =
15f57 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
15f58 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 c(SQLITE_MUTEX_F
15f59 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66 AST);. if
15f5a 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 ( pBt->mutex==0
15f5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
15f5c 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
15f5d 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 db->ma
15f5e 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
15f5f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 goto b
15f60 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
15f61 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
15f62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15f63 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
15f64 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 Shared);. p
15f65 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 Bt->pNext = GLOB
15f66 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15f67 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15f68 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 ist);. GLOB
15f69 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15f6a 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15f6b 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 ist) = pBt;.
15f6c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15f6d 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 leave(mutexShare
15f6e 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 d);. }.#endif
15f6f 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e . }..#if !defin
15f70 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
15f71 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
15f72 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15f73 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f OMIT_DISKIO). /
15f74 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 72 * If the new Btr
15f75 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 62 ee uses a sharab
15f76 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 68 le pBtShared, th
15f77 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a en link the new.
15f78 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 ** Btree into
15f79 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 the list of all
15f7a 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 sharable Btrees
15f7b 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6e for the same con
15f7c 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 nection.. ** Th
15f7d 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 69 e list is kept i
15f7e 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
15f7f 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 73 r by pBt address
15f80 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
15f81 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
15f82 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 65 int i;. Btree
15f83 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 *pSib;. for(
15f84 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
15f85 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
15f86 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b (pSib = db->aDb[
15f87 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53 i].pBt)!=0 && pS
15f88 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a ib->sharable ){.
15f89 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
15f8a 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 Sib->pPrev ){ pS
15f8b 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 ib = pSib->pPrev
15f8c 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
15f8d 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 p->pBt<pSib->pBt
15f8e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
15f8f 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 >pNext = pSib;.
15f90 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65 p->pPre
15f91 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 v = 0;.
15f92 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 pSib->pPrev = p
15f93 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
15f94 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
15f95 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 ( pSib->pNext &&
15f96 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 pSib->pNext->pB
15f97 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 t<p->pBt ){.
15f98 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 70 pSib = p
15f99 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Sib->pNext;.
15f9a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15f9b 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 p->pNext = pSi
15f9c 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 b->pNext;.
15f9d 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 p->pPrev = p
15f9e 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Sib;. i
15f9f 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 f( p->pNext ){.
15fa0 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e p->pN
15fa1 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a ext->pPrev = p;.
15fa2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
15fa3 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 pSib->pNex
15fa4 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d t = p;. }
15fa5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
15fa6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
15fa7 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 }.#endif. *ppBt
15fa8 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f ree = p;..btree_
15fa9 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 open_out:. if(
15faa 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15fab 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20 . if( pBt &&
15fac 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 pBt->pPager ){.
15fad 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
15fae 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 rClose(pBt->pPag
15faf 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 er);. }. s
15fb0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
15fb1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
15fb2 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 ee(p);. *ppBt
15fb3 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 ree = 0;. }. i
15fb4 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a f( mutexOpen ){.
15fb5 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
15fb6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
15fb7 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 utexOpen) );.
15fb8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
15fb9 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b eave(mutexOpen);
15fba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
15fbb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 ;.}../*.** Decre
15fbc 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 ment the BtShare
15fbd 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 d.nRef counter.
15fbe 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 When it reaches
15fbf 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 zero,.** remove
15fc0 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 the BtShared st
15fc1 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 ructure from the
15fc2 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 sharing list.
15fc3 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 Return.** true i
15fc4 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e f the BtShared.n
15fc5 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 Ref counter reac
15fc6 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 hes zero and ret
15fc7 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 urn.** false if
15fc8 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 it is still posi
15fc9 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tive..*/.static
15fca 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 int removeFromSh
15fcb 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 aringList(BtShar
15fcc 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 ed *pBt){.#ifnde
15fcd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
15fce 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c ARED_CACHE. sql
15fcf 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 ite3_mutex *pMas
15fd0 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ter;. BtShared
15fd1 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 *pList;. int re
15fd2 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 moved = 0;.. as
15fd3 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15fd4 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d tex_notheld(pBt-
15fd5 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 >mutex) );. pMa
15fd6 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 ster = sqlite3Mu
15fd7 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
15fd8 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
15fd9 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f TER);. sqlite3_
15fda 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 mutex_enter(pMas
15fdb 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 ter);. pBt->nRe
15fdc 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e f--;. if( pBt->
15fdd 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 nRef<=0 ){. i
15fde 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 f( GLOBAL(BtShar
15fdf 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
15fe0 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 dCacheList)==pBt
15fe1 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c ){. GLOBAL
15fe2 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
15fe3 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
15fe4 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b t) = pBt->pNext;
15fe5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
15fe6 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c pList = GLOBAL
15fe7 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 (BtShared*,sqlit
15fe8 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
15fe9 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 t);. while(
15fea 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26 ALWAYS(pList) &
15feb 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d & pList->pNext!=
15fec 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 pBt ){. p
15fed 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 List=pList->pNex
15fee 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
15fef 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73 if( ALWAYS(pLis
15ff0 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c t) ){. pL
15ff1 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 ist->pNext = pBt
15ff2 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
15ff3 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 . }. if( S
15ff4 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
15ff5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
15ff6 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74 3_mutex_free(pBt
15ff7 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a ->mutex);. }.
15ff8 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b removed = 1;
15ff9 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
15ffa 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 utex_leave(pMast
15ffb 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 er);. return re
15ffc 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 moved;.#else. r
15ffd 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a eturn 1;.#endif.
15ffe 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
15fff 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 re pBt->pTmpSpac
16000 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 e points to an a
16001 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a llocation of .**
16002 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 MX_CELL_SIZE(pB
16003 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 t) bytes..*/.sta
16004 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 tic void allocat
16005 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 eTempSpace(BtSha
16006 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 red *pBt){. if(
16007 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 !pBt->pTmpSpace
16008 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d ){. pBt->pTm
16009 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 pSpace = sqlite3
1600a 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d PageMalloc( pBt-
1600b 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d >pageSize );. }
1600c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 .}../*.** Free t
1600d 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 he pBt->pTmpSpac
1600e 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a e allocation.*/.
1600f 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
16010 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 TempSpace(BtShar
16011 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 ed *pBt){. sqli
16012 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 74 te3PageFree( pBt
16013 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
16014 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
16015 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 0;.}../*.** Clo
16016 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 se an open datab
16017 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 ase and invalida
16018 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a te all cursors..
16019 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1601a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1601b 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 eeClose(Btree *p
1601c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
1601d 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
1601e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a tCursor *pCur;..
1601f 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 /* Close all c
16020 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 ursors opened vi
16021 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 a this handle.
16022 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c */. assert( sql
16023 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
16024 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
16025 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
16026 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72 20 nter(p);. pCur
16027 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a = pBt->pCursor;.
16028 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 7b while( pCur ){
16029 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 . BtCursor *p
1602a 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 Tmp = pCur;.
1602b 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 pCur = pCur->pNe
1602c 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d 70 xt;. if( pTmp
1602d 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
1602e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1602f 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 6d eCloseCursor(pTm
16030 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 p);. }. }..
16031 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 /* Rollback any
16032 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 active transact
16033 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 65 ion and free the
16034 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 handle structur
16035 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c e.. ** The call
16036 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
16037 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 Rollback() drops
16038 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 any table-locks
16039 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 held by. ** th
1603a 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a is handle.. */.
1603b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f sqlite3BtreeRo
1603c 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c llback(p);. sql
1603d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1603e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 );.. /* If ther
1603f 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 e are still othe
16040 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 r outstanding re
16041 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
16042 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 2a shared-btree. *
16043 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 74 * structure, ret
16044 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d urn now. The rem
16045 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 70 ainder of this p
16046 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 20 rocedure cleans
16047 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 61 . ** up the sha
16048 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a red-btree.. */.
16049 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
1604a 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d tToLock==0 && p-
1604b 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 >locked==0 );.
1604c 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 if( !p->sharable
1604d 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 || removeFromSh
1604e 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 29 aringList(pBt) )
1604f 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 74 {. /* The pBt
16050 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e is no longer on
16051 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 the sharing lis
16052 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 63 t, so we can acc
16053 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 ess. ** it wi
16054 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
16055 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e 0a hold the mutex..
16056 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c **. ** Cl
16057 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 ean out and dele
16058 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 te the BtShared
16059 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 object.. */.
1605a 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
1605b 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 >pCursor );.
1605c 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 sqlite3PagerClos
1605d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a e(pBt->pPager);.
1605e 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 72 if( pBt->xFr
1605f 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 2d eeSchema && pBt-
16060 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 >pSchema ){.
16061 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 pBt->xFreeSche
16062 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 ma(pBt->pSchema)
16063 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
16064 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 te3_free(pBt->pS
16065 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 chema);. free
16066 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
16067 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
16068 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e (pBt);. }..#ifn
16069 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1606a 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 61 SHARED_CACHE. a
1606b 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
1606c 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Lock==0 );. ass
1606d 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d ert( p->locked==
1606e 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 0 );. if( p->pP
1606f 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e rev ) p->pPrev->
16070 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 pNext = p->pNext
16071 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 ;. if( p->pNext
16072 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 ) p->pNext->pPr
16073 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 ev = p->pPrev;.#
16074 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
16075 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 _free(p);. retu
16076 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
16077 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
16078 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e e limit on the n
16079 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 umber of pages a
1607a 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63 61 llowed in the ca
1607b 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d che..**.** The m
1607c 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
1607d 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73 20 cache pages is
1607e 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f 6c set to the absol
1607f 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 ute.** value of
16080 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50 61 mxPage. If mxPa
16081 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 ge is negative,
16082 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a the pager will.*
16083 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63 68 * operate asynch
16084 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 ronously - it wi
16085 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 ll not stop to d
16086 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f o fsync()s.** to
16087 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73 20 insure data is
16088 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 written to the d
16089 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66 6f isk surface befo
1608a 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 re.** continuing
1608b 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 . Transactions
1608c 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 still work if sy
1608d 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 nchronous is off
1608e 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 ,.** and the dat
1608f 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 abase cannot be
16090 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 69 corrupted if thi
16091 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 s program.** cra
16092 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74 68 shes. But if th
16093 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
16094 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74 68 em crashes or th
16095 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 ere is.** an abr
16096 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75 72 upt power failur
16097 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f e when synchrono
16098 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20 64 us is off, the d
16099 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 atabase.** could
1609a 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 be left in an i
1609b 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 nconsistent and
1609c 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73 74 unrecoverable st
1609d 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e ate..** Synchron
1609e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65 66 ous is on by def
1609f 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73 65 ault so database
160a0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 6e corruption is n
160a1 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 ot.** normally a
160a2 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 worry..*/.SQLIT
160a3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
160a4 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
160a5 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c heSize(Btree *p,
160a6 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 int mxPage){.
160a7 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
160a8 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 p->pBt;. assert
160a9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
160aa 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
160ab 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 x) );. sqlite3B
160ac 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
160ad 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
160ae 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 achesize(pBt->pP
160af 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 ager, mxPage);.
160b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
160b1 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
160b2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
160b3 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 77 .** Change the w
160b4 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 65 ay data is synce
160b5 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 d to disk in ord
160b6 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 6f er to increase o
160b7 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f r decrease.** ho
160b8 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 62 w well the datab
160b9 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d 61 ase resists dama
160ba 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 ge due to OS cra
160bb 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a shes and power.*
160bc 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 * failures. Lev
160bd 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d 65 el 1 is the same
160be 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 as asynchronous
160bf 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 (no syncs() occ
160c0 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 ur and.** there
160c1 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 62 is a high probab
160c2 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 29 ility of damage)
160c3 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 65 Level 2 is the
160c4 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 65 default. There
160c5 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c 6f .** is a very lo
160c6 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 w but non-zero p
160c7 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 robability of da
160c8 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 mage. Level 3 r
160c9 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 educes the.** pr
160ca 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d obability of dam
160cb 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f age to near zero
160cc 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 74 but with a writ
160cd 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 e performance re
160ce 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e duction..*/.#ifn
160cf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
160d0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 PAGER_PRAGMAS.SQ
160d1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
160d2 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
160d3 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 SafetyLevel(Btre
160d4 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c e *p, int level,
160d5 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a int fullSync){.
160d6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
160d7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 = p->pBt;. asse
160d8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
160d9 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
160da 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
160db 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
160dc 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
160dd 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 74 tSafetyLevel(pBt
160de 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c ->pPager, level,
160df 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71 fullSync);. sq
160e0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
160e1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
160e2 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
160e3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
160e4 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e RUE if the given
160e5 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 6f btree is set to
160e6 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 2e safety level 1.
160e7 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f In other.** wo
160e8 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 45 rds, return TRUE
160e9 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 if no sync() oc
160ea 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 6b curs on the disk
160eb 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
160ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
160ed 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 lite3BtreeSyncDi
160ee 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 29 sabled(Btree *p)
160ef 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
160f0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e t = p->pBt;. in
160f1 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
160f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
160f3 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
160f4 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 42 ); . sqlite3B
160f5 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
160f6 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 70 assert( pBt && p
160f7 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 Bt->pPager );.
160f8 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
160f9 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 rNosync(pBt->pPa
160fa 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ger);. sqlite3B
160fb 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
160fc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 return rc;.}..#i
160fd 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
160fe 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
160ff 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 GMAS) || !define
16100 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 d(SQLITE_OMIT_VA
16101 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e CUUM)./*.** Chan
16102 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 ge the default p
16103 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 68 ages size and th
16104 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 e number of rese
16105 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20 70 rved bytes per p
16106 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 age..** Or, if t
16107 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 73 he page size has
16108 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69 already been fi
16109 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 xed, return SQLI
1610a 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 TE_READONLY .**
1610b 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 without changing
1610c 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a anything..**.**
1610d 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d The page size m
1610e 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
1610f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20 f 2 between 512
16110 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 74 and 65536. If t
16111 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 he page.** size
16112 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f supplied does no
16113 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e 73 t meet this cons
16114 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 20 traint then the
16115 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 page size is not
16116 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a .** changed..**.
16117 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 72 ** Page sizes ar
16118 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f e constrained to
16119 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 be a power of t
1611a 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 wo so that the r
1611b 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 egion.** of the
1611c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
1611d 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 ed for locking (
1611e 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e beginning at PEN
1611f 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 DING_BYTE,.** th
16120 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 e first byte pas
16121 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 61 t the 1GB bounda
16122 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 20 ry, 0x40000000)
16123 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a needs to occur.*
16124 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 * at the beginni
16125 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a ng of a page..**
16126 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 .** If parameter
16127 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 73 nReserve is les
16128 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 s than zero, the
16129 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1612a 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 reserved.** byte
1612b 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c 65 s per page is le
1612c 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a ft unchanged..**
1612d 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 21 .** If the iFix!
1612e 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 =0 then the page
1612f 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20 69 SizeFixed flag i
16130 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 68 s set so that th
16131 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 e page size.** a
16132 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f nd autovacuum mo
16133 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 de can no longer
16134 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a be changed..*/.
16135 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16136 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
16137 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
16138 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a *p, int pageSiz
16139 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c e, int nReserve,
1613a 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 6e int iFix){. in
1613b 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1613c 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
1613d 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 t = p->pBt;. as
1613e 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d sert( nReserve>=
1613f 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d -1 && nReserve<=
16140 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 255 );. sqlite3
16141 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
16142 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 if( pBt->pageSi
16143 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 73 zeFixed ){. s
16144 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16145 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 (p);. return
16146 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
16147 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 . }. if( nRese
16148 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 rve<0 ){. nRe
16149 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 serve = pBt->pag
1614a 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 eSize - pBt->usa
1614b 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 bleSize;. }. a
1614c 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e ssert( nReserve>
1614d 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d =0 && nReserve<=
1614e 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61 67 255 );. if( pag
1614f 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 eSize>=512 && pa
16150 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d geSize<=SQLITE_M
16151 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 0a AX_PAGE_SIZE &&.
16152 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53 69 ((pageSi
16153 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 3d ze-1)&pageSize)=
16154 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
16155 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 ( (pageSize & 7)
16156 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
16157 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 20 t( !pBt->pPage1
16158 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 && !pBt->pCursor
16159 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 );. pBt->pag
1615a 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67 eSize = (u16)pag
1615b 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 eSize;. freeT
1615c 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 empSpace(pBt);.
1615d 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }. rc = sqlite
1615e 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
1615f 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 e(pBt->pPager, &
16160 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e pBt->pageSize, n
16161 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74 2d Reserve);. pBt-
16162 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 >usableSize = pB
16163 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 75 t->pageSize - (u
16164 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 16)nReserve;. i
16165 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e 70 f( iFix ) pBt->p
16166 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 ageSizeFixed = 1
16167 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16168 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
16169 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1616a 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 Return the curre
1616b 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 ntly defined pag
1616c 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 e size.*/.SQLITE
1616d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1616e 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
1616f 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a Size(Btree *p){.
16170 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d return p->pBt-
16171 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a >pageSize;.}../*
16172 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
16173 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
16174 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 f space at the e
16175 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 nd of every page
16176 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 that.** are int
16177 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e entually left un
16178 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 used. This is t
16179 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70 he "reserved" sp
1617a 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 ace that is.** s
1617b 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79 ometimes used by
1617c 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
1617d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1617e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
1617f 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20 etReserve(Btree
16180 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 *p){. int n;.
16181 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
16182 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 r(p);. n = p->p
16183 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 Bt->pageSize - p
16184 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
16185 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 e;. sqlite3Btre
16186 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16187 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
16188 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 Set the maximum
16189 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 page count for a
1618a 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50 database if mxP
1618b 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e age is positive.
1618c 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 .** No changes a
1618d 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67 re made if mxPag
1618e 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69 e is 0 or negati
1618f 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 ve..** Regardles
16190 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f s of the value o
16191 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e f mxPage, return
16192 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
16193 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 e count..*/.SQLI
16194 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16195 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 qlite3BtreeMaxPa
16196 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 geCount(Btree *p
16197 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
16198 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 int n;. sqlite
16199 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1619a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 n = sqlite3Pag
1619b 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 erMaxPageCount(p
1619c 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d ->pBt->pPager, m
1619d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 xPage);. sqlite
1619e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1619f 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 return n;.}.#e
161a0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
161a1 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 (SQLITE_OMIT_PAG
161a2 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 ER_PRAGMAS) || !
161a3 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
161a4 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a MIT_VACUUM) */..
161a5 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
161a6 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 'auto-vacuum' p
161a7 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64 roperty of the d
161a8 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 atabase. If the
161a9 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 'autoVacuum'.**
161aa 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
161ab 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f -zero, then auto
161ac 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 -vacuum mode is
161ad 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f enabled. If zero
161ae 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 , it.** is disab
161af 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 led. The default
161b0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61 value for the a
161b1 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 uto-vacuum prope
161b2 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 rty is .** deter
161b3 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c mined by the SQL
161b4 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
161b5 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f VACUUM macro..*/
161b6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
161b7 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
161b8 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 SetAutoVacuum(Bt
161b9 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f ree *p, int auto
161ba 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 Vacuum){.#ifdef
161bb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
161bc 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 VACUUM. return
161bd 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
161be 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 .#else. BtShare
161bf 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
161c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
161c1 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d TE_OK;. u8 av =
161c2 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b (u8)autoVacuum;
161c3 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
161c4 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
161c5 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
161c6 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 ed && (av ?1:0)!
161c7 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d =pBt->autoVacuum
161c8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
161c9 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
161ca 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e }else{. pBt->
161cb 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 autoVacuum = av
161cc 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 ?1:0;. pBt->i
161cd 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d ncrVacuum = av==
161ce 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 2 ?1:0;. }. sq
161cf 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
161d0 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
161d1 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
161d2 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 Return the valu
161d3 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 e of the 'auto-v
161d4 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e acuum' property.
161d5 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 If auto-vacuum
161d6 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 is .** enabled 1
161d7 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
161d8 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 herwise 0..*/.SQ
161d9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
161da 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
161db 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
161dc 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c *p){.#ifdef SQL
161dd 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
161de 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52 UUM. return BTR
161df 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f EE_AUTOVACUUM_NO
161e0 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 NE;.#else. int
161e1 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 rc;. sqlite3Btr
161e2 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
161e3 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 = (. (!p->pB
161e4 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 t->autoVacuum)?B
161e5 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
161e6 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 NONE:. (!p->p
161e7 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f Bt->incrVacuum)?
161e8 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
161e9 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 _FULL:. BTREE
161ea 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 _AUTOVACUUM_INCR
161eb 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 . );. sqlite3B
161ec 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
161ed 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
161ee 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 f.}.../*.** Get
161ef 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 a reference to p
161f0 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74 Page1 of the dat
161f1 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 abase file. Thi
161f2 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 s will.** also a
161f3 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63 cquire a readloc
161f4 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a k on that file..
161f5 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
161f6 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
161f7 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20 uccess. If the
161f8 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a file is not a.**
161f9 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 well-formed dat
161fa 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e abase file, then
161fb 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
161fc 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
161fd 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
161fe 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64 eturned if the d
161ff 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 atabase is locke
16200 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d d. SQLITE_NOMEM
16201 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 .** is returned
16202 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 if we run out of
16203 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 memory. .*/.sta
16204 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 tic int lockBtre
16205 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
16206 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
16207 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 mPage *pPage1;.
16208 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 int nPage;.. a
16209 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1620a 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1620b 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1620c 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d t( pBt->pPage1==
1620d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
1620e 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f te3PagerSharedLo
1620f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b ck(pBt->pPager);
16210 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
16211 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
16212 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 ;. rc = btreeGe
16213 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 tPage(pBt, 1, &p
16214 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 Page1, 0);. if(
16215 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
16216 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f return rc;.. /
16217 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 * Do some checki
16218 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 ng to help insur
16219 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f 70 e the file we op
1621a 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 ened really is.
1621b 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74 61 ** a valid data
1621c 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f base file. . */
1621d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1621e 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
1621f 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 t->pPager, &nPag
16220 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 e);. if( rc!=SQ
16221 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 LITE_OK ){. g
16222 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
16223 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 ailed;. }else i
16224 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 f( nPage>0 ){.
16225 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a int pageSize;.
16226 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 int usableSi
16227 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 65 ze;. u8 *page
16228 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 74 1 = pPage1->aDat
16229 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 a;. rc = SQLI
1622a 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 TE_NOTADB;. i
1622b 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c f( memcmp(page1,
1622c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 31 zMagicHeader, 1
1622d 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6)!=0 ){. g
1622e 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
1622f 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
16230 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 if( page1[18]>1
16231 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 ){. pBt->r
16232 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 eadOnly = 1;.
16233 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 }. if( page1
16234 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 [19]>1 ){.
16235 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16236 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 failed;. }..
16237 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 /* The maximu
16238 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 m embedded fract
16239 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 63 ion must be exac
1623a 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74 68 tly 25%. And th
1623b 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a e minimum. **
1623c 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 embedded fracti
1623d 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35 25 on must be 12.5%
1623e 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 for both leaf-d
1623f 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 ata and non-leaf
16240 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 -data.. ** Th
16241 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67 e original desig
16242 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 n allowed these
16243 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c amounts to vary,
16244 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a but as of. *
16245 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c * version 3.6.0,
16246 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65 6d we require them
16247 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20 to be fixed..
16248 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d */. if( mem
16249 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 cmp(&page1[21],
1624a 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 "\100\040\040",3
1624b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f )!=0 ){. go
1624c 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 to page1_init_fa
1624d 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iled;. }.
1624e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 62 pageSize = get2b
1624f 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29 3b yte(&page1[16]);
16250 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 53 . if( ((pageS
16251 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 ize-1)&pageSize)
16252 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c !=0 || pageSize<
16253 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 512 ||. (
16254 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
16255 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70 61 SIZE<32768 && pa
16256 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 geSize>SQLITE_MA
16257 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 X_PAGE_SIZE).
16258 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
16259 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
1625a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1625b 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 rt( (pageSize &
1625c 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 7)==0 );. usa
1625d 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 bleSize = pageSi
1625e 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a ze - page1[20];.
1625f 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 if( pageSize
16260 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 !=pBt->pageSize
16261 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65 ){. /* Afte
16262 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 69 r reading the fi
16263 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 rst page of the
16264 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e database assumin
16265 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20 20 g a page size.
16266 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 ** of BtShar
16267 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20 ed.pageSize, we
16268 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 20 have discovered
16269 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 that the page-si
1626a 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 ze is. ** a
1626b 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 ctually pageSize
1626c 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 . Unlock the dat
1626d 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42 74 abase, leave pBt
1626e 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20 20 ->pPage1 at.
1626f 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 ** zero and re
16270 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 turn SQLITE_OK.
16271 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 The caller will
16272 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 call this functi
16273 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 on. ** agai
16274 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 n with the corre
16275 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 ct page-size..
16276 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c */. rel
16277 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 easePage(pPage1)
16278 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61 ;. pBt->usa
16279 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29 75 bleSize = (u16)u
1627a 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 sableSize;.
1627b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d pBt->pageSize =
1627c 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a (u16)pageSize;.
1627d 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 freeTempSp
1627e 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 ace(pBt);.
1627f 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16280 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 rSetPagesize(pBt
16281 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e ->pPager, &pBt->
16282 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 pageSize,.
16283 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16284 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
16285 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 eSize-usableSize
16286 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
16287 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rc;. }. if
16288 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 ( usableSize<480
16289 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
1628a 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
1628b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d ;. }. pBt-
1628c 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 >pageSize = (u16
1628d 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 )pageSize;. p
1628e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d Bt->usableSize =
1628f 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65 (u16)usableSize
16290 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
16291 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16292 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 . pBt->autoVa
16293 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
16294 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 (&page1[36 + 4*4
16295 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 ])?1:0);. pBt
16296 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
16297 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b get4byte(&page1[
16298 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 36 + 7*4])?1:0);
16299 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f .#endif. }.. /
1629a 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 * maxLocal is th
1629b 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 e maximum amount
1629c 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 of payload to s
1629d 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 tore locally for
1629e 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d . ** a cell. M
1629f 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 73 ake sure it is s
162a0 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 mall enough so t
162a1 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e hat at least min
162a2 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c Fanout. ** cell
162a3 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f s can will fit o
162a4 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20 n one page. We
162a5 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65 assume a 10-byte
162a6 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20 page header..
162a7 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 70 ** Besides the p
162a8 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c ayload, the cell
162a9 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a must store:. *
162aa 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69 * 2-byte poi
162ab 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
162ac 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 . ** 4-byte
162ad 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 child pointer.
162ae 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e ** 9-byte n
162af 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 Key value. **
162b0 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20 4-byte nData
162b1 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 value. ** 4
162b2 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 -byte overflow p
162b3 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a age pointer. **
162b4 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 So a cell consi
162b5 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20 sts of a 2-byte
162b6 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 72 poiner, a header
162b7 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 63 which is as muc
162b8 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 h as. ** 17 byt
162b9 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 es long, 0 to N
162ba 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 bytes of payload
162bb 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 , and an optiona
162bc 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f l 4 byte overflo
162bd 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e w. ** page poin
162be 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d ter.. */. pBt-
162bf 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 >maxLocal = (pBt
162c0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 ->usableSize-12)
162c1 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 *64/255 - 23;.
162c2 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 pBt->minLocal =
162c3 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
162c4 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 -12)*32/255 - 23
162c5 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 ;. pBt->maxLeaf
162c6 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
162c7 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e ze - 35;. pBt->
162c8 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e minLeaf = (pBt->
162c9 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 usableSize-12)*3
162ca 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 2/255 - 23;. as
162cb 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 sert( pBt->maxLe
162cc 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 af + 23 <= MX_CE
162cd 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a LL_SIZE(pBt) );.
162ce 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 pBt->pPage1 =
162cf 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 6e pPage1;. return
162d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 SQLITE_OK;..pag
162d1 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a e1_init_failed:.
162d2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
162d3 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 age1);. pBt->pP
162d4 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 age1 = 0;. retu
162d5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
162d6 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
162d7 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 outstanding curs
162d8 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e ors and we are n
162d9 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 ot in the middle
162da 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63 .** of a transac
162db 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69 tion but there i
162dc 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e s a read lock on
162dd 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 the database, t
162de 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 hen.** this rout
162df 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 66 ine unrefs the f
162e0 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
162e1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
162e2 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 hich .** has the
162e3 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 61 effect of relea
162e4 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c 6f sing the read lo
162e5 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
162e6 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 re is a transact
162e7 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2c ion in progress,
162e8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
162e9 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
162ea 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 tic void unlockB
162eb 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 53 treeIfUnused(BtS
162ec 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 hared *pBt){. a
162ed 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
162ee 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
162ef 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
162f0 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d t( pBt->pCursor=
162f1 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 =0 || pBt->inTra
162f2 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e nsaction>TRANS_N
162f3 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42 74 ONE );. if( pBt
162f4 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
162f5 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 =TRANS_NONE && p
162f6 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b Bt->pPage1!=0 ){
162f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
162f8 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 ->pPage1->aData
162f9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
162fa 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
162fb 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
162fc 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
162fd 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e t( pBt->pPage1->
162fe 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65 6c aData );. rel
162ff 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70 50 easePage(pBt->pP
16300 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e age1);. pBt->
16301 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a pPage1 = 0;. }.
16302 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 }../*.** If pBt
16303 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 points to an emp
16304 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e ty file then con
16305 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79 20 vert that empty
16306 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e file.** into a n
16307 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61 73 ew empty databas
16308 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e e by initializin
16309 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 g the first page
1630a 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 of.** the datab
1630b 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
1630c 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 42 nt newDatabase(B
1630d 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
1630e 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 MemPage *pP1;.
1630f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
16310 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a data;. int rc;.
16311 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 int nPage;..
16312 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16313 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
16314 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
16315 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
16316 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
16317 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 er, &nPage);. i
16318 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16319 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 || nPage>0 ){.
1631a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1631b 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 }. pP1 = pBt->p
1631c 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 Page1;. assert(
1631d 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 pP1!=0 );. dat
1631e 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a a = pP1->aData;.
1631f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16320 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 gerWrite(pP1->pD
16321 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
16322 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
16323 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 memcpy(data, zMa
16324 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f gicHeader, sizeo
16325 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 f(zMagicHeader))
16326 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
16327 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 of(zMagicHeader)
16328 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 ==16 );. put2by
16329 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 te(&data[16], pB
1632a 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
1632b 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 data[18] = 1;.
1632c 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 data[19] = 1;.
1632d 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 assert( pBt->usa
1632e 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 bleSize<=pBt->pa
1632f 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 geSize && pBt->u
16330 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 sableSize+255>=p
16331 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
16332 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 data[20] = (u8)
16333 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d (pBt->pageSize -
16334 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16335 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 );. data[21] =
16336 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 64;. data[22] =
16337 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 32;. data[23]
16338 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 32;. memset(&
16339 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 data[24], 0, 100
1633a 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 -24);. zeroPage
1633b 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 (pP1, PTF_INTKEY
1633c 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 |PTF_LEAF|PTF_LE
1633d 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d AFDATA );. pBt-
1633e 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
1633f 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 1;.#ifndef SQLI
16340 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
16341 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 UM. assert( pBt
16342 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 ->autoVacuum==1
16343 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 || pBt->autoVacu
16344 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 um==0 );. asser
16345 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 t( pBt->incrVacu
16346 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e um==1 || pBt->in
16347 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 crVacuum==0 );.
16348 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
16349 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 36 + 4*4], pBt->
1634a 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 autoVacuum);. p
1634b 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 ut4byte(&data[36
1634c 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e + 7*4], pBt->in
1634d 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 crVacuum);.#endi
1634e 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 f. return SQLIT
1634f 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
16350 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 ttempt to start
16351 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f a new transactio
16352 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 n. A write-trans
16353 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 action.** is sta
16354 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f rted if the seco
16355 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e nd argument is n
16356 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 onzero, otherwis
16357 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 e a read-.** tra
16358 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 nsaction. If th
16359 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
1635a 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 t is 2 or more a
1635b 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 nd exclusive.**
1635c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1635d 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 tarted, meaning
1635e 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 that no other pr
1635f 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 ocess is allowed
16360 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 .** to access th
16361 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 e database. A p
16362 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 reexisting trans
16363 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 action may not b
16364 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f e.** upgraded to
16365 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 exclusive by ca
16366 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
16367 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 ne a second time
16368 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 - the.** exclus
16369 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 ivity flag only
1636a 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 works for a new
1636b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
1636c 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 ** A write-trans
1636d 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 action must be s
1636e 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 tarted before at
1636f 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a tempting any .**
16370 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 changes to the
16371 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 database. None
16372 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
16373 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 routines .** wi
16374 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 ll work unless a
16375 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
16376 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a started first:.*
16377 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 *.** sqlite
16378 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
16379 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
1637a 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e te3BtreeCreateIn
1637b 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 dex().** sq
1637c 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
1637d 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
1637e 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
1637f 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
16380 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
16381 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 t().** sqli
16382 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 te3BtreeDelete()
16383 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
16384 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 BtreeUpdateMeta(
16385 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ).**.** If an in
16386 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f itial attempt to
16387 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 acquire the loc
16388 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 k fails because
16389 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 of lock contenti
1638a 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 on.** and the da
1638b 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69 tabase was previ
1638c 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 ously unlocked,
1638d 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 then invoke the
1638e 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 busy handler.**
1638f 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e if there is one.
16390 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77 But if there w
16391 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 as previously a
16392 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f read-lock, do no
16393 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 t.** invoke the
16394 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a busy handler - j
16395 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ust return SQLIT
16396 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f E_BUSY. SQLITE_
16397 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 BUSY is .** retu
16398 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 rned when there
16399 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 is already a rea
1639a 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 d-lock in order
1639b 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c to avoid a deadl
1639c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f ock..**.** Suppo
1639d 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f se there are two
1639e 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 processes A and
1639f 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 B. A has a rea
163a0 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 d lock and B has
163a1 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c .** a reserved l
163a2 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f ock. B tries to
163a3 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c promote to excl
163a4 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f usive but is blo
163a5 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 cked because.**
163a6 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b of A's read lock
163a7 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 . A tries to pr
163a8 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 omote to reserve
163a9 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 d but is blocked
163aa 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 by B..** One or
163ab 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 the other of th
163ac 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
163ad 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 must give way or
163ae 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a there can be.**
163af 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 no progress. B
163b0 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 y returning SQLI
163b1 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 TE_BUSY and not
163b2 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 invoking the bus
163b3 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 y callback.** wh
163b4 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 en A already has
163b5 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 a read lock, we
163b6 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 encourage A to
163b7 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 give up and let
163b8 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f B.** proceed..*/
163b9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
163ba 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
163bb 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 BeginTrans(Btree
163bc 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 *p, int wrflag)
163bd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c {. sqlite3 *pBl
163be 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 ock = 0;. BtSha
163bf 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
163c0 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
163c1 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 LITE_OK;.. sqli
163c2 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
163c3 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 ;. btreeIntegri
163c4 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ty(p);.. /* If
163c5 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 the btree is alr
163c6 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d eady in a write-
163c7 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 transaction, or
163c8 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 it. ** is alrea
163c9 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 dy in a read-tra
163ca 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 nsaction and a r
163cb 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a ead-transaction.
163cc 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 ** is requeste
163cd 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d d, this is a no-
163ce 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 op.. */. if( p
163cf 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
163d0 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e _WRITE || (p->in
163d1 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 Trans==TRANS_REA
163d2 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b D && !wrflag) ){
163d3 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f . goto trans_
163d4 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a begun;. }.. /*
163d5 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 Write transacti
163d6 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 ons are not poss
163d7 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f ible on a read-o
163d8 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
163d9 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f if( pBt->readO
163da 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b nly && wrflag ){
163db 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
163dc 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 _READONLY;. g
163dd 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
163de 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
163df 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
163e0 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20 61 _CACHE. /* If a
163e1 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 nother database
163e2 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61 handle has alrea
163e3 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74 dy opened a writ
163e4 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 e transaction .
163e5 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72 ** on this shar
163e6 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 ed-btree structu
163e7 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 re and a second
163e8 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
163e9 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 n is. ** reques
163ea 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ted, return SQLI
163eb 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a TE_LOCKED.. */.
163ec 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26 26 if( (wrflag &&
163ed 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
163ee 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
163ef 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64 ) || pBt->isPend
163f0 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 ing ){. pBloc
163f1 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 72 k = pBt->pWriter
163f2 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ->db;. }else if
163f3 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 ( wrflag>1 ){.
163f4 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b BtLock *pIter;
163f5 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 . for(pIter=p
163f6 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 Bt->pLock; pIter
163f7 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 ; pIter=pIter->p
163f8 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 Next){. if(
163f9 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d pIter->pBtree!=
163fa 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 6c p ){. pBl
163fb 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 ock = pIter->pBt
163fc 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 ree->db;.
163fd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
163fe 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
163ff 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 pBlock ){. sq
16400 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
16401 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 locked(p->db, pB
16402 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 lock);. rc =
16403 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 SQLITE_LOCKED_SH
16404 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 67 AREDCACHE;. g
16405 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
16406 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
16407 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 * Any read-only
16408 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 or read-write tr
16409 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65 ansaction implie
1640a 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e s a read-lock on
1640b 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 53 . ** page 1. S
1640c 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 o if some other
1640d 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c 69 shared-cache cli
1640e 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 20 ent already has
1640f 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 a write-lock .
16410 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 ** on page 1, th
16411 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 e transaction ca
16412 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20 nnot be opened.
16413 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 */. rc = queryS
16414 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
16415 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f ock(p, MASTER_RO
16416 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a OT, READ_LOCK);.
16417 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
16418 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e 73 =rc ) goto trans
16419 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a _begun;.. do {.
1641a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b /* Call lock
1641b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 Btree() until ei
1641c 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 ther pBt->pPage1
1641d 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 is populated or
1641e 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 . ** lockBtre
1641f 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 e() returns some
16420 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e thing other than
16421 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b SQLITE_OK. lock
16422 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d Btree(). ** m
16423 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
16424 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 _OK but leave pB
16425 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f t->pPage1 set to
16426 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 0 if after.
16427 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 ** reading page
16428 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 1 it discovers t
16429 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a hat the page-siz
1642a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1642b 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 e . ** file i
1642c 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 s not pBt->pageS
1642d 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ize. In this cas
1642e 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 e lockBtree() wi
1642f 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a ll update. **
16430 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 pBt->pageSize t
16431 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 o the page-size
16432 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 of the file on d
16433 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 isk.. */.
16434 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 while( pBt->pPag
16435 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f e1==0 && SQLITE_
16436 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 OK==(rc = lockBt
16437 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 ree(pBt)) );..
16438 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16439 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b _OK && wrflag ){
1643a 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
1643b 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
1643c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1643d 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 READONLY;.
1643e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1643f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16440 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65 Begin(pBt->pPage
16441 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74 r,wrflag>1,sqlit
16442 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 e3TempInMemory(p
16443 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20 ->db));.
16444 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16445 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
16446 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 c = newDatabase(
16447 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a pBt);. }.
16448 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
16449 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1644a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1644b 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
1644c 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a sed(pBt);. }.
1644d 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 }while( rc==SQ
1644e 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 LITE_BUSY && pBt
1644f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
16450 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 =TRANS_NONE &&.
16451 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e btreeIn
16452 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
16453 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 pBt) );.. if( r
16454 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16455 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 if( p->inTra
16456 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns==TRANS_NONE )
16457 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 {. pBt->nTr
16458 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 ansaction++;.#if
16459 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1645a 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
1645b 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 if( p->shara
1645c 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28 20 ble ){..assert(
1645d 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d 3d p->lock.pBtree==
1645e 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 p && p->lock.iTa
1645f 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ble==1 );.
16460 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 p->lock.eLock
16461 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 = READ_LOCK;.
16462 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 p->lock.pNe
16463 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b xt = pBt->pLock;
16464 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 4c . pBt->pL
16465 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a ock = &p->lock;.
16466 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
16467 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 }. p->inTr
16468 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 ans = (wrflag?TR
16469 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f ANS_WRITE:TRANS_
1646a 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 READ);. if( p
1646b 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 ->inTrans>pBt->i
1646c 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a nTransaction ){.
1646d 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 pBt->inTra
1646e 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e nsaction = p->in
1646f 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 Trans;. }.#if
16470 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16471 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
16472 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a if( wrflag ){.
16473 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
16474 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 Bt->pWriter );.
16475 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 pBt->pWrite
16476 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74 r = p;. pBt
16477 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 ->isExclusive =
16478 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a (u8)(wrflag>1);.
16479 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
1647a 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a ...trans_begun:.
1647b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1647c 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b _OK && wrflag ){
1647d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c . /* This cal
1647e 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 l makes sure tha
1647f 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 t the pager has
16480 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 the correct numb
16481 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 er of. ** ope
16482 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 n savepoints. If
16483 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
16484 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72 meter is greater
16485 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 than 0 and.
16486 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e ** the sub-journ
16487 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 al is not alread
16488 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 y open, then it
16489 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 will be opened h
1648a 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ere.. */.
1648b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1648c 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 rOpenSavepoint(p
1648d 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 Bt->pPager, p->d
1648e 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a b->nSavepoint);.
1648f 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 }.. btreeInte
16490 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 grity(p);. sqli
16491 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16492 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
16493 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16494 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16495 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
16496 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
16497 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c ies for all chil
16498 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 dren of page pPa
16499 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 ge. Also, if.**
1649a 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 pPage contains c
1649b 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 ells that point
1649c 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
1649d 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 s, set the point
1649e 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 er.** map entrie
1649f 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c s for the overfl
164a0 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c ow pages as well
164a1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
164a2 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
164a3 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
164a4 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
164a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164a6 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
164a7 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 variable */. i
164a8 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 nt nCell;
164a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164aa 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
164ab 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 ells in page pPa
164ac 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ge */. int rc;
164ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
164af 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 turn code */. B
164b0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
164b1 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
164b2 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 isInitOrig = pPa
164b3 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 ge->isInit;. Pg
164b4 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d no pgno = pPage-
164b5 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 >pgno;.. assert
164b6 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
164b7 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
164b8 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
164b9 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 = btreeInitPage(
164ba 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 pPage);. if( rc
164bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
164bc 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c goto set_chil
164bd 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 d_ptrmaps_out;.
164be 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 }. nCell = pPa
164bf 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f ge->nCell;.. fo
164c0 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
164c1 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 i++){. u8 *pC
164c2 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
164c3 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70 Page, i);.. p
164c4 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
164c5 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 pPage, pCell, &r
164c6 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50 c);.. if( !pP
164c7 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
164c8 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e Pgno childPgn
164c9 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 o = get4byte(pCe
164ca 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 ll);. ptrma
164cb 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
164cc 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
164cd 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 E, pgno, &rc);.
164ce 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
164cf 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
164d0 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 Pgno childPg
164d1 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
164d2 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
164d3 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
164d4 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ;. ptrmapPut(
164d5 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 pBt, childPgno,
164d6 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 PTRMAP_BTREE, pg
164d7 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 no, &rc);. }..s
164d8 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
164d9 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 _out:. pPage->i
164da 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 sInit = isInitOr
164db 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ig;. return rc;
164dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 .}../*.** Somewh
164dd 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20 ere on pPage is
164de 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 a pointer to pag
164df 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 e iFrom. Modify
164e0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f this pointer so
164e1 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e .** that it poin
164e2 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d ts to iTo. Param
164e3 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72 eter eType descr
164e4 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 ibes the type of
164e5 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 pointer to.** b
164e6 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20 e modified, as
164e7 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 follows:.**.** P
164e8 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 TRMAP_BTREE:
164e9 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 pPage is a btre
164ea 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e e-page. The poin
164eb 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 ter points at a
164ec 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 child .**
164ed 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
164ee 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a of pPage..**.**
164ef 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
164f0 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74 1: pPage is a bt
164f1 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f ree-page. The po
164f2 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 inter points at
164f3 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 an overflow.**
164f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164f5 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
164f6 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 by one of the c
164f7 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a ells on pPage..*
164f8 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 *.** PTRMAP_OVER
164f9 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 FLOW2: pPage is
164fa 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 an overflow-page
164fb 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
164fc 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 ints at the next
164fd 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
164fe 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 overflow p
164ff 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e age in the list.
16500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
16501 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 odifyPagePointer
16502 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
16503 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e Pgno iFrom, Pgn
16504 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 o iTo, u8 eType)
16505 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
16506 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16507 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
16508 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
16509 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1650a 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
1650b 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 bPage) );. if(
1650c 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
1650d 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f ERFLOW2 ){. /
1650e 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 * The pointer is
1650f 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 always the firs
16510 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 4 bytes of the
16511 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 page in this ca
16512 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 se. */. if(
16513 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e get4byte(pPage->
16514 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b aData)!=iFrom ){
16515 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
16516 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
16517 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 T;. }. put
16518 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
16519 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 ta, iTo);. }els
1651a 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 e{. u8 isInit
1651b 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 Orig = pPage->is
1651c 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b Init;. int i;
1651d 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a . int nCell;.
1651e 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 . btreeInitPa
1651f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e ge(pPage);. n
16520 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
16521 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d ell;.. for(i=
16522 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
16523 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c {. u8 *pCel
16524 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
16525 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 ge, i);. if
16526 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
16527 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 OVERFLOW1 ){.
16528 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e CellInfo in
16529 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 fo;. btre
1652a 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
1652b 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
1652c 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
1652d 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 info.iOverflow )
1652e 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1652f 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28 iFrom==get4byte(
16530 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
16531 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20 rflow]) ){.
16532 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
16533 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
16534 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 rflow], iTo);.
16535 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
16536 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
16537 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
16538 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 se{. if(
16539 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d get4byte(pCell)=
1653a 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
1653b 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 put4byte(pCe
1653c 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 ll, iTo);.
1653d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1653e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1653f 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d }. . if( i=
16540 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 =nCell ){.
16541 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 if( eType!=PTRMA
16542 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 P_BTREE || .
16543 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 get4byte(&
16544 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
16545 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
16546 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 )!=iFrom ){.
16547 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16548 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
16549 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 }. pu
1654a 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
1654b 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
1654c 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a ffset+8], iTo);.
1654d 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 }.. pPage
1654e 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 ->isInit = isIni
1654f 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 tOrig;. }. ret
16550 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16551 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 .../*.** Move th
16552 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 e open database
16553 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 page pDbPage to
16554 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 location iFreePa
16555 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 ge in the .** da
16556 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 tabase. The pDbP
16557 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 age reference re
16558 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a mains valid..**.
16559 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20 ** The isCommit
1655a 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 74 flag indicates t
1655b 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hat there is no
1655c 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 need to remember
1655d 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 that.** the jou
1655e 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 rnal needs to be
1655f 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 sync()ed before
16560 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 database page p
16561 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a DbPage->pgno .**
16562 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
16563 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68 to. The caller h
16564 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 as already promi
16565 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 sed not to write
16566 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65 to that.** page
16567 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16568 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20 relocatePage(.
16569 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
1656a 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 /* Btre
1656b 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a e */. MemPage *
1656c 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 20 pDbPage,
1656d 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20 /* Open page to
1656e 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79 move */. u8 eTy
1656f 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pe,
16570 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 /* Pointer ma
16571 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 66 p 'type' entry f
16572 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 or pDbPage */.
16573 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20 Pgno iPtrPage,
16574 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
16575 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f ter map 'page-no
16576 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 ' entry for pDbP
16577 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 age */. Pgno iF
16578 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 20 reePage,
16579 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f /* The locatio
1657a 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67 n to move pDbPag
1657b 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 e to */. int is
1657c 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 Commit
1657d 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66 /* isCommit f
1657e 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 71 lag passed to sq
1657f 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
16580 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 ge */.){. MemPa
16581 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20 ge *pPtrPage;
16582 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 /* The page that
16583 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
16584 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a ter to pDbPage *
16585 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65 /. Pgno iDbPage
16586 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f = pDbPage->pgno
16587 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
16588 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b r = pBt->pPager;
16589 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
1658a 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52 sert( eType==PTR
1658b 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c MAP_OVERFLOW2 ||
1658c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
1658d 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 VERFLOW1 || .
1658e 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 eType==PTRMAP
1658f 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d _BTREE || eType=
16590 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
16591 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
16592 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
16593 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
16594 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 67 assert( pDbPag
16595 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a e->pBt==pBt );..
16596 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69 /* Move page i
16597 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20 DbPage from its
16598 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
16599 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 to page number
1659a 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54 iFreePage */. T
1659b 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 55 RACE(("AUTOVACUU
1659c 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20 M: Moving %d to
1659d 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 74 free page %d (pt
1659e 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 25 r page %d type %
1659f 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44 d)\n", . iD
165a0 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
165a1 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 70 , iPtrPage, eTyp
165a2 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 e));. rc = sqli
165a3 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 te3PagerMovepage
165a4 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 65 (pPager, pDbPage
165a5 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 65 ->pDbPage, iFree
165a6 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b Page, isCommit);
165a7 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
165a8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
165a9 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62 rn rc;. }. pDb
165aa 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 Page->pgno = iFr
165ab 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 eePage;.. /* If
165ac 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 62 pDbPage was a b
165ad 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20 tree-page, then
165ae 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c it may have chil
165af 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63 d pages and/or c
165b0 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70 ells. ** that p
165b1 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 oint to overflow
165b2 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e pages. The poin
165b3 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 ter map entries
165b4 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20 for all these.
165b5 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f ** pages need to
165b6 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a be changed.. *
165b7 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67 *. ** If pDbPag
165b8 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e is an overflow
165b9 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 page, then the
165ba 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d 61 first 4 bytes ma
165bb 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70 y store a. ** p
165bc 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73 ointer to a subs
165bd 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 equent overflow
165be 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 73 page. If this is
165bf 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a the case, then.
165c0 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 ** the pointer
165c1 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65 map needs to be
165c2 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 65 updated for the
165c3 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 subsequent over
165c4 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a flow page.. */.
165c5 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
165c6 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 MAP_BTREE || eTy
165c7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
165c8 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 AGE ){. rc =
165c9 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
165ca 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
165cb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
165cc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
165cd 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 rc;. }. }els
165ce 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 e{. Pgno next
165cf 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 Ovfl = get4byte(
165d0 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b pDbPage->aData);
165d1 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66 . if( nextOvf
165d2 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74 l!=0 ){. pt
165d3 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 rmapPut(pBt, nex
165d4 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 tOvfl, PTRMAP_OV
165d5 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 ERFLOW2, iFreePa
165d6 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ge, &rc);.
165d7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
165d8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
165d9 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
165da 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
165db 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65 Fix the database
165dc 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65 pointer on page
165dd 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 70 iPtrPage that p
165de 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 67 ointed at iDbPag
165df 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69 e so. ** that i
165e0 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65 t points at iFre
165e1 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20 ePage. Also fix
165e2 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
165e3 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69 entry for. ** i
165e4 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 PtrPage.. */.
165e5 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 if( eType!=PTRMA
165e6 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 P_ROOTPAGE ){.
165e7 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
165e8 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 age(pBt, iPtrPag
165e9 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 e, &pPtrPage, 0)
165ea 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
165eb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
165ec 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
165ed 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
165ee 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 e3PagerWrite(pPt
165ef 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b rPage->pDbPage);
165f0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
165f1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
165f2 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 releasePage(pPtr
165f3 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 Page);. ret
165f4 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
165f5 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 rc = modifyPag
165f6 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 ePointer(pPtrPag
165f7 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 e, iDbPage, iFre
165f8 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 ePage, eType);.
165f9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
165fa 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 PtrPage);. if
165fb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
165fc 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 ){. ptrmapP
165fd 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 ut(pBt, iFreePag
165fe 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 e, eType, iPtrPa
165ff 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a ge, &rc);. }.
16600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
16601 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 .}../* Forward d
16602 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 eclaration requi
16603 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 red by incrVacuu
16604 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 mStep(). */.stat
16605 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 ic int allocateB
16606 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 treePage(BtShare
16607 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c d *, MemPage **,
16608 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 Pgno *, Pgno, u
16609 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 8);../*.** Perfo
1660a 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 rm a single step
1660b 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 of an increment
1660c 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 al-vacuum. If su
1660d 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 ccessful,.** ret
1660e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 urn SQLITE_OK. I
1660f 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f f there is no wo
16610 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 rk to do (and th
16611 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f erefore no.** po
16612 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 int in calling t
16613 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 his function aga
16614 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 in), return SQLI
16615 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d TE_DONE..**.** M
16616 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 ore specificly,
16617 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 this function at
16618 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 tempts to re-org
16619 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 anize the .** da
1661a 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 tabase so that t
1661b 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 he last page of
1661c 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 the file current
1661d 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 ly in use.** is
1661e 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 no longer in use
1661f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
16620 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 Fin parameter is
16621 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20 non-zero, this
16622 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 function assumes
16623 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c .** that the cal
16624 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 ler will keep ca
16625 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d lling incrVacuum
16626 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 Step() until.**
16627 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 it returns SQLIT
16628 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 E_DONE or an err
16629 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 or, and that nFi
1662a 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 n is the.** numb
1662b 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 er of pages the
1662c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
1662d 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 ll contain after
1662e 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 this .** proces
1662f 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20 s is complete.
16630 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c If nFin is zero,
16631 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 it is assumed t
16632 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75 hat.** incrVacuu
16633 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20 mStep() will be
16634 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20 called a finite
16635 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a amount of times.
16636 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 ** which may or
16637 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68 may not empty th
16638 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66 e freelist. A f
16639 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a ull autovacuum.*
1663a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41 * has nFin>0. A
1663b 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65 "PRAGMA increme
1663c 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73 ntal_vacuum" has
1663d 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 nFin==0..*/.sta
1663e 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75 tic int incrVacu
1663f 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64 20 umStep(BtShared
16640 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c *pBt, Pgno nFin,
16641 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a Pgno iLastPg){.
16642 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74 Pgno nFreeList
16643 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
16644 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
16645 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65 till on the free
16646 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 -list */.. asse
16647 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
16648 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
16649 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1664a 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a iLastPg>nFin );.
1664b 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 . if( !PTRMAP_I
1664c 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
1664d 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d Pg) && iLastPg!=
1664e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1664f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e E(pBt) ){. in
16650 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79 t rc;. u8 eTy
16651 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 pe;. Pgno iPt
16652 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 rPage;.. nFre
16653 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 eList = get4byte
16654 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
16655 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 Data[36]);. i
16656 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 f( nFreeList==0
16657 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16658 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
16659 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 }.. rc = ptr
1665a 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 mapGet(pBt, iLas
1665b 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 tPg, &eType, &iP
1665c 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 trPage);. if(
1665d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1665e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
1665f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 c;. }. if(
16660 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
16661 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 OOTPAGE ){.
16662 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
16663 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
16664 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 }.. if( eTyp
16665 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e==PTRMAP_FREEPA
16666 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 GE ){. if(
16667 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 nFin==0 ){.
16668 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 /* Remove the
16669 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 page from the f
1666a 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 iles free-list.
1666b 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 This is not requ
1666c 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 ired. **
1666d 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a if nFin is non-z
1666e 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73 ero. In that cas
1666f 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 e, the free-list
16670 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 will be.
16671 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f ** truncated to
16672 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 zero after this
16673 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
16674 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 s, so it doesn't
16675 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 . ** mat
16676 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20 ter if it still
16677 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 contains some ga
16678 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20 rbage entries..
16679 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1667a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a Pgno iFreePg;.
1667b 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 MemPage
1667c 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 *pFreePg;.
1667d 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
1667e 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
1667f 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67 FreePg, &iFreePg
16680 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20 , iLastPg, 1);.
16681 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
16682 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16683 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16684 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
16685 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
16686 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20 Pg==iLastPg );.
16687 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
16688 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 ge(pFreePg);.
16689 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
1668a 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 {. Pgno iFr
1668b 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 eePg;
1668c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72 /* Index of fr
1668d 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 ee page to move
1668e 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20 pLastPg to */.
1668f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61 MemPage *pLa
16690 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20 stPg;.. rc
16691 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
16692 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c Bt, iLastPg, &pL
16693 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 astPg, 0);.
16694 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16695 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
16696 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
16697 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46 .. /* If nF
16698 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 in is zero, this
16699 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 loop runs exact
1669a 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 ly once and page
1669b 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a pLastPg. *
1669c 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74 * is swapped wit
1669d 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 h the first free
1669e 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 page pulled off
1669f 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a the free list..
166a0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
166a1 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
166a2 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20 and, if nFin is
166a3 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
166a4 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20 o, then keep.
166a5 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e ** looping un
166a6 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20 til a free-page
166a7 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74 located within t
166a8 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61 he first nFin pa
166a9 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ges. ** of
166aa 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e the file is foun
166ab 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
166ac 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d do {. M
166ad 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b emPage *pFreePg;
166ae 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c . rc = al
166af 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
166b0 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 pBt, &pFreePg, &
166b1 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a iFreePg, 0, 0);.
166b2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
166b3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
166b4 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
166b5 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 ge(pLastPg);.
166b6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
166b7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
166b8 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
166b9 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 pFreePg);.
166ba 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20 }while( nFin!=0
166bb 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 && iFreePg>nFin
166bc 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
166bd 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67 iFreePg<iLastPg
166be 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 );. .
166bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
166c0 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d erWrite(pLastPg-
166c1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
166c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
166c3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
166c4 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
166c5 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 pBt, pLastPg, eT
166c6 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 ype, iPtrPage, i
166c7 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29 FreePg, nFin!=0)
166c8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
166c9 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 releasePage(pLas
166ca 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 tPg);. if(
166cb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
166cc 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
166cd 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
166ce 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69 }. }.. if( nFi
166cf 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 n==0 ){. iLas
166d0 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 tPg--;. while
166d1 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 ( iLastPg==PENDI
166d2 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
166d3 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 )||PTRMAP_ISPAGE
166d4 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 (pBt, iLastPg) )
166d5 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d {. if( PTRM
166d6 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 AP_ISPAGE(pBt, i
166d7 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20 LastPg) ){.
166d8 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b MemPage *pPg;
166d9 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 . int rc
166da 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
166db 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 50 Bt, iLastPg, &pP
166dc 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 g, 0);. i
166dd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
166de 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
166df 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
166e0 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
166e1 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
166e2 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b e(pPg->pDbPage);
166e3 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
166e4 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
166e5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
166e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
166e7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
166e8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
166e9 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a iLastPg--;.
166ea 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
166eb 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
166ec 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c age(pBt->pPager,
166ed 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20 iLastPg);. }.
166ee 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
166ef 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 K;.}../*.** A wr
166f0 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
166f1 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 must be opened b
166f2 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 efore calling th
166f3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 is function..**
166f4 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 It performs a si
166f5 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 ngle unit of wor
166f6 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 k towards an inc
166f7 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e remental vacuum.
166f8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e .**.** If the in
166f9 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
166fa 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 is finished aft
166fb 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
166fc 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c has run,.** SQL
166fd 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 ITE_DONE is retu
166fe 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e rned. If it is n
166ff 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 ot finished, but
16700 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 no error occurr
16701 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b ed,.** SQLITE_OK
16702 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
16703 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74 herwise an SQLit
16704 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a e error code. .*
16705 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16706 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16707 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 eIncrVacuum(Btre
16708 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
16709 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1670a 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 = p->pBt;.. sq
1670b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1670c 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 p);. assert( pB
1670d 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1670e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 ==TRANS_WRITE &&
1670f 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
16710 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 NS_WRITE );. if
16711 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 ( !pBt->autoVacu
16712 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 um ){. rc = S
16713 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 QLITE_DONE;. }e
16714 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 lse{. invalid
16715 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 ateAllOverflowCa
16716 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 che(pBt);. rc
16717 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 = incrVacuumSte
16718 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72 50 p(pBt, 0, pagerP
16719 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a agecount(pBt));.
1671a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1671b 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1671c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1671d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1671e 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 s called prior t
1671f 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f o sqlite3PagerCo
16720 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e mmit when a tran
16721 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f saction.** is co
16722 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75 mmited for an au
16723 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
16724 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c se..**.** If SQL
16725 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
16726 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e ed, then *pnTrun
16727 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 c is set to the
16728 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a number of pages.
16729 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
1672a 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74 file should be t
1672b 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69 runcated to duri
1672c 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 ng the commit pr
1672d 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 ocess. .** i.e.
1672e 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
1672f 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 been reorganize
16730 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 d so that only t
16731 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e he first *pnTrun
16732 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69 c.** pages are i
16733 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 n use..*/.static
16734 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43 int autoVacuumC
16735 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a ommit(BtShared *
16736 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pBt){. int rc =
16737 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 SQLITE_OK;. Pa
16738 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 ger *pPager = pB
16739 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 t->pPager;. VVA
1673a 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 _ONLY( int nRef
1673b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 = sqlite3PagerRe
1673c 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
1673d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1673e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1673f 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
16740 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
16741 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 erflowCache(pBt)
16742 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e ;. assert(pBt->
16743 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 autoVacuum);. i
16744 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 f( !pBt->incrVac
16745 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 uum ){. Pgno
16746 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a nFin; /*
16747 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
16748 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 74 in database aft
16749 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e 67 er autovacuuming
1674a 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 */. Pgno nFr
1674b 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ee; /* Nu
1674c 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e mber of pages on
1674d 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
1674e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 itially */. P
1674f 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 20 gno nPtrmap;
16750 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 50 /* Number of P
16751 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20 62 trMap pages to b
16752 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 50 e freed */. P
16753 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 20 gno iFree;
16754 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 61 /* The next pa
16755 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a ge to be freed *
16756 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79 /. int nEntry
16757 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
16758 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e er of entries on
16759 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65 one ptrmap page
1675a 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 */. Pgno nOr
1675b 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ig; /* Da
1675c 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f tabase size befo
1675d 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 re freeing */..
1675e 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72 nOrig = pager
1675f 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
16760 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
16761 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 SPAGE(pBt, nOrig
16762 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 ) || nOrig==PEND
16763 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16764 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 t) ){. /* I
16765 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
16766 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61 e to create a da
16767 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68 tabase for which
16768 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a the final page.
16769 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68 ** is eith
1676a 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 er a pointer-map
1676b 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e page or the pen
1676c 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20 ding-byte page.
1676d 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20 If one. **
1676e 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 is encountered,
1676f 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63 this indicates c
16770 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 orruption..
16771 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
16772 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
16773 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 BKPT;. }..
16774 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
16775 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
16776 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 aData[36]);.
16777 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 73 nEntry = pBt->us
16778 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20 20 ableSize/5;.
16779 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 nPtrmap = (nFree
1677a 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 -nOrig+PTRMAP_PA
1677b 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 GENO(pBt, nOrig)
1677c 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79 3b +nEntry)/nEntry;
1677d 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 . nFin = nOri
1677e 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 g - nFree - nPtr
1677f 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72 map;. if( nOr
16780 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ig>PENDING_BYTE_
16781 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 PAGE(pBt) && nFi
16782 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 n<PENDING_BYTE_P
16783 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
16784 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a nFin--;. }.
16785 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 while( PTRMA
16786 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46 P_ISPAGE(pBt, nF
16787 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e in) || nFin==PEN
16788 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
16789 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 Bt) ){. nFi
1678a 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n--;. }. i
1678b 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20 f( nFin>nOrig )
1678c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1678d 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 RRUPT_BKPT;..
1678e 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67 for(iFree=nOrig
1678f 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20 ; iFree>nFin &&
16790 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 rc==SQLITE_OK; i
16791 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72 Free--){. r
16792 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 c = incrVacuumSt
16793 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46 ep(pBt, nFin, iF
16794 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ree);. }.
16795 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f if( (rc==SQLITE_
16796 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 DONE || rc==SQLI
16797 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e TE_OK) && nFree>
16798 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
16799 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
1679a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1679b 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 erWrite(pBt->pPa
1679c 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
1679d 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
1679e 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
1679f 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20 a[32], 0);.
167a0 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
167a1 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
167a2 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c ], 0);. sql
167a3 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
167a4 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 eImage(pBt->pPag
167a5 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d er, nFin);. }
167a6 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
167a7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
167a8 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
167a9 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 back(pPager);.
167aa 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
167ab 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33 t( nRef==sqlite3
167ac 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 PagerRefcount(pP
167ad 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 ager) );. retur
167ae 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f n rc;.}..#else /
167af 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f * ifndef SQLITE_
167b0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 OMIT_AUTOVACUUM
167b1 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74 43 */.# define setC
167b2 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20 53 hildPtrmaps(x) S
167b3 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a QLITE_OK.#endif.
167b4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
167b5 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
167b6 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77 st phase of a tw
167b7 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 o-phase commit.
167b8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
167b9 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 causes a rollba
167ba 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 ck journal to be
167bb 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20 created (if it
167bc 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
167bd 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 exist).** and p
167be 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e opulated with en
167bf 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ough information
167c0 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f so that if a po
167c1 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a wer loss occurs.
167c2 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
167c3 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 can be restored
167c4 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
167c5 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67 state by playing
167c6 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 back.** the jou
167c7 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 rnal. Then the
167c8 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
167c9 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 journal are flus
167ca 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 hed out to.** th
167cb 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 e disk. After t
167cc 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 he journal is sa
167cd 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 fely on oxide, t
167ce 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 he changes to th
167cf 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72 e.** database ar
167d0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
167d1 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
167d2 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20 and flushed to
167d3 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 oxide..** At the
167d4 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c end of this cal
167d5 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 l, the rollback
167d6 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 journal still ex
167d7 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 ists on the.** d
167d8 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73 isk and we are s
167d9 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c till holding all
167da 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 locks, so the t
167db 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e ransaction has n
167dc 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e ot.** committed.
167dd 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72 See sqlite3Btr
167de 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
167df 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e () for the secon
167e0 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a d phase of the.*
167e1 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 * commit process
167e2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c ..**.** This cal
167e3 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 l is a no-op if
167e4 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 no write-transac
167e5 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c tion is currentl
167e6 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e y active on pBt.
167e7 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
167e8 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 , sync the datab
167e9 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 ase file for the
167ea 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 btree pBt. zMas
167eb 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a ter points to.**
167ec 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d the name of a m
167ed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
167ee 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 le that should b
167ef 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
167f0 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c he.** individual
167f1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f journal file, o
167f2 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 r is NULL, indic
167f3 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 ating no master
167f4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a journal file .**
167f5 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 (single databas
167f6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a e transaction)..
167f7 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 **.** When this
167f8 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d is called, the m
167f9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 aster journal sh
167fa 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76 ould already hav
167fb 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 e been.** create
167fc 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 d, populated wit
167fd 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 h this journal p
167fe 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65 ointer and synce
167ff 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a d to disk..**.**
16800 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f Once this is ro
16801 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e utine has return
16802 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 ed, the only thi
16803 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 ng required to c
16804 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 ommit.** the wri
16805 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 te-transaction f
16806 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
16807 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 file is to dele
16808 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a te the journal..
16809 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1680a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1680b 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
1680c 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 (Btree *p, const
1680d 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b char *zMaster){
1680e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1680f 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e TE_OK;. if( p->
16810 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
16811 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 RITE ){. BtSh
16812 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
16813 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 Bt;. sqlite3B
16814 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 treeEnter(p);.#i
16815 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16816 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
16817 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
16818 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 cuum ){. rc
16819 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d = autoVacuumCom
1681a 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 mit(pBt);.
1681b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1681c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c K ){. sql
1681d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1681e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1681f 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
16820 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 }.#endif. r
16821 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16822 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
16823 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 Bt->pPager, zMas
16824 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ter, 0);. sql
16825 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
16826 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
16827 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
16828 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
16829 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 42 lled from both B
1682a 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
1682b 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 6f wo() and BtreeRo
1682c 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 74 llback().** at t
1682d 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 he conclusion of
1682e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a a transaction..
1682f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 */.static void b
16830 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
16831 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 on(Btree *p){.
16832 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16833 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 p->pBt;. assert
16834 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
16835 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a ldsMutex(p) );..
16836 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 btreeClearHasC
16837 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69 ontent(pBt);. i
16838 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 f( p->inTrans>TR
16839 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 2d 3e 64 ANS_NONE && p->d
1683a 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1683b 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 >1 ){. /* If
1683c 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 there are other
1683d 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 active statement
1683e 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f s that belong to
1683f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0a 20 this database.
16840 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20 64 6f ** handle, do
16841 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72 65 61 wngrade to a rea
16842 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 d-only transacti
16843 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20 73 74 on. The other st
16844 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20 atements. **
16845 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 may still be rea
16846 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64 61 ding from the da
16847 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 tabase. */.
16848 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 downgradeAllShar
16849 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1684a 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 s(p);. p->inT
1684b 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45 41 rans = TRANS_REA
1684c 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 D;. }else{.
1684d 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 /* If the handle
1684e 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66 had any kind of
1684f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
16850 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 n, decrement the
16851 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 . ** transac
16852 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68 tion count of th
16853 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 e shared btree.
16854 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 If the transacti
16855 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a on count . **
16856 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74 20 reaches 0, set
16857 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 65 the shared state
16858 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 to TRANS_NONE.
16859 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 The unlockBtreeI
1685a 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a 2a fUnused(). **
1685b 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c call below will
1685c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 unlock the page
1685d 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 r. */. if( p
1685e 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 ->inTrans!=TRANS
1685f 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 _NONE ){. c
16860 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
16861 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b heTableLocks(p);
16862 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 . pBt->nTra
16863 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 nsaction--;.
16864 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 if( 0==pBt->nT
16865 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 ransaction ){.
16866 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 pBt->inTra
16867 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 nsaction = TRANS
16868 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 _NONE;. }.
16869 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 }.. /* Set
1686a 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
1686b 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 74 nsaction state t
1686c 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 o TRANS_NONE and
1686d 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20 unlock the .
1686e 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68 69 ** pager if thi
1686f 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 s call closed th
16870 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 e only read or w
16871 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
16872 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 . */. p->inT
16873 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e rans = TRANS_NON
16874 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 E;. unlockBtr
16875 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
16876 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 . }.. btreeInt
16877 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a egrity(p);.}../*
16878 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 .** Commit the t
16879 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65 ransaction curre
1687a 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 73 ntly in progress
1687b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1687c 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 tine implements
1687d 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 65 the second phase
1687e 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 6f of a 2-phase co
1687f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71 mmit. The.** sq
16880 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
16881 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 69 PhaseOne() routi
16882 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 ne does the firs
16883 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 t phase and shou
16884 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64 ld.** be invoked
16885 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
16886 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
16887 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 The sqlite3Btre
16888 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
16889 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 64 ).** routine did
1688a 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66 all the work of
1688b 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61 writing informa
1688c 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b tion out to disk
1688d 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 68 and flushing th
1688e 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f e.** contents so
1688f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 77 that they are w
16890 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20 ritten onto the
16891 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 41 disk platter. A
16892 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 ll this.** routi
16893 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 20 ne has to do is
16894 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 61 delete or trunca
16895 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 68 te or zero the h
16896 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 eader in the.**
16897 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
16898 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 73 rnal (which caus
16899 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 es the transacti
1689a 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e on to commit) an
1689b 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e d.** drop locks.
1689c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c .**.** This will
1689d 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 release the wri
1689e 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 te lock on the d
1689f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 atabase file. I
168a0 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e f there.** are n
168a1 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 o active cursors
168a2 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 , it also releas
168a3 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b es the read lock
168a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
168a5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
168a6 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
168a7 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 wo(Btree *p){.
168a8 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
168a9 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 p->pBt;.. sqlit
168aa 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
168ab 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
168ac 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 y(p);.. /* If t
168ad 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 20 he handle has a
168ae 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
168af 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 n open, commit t
168b0 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 73 he shared-btrees
168b1 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 . ** transacti
168b2 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 73 on and set the s
168b3 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 hared state to T
168b4 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a RANS_READ.. */.
168b5 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
168b6 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
168b7 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 . int rc;.
168b8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
168b9 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
168ba 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 NS_WRITE );.
168bb 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 assert( pBt->nTr
168bc 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 ansaction>0 );.
168bd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
168be 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 agerCommitPhaseT
168bf 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b wo(pBt->pPager);
168c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
168c1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
168c2 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
168c3 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 75 e(p);. retu
168c4 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
168c5 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
168c6 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 ion = TRANS_READ
168c7 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e ;. }.. btreeEn
168c8 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b dTransaction(p);
168c9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
168ca 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
168cb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
168cc 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 /*.** Do both ph
168cd 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 ases of a commit
168ce 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
168cf 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
168d0 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 treeCommit(Btree
168d1 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
168d2 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
168d3 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 ter(p);. rc = s
168d4 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
168d5 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b tPhaseOne(p, 0);
168d6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
168d7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
168d8 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
168d9 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b 0a mitPhaseTwo(p);.
168da 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
168db 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
168dc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e turn rc;.}..#ifn
168dd 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a def NDEBUG./*.**
168de 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
168df 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72 73 er of write-curs
168e0 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 ors open on this
168e1 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 handle. This is
168e2 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 for use.** in a
168e3 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73 69 ssert() expressi
168e4 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e ons, so it is on
168e5 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e ly compiled if N
168e6 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 DEBUG is not.**
168e7 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 defined..**.** F
168e8 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 or the purposes
168e9 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c of this routine,
168ea 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 a write-cursor
168eb 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 68 is any cursor th
168ec 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 at.** is capable
168ed 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 74 of writing to t
168ee 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68 61 he databse. Tha
168ef 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72 73 t means the curs
168f0 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e or was.** origin
168f1 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 ally opened for
168f2 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65 20 writing and the
168f3 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20 62 cursor has not b
168f4 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79 e disabled.** by
168f5 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61 74 having its stat
168f6 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55 52 e changed to CUR
168f7 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 SOR_FAULT..*/.st
168f8 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57 72 atic int countWr
168f9 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68 61 iteCursors(BtSha
168fa 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 red *pBt){. BtC
168fb 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 ursor *pCur;. i
168fc 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 nt r = 0;. for(
168fd 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f pCur=pBt->pCurso
168fe 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70 43 r; pCur; pCur=pC
168ff 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ur->pNext){.
16900 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 if( pCur->wrFlag
16901 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 && pCur->eState
16902 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 !=CURSOR_FAULT )
16903 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 r++; . }. ret
16904 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn r;.}.#endif.
16905 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
16906 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 61 ine sets the sta
16907 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 te to CURSOR_FAU
16908 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 LT and the error
16909 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43 .** code to errC
1690a 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 75 ode for every cu
1690b 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 64 rsor on BtShared
1690c 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 that pBtree.**
1690d 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a references..**.*
1690e 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 69 * Every cursor i
1690f 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 s tripped, inclu
16910 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 61 ding cursors tha
16911 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f t belong.** to o
16912 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
16913 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 68 nnections that h
16914 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 72 appen to be shar
16915 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 ing.** the cache
16916 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a with pBtree..**
16917 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16918 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 65 gets called whe
16919 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 n a rollback occ
1691a 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 urs..** All curs
1691b 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 61 ors using the sa
1691c 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 65 me cache must be
1691d 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 tripped.** to p
1691e 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d revent them from
1691f 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 74 trying to use t
16920 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a 2a he btree after.*
16921 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 * the rollback.
16922 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 The rollback ma
16923 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74 y have deleted t
16924 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 ables.** or move
16925 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f d root pages, so
16926 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69 it is not suffi
16927 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 cient to.** save
16928 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
16929 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 e cursor. The c
1692a 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a ursor must be.**
1692b 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f invalidated..*/
1692c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1692d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1692e 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 eTripAllCursors(
1692f 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 Btree *pBtree, i
16930 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 nt errCode){. B
16931 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71 tCursor *p;. sq
16932 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16933 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28 70 pBtree);. for(p
16934 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 =pBtree->pBt->pC
16935 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
16936 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 Next){. int i
16937 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
16938 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 29 eeClearCursor(p)
16939 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 ;. p->eState
1693a 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a = CURSOR_FAULT;.
1693b 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20 p->skipNext
1693c 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 = errCode;. f
1693d 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 or(i=0; i<=p->iP
1693e 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 age; i++){.
1693f 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d 3e releasePage(p->
16940 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 apPage[i]);.
16941 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d p->apPage[i] =
16942 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
16943 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16944 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a e(pBtree);.}../*
16945 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 .** Rollback the
16946 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 transaction in
16947 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 progress. All c
16948 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a ursors will be.*
16949 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20 74 * invalided by t
1694a 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 his operation.
1694b 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 Any attempt to u
1694c 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 se a cursor.** t
1694d 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 20 hat was open at
1694e 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
1694f 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
16950 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 will result.** i
16951 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a n an error..**.*
16952 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 * This will rele
16953 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f ase the write lo
16954 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
16955 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 se file. If the
16956 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 re.** are no act
16957 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 ive cursors, it
16958 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 also releases th
16959 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a e read lock..*/.
1695a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1695b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 nt sqlite3BtreeR
1695c 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a 70 ollback(Btree *p
1695d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
1695e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1695f 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 ->pBt;. MemPage
16960 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c *pPage1;.. sql
16961 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
16962 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c );. rc = saveAl
16963 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c lCursors(pBt, 0,
16964 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0);.#ifndef SQL
16965 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
16966 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d CACHE. if( rc!=
16967 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16968 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f /* This is a ho
16969 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e rrible situation
1696a 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f . An IO or mallo
1696b 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 72 c() error occurr
1696c 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a ed whilst. **
1696d 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 20 trying to save
1696e 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 cursor positions
1696f 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 . If this is an
16970 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 automatic rollba
16971 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 ck (as. ** th
16972 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 6f e result of a co
16973 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 nstraint, malloc
16974 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 4f () failure or IO
16975 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 error) then .
16976 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 6d ** the cache m
16977 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 ay be internally
16978 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e inconsistent (n
16979 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 ot contain valid
1697a 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 2a trees) so. *
1697b 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 * we cannot simp
1697c 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 ly return the er
1697d 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ror to the calle
1697e 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 r. Instead, abor
1697f 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 t . ** all qu
16980 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 62 eries that may b
16981 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 e using any of t
16982 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 he cursors that
16983 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a failed to save..
16984 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
16985 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 e3BtreeTripAllCu
16986 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 rsors(p, rc);.
16987 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 65 }.#endif. btree
16988 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 Integrity(p);..
16989 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d if( p->inTrans=
1698a 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a =TRANS_WRITE ){.
1698b 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 int rc2;..
1698c 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f assert( TRANS_
1698d 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 WRITE==pBt->inTr
1698e 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 ansaction );.
1698f 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61 rc2 = sqlite3Pa
16990 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d gerRollback(pBt-
16991 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 >pPager);. if
16992 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b ( rc2!=SQLITE_OK
16993 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 ){. rc = r
16994 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f c2;. }.. /
16995 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d * The rollback m
16996 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65 ay have destroye
16997 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44 d the pPage1->aD
16998 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 ata value. So.
16999 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 ** call btree
1699a 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61 67 GetPage() on pag
1699b 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b e 1 again to mak
1699c 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70 50 e. ** sure pP
1699d 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20 73 age1->aData is s
1699e 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f et correctly. */
1699f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47 65 . if( btreeGe
169a0 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 tPage(pBt, 1, &p
169a1 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 Page1, 0)==SQLIT
169a2 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
169a3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 leasePage(pPage1
169a4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
169a5 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43 ert( countWriteC
169a6 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 ursors(pBt)==0 )
169a7 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 ;. pBt->inTra
169a8 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 nsaction = TRANS
169a9 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 _READ;. }.. bt
169aa 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f reeEndTransactio
169ab 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 n(p);. sqlite3B
169ac 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
169ad 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
169ae 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 74 .** Start a stat
169af 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 ement subtransac
169b0 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61 tion. The subtra
169b1 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e nsaction can can
169b2 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 be rolled.** ba
169b3 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
169b4 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 of the main tra
169b5 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 nsaction. You mu
169b6 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 st start a trans
169b7 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 action .** befor
169b8 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62 e starting a sub
169b9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 transaction. The
169ba 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 subtransaction
169bb 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 is ended automat
169bc 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 ically .** if th
169bd 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 e main transacti
169be 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f on commits or ro
169bf 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 lls back..**.**
169c0 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 Statement subtra
169c1 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 nsactions are us
169c2 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 ed around indivi
169c3 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 dual SQL stateme
169c4 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 nts.** that are
169c5 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
169c6 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 a BEGIN...COMMI
169c7 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 T block. If a c
169c8 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
169c9 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e or occurs within
169ca 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
169cb 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 the effect of th
169cc 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 at one statement
169cd 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 .** can be rolle
169ce 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 d back without h
169cf 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 aving to rollbac
169d0 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 k the entire tra
169d1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
169d2 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 2d A statement sub-
169d3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 transaction is i
169d4 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 6e mplemented as an
169d5 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 anonymous savep
169d6 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c oint. The.** val
169d7 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ue passed as the
169d8 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
169d9 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e r is the total n
169da 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
169db 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e nts,.** includin
169dc 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d g the new anonym
169dd 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f ous savepoint, o
169de 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 pen on the B-Tre
169df 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 65 e. i.e. if there
169e0 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 .** are no activ
169e1 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
169e2 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d no other statem
169e3 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent-transactions
169e4 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 open,.** iState
169e5 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 20 ment is 1. This
169e6 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f anonymous savepo
169e7 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 61 int can be relea
169e8 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 sed or rolled ba
169e9 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 ck.** using the
169ea 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
169eb 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e point() function
169ec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
169ed 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
169ee 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 treeBeginStmt(Bt
169ef 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 ree *p, int iSta
169f0 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 tement){. int r
169f1 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 c;. BtShared *p
169f2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 Bt = p->pBt;. s
169f3 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
169f4 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
169f5 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
169f6 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
169f7 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c rt( pBt->readOnl
169f8 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 y==0 );. assert
169f9 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 ( iStatement>0 )
169fa 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 ;. assert( iSta
169fb 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 tement>p->db->nS
169fc 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 avepoint );. if
169fd 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72 61 ( NEVER(p->inTra
169fe 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns!=TRANS_WRITE
169ff 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 || pBt->readOnly
16a00 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 ) ){. rc = SQ
16a01 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 LITE_INTERNAL;.
16a02 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
16a03 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
16a04 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
16a05 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 ITE );. /* At
16a06 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 6c the pager level
16a07 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 , a statement tr
16a08 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 73 ansaction is a s
16a09 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 avepoint with.
16a0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 ** an index gr
16a0b 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 eater than all s
16a0c 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 65 avepoints create
16a0d 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 69 d explicitly usi
16a0e 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 ng. ** SQL st
16a0f 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 atements. It is
16a10 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c illegal to open,
16a11 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c release or roll
16a12 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 back any. **
16a13 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 20 such savepoints
16a14 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 6d while the statem
16a15 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
16a16 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 savepoint is act
16a17 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ive.. */.
16a18 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16a19 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 rOpenSavepoint(p
16a1a 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74 61 Bt->pPager, iSta
16a1b 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73 tement);. }. s
16a1c 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16a1d 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
16a1e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
16a1f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
16a20 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c o this function,
16a21 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20 53 op, is always S
16a22 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
16a23 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e K.** or SAVEPOIN
16a24 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73 20 T_RELEASE. This
16a25 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 20 function either
16a26 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c releases or roll
16a27 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 s back the.** sa
16a28 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 vepoint identifi
16a29 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
16a2a 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 iSavepoint, depe
16a2b 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c nding on the val
16a2c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a ue .** of op..**
16a2d 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 .** Normally, iS
16a2e 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 avepoint is grea
16a2f 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
16a30 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 l to zero. Howev
16a31 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 er, if op is.**
16a32 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
16a33 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70 6f CK, then iSavepo
16a34 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 int may also be
16a35 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 -1. In this case
16a36 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
16a37 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 s of the entire
16a38 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 transaction are
16a39 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 rolled back. Thi
16a3a 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a 2a s is different.*
16a3b 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 * from a normal
16a3c 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c transaction roll
16a3d 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b back, as no lock
16a3e 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 61 s are released a
16a3f 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 nd the.** transa
16a40 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 ction remains op
16a41 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 en..*/.SQLITE_PR
16a42 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
16a43 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
16a44 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 Btree *p, int op
16a45 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 , int iSavepoint
16a46 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
16a47 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 LITE_OK;. if( p
16a48 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d && p->inTrans==
16a49 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 TRANS_WRITE ){.
16a4a 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 BtShared *pBt
16a4b 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 = p->pBt;. a
16a4c 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 ssert( op==SAVEP
16a4d 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 OINT_RELEASE ||
16a4e 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f op==SAVEPOINT_RO
16a4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 LLBACK );. as
16a50 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 sert( iSavepoint
16a51 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 >=0 || (iSavepoi
16a52 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 nt==-1 && op==SA
16a53 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
16a54 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
16a55 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
16a56 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
16a57 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 42 agerSavepoint(pB
16a58 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 t->pPager, op, i
16a59 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Savepoint);.
16a5a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16a5b 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
16a5c 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 newDatabase(pBt)
16a5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
16a5e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16a5f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
16a60 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 c;.}../*.** Crea
16a61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 te a new cursor
16a62 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 68 for the BTree wh
16a63 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 ose root is on t
16a64 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c he page.** iTabl
16a65 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c e. If a read-onl
16a66 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 y cursor is requ
16a67 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 73 ested, it is ass
16a68 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 umed that.** the
16a69 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 caller already
16a6a 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 72 has at least a r
16a6b 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 ead-only transac
16a6c 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 tion open.** on
16a6d 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c 72 the database alr
16a6e 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 65 eady. If a write
16a6f 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 -cursor is reque
16a70 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 sted, then.** th
16a71 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 75 e caller is assu
16a72 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 6f med to have an o
16a73 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 pen write transa
16a74 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
16a75 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 wrFlag==0, then
16a76 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f the cursor can o
16a77 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 20 nly be used for
16a78 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 reading..** If w
16a79 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 rFlag==1, then t
16a7a 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 he cursor can be
16a7b 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e used for readin
16a7c 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 g or for.** writ
16a7d 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e ing if other con
16a7e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 ditions for writ
16a7f 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 ing are also met
16a80 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 . These.** are
16a81 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 the conditions t
16a82 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20 hat must be met
16a83 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69 in order for wri
16a84 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c ting to.** be al
16a85 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 lowed:.**.** 1:
16a86 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 The cursor must
16a87 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 have been opene
16a88 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 d with wrFlag==1
16a89 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 .**.** 2: Other
16a8a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
16a8b 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 tions that share
16a8c 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 the same pager
16a8d 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 cache.** but
16a8e 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 which are not i
16a8f 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d n the READ_UNCOM
16a90 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 MITTED state may
16a91 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 not have.**
16a92 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 cursors open wi
16a93 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 th wrFlag==0 on
16a94 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 the same table.
16a95 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 Otherwise.**
16a96 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 the changes ma
16a97 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74 65 de by this write
16a98 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 cursor would be
16a99 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 visible to.**
16a9a 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72 73 the read curs
16a9b 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 ors in the other
16a9c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
16a9d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 tion..**.** 3:
16a9e 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 The database mus
16a9f 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e t be writable (n
16aa0 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 ot on read-only
16aa1 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 media).**.** 4:
16aa2 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 There must be a
16aa3 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 n active transac
16aa4 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 tion..**.** No c
16aa5 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 hecking is done
16aa6 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
16aa7 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 t page iTable re
16aa8 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 ally is the.** r
16aa9 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d oot page of a b-
16aaa 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 tree. If it is
16aab 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 not, then the cu
16aac 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a rsor acquired.**
16aad 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 will not work c
16aae 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 orrectly..**.**
16aaf 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
16ab0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 at the sqlite3Bt
16ab1 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 20 reeCursorZero()
16ab2 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 0a has been called.
16ab3 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 6e ** on pCur to in
16ab4 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
16ab5 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 20 ory space prior
16ab6 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
16ab7 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 routine..*/.sta
16ab8 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 72 tic int btreeCur
16ab9 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c sor(. Btree *p,
16aba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16abb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16abc 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 The btree */.
16abd 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 int iTable,
16abe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16abf 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 /* Root p
16ac0 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 age of table to
16ac1 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 open */. int wr
16ac2 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 Flag,
16ac3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ac4 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 /* 1 to write.
16ac5 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 0 read-only */.
16ac6 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
16ac7 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 *pKeyInfo,
16ac8 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
16ac9 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73 arg to comparis
16aca 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
16acb 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 BtCursor *pCur
16acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16acd 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
16ace 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20 for new cursor
16acf 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 */.){. BtShared
16ad0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 *pBt = p->pBt;
16ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16ad2 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 20 * Shared b-tree
16ad3 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 handle */.. ass
16ad4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
16ad5 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 eHoldsMutex(p) )
16ad6 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c ;. assert( wrFl
16ad7 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 3d ag==0 || wrFlag=
16ad8 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 =1 );.. /* The
16ad9 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
16ada 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69 statements veri
16adb 66 79 20 74 68 61 74 20 69 66 20 74 68 69 73 20 fy that if this
16adc 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a 20 is a sharable .
16add 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62 ** b-tree datab
16ade 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74 ase, the connect
16adf 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 ion is holding t
16ae0 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c he required tabl
16ae1 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 61 e locks, . ** a
16ae2 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 nd that no other
16ae3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 connection has
16ae4 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 any open cursor
16ae5 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 that conflicts w
16ae6 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 6c ith . ** this l
16ae7 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 ock. */. asser
16ae8 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 t( hasSharedCach
16ae9 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 eTableLock(p, iT
16aea 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 3d able, pKeyInfo!=
16aeb 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b 0a 0, wrFlag+1) );.
16aec 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
16aed 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64 43 ==0 || !hasReadC
16aee 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61 62 onflicts(p, iTab
16aef 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 le) );.. /* Ass
16af0 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c ert that the cal
16af1 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20 74 ler has opened t
16af2 68 65 20 72 65 71 75 69 72 65 64 20 74 72 61 6e he required tran
16af3 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 73 saction. */. as
16af4 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 sert( p->inTrans
16af5 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 >TRANS_NONE );.
16af6 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d assert( wrFlag=
16af7 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 =0 || p->inTrans
16af8 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
16af9 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
16afa 70 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e 70 pPage1 && pBt->p
16afb 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a Page1->aData );.
16afc 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 72 46 . if( NEVER(wrF
16afd 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65 61 64 lag && pBt->read
16afe 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 65 74 Only) ){. ret
16aff 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
16b00 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 NLY;. }. if( i
16b01 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61 67 65 Table==1 && page
16b02 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3d rPagecount(pBt)=
16b03 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
16b04 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 SQLITE_EMPTY;.
16b05 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68 61 }.. /* Now tha
16b06 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f 72 t no other error
16b07 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 69 6e s can occur, fin
16b08 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 ish filling in t
16b09 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 2a 2a he BtCursor. **
16b0a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 6c variables and l
16b0b 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 ink the cursor i
16b0c 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 nto the BtShared
16b0d 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43 75 list. */. pCu
16b0e 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 r->pgnoRoot = (P
16b0f 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70 43 gno)iTable;. pC
16b10 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a ur->iPage = -1;.
16b11 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f pCur->pKeyInfo
16b12 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 = pKeyInfo;. p
16b13 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b Cur->pBtree = p;
16b14 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 . pCur->pBt = p
16b15 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c Bt;. pCur->wrFl
16b16 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b ag = (u8)wrFlag;
16b17 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d . pCur->pNext =
16b18 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 pBt->pCursor;.
16b19 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 if( pCur->pNext
16b1a 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e ){. pCur->pN
16b1b 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 ext->pPrev = pCu
16b1c 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 r;. }. pBt->pC
16b1d 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 ursor = pCur;.
16b1e 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
16b1f 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
16b20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 pCur->cachedRow
16b21 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e id = 0;. return
16b22 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 SQLITE_OK;.}.SQ
16b23 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16b24 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
16b25 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c sor(. Btree *p,
16b26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b28 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
16b29 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c */. int iTable,
16b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b2c 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
16b2d 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a table to open *
16b2e 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 /. int wrFlag,
16b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b31 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 /* 1 to write. 0
16b32 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 read-only */.
16b33 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a struct KeyInfo *
16b34 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 pKeyInfo,
16b35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
16b36 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d irst arg to xCom
16b37 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 pare() */. BtCu
16b38 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 rsor *pCur
16b39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b3a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
16b3b 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 new cursor here
16b3c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
16b3d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
16b3e 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
16b3f 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 btreeCursor(p, i
16b40 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 Table, wrFlag, p
16b41 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a KeyInfo, pCur);.
16b42 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16b43 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
16b44 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
16b45 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 turn the size of
16b46 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 a BtCursor obje
16b47 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a ct in bytes..**.
16b48 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
16b49 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 es is needed so
16b4a 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 that users of cu
16b4b 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c rsors can preall
16b4c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 ocate.** suffici
16b4d 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 ent storage to h
16b4e 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 old a cursor. T
16b4f 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 he BtCursor obje
16b50 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 ct is opaque.**
16b51 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 to users so they
16b52 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 cannot do the s
16b53 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 izeof() themselv
16b54 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 es - they must c
16b55 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 all.** this rout
16b56 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ine..*/.SQLITE_P
16b57 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16b58 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a e3BtreeCursorSiz
16b59 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 e(void){. retur
16b5a 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 n ROUND8(sizeof(
16b5b 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a 2f BtCursor));.}../
16b5c 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
16b5d 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c 6c memory that will
16b5e 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e be converted in
16b5f 74 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 to a BtCursor ob
16b60 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ject..**.** The
16b61 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 20 simple approach
16b62 68 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74 6f here would be to
16b63 20 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65 6e memset() the en
16b64 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 tire object.** t
16b65 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74 20 o zero. But it
16b66 74 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 74 turns out that t
16b67 68 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 he apPage[] and
16b68 61 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a 2a aiIdx[] arrays.*
16b69 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f * do not need to
16b6a 20 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20 74 be zeroed and t
16b6b 68 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20 73 hey are large, s
16b6c 6f 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 o we can save a
16b6d 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 69 lot.** of run-ti
16b6e 6d 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20 74 me by skipping t
16b6f 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f he initializatio
16b70 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d 65 n of those eleme
16b71 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts..*/.SQLITE_P
16b72 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
16b73 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 te3BtreeCursorZe
16b74 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b ro(BtCursor *p){
16b75 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
16b76 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73 6f offsetof(BtCurso
16b77 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a 2f r, iPage));.}../
16b78 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 63 *.** Set the cac
16b79 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 20 hed rowid value
16b7a 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 of every cursor
16b7b 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 in the same data
16b7c 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73 20 base file.** as
16b7d 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 20 pCur and having
16b7e 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 61 the same root pa
16b7f 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 75 ge number as pCu
16b80 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 r. The value is
16b81 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77 69 .** set to iRowi
16b82 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70 6f d..**.** Only po
16b83 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 61 6c sitive rowid val
16b84 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 ues are consider
16b85 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68 69 ed valid for thi
16b86 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 20 s cache..** The
16b87 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 61 6c cache is initial
16b88 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69 6e ized to zero, in
16b89 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76 61 dicating an inva
16b8a 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41 20 lid cache..** A
16b8b 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b 20 btree will work
16b8c 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20 6f fine with zero o
16b8d 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64 r negative rowid
16b8e 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e 6e s. We just cann
16b8f 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72 6f ot.** cache zero
16b90 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 or negative row
16b91 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 ids, which means
16b92 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 73 65 tables that use
16b93 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 zero or.** nega
16b94 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67 68 tive rowids migh
16b95 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 t run a little s
16b96 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20 70 lower. But in p
16b97 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a 2a ractice, zero.**
16b98 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 or negative row
16b99 69 64 73 20 61 72 65 20 76 65 72 79 20 75 6e 63 ids are very unc
16b9a 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73 68 ommon so this sh
16b9b 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70 72 ould not be a pr
16b9c 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 oblem..*/.SQLITE
16b9d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
16b9e 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
16b9f 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f hedRowid(BtCurso
16ba0 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65 33 r *pCur, sqlite3
16ba1 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a _int64 iRowid){.
16ba2 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
16ba3 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42 74 for(p=pCur->pBt
16ba4 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
16ba5 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
16ba6 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d f( p->pgnoRoot==
16ba7 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 pCur->pgnoRoot )
16ba8 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 p->cachedRowid
16ba9 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20 20 = iRowid;. }.
16baa 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 61 assert( pCur->ca
16bab 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 69 chedRowid==iRowi
16bac 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 d );.}../*.** Re
16bad 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 64 20 turn the cached
16bae 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67 69 rowid for the gi
16baf 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20 6e ven cursor. A n
16bb0 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 0a egative or zero.
16bb1 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 ** return value
16bb2 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 indicates that t
16bb3 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 69 he rowid cache i
16bb4 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73 68 s invalid and sh
16bb5 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f 72 ould be.** ignor
16bb6 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77 69 ed. If the rowi
16bb7 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 76 65 d cache has neve
16bb8 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73 65 r before been se
16bb9 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65 72 t, then a.** zer
16bba 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a o is returned..*
16bbb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16bbc 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
16bbd 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61 qlite3BtreeGetCa
16bbe 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 chedRowid(BtCurs
16bbf 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 or *pCur){. ret
16bc0 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65 64 urn pCur->cached
16bc1 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Rowid;.}../*.**
16bc2 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 20 Close a cursor.
16bc3 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f The read lock o
16bc4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
16bc5 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 0a ile is released.
16bc6 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 ** when the last
16bc7 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 cursor is close
16bc8 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
16bc9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16bca 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 BtreeCloseCursor
16bcb 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
16bcc 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 {. Btree *pBtre
16bcd 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 e = pCur->pBtree
16bce 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29 ;. if( pBtree )
16bcf 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
16bd0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
16bd1 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20 pCur->pBt;.
16bd2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
16bd3 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 73 r(pBtree);. s
16bd4 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
16bd5 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 Cursor(pCur);.
16bd6 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65 if( pCur->pPre
16bd7 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d v ){. pCur-
16bd8 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
16bd9 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pCur->pNext;.
16bda 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 }else{. pB
16bdb 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 t->pCursor = pCu
16bdc 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a r->pNext;. }.
16bdd 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e if( pCur->pN
16bde 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 ext ){. pCu
16bdf 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 r->pNext->pPrev
16be0 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 = pCur->pPrev;.
16be1 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 }. for(i=0
16be2 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 ; i<=pCur->iPage
16be3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
16be4 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e leasePage(pCur->
16be5 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 apPage[i]);.
16be6 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 }. unlockBtre
16be7 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
16be8 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 invalidateOv
16be9 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 erflowCache(pCur
16bea 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 );. /* sqlite
16beb 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 3_free(pCur); */
16bec 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
16bed 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a eLeave(pBtree);.
16bee 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
16bef 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
16bf0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 42 Make sure the B
16bf1 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69 tCursor* given i
16bf2 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 n the argument h
16bf3 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74 as a valid.** Bt
16bf4 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75 Cursor.info stru
16bf5 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 73 cture. If it is
16bf6 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c not already val
16bf7 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 65 id, call.** btre
16bf8 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 eParseCell() to
16bf9 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a fill it in..**.*
16bfa 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 * BtCursor.info
16bfb 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 is a cache of th
16bfc 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e e information in
16bfd 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c the current cel
16bfe 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 l..** Using this
16bff 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74 cache reduces t
16c00 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c he number of cal
16c01 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 65 ls to btreeParse
16c02 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 Cell()..**.** 20
16c03 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 65 07-06-25: There
16c04 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f 6d is a bug in som
16c05 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d 53 e versions of MS
16c06 56 43 20 74 68 61 74 20 63 61 75 73 65 20 74 68 VC that cause th
16c07 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 6f e.** compiler to
16c08 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 43 crash when getC
16c09 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d 70 ellInfo() is imp
16c0a 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 lemented as a ma
16c0b 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 72 cro..** But ther
16c0c 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 62 e is a measureab
16c0d 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 61 le speed advanta
16c0e 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 20 ge to using the
16c0f 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20 macro on gcc.**
16c10 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 69 (when less compi
16c11 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ler optimization
16c12 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f s like -Os or -O
16c13 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 74 0 are used and t
16c14 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 69 he.** compiler i
16c15 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72 65 s not doing agre
16c16 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e 29 ssive inlining.)
16c17 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 72 65 So we use a re
16c18 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 al function.** f
16c19 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d 61 or MSVC and a ma
16c1a 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68 69 cro for everythi
16c1b 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65 74 ng else. Ticket
16c1c 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64 #2457..*/.#ifnd
16c1d 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61 74 ef NDEBUG. stat
16c1e 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 43 65 ic void assertCe
16c1f 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 llInfo(BtCursor
16c20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c 6c *pCur){. Cell
16c21 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 69 Info info;. i
16c22 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d nt iPage = pCur-
16c23 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d 73 >iPage;. mems
16c24 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69 7a et(&info, 0, siz
16c25 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20 20 eof(info));.
16c26 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 btreeParseCell(p
16c27 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 Cur->apPage[iPag
16c28 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b e], pCur->aiIdx[
16c29 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a iPage], &info);.
16c2a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 assert( memc
16c2b 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d mp(&info, &pCur-
16c2c 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e >info, sizeof(in
16c2d 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 fo))==0 );. }.#
16c2e 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 else. #define a
16c2f 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 ssertCellInfo(x)
16c30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f .#endif.#ifdef _
16c31 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 MSC_VER. /* Use
16c32 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e a real function
16c33 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b in MSVC to work
16c34 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 around bugs in
16c35 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a that compiler. *
16c36 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 /. static void
16c37 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 getCellInfo(BtCu
16c38 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 rsor *pCur){.
16c39 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e if( pCur->info.
16c3a 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nSize==0 ){.
16c3b 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 int iPage = pC
16c3c 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 ur->iPage;.
16c3d 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
16c3e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
16c3f 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
16c40 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
16c41 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d fo);. pCur-
16c42 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a >validNKey = 1;.
16c43 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16c44 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
16c45 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pCur);. }. }
16c46 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 .#else /* if not
16c47 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f _MSC_VER */. /
16c48 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e * Use a macro in
16c49 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 all other compi
16c4a 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 lers so that the
16c4b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c function is inl
16c4c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ined */.#define
16c4d 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
16c4e 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
16c4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c51 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70 \. if( p
16c52 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
16c53 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 =0 ){
16c54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c56 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e \. in
16c57 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e t iPage = pCur->
16c58 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 iPage;
16c59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c5b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 62 \. b
16c5c 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 treeParseCell(pC
16c5d 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 ur->apPage[iPage
16c5e 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 ],pCur->aiIdx[iP
16c5f 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f age],&pCur->info
16c60 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e 76 ); \. pCur->v
16c61 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 20 alidNKey = 1;
16c62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c65 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 \. }else{
16c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c6a 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 72 \. asser
16c6b 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
16c6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c6f 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e 64 \. }.#end
16c70 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 2a if /* _MSC_VER *
16c71 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 /..#ifndef NDEBU
16c72 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 G /* The next r
16c73 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c 79 outine used only
16c74 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 within assert()
16c75 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 2f statements */./
16c76 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
16c77 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 42 74 if the given Bt
16c78 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 2e Cursor is valid.
16c79 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f 72 A valid cursor
16c7a 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 is one.** that
16c7b 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
16c7c 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 69 nting to a row i
16c7d 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 20 n a (non-empty)
16c7e 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 69 table..** This i
16c7f 73 20 61 20 76 65 72 69 66 69 63 61 74 69 6f 6e s a verification
16c80 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
16c81 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 only within ass
16c82 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
16c83 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16c84 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16c85 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 treeCursorIsVali
16c86 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
16c87 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 ){. return pCur
16c88 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65 && pCur->eState
16c89 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a ==CURSOR_VALID;.
16c8a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 }.#endif /* NDEB
16c8b 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 UG */../*.** Set
16c8c 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 73 *pSize to the s
16c8d 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 ize of the buffe
16c8e 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 r needed to hold
16c8f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a the value of.**
16c90 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68 65 the key for the
16c91 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 current entry.
16c92 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 If the cursor i
16c93 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a s not pointing.*
16c94 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 * to a valid ent
16c95 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 65 ry, *pSize is se
16c96 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 t to 0. .**.** F
16c97 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 or a table with
16c98 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 the INTKEY flag
16c99 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e set, this routin
16c9a 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b 65 e returns the ke
16c9b 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 y.** itself, not
16c9c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
16c9d 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 2e ytes in the key.
16c9e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 .**.** The calle
16c9f 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e 20 r must position
16ca0 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f 72 the cursor prior
16ca1 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 to invoking thi
16ca2 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a 2a s routine..** .*
16ca3 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
16ca4 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 20 annot fail. It
16ca5 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 always returns S
16ca6 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53 QLITE_OK. .*/.S
16ca7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16ca8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 t sqlite3BtreeKe
16ca9 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a ySize(BtCursor *
16caa 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a 65 pCur, i64 *pSize
16cab 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
16cac 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16cad 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
16cae 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16caf 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c URSOR_INVALID ||
16cb0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16cb1 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16cb2 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
16cb3 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
16cb4 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 ){. *pSize =
16cb5 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
16cb6 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
16cb7 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 );. *pSize =
16cb8 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b pCur->info.nKey;
16cb9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
16cba 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16cbb 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 * Set *pSize to
16cbc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
16cbd 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 tes of data in t
16cbe 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 he entry the.**
16cbf 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 cursor currently
16cc0 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a 2a points to..**.*
16cc1 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 * The caller mus
16cc2 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 t guarantee that
16cc3 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 the cursor is p
16cc4 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f 6e ointing to a non
16cc5 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20 65 -NULL.** valid e
16cc6 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72 20 ntry. In other
16cc7 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 69 words, the calli
16cc8 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75 73 ng procedure mus
16cc9 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20 74 t guarantee.** t
16cca 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 hat the cursor h
16ccb 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74 65 as Cursor.eState
16ccc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a ==CURSOR_VALID..
16ccd 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 **.** Failure is
16cce 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 not possible.
16ccf 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c This function al
16cd0 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 4c ways returns SQL
16cd1 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d 69 ITE_OK..** It mi
16cd2 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c 6c ght just as well
16cd3 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 65 20 be a procedure
16cd4 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64 29 (returning void)
16cd5 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75 65 but we continue
16cd6 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 6e .** to return an
16cd7 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 integer result
16cd8 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72 69 code for histori
16cd9 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f 0a cal reasons..*/.
16cda 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16cdb 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
16cdc 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 ataSize(BtCursor
16cdd 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 *pCur, u32 *pSi
16cde 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ze){. assert( c
16cdf 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16ce0 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
16ce1 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16ce2 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16ce3 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 . getCellInfo(p
16ce4 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d Cur);. *pSize =
16ce5 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 pCur->info.nDat
16ce6 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 a;. return SQLI
16ce7 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
16ce8 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e Given the page n
16ce9 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 umber of an over
16cea 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 flow page in the
16ceb 20 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d database (param
16cec 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 eter.** ovfl), t
16ced 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e his function fin
16cee 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 ds the page numb
16cef 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 er of the next p
16cf0 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c age in the .** l
16cf1 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 inked list of ov
16cf2 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 erflow pages. If
16cf3 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73 possible, it us
16cf4 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 es the auto-vacu
16cf5 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 um.** pointer-ma
16cf6 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f p data instead o
16cf7 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f f reading the co
16cf8 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 ntent of page ov
16cf9 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a fl to do so. .**
16cfa 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
16cfb 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65 occurs an SQLite
16cfc 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
16cfd 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 eturned. Otherwi
16cfe 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 se:.**.** The pa
16cff 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
16d00 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 next overflow p
16d01 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 age in the linke
16d02 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 72 d list is .** wr
16d03 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e itten to *pPgnoN
16d04 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 ext. If page ovf
16d05 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 61 l is the last pa
16d06 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 64 ge in its linked
16d07 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 6e .** list, *pPgn
16d08 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 oNext is set to
16d09 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 zero. .**.** If
16d0a 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 ppPage is not NU
16d0b 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65 LL, and a refere
16d0c 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 nce to the MemPa
16d0d 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73 ge object corres
16d0e 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 61 ponding.** to pa
16d0f 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20 ge number pOvfl
16d10 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 was obtained, th
16d11 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 en *ppPage is se
16d12 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
16d13 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e at.** reference.
16d14 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f It is the respo
16d15 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 nsibility of the
16d16 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20 caller to call
16d17 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a 2a releasePage().**
16d18 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 66 on *ppPage to f
16d19 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 ree the referenc
16d1a 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 6e e. In no referen
16d1b 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 ce was obtained
16d1c 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 (because.** the
16d1d 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 20 pointer-map was
16d1e 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 used to obtain t
16d1f 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 50 he value for *pP
16d20 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a 2a gnoNext), then.*
16d21 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 * *ppPage is set
16d22 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 to zero..*/.sta
16d23 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66 tic int getOverf
16d24 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61 lowPage(. BtSha
16d25 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 red *pBt,
16d26 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
16d27 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
16d28 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 Pgno ovfl,
16d29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16d2a 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 6c * Current overfl
16d2b 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a ow page number *
16d2c 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 /. MemPage **pp
16d2d 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
16d2e 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 /* OUT: MemPage
16d2f 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 handle (may be
16d30 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 NULL) */. Pgno
16d31 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 *pPgnoNext
16d32 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
16d33 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 Next overflow pa
16d34 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a ge number */.){.
16d35 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b Pgno next = 0;
16d36 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
16d37 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 e = 0;. int rc
16d38 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
16d39 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16d3a 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
16d3b 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
16d3c 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a rt(pPgnoNext);..
16d3d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16d3e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
16d3f 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 /* Try to find
16d40 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e the next page in
16d41 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 the overflow li
16d42 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a st using the. *
16d43 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 * autovacuum poi
16d44 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 nter-map pages.
16d45 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e Guess that the n
16d46 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a ext page in . *
16d47 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c * the overflow l
16d48 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 ist is page numb
16d49 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 er (ovfl+1). If
16d4a 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 that guess turns
16d4b 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 . ** out to be
16d4c 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 wrong, fall bac
16d4d 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 k to loading the
16d4e 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 data of page .
16d4f 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 ** number ovfl
16d50 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
16d51 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 next page numbe
16d52 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 r.. */. if( pB
16d53 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
16d54 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a . Pgno pgno;.
16d55 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 Pgno iGuess
16d56 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 = ovfl+1;. u8
16d57 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 eType;.. whi
16d58 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 le( PTRMAP_ISPAG
16d59 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c E(pBt, iGuess) |
16d5a 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e | iGuess==PENDIN
16d5b 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
16d5c 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 ){. iGuess
16d5d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 ++;. }.. i
16d5e 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 f( iGuess<=pager
16d5f 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 Pagecount(pBt) )
16d60 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 {. rc = ptr
16d61 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 mapGet(pBt, iGue
16d62 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e ss, &eType, &pgn
16d63 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 o);. if( rc
16d64 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 ==SQLITE_OK && e
16d65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 Type==PTRMAP_OVE
16d66 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d RFLOW2 && pgno==
16d67 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 ovfl ){.
16d68 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 next = iGuess;.
16d69 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
16d6a 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d TE_DONE;. }
16d6b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
16d6c 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 78 f.. assert( nex
16d6d 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 t==0 || rc==SQLI
16d6e 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 TE_DONE );. if(
16d6f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16d70 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 {. rc = btree
16d71 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 GetPage(pBt, ovf
16d72 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 l, &pPage, 0);.
16d73 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
16d74 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
16d75 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 e==0 );. if(
16d76 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
16d77 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 . next = ge
16d78 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
16d79 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ata);. }. }.
16d7a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20 . *pPgnoNext =
16d7b 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50 61 next;. if( ppPa
16d7c 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61 67 ge ){. *ppPag
16d7d 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65 6c e = pPage;. }el
16d7e 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 se{. releaseP
16d7f 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a age(pPage);. }.
16d80 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 return (rc==SQ
16d81 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 LITE_DONE ? SQLI
16d82 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a TE_OK : rc);.}..
16d83 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20 /*.** Copy data
16d84 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f from a buffer to
16d85 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d a page, or from
16d86 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66 a page to a buf
16d87 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c fer..**.** pPayl
16d88 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 72 oad is a pointer
16d89 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 20 to data stored
16d8a 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 on database page
16d8b 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20 pDbPage..** If
16d8c 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 20 argument eOp is
16d8d 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 74 false, then nByt
16d8e 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 e bytes of data
16d8f 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 are copied.** fr
16d90 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 74 om pPayload to t
16d91 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 he buffer pointe
16d92 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 66 d at by pBuf. If
16d93 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a eOp is true,.**
16d94 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67 then sqlite3Pag
16d95 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c erWrite() is cal
16d96 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 61 led on pDbPage a
16d97 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a 2a nd nByte bytes.*
16d98 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f * of data are co
16d99 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 pied from the bu
16d9a 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 61 ffer pBuf to pPa
16d9b 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c yload..**.** SQL
16d9c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
16d9d 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f ed on success, o
16d9e 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f therwise an erro
16d9f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 r code..*/.stati
16da0 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61 c int copyPayloa
16da1 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 6c d(. void *pPayl
16da2 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f oad, /
16da3 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 * Pointer to pag
16da4 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 e data */. void
16da5 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
16da6 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
16da7 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 to buffer */.
16da8 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 int nByte,
16da9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
16daa 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
16dab 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f copy */. int eO
16dac 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
16dad 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 /* 0 -> copy
16dae 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e from page, 1 ->
16daf 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a 2f copy to page */
16db0 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
16db1 67 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ge /*
16db2 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Page containing
16db3 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 pPayload */.){.
16db4 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 20 if( eOp ){.
16db5 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f /* Copy data fro
16db6 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 65 m buffer to page
16db7 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 74 (a write operat
16db8 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ion) */. int
16db9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16dba 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b rWrite(pDbPage);
16dbb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
16dbc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16dbd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
16dbe 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79 . memcpy(pPay
16dbf 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 74 load, pBuf, nByt
16dc0 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e);. }else{.
16dc1 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 /* Copy data fr
16dc2 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 65 om page to buffe
16dc3 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 74 r (a read operat
16dc4 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 ion) */. memc
16dc5 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f 61 py(pBuf, pPayloa
16dc6 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 d, nByte);. }.
16dc7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16dc8 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 K;.}../*.** This
16dc9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
16dca 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65 d to read or ove
16dcb 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69 rwrite payload i
16dcc 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f nformation.** fo
16dcd 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 r the entry that
16dce 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72 the pCur cursor
16dcf 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e is pointing to.
16dd0 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70 If the eOp.** p
16dd1 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 arameter is 0, t
16dd2 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70 his is a read op
16dd3 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 6f eration (data co
16dd4 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 pied into.** buf
16dd5 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 74 fer pBuf). If it
16dd6 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 is non-zero, a
16dd7 77 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 69 write (data copi
16dd8 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 ed from.** buffe
16dd9 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 r pBuf)..**.** A
16dda 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 total of "amt"
16ddb 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f bytes are read o
16ddc 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e r written beginn
16ddd 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e ing at "offset".
16dde 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64 .** Data is read
16ddf 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 to or from the
16de0 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a buffer pBuf..**.
16de1 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62 ** The content b
16de2 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69 eing read or wri
16de3 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61 tten might appea
16de4 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61 r on the main pa
16de5 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74 ge.** or be scat
16de6 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c tered out on mul
16de7 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70 tiple overflow p
16de8 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ages..**.** If t
16de9 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 6e he BtCursor.isIn
16dea 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 crblobHandle fla
16deb 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 68 g is set, and th
16dec 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72 e current.** cur
16ded 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f sor entry uses o
16dee 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 ne or more overf
16def 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 20 low pages, this
16df0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f function.** allo
16df1 63 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 20 cates space for
16df2 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 and lazily poplu
16df3 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c 6f ates the overflo
16df4 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 w page-list .**
16df5 63 61 63 68 65 20 61 72 72 61 79 20 28 42 74 43 cache array (BtC
16df6 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 ursor.aOverflow)
16df7 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c . Subsequent cal
16df8 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 63 ls use this.** c
16df9 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 ache to make see
16dfa 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 king to the supp
16dfb 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65 lied offset more
16dfc 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a efficient..**.*
16dfd 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c * Once an overfl
16dfe 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ow page-list cac
16dff 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f he has been allo
16e00 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 65 cated, it may be
16e01 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20 .** invalidated
16e02 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75 if some other cu
16e03 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74 rsor writes to t
16e04 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f he same table, o
16e05 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73 r if.** the curs
16e06 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61 or is moved to a
16e07 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20 different row.
16e08 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e Additionally, in
16e09 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 auto-vacuum.**
16e0a 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 mode, the follow
16e0b 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69 ing events may i
16e0c 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65 nvalidate an ove
16e0d 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
16e0e 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a cache..**.** *
16e0f 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 An incremental
16e10 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 vacuum,.** * A
16e11 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f commit in auto_
16e12 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f vacuum="full" mo
16e13 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74 de,.** * Creat
16e14 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79 ing a table (may
16e15 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 require moving
16e16 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
16e17 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
16e18 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a accessPayload(.
16e19 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
16e1a 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 , /* Cursor
16e1b 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 pointing to ent
16e1c 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 ry to read from
16e1d 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 2c */. u32 offset,
16e1e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 /* Beg
16e1f 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 in reading this
16e20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64 far into payload
16e21 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 20 */. u32 amt,
16e22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
16e23 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 ad this many byt
16e24 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 es */. unsigned
16e25 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 char *pBuf, /*
16e26 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20 Write the bytes
16e27 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 into this buffer
16e28 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 20 */ . int eOp
16e29 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
16e2a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e ero to read. non
16e2b 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 -zero to write.
16e2c 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
16e2d 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b char *aPayload;
16e2e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
16e2f 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 TE_OK;. u32 nKe
16e30 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 y;. int iIdx =
16e31 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 0;. MemPage *pP
16e32 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
16e33 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
16e34 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f /* Btree page o
16e35 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 f current entry
16e36 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
16e37 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 Bt = pCur->pBt;
16e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e39 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 /* Btree this c
16e3a 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f ursor belongs to
16e3b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
16e3c 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Page );. assert
16e3d 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16e3e 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16e3f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16e40 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
16e41 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 e]<pPage->nCell
16e42 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 );. assert( cur
16e43 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16e44 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c ur) );.. getCel
16e45 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 lInfo(pCur);. a
16e46 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e Payload = pCur->
16e47 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 info.pCell + pCu
16e48 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b r->info.nHeader;
16e49 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 . nKey = (pPage
16e4a 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 ->intKey ? 0 : (
16e4b 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e int)pCur->info.n
16e4c 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 Key);.. if( NEV
16e4d 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 ER(offset+amt >
16e4e 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e nKey+pCur->info.
16e4f 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26 61 nData) . || &a
16e50 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e Payload[pCur->in
16e51 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 fo.nLocal] > &pP
16e52 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
16e53 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b usableSize]. ){
16e54 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 . /* Trying t
16e55 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 o read or write
16e56 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
16e57 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 65 the data is an e
16e58 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75 rror */. retu
16e59 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
16e5a 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f T_BKPT;. }.. /
16e5b 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 * Check if data
16e5c 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 must be read/wri
16e5d 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 tten to/from the
16e5e 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73 65 btree page itse
16e5f 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 lf. */. if( off
16e60 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e set<pCur->info.n
16e61 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 Local ){. int
16e62 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 a = amt;. if
16e63 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d ( a+offset>pCur-
16e64 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a >info.nLocal ){.
16e65 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e a = pCur->
16e66 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 info.nLocal - of
16e67 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 fset;. }.
16e68 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 rc = copyPayload
16e69 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 (&aPayload[offse
16e6a 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 t], pBuf, a, eOp
16e6b 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 , pPage->pDbPage
16e6c 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 );. offset =
16e6d 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 0;. pBuf += a
16e6e 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a ;. amt -= a;.
16e6f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 }else{. off
16e70 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 set -= pCur->inf
16e71 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 o.nLocal;. }..
16e72 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16e73 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 OK && amt>0 ){.
16e74 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66 const u32 ovf
16e75 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 lSize = pBt->usa
16e76 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a bleSize - 4; /*
16e77 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70 Bytes content p
16e78 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a er ovfl page */.
16e79 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67 Pgno nextPag
16e7a 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65 e;.. nextPage
16e7b 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 61 = get4byte(&aPa
16e7c 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f yload[pCur->info
16e7d 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e .nLocal]);..#ifn
16e7e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16e7f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 INCRBLOB. /*
16e80 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f If the isIncrblo
16e81 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 bHandle flag is
16e82 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 75 set and the BtCu
16e83 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d rsor.aOverflow[]
16e84 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 . ** has not
16e85 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 been allocated,
16e86 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e allocate it now.
16e87 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73 69 The array is si
16e88 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e zed at. ** on
16e89 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 e entry for each
16e8a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
16e8b 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 n the overflow c
16e8c 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a hain. The. **
16e8d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
16e8e 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c the first overfl
16e8f 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 65 ow page is store
16e90 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30 d in aOverflow[0
16e91 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41 ],. ** etc. A
16e92 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74 value of 0 in t
16e93 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 he aOverflow[] a
16e94 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20 rray means "not
16e95 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a yet known". *
16e96 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73 20 * (the cache is
16e97 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64 lazily populated
16e98 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ).. */. if
16e99 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c ( pCur->isIncrbl
16e9a 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75 obHandle && !pCu
16e9b 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a r->aOverflow ){.
16e9c 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20 int nOvfl
16e9d 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 = (pCur->info.nP
16e9e 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66 ayload-pCur->inf
16e9f 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a o.nLocal+ovflSiz
16ea0 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 e-1)/ovflSize;.
16ea1 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 pCur->aOver
16ea2 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73 flow = (Pgno *)s
16ea3 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
16ea4 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f (sizeof(Pgno)*nO
16ea5 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 6e vfl);. /* n
16ea6 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20 70 Ovfl is always p
16ea7 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 74 20 ositive. If it
16ea8 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63 68 were zero, fetch
16ea9 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68 61 Payload would ha
16eaa 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65 6e ve. ** been
16eab 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 used instead of
16eac 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a this routine. *
16ead 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 /. if( ALWA
16eae 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70 43 YS(nOvfl) && !pC
16eaf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b ur->aOverflow ){
16eb0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
16eb1 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
16eb2 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
16eb3 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f * If the overflo
16eb4 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
16eb5 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
16eb6 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 20 ated and the.
16eb7 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 ** entry for th
16eb8 65 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64 e first required
16eb9 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
16eba 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 s valid, skip.
16ebb 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f ** directly to
16ebc 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
16ebd 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 if( pCur->aOverf
16ebe 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 low && pCur->aOv
16ebf 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 erflow[offset/ov
16ec0 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 flSize] ){.
16ec1 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f iIdx = (offset/
16ec2 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 ovflSize);.
16ec3 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 nextPage = pCur
16ec4 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
16ec5 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 ];. offset
16ec6 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 = (offset%ovflSi
16ec7 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ze);. }.#endi
16ec8 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63 f.. for( ; rc
16ec9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 ==SQLITE_OK && a
16eca 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65 mt>0 && nextPage
16ecb 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e ; iIdx++){..#ifn
16ecc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16ecd 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f INCRBLOB. /
16ece 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 70 * If required, p
16ecf 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72 opulate the over
16ed0 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
16ed1 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 ache. */. i
16ed2 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c f( pCur->aOverfl
16ed3 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ow ){. as
16ed4 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 sert(!pCur->aOve
16ed5 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 rflow[iIdx] || p
16ed6 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16ed7 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b Idx]==nextPage);
16ed8 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 . pCur->a
16ed9 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d Overflow[iIdx] =
16eda 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 20 nextPage;.
16edb 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 }.#endif..
16edc 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 if( offset>=ovf
16edd 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 lSize ){.
16ede 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 /* The only rea
16edf 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73 son to read this
16ee0 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61 page is to obta
16ee1 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 in the page.
16ee2 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f ** number fo
16ee3 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 r the next page
16ee4 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
16ee5 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a chain. The page.
16ee6 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 ** data
16ee7 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e is not required.
16ee8 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f So first try to
16ee9 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 lookup the over
16eea 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 flow. **
16eeb 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c page-list cache,
16eec 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 if any, then fa
16eed 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67 ll back to the g
16eee 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29 etOverflowPage()
16eef 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 . ** func
16ef0 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f tion.. */
16ef1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
16ef2 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
16ef3 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e if( pCur->
16ef4 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 aOverflow && pCu
16ef5 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 r->aOverflow[iId
16ef6 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 x+1] ){.
16ef7 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 nextPage = pCu
16ef8 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 r->aOverflow[iId
16ef9 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 x+1];. }
16efa 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 else .#endif.
16efb 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f rc = getO
16efc 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c verflowPage(pBt,
16efd 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e nextPage, 0, &n
16efe 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 extPage);.
16eff 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c offset -= ovfl
16f00 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Size;. }els
16f01 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 e{. /* Ne
16f02 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 ed to read this
16f03 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 page properly. I
16f04 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 t contains some
16f05 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a of the. *
16f06 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 20 * range of data
16f07 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 65 that is being re
16f08 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 ad (eOp==0) or w
16f09 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e ritten (eOp!=0).
16f0a 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
16f0b 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 DbPage *pDbP
16f0c 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 age;. int
16f0d 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 a = amt;.
16f0e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16f0f 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 gerGet(pBt->pPag
16f10 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 er, nextPage, &p
16f11 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
16f12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16f13 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
16f14 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 aPayload = sqlit
16f15 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
16f16 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
16f17 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 nextPage = ge
16f18 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64 29 t4byte(aPayload)
16f19 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
16f1a 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 a + offset > ovf
16f1b 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 lSize ){.
16f1c 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a a = ovflSiz
16f1d 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 e - offset;.
16f1e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16f1f 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f rc = copyPaylo
16f20 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 ad(&aPayload[off
16f21 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c set+4], pBuf, a,
16f22 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b 0a eOp, pDbPage);.
16f23 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
16f24 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 3PagerUnref(pDbP
16f25 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
16f26 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 offset = 0;.
16f27 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a amt -= a;.
16f28 20 20 20 20 20 20 20 20 20 20 70 42 75 66 20 2b pBuf +
16f29 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = a;. }.
16f2a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
16f2b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
16f2c 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 TE_OK && amt>0 )
16f2d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
16f2e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
16f2f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
16f30 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
16f31 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79 part of the key
16f32 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
16f33 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 cursor pCur. E
16f34 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 xactly.** "amt"
16f35 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 bytes will be tr
16f36 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 ansfered into pB
16f37 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 uf[]. The trans
16f38 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 fer.** begins at
16f39 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a "offset"..**.**
16f3a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
16f3b 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75 ensure that pCu
16f3c 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
16f3d 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20 a valid row.**
16f3e 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a in the table..**
16f3f 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
16f40 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
16f41 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
16f42 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
16f43 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 s.** wrong. An
16f44 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
16f45 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 d if "offset+amt
16f46 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e " is larger than
16f47 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c .** the availabl
16f48 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 e payload..*/.SQ
16f49 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16f4a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
16f4b 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
16f4c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
16f4d 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 amt, void *pBuf
16f4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
16f4f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16f50 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
16f51 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16f52 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16f53 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
16f54 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d Page>=0 && pCur-
16f55 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
16f56 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 age] );. assert
16f57 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
16f58 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d ur->iPage]<pCur-
16f59 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
16f5a 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 age]->nCell );.
16f5b 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 return accessPa
16f5c 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 yload(pCur, offs
16f5d 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e et, amt, (unsign
16f5e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 ed char*)pBuf, 0
16f5f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
16f60 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
16f61 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
16f62 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 h cursor pCur.
16f63 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 Exactly.** "amt"
16f64 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 bytes will be t
16f65 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 ransfered into p
16f66 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e Buf[]. The tran
16f67 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 sfer.** begins a
16f68 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a t "offset"..**.*
16f69 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
16f6a 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 OK on success or
16f6b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
16f6c 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
16f6d 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 ** wrong. An er
16f6e 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
16f6f 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 if "offset+amt"
16f70 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a is larger than.*
16f71 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 * the available
16f72 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 payload..*/.SQLI
16f73 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16f74 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
16f75 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
16f76 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
16f77 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 amt, void *pBuf)
16f78 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 {. int rc;..#if
16f79 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16f7a 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 _INCRBLOB. if (
16f7b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16f7c 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
16f7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
16f7e 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 TE_ABORT;. }.#e
16f7f 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
16f80 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16f81 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
16f82 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
16f83 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
16f84 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16f85 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
16f86 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16f87 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16f88 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
16f89 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 >iPage>=0 && pCu
16f8a 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16f8b 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 iPage] );. as
16f8c 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
16f8d 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
16f8e 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16f8f 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 ->iPage]->nCell
16f90 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 );. rc = acce
16f91 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
16f92 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 offset, amt, pBu
16f93 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 f, 0);. }. ret
16f94 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16f95 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
16f96 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 r to payload inf
16f97 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 ormation from th
16f98 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 e entry that the
16f99 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72 .** pCur cursor
16f9a 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e is pointing to.
16f9b 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 The pointer is
16f9c 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e to the beginnin
16f9d 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 g of.** the key
16f9e 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e if skipKey==0 an
16f9f 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 d it points to t
16fa0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
16fa1 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b data if.** skipK
16fa2 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 ey==1. The numb
16fa3 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 er of bytes of a
16fa4 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 vailable key/dat
16fa5 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 a is written.**
16fa6 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 into *pAmt. If
16fa7 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 *pAmt==0, then t
16fa8 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
16fa9 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a d will not be.**
16faa 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 a valid pointer
16fab 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
16fac 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d tine is an optim
16fad 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 ization. It is
16fae 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 common for the e
16faf 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 ntire key.** and
16fb0 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 data to fit on
16fb1 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 the local page a
16fb2 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 nd for there to
16fb3 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a be no overflow.*
16fb4 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 * pages. When t
16fb5 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 hat is so, this
16fb6 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 routine can be u
16fb7 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 sed to access th
16fb8 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 e.** key and dat
16fb9 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 a without making
16fba 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65 a copy. If the
16fbb 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 key and/or data
16fbc 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 spills.** onto
16fbd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 overflow pages,
16fbe 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f then accessPaylo
16fbf 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65 ad() must be use
16fc0 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a d to reassemble.
16fc1 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 ** the key/data
16fc2 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f and copy it into
16fc3 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 a preallocated
16fc4 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 buffer..**.** Th
16fc5 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e e pointer return
16fc6 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
16fc7 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c ne looks directl
16fc8 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 y into the cache
16fc9 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65 d.** page of the
16fca 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 database. The
16fcb 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67 data might chang
16fcc 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 e or move the ne
16fcd 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 xt time.** any b
16fce 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20 tree routine is
16fcf 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
16fd0 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
16fd1 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c char *fetchPayl
16fd2 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 oad(. BtCursor
16fd3 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 *pCur, /* C
16fd4 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
16fd5 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 o entry to read
16fd6 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 from */. int *p
16fd7 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f Amt, /
16fd8 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 * Write the numb
16fd9 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 er of available
16fda 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 bytes here */.
16fdb 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20 int skipKey
16fdc 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67 /* read beg
16fdd 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69 inning at data i
16fde 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a f this is true *
16fdf 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 /.){. unsigned
16fe0 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a char *aPayload;.
16fe1 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
16fe2 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 ;. u32 nKey;.
16fe3 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 u32 nLocal;.. a
16fe4 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26 ssert( pCur!=0 &
16fe5 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 & pCur->iPage>=0
16fe6 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 && pCur->apPage
16fe7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a [pCur->iPage]);.
16fe8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16fe9 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16fea 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
16feb 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16fec 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 ex(pCur) );. pP
16fed 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
16fee 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
16fef 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16ff0 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
16ff1 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ge]<pPage->nCell
16ff2 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );. if( NEVER(
16ff3 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
16ff4 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65 ==0) ){. btre
16ff5 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d eParseCell(pCur-
16ff6 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
16ff7 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 age], pCur->aiId
16ff8 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a x[pCur->iPage],.
16ff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ffa 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b &pCur->info);
16ffb 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20 . }. aPayload
16ffc 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 = pCur->info.pCe
16ffd 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b ll;. aPayload +
16ffe 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 = pCur->info.nHe
16fff 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 ader;. if( pPag
17000 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
17001 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c nKey = 0;. }el
17002 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28 se{. nKey = (
17003 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e int)pCur->info.n
17004 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 Key;. }. if( s
17005 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 kipKey ){. aP
17006 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a ayload += nKey;.
17007 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 nLocal = pCu
17008 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d r->info.nLocal -
17009 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a nKey;. }else{.
1700a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 nLocal = pCu
1700b 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a r->info.nLocal;.
1700c 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63 assert( nLoc
1700d 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a al<=nKey );. }.
1700e 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c *pAmt = nLocal
1700f 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c ;. return aPayl
17010 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 oad;.}.../*.** F
17011 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 or the entry tha
17012 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 t cursor pCur is
17013 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 point to, retur
17014 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 n as.** many byt
17015 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 es of the key or
17016 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 data as are ava
17017 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f ilable on the lo
17018 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 cal.** b-tree pa
17019 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e ge. Write the n
1701a 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 umber of availab
1701b 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 le bytes into *p
1701c 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 Amt..**.** The p
1701d 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 ointer returned
1701e 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 is ephemeral. T
1701f 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 he key/data may
17020 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 move.** or be de
17021 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e stroyed on the n
17022 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 ext call to any
17023 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a Btree routine,.*
17024 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c * including call
17025 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 s from other thr
17026 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 eads against the
17027 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 same cache..**
17028 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f Hence, a mutex o
17029 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 n the BtShared s
1702a 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 hould be held pr
1702b 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a ior to calling.*
1702c 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a * this routine..
1702d 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
1702e 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 ines is used to
1702f 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 get quick access
17030 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 to key and data
17031 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f .** in the commo
17032 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 n case where no
17033 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 overflow pages a
17034 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 re used..*/.SQLI
17035 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
17036 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
17037 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 reeKeyFetch(BtCu
17038 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
17039 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 *pAmt){. const
1703a 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 void *p = 0;. a
1703b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1703c 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e utex_held(pCur->
1703d 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 pBtree->db->mute
1703e 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1703f 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
17040 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 (pCur) );. if(
17041 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 ALWAYS(pCur->eSt
17042 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
17043 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 D) ){. p = (c
17044 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 onst void*)fetch
17045 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 Payload(pCur, pA
17046 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 mt, 0);. }. re
17047 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 turn p;.}.SQLITE
17048 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 _PRIVATE const v
17049 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 oid *sqlite3Btre
1704a 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 eDataFetch(BtCur
1704b 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
1704c 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 pAmt){. const v
1704d 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 oid *p = 0;. as
1704e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1704f 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
17050 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
17051 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 ) );. assert( c
17052 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
17053 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 pCur) );. if( A
17054 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 LWAYS(pCur->eSta
17055 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
17056 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f ) ){. p = (co
17057 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 nst void*)fetchP
17058 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d ayload(pCur, pAm
17059 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 t, 1);. }. ret
1705a 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn p;.}.../*.**
1705b 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
1705c 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 down to a new c
1705d 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 hild page. The
1705e 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 newPgno argument
1705f 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 is the.** page
17060 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 number of the ch
17061 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 ild page to move
17062 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 to..**.** This
17063 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
17064 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
17065 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 if the page-head
17066 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f er flags field o
17067 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 f.** the new chi
17068 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 ld page does not
17069 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 match the flags
1706a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 field of the pa
1706b 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 rent (i.e..** if
1706c 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 an intkey page
1706d 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 appears to be th
1706e 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f e parent of a no
1706f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f n-intkey page, o
17070 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 r.** vice-versa)
17071 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
17072 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 moveToChild(BtCu
17073 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 rsor *pCur, u32
17074 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 newPgno){. int
17075 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 rc;. int i = pC
17076 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d ur->iPage;. Mem
17077 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a Page *pNewPage;.
17078 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
17079 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 = pCur->pBt;..
1707a 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1707b 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1707c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
1707d 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1707e 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
1707f 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c rt( pCur->iPage<
17080 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 BTCURSOR_MAX_DEP
17081 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 TH );. if( pCur
17082 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 ->iPage>=(BTCURS
17083 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 OR_MAX_DEPTH-1)
17084 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17085 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
17086 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 T;. }. rc = ge
17087 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 tAndInitPage(pBt
17088 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 , newPgno, &pNew
17089 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 Page);. if( rc
1708a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 ) return rc;. p
1708b 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d Cur->apPage[i+1]
1708c 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 = pNewPage;. p
1708d 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 Cur->aiIdx[i+1]
1708e 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 = 0;. pCur->iPa
1708f 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 ge++;.. pCur->i
17090 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
17091 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
17092 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 = 0;. if( pNew
17093 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c Page->nCell<1 ||
17094 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 pNewPage->intKe
17095 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b y!=pCur->apPage[
17096 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 i]->intKey ){.
17097 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17098 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
17099 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1709a 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 E_OK;.}..#ifndef
1709b 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 NDEBUG./*.** Pa
1709c 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e ge pParent is an
1709d 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c internal (non-l
1709e 65 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20 eaf) tree page.
1709f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a This function .*
170a0 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70 * asserts that p
170a1 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c age number iChil
170a2 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68 d is the left-ch
170a3 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27 ild if the iIdx'
170a4 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 th.** cell in pa
170a5 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 ge pParent. Or,
170a6 69 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c if iIdx is equal
170a7 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 to the total nu
170a8 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 mber of.** cells
170a9 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61 in pParent, tha
170aa 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 t page number iC
170ab 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68 hild is the righ
170ac 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 t-child of.** th
170ad 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 e page..*/.stati
170ae 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72 c void assertPar
170af 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 entIndex(MemPage
170b0 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 *pParent, int i
170b1 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 Idx, Pgno iChild
170b2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 ){. assert( iId
170b3 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c x<=pParent->nCel
170b4 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d l );. if( iIdx=
170b5 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 =pParent->nCell
170b6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 ){. assert( g
170b7 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 et4byte(&pParent
170b8 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
170b9 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d >hdrOffset+8])==
170ba 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 iChild );. }els
170bb 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 e{. assert( g
170bc 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
170bd 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29 (pParent, iIdx))
170be 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a ==iChild );. }.
170bf 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e }.#else.# defin
170c0 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e e assertParentIn
170c1 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 dex(x,y,z) .#end
170c2 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 if../*.** Move t
170c3 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20 he cursor up to
170c4 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
170c5 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 .**.** pCur->idx
170c6 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 is set to the c
170c7 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63 ell index that c
170c8 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e ontains the poin
170c9 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 ter.** to the pa
170ca 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 ge we are coming
170cb 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 from. If we ar
170cc 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 e coming from th
170cd 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 e.** right-most
170ce 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20 child page then
170cf 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 pCur->idx is set
170d0 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 to one more tha
170d1 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 n.** the largest
170d2 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a cell index..*/.
170d3 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65 static void move
170d4 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f ToParent(BtCurso
170d5 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 r *pCur){. asse
170d6 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
170d7 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
170d8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
170d9 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
170da 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ID );. assert(
170db 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b pCur->iPage>0 );
170dc 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
170dd 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
170de 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 age] );. assert
170df 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 ParentIndex(.
170e0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
170e1 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 ur->iPage-1], .
170e2 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
170e3 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a Cur->iPage-1], .
170e4 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 pCur->apPage
170e5 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 [pCur->iPage]->p
170e6 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61 gno. );. relea
170e7 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 sePage(pCur->apP
170e8 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
170e9 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65 );. pCur->iPage
170ea 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f --;. pCur->info
170eb 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 .nSize = 0;. pC
170ec 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
170ed 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 0;.}../*.** Move
170ee 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 70 the cursor to p
170ef 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 oint to the root
170f0 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d 74 page of its b-t
170f1 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ree structure..*
170f2 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c *.** If the tabl
170f3 65 20 68 61 73 20 61 20 76 69 72 74 75 61 6c 20 e has a virtual
170f4 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 root page, then
170f5 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f the cursor is mo
170f6 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 ved to point.**
170f7 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 to the virtual r
170f8 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61 64 oot page instead
170f9 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20 72 of the actual r
170fa 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62 6c oot page. A tabl
170fb 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74 75 e has a.** virtu
170fc 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68 65 al root page whe
170fd 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f 6f n the actual roo
170fe 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 t page contains
170ff 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20 0a no cells and a .
17100 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64 20 ** single child
17101 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20 6f page. This can o
17102 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68 20 nly happen with
17103 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 the table rooted
17104 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a 2a at page 1..**.*
17105 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 * If the b-tree
17106 73 74 72 75 63 74 75 72 65 20 69 73 20 65 6d 70 structure is emp
17107 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20 73 ty, the cursor s
17108 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 0a tate is set to .
17109 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 ** CURSOR_INVALI
1710a 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 D. Otherwise, th
1710b 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 74 20 e cursor is set
1710c 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
1710d 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c 6f first.** cell lo
1710e 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f cated on the roo
1710f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72 6f t (or virtual ro
17110 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 68 65 ot) page and the
17111 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a 2a cursor state.**
17112 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f is set to CURSO
17113 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 49 R_VALID..**.** I
17114 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 f this function
17115 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 66 returns successf
17116 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 20 ully, it may be
17117 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
17118 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 20 .** page-header
17119 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 74 flags indicate t
1711a 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 6c hat the [virtual
1711b 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 74 ] root-page is t
1711c 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20 he expected .**
1711d 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 70 kind of b-tree p
1711e 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 65 age (i.e. if whe
1711f 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 75 n opening the cu
17120 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 20 rsor the caller
17121 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 69 did not.** speci
17122 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 fy a KeyInfo str
17123 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 ucture the flags
17124 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 byte is set to
17125 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 2a 0x05 or 0x0D,.**
17126 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61 indicating a ta
17127 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69 ble b-tree, or i
17128 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64 f the caller did
17129 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e specify a KeyIn
1712a 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 fo .** structure
1712b 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
1712c 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f is set to 0x02 o
1712d 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69 r 0x0A, indicati
1712e 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 62 ng an index.** b
1712f 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 -tree)..*/.stati
17130 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 c int moveToRoot
17131 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
17132 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f {. MemPage *pRo
17133 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ot;. int rc = S
17134 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 QLITE_OK;. Btre
17135 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 e *p = pCur->pBt
17136 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ree;. BtShared
17137 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
17138 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
17139 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
1713a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 );. assert( CU
1713b 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 RSOR_INVALID < C
1713c 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
1713d 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 K );. assert( C
1713e 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 URSOR_VALID <
1713f 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
17140 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 EK );. assert(
17141 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e CURSOR_FAULT >
17142 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
17143 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 EEK );. if( pCu
17144 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f r->eState>=CURSO
17145 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b R_REQUIRESEEK ){
17146 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 . if( pCur->e
17147 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 State==CURSOR_FA
17148 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ULT ){. ass
17149 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e ert( pCur->skipN
1714a 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 ext!=SQLITE_OK )
1714b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 ;. return p
1714c 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 Cur->skipNext;.
1714d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1714e 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
1714f 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 (pCur);. }.. i
17150 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d f( pCur->iPage>=
17151 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0 ){. int i;.
17152 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d for(i=1; i<=
17153 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b pCur->iPage; i++
17154 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
17155 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
17156 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 e[i]);. }.
17157 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 pCur->iPage = 0
17158 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
17159 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 c = getAndInitPa
1715a 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 ge(pBt, pCur->pg
1715b 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 noRoot, &pCur->a
1715c 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69 pPage[0]);. i
1715d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1715e 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e ){. pCur->
1715f 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
17160 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 INVALID;. r
17161 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
17162 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 pCur->iPage
17163 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 = 0;.. /* If
17164 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 pCur->pKeyInfo i
17165 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
17166 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 the caller that
17167 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 opened this cur
17168 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63 sor. ** expec
17169 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f ted to open it o
1716a 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 n an index b-tre
1716b 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 e. Otherwise, if
1716c 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20 pKeyInfo is.
1716d 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 ** NULL, the ca
1716e 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 74 ller expects a t
1716f 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 able b-tree. If
17170 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 this is not the
17171 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74 case,. ** ret
17172 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f urn an SQLITE_CO
17173 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f RRUPT error. */
17174 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
17175 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
17176 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d tKey==1 || pCur-
17177 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
17178 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ey==0 );. if(
17179 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f (pCur->pKeyInfo
1717a 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61 ==0)!=pCur->apPa
1717b 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b ge[0]->intKey ){
1717c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1717d 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1717e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 T;. }. }..
1717f 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 /* Assert that t
17180 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 he root page is
17181 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 of the correct t
17182 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62 ype. This must b
17183 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 e the. ** case
17184 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 as the call to t
17185 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 his function tha
17186 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f t loaded the roo
17187 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20 t-page (either.
17188 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72 ** this call or
17189 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f a previous invo
1718a 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61 cation) would ha
1718b 76 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 ve detected corr
1718c 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20 uption . ** if
1718d 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 the assumption w
1718e 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e ere not true, an
1718f 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 d it is not poss
17190 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61 ible for the fla
17191 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f gs . ** byte to
17192 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
17193 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 ied while this c
17194 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 ursor is holding
17195 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a a reference. *
17196 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 * to the page.
17197 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 */. pRoot = pCu
17198 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 r->apPage[0];.
17199 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 assert( pRoot->p
1719a 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 gno==pCur->pgnoR
1719b 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 oot );. assert(
1719c 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26 pRoot->isInit &
1719d 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 & (pCur->pKeyInf
1719e 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e o==0)==pRoot->in
1719f 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d tKey );.. pCur-
171a0 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 >aiIdx[0] = 0;.
171a1 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
171a2 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 e = 0;. pCur->a
171a3 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 tLast = 0;. pCu
171a4 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
171a5 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e ;.. if( pRoot->
171a6 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f nCell==0 && !pRo
171a7 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ot->leaf ){.
171a8 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 Pgno subpage;.
171a9 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e if( pRoot->pgn
171aa 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 o!=1 ) return SQ
171ab 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
171ac 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d T;. subpage =
171ad 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 get4byte(&pRoot
171ae 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 ->aData[pRoot->h
171af 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
171b0 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
171b1 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 CURSOR_VALID;.
171b2 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
171b3 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 ild(pCur, subpag
171b4 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e);. }else{.
171b5 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
171b6 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 ((pRoot->nCell>0
171b7 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 )?CURSOR_VALID:C
171b8 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a URSOR_INVALID);.
171b9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
171ba 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 .}../*.** Move t
171bb 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
171bc 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 o the left-most
171bd 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 leaf entry benea
171be 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 th the.** entry
171bf 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 to which it is c
171c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
171c1 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 g..**.** The lef
171c2 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 t-most leaf is t
171c3 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 he one with the
171c4 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 smallest key - t
171c5 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 he first.** in a
171c6 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a scending order..
171c7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
171c8 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 veToLeftmost(BtC
171c9 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
171ca 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 Pgno pgno;. int
171cb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
171cc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
171cd 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 e;.. assert( cu
171ce 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
171cf 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
171d0 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
171d1 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
171d2 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
171d3 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 ITE_OK && !(pPag
171d4 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
171d5 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e [pCur->iPage])->
171d6 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 leaf ){. asse
171d7 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
171d8 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 pCur->iPage]<pPa
171d9 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 ge->nCell );.
171da 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pgno = get4byte
171db 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c (findCell(pPage,
171dc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
171dd 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 r->iPage]));.
171de 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
171df 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 d(pCur, pgno);.
171e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
171e1 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 }../*.** Move th
171e2 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f e cursor down to
171e3 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 the right-most
171e4 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 leaf entry benea
171e5 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 th the.** page t
171e6 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 o which it is cu
171e7 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
171e8 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 . Notice the di
171e9 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 fference.** betw
171ea 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f een moveToLeftmo
171eb 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 st() and moveToR
171ec 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 ightmost(). mov
171ed 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a eToLeftmost().**
171ee 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d finds the left-
171ef 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 most entry benea
171f0 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 th the *entry* w
171f1 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 hereas moveToRig
171f2 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 htmost().** find
171f3 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 s the right-most
171f4 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 entry beneath t
171f5 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a he *page*..**.**
171f6 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 The right-most
171f7 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 entry is the one
171f8 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 with the larges
171f9 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 t key - the last
171fa 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e .** key in ascen
171fb 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 ding order..*/.s
171fc 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
171fd 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 Rightmost(BtCurs
171fe 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e or *pCur){. Pgn
171ff 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 o pgno;. int rc
17200 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
17201 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
17202 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 0;.. assert( c
17203 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
17204 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
17205 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
17206 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
17207 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 . while( rc==SQ
17208 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 LITE_OK && !(pPa
17209 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1720a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d e[pCur->iPage])-
1720b 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e >leaf ){. pgn
1720c 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 o = get4byte(&pP
1720d 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
1720e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
1720f 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
17210 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
17211 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
17212 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
17213 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a ld(pCur, pgno);.
17214 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
17215 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
17216 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
17217 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d >iPage] = pPage-
17218 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 >nCell-1;. pC
17219 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
1721a 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 0;. pCur->va
1721b 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d lidNKey = 0;. }
1721c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1721d 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 ./* Move the cur
1721e 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 sor to the first
1721f 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
17220 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ble. Return SQL
17221 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 ITE_OK.** on suc
17222 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 cess. Set *pRes
17223 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 to 0 if the cur
17224 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 sor actually poi
17225 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 nts to something
17226 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 .** or set *pRes
17227 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 to 1 if the tab
17228 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a le is empty..*/.
17229 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1722a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 nt sqlite3BtreeF
1722b 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 irst(BtCursor *p
1722c 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
1722d 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
1722e 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
1722f 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
17230 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17231 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
17232 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
17233 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 utex) );. rc =
17234 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 moveToRoot(pCur)
17235 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
17236 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
17237 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
17238 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
17239 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1723a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
1723b 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d ->iPage]->nCell=
1723c 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 =0 );. *pRe
1723d 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 s = 1;. rc
1723e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1723f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
17240 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
17241 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
17242 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 >nCell>0 );.
17243 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
17244 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 rc = moveToLe
17245 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 ftmost(pCur);.
17246 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
17247 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 rc;.}../* Move
17248 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
17249 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 e last entry in
1724a 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 the table. Retu
1724b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 rn SQLITE_OK.**
1724c 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 on success. Set
1724d 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 *pRes to 0 if t
1724e 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c he cursor actual
1724f 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d ly points to som
17250 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 ething.** or set
17251 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 *pRes to 1 if t
17252 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
17253 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
17254 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17255 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 BtreeLast(BtCurs
17256 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
17257 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
17258 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 . assert( curs
17259 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1725a 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1725b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1725c 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d ld(pCur->pBtree-
1725d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
1725e 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 /* If the curs
1725f 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 or already point
17260 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e s to the last en
17261 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e try, this is a n
17262 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 o-op. */. if( C
17263 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 URSOR_VALID==pCu
17264 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43 75 r->eState && pCu
17265 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 r->atLast ){.#if
17266 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
17267 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f . /* This blo
17268 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73 73 ck serves to ass
17269 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20 63 ert() that the c
1726a 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 ursor really doe
1726b 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 s point . **
1726c 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 to the last entr
1726d 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e y in the b-tree.
1726e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a */. int ii;.
1726f 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 for(ii=0; ii
17270 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 <pCur->iPage; ii
17271 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
17272 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 t( pCur->aiIdx[i
17273 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 i]==pCur->apPage
17274 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 [ii]->nCell );.
17275 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
17276 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
17277 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d r->iPage]==pCur-
17278 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
17279 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b age]->nCell-1 );
1727a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
1727b 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
1727c 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a iPage]->leaf );.
1727d 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 #endif. retur
1727e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1727f 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 .. rc = moveToR
17280 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 oot(pCur);. if(
17281 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
17282 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 {. if( CURSOR
17283 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e _INVALID==pCur->
17284 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 eState ){.
17285 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
17286 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
17287 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 ]->nCell==0 );.
17288 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a *pRes = 1;.
17289 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1728a 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
1728b 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
1728c 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 LID );. *pR
1728d 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 es = 0;. rc
1728e 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f = moveToRightmo
1728f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 st(pCur);.
17290 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72 pCur->atLast = r
17291 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a c==SQLITE_OK ?1:
17292 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
17293 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
17294 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
17295 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
17296 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65 s to an entry ne
17297 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73 ar the key .** s
17298 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78 pecified by pIdx
17299 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20 Key or intKey.
1729a 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 Return a succes
1729b 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f s code..**.** Fo
1729c 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c r INTKEY tables,
1729d 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61 the intKey para
1729e 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 meter is used.
1729f 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 pIdxKey .** must
172a0 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 be NULL. For i
172a1 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64 ndex tables, pId
172a2 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64 xKey is used and
172a3 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67 intKey.** is ig
172a4 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 nored..**.** If
172a5 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69 an exact match i
172a6 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65 s not found, the
172a7 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 n the cursor is
172a8 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70 always.** left p
172a9 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61 ointing at a lea
172aa 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75 f page which wou
172ab 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72 ld hold the entr
172ac 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20 y if it.** were
172ad 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75 present. The cu
172ae 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74 rsor might point
172af 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61 to an entry tha
172b0 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 t comes.** befor
172b1 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b e or after the k
172b2 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 ey..**.** An int
172b3 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 eger is written
172b4 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68 into *pRes which
172b5 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f is the result o
172b6 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74 f.** comparing t
172b7 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20 he key with the
172b8 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 entry to which t
172b9 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a he cursor is .**
172ba 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20 pointing. The
172bb 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69 meaning of the i
172bc 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69 nteger written i
172bd 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20 nto.** *pRes is
172be 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
172bf 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20 * *pRes<0
172c0 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 The cursor is
172c1 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
172c2 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a t an entry that.
172c3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
172c4 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 is smaller t
172c5 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
172c6 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 ey or if the tab
172c7 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 le is empty.**
172c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
172c9 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 and the cursor i
172ca 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 s therefore left
172cb 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e point to nothin
172cc 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 g..**.** *pR
172cd 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 es==0 The cu
172ce 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 rsor is left poi
172cf 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 nting at an entr
172d0 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 y that.**
172d1 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74 exact
172d2 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65 ly matches intKe
172d3 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a y/pIdxKey..**.**
172d4 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20 *pRes>0
172d5 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
172d6 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
172d7 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a an entry that.*
172d8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
172d9 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 is larger tha
172da 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 n intKey/pIdxKey
172db 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ..**.*/.SQLITE_P
172dc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
172dd 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
172de 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f acked(. BtCurso
172df 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 r *pCur,
172e0 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
172e1 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 to be moved */.
172e2 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
172e3 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 *pIdxKey, /* Unp
172e4 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 acked index key
172e5 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c */. i64 intKey,
172e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
172e7 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a The table key *
172e8 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68 /. int biasRigh
172e9 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t, /*
172ea 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 If true, bias th
172eb 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 e search to the
172ec 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e high end */. in
172ed 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 t *pRes
172ee 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
172ef 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 search results h
172f0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
172f1 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 rc;.. assert( c
172f2 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
172f3 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
172f4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
172f5 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
172f6 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
172f7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73 ;. assert( pRes
172f8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
172f9 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 IdxKey==0)==(pCu
172fa 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 r->pKeyInfo==0)
172fb 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
172fc 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 cursor is alread
172fd 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20 y positioned at
172fe 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65 the point we are
172ff 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 trying. ** to
17300 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75 move to, then ju
17301 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 st return withou
17302 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b t doing any work
17303 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e */. if( pCur->
17304 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
17305 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61 ALID && pCur->va
17306 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70 lidNKey . && p
17307 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
17308 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20 intKey . ){.
17309 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e if( pCur->info.
1730a 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a nKey==intKey ){.
1730b 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
1730c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1730d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
1730e 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c if( pCur->atL
1730f 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66 ast && pCur->inf
17310 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b o.nKey<intKey ){
17311 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d . *pRes = -
17312 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 1;. return
17313 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
17314 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 . }.. rc = mov
17315 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 eToRoot(pCur);.
17316 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 if( rc ){. r
17317 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
17318 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
17319 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1731a 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ] );. assert( p
1731b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
1731c 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74 ->iPage]->isInit
1731d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
1731e 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1731f 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 >iPage]->nCell>0
17320 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
17321 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
17322 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e );. if( pCur->
17323 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
17324 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 NVALID ){. *p
17325 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 Res = -1;. as
17326 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
17327 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
17328 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 >nCell==0 );.
17329 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1732a 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 K;. }. assert(
1732b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
1732c 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78 ->intKey || pIdx
1732d 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 Key );. for(;;)
1732e 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75 {. int lwr, u
1732f 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c pr;. Pgno chl
17330 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 dPg;. MemPage
17331 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e *pPage = pCur->
17332 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17333 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b 0a ge];. int c;.
17334 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 6e . /* pPage->n
17335 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72 65 Cell must be gre
17336 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 ater than zero.
17337 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72 If this is the r
17338 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 oot-page. **
17339 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 the cursor would
1733a 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41 4c have been INVAL
1733b 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68 69 ID above and thi
1733c 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a 20 s for(;;) loop.
1733d 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 49 ** not run. I
1733e 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 f this is not th
1733f 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 65 e root-page, the
17340 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 6c n the moveToChil
17341 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 d() routine.
17342 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 6c ** would have al
17343 72 65 61 64 79 20 64 65 74 65 63 74 65 64 20 64 ready detected d
17344 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 69 b corruption. Si
17345 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 6d milarly, pPage m
17346 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 68 ust. ** be th
17347 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 6e e right kind (in
17348 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f 66 dex or table) of
17349 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f 74 b-tree page. Ot
1734a 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 61 herwise. ** a
1734b 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 6f moveToChild() o
1734c 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 63 r moveToRoot() c
1734d 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20 64 all would have d
1734e 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 69 etected corrupti
1734f 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 on. */. asse
17350 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
17351 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
17352 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d ( pPage->intKey=
17353 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29 3b =(pIdxKey==0) );
17354 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 . lwr = 0;.
17355 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e upr = pPage->n
17356 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 Cell-1;. if(
17357 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20 20 biasRight ){.
17358 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
17359 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 Cur->iPage] = (u
1735a 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 16)upr;. }els
1735b 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 e{. pCur->a
1735c 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1735d 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b 6c ] = (u16)((upr+l
1735e 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 wr)/2);. }.
1735f 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 for(;;){.
17360 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
17361 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17362 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 ge]; /* Index of
17363 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e current cell in
17364 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 pPage */.
17365 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 u8 *pCell;
17366 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17367 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
17368 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 o current cell i
17369 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20 n pPage */..
1736a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
1736b 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 ze = 0;. pC
1736c 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
1736d 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50 61 Page, idx) + pPa
1736e 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
1736f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 ;. if( pPag
17370 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
17371 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 i64 nCellKe
17372 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 y;. if( p
17373 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b Page->hasData ){
17374 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 64 . u32 d
17375 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20 20 ummy;.
17376 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72 69 pCell += getVari
17377 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d 6d nt32(pCell, dumm
17378 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 y);. }.
17379 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28 getVarint(
1737a 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43 pCell, (u64*)&nC
1737b 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 ellKey);.
1737c 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d 69 if( nCellKey==i
1737d 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 ntKey ){.
1737e 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 c = 0;.
1737f 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c }else if( nCel
17380 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 lKey<intKey ){.
17381 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 3b c = -1;
17382 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
17383 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
17384 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b 65 ( nCellKey>intKe
17385 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 y );. c
17386 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d = +1;. }
17387 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 76 . pCur->v
17388 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 alidNKey = 1;.
17389 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
1738a 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 .nKey = nCellKey
1738b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1738c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 /* The ma
1738d 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 ximum supported
1738e 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32 37 page-size is 327
1738f 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d 68 bytes. This m
17390 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20 eans that.
17391 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d ** the maximum
17392 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 number of recor
17393 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f d bytes stored o
17394 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65 n an index B-Tre
17395 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 e. ** pag
17396 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31 39 e is at most 819
17397 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 20 6d 8 bytes, which m
17398 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 ay be stored as
17399 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20 a 2-byte.
1739a 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73 ** varint. This
1739b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
1739c 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 used to attempt
1739d 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67 to avoid parsing
1739e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 . ** the
1739f 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20 entire cell by
173a0 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 checking for the
173a1 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65 cases where the
173a2 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20 record is .
173a3 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e ** stored en
173a4 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68 tirely within th
173a5 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79 e b-tree page by
173a6 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 inspecting the
173a7 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a first . *
173a8 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65 * 2 bytes of the
173a9 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a cell.. *
173aa 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43 /. int nC
173ab 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b 0a ell = pCell[0];.
173ac 20 20 20 20 20 20 20 20 69 66 28 20 21 28 6e 43 if( !(nC
173ad 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20 6e ell & 0x80) && n
173ae 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 Cell<=pPage->max
173af 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 Local ){.
173b0 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
173b1 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 h runs if the re
173b2 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 cord-size field
173b3 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 of the cell is a
173b4 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 . ** si
173b5 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 ngle byte varint
173b6 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 and the record
173b7 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e fits entirely on
173b8 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 the main.
173b9 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 ** b-tree pa
173ba 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
173bb 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 c = sqlite3Vdb
173bc 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e eRecordCompare(n
173bd 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 Cell, (void*)&pC
173be 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79 29 ell[1], pIdxKey)
173bf 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
173c0 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 26 if( !(pCell[1] &
173c1 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 20 20 0x80) .
173c2 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 28 && (nCell = ((
173c3 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 20 nCell&0x7f)<<7)
173c4 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61 + pCell[1])<=pPa
173c5 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 20 ge->maxLocal.
173c6 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
173c7 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 2d /* The record-
173c8 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 20 size field is a
173c9 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e 2 byte varint an
173ca 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20 20 d the record .
173cb 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73 20 ** fits
173cc 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 entirely on the
173cd 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65 main b-tree page
173ce 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 . */.
173cf 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 c = sqlite3VdbeR
173d0 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 ecordCompare(nCe
173d1 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c ll, (void*)&pCel
173d2 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a l[2], pIdxKey);.
173d3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
173d4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
173d5 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 record flows ove
173d6 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f r onto one or mo
173d7 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 re overflow page
173d8 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20 s. In.
173d9 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65 ** this case the
173da 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 whole cell need
173db 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20 s to be parsed,
173dc 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 a buffer allocat
173dd 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ed. **
173de 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61 and accessPayloa
173df 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72 d() used to retr
173e0 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 ieve the record
173e1 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 into the.
173e2 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66 ** buffer bef
173e3 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f ore VdbeRecordCo
173e4 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63 mpare() can be c
173e5 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 alled. */.
173e6 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b void *pCellK
173e7 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 ey;. u8
173e8 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f * const pCellBo
173e9 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 dy = pCell - pPa
173ea 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
173eb 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65 ;. btre
173ec 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
173ed 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 age, pCellBody,
173ee 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 &pCur->info);.
173ef 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 nCell =
173f0 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e (int)pCur->info.
173f1 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 nKey;.
173f2 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 pCellKey = sqlit
173f3 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 e3Malloc( nCell
173f4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
173f5 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a pCellKey==0 ){.
173f6 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
173f7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
173f8 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
173f9 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
173fa 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
173fb 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 rc = access
173fc 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c Payload(pCur, 0,
173fd 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 nCell, (unsigne
173fe 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79 d char*)pCellKey
173ff 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
17400 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
17401 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
17402 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 ee(pCellKey);.
17403 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
17404 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
17405 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17406 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
17407 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
17408 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 (nCell, pCellKey
17409 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 , pIdxKey);.
1740a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
1740b 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 ee(pCellKey);.
1740c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1740d 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 if( c==0 )
1740e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
1740f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 age->intKey && !
17410 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
17411 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 lwr = i
17412 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70 dx;. up
17413 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20 r = lwr - 1;.
17414 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
17415 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
17416 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 *pRes = 0
17417 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
17418 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
17419 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 goto movet
1741a 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 o_finish;.
1741b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1741c 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 if( c<0 ){.
1741d 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 lwr = idx+1
1741e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1741f 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 upr = idx
17420 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 -1;. }.
17421 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b if( lwr>upr ){
17422 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
17423 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
17424 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
17425 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 iPage] = (u16)((
17426 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 lwr+upr)/2);.
17427 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c }. assert( l
17428 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 wr==upr+1 );.
17429 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1742a 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 isInit );. if
1742b 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
1742c 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 . chldPg =
1742d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1742e 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 lwr>=pPage->nCe
1742f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 ll ){. chld
17430 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 Pg = get4byte(&p
17431 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
17432 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
17433 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
17434 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 chldPg = get4
17435 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
17436 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 age, lwr));.
17437 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 }. if( chldPg
17438 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
17439 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
1743a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
1743b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1743c 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
1743d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
1743e 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 c;. rc = SQ
1743f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 LITE_OK;. g
17440 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 oto moveto_finis
17441 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 h;. }. pCu
17442 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
17443 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72 Page] = (u16)lwr
17444 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f ;. pCur->info
17445 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
17446 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
17447 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f = 0;. rc = mo
17448 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
17449 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 chldPg);. if(
1744a 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74 rc ) goto movet
1744b 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f o_finish;. }.mo
1744c 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72 veto_finish:. r
1744d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
1744e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
1744f 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 if the cursor is
17450 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 not pointing at
17451 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 an entry of the
17452 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 table..**.** TR
17453 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 UE will be retur
17454 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c ned after a call
17455 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
17456 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 Next() moves.**
17457 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e past the last en
17458 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 try in the table
17459 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 or sqlite3Btree
1745a 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 Prev() moves pas
1745b 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 t.** the first e
1745c 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 ntry. TRUE is a
1745d 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 lso returned if
1745e 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
1745f 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ty..*/.SQLITE_PR
17460 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
17461 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 3BtreeEof(BtCurs
17462 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 or *pCur){. /*
17463 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 TODO: What if th
17464 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 e cursor is in C
17465 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
17466 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 K but all table
17467 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 entries. ** hav
17468 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 e been deleted?
17469 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 This API will ne
1746a 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 ed to change to
1746b 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1746c 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c code. ** as wel
1746d 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e l as the boolean
1746e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 result value..
1746f 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 */. return (CU
17470 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 RSOR_VALID!=pCur
17471 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a ->eState);.}../*
17472 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 .** Advance the
17473 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 cursor to the ne
17474 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 xt entry in the
17475 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a database. If.**
17476 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e successful then
17477 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 set *pRes=0. I
17478 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 f the cursor.**
17479 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e was already poin
1747a 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 ting to the last
1747b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1747c 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a tabase before.**
1747d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
1747e 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 s called, then s
1747f 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 et *pRes=1..*/.S
17480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17481 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 t sqlite3BtreeNe
17482 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 xt(BtCursor *pCu
17483 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
17484 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 int rc;. int i
17485 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 dx;. MemPage *p
17486 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
17487 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17488 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 x(pCur) );. rc
17489 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
1748a 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 osition(pCur);.
1748b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1748c 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1748d 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
1748e 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 t( pRes!=0 );.
1748f 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c if( CURSOR_INVAL
17490 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
17491 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
17492 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
17493 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
17494 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 f( pCur->skipNex
17495 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d t>0 ){. pCur-
17496 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 >skipNext = 0;.
17497 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
17498 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17499 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e OK;. }. pCur->
1749a 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 skipNext = 0;..
1749b 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
1749c 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1749d 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 e];. idx = ++pC
1749e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1749f 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
174a0 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
174a1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 );. assert( idx
174a2 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 <=pPage->nCell )
174a3 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ;.. pCur->info.
174a4 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 nSize = 0;. pCu
174a5 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 r->validNKey = 0
174a6 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 ;. if( idx>=pPa
174a7 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 ge->nCell ){.
174a8 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
174a9 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 f ){. rc =
174aa 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
174ab 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 , get4byte(&pPag
174ac 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
174ad 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a hdrOffset+8]));.
174ae 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
174af 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
174b0 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d rc = moveToLeftm
174b1 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 ost(pCur);.
174b2 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
174b3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
174b4 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 }. do{.
174b5 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
174b6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a ==0 ){. *
174b7 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 pRes = 1;.
174b8 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
174b9 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
174ba 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
174bb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
174bc 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 }. moveToP
174bd 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
174be 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
174bf 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
174c0 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 age];. }while
174c1 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
174c2 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 ur->iPage]>=pPag
174c3 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 e->nCell );.
174c4 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 *pRes = 0;. i
174c5 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
174c6 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
174c7 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 qlite3BtreeNext(
174c8 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 pCur, pRes);.
174c9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
174ca 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
174cb 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 }. return r
174cc 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d c;. }. *pRes =
174cd 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 0;. if( pPage-
174ce 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 74 >leaf ){. ret
174cf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
174d0 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f }. rc = moveTo
174d1 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a Leftmost(pCur);.
174d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
174d3 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 20 ./*.** Step the
174d4 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61 cursor to the ba
174d5 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f ck to the previo
174d6 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 us entry in the
174d7 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a database. If.**
174d8 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e successful then
174d9 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 set *pRes=0. I
174da 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 f the cursor.**
174db 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e was already poin
174dc 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72 73 ting to the firs
174dd 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 t entry in the d
174de 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a atabase before.*
174df 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * this routine w
174e0 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 as called, then
174e1 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a set *pRes=1..*/.
174e2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
174e3 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
174e4 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 revious(BtCursor
174e5 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
174e6 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
174e7 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
174e8 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
174e9 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
174ea 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 ) );. rc = rest
174eb 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
174ec 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
174ed 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
174ee 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
174ef 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 }. pCur->atLas
174f0 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55 52 t = 0;. if( CUR
174f1 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 SOR_INVALID==pCu
174f2 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 r->eState ){.
174f3 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 *pRes = 1;.
174f4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
174f5 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 ;. }. if( pCur
174f6 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a ->skipNext<0 ){.
174f7 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 pCur->skipNe
174f8 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 xt = 0;. *pRe
174f9 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 s = 0;. retur
174fa 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
174fb 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 . pCur->skipNex
174fc 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 t = 0;.. pPage
174fd 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
174fe 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 Cur->iPage];. a
174ff 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 ssert( pPage->is
17500 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 Init );. if( !p
17501 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
17502 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 int idx = pCur
17503 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
17504 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d age];. rc = m
17505 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
17506 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
17507 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 ll(pPage, idx)))
17508 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
17509 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1750a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1750b 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
1750c 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pCur);. }else{.
1750d 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d while( pCur-
1750e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1750f 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ge]==0 ){.
17510 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
17511 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 =0 ){. pC
17512 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
17513 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 SOR_INVALID;.
17514 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a *pRes = 1;.
17515 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
17516 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
17517 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 }. moveToPa
17518 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 rent(pCur);.
17519 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f }. pCur->info
1751a 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
1751b 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1751c 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e = 0;.. pCur->
1751d 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1751e 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 20 e]--;. pPage
1751f 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
17520 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 Cur->iPage];.
17521 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
17522 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 ey && !pPage->le
17523 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d af ){. rc =
17524 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 sqlite3BtreePre
17525 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73 vious(pCur, pRes
17526 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
17527 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
17528 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 OK;. }. }.
17529 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 74 *pRes = 0;. ret
1752a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1752b 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
1752c 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61 page from the da
1752d 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1752e 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 ** The new page
1752f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 is marked as dir
17530 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 ty. (In other w
17531 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 67 ords, sqlite3Pag
17532 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 73 erWrite().** has
17533 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 already been ca
17534 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20 lled on the new
17535 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20 page.) The new
17536 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a page has also.**
17537 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64 been referenced
17538 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67 and the calling
17539 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70 routine is resp
1753a 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c onsible for call
1753b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 ing.** sqlite3Pa
1753c 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68 gerUnref() on th
1753d 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20 e new page when
1753e 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a it is done..**.*
1753f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
17540 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 eturned on succe
17541 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 72 ss. Any other r
17542 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 eturn value indi
17543 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f cates.** an erro
17544 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 20 r. *ppPage and
17545 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 66 *pPgno are undef
17546 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e ined in the even
17547 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a t of an error..*
17548 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 * Do not invoke
17549 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1754a 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 69 f() on *ppPage i
1754b 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 f an error is re
1754c 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
1754d 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 61 the "nearby" pa
1754e 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 rameter is not 0
1754f 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c 65 , then a (feeble
17550 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 65 ) effort is made
17551 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61 to .** locate a
17552 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20 74 page close to t
17553 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 22 he page number "
17554 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 20 63 nearby". This c
17555 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e an be used in an
17556 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 6b .** attempt to k
17557 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 67 65 eep related page
17558 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68 20 s close to each
17559 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 61 74 other in the dat
1755a 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77 abase file,.** w
1755b 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61 6e hich in turn can
1755c 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 61 make database a
1755d 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a ccess faster..**
1755e 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61 63 .** If the "exac
1755f 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 t" parameter is
17560 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20 70 not 0, and the p
17561 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62 age-number nearb
17562 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79 y exists .** any
17563 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 where on the fre
17564 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 20 e-list, then it
17565 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 74 6f is guarenteed to
17566 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 be returned. Th
17567 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 is.** is only us
17568 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 ed by auto-vacuu
17569 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 65 6e m databases when
1756a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 allocating a ne
1756b 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 w table..*/.stat
1756c 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 ic int allocateB
1756d 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68 treePage(. BtSh
1756e 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65 ared *pBt, . Me
1756f 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 mPage **ppPage,
17570 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20 . Pgno *pPgno,
17571 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a . Pgno nearby,.
17572 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20 20 u8 exact.){.
17573 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
17574 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 . int rc;. u32
17575 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 n; /* Numbe
17576 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 r of pages on th
17577 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20 e freelist */.
17578 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75 u32 k; /* Nu
17579 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 6f mber of leaves o
1757a 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 74 n the trunk of t
1757b 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 he freelist */.
1757c 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b MemPage *pTrunk
1757d 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 = 0;. MemPage
1757e 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b *pPrevTrunk = 0;
1757f 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b 20 . Pgno mxPage;
17580 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a /* Total siz
17581 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
17582 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 e file */.. ass
17583 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17584 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
17585 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 ex) );. pPage1
17586 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 = pBt->pPage1;.
17587 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72 50 mxPage = pagerP
17588 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
17589 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 n = get4byte(&p
1758a 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d Page1->aData[36]
1758b 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
1758c 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20 ==mxPage-1 );.
1758d 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b if( n>=mxPage ){
1758e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1758f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
17590 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29 . }. if( n>0 )
17591 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
17592 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 re pages on the
17593 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 freelist. Reuse
17594 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 one of those pa
17595 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ges. */. Pgno
17596 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 iTrunk;. u8
17597 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 searchList = 0;
17598 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c /* If the free-l
17599 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 ist must be sear
1759a 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 ched for 'nearby
1759b 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a ' */. . /*
1759c 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 If the 'exact'
1759d 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 parameter was tr
1759e 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f ue and a query o
1759f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 f the pointer-ma
175a0 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 p. ** shows t
175a1 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 hat the page 'ne
175a2 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 arby' is somewhe
175a3 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c re on the free-l
175a4 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a ist, then. **
175a5 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 the entire-list
175a6 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 will be searche
175a7 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e d for that page.
175a8 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 . */.#ifndef
175a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
175aa 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 VACUUM. if( e
175ab 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d xact && nearby<=
175ac 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 mxPage ){.
175ad 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 u8 eType;.
175ae 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 assert( nearby>0
175af 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
175b0 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
175b1 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 m );. rc =
175b2 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e ptrmapGet(pBt, n
175b3 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 earby, &eType, 0
175b4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
175b5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
175b6 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 if( eType==PT
175b7 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b RMAP_FREEPAGE ){
175b8 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c . searchL
175b9 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d ist = 1;. }
175ba 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 . *pPgno =
175bb 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 nearby;. }.#e
175bc 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 ndif.. /* Dec
175bd 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d rement the free-
175be 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e list count by 1.
175bf 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 Set iTrunk to t
175c0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a he index of the.
175c1 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 ** first fre
175c2 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 e-list trunk pag
175c3 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 e. iPrevTrunk is
175c4 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 initially 1..
175c5 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
175c6 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
175c7 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
175c8 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
175c9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 eturn rc;. pu
175ca 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
175cb 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b aData[36], n-1);
175cc 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 .. /* The cod
175cd 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f e within this lo
175ce 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f op is run only o
175cf 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 nce if the 'sear
175d0 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 chList' variable
175d1 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 . ** is not t
175d2 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 rue. Otherwise,
175d3 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 it runs once for
175d4 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 each trunk-page
175d5 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 on the. ** f
175d6 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 ree-list until t
175d7 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 he page 'nearby'
175d8 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 is located..
175d9 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 */. do {.
175da 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 pPrevTrunk =
175db 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 pTrunk;. if
175dc 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a ( pPrevTrunk ){.
175dd 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d iTrunk =
175de 20 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 get4byte(&pPrev
175df 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 Trunk->aData[0])
175e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
175e1 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 iTrunk =
175e2 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 get4byte(&pPage1
175e3 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 ->aData[32]);.
175e4 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 }. test
175e5 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d 78 case( iTrunk==mx
175e6 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69 66 Page );. if
175e7 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 ( iTrunk>mxPage
175e8 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
175e9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
175ea 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 KPT;. }else
175eb 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 {. rc = b
175ec 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
175ed 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b iTrunk, &pTrunk
175ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
175ef 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
175f0 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 pTrunk = 0
175f1 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 ;. goto e
175f2 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
175f3 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
175f4 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 k = get4byte(&p
175f5 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 Trunk->aData[4])
175f6 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 ;. if( k==0
175f7 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 && !searchList
175f8 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
175f9 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c e trunk has no l
175fa 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 eaves and the li
175fb 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 st is not being
175fc 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 searched. .
175fd 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 ** So extract
175fe 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 the trunk page
175ff 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 itself and use i
17600 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a t as the newly .
17601 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 ** alloc
17602 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 ated page */.
17603 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 assert( pPr
17604 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 evTrunk==0 );.
17605 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
17606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
17607 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
17608 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
17609 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1760a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
1760b 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
1760c 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 *pPgno = iT
1760d 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 runk;. me
1760e 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 mcpy(&pPage1->aD
1760f 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b ata[32], &pTrunk
17610 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a ->aData[0], 4);.
17611 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 *ppPage
17612 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = pTrunk;.
17613 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 pTrunk = 0;.
17614 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
17615 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b LOCATE: %d trunk
17616 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 - %d free pages
17617 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f left\n", *pPgno
17618 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d , n-1));. }
17619 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32 29 else if( k>(u32)
1761a 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
1761b 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 20 /4 - 2) ){.
1761c 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b /* Value of k
1761d 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
1761e 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 . Database corr
1761f 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uption */.
17620 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
17621 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
17622 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
17623 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e ocate_page;.#ifn
17624 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
17625 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
17626 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63 }else if( searc
17627 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d hList && nearby=
17628 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 =iTrunk ){.
17629 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 /* The list i
1762a 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 s being searched
1762b 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 and this trunk
1762c 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65 page is the page
1762d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 . ** to a
1762e 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c llocate, regardl
1762f 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69 ess of whether i
17630 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 t has leaves..
17631 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
17632 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d assert( *pPgno=
17633 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 =iTrunk );.
17634 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 *ppPage = pTr
17635 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61 unk;. sea
17636 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 rchList = 0;.
17637 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17638 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 3PagerWrite(pTru
17639 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
1763a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
1763b 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
1763c 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
1763d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1763e 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a if( k==0 ){.
1763f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 if( !p
17640 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 PrevTrunk ){.
17641 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
17642 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
17643 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 2], &pTrunk->aDa
17644 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 ta[0], 4);.
17645 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17646 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
17647 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 pPrevTrunk->aDat
17648 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 a[0], &pTrunk->a
17649 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
1764a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1764b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1764c 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 /* The trunk p
1764d 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20 age is required
1764e 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75 by the caller bu
1764f 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 t it contains .
17650 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e ** poin
17651 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 ters to free-lis
17652 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69 t leaves. The fi
17653 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 rst leaf becomes
17654 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 a trunk.
17655 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 ** page in th
17656 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 is case..
17657 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
17658 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75 MemPage *pNewTru
17659 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 nk;. Pg
1765a 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 no iNewTrunk = g
1765b 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d et4byte(&pTrunk-
1765c 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 >aData[8]);.
1765d 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 72 if( iNewTr
1765e 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a 20 unk>mxPage ){ .
1765f 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
17660 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17661 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20 KPT;.
17662 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
17663 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
17664 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74 }. t
17665 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75 estcase( iNewTru
17666 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 nk==mxPage );.
17667 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 rc = btr
17668 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 eeGetPage(pBt, i
17669 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 NewTrunk, &pNewT
1766a 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 runk, 0);.
1766b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1766c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1766d 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
1766e 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
1766f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
17670 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
17671 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 agerWrite(pNewTr
17672 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
17673 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
17674 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17675 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
17676 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 ePage(pNewTrunk)
17677 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f ;. go
17678 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
17679 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 page;.
1767a 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 }. memc
1767b 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 py(&pNewTrunk->a
1767c 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b Data[0], &pTrunk
1767d 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a ->aData[0], 4);.
1767e 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
1767f 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 te(&pNewTrunk->a
17680 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 Data[4], k-1);.
17681 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
17682 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 &pNewTrunk->aDat
17683 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 a[8], &pTrunk->a
17684 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a Data[12], (k-1)*
17685 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 4);. re
17686 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 leasePage(pNewTr
17687 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 unk);.
17688 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 if( !pPrevTrunk
17689 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 ){. a
1768a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
1768b 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
1768c 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20 Page1->pDbPage)
1768d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 );. p
1768e 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d ut4byte(&pPage1-
1768f 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 >aData[32], iNew
17690 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
17691 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17692 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17693 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72 65 3PagerWrite(pPre
17694 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 vTrunk->pDbPage)
17695 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
17696 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
17697 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
17698 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
17699 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1769a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
1769b 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 (&pPrevTrunk->aD
1769c 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e ata[0], iNewTrun
1769d 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a k);. }.
1769e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1769f 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 pTrunk = 0;.
176a0 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
176a1 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b LOCATE: %d trunk
176a2 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73 - %d free pages
176a3 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f left\n", *pPgno
176a4 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a , n-1));.#endif.
176a5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
176a6 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f k>0 ){. /
176a7 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 * Extract a leaf
176a8 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 from the trunk
176a9 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 */. u32 c
176aa 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 losest;.
176ab 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 Pgno iPage;.
176ac 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
176ad 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e r *aData = pTrun
176ae 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 k->aData;.
176af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
176b0 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d gerWrite(pTrunk-
176b1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
176b2 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
176b3 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
176b4 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
176b5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
176b6 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b if( nearby>0 ){
176b7 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 69 . u32 i
176b8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 ;. int
176b9 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 dist;.
176ba 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 closest = 0;.
176bb 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65 dist = ge
176bc 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d t4byte(&aData[8]
176bd 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 ) - nearby;.
176be 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30 if( dist<0
176bf 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b ) dist = -dist;
176c0 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 . for(i
176c1 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 =1; i<k; i++){.
176c2 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 int d
176c3 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 2 = get4byte(&aD
176c4 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 ata[8+i*4]) - ne
176c5 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 arby;.
176c6 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20 if( d2<0 ) d2
176c7 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20 = -d2;.
176c8 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 if( d2<dist )
176c9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
176ca 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 closest = i;.
176cb 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 dist
176cc 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 = d2;.
176cd 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
176ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
176cf 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 closest
176d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
176d1 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d . iPage =
176d2 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 get4byte(&aData
176d3 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a [8+closest*4]);.
176d4 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
176d5 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 ( iPage==mxPage
176d6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 );. if( i
176d7 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a 20 Page>mxPage ){.
176d8 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
176d9 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
176da 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 T;. got
176db 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
176dc 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
176dd 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
176de 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29 iPage==mxPage )
176df 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 ;. if( !s
176e0 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61 earchList || iPa
176e1 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 ge==nearby ){.
176e2 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f int noCo
176e3 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 ntent;.
176e4 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b *pPgno = iPage;
176e5 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43 45 . TRACE
176e6 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 (("ALLOCATE: %d
176e7 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25 was leaf %d of %
176e8 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 d on trunk %d".
176e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176ea 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20 ": %d more free
176eb 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 pages\n",.
176ec 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e *pPgn
176ed 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c o, closest+1, k,
176ee 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e pTrunk->pgno, n
176ef 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 -1));.
176f0 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 if( closest<k-1
176f1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d ){. m
176f2 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63 emcpy(&aData[8+c
176f3 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74 losest*4], &aDat
176f4 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 a[4+k*4], 4);.
176f5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
176f6 20 20 20 20 70 75 74 34 62 79 74 65 28 26 61 44 put4byte(&aD
176f7 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 ata[4], k-1);.
176f8 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
176f9 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
176fa 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d 3e iteable(pTrunk->
176fb 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
176fc 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20 noContent
176fd 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43 6f = !btreeGetHasCo
176fe 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e ntent(pBt, *pPgn
176ff 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 o);. rc
17700 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
17701 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 pBt, *pPgno, ppP
17702 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b age, noContent);
17703 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
17704 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
17705 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
17706 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17707 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 te((*ppPage)->pD
17708 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
17709 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1770a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1770b 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1770c 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
1770d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1770e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1770f 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b searchList = 0;
17710 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
17711 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 }. release
17712 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 Page(pPrevTrunk)
17713 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 ;. pPrevTru
17714 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 nk = 0;. }whi
17715 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 le( searchList )
17716 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
17717 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 * There are no p
17718 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
17719 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 list, so create
1771a 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 a new page at th
1771b 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 e. ** end of
1771c 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 the file */.
1771d 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 int nPage = page
1771e 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b rPagecount(pBt);
1771f 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 . *pPgno = nP
17720 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 age + 1;.. if
17721 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e ( *pPgno==PENDIN
17722 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
17723 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e ){. (*pPgn
17724 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 o)++;. }..#if
17725 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17726 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
17727 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
17728 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 uum && PTRMAP_IS
17729 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f PAGE(pBt, *pPgno
1772a 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 ) ){. /* If
1772b 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 *pPgno refers t
1772c 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 o a pointer-map
1772d 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 page, allocate t
1772e 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 wo new pages.
1772f 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ** at the end
17730 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 of the file ins
17731 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 tead of one. The
17732 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 first allocated
17733 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 page. ** b
17734 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 ecomes a new poi
17735 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 nter-map page, t
17736 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 he second is use
17737 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e d by the caller.
17738 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
17739 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 MemPage *pPg = 0
1773a 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 ;. TRACE(("
1773b 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f ALLOCATE: %d fro
1773c 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 m end of file (p
1773d 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 ointer-map page)
1773e 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 \n", *pPgno));.
1773f 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 assert( *pP
17740 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 gno!=PENDING_BYT
17741 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 E_PAGE(pBt) );.
17742 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
17743 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 etPage(pBt, *pPg
17744 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 no, &pPg, 0);.
17745 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
17746 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
17747 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
17748 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 erWrite(pPg->pDb
17749 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 Page);. r
1774a 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b eleasePage(pPg);
1774b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1774c 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1774d 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f c;. (*pPgno
1774e 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 2a )++;. if( *
1774f 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 pPgno==PENDING_B
17750 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
17751 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 (*pPgno)++; }.
17752 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
17753 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 assert( *pPgno!
17754 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
17755 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 72 GE(pBt) );. r
17756 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
17757 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 (pBt, *pPgno, pp
17758 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 Page, 0);. if
17759 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
1775a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1775b 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 e3PagerWrite((*p
1775c 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 pPage)->pDbPage)
1775d 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1775e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1775f 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 releasePage(*pp
17760 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Page);. }.
17761 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
17762 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f E: %d from end o
17763 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e f file\n", *pPgn
17764 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 o));. }.. asse
17765 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 rt( *pPgno!=PEND
17766 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
17767 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 t) );..end_alloc
17768 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 ate_page:. rele
17769 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b asePage(pTrunk);
1776a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
1776b 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 PrevTrunk);. if
1776c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1776d 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1776e 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f e3PagerPageRefco
1776f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 unt((*ppPage)->p
17770 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20 DbPage)>1 ){.
17771 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
17772 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 ppPage);. r
17773 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
17774 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
17775 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e . (*ppPage)->
17776 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 65 isInit = 0;. }e
17777 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 lse{. *ppPage
17778 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
17779 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1777a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1777b 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67 used to add pag
1777c 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64 e iPage to the d
1777d 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65 atabase file fre
1777e 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69 e-list. .** It i
1777f 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 s assumed that t
17780 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
17781 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66 lready a part of
17782 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a the free-list..
17783 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
17784 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
17785 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
17786 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
17787 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 s optional..** I
17788 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70 f the caller hap
17789 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70 pens to have a p
1778a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 ointer to the Me
1778b 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a mPage object .**
1778c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1778d 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e o page iPage han
1778e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 dy, it may pass
1778f 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 it as the second
17790 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 value. .** Othe
17791 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61 rwise, it may pa
17792 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 ss NULL..**.** I
17793 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 f a pointer to a
17794 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 MemPage object
17795 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
17796 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
17797 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e ,.** its referen
17798 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 ce count is not
17799 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20 altered by this
1779a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
1779b 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 tic int freePage
1779c 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 2(BtShared *pBt,
1779d 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 MemPage *pMemPa
1779e 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b ge, Pgno iPage){
1779f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 . MemPage *pTru
177a0 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 nk = 0;
177a1 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c /* Free-l
177a2 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a ist trunk page *
177a3 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20 /. Pgno iTrunk
177a4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
177a5 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
177a6 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c number of free-l
177a7 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a ist trunk page *
177a8 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 / . MemPage *pP
177a9 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 age1 = pBt->pPag
177aa 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 e1; /* Loca
177ab 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 l reference to p
177ac 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 age 1 */. MemPa
177ad 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 ge *pPage;
177ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
177af 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65 * Page being fre
177b0 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e ed. May be NULL.
177b1 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
177b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
177b4 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e urn Code */. in
177b5 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 t nFree;
177b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177b7 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d /* Initial num
177b8 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
177b9 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 free-list */..
177ba 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
177bb 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
177bc 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
177bd 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20 rt( iPage>1 );.
177be 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61 assert( !pMemPa
177bf 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e ge || pMemPage->
177c0 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a pgno==iPage );..
177c1 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29 if( pMemPage )
177c2 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d {. pPage = pM
177c3 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69 emPage;. sqli
177c4 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67 te3PagerRef(pPag
177c5 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d e->pDbPage);. }
177c6 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20 else{. pPage
177c7 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 = btreePageLooku
177c8 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 p(pBt, iPage);.
177c9 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 }.. /* Increme
177ca 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65 nt the free page
177cb 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31 count on pPage1
177cc 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
177cd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
177ce 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
177cf 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 if( rc ) goto f
177d0 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e reepage_out;. n
177d1 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 Free = get4byte(
177d2 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
177d3 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 6]);. put4byte(
177d4 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
177d5 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 23 6], nFree+1);..#
177d6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 ifdef SQLITE_SEC
177d7 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 URE_DELETE. /*
177d8 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 45 If the SQLITE_SE
177d9 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d 70 CURE_DELETE comp
177da 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 ile-time option
177db 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e is enabled, then
177dc 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c . ** always ful
177dd 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c ly overwrite del
177de 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e eted information
177df 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 2a with zeros.. *
177e0 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 65 20 /. if( (!pPage
177e1 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 47 65 && (rc = btreeGe
177e2 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 tPage(pBt, iPage
177e3 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a 20 , &pPage, 0))).
177e4 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20 20 ||
177e5 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
177e6 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
177e7 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20 20 DbPage)). ){.
177e8 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
177e9 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 out;. }. memse
177ea 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 t(pPage->aData,
177eb 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 0, pPage->pBt->p
177ec 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 ageSize);.#endif
177ed 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 .. /* If the da
177ee 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
177ef 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 auto-vacuum, wri
177f0 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 te an entry in t
177f1 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 he pointer-map.
177f2 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 ** to indicate
177f3 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 that the page is
177f4 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 free.. */. if
177f5 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 ( ISAUTOVACUUM )
177f6 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 {. ptrmapPut(
177f7 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d pBt, iPage, PTRM
177f8 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 AP_FREEPAGE, 0,
177f9 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 &rc);. if( rc
177fa 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 ) goto freepage
177fb 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 _out;. }.. /*
177fc 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 74 Now manipulate t
177fd 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 61 he actual databa
177fe 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 72 se free-list str
177ff 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 72 ucture. There ar
17800 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 69 e two. ** possi
17801 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 65 bilities. If the
17802 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 75 free-list is cu
17803 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 6f rrently empty, o
17804 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a 20 r if the first.
17805 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 69 ** trunk page i
17806 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
17807 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 68 is full, then th
17808 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 is page will bec
17809 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 66 ome a. ** new f
1780a 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 ree-list trunk p
1780b 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 age. Otherwise,
1780c 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 it will become a
1780d 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 2a leaf of the. *
1780e 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 * first trunk pa
1780f 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e ge in the curren
17810 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 t free-list. Thi
17811 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 66 s block tests if
17812 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 it. ** is poss
17813 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 20 ible to add the
17814 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 72 page as a new fr
17815 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 20 ee-list leaf..
17816 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 3d */. if( nFree!=
17817 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c 65 0 ){. u32 nLe
17818 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 af;
17819 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 /* Initial nu
1781a 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 6c mber of leaf cel
1781b 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 ls on trunk page
1781c 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b 20 */.. iTrunk
1781d 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
1781e 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a e1->aData[32]);.
1781f 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
17820 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e tPage(pBt, iTrun
17821 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a k, &pTrunk, 0);.
17822 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
17823 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
17824 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
17825 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 ;. }.. nLe
17826 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 af = get4byte(&p
17827 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 Trunk->aData[4])
17828 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
17829 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 32 t->usableSize>32
1782a 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 );. if( nLea
1782b 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 73 f > (u32)pBt->us
1782c 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29 ableSize/4 - 2 )
1782d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1782e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1782f 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 ;. goto fre
17830 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d epage_out;. }
17831 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 3c . if( nLeaf <
17832 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 6c (u32)pBt->usabl
17833 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20 eSize/4 - 8 ){.
17834 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 /* In this
17835 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72 6f case there is ro
17836 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 om on the trunk
17837 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74 page to insert t
17838 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a he page. **
17839 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 being freed as
1783a 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20 a new leaf..
1783b 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f **. ** No
1783c 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 6e te that the trun
1783d 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65 k page is not re
1783e 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 ally full until
1783f 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 it contains.
17840 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f ** usableSize/
17841 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 4 - 2 entries, n
17842 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 ot usableSize/4
17843 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 77 - 8 entries as w
17844 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 e have. **
17845 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 20 coded. But due
17846 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f to a coding erro
17847 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 r in versions of
17848 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f SQLite prior to
17849 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c . ** 3.6.0,
1784a 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20 databases with
1784b 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 freelist trunk p
1784c 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 ages holding mor
1784d 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 e than. **
1784e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 usableSize/4 - 8
1784f 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65 entries will be
17850 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72 reported as cor
17851 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a rupt. In order.
17852 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e ** to main
17853 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 tain backwards c
17854 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 ompatibility wit
17855 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 h older versions
17856 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 of SQLite,.
17857 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e ** we will con
17858 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 63 tinue to restric
17859 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
1785a 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c entries to usabl
1785b 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 eSize/4 - 8.
1785c 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 ** for now. A
1785d 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 t some point in
1785e 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65 the future (once
1785f 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70 everyone has up
17860 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 graded. **
17861 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 to 3.6.0 or late
17862 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e r) we should con
17863 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65 sider fixing the
17864 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f conditional abo
17865 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 ve. ** to r
17866 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f ead "usableSize/
17867 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 4-2" instead of
17868 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 "usableSize/4-8"
17869 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1786a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1786b 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e erWrite(pTrunk->
1786c 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1786d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1786e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 K ){. put
1786f 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 4byte(&pTrunk->a
17870 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31 Data[4], nLeaf+1
17871 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 );. put4b
17872 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
17873 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69 ta[8+nLeaf*4], i
17874 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 Page);.#ifndef S
17875 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c QLITE_SECURE_DEL
17876 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 ETE. if(
17877 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 pPage ){.
17878 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 sqlite3PagerD
17879 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e ontWrite(pPage->
1787a 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1787b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
1787c 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 65 74 rc = btreeSet
1787d 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 HasContent(pBt,
1787e 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a iPage);. }.
1787f 20 20 20 20 20 20 54 52 41 43 45 28 28 22 46 52 TRACE(("FR
17880 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66 EE-PAGE: %d leaf
17881 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25 on trunk page %
17882 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f d\n",pPage->pgno
17883 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b ,pTrunk->pgno));
17884 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 . goto free
17885 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
17886 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e }.. /* If con
17887 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 trol flows to th
17888 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69 is point, then i
17889 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 t was not possib
1788a 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20 20 le to add the.
1788b 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 69 6e ** the page bein
1788c 67 20 66 72 65 65 64 20 61 73 20 61 20 6c 65 61 g freed as a lea
1788d 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 f page of the fi
1788e 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 rst trunk in the
1788f 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a free-list.. **
17890 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 Possibly becaus
17891 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 e the free-list
17892 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73 is empty, or pos
17893 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 sibly because th
17894 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 e . ** first tr
17895 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d unk in the free-
17896 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 69 list is full. Ei
17897 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 70 61 ther way, the pa
17898 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a 20 ge being freed.
17899 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 ** will become
1789a 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 74 72 the new first tr
1789b 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 unk page in the
1789c 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a free-list.. */.
1789d 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26 if( pPage==0 &
1789e 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 & SQLITE_OK!=(rc
1789f 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
178a0 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 pBt, iPage, &pPa
178a1 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 67 ge, 0)) ){. g
178a2 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
178a3 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
178a4 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
178a5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
178a6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
178a7 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 _OK ){. goto
178a8 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
178a9 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61 }. put4byte(pPa
178aa 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e ge->aData, iTrun
178ab 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 k);. put4byte(&
178ac 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c pPage->aData[4],
178ad 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 0);. put4byte(
178ae 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
178af 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 2], iPage);. TR
178b0 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a ACE(("FREE-PAGE:
178b1 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 %d new trunk pa
178b2 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c ge replacing %d\
178b3 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c n", pPage->pgno,
178b4 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 iTrunk));..free
178b5 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 page_out:. if(
178b6 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 pPage ){. pPa
178b7 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ge->isInit = 0;.
178b8 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 }. releasePag
178b9 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 e(pPage);. rele
178ba 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b asePage(pTrunk);
178bb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
178bc 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
178bd 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
178be 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a age, int *pRC){.
178bf 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 if( (*pRC)==SQ
178c0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a LITE_OK ){. *
178c1 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 32 28 pRC = freePage2(
178c2 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 67 pPage->pBt, pPag
178c3 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b e, pPage->pgno);
178c4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 . }.}../*.** Fr
178c5 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 ee any overflow
178c6 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 pages associated
178c7 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 with the given
178c8 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Cell..*/.static
178c9 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65 int clearCell(Me
178ca 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e mPage *pPage, un
178cb 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
178cc 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ll){. BtShared
178cd 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 *pBt = pPage->pB
178ce 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e t;. CellInfo in
178cf 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50 fo;. Pgno ovflP
178d0 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gno;. int rc;.
178d1 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 31 int nOvfl;. u1
178d2 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 6 ovflPageSize;.
178d3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
178d4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
178d5 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
178d6 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 );. btreeParse
178d7 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
178d8 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
178d9 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c if( info.iOverfl
178da 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ow==0 ){. ret
178db 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 urn SQLITE_OK;
178dc 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 /* No overflow p
178dd 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 ages. Return wit
178de 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 hout doing anyth
178df 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 ing */. }. ovf
178e0 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 lPgno = get4byte
178e1 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
178e2 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65 erflow]);. asse
178e3 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
178e4 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66 ize > 4 );. ovf
178e5 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d lPageSize = pBt-
178e6 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b >usableSize - 4;
178e7 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f . nOvfl = (info
178e8 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f .nPayload - info
178e9 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 .nLocal + ovflPa
178ea 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c geSize - 1)/ovfl
178eb 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 PageSize;. asse
178ec 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 rt( ovflPgno==0
178ed 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 || nOvfl>0 );.
178ee 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 while( nOvfl-- )
178ef 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74 {. Pgno iNext
178f0 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67 = 0;. MemPag
178f1 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 e *pOvfl = 0;.
178f2 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32 if( ovflPgno<2
178f3 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 || ovflPgno>pag
178f4 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
178f5 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69 ){. /* 0 i
178f6 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61 s not a legal pa
178f7 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 ge number and pa
178f8 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61 ge 1 cannot be a
178f9 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 n . ** over
178fa 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65 flow page. There
178fb 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f fore if ovflPgno
178fc 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65 <2 or past the e
178fd 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 nd of the .
178fe 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74 ** file the dat
178ff 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f abase must be co
17900 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20 rrupt. */.
17901 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
17902 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
17903 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 }. if( nOvfl
17904 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 ){. rc = ge
17905 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 tOverflowPage(pB
17906 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f t, ovflPgno, &pO
17907 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20 vfl, &iNext);.
17908 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
17909 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1790a 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 32 rc = freePage2
1790b 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66 (pBt, pOvfl, ovf
1790c 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 lPgno);. if(
1790d 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 73 pOvfl ){. s
1790e 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
1790f 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 (pOvfl->pDbPage)
17910 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
17911 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
17912 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69 ovflPgno = i
17913 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Next;. }. retu
17914 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
17915 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 ./*.** Create th
17916 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 e byte sequence
17917 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e used to represen
17918 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65 t a cell on page
17919 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72 pPage.** and wr
1791a 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65 ite that byte se
1791b 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c quence into pCel
1791c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70 l[]. Overflow p
1791d 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f ages are.** allo
1791e 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64 cated and filled
1791f 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79 in as necessary
17920 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 . The calling p
17921 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72 rocedure.** is r
17922 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d esponsible for m
17923 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69 aking sure suffi
17924 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20 cient space has
17925 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a been allocated.*
17926 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a * for pCell[]..*
17927 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70 *.** Note that p
17928 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65 Cell does not ne
17929 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20 cessary need to
1792a 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61 point to the pPa
1792b 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65 ge->aData.** are
1792c 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 a. pCell might
1792d 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65 point to some te
1792e 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e mporary storage.
1792f 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a The cell will.
17930 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 ** be constructe
17931 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72 d in this tempor
17932 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f ary area then co
17933 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d pied into pPage-
17934 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e >aData.** later.
17935 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
17936 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d illInCell(. Mem
17937 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
17938 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
17939 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
1793a 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a tains the cell *
1793b 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
1793c 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 r *pCell,
1793d 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 /* Complete t
1793e 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 ext of the cell
1793f 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
17940 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c *pKey, i64 nKey,
17941 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a /* The key *
17942 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
17943 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c pData,int nData,
17944 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a /* The data *
17945 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 /. int nZero,
17946 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17947 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f /* Extra zero
17948 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 bytes to append
17949 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69 to pData */. i
1794a 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20 nt *pnSize
1794b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1794c 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65 Write cell size
1794d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
1794e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f t nPayload;. co
1794f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20 nst u8 *pSrc;.
17950 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b int nSrc, n, rc;
17951 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74 . int spaceLeft
17952 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 ;. MemPage *pOv
17953 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 fl = 0;. MemPag
17954 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 e *pToRelease =
17955 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 0;. unsigned ch
17956 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e ar *pPrior;. un
17957 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61 signed char *pPa
17958 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65 yload;. BtShare
17959 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
1795a 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f pBt;. Pgno pgno
1795b 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 Ovfl = 0;. int
1795c 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49 nHeader;. CellI
1795d 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73 nfo info;.. ass
1795e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1795f 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
17960 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 Bt->mutex) );..
17961 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74 /* pPage is not
17962 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69 necessarily wri
17963 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65 teable since pCe
17964 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69 ll might be auxi
17965 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65 liary. ** buffe
17966 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20 r space that is
17967 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 separate from th
17968 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61 e pPage buffer a
17969 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 rea */. assert(
1796a 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44 pCell<pPage->aD
1796b 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70 ata || pCell>=&p
1796c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
1796d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 >pageSize].
1796e 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
1796f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
17970 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
17971 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 ) );.. /* Fill
17972 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a in the header. *
17973 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b /. nHeader = 0;
17974 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c . if( !pPage->l
17975 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 eaf ){. nHead
17976 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 er += 4;. }. i
17977 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 f( pPage->hasDat
17978 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 a ){. nHeader
17979 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 += putVarint(&p
1797a 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e Cell[nHeader], n
1797b 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d Data+nZero);. }
1797c 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 else{. nData
1797d 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d = nZero = 0;. }
1797e 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 . nHeader += pu
1797f 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e tVarint(&pCell[n
17980 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 Header], *(u64*)
17981 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 50 &nKey);. btreeP
17982 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
17983 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
17984 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f ;. assert( info
17985 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 .nHeader==nHeade
17986 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 r );. assert( i
17987 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 nfo.nKey==nKey )
17988 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f ;. assert( info
17989 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 .nData==(u32)(nD
1798a 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 ata+nZero) );.
1798b 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 . /* Fill in th
1798c 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e e payload */. n
1798d 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 Payload = nData
1798e 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 + nZero;. if( p
1798f 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
17990 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61 pSrc = pData
17991 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 ;. nSrc = nDa
17992 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 ta;. nData =
17993 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 0;. }else{ .
17994 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 3e if( NEVER(nKey>
17995 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b 0x7fffffff || pK
17996 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 ey==0) ){.
17997 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
17998 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
17999 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b }. nPayload +
1799a 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20 = (int)nKey;.
1799b 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20 pSrc = pKey;.
1799c 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b nSrc = (int)nK
1799d 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a ey;. }. *pnSiz
1799e 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a e = info.nSize;.
1799f 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e spaceLeft = in
179a0 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61 fo.nLocal;. pPa
179a1 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e yload = &pCell[n
179a2 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f Header];. pPrio
179a3 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e r = &pCell[info.
179a4 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77 iOverflow];.. w
179a5 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30 hile( nPayload>0
179a6 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63 ){. if( spac
179a7 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e eLeft==0 ){.#ifn
179a8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
179a9 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
179aa 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70 Pgno pgnoPtrmap
179ab 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 = pgnoOvfl; /*
179ac 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f Overflow page po
179ad 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 inter-map entry
179ae 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 page */. if
179af 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
179b0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b m ){. do{
179b1 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f . pgnoO
179b2 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d vfl++;. }
179b3 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20 while( .
179b4 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 PTRMAP_ISPAGE
179b5 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20 (pBt, pgnoOvfl)
179b6 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e || pgnoOvfl==PEN
179b7 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
179b8 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a Bt) . );.
179b9 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
179ba 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
179bb 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
179bc 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 &pOvfl, &pgnoOv
179bd 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29 fl, pgnoOvfl, 0)
179be 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
179bf 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
179c0 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
179c1 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
179c2 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 ts auto-vacuum,
179c3 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f and the second o
179c4 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 r subsequent.
179c5 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 ** overflow p
179c6 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c age is being all
179c7 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 ocated, add an e
179c8 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e ntry to the poin
179c9 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a ter-map. **
179ca 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e for that page n
179cb 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ow. . **.
179cc 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
179cd 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 s the first over
179ce 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 flow page, then
179cf 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 write a partial
179d0 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 entry . **
179d1 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d to the pointer-m
179d2 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 ap. If we write
179d3 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 nothing to this
179d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 pointer-map slot
179d5 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ,. ** then
179d6 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f the optimistic o
179d7 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 verflow chain pr
179d8 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 ocessing in clea
179d9 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a rCell(). **
179da 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 may misinterpre
179db 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 t the uninitiali
179dc 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 sed values and d
179dd 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 elete the.
179de 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 ** wrong pages f
179df 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
179e0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
179e1 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
179e2 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 cuum && rc==SQLI
179e3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
179e4 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e u8 eType = (pgn
179e5 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f oPtrmap?PTRMAP_O
179e6 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f VERFLOW2:PTRMAP_
179e7 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 OVERFLOW1);.
179e8 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
179e9 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 t, pgnoOvfl, eTy
179ea 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c 20 pe, pgnoPtrmap,
179eb 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 &rc);. if
179ec 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
179ed 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f releasePage(pO
179ee 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a vfl);. }.
179ef 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
179f0 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
179f1 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
179f2 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
179f3 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
179f4 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 c;. }..
179f5 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 /* If pToRelea
179f6 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 se is not zero t
179f7 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74 han pPrior point
179f8 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 s into the data
179f9 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66 area. ** of
179fa 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 pToRelease. Ma
179fb 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 ke sure pToRelea
179fc 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 se is still writ
179fd 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 eable. */.
179fe 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 assert( pToRelea
179ff 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 se==0 || sqlite3
17a00 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
17a01 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 (pToRelease->pDb
17a02 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 Page) );..
17a03 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20 /* If pPrior is
17a04 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
17a05 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 area of pPage,
17a06 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 then make sure p
17a07 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 Page. ** is
17a08 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 still writeable
17a09 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
17a0a 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e ( pPrior<pPage->
17a0b 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e aData || pPrior>
17a0c 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 =&pPage->aData[p
17a0d 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 Bt->pageSize].
17a0e 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c || sql
17a0f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
17a10 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
17a11 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70 age) );.. p
17a12 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 ut4byte(pPrior,
17a13 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 pgnoOvfl);.
17a14 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f releasePage(pTo
17a15 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 Release);.
17a16 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76 pToRelease = pOv
17a17 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 fl;. pPrior
17a18 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b = pOvfl->aData;
17a19 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
17a1a 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20 pPrior, 0);.
17a1b 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f pPayload = &pO
17a1c 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20 vfl->aData[4];.
17a1d 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d spaceLeft =
17a1e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
17a1f 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 - 4;. }.
17a20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 n = nPayload;.
17a21 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66 if( n>spaceLef
17a22 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66 t ) n = spaceLef
17a23 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54 t;.. /* If pT
17a24 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 oRelease is not
17a25 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f zero than pPaylo
17a26 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 ad points into t
17a27 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 he data area.
17a28 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 ** of pToReleas
17a29 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 e. Make sure pT
17a2a 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c oRelease is stil
17a2b 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a l writeable. */.
17a2c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 assert( pToR
17a2d 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c elease==0 || sql
17a2e 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
17a2f 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d able(pToRelease-
17a30 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
17a31 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64 /* If pPayload
17a32 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 is part of the
17a33 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 data area of pPa
17a34 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 ge, then make su
17a35 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20 re pPage. **
17a36 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 is still writeab
17a37 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 le */. assert
17a38 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65 ( pPayload<pPage
17a39 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c ->aData || pPayl
17a3a 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 oad>=&pPage->aDa
17a3b 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 ta[pBt->pageSize
17a3c 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ]. ||
17a3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
17a3e 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
17a3f 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
17a40 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20 if( nSrc>0 ){.
17a41 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20 if( n>nSrc
17a42 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20 ) n = nSrc;.
17a43 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29 assert( pSrc )
17a44 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 ;. memcpy(p
17a45 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e Payload, pSrc, n
17a46 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
17a47 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c memset(pPayl
17a48 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20 oad, 0, n);.
17a49 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d }. nPayload -
17a4a 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61 = n;. pPayloa
17a4b 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63 d += n;. pSrc
17a4c 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20 += n;. nSrc
17a4d 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c -= n;. spaceL
17a4e 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66 eft -= n;. if
17a4f 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 ( nSrc==0 ){.
17a50 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b nSrc = nData;
17a51 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44 . pSrc = pD
17a52 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ata;. }. }.
17a53 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f releasePage(pTo
17a54 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75 Release);. retu
17a55 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
17a56 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 ./*.** Remove th
17a57 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d e i-th cell from
17a58 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f pPage. This ro
17a59 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50 utine effects pP
17a5a 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65 age only..** The
17a5b 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 cell content is
17a5c 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65 not freed or de
17a5d 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69 allocated. It i
17a5e 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a s assumed that.*
17a5f 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 * the cell conte
17a60 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69 nt has been copi
17a61 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73 ed someplace els
17a62 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
17a63 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73 just.** removes
17a64 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 the reference t
17a65 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 o the cell from
17a66 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a pPage..**.** "sz
17a67 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75 " must be the nu
17a68 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
17a69 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 the cell..*/.st
17a6a 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65 atic void dropCe
17a6b 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ll(MemPage *pPag
17a6c 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 e, int idx, int
17a6d 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 sz, int *pRC){.
17a6e 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
17a6f 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
17a70 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 */. int pc;
17a71 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
17a72 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 to cell content
17a73 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 of cell being de
17a74 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 leted */. u8 *d
17a75 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 ata; /* pP
17a76 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 age->aData */.
17a77 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 u8 *ptr;
17a78 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 /* Used to move
17a79 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74 bytes around wit
17a7a 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 hin data[] */.
17a7b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
17a7c 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f /* The return co
17a7d 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b de */. int hdr;
17a7e 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e /* Begin
17a7f 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64 ning of the head
17a80 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65 er. 0 most page
17a81 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a s. 100 page 1 *
17a82 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 /.. if( *pRC )
17a83 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 return;.. asser
17a84 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78 t( idx>=0 && idx
17a85 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b <pPage->nCell );
17a86 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 . assert( sz==c
17a87 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69 ellSize(pPage, i
17a88 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 dx) );. assert(
17a89 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
17a8a 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
17a8b 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
17a8c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17a8d 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
17a8e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
17a8f 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
17a90 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 64 Data;. ptr = &d
17a91 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f ata[pPage->cellO
17a92 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a ffset + 2*idx];.
17a93 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 pc = get2byte(
17a94 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 50 ptr);. hdr = pP
17a95 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
17a96 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d testcase( pc==
17a97 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
17a98 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 74 dr+5]) );. test
17a99 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 61 case( pc+sz==pPa
17a9a 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
17a9b 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 20 ize );. if( pc
17a9c 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 < get2byte(&data
17a9d 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 [hdr+5]) || pc+s
17a9e 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e z > pPage->pBt->
17a9f 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
17aa0 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f *pRC = SQLITE_
17aa1 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
17aa2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
17aa3 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 rc = freeSpace(p
17aa4 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 Page, pc, sz);.
17aa5 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a if( rc ){. *
17aa6 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 pRC = rc;. re
17aa7 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 turn;. }. for(
17aa8 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 i=idx+1; i<pPage
17aa9 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 ->nCell; i++, pt
17aaa 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 r+=2){. ptr[0
17aab 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 ] = ptr[2];.
17aac 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b ptr[1] = ptr[3];
17aad 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 . }. pPage->nC
17aae 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 ell--;. put2byt
17aaf 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 e(&data[hdr+3],
17ab0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 pPage->nCell);.
17ab1 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d pPage->nFree +=
17ab2 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 2;.}../*.** Ins
17ab3 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f ert a new cell o
17ab4 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 n pPage at cell
17ab5 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c index "i". pCel
17ab6 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a l points to the.
17ab7 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 ** content of th
17ab8 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 e cell..**.** If
17ab9 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
17aba 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 t will fit on th
17abb 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 e page, then put
17abc 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 it there. If i
17abd 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 t.** will not fi
17abe 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 t, then make a c
17abf 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 opy of the cell
17ac0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 content into pTe
17ac1 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 mp if.** pTemp i
17ac2 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 s not null. Reg
17ac3 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 ardless of pTemp
17ac4 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 , allocate a new
17ac5 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 entry.** in pPa
17ac6 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 ge->aOvfl[] and
17ac7 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f make it point to
17ac8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
17ac9 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 t (either.** in
17aca 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 pTemp or the ori
17acb 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 ginal pCell) and
17acc 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 also record its
17acd 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f index. .** Allo
17ace 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 cating a new ent
17acf 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 ry in pPage->aCe
17ad0 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 ll[] implies tha
17ad1 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 t .** pPage->nOv
17ad2 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d erflow is increm
17ad3 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ented..**.** If
17ad4 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 nSkip is non-zer
17ad5 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 o, then do not c
17ad6 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 opy the first nS
17ad7 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 kip bytes of the
17ad8 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 .** cell. The ca
17ad9 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 ller will overwr
17ada 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 ite them after t
17adb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
17adc 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 urns. If.** nSki
17add 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
17ade 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f hen pCell may no
17adf 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e t point to an in
17ae0 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 valid memory loc
17ae1 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 ation .** (but p
17ae2 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c Cell+nSkip is al
17ae3 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a ways valid)..*/.
17ae4 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 65 static void inse
17ae5 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 rtCell(. MemPag
17ae6 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 e *pPage, /* P
17ae7 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 age into which w
17ae8 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f e are copying */
17ae9 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 . int i,
17aea 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c /* New cell
17aeb 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 becomes the i-t
17aec 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 h cell of the pa
17aed 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c ge */. u8 *pCel
17aee 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e l, /* Con
17aef 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 tent of the new
17af0 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a cell */. int sz
17af1 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 , /* B
17af2 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 ytes of content
17af3 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 in pCell */. u8
17af4 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 *pTemp,
17af5 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 /* Temp storage
17af6 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c space for pCell,
17af7 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 if needed */.
17af8 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 Pgno iChild,
17af9 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f /* If non-zero
17afa 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 20 , replace first
17afb 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 69 4 bytes with thi
17afc 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 s value */. int
17afd 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 2f *pRC /
17afe 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 * Read and write
17aff 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f return code fro
17b00 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 m here */.){. i
17b01 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 nt idx;
17b02 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 /* Where to wri
17b03 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74 te new cell cont
17b04 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f ent in data[] */
17b05 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 . int j;
17b06 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
17b07 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6e nter */. int en
17b08 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 d; /* F
17b09 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
17b0a 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 he last cell poi
17b0b 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a nter in data[] *
17b0c 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 /. int ins;
17b0d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
17b0e 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e n data[] where n
17b0f 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 ew cell pointer
17b10 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 is inserted */.
17b11 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b int cellOffset;
17b12 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
17b13 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e first cell poin
17b14 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f ter in data[] */
17b15 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 . u8 *data;
17b16 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 /* The cont
17b17 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 ent of the whole
17b18 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 page */. u8 *p
17b19 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 tr; /*
17b1a 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 Used for moving
17b1b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 information arou
17b1c 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a nd in data[] */.
17b1d 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20 28 . int nSkip = (
17b1e 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29 3b iChild ? 4 : 0);
17b1f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 .. if( *pRC ) r
17b20 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 eturn;.. assert
17b21 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61 ( i>=0 && i<=pPa
17b22 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d ge->nCell+pPage-
17b23 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 >nOverflow );.
17b24 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
17b25 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 Cell<=MX_CELL(pP
17b26 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f age->pBt) && MX_
17b27 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 CELL(pPage->pBt)
17b28 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65 <=5460 );. asse
17b29 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
17b2a 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28 flow<=ArraySize(
17b2b 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b pPage->aOvfl) );
17b2c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17b2d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
17b2e 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
17b2f 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 63 65 6c );. /* The cel
17b30 6c 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c l should normall
17b31 79 20 62 65 20 73 69 7a 65 64 20 63 6f 72 72 65 y be sized corre
17b32 63 74 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 ctly. However,
17b33 77 68 65 6e 20 6d 6f 76 69 6e 67 20 61 0a 20 20 when moving a.
17b34 2a 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65 6c ** malformed cel
17b35 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70 61 l from a leaf pa
17b36 67 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f ge to an interio
17b37 72 20 70 61 67 65 2c 20 69 66 20 74 68 65 20 63 r page, if the c
17b38 65 6c 6c 20 73 69 7a 65 0a 20 20 2a 2a 20 77 61 ell size. ** wa
17b39 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 nted to be less
17b3a 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20 72 than 4 but got r
17b3b 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 6f ounded up to 4 o
17b3c 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65 6e n the leaf, then
17b3d 20 73 69 7a 65 0a 20 20 2a 2a 20 6d 69 67 68 74 size. ** might
17b3e 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38 20 be less than 8
17b3f 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f 69 (leaf-size + poi
17b40 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e 74 nter) on the int
17b41 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65 6e erior node. Hen
17b42 63 65 0a 20 20 2a 2a 20 74 68 65 20 74 65 72 6d ce. ** the term
17b43 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20 69 6e after the || in
17b44 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
17b45 73 73 65 72 74 28 29 2e 20 2a 2f 0a 20 20 61 73 ssert(). */. as
17b46 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 sert( sz==cellSi
17b47 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 zePtr(pPage, pCe
17b48 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38 20 26 26 ll) || (sz==8 &&
17b49 20 69 43 68 69 6c 64 3e 30 29 20 29 3b 0a 20 20 iChild>0) );.
17b4a 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 if( pPage->nOver
17b4b 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 flow || sz+2>pPa
17b4c 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 ge->nFree ){.
17b4d 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20 if( pTemp ){.
17b4e 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 memcpy(pTemp
17b4f 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 +nSkip, pCell+nS
17b50 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a kip, sz-nSkip);.
17b51 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 pCell = pT
17b52 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 emp;. }. i
17b53 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 f( iChild ){.
17b54 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c put4byte(pCel
17b55 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 l, iChild);.
17b56 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d }. j = pPage-
17b57 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 >nOverflow++;.
17b58 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74 assert( j<(int
17b59 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e )(sizeof(pPage->
17b5a 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50 aOvfl)/sizeof(pP
17b5b 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20 age->aOvfl[0]))
17b5c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f );. pPage->aO
17b5d 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70 vfl[j].pCell = p
17b5e 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d Cell;. pPage-
17b5f 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20 >aOvfl[j].idx =
17b60 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b (u16)i;. }else{
17b61 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 . int rc = sq
17b62 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
17b63 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
17b64 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
17b65 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17b66 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20 *pRC = rc;.
17b67 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
17b68 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
17b69 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17b6a 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
17b6b 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d e) );. data =
17b6c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
17b6d 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 cellOffset =
17b6e 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
17b6f 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 6c t;. end = cel
17b70 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 lOffset + 2*pPag
17b71 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e e->nCell;. in
17b72 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b s = cellOffset +
17b73 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 61 2*i;. rc = a
17b74 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 llocateSpace(pPa
17b75 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20 ge, sz, &idx);.
17b76 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 52 if( rc ){ *pR
17b77 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b 20 C = rc; return;
17b78 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c }. /* The all
17b79 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f 75 ocateSpace() rou
17b7a 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 20 tine guarantees
17b7b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 the following tw
17b7c 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 20 o properties.
17b7d 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e ** if it return
17b7e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 20 s success */.
17b7f 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20 assert( idx >=
17b80 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 end+2 );. ass
17b81 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 70 ert( idx+sz <= p
17b82 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
17b83 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 61 eSize );. pPa
17b84 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 ge->nCell++;.
17b85 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d pPage->nFree -=
17b86 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b 0a (u16)(2 + sz);.
17b87 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
17b88 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 [idx+nSkip], pCe
17b89 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b ll+nSkip, sz-nSk
17b8a 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43 68 ip);. if( iCh
17b8b 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74 ild ){. put
17b8c 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 5d 4byte(&data[idx]
17b8d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d , iChild);. }
17b8e 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c 20 . for(j=end,
17b8f 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e ptr=&data[j]; j>
17b90 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d ins; j-=2, ptr-=
17b91 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d 2){. ptr[0]
17b92 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 = ptr[-2];.
17b93 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d ptr[1] = ptr[-
17b94 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 1];. }. pu
17b95 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73 t2byte(&data[ins
17b96 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74 ], idx);. put
17b97 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 67 2byte(&data[pPag
17b98 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 2c e->hdrOffset+3],
17b99 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a pPage->nCell);.
17b9a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17b9b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17b9c 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 if( pPage->pB
17b9d 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
17b9e 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 . /* The ce
17b9f 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 ll may contain a
17ba0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f pointer to an o
17ba1 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 verflow page. If
17ba2 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 so, write.
17ba3 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f ** the entry fo
17ba4 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 r the overflow p
17ba5 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 age into the poi
17ba6 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 nter map..
17ba7 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 */. ptrmapP
17ba8 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c utOvflPtr(pPage,
17ba9 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 20 pCell, pRC);.
17baa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d }.#endif. }.}
17bab 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 ../*.** Add a li
17bac 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 st of cells to a
17bad 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 page. The page
17bae 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 should be initi
17baf 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 ally empty..** T
17bb0 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 he cells are gua
17bb1 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f ranteed to fit o
17bb2 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 n the page..*/.s
17bb3 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d tatic void assem
17bb4 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 blePage(. MemPa
17bb5 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 ge *pPage, /*
17bb6 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 The page to be a
17bb7 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 ssemblied */. i
17bb8 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 nt nCell,
17bb9 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f /* The number o
17bba 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 f cells to add t
17bbb 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 o this page */.
17bbc 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 u8 **apCell,
17bbd 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 /* Pointers t
17bbe 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f o cell bodies */
17bbf 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 . u16 *aSize
17bc0 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 /* Sizes of
17bc1 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b the cells */.){
17bc2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
17bc3 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
17bc4 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43 nter */. u8 *pC
17bc5 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41 ellptr; /* A
17bc6 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 ddress of next c
17bc7 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
17bc8 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 int cellbody;
17bc9 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
17bca 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 next cell body
17bcb 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 */. u8 * const
17bcc 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
17bcd 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
17bce 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 /* Pointer to d
17bcf 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f ata for pPage */
17bd0 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 . const int hdr
17bd1 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
17bd2 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f set; /
17bd3 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 * Offset of head
17bd4 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 er on pPage */.
17bd5 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62 const int nUsab
17bd6 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d le = pPage->pBt-
17bd7 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 >usableSize; /*
17bd8 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70 Usable size of p
17bd9 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 age */.. assert
17bda 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
17bdb 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ow==0 );. asser
17bdc 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
17bdd 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
17bde 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17bdf 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 sert( nCell>=0 &
17be0 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c & nCell<=MX_CELL
17be1 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 (pPage->pBt) &&
17be2 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
17be3 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 Bt)<=5460 );. a
17be4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17be5 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17be6 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
17be7 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 ;.. /* Check th
17be8 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20 at the page has
17be9 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 just been zeroed
17bea 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a by zeroPage() *
17beb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
17bec 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 e->nCell==0 );.
17bed 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74 assert( get2byt
17bee 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d e(&data[hdr+5])=
17bef 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 =nUsable );.. p
17bf0 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b Cellptr = &data[
17bf1 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
17bf2 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 t + nCell*2];.
17bf3 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 cellbody = nUsab
17bf4 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c le;. for(i=nCel
17bf5 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b l-1; i>=0; i--){
17bf6 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d . pCellptr -=
17bf7 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79 2;. cellbody
17bf8 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 -= aSize[i];.
17bf9 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c put2byte(pCell
17bfa 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a ptr, cellbody);.
17bfb 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
17bfc 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 [cellbody], apCe
17bfd 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 ll[i], aSize[i])
17bfe 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 ;. }. put2byte
17bff 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e (&data[hdr+3], n
17c00 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 Cell);. put2byt
17c01 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
17c02 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 cellbody);. pPa
17c03 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 ge->nFree -= (nC
17c04 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 ell*2 + nUsable
17c05 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 - cellbody);. p
17c06 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 Page->nCell = (u
17c07 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 16)nCell;.}../*.
17c08 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
17c09 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 parameters dete
17c0a 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 rmine how many a
17c0b 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 djacent pages ge
17c0c 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e t involved.** in
17c0d 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 a balancing ope
17c0e 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 ration. NN is t
17c0f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 he number of nei
17c10 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
17c11 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 side.** of the
17c12 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 page that partic
17c13 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
17c14 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
17c15 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 . NB is the.**
17c16 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
17c17 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 pages that parti
17c18 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e cipate, includin
17c19 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 g the target pag
17c1a 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 e and.** NN neig
17c1b 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
17c1c 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 side..**.** The
17c1d 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 minimum value of
17c1e 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 NN is 1 (of cou
17c1f 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e rse). Increasin
17c20 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 g NN above 1.**
17c21 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 (to 2 or 3) give
17c22 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f s a modest impro
17c23 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 vement in SELECT
17c24 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 and DELETE perf
17c25 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 ormance.** in ex
17c26 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 change for a lar
17c27 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 ger degradation
17c28 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 in INSERT and UP
17c29 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 DATE performance
17c2a 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
17c2b 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 f NN appears to
17c2c 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 give the best re
17c2d 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a sults overall..*
17c2e 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 /.#define NN 1
17c2f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
17c30 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
17c31 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
17c32 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 of pPage */.#de
17c33 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 fine NB (NN*2+1)
17c34 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 /* Total p
17c35 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e ages involved in
17c36 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a the balance */.
17c37 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
17c38 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
17c39 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 CE./*.** This ve
17c3a 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 rsion of balance
17c3b 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 () handles the c
17c3c 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 ommon special ca
17c3d 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 se where.** a ne
17c3e 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 w entry is being
17c3f 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 inserted on the
17c40 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 extreme right-e
17c41 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 nd of the.** tre
17c42 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 e, in other word
17c43 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 s, when the new
17c44 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d entry will becom
17c45 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a e the largest.**
17c46 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 entry in the tr
17c47 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 ee..**.** Instea
17c48 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62 d of trying to b
17c49 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 alance the 3 rig
17c4a 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 ht-most leaf pag
17c4b 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 es, just add.**
17c4c 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 a new page to th
17c4d 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
17c4e 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e e and put the on
17c4f 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a e new entry in.*
17c50 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 * that page. Th
17c51 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 is leaves the ri
17c52 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 ght side of the
17c53 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a tree somewhat.**
17c54 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 unbalanced. Bu
17c55 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 t odds are that
17c56 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 we will be inser
17c57 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 ting new entries
17c58 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 .** at the end s
17c59 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 oon afterwards s
17c5a 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 o the nearly emp
17c5b 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 ty page will qui
17c5c 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e ckly.** fill up.
17c5d 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a On average..**
17c5e 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 .** pPage is the
17c5f 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 leaf page which
17c60 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f is the right-mo
17c61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 st page in the t
17c62 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 ree..** pParent
17c63 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 is its parent.
17c64 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 pPage must have
17c65 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f a single overflo
17c66 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 w entry.** which
17c67 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 is also the rig
17c68 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e ht-most entry on
17c69 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
17c6a 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 The pSpace buff
17c6b 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 er is used to st
17c6c 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ore a temporary
17c6d 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69 copy of the divi
17c6e 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74 der.** cell that
17c6f 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 will be inserte
17c70 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 d into pParent.
17c71 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73 Such a cell cons
17c72 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62 ists of a 4.** b
17c73 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
17c74 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61 followed by a va
17c75 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
17c76 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a teger. In other.
17c77 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73 ** words, at mos
17c78 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63 t 13 bytes. Henc
17c79 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 e the pSpace buf
17c7a 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a fer must be at.*
17c7b 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73 * least 13 bytes
17c7c 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
17c7d 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
17c7e 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70 quick(MemPage *p
17c7f 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20 Parent, MemPage
17c80 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61 *pPage, u8 *pSpa
17c81 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ce){. BtShared
17c82 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61 *const pBt = pPa
17c83 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42 ge->pBt; /* B
17c84 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a -Tree Database *
17c85 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 /. MemPage *pNe
17c86 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
17c87 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c /* Newl
17c88 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 y allocated page
17c89 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
17c8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c8b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
17c8c 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50 turn Code */. P
17c8d 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 gno pgnoNew;
17c8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c8f 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
17c90 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20 er of pNew */..
17c91 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17c92 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
17c93 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
17c94 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17c95 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17c96 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ble(pParent->pDb
17c97 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
17c98 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
17c99 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66 28 low==1 );.. if(
17c9a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 30 pPage->nCell<=0
17c9b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
17c9c 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a _CORRUPT_BKPT;..
17c9d 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 /* Allocate a
17c9e 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20 70 new page. This p
17c9f 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 age will become
17ca0 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69 6e the right-siblin
17ca1 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67 65 g of . ** pPage
17ca2 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65 6e . Make the paren
17ca3 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65 2c t page writable,
17ca4 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 so that the new
17ca5 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20 divider cell.
17ca6 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 ** may be insert
17ca7 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65 73 ed. If both thes
17ca8 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 e operations are
17ca9 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72 6f successful, pro
17caa 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 ceed.. */. rc
17cab 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
17cac 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 age(pBt, &pNew,
17cad 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 3b &pgnoNew, 0, 0);
17cae 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
17caf 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75 38 TE_OK ){.. u8
17cb0 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63 65 *pOut = &pSpace
17cb1 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 65 [4];. u8 *pCe
17cb2 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66 ll = pPage->aOvf
17cb3 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20 l[0].pCell;.
17cb4 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c u16 szCell = cel
17cb5 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
17cb6 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a pCell);. u8 *
17cb7 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65 pStop;.. asse
17cb8 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
17cb9 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77 Iswriteable(pNew
17cba 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
17cbb 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17cbc 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f >aData[0]==(PTF_
17cbd 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 INTKEY|PTF_LEAFD
17cbe 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b ATA|PTF_LEAF) );
17cbf 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e . zeroPage(pN
17cc0 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 ew, PTF_INTKEY|P
17cc1 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f TF_LEAFDATA|PTF_
17cc2 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 6d LEAF);. assem
17cc3 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c blePage(pNew, 1,
17cc4 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c &pCell, &szCell
17cc5 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
17cc6 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 is is an auto-va
17cc7 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 cuum database, u
17cc8 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
17cc9 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74 r map. ** wit
17cca 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 h entries for th
17ccb 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20 e new page, and
17ccc 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d any pointer from
17ccd 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c the . ** cel
17cce 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f l on the page to
17ccf 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
17cd0 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 e. If either of
17cd1 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65 these. ** ope
17cd2 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74 rations fails, t
17cd3 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 he return code i
17cd4 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63 s set, but the c
17cd5 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f ontents. ** o
17cd6 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 f the parent pag
17cd7 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69 e are still mani
17cd8 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63 pulated by thh c
17cd9 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a ode below.. *
17cda 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74 * That is Ok, at
17cdb 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 this point the
17cdc 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67 parent page is g
17cdd 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20 uaranteed to.
17cde 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ** be marked as
17cdf 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e dirty. Returnin
17ce0 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 g an error code
17ce1 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20 will cause a.
17ce2 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e ** rollback, un
17ce3 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 doing any change
17ce4 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61 s made to the pa
17ce5 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a rent page.. *
17ce6 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f /. if( ISAUTO
17ce7 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
17ce8 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
17ce9 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 gnoNew, PTRMAP_B
17cea 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 TREE, pParent->p
17ceb 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
17cec 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77 if( szCell>pNew
17ced 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->minLocal ){.
17cee 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f ptrmapPutO
17cef 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65 vflPtr(pNew, pCe
17cf0 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ll, &rc);.
17cf1 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
17cf2 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64 * Create a divid
17cf3 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72 er cell to inser
17cf4 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 t into pParent.
17cf5 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c The divider cell
17cf6 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73 . ** consists
17cf7 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67 of a 4-byte pag
17cf8 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61 e number (the pa
17cf9 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61 ge number of pPa
17cfa 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 ge) and. ** a
17cfb 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
17cfc 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63 key value (whic
17cfd 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 h must be the sa
17cfe 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a me value as the.
17cff 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b ** largest k
17d00 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20 ey on pPage)..
17d01 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66 **. ** To f
17d02 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 ind the largest
17d03 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61 key value on pPa
17d04 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74 ge, first find t
17d05 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20 he right-most .
17d06 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 ** cell on pP
17d07 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74 age. The first t
17d08 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 wo fields of thi
17d09 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a s cell are the .
17d0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65 ** record-le
17d0b 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65 ngth (a variable
17d0c 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 length integer
17d0d 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20 at most 32-bits
17d0e 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20 in size). **
17d0f 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75 and the key valu
17d10 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 e (a variable le
17d11 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61 ngth integer, ma
17d12 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65 y have any value
17d13 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 ).. ** The fi
17d14 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65 rst of the while
17d15 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f (...) loops belo
17d16 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65 w skips over the
17d17 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20 record-length.
17d18 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65 ** field. The
17d19 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e second while(..
17d1a 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74 .) loop copies t
17d1b 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f he key value fro
17d1c 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c m the. ** cel
17d1d 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20 l on pPage into
17d1e 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
17d1f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 r.. */. pC
17d20 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
17d21 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 Page, pPage->nCe
17d22 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70 ll-1);. pStop
17d23 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 = &pCell[9];.
17d24 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c while( (*(pCel
17d25 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43 l++)&0x80) && pC
17d26 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20 ell<pStop );.
17d27 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b pStop = &pCell[
17d28 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 9];. while( (
17d29 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70 (*(pOut++) = *(p
17d2a 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26 Cell++))&0x80) &
17d2b 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b & pCell<pStop );
17d2c 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 .. /* Insert
17d2d 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 the new divider
17d2e 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e cell into pParen
17d2f 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74 t. */. insert
17d30 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50 Cell(pParent, pP
17d31 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53 arent->nCell, pS
17d32 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74 pace, (int)(pOut
17d33 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20 -pSpace),.
17d34 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67 0, pPag
17d35 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a e->pgno, &rc);..
17d36 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72 /* Set the r
17d37 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 ight-child point
17d38 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f er of pParent to
17d39 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 point to the ne
17d3a 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70 w page. */. p
17d3b 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 ut4byte(&pParent
17d3c 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
17d3d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 >hdrOffset+8], p
17d3e 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 gnoNew);. .
17d3f 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72 /* Release the r
17d40 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
17d41 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 new page. */.
17d42 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 releasePage(pNe
17d43 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 w);. }.. retur
17d44 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
17d45 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 * SQLITE_OMIT_QU
17d46 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23 ICKBALANCE */..#
17d47 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 if 0./*.** This
17d48 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
17d49 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79 t contribute any
17d4a 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65 thing to the ope
17d4b 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ration of SQLite
17d4c 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 ..** it is somet
17d4d 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74 imes activated t
17d4e 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65 emporarily while
17d4f 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20 debugging code
17d50 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20 responsible .**
17d51 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e for setting poin
17d52 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e ter-map entries.
17d53 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
17d54 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
17d55 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65 MemPage **apPage
17d56 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 , int nPage){.
17d57 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28 int i, j;. for(
17d58 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b i=0; i<nPage; i+
17d59 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a +){. Pgno n;.
17d5a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65 u8 e;. Me
17d5b 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61 mPage *pPage = a
17d5c 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74 pPage[i];. Bt
17d5d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
17d5e 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 age->pBt;. as
17d5f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
17d60 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 nit );.. for(
17d61 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43 j=0; j<pPage->nC
17d62 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 ell; j++){.
17d63 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
17d64 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20 u8 *z;.
17d65 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e . z = fin
17d66 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b dCell(pPage, j);
17d67 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 . btreePars
17d68 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
17d69 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 z, &info);.
17d6a 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 if( info.iOverf
17d6b 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50 low ){. P
17d6c 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 gno ovfl = get4b
17d6d 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65 yte(&z[info.iOve
17d6e 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20 rflow]);.
17d6f 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
17d70 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 ovfl, &e, &n);.
17d71 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e assert( n
17d72 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 ==pPage->pgno &&
17d73 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 e==PTRMAP_OVERF
17d74 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a LOW1 );. }.
17d75 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
17d76 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
17d77 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 Pgno child = g
17d78 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20 et4byte(z);.
17d79 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17d7a 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
17d7b 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
17d7c 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e t( n==pPage->pgn
17d7d 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 o && e==PTRMAP_B
17d7e 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a TREE );. }.
17d7f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 }. if( !p
17d80 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
17d81 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d Pgno child =
17d82 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
17d83 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
17d84 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
17d85 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17d86 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e t, child, &e, &n
17d87 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
17d88 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 n==pPage->pgno
17d89 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 && e==PTRMAP_BTR
17d8a 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a EE );. }. }.
17d8b 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
17d8c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
17d8d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
17d8e 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f d to copy the co
17d8f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d ntents of the b-
17d90 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 tree node stored
17d91 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72 .** on page pFr
17d92 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 om to page pTo.
17d93 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61 If page pFrom wa
17d94 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 s not a leaf pag
17d95 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70 e, then.** the p
17d96 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
17d97 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c es for each chil
17d98 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74 d page are updat
17d99 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a ed so that the.*
17d9a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74 * parent page st
17d9b 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e ored in the poin
17d9c 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20 ter map is page
17d9d 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f pTo. If pFrom co
17d9e 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63 ntained.** any c
17d9f 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c ells with overfl
17da0 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73 ow page pointers
17da1 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 , then the corre
17da2 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72 sponding pointer
17da3 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 .** map entries
17da4 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64 are also updated
17da5 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72 so that the par
17da6 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65 ent page is page
17da7 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 pTo..**.** If p
17da8 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c From is currentl
17da9 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f y carrying any o
17daa 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65 verflow cells (e
17dab 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a ntries in the.**
17dac 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b 5d MemPage.aOvfl[]
17dad 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 72 array), they ar
17dae 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f 20 e not copied to
17daf 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 6f pTo. .**.** Befo
17db0 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 61 re returning, pa
17db1 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 74 ge pTo is reinit
17db2 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 74 ialized using bt
17db3 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a reeInitPage()..*
17db4 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d *.** The perform
17db5 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e ance of this fun
17db6 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69 ction is not cri
17db7 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c tical. It is onl
17db8 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 68 y used by .** th
17db9 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f e balance_shallo
17dba 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63 wer() and balanc
17dbb 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65 e_deeper() proce
17dbc 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f dures, neither o
17dbd 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63 f.** which are c
17dbe 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65 alled often unde
17dbf 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73 r normal circums
17dc0 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 tances..*/.stati
17dc1 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 43 c void copyNodeC
17dc2 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 2a ontent(MemPage *
17dc3 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 2a pFrom, MemPage *
17dc4 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a pTo, int *pRC){.
17dc5 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51 if( (*pRC)==SQ
17dc6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 42 LITE_OK ){. B
17dc7 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 20 tShared * const
17dc8 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 pBt = pFrom->pBt
17dc9 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 ;. u8 * const
17dca 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e aFrom = pFrom->
17dcb 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a 20 aData;. u8 *
17dcc 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f 2d const aTo = pTo-
17dcd 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 20 >aData;. int
17dce 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 3d const iFromHdr =
17dcf 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 65 pFrom->hdrOffse
17dd0 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 t;. int const
17dd1 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f 2d iToHdr = ((pTo-
17dd2 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 20 >pgno==1) ? 100
17dd3 3a 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 72 63 : 0);. int rc
17dd4 3b 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61 3b ;. int iData;
17dd5 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65 72 . . . asser
17dd6 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 t( pFrom->isInit
17dd7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
17dd8 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 pFrom->nFree>=iT
17dd9 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73 65 oHdr );. asse
17dda 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61 46 rt( get2byte(&aF
17ddb 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 rom[iFromHdr+5])
17ddc 3c 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a <=pBt->usableSiz
17ddd 65 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 e );. . /* C
17dde 6f 70 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e opy the b-tree n
17ddf 6f 64 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d ode content from
17de0 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 page pFrom to p
17de1 61 67 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 age pTo. */.
17de2 69 44 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 iData = get2byte
17de3 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 (&aFrom[iFromHdr
17de4 2b 35 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 +5]);. memcpy
17de5 28 26 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 (&aTo[iData], &a
17de6 46 72 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 From[iData], pBt
17de7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 ->usableSize-iDa
17de8 74 61 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ta);. memcpy(
17de9 26 61 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 &aTo[iToHdr], &a
17dea 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 From[iFromHdr],
17deb 70 46 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 pFrom->cellOffse
17dec 74 20 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 t + 2*pFrom->nCe
17ded 6c 6c 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 ll);. . /* R
17dee 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 einitialize page
17def 20 70 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 pTo so that the
17df0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
17df1 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 MemPage structu
17df2 72 65 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 re. ** match
17df3 74 68 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 the new data. Th
17df4 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
17df5 20 6f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75 of pTo can actu
17df6 61 6c 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0a ally fail under.
17df7 20 20 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62 ** fairly ob
17df8 73 63 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e scure circumstan
17df9 63 65 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 ces, even though
17dfa 20 69 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 it is a copy of
17dfb 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 initialized .
17dfc 20 20 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e ** page pFrom.
17dfd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d . */. pTo-
17dfe 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 >isInit = 0;.
17dff 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 rc = btreeInitP
17e00 61 67 65 28 70 54 6f 29 3b 0a 20 20 20 20 69 66 age(pTo);. if
17e01 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17e02 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 ){. *pRC =
17e03 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e rc;. return
17e04 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
17e05 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
17e06 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
17e07 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 base, update the
17e08 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
17e09 72 69 65 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 ries. ** for
17e0a 61 6e 79 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 any b-tree or ov
17e0b 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 erflow pages tha
17e0c 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 t pTo now contai
17e0d 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 ns the pointers
17e0e 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 to.. */. i
17e0f 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
17e10 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 ){. *pRC =
17e11 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
17e12 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a pTo);. }. }.
17e13 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
17e14 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 utine redistribu
17e15 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 tes cells on the
17e16 20 69 50 61 72 65 6e 74 49 64 78 27 74 68 20 63 iParentIdx'th c
17e17 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 0a hild of pParent.
17e18 2a 2a 20 28 68 65 72 65 61 66 74 65 72 20 22 74 ** (hereafter "t
17e19 68 65 20 70 61 67 65 22 29 20 61 6e 64 20 75 70 he page") and up
17e1a 20 74 6f 20 32 20 73 69 62 6c 69 6e 67 73 20 73 to 2 siblings s
17e1b 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73 o that all pages
17e1c 20 68 61 76 65 20 61 62 6f 75 74 20 74 68 65 0a have about the.
17e1d 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f ** same amount o
17e1e 66 20 66 72 65 65 20 73 70 61 63 65 2e 20 55 73 f free space. Us
17e1f 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 ually a single s
17e20 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 ibling on either
17e21 20 73 69 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 side of the.**
17e22 70 61 67 65 20 61 72 65 20 75 73 65 64 20 69 6e page are used in
17e23 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 the balancing,
17e24 74 68 6f 75 67 68 20 62 6f 74 68 20 73 69 62 6c though both sibl
17e25 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 ings might come
17e26 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 from one.** side
17e27 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
17e28 74 68 65 20 66 69 72 73 74 20 6f 72 20 6c 61 73 the first or las
17e29 74 20 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 t child of its p
17e2a 61 72 65 6e 74 2e 20 49 66 20 74 68 65 20 70 61 arent. If the pa
17e2b 67 65 20 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 ge .** has fewer
17e2c 20 74 68 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 than 2 siblings
17e2d 20 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 (something whic
17e2e 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 h can only happe
17e2f 6e 20 69 66 20 74 68 65 20 70 61 67 65 0a 2a 2a n if the page.**
17e30 20 69 73 20 61 20 72 6f 6f 74 20 70 61 67 65 20 is a root page
17e31 6f 72 20 61 20 63 68 69 6c 64 20 6f 66 20 61 20 or a child of a
17e32 72 6f 6f 74 20 70 61 67 65 29 20 74 68 65 6e 20 root page) then
17e33 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69 all available si
17e34 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 blings.** partic
17e35 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
17e36 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 ancing..**.** Th
17e37 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c e number of sibl
17e38 69 6e 67 73 20 6f 66 20 74 68 65 20 70 61 67 65 ings of the page
17e39 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 might be increa
17e3a 73 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 sed or decreased
17e3b 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 by .** one or t
17e3c 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 wo in an effort
17e3d 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 to keep pages ne
17e3e 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f arly full but no
17e3f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a t over full. .**
17e40 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 .** Note that wh
17e41 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
17e42 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 is called, some
17e43 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 of the cells on
17e44 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 the page.** migh
17e45 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 t not actually b
17e46 65 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 e stored in MemP
17e47 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 age.aData[]. Thi
17e48 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 s can happen.**
17e49 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f if the page is o
17e4a 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f verfull. This ro
17e4b 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 utine ensures th
17e4c 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c at all cells all
17e4d 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 ocated.** to the
17e4e 20 70 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 page and its si
17e4f 62 6c 69 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 blings fit into
17e50 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 MemPage.aData[]
17e51 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
17e52 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 ..**.** In the c
17e53 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 ourse of balanci
17e54 6e 67 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 ng the page and
17e55 69 74 73 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 its siblings, ce
17e56 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e lls may be.** in
17e57 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 serted into or r
17e58 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 emoved from the
17e59 70 61 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 parent page (pPa
17e5a 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a rent). Doing so.
17e5b 2a 2a 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 ** may cause the
17e5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 parent page to
17e5d 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 become overfull
17e5e 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 or underfull. If
17e5f 20 74 68 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 this.** happens
17e60 2c 20 69 74 20 69 73 20 74 68 65 20 72 65 73 70 , it is the resp
17e61 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 onsibility of th
17e62 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f e caller to invo
17e63 6b 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a ke the correct.*
17e64 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 * balancing rout
17e65 69 6e 65 20 74 6f 20 66 69 78 20 74 68 69 73 20 ine to fix this
17e66 70 72 6f 62 6c 65 6d 20 28 73 65 65 20 74 68 65 problem (see the
17e67 20 62 61 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 balance() routi
17e68 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ne). .**.** If t
17e69 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c his routine fail
17e6a 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e s for any reason
17e6b 2c 20 69 74 20 6d 69 67 68 74 20 6c 65 61 76 65 , it might leave
17e6c 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
17e6d 20 69 6e 20 61 20 63 6f 72 72 75 70 74 65 64 20 in a corrupted
17e6e 73 74 61 74 65 2e 20 53 6f 20 69 66 20 74 68 69 state. So if thi
17e6f 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c s routine fails,
17e70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 68 the database sh
17e71 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 ould.** be rolle
17e72 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
17e73 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
17e74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
17e75 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 n, aOvflSpace, i
17e76 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
17e77 0a 2a 2a 20 62 75 66 66 65 72 20 62 69 67 20 65 .** buffer big e
17e78 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e nough to hold on
17e79 65 20 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65 e page. If while
17e7a 20 69 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 inserting cells
17e7b 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 into the parent
17e7c 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e .** page (pParen
17e7d 74 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 t) the parent pa
17e7e 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 ge becomes overf
17e7f 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72 ull, this buffer
17e80 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 is.** used to s
17e81 74 6f 72 65 20 74 68 65 20 70 61 72 65 6e 74 27 tore the parent'
17e82 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 s overflow cells
17e83 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 20 66 . Because this f
17e84 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a unction inserts.
17e85 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 ** a maximum of
17e86 66 6f 75 72 20 64 69 76 69 64 65 72 20 63 65 6c four divider cel
17e87 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 ls into the pare
17e88 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 74 68 65 nt page, and the
17e89 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 maximum.** size
17e8a 20 6f 66 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 of a cell store
17e8b 64 20 77 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 d within an inte
17e8c 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 rnal node is alw
17e8d 61 79 73 20 6c 65 73 73 20 74 68 61 6e 20 31 2f ays less than 1/
17e8e 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 4.** of the page
17e8f 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c -size, the aOvfl
17e90 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72 20 69 Space[] buffer i
17e91 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 s guaranteed to
17e92 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 be large.** enou
17e93 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 gh for all overf
17e94 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a low cells..**.**
17e95 20 49 66 20 61 4f 76 66 6c 53 70 61 63 65 20 69 If aOvflSpace i
17e96 73 20 73 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 s set to a null
17e97 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 pointer, this fu
17e98 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a nction returns .
17e99 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e ** SQLITE_NOMEM.
17e9a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
17e9b 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a alance_nonroot(.
17e9c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 MemPage *pPare
17e9d 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nt,
17e9e 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61 67 65 /* Parent page
17e9f 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 62 65 69 of siblings bei
17ea0 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 ng balanced */.
17ea1 20 69 6e 74 20 69 50 61 72 65 6e 74 49 64 78 2c int iParentIdx,
17ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17ea3 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 22 74 68 /* Index of "th
17ea4 65 20 70 61 67 65 22 20 69 6e 20 70 50 61 72 65 e page" in pPare
17ea5 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61 4f 76 66 nt */. u8 *aOvf
17ea6 6c 53 70 61 63 65 2c 20 20 20 20 20 20 20 20 20 lSpace,
17ea7 20 20 20 20 20 20 20 20 2f 2a 20 70 61 67 65 2d /* page-
17ea8 73 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 size bytes of sp
17ea9 61 63 65 20 66 6f 72 20 70 61 72 65 6e 74 20 6f ace for parent o
17eaa 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 vfl */. int isR
17eab 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 oot
17eac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
17ead 20 69 66 20 70 50 61 72 65 6e 74 20 69 73 20 61 if pParent is a
17eae 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a 29 7b root-page */.){
17eaf 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
17eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17eb1 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74 /* The whole dat
17eb2 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e abase */. int n
17eb3 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 Cell = 0;
17eb4 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
17eb5 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 r of cells in ap
17eb6 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Cell[] */. int
17eb7 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 nMaxCells = 0;
17eb8 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f /* Allo
17eb9 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 70 cated size of ap
17eba 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 Cell, szCell, aF
17ebb 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e rom. */. int nN
17ebc 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ew = 0;
17ebd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
17ebe 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4e of pages in apN
17ebf 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f ew[] */. int nO
17ec0 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ld;
17ec1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
17ec2 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4f of pages in apO
17ec3 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c ld[] */. int i,
17ec4 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20 j, k;
17ec5 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
17ec6 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 ounters */. int
17ec7 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 20 20 nxDiv;
17ec8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 /* Nex
17ec9 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 20 69 t divider slot i
17eca 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c n pParent->aCell
17ecb 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d [] */. int rc =
17ecc 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
17ecd 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 /* The retu
17ece 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 rn code */. u16
17ecf 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
17ed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20 69 /* 4 i
17ed1 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 f pPage is a lea
17ed2 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a f. 0 if not */.
17ed3 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 int leafData;
17ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17ed5 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 65 20 * True if pPage
17ed6 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 20 4c is a leaf of a L
17ed7 45 41 46 44 41 54 41 20 74 72 65 65 20 2a 2f 0a EAFDATA tree */.
17ed8 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 61 63 int usableSpac
17ed9 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
17eda 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 67 65 * Bytes in pPage
17edb 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 61 64 beyond the head
17edc 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 er */. int page
17edd 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
17ede 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 /* Value of
17edf 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d pPage->aData[0]
17ee0 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 */. int subtot
17ee1 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 al;
17ee2 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f /* Subtotal o
17ee3 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 f bytes in cells
17ee4 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a on one page */.
17ee5 20 20 69 6e 74 20 69 53 70 61 63 65 31 20 3d 20 int iSpace1 =
17ee6 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
17ee7 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62 * First unused b
17ee8 79 74 65 20 6f 66 20 61 53 70 61 63 65 31 5b 5d yte of aSpace1[]
17ee9 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 */. int iOvflS
17eea 70 61 63 65 20 3d 20 30 3b 20 20 20 20 20 20 20 pace = 0;
17eeb 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 /* First unus
17eec 65 64 20 62 79 74 65 20 6f 66 20 61 4f 76 66 6c ed byte of aOvfl
17eed 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 Space[] */. int
17eee 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20 szScratch;
17eef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
17ef0 65 20 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d e of scratch mem
17ef1 6f 72 79 20 72 65 71 75 65 73 74 65 64 20 2a 2f ory requested */
17ef2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c . MemPage *apOl
17ef3 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 d[NB];
17ef4 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 /* pPage and up
17ef5 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 to two siblings
17ef6 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 */. MemPage *ap
17ef7 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 Copy[NB];
17ef8 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70 /* Private cop
17ef9 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 ies of apOld[] p
17efa 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ages */. MemPag
17efb 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 e *apNew[NB+2];
17efc 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
17efd 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62 and up to NB sib
17efe 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61 lings after bala
17eff 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70 ncing */. u8 *p
17f00 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 Right;
17f01 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74 /* Locat
17f02 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 ion in parent of
17f03 20 72 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 70 right-sibling p
17f04 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a ointer */. u8 *
17f05 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20 apDiv[NB-1];
17f06 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 /* Divi
17f07 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 der cells in pPa
17f08 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e rent */. int cn
17f09 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 tNew[NB+2];
17f0a 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
17f0b 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 in aCell[] of ce
17f0c 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20 70 61 ll after i-th pa
17f0d 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 ge */. int szNe
17f0e 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
17f0f 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 /* Combined
17f10 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 size of cells p
17f11 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 lace on i-th pag
17f12 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 e */. u8 **apCe
17f13 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ll = 0;
17f14 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 /* All cells
17f15 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 begin balanced
17f16 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c */. u16 *szCell
17f17 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17f18 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 /* Local size
17f19 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 of all cells in
17f1a 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 apCell[] */. u8
17f1b 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20 20 *aSpace1;
17f1c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
17f1d 61 63 65 20 66 6f 72 20 63 6f 70 69 65 73 20 6f ace for copies o
17f1e 66 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 f dividers cells
17f1f 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
17f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f21 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 72 20 74 /* Temp var t
17f22 6f 20 73 74 6f 72 65 20 61 20 70 61 67 65 20 6e o store a page n
17f23 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70 umber in */.. p
17f24 42 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42 Bt = pParent->pB
17f25 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
17f26 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17f27 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
17f28 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17f29 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
17f2a 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 (pParent->pDbPag
17f2b 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54 e) );..#if 0. T
17f2c 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
17f2d 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 63 68 begin page %d ch
17f2e 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 ild of %d\n", pP
17f2f 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 age->pgno, pPare
17f30 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64 nt->pgno));.#end
17f31 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 if.. /* At this
17f32 20 70 6f 69 6e 74 20 70 50 61 72 65 6e 74 20 6d point pParent m
17f33 61 79 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 ay have at most
17f34 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c one overflow cel
17f35 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74 l. And if. ** t
17f36 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c his overflow cel
17f37 6c 20 69 73 20 70 72 65 73 65 6e 74 2c 20 69 74 l is present, it
17f38 20 6d 75 73 74 20 62 65 20 74 68 65 20 63 65 6c must be the cel
17f39 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64 l with . ** ind
17f3a 65 78 20 69 50 61 72 65 6e 74 49 64 78 2e 20 54 ex iParentIdx. T
17f3b 68 69 73 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d his scenario com
17f3c 65 73 20 61 62 6f 75 74 20 77 68 65 6e 20 74 68 es about when th
17f3d 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a is function. **
17f3e 20 69 73 20 63 61 6c 6c 65 64 20 28 69 6e 64 69 is called (indi
17f3f 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c rectly) from sql
17f40 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
17f41 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ).. */. assert
17f42 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 ( pParent->nOver
17f43 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 flow==0 || pPare
17f44 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 nt->nOverflow==1
17f45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
17f46 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
17f47 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e ==0 || pParent->
17f48 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 69 50 aOvfl[0].idx==iP
17f49 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a 20 20 69 arentIdx );.. i
17f4a 66 28 20 21 61 4f 76 66 6c 53 70 61 63 65 20 29 f( !aOvflSpace )
17f4b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
17f4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
17f4d 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 69 /* Find the si
17f4e 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 62 bling pages to b
17f4f 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63 alance. Also loc
17f50 61 74 65 20 74 68 65 20 63 65 6c 6c 73 20 69 6e ate the cells in
17f51 20 70 50 61 72 65 6e 74 20 0a 20 20 2a 2a 20 74 pParent . ** t
17f52 68 61 74 20 64 69 76 69 64 65 20 74 68 65 20 73 hat divide the s
17f53 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74 74 65 iblings. An atte
17f54 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66 mpt is made to f
17f55 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 ind NN siblings
17f56 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 on . ** either
17f57 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20 4d side of pPage. M
17f58 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 ore siblings are
17f59 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 taken from one
17f5a 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 0a side, however, .
17f5b 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 ** if there ar
17f5c 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e 4e 20 e fewer than NN
17f5d 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 65 20 siblings on the
17f5e 6f 74 68 65 72 20 73 69 64 65 2e 20 49 66 20 70 other side. If p
17f5f 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 Parent. ** has
17f60 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68 69 6c NB or fewer chil
17f61 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 dren then all ch
17f62 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72 65 6e ildren of pParen
17f63 74 20 61 72 65 20 74 61 6b 65 6e 2e 20 20 0a 20 t are taken. .
17f64 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f **. ** This lo
17f65 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73 20 74 68 op also drops th
17f66 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 e divider cells
17f67 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 from the parent
17f68 70 61 67 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 page. This. **
17f69 77 61 79 2c 20 74 68 65 20 72 65 6d 61 69 6e 64 way, the remaind
17f6a 65 72 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 er of the functi
17f6b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 on does not have
17f6c 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 61 6e to deal with an
17f6d 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 y. ** overflow
17f6e 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 72 cells in the par
17f6f 65 6e 74 20 70 61 67 65 2c 20 73 69 6e 63 65 20 ent page, since
17f70 69 66 20 61 6e 79 20 65 78 69 73 74 65 64 20 74 if any existed t
17f71 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 hey will. ** ha
17f72 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
17f73 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 removed.. */.
17f74 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 i = pParent->nOv
17f75 65 72 66 6c 6f 77 20 2b 20 70 50 61 72 65 6e 74 erflow + pParent
17f76 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 28 20 69 ->nCell;. if( i
17f77 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 <2 ){. nxDiv
17f78 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 = 0;. nOld =
17f79 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 i+1;. }else{.
17f7a 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20 20 20 20 nOld = 3;.
17f7b 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d if( iParentIdx==
17f7c 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
17f7d 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78 44 69 . nxDi
17f7e 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 v = 0;. }else
17f7f 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d if( iParentIdx=
17f80 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78 44 69 =i ){. nxDi
17f81 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20 7d 65 6c v = i-2;. }el
17f82 73 65 7b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 se{. nxDiv
17f83 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a = iParentIdx-1;.
17f84 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 32 3b }. i = 2;
17f85 0a 20 20 7d 0a 20 20 69 66 28 20 28 69 2b 6e 78 . }. if( (i+nx
17f86 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 Div-pParent->nOv
17f87 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72 65 6e 74 erflow)==pParent
17f88 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 70 ->nCell ){. p
17f89 52 69 67 68 74 20 3d 20 26 70 50 61 72 65 6e 74 Right = &pParent
17f8a 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
17f8b 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 3b 0a 20 >hdrOffset+8];.
17f8c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 69 67 }else{. pRig
17f8d 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ht = findCell(pP
17f8e 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 arent, i+nxDiv-p
17f8f 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
17f90 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e 6f 20 3d w);. }. pgno =
17f91 20 67 65 74 34 62 79 74 65 28 70 52 69 67 68 74 get4byte(pRight
17f92 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b );. while( 1 ){
17f93 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 . rc = getAnd
17f94 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 InitPage(pBt, pg
17f95 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a no, &apOld[i]);.
17f96 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
17f97 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f 6c 64 memset(apOld
17f98 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a 65 6f , 0, (i+1)*sizeo
17f99 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a 20 20 f(MemPage*));.
17f9a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
17f9b 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a _cleanup;. }.
17f9c 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d nMaxCells +=
17f9d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 1+apOld[i]->nCe
17f9e 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 ll+apOld[i]->nOv
17f9f 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 erflow;. if(
17fa0 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 65 61 6b (i--)==0 ) break
17fa1 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b 6e 78 44 ;.. if( i+nxD
17fa2 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 61 4f 76 iv==pParent->aOv
17fa3 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20 70 50 61 fl[0].idx && pPa
17fa4 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 rent->nOverflow
17fa5 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 ){. apDiv[i
17fa6 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76 ] = pParent->aOv
17fa7 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 fl[0].pCell;.
17fa8 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17fa9 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 te(apDiv[i]);.
17faa 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 szNew[i] = c
17fab 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 ellSizePtr(pPare
17fac 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20 nt, apDiv[i]);.
17fad 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 6e 4f pParent->nO
17fae 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 verflow = 0;.
17faf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 70 }else{. ap
17fb0 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c Div[i] = findCel
17fb1 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 l(pParent, i+nxD
17fb2 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 iv-pParent->nOve
17fb3 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20 20 70 67 rflow);. pg
17fb4 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 no = get4byte(ap
17fb5 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 Div[i]);. s
17fb6 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 zNew[i] = cellSi
17fb7 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 zePtr(pParent, a
17fb8 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20 20 20 20 pDiv[i]);..
17fb9 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 63 65 6c /* Drop the cel
17fba 6c 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e l from the paren
17fbb 74 20 70 61 67 65 2e 20 61 70 44 69 76 5b 69 5d t page. apDiv[i]
17fbc 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f still points to
17fbd 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 65 . ** the ce
17fbe 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 ll within the pa
17fbf 72 65 6e 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 rent, even thoug
17fc0 68 20 69 74 20 68 61 73 20 62 65 65 6e 20 64 72 h it has been dr
17fc1 6f 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 opped.. **
17fc2 54 68 69 73 20 69 73 20 73 61 66 65 20 62 65 63 This is safe bec
17fc3 61 75 73 65 20 64 72 6f 70 70 69 6e 67 20 61 20 ause dropping a
17fc4 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65 72 77 72 cell only overwr
17fc5 69 74 65 73 20 74 68 65 20 66 69 72 73 74 0a 20 ites the first.
17fc6 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 ** four byt
17fc7 65 73 20 6f 66 20 69 74 2c 20 61 6e 64 20 74 68 es of it, and th
17fc8 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 is function does
17fc9 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 66 69 not need the fi
17fca 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 rst. ** fou
17fcb 72 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 64 r bytes of the d
17fcc 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 53 6f 20 ivider cell. So
17fcd 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73 the pointer is s
17fce 61 66 65 20 74 6f 20 75 73 65 0a 20 20 20 20 20 afe to use.
17fcf 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e 20 20 0a ** later on. .
17fd0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
17fd1 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69 74 65 20 * Unless SQLite
17fd2 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e 20 73 is compiled in s
17fd3 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64 ecure-delete mod
17fd4 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c e. In this case,
17fd5 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 72 . ** the dr
17fd6 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65 opCell() routine
17fd7 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 will overwrite
17fd8 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 the entire cell
17fd9 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20 with zeroes..
17fda 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 ** In this ca
17fdb 73 65 2c 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 se, temporarily
17fdc 63 6f 70 79 20 74 68 65 20 63 65 6c 6c 20 69 6e copy the cell in
17fdd 74 6f 20 74 68 65 20 61 4f 76 66 6c 53 70 61 63 to the aOvflSpac
17fde 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 e[]. ** buf
17fdf 66 65 72 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 fer. It will be
17fe0 63 6f 70 69 65 64 20 6f 75 74 20 61 67 61 69 6e copied out again
17fe1 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 as soon as the
17fe2 61 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72 0a aSpace[] buffer.
17fe3 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f ** is allo
17fe4 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69 66 64 65 cated. */.#ifde
17fe5 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f f SQLITE_SECURE_
17fe6 44 45 4c 45 54 45 0a 20 20 20 20 20 20 6d 65 6d DELETE. mem
17fe7 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63 65 5b cpy(&aOvflSpace[
17fe8 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 apDiv[i]-pParent
17fe9 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44 69 76 5b ->aData], apDiv[
17fea 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 i], szNew[i]);.
17feb 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 apDiv[i] =
17fec 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44 69 &aOvflSpace[apDi
17fed 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 v[i]-pParent->aD
17fee 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 ata];.#endif.
17fef 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 dropCell(pPar
17ff0 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 ent, i+nxDiv-pPa
17ff1 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c rent->nOverflow,
17ff2 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b szNew[i], &rc);
17ff3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
17ff4 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 Make nMaxCells
17ff5 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 a multiple of 4
17ff6 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 in order to pres
17ff7 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a erve 8-byte. **
17ff8 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 alignment */.
17ff9 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 nMaxCells = (nMa
17ffa 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a xCells + 3)&~3;.
17ffb 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 . /*. ** Alloc
17ffc 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 ate space for me
17ffd 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a mory structures.
17ffe 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e */. k = pBt->
17fff 70 61 67 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44 pageSize + ROUND
18000 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 8(sizeof(MemPage
18001 29 29 3b 0a 20 20 73 7a 53 63 72 61 74 63 68 20 ));. szScratch
18002 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c =. nMaxCel
18003 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 ls*sizeof(u8*)
18004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18005 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a /* apCell *
18006 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c /. + nMaxCel
18007 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 20 ls*sizeof(u16)
18008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18009 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a /* szCell *
1800a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 /. + pBt->pa
1800b 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 geSize
1800c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1800d 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20 /* aSpace1
1800e 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 */. + k*nOld
1800f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
18010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18011 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
18012 70 69 65 73 20 28 61 70 43 6f 70 79 29 20 2a 2f pies (apCopy) */
18013 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 . apCell = sqli
18014 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 te3ScratchMalloc
18015 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b 20 0a ( szScratch ); .
18016 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 if( apCell==0
18017 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
18018 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f TE_NOMEM;. go
18019 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
1801a 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c up;. }. szCell
1801b 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c = (u16*)&apCell
1801c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 [nMaxCells];. a
1801d 53 70 61 63 65 31 20 3d 20 28 75 38 2a 29 26 73 Space1 = (u8*)&s
1801e 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d zCell[nMaxCells]
1801f 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 ;. assert( EIGH
18020 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
18021 28 61 53 70 61 63 65 31 29 20 29 3b 0a 0a 20 20 (aSpace1) );..
18022 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 /*. ** Load poi
18023 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c nters to all cel
18024 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 ls on sibling pa
18025 67 65 73 20 61 6e 64 20 74 68 65 20 64 69 76 69 ges and the divi
18026 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 der cells. ** i
18027 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 nto the local ap
18028 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d Cell[] array. M
18029 61 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 ake copies of th
1802a 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a e divider cells.
1802b 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 ** into space
1802c 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 53 obtained from aS
1802d 70 61 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f pace1[] and remo
1802e 76 65 20 74 68 65 20 74 68 65 20 64 69 76 69 64 ve the the divid
1802f 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 er Cells. ** fr
18030 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a om pParent.. **
18031 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 69 62 . ** If the sib
18032 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 lings are on lea
18033 66 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74 68 f pages, then th
18034 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 e child pointers
18035 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 69 76 of the. ** div
18036 69 64 65 72 20 63 65 6c 6c 73 20 61 72 65 20 73 ider cells are s
18037 74 72 69 70 70 65 64 20 66 72 6f 6d 20 74 68 65 tripped from the
18038 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 cells before th
18039 65 79 20 61 72 65 20 63 6f 70 69 65 64 0a 20 20 ey are copied.
1803a 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 31 5b ** into aSpace1[
1803b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c ]. In this way,
1803c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 all cells in ap
1803d 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f Cell[] are witho
1803e 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f ut. ** child po
1803f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 69 62 6c inters. If sibl
18040 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 ings are not lea
18041 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65 ves, then all ce
18042 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c ll in. ** apCel
18043 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68 69 6c l[] include chil
18044 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 45 69 74 d pointers. Eit
18045 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c her way, all cel
18046 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 ls in apCell[].
18047 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20 ** are alike..
18048 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72 **. ** leafCor
18049 72 65 63 74 69 6f 6e 3a 20 20 34 20 69 66 20 70 rection: 4 if p
1804a 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 Page is a leaf.
1804b 20 30 20 69 66 20 70 50 61 67 65 20 69 73 20 6e 0 if pPage is n
1804c 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 ot a leaf.. **
1804d 20 20 20 20 20 20 6c 65 61 66 44 61 74 61 3a 20 leafData:
1804e 20 31 20 69 66 20 70 50 61 67 65 20 68 6f 6c 64 1 if pPage hold
1804f 73 20 6b 65 79 2b 64 61 74 61 20 61 6e 64 20 70 s key+data and p
18050 50 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c Parent holds onl
18051 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6c y keys.. */. l
18052 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 3d 20 eafCorrection =
18053 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61 66 2a 34 apOld[0]->leaf*4
18054 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d 20 61 ;. leafData = a
18055 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44 61 74 61 pOld[0]->hasData
18056 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
18057 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 Old; i++){. i
18058 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20 20 0a 20 nt limit;. .
18059 20 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 /* Before doi
1805a 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 ng anything else
1805b 2c 20 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 , take a copy of
1805c 20 74 68 65 20 69 27 74 68 20 6f 72 69 67 69 6e the i'th origin
1805d 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a al sibling. *
1805e 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 * The rest of th
1805f 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c is function will
18060 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 use data from t
18061 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 he copies rather
18062 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 . ** that the
18063 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 original pages
18064 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e since the origin
18065 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 al pages will be
18066 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 in the. ** p
18067 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 rocess of being
18068 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 2a 2f overwritten. */
18069 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f . MemPage *pO
1806a 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d ld = apCopy[i] =
1806b 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 53 70 61 (MemPage*)&aSpa
1806c 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a ce1[pBt->pageSiz
1806d 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20 20 6d 65 e + k*i];. me
1806e 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 4f 6c 64 mcpy(pOld, apOld
1806f 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50 [i], sizeof(MemP
18070 61 67 65 29 29 3b 0a 20 20 20 20 70 4f 6c 64 2d age));. pOld-
18071 3e 61 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 >aData = (void*)
18072 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20 20 6d 65 &pOld[1];. me
18073 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44 61 74 61 mcpy(pOld->aData
18074 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 , apOld[i]->aDat
18075 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 a, pBt->pageSize
18076 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74 20 3d 20 );.. limit =
18077 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 pOld->nCell+pOld
18078 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 ->nOverflow;.
18079 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 for(j=0; j<limi
1807a 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 t; j++){. a
1807b 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 ssert( nCell<nMa
1807c 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
1807d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 apCell[nCell] =
1807e 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
1807f 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 (pOld, j);.
18080 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d szCell[nCell] =
18081 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c cellSizePtr(pOl
18082 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d d, apCell[nCell]
18083 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b );. nCell++
18084 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
18085 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 i<nOld-1 && !lea
18086 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20 75 31 fData){. u1
18087 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65 6 sz = (u16)szNe
18088 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a w[i];. u8 *
18089 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 pTemp;. ass
1808a 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 ert( nCell<nMaxC
1808b 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 73 7a ells );. sz
1808c 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a Cell[nCell] = sz
1808d 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 ;. pTemp =
1808e 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31 &aSpace1[iSpace1
1808f 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63 65 31 ];. iSpace1
18090 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 += sz;. as
18091 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 sert( sz<=pBt->p
18092 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 ageSize/4 );.
18093 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63 assert( iSpac
18094 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a e1<=pBt->pageSiz
18095 65 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 e );. memcp
18096 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 5b 69 y(pTemp, apDiv[i
18097 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 61 70 ], sz);. ap
18098 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 Cell[nCell] = pT
18099 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 emp+leafCorrecti
1809a 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 on;. assert
1809b 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e ( leafCorrection
1809c 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f 72 72 65 ==0 || leafCorre
1809d 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 ction==4 );.
1809e 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 szCell[nCell]
1809f 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 = szCell[nCell]
180a0 2d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e - leafCorrection
180a1 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4f 6c ;. if( !pOl
180a2 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 d->leaf ){.
180a3 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 assert( leafC
180a4 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a orrection==0 );.
180a5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
180a6 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 3d pOld->hdrOffset=
180a7 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a =0 );. /*
180a8 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 The right point
180a9 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 er of the child
180aa 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 page pOld become
180ab 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 s the left.
180ac 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 ** pointer of
180ad 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
180ae 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d l */. mem
180af 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c cpy(apCell[nCell
180b0 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b ], &pOld->aData[
180b1 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 7d 65 8], 4);. }e
180b2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 lse{. ass
180b3 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 ert( leafCorrect
180b4 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 ion==4 );.
180b5 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e 43 65 if( szCell[nCe
180b6 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 ll]<4 ){.
180b7 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c /* Do not all
180b8 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 ow any cells sma
180b9 6c 6c 65 72 20 74 68 61 6e 20 34 20 62 79 74 65 ller than 4 byte
180ba 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 s. */.
180bb 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 szCell[nCell] =
180bc 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 4;. }.
180bd 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65 6c 6c }. nCell
180be 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ++;. }. }..
180bf 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 /*. ** Figure
180c0 6f 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f out the number o
180c1 66 20 70 61 67 65 73 20 6e 65 65 64 65 64 20 74 f pages needed t
180c2 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c o hold all nCell
180c3 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f cells.. ** Sto
180c4 72 65 20 74 68 69 73 20 6e 75 6d 62 65 72 20 69 re this number i
180c5 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d n "k". Also com
180c6 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 pute szNew[] whi
180c7 63 68 20 69 73 20 74 68 65 20 74 6f 74 61 6c 0a ch is the total.
180c8 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c ** size of all
180c9 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d cells on the i-
180ca 74 68 20 70 61 67 65 20 61 6e 64 20 63 6e 74 4e th page and cntN
180cb 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 ew[] which is th
180cc 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 e index. ** in
180cd 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 apCell[] of the
180ce 63 65 6c 6c 20 74 68 61 74 20 64 69 76 69 64 65 cell that divide
180cf 73 20 70 61 67 65 20 69 20 66 72 6f 6d 20 70 61 s page i from pa
180d0 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 ge i+1. . ** c
180d1 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 ntNew[k] should
180d2 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a equal nCell.. *
180d3 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f *. ** Values co
180d4 6d 70 75 74 65 64 20 62 79 20 74 68 69 73 20 62 mputed by this b
180d5 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 lock:. **. **
180d6 20 20 20 20 20 20 20 20 20 20 6b 3a 20 54 68 65 k: The
180d7 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
180d8 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 sibling pages.
180d9 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a ** szNew[i]:
180da 20 53 70 61 63 65 64 20 75 73 65 64 20 6f 6e 20 Spaced used on
180db 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 the i-th sibling
180dc 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e page.. ** cn
180dd 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 tNew[i]: Index i
180de 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 n apCell[] and s
180df 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 zCell[] for the
180e0 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 first cell to.
180e1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
180e2 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 the right of the
180e3 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 i-th sibling pa
180e4 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 ge.. ** usableS
180e5 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 pace: Number of
180e6 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 bytes of space a
180e7 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 vailable on each
180e8 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a sibling.. ** .
180e9 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 */. usableSpa
180ea 63 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ce = pBt->usable
180eb 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 Size - 12 + leaf
180ec 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f Correction;. fo
180ed 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 r(subtotal=k=i=0
180ee 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ; i<nCell; i++){
180ef 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e . assert( i<n
180f0 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
180f1 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 subtotal += szCe
180f2 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 ll[i] + 2;. i
180f3 66 28 20 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 f( subtotal > us
180f4 61 62 6c 65 53 70 61 63 65 20 29 7b 0a 20 20 20 ableSpace ){.
180f5 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 szNew[k] = su
180f6 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b btotal - szCell[
180f7 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 i];. cntNew
180f8 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 [k] = i;. i
180f9 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 20 69 f( leafData ){ i
180fa 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73 75 62 74 --; }. subt
180fb 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 otal = 0;.
180fc 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 6b k++;. if( k
180fd 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d 20 53 51 >NB+1 ){ rc = SQ
180fe 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 20 67 6f LITE_CORRUPT; go
180ff 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
18100 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a up; }. }. }.
18101 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 szNew[k] = sub
18102 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b total;. cntNew[
18103 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b k] = nCell;. k+
18104 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 +;.. /*. ** Th
18105 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74 e packing comput
18106 65 64 20 62 79 20 74 68 65 20 70 72 65 76 69 6f ed by the previo
18107 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 69 61 73 us block is bias
18108 65 64 20 74 6f 77 61 72 64 20 74 68 65 20 73 69 ed toward the si
18109 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 blings. ** on t
1810a 68 65 20 6c 65 66 74 20 73 69 64 65 2e 20 20 54 he left side. T
1810b 68 65 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 he left siblings
1810c 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65 61 72 are always near
1810d 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 ly full, while t
1810e 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f he. ** right-mo
1810f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 st sibling might
18110 20 62 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 be nearly empty
18111 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 . This block of
18112 20 63 6f 64 65 20 61 74 74 65 6d 70 74 73 0a 20 code attempts.
18113 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20 74 68 ** to adjust th
18114 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 e packing of sib
18115 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 61 20 62 lings to get a b
18116 65 74 74 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20 etter balance..
18117 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 61 64 **. ** This ad
18118 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65 justment is more
18119 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a than an optimiz
1811a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b ation. The pack
1811b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 68 74 0a ing above might.
1811c 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f ** be so out o
1811d 66 20 62 61 6c 61 6e 63 65 20 61 73 20 74 6f 20 f balance as to
1811e 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 be illegal. For
1811f 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 72 69 example, the ri
18120 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 ght-most. ** si
18121 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 63 bling might be c
18122 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e ompletely empty.
18123 20 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e This adjustmen
18124 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 t is not optiona
18125 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d l.. */. for(i=
18126 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a k-1; i>0; i--){.
18127 20 20 20 20 69 6e 74 20 73 7a 52 69 67 68 74 20 int szRight
18128 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 = szNew[i]; /*
18129 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 Size of sibling
1812a 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a 2f 0a on the right */.
1812b 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d int szLeft =
1812c 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 szNew[i-1]; /*
1812d 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 Size of sibling
1812e 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20 on the left */.
1812f 20 20 20 69 6e 74 20 72 3b 20 20 20 20 20 20 20 int r;
18130 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
18131 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 of right-most ce
18132 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 62 6c 69 ll in left sibli
18133 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b ng */. int d;
18134 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18135 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20 Index of first
18136 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c 65 66 74 cell to the left
18137 20 6f 66 20 72 69 67 68 74 20 73 69 62 6c 69 6e of right siblin
18138 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e g */.. r = cn
18139 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 tNew[i-1] - 1;.
1813a 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c d = r + 1 - l
1813b 65 61 66 44 61 74 61 3b 0a 20 20 20 20 61 73 73 eafData;. ass
1813c 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 ert( d<nMaxCells
1813d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1813e 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 r<nMaxCells );.
1813f 20 20 20 77 68 69 6c 65 28 20 73 7a 52 69 67 68 while( szRigh
18140 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 68 74 2b t==0 || szRight+
18141 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c szCell[d]+2<=szL
18142 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 eft-(szCell[r]+2
18143 29 20 29 7b 0a 20 20 20 20 20 20 73 7a 52 69 67 ) ){. szRig
18144 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 ht += szCell[d]
18145 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a 4c 65 66 + 2;. szLef
18146 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b t -= szCell[r] +
18147 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 2;. cntNew
18148 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 [i-1]--;. r
18149 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d = cntNew[i-1] -
1814a 20 31 3b 0a 20 20 20 20 20 20 64 20 3d 20 72 20 1;. d = r
1814b 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a + 1 - leafData;.
1814c 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e 65 77 5b }. szNew[
1814d 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b 0a 20 20 i] = szRight;.
1814e 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 szNew[i-1] = s
1814f 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a zLeft;. }.. /*
18150 20 45 69 74 68 65 72 20 77 65 20 66 6f 75 6e 64 Either we found
18151 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 65 6c one or more cel
18152 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 ls (cntnew[0])>0
18153 29 20 6f 72 20 70 50 61 67 65 20 69 73 0a 20 20 ) or pPage is.
18154 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f ** a virtual roo
18155 74 20 70 61 67 65 2e 20 20 41 20 76 69 72 74 75 t page. A virtu
18156 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 al root page is
18157 77 68 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f when the real ro
18158 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20 ot. ** page is
18159 70 61 67 65 20 31 20 61 6e 64 20 77 65 20 61 72 page 1 and we ar
1815a 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 e the only child
1815b 20 6f 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20 of that page..
1815c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e */. assert( cn
1815d 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 tNew[0]>0 || (pP
1815e 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 arent->pgno==1 &
1815f 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c & pParent->nCell
18160 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52 41 43 45 ==0) );.. TRACE
18161 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a (("BALANCE: old:
18162 20 25 64 20 25 64 20 25 64 20 20 22 2c 0a 20 20 %d %d %d ",.
18163 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f apOld[0]->pgno
18164 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f , . nOld>=2 ?
18165 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67 6e 6f 20 apOld[1]->pgno
18166 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 : 0,. nOld>=3
18167 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e 70 67 6e ? apOld[2]->pgn
18168 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a 20 20 2f o : 0. ));.. /
18169 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 *. ** Allocate
1816a 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 52 65 k new pages. Re
1816b 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 77 68 use old pages wh
1816c 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 ere possible..
1816d 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c 64 5b 30 */. if( apOld[0
1816e 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b 0a 20 20 ]->pgno<=1 ){.
1816f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
18170 52 52 55 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 RRUPT;. goto
18171 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
18172 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c 61 67 73 . }. pageFlags
18173 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 = apOld[0]->aDa
18174 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 ta[0];. for(i=0
18175 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 ; i<k; i++){.
18176 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a MemPage *pNew;.
18177 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 if( i<nOld )
18178 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 {. pNew = a
18179 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b pNew[i] = apOld[
1817a 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b i];. apOld[
1817b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 i] = 0;. rc
1817c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1817d 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 rite(pNew->pDbPa
1817e 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b ge);. nNew+
1817f 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 +;. if( rc
18180 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 ) goto balance_c
18181 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 leanup;. }els
18182 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
18183 20 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 i>0 );. rc
18184 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
18185 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c Page(pBt, &pNew,
18186 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c 20 30 29 &pgno, pgno, 0)
18187 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
18188 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
18189 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 70 4e eanup;. apN
1818a 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 ew[i] = pNew;.
1818b 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20 nNew++;..
1818c 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70 6f /* Set the po
1818d 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 inter-map entry
1818e 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62 6c for the new sibl
1818f 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 ing page. */.
18190 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
18191 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 UUM ){. p
18192 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 4e trmapPut(pBt, pN
18193 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 ew->pgno, PTRMAP
18194 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d _BTREE, pParent-
18195 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 >pgno, &rc);.
18196 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18197 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18198 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
18199 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 _cleanup;.
1819a 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1819b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 }. }.. /* Free
1819c 20 61 6e 79 20 6f 6c 64 20 70 61 67 65 73 20 74 any old pages t
1819d 68 61 74 20 77 65 72 65 20 6e 6f 74 20 72 65 75 hat were not reu
1819e 73 65 64 20 61 73 20 6e 65 77 20 70 61 67 65 73 sed as new pages
1819f 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 .. */. while(
181a0 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72 i<nOld ){. fr
181a1 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c eePage(apOld[i],
181a2 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
181a3 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 c ) goto balance
181a4 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 _cleanup;. re
181a5 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b leasePage(apOld[
181a6 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 i]);. apOld[i
181a7 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a ] = 0;. i++;.
181a8 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 }.. /*. ** P
181a9 75 74 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 ut the new pages
181aa 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 in accending or
181ab 64 65 72 2e 20 20 54 68 69 73 20 68 65 6c 70 73 der. This helps
181ac 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e to. ** keep en
181ad 74 72 69 65 73 20 69 6e 20 74 68 65 20 64 69 73 tries in the dis
181ae 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 k file in order
181af 73 6f 20 74 68 61 74 20 61 20 73 63 61 6e 0a 20 so that a scan.
181b0 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ** of the table
181b1 20 69 73 20 61 20 6c 69 6e 65 61 72 20 73 63 61 is a linear sca
181b2 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 n through the fi
181b3 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 le. That. ** i
181b4 6e 20 74 75 72 6e 20 68 65 6c 70 73 20 74 68 65 n turn helps the
181b5 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
181b6 6d 20 74 6f 20 64 65 6c 69 76 65 72 20 70 61 67 m to deliver pag
181b7 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 es. ** from the
181b8 20 64 69 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 disk more rapid
181b9 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e ly.. **. ** An
181ba 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f O(n^2) insertio
181bb 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d n sort algorithm
181bc 20 69 73 20 75 73 65 64 2c 20 62 75 74 20 73 69 is used, but si
181bd 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 nce. ** n is ne
181be 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 ver more than NB
181bf 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 (a small consta
181c0 6e 74 29 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 nt), that should
181c1 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 . ** not be a p
181c2 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a roblem.. **. *
181c3 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 * When NB==3, th
181c4 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 is one optimizat
181c5 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65 20 64 61 ion makes the da
181c6 74 61 62 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 tabase. ** abou
181c7 74 20 32 35 25 20 66 61 73 74 65 72 20 66 6f 72 t 25% faster for
181c8 20 6c 61 72 67 65 20 69 6e 73 65 72 74 69 6f 6e large insertion
181c9 73 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e s and deletions.
181ca 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
181cb 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 i<k-1; i++){.
181cc 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e int minV = apN
181cd 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 ew[i]->pgno;.
181ce 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 int minI = i;.
181cf 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c for(j=i+1; j<
181d0 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 k; j++){. i
181d1 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e f( apNew[j]->pgn
181d2 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 o<(unsigned)minV
181d3 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 ){. minI
181d4 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 = j;. mi
181d5 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 nV = apNew[j]->p
181d6 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 gno;. }.
181d7 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e }. if( minI>
181d8 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 i ){. int t
181d9 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 ;. MemPage
181da 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 61 *pT;. t = a
181db 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 pNew[i]->pgno;.
181dc 20 20 20 20 20 70 54 20 3d 20 61 70 4e 65 77 5b pT = apNew[
181dd 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b i];. apNew[
181de 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d i] = apNew[minI]
181df 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69 ;. apNew[mi
181e0 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a nI] = pT;. }.
181e1 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22 6e 65 }. TRACE(("ne
181e2 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29 w: %d(%d) %d(%d)
181e3 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 %d(%d) %d(%d) %
181e4 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 61 70 d(%d)\n",. ap
181e5 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 73 7a New[0]->pgno, sz
181e6 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77 New[0],. nNew
181e7 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 5d 2d 3e >=2 ? apNew[1]->
181e8 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d pgno : 0, nNew>=
181e9 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2 ? szNew[1] : 0
181ea 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 ,. nNew>=3 ?
181eb 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e 6f 20 3a apNew[2]->pgno :
181ec 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 0, nNew>=3 ? sz
181ed 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 New[2] : 0,.
181ee 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e 65 77 5b nNew>=4 ? apNew[
181ef 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 3]->pgno : 0, nN
181f0 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d ew>=4 ? szNew[3]
181f1 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d : 0,. nNew>=
181f2 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d 3e 70 67 5 ? apNew[4]->pg
181f3 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20 no : 0, nNew>=5
181f4 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29 ? szNew[4] : 0))
181f5 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
181f6 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
181f7 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 able(pParent->pD
181f8 62 50 61 67 65 29 20 29 3b 0a 20 20 70 75 74 34 bPage) );. put4
181f9 62 79 74 65 28 70 52 69 67 68 74 2c 20 61 70 4e byte(pRight, apN
181fa 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67 6e 6f ew[nNew-1]->pgno
181fb 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76 );.. /*. ** Ev
181fc 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20 enly distribute
181fd 74 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65 the data in apCe
181fe 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20 ll[] across the
181ff 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 new pages.. **
18200 49 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63 Insert divider c
18201 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e ells into pParen
18202 74 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a t as necessary..
18203 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20 */. j = 0;.
18204 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b for(i=0; i<nNew;
18205 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73 i++){. /* As
18206 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73 semble the new s
18207 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a ibling page. */.
18208 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 MemPage *pNe
18209 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 w = apNew[i];.
1820a 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 assert( j<nMax
1820b 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 7a 65 72 Cells );. zer
1820c 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 oPage(pNew, page
1820d 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65 Flags);. asse
1820e 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63 mblePage(pNew, c
1820f 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 ntNew[i]-j, &apC
18210 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b ell[j], &szCell[
18211 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 j]);. assert(
18212 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c pNew->nCell>0 |
18213 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e | (nNew==1 && cn
18214 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 tNew[0]==0) );.
18215 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d assert( pNew-
18216 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
18217 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 .. j = cntNew
18218 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 [i];.. /* If
18219 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
1821a 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 assembled above
1821b 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72 69 67 was not the rig
1821c 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c ht-most sibling,
1821d 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 . ** insert a
1821e 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e divider cell in
1821f 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 to the parent pa
18220 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 ge.. */. a
18221 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 2d 31 20 ssert( i<nNew-1
18222 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 || j==nCell );.
18223 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c 6c 20 29 if( j<nCell )
18224 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c {. u8 *pCel
18225 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65 l;. u8 *pTe
18226 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a mp;. int sz
18227 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ;.. assert(
18228 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a j<nMaxCells );.
18229 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61 70 pCell = ap
1822a 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73 Cell[j];. s
1822b 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20 z = szCell[j] +
1822c 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a leafCorrection;.
1822d 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 pTemp = &a
1822e 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 66 6c 53 OvflSpace[iOvflS
1822f 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 69 66 28 pace];. if(
18230 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a !pNew->leaf ){.
18231 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
18232 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 pNew->aData[8],
18233 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 pCell, 4);.
18234 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 66 44 }else if( leafD
18235 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ata ){. /
18236 2a 20 49 66 20 74 68 65 20 74 72 65 65 20 69 73 * If the tree is
18237 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 a leaf-data tre
18238 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62 6c 69 e, and the sibli
18239 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73 2c 20 ngs are leaves,
1823a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e . ** then
1823b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 76 there is no div
1823c 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61 70 43 ider cell in apC
1823d 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20 ell[]. Instead,
1823e 74 68 65 20 64 69 76 69 64 65 72 20 0a 20 20 20 the divider .
1823f 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e ** cell con
18240 73 69 73 74 73 20 6f 66 20 74 68 65 20 69 6e 74 sists of the int
18241 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 68 65 eger key for the
18242 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c right-most cell
18243 20 6f 66 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 of . **
18244 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 61 67 65 the sibling-page
18245 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 assembled above
18246 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a only.. *
18247 2f 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e /. CellIn
18248 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 fo info;.
18249 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 20 62 74 j--;. bt
1824a 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
1824b 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c pNew, apCell[j],
1824c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 &info);.
1824d 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a pCell = pTemp;.
1824e 20 20 20 20 20 20 20 20 73 7a 20 3d 20 34 20 2b sz = 4 +
1824f 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c putVarint(&pCel
18250 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b 65 79 29 l[4], info.nKey)
18251 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 ;. pTemp
18252 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
18253 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 {. pCell
18254 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 2f 2a -= 4;. /*
18255 20 4f 62 73 63 75 72 65 20 63 61 73 65 20 66 6f Obscure case fo
18256 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 20 r non-leaf-data
18257 74 72 65 65 73 3a 20 49 66 20 74 68 65 20 63 65 trees: If the ce
18258 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61 73 0a ll at pCell was.
18259 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 ** previ
1825a 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f 6e 20 ously stored on
1825b 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61 6e 64 a leaf node, and
1825c 20 69 74 73 20 72 65 70 6f 72 74 65 64 20 73 69 its reported si
1825d 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20 20 20 ze was 4.
1825e 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65 6e 20 ** bytes, then
1825f 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 it may actually
18260 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 be smaller than
18261 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a this . **
18262 20 28 73 65 65 20 62 74 72 65 65 50 61 72 73 65 (see btreeParse
18263 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74 CellPtr(), 4 byt
18264 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 es is the minimu
18265 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 m size of.
18266 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 ** any cell).
18267 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74 But it is import
18268 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68 65 20 ant to pass the
18269 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20 correct size to
1826a 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 . ** inse
1826b 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 rtCell(), so rep
1826c 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f arse the cell no
1826d 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 w.. **.
1826e 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 ** Note th
1826f 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65 at this can neve
18270 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 r happen in an S
18271 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c QLite data file,
18272 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 as all.
18273 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20 ** cells are at
18274 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e 20 49 least 4 bytes. I
18275 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 t only happens i
18276 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20 n b-trees used.
18277 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 ** to eva
18278 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43 luate "IN (SELEC
18279 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 T ...)" and simi
1827a 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 lar clauses..
1827b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1827c 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 if( szCell[j]==4
1827d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 ){. as
1827e 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74 sert(leafCorrect
1827f 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 ion==4);.
18280 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 sz = cellSize
18281 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65 Ptr(pParent, pCe
18282 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ll);. }.
18283 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4f 76 }. iOv
18284 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a 3b 0a 20 flSpace += sz;.
18285 20 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c assert( sz<
18286 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 =pBt->pageSize/4
18287 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
18288 28 20 69 4f 76 66 6c 53 70 61 63 65 3c 3d 70 42 ( iOvflSpace<=pB
18289 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 t->pageSize );.
1828a 20 20 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 insertCell(
1828b 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 pParent, nxDiv,
1828c 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 pCell, sz, pTemp
1828d 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 , pNew->pgno, &r
1828e 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 c);. if( rc
1828f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
18290 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
18291 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 up;. assert
18292 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
18293 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
18294 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a t->pDbPage) );..
18295 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 j++;.
18296 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a nxDiv++;. }.
18297 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d }. assert( j=
18298 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 =nCell );. asse
18299 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 rt( nOld>0 );.
1829a 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 assert( nNew>0 )
1829b 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c 61 ;. if( (pageFla
1829c 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d gs & PTF_LEAF)==
1829d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 0 ){. u8 *zCh
1829e 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f ild = &apCopy[nO
1829f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b ld-1]->aData[8];
182a0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e . memcpy(&apN
182a1 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 ew[nNew-1]->aDat
182a2 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 a[8], zChild, 4)
182a3 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 ;. }.. if( isR
182a4 6f 6f 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e oot && pParent->
182a5 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72 nCell==0 && pPar
182a6 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d ent->hdrOffset<=
182a7 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 apNew[0]->nFree
182a8 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f ){. /* The ro
182a9 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 ot page of the b
182aa 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69 -tree now contai
182ab 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65 ns no cells. The
182ac 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20 only sibling.
182ad 20 20 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65 ** page is the
182ae 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 right-child of
182af 74 68 65 20 70 61 72 65 6e 74 2e 20 43 6f 70 79 the parent. Copy
182b0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
182b1 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c the. ** chil
182b2 64 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 d page into the
182b3 70 61 72 65 6e 74 2c 20 64 65 63 72 65 61 73 69 parent, decreasi
182b4 6e 67 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 68 ng the overall h
182b5 65 69 67 68 74 20 6f 66 20 74 68 65 0a 20 20 20 eight of the.
182b6 20 2a 2a 20 62 2d 74 72 65 65 20 73 74 72 75 63 ** b-tree struc
182b7 74 75 72 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 ture by one. Thi
182b8 73 20 69 73 20 64 65 73 63 72 69 62 65 64 20 61 s is described a
182b9 73 20 74 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 s the "balance-s
182ba 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a hallower". **
182bb 20 73 75 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 sub-algorithm i
182bc 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 n some documenta
182bd 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 tion.. **.
182be 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 ** If this is a
182bf 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 n auto-vacuum da
182c0 74 61 62 61 73 65 2c 20 74 68 65 20 63 61 6c 6c tabase, the call
182c1 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 to copyNodeCont
182c2 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a 20 73 65 ent() . ** se
182c3 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d ts all pointer-m
182c4 61 70 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 ap entries corre
182c5 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 sponding to data
182c6 62 61 73 65 20 69 6d 61 67 65 20 70 61 67 65 73 base image pages
182c7 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 . ** for whi
182c8 63 68 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 ch the pointer i
182c9 73 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 s stored within
182ca 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e the content bein
182cb 67 20 63 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a g copied.. **
182cc 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f . ** The seco
182cd 6e 64 20 61 73 73 65 72 74 20 62 65 6c 6f 77 20 nd assert below
182ce 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 68 verifies that th
182cf 65 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 e child page is
182d0 64 65 66 72 61 67 6d 65 6e 74 65 64 0a 20 20 20 defragmented.
182d1 20 2a 2a 20 28 69 74 20 6d 75 73 74 20 62 65 2c ** (it must be,
182d2 20 61 73 20 69 74 20 77 61 73 20 6a 75 73 74 20 as it was just
182d3 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20 75 73 reconstructed us
182d4 69 6e 67 20 61 73 73 65 6d 62 6c 65 50 61 67 65 ing assemblePage
182d5 28 29 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a ()). This. **
182d6 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 66 is important if
182d7 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
182d8 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 70 happens to be p
182d9 61 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 age 1 of the dat
182da 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61 abase. ** ima
182db 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 ge. */. asse
182dc 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20 rt( nNew==1 );.
182dd 20 20 20 61 73 73 65 72 74 28 20 61 70 4e 65 77 assert( apNew
182de 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20 [0]->nFree == .
182df 20 20 20 20 20 20 20 28 67 65 74 32 62 79 74 65 (get2byte
182e0 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74 (&apNew[0]->aDat
182e1 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e a[5])-apNew[0]->
182e2 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77 cellOffset-apNew
182e3 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20 [0]->nCell*2) .
182e4 20 20 20 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f );. copyNo
182e5 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b deContent(apNew[
182e6 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20 26 72 63 0], pParent, &rc
182e7 29 3b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 );. freePage(
182e8 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a apNew[0], &rc);.
182e9 20 20 7d 65 6c 73 65 20 69 66 28 20 49 53 41 55 }else if( ISAU
182ea 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
182eb 2f 2a 20 46 69 78 20 74 68 65 20 70 6f 69 6e 74 /* Fix the point
182ec 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 er-map entries f
182ed 6f 72 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 or all the cells
182ee 20 74 68 61 74 20 77 65 72 65 20 73 68 69 66 74 that were shift
182ef 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20 ed around. .
182f0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73 65 76 ** There are sev
182f1 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 74 eral different t
182f2 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d ypes of pointer-
182f3 6d 61 70 20 65 6e 74 72 69 65 73 20 74 68 61 74 map entries that
182f4 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 need to. **
182f5 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 be dealt with by
182f6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 53 this routine. S
182f7 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 68 61 76 ome of these hav
182f8 65 20 62 65 65 6e 20 73 65 74 20 61 6c 72 65 61 e been set alrea
182f9 64 79 2c 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d dy, but. ** m
182fa 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e 20 54 68 any have not. Th
182fb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 e following is a
182fc 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a summary:. **
182fd 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65 . ** 1) The
182fe 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 entries associa
182ff 74 65 64 20 77 69 74 68 20 6e 65 77 20 73 69 62 ted with new sib
18300 6c 69 6e 67 20 70 61 67 65 73 20 74 68 61 74 20 ling pages that
18301 77 65 72 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 were not. **
18302 20 20 20 20 20 73 69 62 6c 69 6e 67 73 20 77 68 siblings wh
18303 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
18304 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 was called. The
18305 73 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a se have already.
18306 20 20 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e ** been
18307 20 73 65 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e set. We don't n
18308 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f eed to worry abo
18309 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20 ut old siblings
1830a 74 68 61 74 20 77 65 72 65 0a 20 20 20 20 2a 2a that were. **
1830b 20 20 20 20 20 20 6d 6f 76 65 64 20 74 6f 20 74 moved to t
1830c 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2d 20 74 he free-list - t
1830d 68 65 20 66 72 65 65 50 61 67 65 28 29 20 63 6f he freePage() co
1830e 64 65 20 68 61 73 20 74 61 6b 65 6e 20 63 61 72 de has taken car
1830f 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 66 e. ** of
18310 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 those.. **.
18311 20 20 20 2a 2a 20 20 20 32 29 20 54 68 65 20 70 ** 2) The p
18312 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
18313 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
18314 74 68 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 th the first ove
18315 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20 rflow. **
18316 20 20 70 61 67 65 20 69 6e 20 61 6e 79 20 6f 76 page in any ov
18317 65 72 66 6c 6f 77 20 63 68 61 69 6e 73 20 75 73 erflow chains us
18318 65 64 20 62 79 20 6e 65 77 20 64 69 76 69 64 65 ed by new divide
18319 72 20 63 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a r cells. These .
1831a 20 20 20 20 2a 2a 20 20 20 20 20 20 68 61 76 65 ** have
1831b 20 61 6c 73 6f 20 61 6c 72 65 61 64 79 20 62 65 also already be
1831c 65 6e 20 74 61 6b 65 6e 20 63 61 72 65 20 6f 66 en taken care of
1831d 20 62 79 20 74 68 65 20 69 6e 73 65 72 74 43 65 by the insertCe
1831e 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a ll() code.. *
1831f 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29 20 49 66 *. ** 3) If
18320 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 the sibling pag
18321 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 es are not leave
18322 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c s, then the chil
18323 64 20 70 61 67 65 73 20 6f 66 0a 20 20 20 20 2a d pages of. *
18324 2a 20 20 20 20 20 20 63 65 6c 6c 73 20 73 74 6f * cells sto
18325 72 65 64 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 red on the sibli
18326 6e 67 20 70 61 67 65 73 20 6d 61 79 20 6e 65 65 ng pages may nee
18327 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e d to be updated.
18328 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
18329 20 34 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 4) If the sibli
1832a 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 ng pages are not
1832b 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 internal intkey
1832c 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 61 6e 79 nodes, then any
1832d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 76 65 . ** ove
1832e 72 66 6c 6f 77 20 70 61 67 65 73 20 75 73 65 64 rflow pages used
1832f 20 62 79 20 74 68 65 73 65 20 63 65 6c 6c 73 20 by these cells
18330 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 may need to be u
18331 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20 20 20 pdated. **
18332 20 20 20 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74 (internal int
18333 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76 65 72 20 key nodes never
18334 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 contain pointers
18335 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
18336 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 es).. **.
18337 2a 2a 20 20 20 35 29 20 49 66 20 74 68 65 20 73 ** 5) If the s
18338 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 ibling pages are
18339 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 not leaves, the
1833a 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 n the pointer-ma
1833b 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 65 6e p. ** en
1833c 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 72 69 tries for the ri
1833d 67 68 74 2d 63 68 69 6c 64 20 70 61 67 65 73 20 ght-child pages
1833e 6f 66 20 65 61 63 68 20 73 69 62 6c 69 6e 67 20 of each sibling
1833f 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 may need. **
18340 20 20 20 20 20 74 6f 20 62 65 20 75 70 64 61 74 to be updat
18341 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ed.. **. *
18342 2a 20 43 61 73 65 73 20 31 20 61 6e 64 20 32 20 * Cases 1 and 2
18343 61 72 65 20 64 65 61 6c 74 20 77 69 74 68 20 61 are dealt with a
18344 62 6f 76 65 20 62 79 20 6f 74 68 65 72 20 63 6f bove by other co
18345 64 65 2e 20 54 68 65 20 6e 65 78 74 0a 20 20 20 de. The next.
18346 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 ** block deals
18347 77 69 74 68 20 63 61 73 65 73 20 33 20 61 6e 64 with cases 3 and
18348 20 34 20 61 6e 64 20 74 68 65 20 6f 6e 65 20 61 4 and the one a
18349 66 74 65 72 20 74 68 61 74 2c 20 63 61 73 65 20 fter that, case
1834a 35 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 5. Since. **
1834b 73 65 74 74 69 6e 67 20 61 20 70 6f 69 6e 74 65 setting a pointe
1834c 72 20 6d 61 70 20 65 6e 74 72 79 20 69 73 20 61 r map entry is a
1834d 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70 65 relatively expe
1834e 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c nsive operation,
1834f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64 this. ** cod
18350 65 20 6f 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e e only sets poin
18351 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 ter map entries
18352 66 6f 72 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 for child or ove
18353 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 rflow pages that
18354 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 61 63 74 have. ** act
18355 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 ually moved betw
18356 65 65 6e 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20 een pages. */.
18357 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 MemPage *pNew
18358 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20 = apNew[0];.
18359 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d MemPage *pOld =
1835a 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20 apCopy[0];.
1835b 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 int nOverflow =
1835c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b pOld->nOverflow;
1835d 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c . int iNextOl
1835e 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 d = pOld->nCell
1835f 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 + nOverflow;.
18360 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d int iOverflow =
18361 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f (nOverflow ? pO
18362 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 ld->aOvfl[0].idx
18363 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a 20 3d 20 : -1);. j =
18364 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
18365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18366 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c 64 27 20 * Current 'old'
18367 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a sibling page */.
18368 20 20 20 20 6b 20 3d 20 30 3b 20 20 20 20 20 20 k = 0;
18369 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1836a 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
1836b 74 20 27 6e 65 77 27 20 73 69 62 6c 69 6e 67 20 t 'new' sibling
1836c 70 61 67 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 page */. for(
1836d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
1836e 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 +){. int is
1836f 44 69 76 69 64 65 72 20 3d 20 30 3b 0a 20 20 20 Divider = 0;.
18370 20 20 20 77 68 69 6c 65 28 20 69 3d 3d 69 4e 65 while( i==iNe
18371 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 xtOld ){.
18372 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 /* Cell i is th
18373 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 e cell immediate
18374 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ly following the
18375 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6f 6c last cell on ol
18376 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 d. ** sib
18377 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20 49 66 20 ling page j. If
18378 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 the siblings are
18379 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 not leaf pages
1837a 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a of an. **
1837b 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 intkey b-tree,
1837c 74 68 65 6e 20 63 65 6c 6c 20 69 20 77 61 73 20 then cell i was
1837d 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 a divider cell.
1837e 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 6c 64 20 */. pOld
1837f 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d 3b 0a 20 = apCopy[++j];.
18380 20 20 20 20 20 20 20 69 4e 65 78 74 4f 6c 64 20 iNextOld
18381 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 = i + !leafData
18382 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 + pOld->nCell +
18383 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b pOld->nOverflow;
18384 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 6c . if( pOl
18385 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a d->nOverflow ){.
18386 20 20 20 20 20 20 20 20 20 20 6e 4f 76 65 72 66 nOverf
18387 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 low = pOld->nOve
18388 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 20 rflow;.
18389 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 69 20 2b iOverflow = i +
1838a 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70 4f 6c !leafData + pOl
1838b 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3b d->aOvfl[0].idx;
1838c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1838d 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 21 isDivider = !
1838e 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20 20 20 leafData; .
1838f 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 }.. asser
18390 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c t(nOverflow>0 ||
18391 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 3b 0a iOverflow<i );.
18392 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76 assert(nOv
18393 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c 64 erflow<2 || pOld
18394 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d ->aOvfl[0].idx==
18395 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 pOld->aOvfl[1].i
18396 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 61 73 73 dx-1);. ass
18397 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 33 20 ert(nOverflow<3
18398 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 || pOld->aOvfl[1
18399 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 ].idx==pOld->aOv
1839a 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 fl[2].idx-1);.
1839b 20 20 20 20 69 66 28 20 69 3d 3d 69 4f 76 65 72 if( i==iOver
1839c 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 flow ){.
1839d 69 73 44 69 76 69 64 65 72 20 3d 20 31 3b 0a 20 isDivider = 1;.
1839e 20 20 20 20 20 20 20 69 66 28 20 28 2d 2d 6e 4f if( (--nO
1839f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b 0a 20 20 verflow)>0 ){.
183a0 20 20 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f iOverflo
183a1 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 w++;. }.
183a2 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
183a3 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b 5d 20 29 ( i==cntNew[k] )
183a4 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c {. /* Cel
183a5 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c 6c 20 l i is the cell
183a6 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
183a7 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 63 owing the last c
183a8 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20 20 20 20 ell on new.
183a9 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 ** sibling pa
183aa 67 65 20 6b 2e 20 49 66 20 74 68 65 20 73 69 62 ge k. If the sib
183ab 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 lings are not le
183ac 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 af pages of an.
183ad 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 ** intkey
183ae 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 63 65 b-tree, then ce
183af 6c 6c 20 69 20 69 73 20 61 20 64 69 76 69 64 65 ll i is a divide
183b0 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 20 20 r cell. */.
183b1 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 pNew = apNew
183b2 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 [++k];. i
183b3 66 28 20 21 6c 65 61 66 44 61 74 61 20 29 20 63 f( !leafData ) c
183b4 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d ontinue;. }
183b5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a . assert( j
183b6 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 20 20 61 <nOld );. a
183b7 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 20 29 3b ssert( k<nNew );
183b8 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 .. /* If th
183b9 65 20 63 65 6c 6c 20 77 61 73 20 6f 72 69 67 69 e cell was origi
183ba 6e 61 6c 6c 79 20 64 69 76 69 64 65 72 20 63 65 nally divider ce
183bb 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f 74 20 6e ll (and is not n
183bc 6f 77 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 ow) or. **
183bd 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c an overflow cell
183be 2c 20 6f 72 20 69 66 20 74 68 65 20 63 65 6c 6c , or if the cell
183bf 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 was located on
183c0 61 20 64 69 66 66 65 72 65 6e 74 20 73 69 62 6c a different sibl
183c1 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 ing. ** pag
183c2 65 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 6c e before the bal
183c3 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 ancing, then the
183c4 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
183c5 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a ries associated.
183c6 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e ** with an
183c7 79 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 y child or overf
183c8 6c 6f 77 20 70 61 67 65 73 20 6e 65 65 64 20 74 low pages need t
183c9 6f 20 62 65 20 75 70 64 61 74 65 64 2e 20 20 2a o be updated. *
183ca 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 44 69 /. if( isDi
183cb 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64 2d 3e 70 vider || pOld->p
183cc 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f 20 gno!=pNew->pgno
183cd 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 ){. if( !
183ce 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 29 leafCorrection )
183cf 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 72 6d {. ptrm
183d0 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 apPut(pBt, get4b
183d1 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d 29 2c 20 yte(apCell[i]),
183d2 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e PTRMAP_BTREE, pN
183d3 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a ew->pgno, &rc);.
183d4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
183d5 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 69 5d 3e if( szCell[i]>
183d6 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 pNew->minLocal )
183d7 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 72 6d {. ptrm
183d8 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 apPutOvflPtr(pNe
183d9 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 26 72 w, apCell[i], &r
183da 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 c);. }.
183db 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
183dc 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 if( !leafCorrec
183dd 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 66 6f tion ){. fo
183de 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 r(i=0; i<nNew; i
183df 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 ++){. u32
183e0 20 6b 65 79 20 3d 20 67 65 74 34 62 79 74 65 28 key = get4byte(
183e1 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44 61 74 61 &apNew[i]->aData
183e2 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 70 74 [8]);. pt
183e3 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6b 65 79 rmapPut(pBt, key
183e4 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
183e5 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 2c 20 apNew[i]->pgno,
183e6 26 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 &rc);. }.
183e7 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f }..#if 0. /
183e8 2a 20 54 68 65 20 70 74 72 6d 61 70 43 68 65 63 * The ptrmapChec
183e9 6b 50 61 67 65 73 28 29 20 63 6f 6e 74 61 69 6e kPages() contain
183ea 73 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 s assert() state
183eb 6d 65 6e 74 73 20 74 68 61 74 20 76 65 72 69 66 ments that verif
183ec 79 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c y that. ** al
183ed 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 l pointer map pa
183ee 67 65 73 20 61 72 65 20 73 65 74 20 63 6f 72 72 ges are set corr
183ef 65 63 74 6c 79 2e 20 54 68 69 73 20 69 73 20 68 ectly. This is h
183f0 65 6c 70 66 75 6c 20 77 68 69 6c 65 20 0a 20 20 elpful while .
183f1 20 20 2a 2a 20 64 65 62 75 67 67 69 6e 67 2e 20 ** debugging.
183f2 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20 This is usually
183f3 64 69 73 61 62 6c 65 64 20 62 65 63 61 75 73 65 disabled because
183f4 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
183f5 61 73 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 63 ase may. ** c
183f6 61 75 73 65 20 61 6e 20 61 73 73 65 72 74 28 29 ause an assert()
183f7 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 66 61 statement to fa
183f8 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 74 72 6d il. */. ptrm
183f9 61 70 43 68 65 63 6b 50 61 67 65 73 28 61 70 4e apCheckPages(apN
183fa 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 70 ew, nNew);. p
183fb 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
183fc 26 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 23 65 &pParent, 1);.#e
183fd 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73 73 65 ndif. }.. asse
183fe 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 73 49 rt( pParent->isI
183ff 6e 69 74 20 29 3b 0a 20 20 54 52 41 43 45 28 28 nit );. TRACE((
18400 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 "BALANCE: finish
18401 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 ed: old=%d new=%
18402 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 d cells=%d\n",.
18403 20 20 20 20 20 20 20 20 20 6e 4f 6c 64 2c 20 6e nOld, n
18404 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20 New, nCell));..
18405 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 /*. ** Cleanup
18406 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
18407 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f g.. */.balance_
18408 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 cleanup:. sqlit
18409 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 e3ScratchFree(ap
1840a 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 Cell);. for(i=0
1840b 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a ; i<nOld; i++){.
1840c 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1840d 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 apOld[i]);. }.
1840e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 for(i=0; i<nNew
1840f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 ; i++){. rele
18410 61 73 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d asePage(apNew[i]
18411 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
18412 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 rc;.}.../*.** T
18413 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
18414 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
18415 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 root page of a b
18416 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -tree structure
18417 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 28 is.** overfull (
18418 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 has one or more
18419 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 2e overflow pages).
1841a 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63 68 69 .**.** A new chi
1841b 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 ld page is alloc
1841c 61 74 65 64 20 61 6e 64 20 74 68 65 20 63 6f 6e ated and the con
1841d 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 tents of the cur
1841e 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 rent root.** pag
1841f 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65 e, including ove
18420 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61 72 65 rflow cells, are
18421 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
18422 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f 6f 74 child. The root
18423 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65 6e .** page is then
18424 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74 6f 20 overwritten to
18425 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70 74 79 make it an empty
18426 20 70 61 67 65 20 77 69 74 68 20 74 68 65 20 72 page with the r
18427 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a 20 70 ight-child .** p
18428 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e 67 20 ointer pointing
18429 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e to the new page.
1842a 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 .**.** Before re
1842b 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70 6f 69 turning, all poi
1842c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
1842d 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1842e 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68 61 74 o pages .** that
1842f 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 2d 70 the new child-p
18430 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 age now contains
18431 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 72 65 pointers to are
18432 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a 2a 2a updated. The.**
18433 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70 6f 6e entry correspon
18434 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20 ding to the new
18435 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e right-child poin
18436 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 0a ter of the root.
18437 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73 6f 20 ** page is also
18438 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 updated..**.** I
18439 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 f successful, *p
1843a 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 74 6f pChild is set to
1843b 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72 contain a refer
1843c 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68 69 6c ence to the chil
1843d 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64 20 53 d .** page and S
1843e 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
1843f 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 rned. In this ca
18440 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 se the caller is
18441 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 required.** to
18442 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 call releasePage
18443 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64 20 65 () on *ppChild e
18444 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49 66 20 xactly once. If
18445 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
18446 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 .** an error cod
18447 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
18448 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 d *ppChild is se
18449 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 t to 0..*/.stati
1844a 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 c int balance_de
1844b 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 52 eper(MemPage *pR
1844c 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 70 oot, MemPage **p
1844d 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74 20 72 pChild){. int r
1844e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1844f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
18450 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 urn value from s
18451 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a ubprocedures */.
18452 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c MemPage *pChil
18453 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 d = 0;
18454 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
18455 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 new child page
18456 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 */. Pgno pgnoCh
18457 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ild = 0;
18458 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
18459 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 63 68 er of the new ch
1845a 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 42 74 ild page */. Bt
1845b 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 52 Shared *pBt = pR
1845c 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 oot->pBt; /*
1845d 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a 20 20 The BTree */..
1845e 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 6e assert( pRoot->n
1845f 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 20 20 Overflow>0 );.
18460 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
18461 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
18462 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 mutex) );.. /*
18463 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68 65 20 Make pRoot, the
18464 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
18465 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61 62 6c b-tree, writabl
18466 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 e. Allocate a ne
18467 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74 68 61 w . ** page tha
18468 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 t will become th
18469 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c e new right-chil
1846a 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f 70 79 d of pPage. Copy
1846b 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 the contents.
1846c 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 73 ** of the node s
1846d 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74 20 69 tored on pRoot i
1846e 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68 69 6c nto the new chil
1846f 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 d page.. */. r
18470 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
18471 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 Write(pRoot->pDb
18472 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d Page);. if( rc=
18473 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18474 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
18475 74 72 65 65 50 61 67 65 28 70 42 74 2c 26 70 43 treePage(pBt,&pC
18476 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c 64 2c hild,&pgnoChild,
18477 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29 3b 0a pRoot->pgno,0);.
18478 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 copyNodeCont
18479 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c ent(pRoot, pChil
1847a 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 d, &rc);. if(
1847b 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
1847c 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
1847d 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c (pBt, pgnoChild,
1847e 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 PTRMAP_BTREE, p
1847f 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 Root->pgno, &rc)
18480 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
18481 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 70 43 ( rc ){. *ppC
18482 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 hild = 0;. re
18483 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 leasePage(pChild
18484 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
18485 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
18486 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
18487 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e iteable(pChild->
18488 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
18489 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1848a 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 52 erIswriteable(pR
1848b 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b oot->pDbPage) );
1848c 0a 20 20 61 73 73 65 72 74 28 20 70 43 68 69 6c . assert( pChil
1848d 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f 74 2d d->nCell==pRoot-
1848e 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54 52 41 >nCell );.. TRA
1848f 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 6f CE(("BALANCE: co
18490 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74 6f 20 py root %d into
18491 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e 70 67 %d\n", pRoot->pg
18492 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f no, pChild->pgno
18493 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 ));.. /* Copy t
18494 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c he overflow cell
18495 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74 6f 20 s from pRoot to
18496 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65 6d 63 pChild */. memc
18497 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f 76 66 6c py(pChild->aOvfl
18498 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c 2c 20 , pRoot->aOvfl,
18499 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pRoot->nOverflow
1849a 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61 *sizeof(pRoot->a
1849b 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 68 Ovfl[0]));. pCh
1849c 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d ild->nOverflow =
1849d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f pRoot->nOverflo
1849e 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 w;.. /* Zero th
1849f 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 52 e contents of pR
184a0 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73 74 61 6c oot. Then instal
184a1 6c 20 70 43 68 69 6c 64 20 61 73 20 74 68 65 20 l pChild as the
184a2 72 69 67 68 74 2d 63 68 69 6c 64 2e 20 2a 2f 0a right-child. */.
184a3 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 zeroPage(pRoot
184a4 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b , pChild->aData[
184a5 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b 0] & ~PTF_LEAF);
184a6 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 52 6f . put4byte(&pRo
184a7 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d ot->aData[pRoot-
184a8 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 >hdrOffset+8], p
184a9 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20 20 2a 70 gnoChild);.. *p
184aa 70 43 68 69 6c 64 20 3d 20 70 43 68 69 6c 64 3b pChild = pChild;
184ab 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
184ac 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
184ad 65 20 70 61 67 65 20 74 68 61 74 20 70 43 75 72 e page that pCur
184ae 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
184af 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20 62 65 s to has just be
184b0 65 6e 20 6d 6f 64 69 66 69 65 64 20 69 6e 0a 2a en modified in.*
184b1 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54 68 69 73 * some way. This
184b2 20 66 75 6e 63 74 69 6f 6e 20 66 69 67 75 72 65 function figure
184b3 73 20 6f 75 74 20 69 66 20 74 68 69 73 20 6d 6f s out if this mo
184b4 64 69 66 69 63 61 74 69 6f 6e 20 6d 65 61 6e 73 dification means
184b5 20 74 68 65 0a 2a 2a 20 74 72 65 65 20 6e 65 65 the.** tree nee
184b6 64 73 20 74 6f 20 62 65 20 62 61 6c 61 6e 63 65 ds to be balance
184b7 64 2c 20 61 6e 64 20 69 66 20 73 6f 20 63 61 6c d, and if so cal
184b8 6c 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 ls the appropria
184b9 74 65 20 62 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a te balancing .**
184ba 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c 61 6e 63 routine. Balanc
184bb 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ing routines are
184bc 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 :.**.** balanc
184bd 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20 20 20 62 e_quick().** b
184be 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 0a alance_deeper().
184bf 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e ** balance_non
184c0 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61 74 69 63 root().*/.static
184c1 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 int balance(BtC
184c2 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
184c3 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
184c4 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 OK;. const int
184c5 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e 70 42 74 nMin = pCur->pBt
184c6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2a 20 32 ->usableSize * 2
184c7 20 2f 20 33 3b 0a 20 20 75 38 20 61 42 61 6c 61 / 3;. u8 aBala
184c8 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 31 33 nceQuickSpace[13
184c9 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65 65 20 3d ];. u8 *pFree =
184ca 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e 4c 59 28 0;.. TESTONLY(
184cb 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 int balance_qui
184cc 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b ck_called = 0 );
184cd 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 . TESTONLY( int
184ce 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f balance_deeper_
184cf 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 0a 20 called = 0 );..
184d0 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 50 do {. int iP
184d1 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 age = pCur->iPag
184d2 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a e;. MemPage *
184d3 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
184d4 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a 0a 20 20 Page[iPage];..
184d5 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 if( iPage==0 )
184d6 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 {. if( pPag
184d7 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a e->nOverflow ){.
184d8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
184d9 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
184da 62 2d 74 72 65 65 20 69 73 20 6f 76 65 72 66 75 b-tree is overfu
184db 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ll. In this case
184dc 20 63 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 call the.
184dd 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 64 65 65 ** balance_dee
184de 70 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 per() function t
184df 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 63 o create a new c
184e0 68 69 6c 64 20 66 6f 72 20 74 68 65 20 72 6f 6f hild for the roo
184e1 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a t-page. *
184e2 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 65 20 63 * and copy the c
184e3 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
184e4 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 of the root-page
184e5 20 74 6f 20 69 74 2e 20 54 68 65 0a 20 20 20 20 to it. The.
184e6 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 ** next iter
184e7 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d ation of the do-
184e8 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 loop will balanc
184e9 65 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 e the child page
184ea 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20 0a 20 20 .. */ .
184eb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 62 assert( (b
184ec 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 alance_deeper_ca
184ed 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 lled++)==0 );.
184ee 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e rc = balan
184ef 63 65 5f 64 65 65 70 65 72 28 70 50 61 67 65 2c ce_deeper(pPage,
184f0 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 &pCur->apPage[1
184f1 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ]);. if(
184f2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
184f3 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d . pCur-
184f4 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 >iPage = 1;.
184f5 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
184f6 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 x[0] = 0;.
184f7 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
184f8 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 1] = 0;.
184f9 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
184fa 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f 76 65 72 apPage[1]->nOver
184fb 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20 20 20 20 flow );.
184fc 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
184fd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
184fe 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 }. }else
184ff 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 if( pPage->nOver
18500 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 flow==0 && pPage
18501 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e 20 29 7b ->nFree<=nMin ){
18502 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
18503 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4d }else{. M
18504 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 74 20 70 emPage * const p
18505 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61 Parent = pCur->a
18506 70 50 61 67 65 5b 69 50 61 67 65 2d 31 5d 3b 0a pPage[iPage-1];.
18507 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 int const
18508 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 iIdx = pCur->aiI
18509 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a 0a 20 20 dx[iPage-1];..
1850a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1850b 50 61 67 65 72 57 72 69 74 65 28 70 50 61 72 65 PagerWrite(pPare
1850c 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nt->pDbPage);.
1850d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1850e 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e 64 65 66 TE_OK ){.#ifndef
1850f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 SQLITE_OMIT_QUI
18510 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20 20 20 20 CKBALANCE.
18511 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 if( pPage->has
18512 44 61 74 61 0a 20 20 20 20 20 20 20 20 20 26 26 Data. &&
18513 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
18514 77 3d 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 w==1. &&
18515 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d pPage->aOvfl[0]
18516 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 .idx==pPage->nCe
18517 6c 6c 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 ll. && p
18518 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 0a Parent->pgno!=1.
18519 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72 && pPar
1851a 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69 49 64 78 ent->nCell==iIdx
1851b 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
1851c 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 62 61 /* Call ba
1851d 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 74 6f lance_quick() to
1851e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 73 69 create a new si
1851f 62 6c 69 6e 67 20 6f 66 20 70 50 61 67 65 20 6f bling of pPage o
18520 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 n which.
18521 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 ** to store th
18522 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e e overflow cell.
18523 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 balance_quick()
18524 20 69 6e 73 65 72 74 73 20 61 20 6e 65 77 20 63 inserts a new c
18525 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ell. **
18526 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2c 20 77 into pParent, w
18527 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 20 70 hich may cause p
18528 50 61 72 65 6e 74 20 6f 76 65 72 66 6c 6f 77 2e Parent overflow.
18529 20 49 66 20 74 68 69 73 0a 20 20 20 20 20 20 20 If this.
1852a 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 ** happens, t
1852b 68 65 20 6e 65 78 74 20 69 6e 74 65 72 61 74 69 he next interati
1852c 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f on of the do-loo
1852d 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 70 p will balance p
1852e 50 61 72 65 6e 74 20 0a 20 20 20 20 20 20 20 20 Parent .
1852f 20 20 2a 2a 20 75 73 65 20 65 69 74 68 65 72 20 ** use either
18530 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
18531 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f 64 65 65 ) or balance_dee
18532 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 74 68 69 per(). Until thi
18533 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 s. ** h
18534 61 70 70 65 6e 73 2c 20 74 68 65 20 6f 76 65 72 appens, the over
18535 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 73 74 6f flow cell is sto
18536 72 65 64 20 69 6e 20 74 68 65 20 61 42 61 6c 61 red in the aBala
18537 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 5d 0a nceQuickSpace[].
18538 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 66 ** buf
18539 66 65 72 2e 20 0a 20 20 20 20 20 20 20 20 20 20 fer. .
1853a 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **. **
1853b 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 The purpose of t
1853c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
1853d 65 72 74 28 29 20 69 73 20 74 6f 20 63 68 65 63 ert() is to chec
1853e 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61 0a 20 20 k that only a.
1853f 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c ** singl
18540 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 e call to balanc
18541 65 5f 71 75 69 63 6b 28 29 20 69 73 20 6d 61 64 e_quick() is mad
18542 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 e for each call
18543 74 6f 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 to this.
18544 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 ** function. I
18545 66 20 74 68 69 73 20 77 65 72 65 20 6e 6f 74 20 f this were not
18546 76 65 72 69 66 69 65 64 2c 20 61 20 73 75 62 74 verified, a subt
18547 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76 69 6e 67 le bug involving
18548 20 72 65 75 73 65 0a 20 20 20 20 20 20 20 20 20 reuse.
18549 20 2a 2a 20 6f 66 20 74 68 65 20 61 42 61 6c 61 ** of the aBala
1854a 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 5d 20 nceQuickSpace[]
1854b 6d 69 67 68 74 20 73 6e 65 61 6b 20 69 6e 2e 0a might sneak in..
1854c 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1854d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 assert( (
1854e 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 balance_quick_ca
1854f 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 lled++)==0 );.
18550 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c rc = bal
18551 61 6e 63 65 5f 71 75 69 63 6b 28 70 50 61 72 65 ance_quick(pPare
18552 6e 74 2c 20 70 50 61 67 65 2c 20 61 42 61 6c 61 nt, pPage, aBala
18553 6e 63 65 51 75 69 63 6b 53 70 61 63 65 29 3b 0a nceQuickSpace);.
18554 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 }else.#e
18555 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 ndif. {.
18556 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 /* In t
18557 68 69 73 20 63 61 73 65 2c 20 63 61 6c 6c 20 62 his case, call b
18558 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
18559 20 74 6f 20 72 65 64 69 73 74 72 69 62 75 74 65 to redistribute
1855a 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 cells.
1855b 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 50 61 67 ** between pPag
1855c 65 20 61 6e 64 20 75 70 20 74 6f 20 32 20 6f 66 e and up to 2 of
1855d 20 69 74 73 20 73 69 62 6c 69 6e 67 20 70 61 67 its sibling pag
1855e 65 73 2e 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 es. This involve
1855f 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d s. ** m
18560 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e odifying the con
18561 74 65 6e 74 73 20 6f 66 20 70 50 61 72 65 6e 74 tents of pParent
18562 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73 , which may caus
18563 65 20 70 50 61 72 65 6e 74 20 74 6f 0a 20 20 20 e pParent to.
18564 20 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 ** become
18565 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 overfull or und
18566 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e 65 78 74 erfull. The next
18567 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
18568 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20 20 20 20 e do-loop.
18569 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 61 6c 61 ** will bala
1856a 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 nce the parent p
1856b 61 67 65 20 74 6f 20 63 6f 72 72 65 63 74 20 74 age to correct t
1856c 68 69 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a his.. *
1856d 2a 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 * . **
1856e 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 If the parent pa
1856f 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 ge becomes overf
18570 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f ull, the overflo
18571 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c 6c 73 0a w cell or cells.
18572 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 72 65 ** are
18573 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 stored in the p
18574 53 70 61 63 65 20 62 75 66 66 65 72 20 61 6c 6c Space buffer all
18575 6f 63 61 74 65 64 20 69 6d 6d 65 64 69 61 74 65 ocated immediate
18576 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20 20 20 20 ly below. .
18577 20 20 20 20 20 2a 2a 20 41 20 73 75 62 73 65 71 ** A subseq
18578 75 65 6e 74 20 69 74 65 72 61 74 69 6f 6e 20 6f uent iteration o
18579 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 f the do-loop wi
1857a 6c 6c 20 64 65 61 6c 20 77 69 74 68 20 74 68 69 ll deal with thi
1857b 73 20 62 79 0a 20 20 20 20 20 20 20 20 20 20 2a s by. *
1857c 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c 61 6e 63 * calling balanc
1857d 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28 62 61 6c e_nonroot() (bal
1857e 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 6d 61 ance_deeper() ma
1857f 79 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73 y be called firs
18580 74 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 t,. **
18581 62 75 74 20 69 74 20 64 6f 65 73 6e 27 74 20 64 but it doesn't d
18582 65 61 6c 20 77 69 74 68 20 6f 76 65 72 66 6c 6f eal with overflo
18583 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73 74 20 6d w cells - just m
18584 6f 76 65 73 20 74 68 65 6d 20 74 6f 20 61 0a 20 oves them to a.
18585 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 66 66 ** diff
18586 65 72 65 6e 74 20 70 61 67 65 29 2e 20 4f 6e 63 erent page). Onc
18587 65 20 74 68 69 73 20 73 75 62 73 65 71 75 65 6e e this subsequen
18588 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 t call to balanc
18589 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a 20 20 20 e_nonroot() .
1858a 20 20 20 20 20 20 20 2a 2a 20 68 61 73 20 63 6f ** has co
1858b 6d 70 6c 65 74 65 64 2c 20 69 74 20 69 73 20 73 mpleted, it is s
1858c 61 66 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 afe to release t
1858d 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 he pSpace buffer
1858e 20 75 73 65 64 20 62 79 0a 20 20 20 20 20 20 20 used by.
1858f 20 20 20 2a 2a 20 74 68 65 20 70 72 65 76 69 6f ** the previo
18590 75 73 20 63 61 6c 6c 2c 20 61 73 20 74 68 65 20 us call, as the
18591 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 64 61 overflow cell da
18592 74 61 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 ta will have bee
18593 6e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 n . **
18594 63 6f 70 69 65 64 20 65 69 74 68 65 72 20 69 6e copied either in
18595 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 to the body of a
18596 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6f database page o
18597 72 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 r into the new.
18598 20 20 20 20 20 20 20 20 20 2a 2a 20 70 53 70 61 ** pSpa
18599 63 65 20 62 75 66 66 65 72 20 70 61 73 73 65 64 ce buffer passed
1859a 20 74 6f 20 74 68 65 20 6c 61 74 74 65 72 20 63 to the latter c
1859b 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e all to balance_n
1859c 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20 20 20 20 onroot()..
1859d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
1859e 20 75 38 20 2a 70 53 70 61 63 65 20 3d 20 73 71 u8 *pSpace = sq
1859f 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
185a0 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 67 65 53 pCur->pBt->pageS
185a1 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ize);.
185a2 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e rc = balance_non
185a3 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c 20 69 49 root(pParent, iI
185a4 64 78 2c 20 70 53 70 61 63 65 2c 20 69 50 61 67 dx, pSpace, iPag
185a5 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 e==1);.
185a6 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 if( pFree ){.
185a7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
185a8 70 46 72 65 65 20 69 73 20 6e 6f 74 20 4e 55 4c pFree is not NUL
185a9 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 L, it points to
185aa 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
185ab 72 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 r used .
185ac 20 20 20 20 2a 2a 20 62 79 20 61 20 70 72 65 76 ** by a prev
185ad 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 62 61 6c ious call to bal
185ae 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 20 ance_nonroot().
185af 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 Its contents are
185b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
185b1 6e 6f 77 20 73 74 6f 72 65 64 20 65 69 74 68 65 now stored eithe
185b2 72 20 6f 6e 20 72 65 61 6c 20 64 61 74 61 62 61 r on real databa
185b3 73 65 20 70 61 67 65 73 20 6f 72 20 77 69 74 68 se pages or with
185b4 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 in the .
185b5 20 20 20 20 2a 2a 20 6e 65 77 20 70 53 70 61 63 ** new pSpac
185b6 65 20 62 75 66 66 65 72 2c 20 73 6f 20 69 74 20 e buffer, so it
185b7 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 66 72 may be safely fr
185b8 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 eed here. */.
185b9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
185ba 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3b PageFree(pFree);
185bb 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 . }..
185bc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
185bd 53 70 61 63 65 20 62 75 66 66 65 72 20 77 69 6c Space buffer wil
185be 6c 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 l be freed after
185bf 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 the next call t
185c0 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 o. ** b
185c1 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
185c2 2c 20 6f 72 20 6a 75 73 74 20 62 65 66 6f 72 65 , or just before
185c3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
185c4 65 74 75 72 6e 73 2c 20 77 68 69 63 68 65 76 65 eturns, whicheve
185c5 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 r. ** c
185c6 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a 2f 0a 20 omes first. */.
185c7 20 20 20 20 20 20 20 20 20 70 46 72 65 65 20 3d pFree =
185c8 20 70 53 70 61 63 65 3b 0a 20 20 20 20 20 20 20 pSpace;.
185c9 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
185ca 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
185cb 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f ow = 0;.. /
185cc 2a 20 54 68 65 20 6e 65 78 74 20 69 74 65 72 61 * The next itera
185cd 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c tion of the do-l
185ce 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20 74 68 65 oop balances the
185cf 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 2a 2f parent page. */
185d0 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
185d1 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
185d2 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a pCur->iPage--;.
185d3 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 }. }while(
185d4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
185d5 0a 0a 20 20 69 66 28 20 70 46 72 65 65 20 29 7b .. if( pFree ){
185d6 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
185d7 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20 20 7d Free(pFree);. }
185d8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
185d9 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 ../*.** Insert a
185da 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f new record into
185db 20 74 68 65 20 42 54 72 65 65 2e 20 20 54 68 65 the BTree. The
185dc 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20 62 79 key is given by
185dd 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 (pKey,nKey).**
185de 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 and the data is
185df 67 69 76 65 6e 20 62 79 20 28 70 44 61 74 61 2c given by (pData,
185e0 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63 75 72 nData). The cur
185e1 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 sor is used only
185e2 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 to.** define wh
185e3 61 74 20 74 61 62 6c 65 20 74 68 65 20 72 65 63 at table the rec
185e4 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 69 6e ord should be in
185e5 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 serted into. Th
185e6 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c e cursor.** is l
185e7 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
185e8 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f a random locatio
185e9 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 n..**.** For an
185ea 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e INTKEY table, on
185eb 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 ly the nKey valu
185ec 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 e of the key is
185ed 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a used. pKey is.*
185ee 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 * ignored. For
185ef 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62 6c 65 a ZERODATA table
185f0 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e 64 20 , the pData and
185f1 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68 20 69 nData are both i
185f2 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 gnored..**.** If
185f3 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 the seekResult
185f4 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
185f5 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73 75 -zero, then a su
185f6 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
185f7 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b .** MovetoUnpack
185f8 65 64 28 29 20 74 6f 20 73 65 65 6b 20 63 75 72 ed() to seek cur
185f9 73 6f 72 20 70 43 75 72 20 74 6f 20 28 70 4b 65 sor pCur to (pKe
185fa 79 2c 20 6e 4b 65 79 29 20 68 61 73 20 61 6c 72 y, nKey) has alr
185fb 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 65 72 eady.** been per
185fc 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52 65 73 75 formed. seekResu
185fd 6c 74 20 69 73 20 74 68 65 20 73 65 61 72 63 68 lt is the search
185fe 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 result returned
185ff 20 28 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 (a negative.**
18600 6e 75 6d 62 65 72 20 69 66 20 70 43 75 72 20 70 number if pCur p
18601 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 6e 74 72 oints at an entr
18602 79 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 y that is smalle
18603 72 20 74 68 61 6e 20 28 70 4b 65 79 2c 20 6e 4b r than (pKey, nK
18604 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20 70 6f 73 ey), or.** a pos
18605 69 74 69 76 65 20 76 61 6c 75 65 20 69 66 20 70 itive value if p
18606 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e Cur points at an
18607 20 65 74 72 79 20 74 68 61 74 20 69 73 20 6c 61 etry that is la
18608 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20 28 70 rger than .** (p
18609 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20 0a 2a 2a Key, nKey)). .**
1860a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 .** If the seekR
1860b 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 esult parameter
1860c 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1860d 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 67 75 61 n the caller gua
1860e 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20 rantees that.**
1860f 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 70 cursor pCur is p
18610 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 65 ointing at the e
18611 78 69 73 74 69 6e 67 20 63 6f 70 79 20 6f 66 20 xisting copy of
18612 61 20 72 6f 77 20 74 68 61 74 20 69 73 20 74 6f a row that is to
18613 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 be.** overwritt
18614 65 6e 2e 20 20 49 66 20 74 68 65 20 73 65 65 6b en. If the seek
18615 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 Result parameter
18616 20 69 73 20 30 2c 20 74 68 65 6e 20 63 75 72 73 is 0, then curs
18617 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a 2a 20 70 or pCur may.** p
18618 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65 6e 74 72 oint to any entr
18619 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e 74 72 79 y or to no entry
1861a 20 61 74 20 61 6c 6c 20 61 6e 64 20 73 6f 20 74 at all and so t
1861b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 his function has
1861c 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20 to seek.** the
1861d 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74 68 cursor before th
1861e 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62 65 e new key can be
1861f 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 inserted..*/.SQ
18620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18621 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
18622 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 ert(. BtCursor
18623 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 *pCur,
18624 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 /* Insert
18625 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 data into the ta
18626 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 ble of this curs
18627 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f or */. const vo
18628 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b id *pKey, i64 nK
18629 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ey, /* The ke
1862a 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 y of the new rec
1862b 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ord */. const v
1862c 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 oid *pData, int
1862d 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 nData, /* The d
1862e 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 ata of the new r
1862f 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
18630 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 Zero,
18631 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
18632 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 ber of extra 0 b
18633 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 ytes to append t
18634 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 o data */. int
18635 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20 20 appendBias,
18636 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
18637 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 ue if this is li
18638 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a kely an append *
18639 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 /. int seekResu
1863a 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 lt
1863b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 /* Result of
1863c 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55 6e 70 61 prior MovetoUnpa
1863d 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a 2f 0a 29 cked() call */.)
1863e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e {. int rc;. in
1863f 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 65 73 75 t loc = seekResu
18640 6c 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 lt; /*
18641 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 73 69 72 -1: before desir
18642 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 2b 31 3a ed location +1:
18643 20 61 66 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 after */. int
18644 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20 69 6e 74 szNew = 0;. int
18645 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 idx;. MemPage
18646 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65 20 *pPage;. Btree
18647 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 *p = pCur->pBtre
18648 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e;. BtShared *p
18649 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 Bt = p->pBt;. u
1864a 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c nsigned char *ol
1864b 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 dCell;. unsigne
1864c 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 d char *newCell
1864d 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75 72 = 0;.. if( pCur
1864e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1864f 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61 73 _FAULT ){. as
18650 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 sert( pCur->skip
18651 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 Next!=SQLITE_OK
18652 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 );. return pC
18653 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 ur->skipNext;.
18654 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 }.. assert( cur
18655 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
18656 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
18657 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 pCur->wrFlag &&
18658 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
18659 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1865a 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e && !pBt->readOn
1865b 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ly );. assert(
1865c 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 hasSharedCacheTa
1865d 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d bleLock(p, pCur-
1865e 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d >pgnoRoot, pCur-
1865f 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 >pKeyInfo!=0, 2)
18660 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 );.. /* Assert
18661 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 that the caller
18662 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69 73 has been consis
18663 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63 75 tent. If this cu
18664 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 0a rsor was opened.
18665 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20 61 ** expecting a
18666 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c 20 n index b-tree,
18667 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 then the caller
18668 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 74 should be insert
18669 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b 65 ing blob. ** ke
1866a 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f 63 ys with no assoc
1866b 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20 74 iated data. If t
1866c 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 he cursor was op
1866d 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20 61 ened expecting a
1866e 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74 61 n. ** intkey ta
1866f 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 ble, the caller
18670 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 74 should be insert
18671 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ing integer keys
18672 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c 6f with a. ** blo
18673 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64 20 b of associated
18674 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73 65 data. */. asse
18675 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d 28 rt( (pKey==0)==(
18676 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d pCur->pKeyInfo==
18677 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 0) );.. /* If t
18678 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 74 his is an insert
18679 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 2d into a table b-
1867a 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 65 tree, invalidate
1867b 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20 any incrblob .
1867c 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e ** cursors open
1867d 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e on the row bein
1867e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 75 g replaced (assu
1867f 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 72 ming this is a r
18680 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72 eplace. ** oper
18681 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 73 ation - if it is
18682 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 not, the follow
18683 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e ing is a no-op).
18684 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d */. if( pCur-
18685 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a >pKeyInfo==0 ){.
18686 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e invalidateIn
18687 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c crblobCursors(p,
18688 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a nKey, 0);. }..
18689 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f /* Save the po
1868a 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f sitions of any o
1868b 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 ther cursors ope
1868c 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e n on this table.
1868d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f . **. ** In so
1868e 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63 61 me cases, the ca
1868f 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 74 ll to btreeMovet
18690 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20 6e o() below is a n
18691 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65 o-op. For. ** e
18692 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73 xample, when ins
18693 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74 6f erting data into
18694 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61 75 a table with au
18695 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e 74 to-generated int
18696 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20 eger. ** keys,
18697 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 69 the VDBE layer i
18698 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 74 nvokes sqlite3Bt
18699 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69 67 reeLast() to fig
1869a 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20 2a ure out the . *
1869b 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74 6f * integer key to
1869c 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63 61 use. It then ca
1869d 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
1869e 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e n to actually in
1869f 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 64 sert the . ** d
186a0 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 ata into the int
186a1 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 key B-Tree. In t
186a2 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d 6f his case btreeMo
186a3 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65 veto() recognize
186a4 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 s. ** that the
186a5 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 cursor is alread
186a6 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64 73 y where it needs
186a7 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75 72 to be and retur
186a8 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 ns without. **
186a9 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20 doing any work.
186aa 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74 69 To avoid thwarti
186ab 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a ng these optimiz
186ac 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d ations, it is im
186ad 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74 portant. ** not
186ae 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63 75 to clear the cu
186af 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a rsor here.. */.
186b0 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 rc = saveAllCu
186b1 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d rsors(pBt, pCur-
186b2 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 >pgnoRoot, pCur)
186b3 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
186b4 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21 6c urn rc;. if( !l
186b5 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 oc ){. rc = b
186b6 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c treeMoveto(pCur,
186b7 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 70 pKey, nKey, app
186b8 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b 0a endBias, &loc);.
186b9 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
186ba 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
186bb 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
186bc 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
186bd 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61 74 || (pCur->eStat
186be 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 e==CURSOR_INVALI
186bf 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 20 D && loc) );..
186c0 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
186c1 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
186c2 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ];. assert( pPa
186c3 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b ge->intKey || nK
186c4 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ey>=0 );. asser
186c5 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c t( pPage->leaf |
186c6 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 | !pPage->intKey
186c7 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 49 );.. TRACE(("I
186c8 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64 20 NSERT: table=%d
186c9 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61 3d nkey=%lld ndata=
186ca 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e 22 %d page=%d %s\n"
186cb 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 ,. pCur
186cc 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 ->pgnoRoot, nKey
186cd 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d 3e , nData, pPage->
186ce 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 pgno,.
186cf 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77 72 loc==0 ? "overwr
186d0 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74 72 ite" : "new entr
186d1 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 y"));. assert(
186d2 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b pPage->isInit );
186d3 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 . allocateTempS
186d4 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65 77 pace(pBt);. new
186d5 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d 70 Cell = pBt->pTmp
186d6 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65 77 Space;. if( new
186d7 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e Cell==0 ) return
186d8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
186d9 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c rc = fillInCell
186da 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 2c (pPage, newCell,
186db 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44 61 pKey, nKey, pDa
186dc 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72 6f ta, nData, nZero
186dd 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66 28 , &szNew);. if(
186de 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 rc ) goto end_i
186df 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74 28 nsert;. assert(
186e0 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a 65 szNew==cellSize
186e1 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43 65 Ptr(pPage, newCe
186e2 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ll) );. assert(
186e3 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f szNew<=MX_CELL_
186e4 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 69 SIZE(pBt) );. i
186e5 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 dx = pCur->aiIdx
186e6 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
186e7 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a 20 if( loc==0 ){.
186e8 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20 u16 szOld;.
186e9 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 50 assert( idx<pP
186ea 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
186eb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
186ec 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
186ed 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
186ee 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 6f ( rc ){. go
186ef 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 to end_insert;.
186f0 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c }. oldCell
186f1 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
186f2 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 28 e, idx);. if(
186f3 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
186f4 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e 65 . memcpy(ne
186f5 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20 wCell, oldCell,
186f6 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4);. }. sz
186f7 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 Old = cellSizePt
186f8 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c r(pPage, oldCell
186f9 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 );. rc = clea
186fa 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64 rCell(pPage, old
186fb 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70 43 Cell);. dropC
186fc 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 ell(pPage, idx,
186fd 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 szOld, &rc);.
186fe 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 if( rc ) goto e
186ff 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65 6c nd_insert;. }el
18700 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 20 se if( loc<0 &&
18701 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 pPage->nCell>0 )
18702 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
18703 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 age->leaf );.
18704 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 idx = ++pCur->a
18705 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
18706 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
18707 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c assert( pPage->l
18708 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e 73 eaf );. }. ins
18709 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ertCell(pPage, i
1870a 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e dx, newCell, szN
1870b 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b 0a ew, 0, 0, &rc);.
1870c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
1870d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 LITE_OK || pPage
1870e 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 ->nCell>0 || pPa
1870f 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 ge->nOverflow>0
18710 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 65 );.. /* If no e
18711 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 rror has occured
18712 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20 61 and pPage has a
18713 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c n overflow cell,
18714 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20 call balance()
18715 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74 72 . ** to redistr
18716 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73 20 ibute the cells
18717 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65 2e within the tree.
18718 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28 29 Since balance()
18719 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 may move. ** t
1871a 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f 20 he cursor, zero
1871b 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 the BtCursor.inf
1871c 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43 75 o.nSize and BtCu
1871d 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a 20 rsor.validNKey.
1871e 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a 20 ** variables..
1871f 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f 75 **. ** Previou
18720 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 s versions of SQ
18721 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76 65 Lite called move
18722 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 65 ToRoot() to move
18723 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a the cursor. **
18724 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f 6f back to the roo
18725 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e 63 t page as balanc
18726 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76 61 e() used to inva
18727 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74 65 lidate the conte
18728 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43 75 nts. ** of BtCu
18729 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e rsor.apPage[] an
1872a 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64 78 d BtCursor.aiIdx
1872b 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 []. Instead of d
1872c 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a 20 oing that,. **
1872d 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 73 set the cursor s
1872e 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69 64 tate to "invalid
1872f 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63 6f ". This makes co
18730 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65 72 mmon insert oper
18731 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 67 ations. ** slig
18732 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20 2a htly faster.. *
18733 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 *. ** There is
18734 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d 70 a subtle but imp
18735 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61 74 ortant optimizat
18736 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57 68 ion here too. Wh
18737 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20 2a en inserting. *
18738 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f 72 * multiple recor
18739 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b 65 ds into an intke
1873a 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20 61 y b-tree using a
1873b 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20 28 single cursor (
1873c 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70 70 as can. ** happ
1873d 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73 73 en while process
1873e 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20 49 ing an "INSERT I
1873f 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22 20 NTO ... SELECT"
18740 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a 20 statement), it.
18741 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67 65 ** is advantage
18742 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68 65 ous to leave the
18743 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 cursor pointing
18744 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
18745 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 62 ry in. ** the b
18746 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62 6c -tree if possibl
18747 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 e. If the cursor
18748 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
18749 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20 20 g to the last.
1874a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 ** entry in the
1874b 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 6e table, and the n
1874c 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65 64 ext row inserted
1874d 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 has an integer
1874e 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 key. ** larger
1874f 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74 than the largest
18750 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20 69 existing key, i
18751 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f t is possible to
18752 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a 2a insert the. **
18753 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65 65 row without see
18754 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 2e king the cursor.
18755 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20 62 This can be a b
18756 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ig performance b
18757 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 75 oost.. */. pCu
18758 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
18759 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 0;. pCur->valid
1875a 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 NKey = 0;. if(
1875b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1875c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
1875d 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 61 w ){. rc = ba
1875e 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20 20 lance(pCur);..
1875f 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20 73 /* Must make s
18760 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69 73 ure nOverflow is
18761 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 65 reset to zero e
18762 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61 6e ven if the balan
18763 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c ce(). ** fail
18764 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74 61 s. Internal data
18765 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 75 structure corru
18766 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c ption will resul
18767 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20 20 t otherwise. .
18768 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20 74 ** Also, set t
18769 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 he cursor state
1876a 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69 73 to invalid. This
1876b 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73 6f stops saveCurso
1876c 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20 20 rPosition().
1876d 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 ** from trying t
1876e 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 o save the curre
1876f 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 nt position of t
18770 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a 20 he cursor. */.
18771 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
18772 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f pCur->iPage]->nO
18773 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 verflow = 0;.
18774 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
18775 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
18776 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
18777 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18778 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c >iPage]->nOverfl
18779 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 6e ow==0 );..end_in
1877a 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 sert:. return r
1877b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 c;.}../*.** Dele
1877c 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 te the entry tha
1877d 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 t the cursor is
1877e 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 pointing to. Th
1877f 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c e cursor.** is l
18780 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
18781 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 61 a arbitrary loca
18782 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
18783 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18784 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 te3BtreeDelete(B
18785 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
18786 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 Btree *p = pCu
18787 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 r->pBtree;. BtS
18788 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
18789 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 pBt;
1878a 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 . int rc;
1878b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1878c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1878d 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d 65 urn code */. Me
1878e 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 mPage *pPage;
1878f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18790 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64 65 /* Page to de
18791 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20 2a lete cell from *
18792 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
18793 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 r *pCell;
18794 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
18795 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20 64 ter to cell to d
18796 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 elete */. int i
18797 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20 20 CellIdx;
18798 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18799 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c 6c /* Index of cell
1879a 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
1879b 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b 20 int iCellDepth;
1879c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1879d 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 /* Depth of
1879e 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 node containing
1879f 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 73 pCell */ .. as
187a0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
187a1 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
187a2 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
187a3 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
187a4 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
187a5 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 ssert( !pBt->rea
187a6 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 dOnly );. asser
187a7 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 t( pCur->wrFlag
187a8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 );. assert( has
187a9 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
187aa 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 Lock(p, pCur->pg
187ab 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b noRoot, pCur->pK
187ac 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b eyInfo!=0, 2) );
187ad 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 . assert( !hasR
187ae 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 eadConflicts(p,
187af 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 pCur->pgnoRoot)
187b0 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );.. if( NEVER(
187b1 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
187b2 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e ->iPage]>=pCur->
187b3 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
187b4 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20 ge]->nCell) .
187b5 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 || NEVER(pCur->e
187b6 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
187b7 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 LID). ){. re
187b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
187b9 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 R; /* Something
187ba 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e 20 has gone awry.
187bb 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 */. }.. /* If
187bc 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74 65 this is a delete
187bd 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72 65 operation to re
187be 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d 20 move a row from
187bf 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 0a a table b-tree,.
187c0 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 20 ** invalidate
187c1 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 any incrblob cur
187c2 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 sors open on the
187c3 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 row being delet
187c4 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 ed. */. if( pC
187c5 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 ur->pKeyInfo==0
187c6 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 ){. invalidat
187c7 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 eIncrblobCursors
187c8 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e (p, pCur->info.n
187c9 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 Key, 0);. }..
187ca 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43 75 iCellDepth = pCu
187cb 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65 6c r->iPage;. iCel
187cc 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 lIdx = pCur->aiI
187cd 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b 0a dx[iCellDepth];.
187ce 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
187cf 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 apPage[iCellDept
187d0 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 h];. pCell = fi
187d1 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 ndCell(pPage, iC
187d2 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 49 ellIdx);.. /* I
187d3 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 f the page conta
187d4 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20 ining the entry
187d5 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f 74 to delete is not
187d6 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d 6f a leaf page, mo
187d7 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 ve. ** the curs
187d8 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 or to the larges
187d9 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 t entry in the t
187da 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61 6c ree that is smal
187db 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68 ler than. ** th
187dc 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64 65 e entry being de
187dd 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c 6c leted. This cell
187de 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74 68 will replace th
187df 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c e cell being del
187e0 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 eted. ** from t
187e1 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
187e2 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73 27 . The 'previous'
187e3 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20 66 entry is used f
187e4 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64 0a or this instead.
187e5 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65 78 ** of the 'nex
187e6 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68 65 t' entry, as the
187e7 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 previous entry
187e8 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72 74 is always a part
187e9 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 of the. ** sub
187ea 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 -tree headed by
187eb 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 6f the child page o
187ec 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 f the cell being
187ed 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 6d deleted. This m
187ee 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 akes. ** balanc
187ef 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f 6c ing the tree fol
187f0 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65 74 lowing the delet
187f1 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73 69 e operation easi
187f2 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 er. */. if( !p
187f3 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
187f4 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 int notUsed;.
187f5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
187f6 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75 treePrevious(pCu
187f7 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 r, ¬Used);.
187f8 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
187f9 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 n rc;. }.. /*
187fa 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f Save the positio
187fb 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 ns of any other
187fc 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 cursors open on
187fd 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f 72 this table befor
187fe 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 6e e. ** making an
187ff 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 2e y modifications.
18800 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20 63 Make the page c
18801 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e ontaining the en
18802 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 try to be . **
18803 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c 65 deleted writable
18804 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79 20 . Then free any
18805 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 overflow pages a
18806 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
18807 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20 61 he . ** entry a
18808 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f 76 nd finally remov
18809 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c e the cell itsel
1880a 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 f from within th
1880b 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a 20 e page. . */.
1880c 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 rc = saveAllCur
1880d 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e sors(pBt, pCur->
1880e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b pgnoRoot, pCur);
1880f 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
18810 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 rn rc;. rc = sq
18811 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
18812 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
18813 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
18814 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c rn rc;. rc = cl
18815 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 earCell(pPage, p
18816 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c Cell);. dropCel
18817 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 l(pPage, iCellId
18818 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 x, cellSizePtr(p
18819 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72 Page, pCell), &r
1881a 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 c);. if( rc ) r
1881b 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 eturn rc;.. /*
1881c 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 If the cell dele
1881d 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61 ted was not loca
1881e 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 ted on a leaf pa
1881f 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 ge, then the cur
18820 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 72 sor. ** is curr
18821 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
18822 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e o the largest en
18823 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d 74 try in the sub-t
18824 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a 20 ree headed. **
18825 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 67 by the child-pag
18826 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 e of the cell th
18827 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c 65 at was just dele
18828 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 ted from an inte
18829 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 rnal. ** node.
1882a 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 The cell from th
1882b 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 64 e leaf node need
1882c 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f s to be moved to
1882d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 the internal.
1882e 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 ** node to repla
1882f 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 63 ce the deleted c
18830 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 ell. */. if( !
18831 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
18832 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 MemPage *pLea
18833 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 f = pCur->apPage
18834 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
18835 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 int nCell;.
18836 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d Pgno n = pCur-
18837 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 >apPage[iCellDep
18838 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 th+1]->pgno;.
18839 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1883a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c pTmp;.. pCell
1883b 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61 = findCell(pLea
1883c 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d f, pLeaf->nCell-
1883d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 1);. nCell =
1883e 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 cellSizePtr(pLea
1883f 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 f, pCell);. a
18840 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 ssert( MX_CELL_S
18841 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c 20 IZE(pBt)>=nCell
18842 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74 65 );.. allocate
18843 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
18844 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d 3e pTmp = pBt->
18845 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20 20 pTmpSpace;..
18846 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
18847 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70 44 rWrite(pLeaf->pD
18848 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73 65 bPage);. inse
18849 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 rtCell(pPage, iC
1884a 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 2c ellIdx, pCell-4,
1884b 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c 20 nCell+4, pTmp,
1884c 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72 6f n, &rc);. dro
1884d 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c 65 pCell(pLeaf, pLe
1884e 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 65 af->nCell-1, nCe
1884f 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 ll, &rc);. if
18850 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
18851 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c 61 ;. }.. /* Bala
18852 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49 66 nce the tree. If
18853 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 74 the entry delet
18854 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f ed was located o
18855 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a 20 n a leaf page,.
18856 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75 72 ** then the cur
18857 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 sor still points
18858 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 49 to that page. I
18859 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
1885a 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c 20 first. ** call
1885b 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65 70 to balance() rep
1885c 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20 61 airs the tree, a
1885d 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 63 nd the if(...) c
1885e 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a 2a ondition is. **
1885f 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20 2a never true.. *
18860 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 *. ** Otherwise
18861 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 64 , if the entry d
18862 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61 6e eleted was on an
18863 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 70 internal node p
18864 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 age, then. ** p
18865 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 Cur is pointing
18866 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67 65 to the leaf page
18867 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63 65 from which a ce
18868 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20 74 ll was removed t
18869 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20 74 o. ** replace t
1886a 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 20 he cell deleted
1886b 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 from the interna
1886c 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73 20 l node. This is
1886d 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74 72 slightly. ** tr
1886e 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61 66 icky as the leaf
1886f 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e 64 node may be und
18870 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65 20 erfull, and the
18871 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d 61 internal node ma
18872 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65 72 y. ** be either
18873 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66 75 under or overfu
18874 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ll. In this case
18875 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 run the balanci
18876 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 2a ng algorithm. *
18877 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f * on the leaf no
18878 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 de first. If the
18879 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65 64 balance proceed
1887a 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70 20 s far enough up
1887b 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74 68 the. ** tree th
1887c 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75 72 at we can be sur
1887d 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62 6c e that any probl
1887e 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72 6e em in the intern
1887f 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a 2a al node has. **
18880 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 2c been corrected,
18881 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 72 so be it. Other
18882 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c 61 wise, after bala
18883 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 6e ncing the leaf n
18884 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 74 ode,. ** walk t
18885 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 65 he cursor up the
18886 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e 74 tree to the int
18887 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 62 ernal node and b
18888 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20 20 alance it as .
18889 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 72 ** well. */. r
1888a 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 c = balance(pCur
1888b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1888c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d 3e ITE_OK && pCur->
1888d 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74 68 iPage>iCellDepth
1888e 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 ){. while( p
1888f 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c Cur->iPage>iCell
18890 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 72 Depth ){. r
18891 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d eleasePage(pCur-
18892 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18893 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a 20 age--]);. }.
18894 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 rc = balance(
18895 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 pCur);. }.. if
18896 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
18897 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f ){. moveToRoo
18898 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 t(pCur);. }. r
18899 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1889a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
1889b 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 BTree table. Wr
1889c 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c ite into *piTabl
1889d 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 e the page.** nu
1889e 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f mber for the roo
1889f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 t page of the ne
188a0 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 w table..**.** T
188a1 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 he type of type
188a2 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
188a3 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
188a4 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a eter. Only the.
188a5 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c ** following val
188a6 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 ues of flags are
188a7 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 currently in us
188a8 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 e. Other values
188a9 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 for.** flags mi
188aa 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a ght not work:.**
188ab 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e .** BTREE_IN
188ac 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 TKEY|BTREE_LEAFD
188ad 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 ATA Used for
188ae 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 SQL tables with
188af 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 rowid keys.**
188b0 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 BTREE_ZERODAT
188b1 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 A
188b2 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 Used for SQL
188b3 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 indices.*/.stati
188b4 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 c int btreeCreat
188b5 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c eTable(Btree *p,
188b6 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 int *piTable, i
188b7 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 nt flags){. BtS
188b8 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
188b9 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a pBt;. MemPage *
188ba 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 pRoot;. Pgno pg
188bb 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 noRoot;. int rc
188bc 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
188bd 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
188be 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
188bf 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
188c0 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
188c1 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
188c2 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 !pBt->readOnly
188c3 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
188c4 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
188c5 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 M. rc = allocat
188c6 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
188c7 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f &pRoot, &pgnoRoo
188c8 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 t, 1, 0);. if(
188c9 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
188ca 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 rc;. }.#else.
188cb 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
188cc 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
188cd 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 pgnoMove;
188ce 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68 /* Move a page h
188cf 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d ere to make room
188d0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 for the root-pa
188d1 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 ge */. MemPag
188d2 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a e *pPageMove; /*
188d3 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 The page to mov
188d4 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a e to. */.. /*
188d5 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 Creating a new
188d6 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 table may probab
188d7 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e ly require movin
188d8 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61 g an existing da
188d9 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f tabase. ** to
188da 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 make room for t
188db 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f he new tables ro
188dc 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65 ot page. In case
188dd 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73 this page turns
188de 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 . ** out to b
188df 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
188e0 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f ge, delete all o
188e1 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 verflow page-map
188e2 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 caches. ** h
188e3 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 eld by open curs
188e4 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ors.. */.
188e5 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
188e6 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b rflowCache(pBt);
188e7 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 .. /* Read th
188e8 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b e value of meta[
188e9 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 3] from the data
188ea 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e base to determin
188eb 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20 e where the.
188ec 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 ** root page of
188ed 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68 the new table sh
188ee 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d ould go. meta[3]
188ef 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 is the largest
188f0 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a root-page. **
188f1 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c created so far,
188f2 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 so the new root
188f3 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33 -page is (meta[3
188f4 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ]+1).. */.
188f5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
188f6 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 Meta(p, BTREE_LA
188f7 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c RGEST_ROOT_PAGE,
188f8 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 &pgnoRoot);.
188f9 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 pgnoRoot++;..
188fa 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f /* The new roo
188fb 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 t-page may not b
188fc 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 e allocated on a
188fd 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
188fe 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a e, or the. **
188ff 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 PENDING_BYTE pa
18900 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 ge.. */. w
18901 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d hile( pgnoRoot==
18902 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
18903 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a t, pgnoRoot) ||.
18904 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 pgnoRoot
18905 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
18906 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
18907 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 pgnoRoot++;.
18908 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
18909 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a pgnoRoot>=3 );..
1890a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 /* Allocate
1890b 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 a page. The page
1890c 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 that currently
1890d 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 resides at pgnoR
1890e 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 oot will. **
1890f 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 be moved to the
18910 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 allocated page (
18911 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 unless the alloc
18912 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e ated page happen
18913 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 s. ** to resi
18914 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e de at pgnoRoot).
18915 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
18916 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
18917 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f ge(pBt, &pPageMo
18918 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 ve, &pgnoMove, p
18919 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 gnoRoot, 1);.
1891a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1891b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
1891c 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
1891d 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d if( pgnoMove!=
1891e 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 pgnoRoot ){.
1891f 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 /* pgnoRoot is
18920 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 the page that w
18921 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 ill be used for
18922 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 the root-page of
18923 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 . ** the ne
18924 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e w table (assumin
18925 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e g an error did n
18926 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 ot occur). But w
18927 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 e were. **
18928 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f allocated pgnoMo
18929 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 ve. If required
1892a 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 (i.e. if it was
1892b 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 not allocated.
1892c 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 ** by extend
1892d 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 ing the file), t
1892e 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 he current page
1892f 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f at position pgno
18930 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 Move. ** is
18931 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c already journal
18932 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
18933 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30 3b u8 eType = 0;
18934 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 . Pgno iPtr
18935 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20 Page = 0;..
18936 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
18937 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20 20 geMove);..
18938 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 /* Move the page
18939 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70 67 currently at pg
1893a 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f noRoot to pgnoMo
1893b 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 ve. */. rc
1893c 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
1893d 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 Bt, pgnoRoot, &p
1893e 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 Root, 0);.
1893f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
18940 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
18941 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
18942 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
18943 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f pGet(pBt, pgnoRo
18944 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 ot, &eType, &iPt
18945 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 rPage);. if
18946 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
18947 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 ROOTPAGE || eTyp
18948 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e==PTRMAP_FREEPA
18949 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 GE ){. rc
1894a 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
1894b 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a T_BKPT;. }.
1894c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1894d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1894e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1894f 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 Root);. r
18950 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
18951 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
18952 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f eType!=PTRMAP_RO
18953 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 OTPAGE );.
18954 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 assert( eType!=P
18955 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
18956 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c ;. rc = rel
18957 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 ocatePage(pBt, p
18958 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74 Root, eType, iPt
18959 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c rPage, pgnoMove,
1895a 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 0);. relea
1895b 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a sePage(pRoot);..
1895c 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 /* Obtain
1895d 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e 6f the page at pgno
1895e 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 Root */. if
1895f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18960 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
18961 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
18962 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
18963 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 tPage(pBt, pgnoR
18964 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b oot, &pRoot, 0);
18965 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
18966 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18967 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18968 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
18969 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1896a 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 ite(pRoot->pDbPa
1896b 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
1896c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1896d 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1896e 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 age(pRoot);.
1896f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18970 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
18971 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 {. pRoot =
18972 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d pPageMove;. }
18973 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 .. /* Update
18974 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
18975 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 77 and meta-data w
18976 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 ith the new root
18977 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f -page number. */
18978 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 . ptrmapPut(p
18979 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 54 Bt, pgnoRoot, PT
1897a 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 RMAP_ROOTPAGE, 0
1897b 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 , &rc);. if(
1897c 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 rc ){. rele
1897d 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
1897e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1897f 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
18980 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 sqlite3BtreeUpda
18981 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 6e teMeta(p, 4, pgn
18982 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 20 oRoot);. if(
18983 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 rc ){. rele
18984 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
18985 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
18986 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b . }.. }else{
18987 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 . rc = alloca
18988 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
18989 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f &pRoot, &pgnoRo
1898a 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 ot, 1, 0);. i
1898b 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1898c 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 c;. }.#endif.
1898d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1898e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
1898f 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 pRoot->pDbPage)
18990 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52 );. zeroPage(pR
18991 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 46 oot, flags | PTF
18992 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 65 _LEAF);. sqlite
18993 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f 6f 3PagerUnref(pRoo
18994 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 2a t->pDbPage);. *
18995 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 70 piTable = (int)p
18996 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72 gnoRoot;. retur
18997 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 n SQLITE_OK;.}.S
18998 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18999 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 t sqlite3BtreeCr
1899a 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 eateTable(Btree
1899b 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 *p, int *piTable
1899c 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1899d 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
1899e 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1899f 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65 61 rc = btreeCrea
189a0 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61 62 teTable(p, piTab
189a1 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73 71 le, flags);. sq
189a2 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
189a3 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
189a4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 .}../*.** Erase
189a5 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 the given databa
189a6 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 se page and all
189a7 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20 52 its children. R
189a8 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61 67 eturn.** the pag
189a9 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 e to the freelis
189aa 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
189ab 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 clearDatabasePa
189ac 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
189ad 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f pBt, /
189ae 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61 74 * The BTree that
189af 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61 contains the ta
189b0 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ble */. Pgno pg
189b1 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 no,
189b2 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
189b3 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 to clear */. i
189b4 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c nt freePageFlag,
189b5 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c /* Deall
189b6 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72 ocate page if tr
189b7 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 ue */. int *pnC
189b8 68 61 6e 67 65 20 20 20 20 20 20 20 20 20 20 20 hange
189b9 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65 72 20 6f /* Add number o
189ba 66 20 43 65 6c 6c 73 20 66 72 65 65 64 20 74 6f f Cells freed to
189bb 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 2a 2f this counter */
189bc 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
189bd 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Page;. int rc;.
189be 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
189bf 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b *pCell;. int i;
189c0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
189c1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
189c2 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
189c3 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61 if( pgno>pagerPa
189c4 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
189c5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
189c6 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
189c7 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41 }.. rc = getA
189c8 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 ndInitPage(pBt,
189c9 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20 pgno, &pPage);.
189ca 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
189cb 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 rc;. for(i=0;
189cc 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 i<pPage->nCell;
189cd 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20 i++){. pCell
189ce 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
189cf 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70 , i);. if( !p
189d0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
189d1 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 rc = clearDa
189d2 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 tabasePage(pBt,
189d3 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c get4byte(pCell),
189d4 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
189d5 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
189d6 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 to cleardatabase
189d7 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
189d8 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65 rc = clearCe
189d9 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 ll(pPage, pCell)
189da 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
189db 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
189dc 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 epage_out;. }.
189dd 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
189de 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c f ){. rc = cl
189df 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
189e0 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70 pBt, get4byte(&p
189e1 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c Page->aData[8]),
189e2 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
189e3 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
189e4 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 cleardatabasepa
189e5 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20 ge_out;. }else
189e6 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a if( pnChange ){.
189e7 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
189e8 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 e->intKey );.
189e9 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50 *pnChange += pP
189ea 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a age->nCell;. }.
189eb 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c if( freePageFl
189ec 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61 ag ){. freePa
189ed 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a ge(pPage, &rc);.
189ee 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 }else if( (rc
189ef 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
189f0 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
189f1 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a ge))==0 ){. z
189f2 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70 eroPage(pPage, p
189f3 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c Page->aData[0] |
189f4 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a PTF_LEAF);. }.
189f5 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 .cleardatabasepa
189f6 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73 ge_out:. releas
189f7 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
189f8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
189f9 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 .** Delete all i
189fa 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
189fb 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 a single table i
189fc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
189fd 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 iTable is.** th
189fe 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
189ff 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18a00 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74 table. After t
18a01 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
18a02 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 rns,.** the root
18a03 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20 page is empty,
18a04 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73 but still exists
18a05 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
18a06 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77 tine will fail w
18a07 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ith SQLITE_LOCKE
18a08 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 D if there are a
18a09 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20 ny open.** read
18a0a 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 cursors on the t
18a0b 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74 able. Open writ
18a0c 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f e cursors are mo
18a0d 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f ved to the.** ro
18a0e 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e ot of the table.
18a0f 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e .**.** If pnChan
18a10 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ge is not NULL,
18a11 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c then table iTabl
18a12 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74 e must be an int
18a13 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a key table. The.*
18a14 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * integer value
18a15 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e pointed to by pn
18a16 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d Change is increm
18a17 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
18a18 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 ber of.** entrie
18a19 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a s in the table..
18a1a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18a1b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18a1c 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 eeClearTable(Btr
18a1d 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c ee *p, int iTabl
18a1e 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65 e, int *pnChange
18a1f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
18a20 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
18a21 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
18a22 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
18a23 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
18a24 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
18a25 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 );.. /* Invali
18a26 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
18a27 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f b cursors open o
18a28 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28 n table iTable (
18a29 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a assuming iTable.
18a2a 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74 ** is the root
18a2b 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72 of a table b-tr
18a2c 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f ee - if it is no
18a2d 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
18a2e 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20 call is. ** a
18a2f 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e no-op). */. in
18a30 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 validateIncrblob
18a31 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29 Cursors(p, 0, 1)
18a32 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c ;.. rc = saveAl
18a33 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50 lCursors(pBt, (P
18a34 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a gno)iTable, 0);.
18a35 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
18a36 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 =rc ){. rc =
18a37 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 clearDatabasePag
18a38 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 e(pBt, (Pgno)iTa
18a39 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 ble, 0, pnChange
18a3a 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
18a3b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18a3c 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18a3d 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 *.** Erase all i
18a3e 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 nformation in a
18a3f 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 table and add th
18a40 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 e root of the ta
18a41 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 ble to.** the fr
18a42 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c eelist. Except,
18a43 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18a44 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 principle table
18a45 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 (the one on.**
18a46 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 page 1) is never
18a47 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 added to the fr
18a48 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 eelist..**.** Th
18a49 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
18a4a 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 fail with SQLITE
18a4b 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 _LOCKED if there
18a4c 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a are any open.**
18a4d 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
18a4e 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 table..**.** If
18a4f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e AUTOVACUUM is en
18a50 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 abled and the pa
18a51 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 ge at iTable is
18a52 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 not the last.**
18a53 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
18a54 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
18a55 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f then the last ro
18a56 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 ot page .** in t
18a57 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
18a58 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 is moved into t
18a59 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 he slot formerly
18a5a 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 occupied by.**
18a5b 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 iTable and that
18a5c 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 last slot former
18a5d 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 ly occupied by t
18a5e 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
18a5f 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f e.** is added to
18a60 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
18a61 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e stead of iTable.
18a62 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 In this say, a
18a63 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 ll.** root pages
18a64 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 are kept at the
18a65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
18a66 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
18a67 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 which.** is nec
18a68 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 essary for AUTOV
18a69 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 ACUUM to work ri
18a6a 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 ght. *piMoved i
18a6b 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a s set to the .**
18a6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 page number tha
18a6d 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 t used to be the
18a6e 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 last root page
18a6f 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f in the file befo
18a70 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 re.** the move.
18a71 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 If no page gets
18a72 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 moved, *piMoved
18a73 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a is set to 0..**
18a74 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 The last root p
18a75 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 age is recorded
18a76 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 in meta[3] and t
18a77 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d he value of.** m
18a78 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 eta[3] is update
18a79 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 d by this proced
18a7a 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
18a7b 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c nt btreeDropTabl
18a7c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f e(Btree *p, Pgno
18a7d 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 iTable, int *pi
18a7e 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 Moved){. int rc
18a7f 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
18a80 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 ge = 0;. BtShar
18a81 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18a82 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
18a83 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
18a84 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
18a85 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
18a86 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a TRANS_WRITE );..
18a87 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 /* It is illeg
18a88 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 al to drop a tab
18a89 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 le if any cursor
18a8a 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 s are open on th
18a8b 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e e. ** database.
18a8c 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
18a8d 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
18a8e 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 mode the backend
18a8f 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 may. ** need t
18a90 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 o move another r
18a91 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c oot-page to fill
18a92 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 a gap left by t
18a93 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 he deleted. **
18a94 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e root page. If an
18a95 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 open cursor was
18a96 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 using this page
18a97 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 a problem would
18a98 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 . ** occur..
18a99 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72 **. ** This err
18a9a 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e or is caught lon
18a9b 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c g before control
18a9c 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f reaches this po
18a9d 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 int.. */. if(
18a9e 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73 NEVER(pBt->pCurs
18a9f 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 or) ){. sqlit
18aa0 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
18aa1 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
18aa2 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d pCursor->pBtree-
18aa3 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e >db);. return
18aa4 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
18aa5 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a HAREDCACHE;. }.
18aa6 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
18aa7 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 Page(pBt, (Pgno)
18aa8 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 iTable, &pPage,
18aa9 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 0);. if( rc ) r
18aaa 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
18aab 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
18aac 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c arTable(p, iTabl
18aad 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 e, 0);. if( rc
18aae 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
18aaf 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 ge(pPage);. r
18ab0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
18ab1 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a *piMoved = 0;..
18ab2 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 if( iTable>1 )
18ab3 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
18ab4 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18ab5 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 freePage(pPa
18ab6 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65 ge, &rc);. re
18ab7 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18ab8 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 ;.#else. if(
18ab9 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18aba 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 ){. Pgno ma
18abb 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 xRootPgno;.
18abc 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
18abd 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 Meta(p, BTREE_LA
18abe 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c RGEST_ROOT_PAGE,
18abf 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a &maxRootPgno);.
18ac0 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c . if( iTabl
18ac1 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 e==maxRootPgno )
18ac2 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
18ac3 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
18ac4 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 dropped is the t
18ac5 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 able with the la
18ac6 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
18ac7 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
18ac8 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 r in the databas
18ac9 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 e, put the root
18aca 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 page on the free
18acb 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 list. .
18acc 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 */. freeP
18acd 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b age(pPage, &rc);
18ace 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
18acf 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
18ad0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18ad1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18ad2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18ad3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
18ad4 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
18ad5 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 The table being
18ad6 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f dropped does no
18ad7 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 t have the large
18ad8 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
18ad9 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 ** number i
18ada 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
18adb 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 So move the page
18adc 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 that does into
18add 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
18ade 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 gap left by the
18adf 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 deleted root-pag
18ae0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
18ae1 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
18ae2 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 Move;. re
18ae3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18ae4 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 ;. rc = b
18ae5 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
18ae6 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 maxRootPgno, &p
18ae7 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Move, 0);.
18ae8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18ae9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
18aea 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
18aeb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
18aec 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
18aed 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d pBt, pMove, PTRM
18aee 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 AP_ROOTPAGE, 0,
18aef 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20 iTable, 0);.
18af0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18af1 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 pMove);.
18af2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
18af3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
18af4 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
18af5 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76 }. pMov
18af6 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 e = 0;. r
18af7 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
18af8 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e (pBt, maxRootPgn
18af9 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 o, &pMove, 0);.
18afa 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 freePage(
18afb 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20 pMove, &rc);.
18afc 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
18afd 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 (pMove);.
18afe 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18aff 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
18b00 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18b01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69 }. *pi
18b02 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50 Moved = maxRootP
18b03 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 gno;. }..
18b04 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e /* Set the n
18b05 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 ew 'max-root-pag
18b06 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 e' value in the
18b07 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e database header.
18b08 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 This. ** i
18b09 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 s the old value
18b0a 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f less one, less o
18b0b 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20 ne more if that
18b0c 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20 happens to.
18b0d 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 ** be a root-pa
18b0e 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 ge number, less
18b0f 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61 one again if tha
18b10 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a t is the. *
18b11 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 * PENDING_BYTE_P
18b12 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 AGE.. */.
18b13 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d maxRootPgno-
18b14 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 -;. while(
18b15 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e maxRootPgno==PEN
18b16 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
18b17 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 Bt).
18b18 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47 || PTRMAP_ISPAG
18b19 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 E(pBt, maxRootPg
18b1a 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d no) ){. m
18b1b 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 axRootPgno--;.
18b1c 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
18b1d 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 rt( maxRootPgno!
18b1e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
18b1f 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 GE(pBt) );..
18b20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
18b21 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c reeUpdateMeta(p,
18b22 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 4, maxRootPgno)
18b23 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
18b24 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67 freePage(pPag
18b25 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72 e, &rc);. r
18b26 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
18b27 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
18b28 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
18b29 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 If sqlite3BtreeD
18b2a 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c ropTable was cal
18b2b 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20 led on page 1..
18b2c 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c ** This reall
18b2d 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68 y never should h
18b2e 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20 appen except in
18b2f 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a a corrupt. **
18b30 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20 database. .
18b31 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 */. zeroPage(
18b32 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45 pPage, PTF_INTKE
18b33 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20 Y|PTF_LEAF );.
18b34 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18b35 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 age);. }. retu
18b36 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 rn rc; .}.SQLIT
18b37 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18b38 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
18b39 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
18b3a 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 t iTable, int *p
18b3b 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 iMoved){. int r
18b3c 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
18b3d 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
18b3e 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 = btreeDropTable
18b3f 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f (p, iTable, piMo
18b40 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ved);. sqlite3B
18b41 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
18b42 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
18b43 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
18b44 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 on may only be c
18b45 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74 alled if the b-t
18b46 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 ree connection a
18b47 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20 lready.** has a
18b48 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
18b49 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f ansaction open o
18b4a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
18b4b 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d **.** Read the m
18b4c 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 eta-information
18b4d 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 out of a databas
18b4e 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d e file. Meta[0]
18b4f 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 .** is the numbe
18b50 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 r of free pages
18b51 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
18b52 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 database. Meta
18b53 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d [1].** through m
18b54 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 eta[15] are avai
18b55 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 lable for use by
18b56 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 higher layers.
18b57 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 Meta[0].** is r
18b58 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 ead-only, the ot
18b59 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 hers are read/wr
18b5a 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 ite..** .** The
18b5b 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d schema layer num
18b5c 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 bers meta values
18b5d 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 differently. A
18b5e 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 t the schema.**
18b5f 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 layer (and the S
18b60 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 etCookie and Rea
18b61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 dCookie opcodes)
18b62 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
18b63 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20 * free pages is
18b64 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f not visible. So
18b65 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 Cookie[0] is th
18b66 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 e same as Meta[1
18b67 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ]..*/.SQLITE_PRI
18b68 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18b69 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 3BtreeGetMeta(Bt
18b6a 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c ree *p, int idx,
18b6b 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 u32 *pMeta){.
18b6c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
18b6d 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 p->pBt;.. sqlit
18b6e 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18b6f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
18b70 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
18b71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 );. assert( SQ
18b72 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 LITE_OK==querySh
18b73 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18b74 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f ck(p, MASTER_ROO
18b75 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b T, READ_LOCK) );
18b76 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
18b77 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65 pPage1 );. asse
18b78 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 rt( idx>=0 && id
18b79 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65 x<=15 );.. *pMe
18b7a 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ta = get4byte(&p
18b7b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
18b7c 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a a[36 + idx*4]);.
18b7d 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61 . /* If auto-va
18b7e 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64 cuum is disabled
18b7f 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61 in this build a
18b80 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 nd this is an au
18b81 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64 to-vacuum. ** d
18b82 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68 atabase, mark th
18b83 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65 e database as re
18b84 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66 ad-only. */.#if
18b85 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18b86 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 AUTOVACUUM. if(
18b87 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47 idx==BTREE_LARG
18b88 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26 EST_ROOT_PAGE &&
18b89 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d *pMeta>0 ) pBt-
18b8a 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 >readOnly = 1;.#
18b8b 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
18b8c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d BtreeLeave(p);.}
18b8d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65 ../*.** Write me
18b8e 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 ta-information b
18b8f 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
18b90 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20 abase. Meta[0]
18b91 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 is.** read-only
18b92 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77 and may not be w
18b93 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ritten..*/.SQLIT
18b94 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18b95 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
18b96 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 Meta(Btree *p, i
18b97 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74 nt idx, u32 iMet
18b98 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a a){. BtShared *
18b99 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
18b9a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
18b9b 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 P1;. int rc;.
18b9c 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26 assert( idx>=1 &
18b9d 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73 & idx<=15 );. s
18b9e 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
18b9f 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
18ba0 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
18ba1 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
18ba2 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21 rt( pBt->pPage1!
18ba3 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42 =0 );. pP1 = pB
18ba4 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
18ba5 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
18ba6 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
18ba7 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
18ba8 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
18ba9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74 TE_OK ){. put
18baa 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 4byte(&pP1[36 +
18bab 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a idx*4], iMeta);.
18bac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18bad 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18bae 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 if( idx==BTRE
18baf 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b E_INCR_VACUUM ){
18bb0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
18bb1 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c Bt->autoVacuum |
18bb2 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20 | iMeta==0 );.
18bb3 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 assert( iMet
18bb4 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 a==0 || iMeta==1
18bb5 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 );. pBt->i
18bb6 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29 ncrVacuum = (u8)
18bb7 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e iMeta;. }.#en
18bb8 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 dif. }. sqlite
18bb9 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
18bba 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
18bbb 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18bbc 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f MIT_BTREECOUNT./
18bbd 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
18bbe 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 rgument, pCur, i
18bbf 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 s a cursor opene
18bc0 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 d on some b-tree
18bc1 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e . Count the.** n
18bc2 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
18bc3 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 in the b-tree a
18bc4 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73 nd write the res
18bc5 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e ult to *pnEntry.
18bc6 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
18bc7 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
18bc8 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 the operation is
18bc9 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 successfully ex
18bca 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 ecuted. .** Othe
18bcb 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 rwise, if an err
18bcc 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
18bcd 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 d (i.e. an IO er
18bce 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a ror or database.
18bcf 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 ** corruption) a
18bd0 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
18bd1 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
18bd2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18bd3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
18bd4 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f reeCount(BtCurso
18bd5 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e r *pCur, i64 *pn
18bd6 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 Entry){. i64 nE
18bd7 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 ntry = 0;
18bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18bd9 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
18bda 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f n in *pnEntry */
18bdb 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
18bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18bdd 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
18bde 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d n code */. rc =
18bdf 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
18be0 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 );.. /* Unless
18be1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
18be2 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c the following l
18be3 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65 oop runs one ite
18be4 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a ration for each.
18be5 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 ** page in the
18be6 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 B-Tree structur
18be7 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 e (not including
18be8 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 overflow pages)
18be9 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
18bea 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
18beb 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20 {. int iIdx;
18bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18bed 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
18bee 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 x of child node
18bef 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20 in parent */.
18bf0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
18bf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18bf2 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 /* Current p
18bf3 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 age of the b-tre
18bf4 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 e */.. /* If
18bf5 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70 this is a leaf p
18bf6 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20 age or the tree
18bf7 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 is not an int-ke
18bf8 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20 y tree, then .
18bf9 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63 ** this page c
18bfa 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c ontains countabl
18bfb 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65 e entries. Incre
18bfc 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63 ment the entry c
18bfd 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63 ounter. ** ac
18bfe 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a cordingly.. *
18bff 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 /. pPage = pC
18c00 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18c01 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 >iPage];. if(
18c02 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 pPage->leaf ||
18c03 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 !pPage->intKey )
18c04 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b {. nEntry +
18c05 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
18c06 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 }.. /* pP
18c07 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f age is a leaf no
18c08 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61 de. This loop na
18c09 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73 vigates the curs
18c0a 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20 or so that it .
18c0b 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 ** points to
18c0c 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69 the first interi
18c0d 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20 or cell that it
18c0e 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61 points to the pa
18c0f 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 rent of. ** t
18c10 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
18c11 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61 the tree that ha
18c12 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76 s not yet been v
18c13 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20 isited. The.
18c14 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ** pCur->aiIdx[p
18c15 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75 Cur->iPage] valu
18c16 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
18c17 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 index of the par
18c18 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 ent cell. **
18c19 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 of the page, or
18c1a 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
18c1b 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 cells in the pa
18c1c 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70 ge if the next p
18c1d 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69 age. ** to vi
18c1e 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74 sit is the right
18c1f 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 -child of its pa
18c20 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 rent.. **.
18c21 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73 ** If all pages
18c22 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76 in the tree hav
18c23 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20 e been visited,
18c24 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18c25 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 to the. ** c
18c26 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 aller.. */.
18c27 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
18c28 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a f ){. do {.
18c29 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
18c2a 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ->iPage==0 ){.
18c2b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 /* All p
18c2c 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72 ages of the b-tr
18c2d 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 ee have been vis
18c2e 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63 ited. Return suc
18c2f 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 cessfully. */.
18c30 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 *pnEntry
18c31 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20 = nEntry;.
18c32 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18c33 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d TE_OK;. }
18c34 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 . moveToP
18c35 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
18c36 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72 }while ( pCur
18c37 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
18c38 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61 age]>=pCur->apPa
18c39 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
18c3a 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 >nCell );..
18c3b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
18c3c 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 r->iPage]++;.
18c3d 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
18c3e 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18c3f 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 age];. }..
18c40 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74 /* Descend to t
18c41 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66 he child node of
18c42 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74 the cell that t
18c43 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e he cursor curren
18c44 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e tly . ** poin
18c45 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74 ts at. This is t
18c46 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 he right-child i
18c47 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e f (iIdx==pPage->
18c48 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 nCell).. */.
18c49 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e iIdx = pCur->
18c4a 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
18c4b 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78 e];. if( iIdx
18c4c 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ==pPage->nCell )
18c4d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 {. rc = mov
18c4e 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 eToChild(pCur, g
18c4f 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18c50 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18c51 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 Offset+8]));.
18c52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
18c53 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
18c54 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 Cur, get4byte(fi
18c55 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 ndCell(pPage, iI
18c56 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d dx)));. }. }
18c57 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 .. /* An error
18c58 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65 has occurred. Re
18c59 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f turn an error co
18c5a 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 de. */. return
18c5b 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a rc;.}.#endif../*
18c5c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 .** Return the p
18c5d 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 ager associated
18c5e 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 with a BTree. T
18c5f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
18c60 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 sed for.** testi
18c61 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
18c62 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
18c63 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a _PRIVATE Pager *
18c64 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
18c65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 r(Btree *p){. r
18c66 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 eturn p->pBt->pP
18c67 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 ager;.}..#ifndef
18c68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18c69 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18c6a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 ** Append a mess
18c6b 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 age to the error
18c6c 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e message string.
18c6d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
18c6e 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a checkAppendMsg(.
18c6f 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18c70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a Check,. char *z
18c71 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Msg1,. const ch
18c72 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e ar *zFormat,. .
18c73 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
18c74 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 ap;. if( !pChec
18c75 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 k->mxErr ) retur
18c76 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 n;. pCheck->mxE
18c77 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e rr--;. pCheck->
18c78 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 nErr++;. va_sta
18c79 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
18c7a 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 . if( pCheck->e
18c7b 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 rrMsg.nChar ){.
18c7c 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
18c7d 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b umAppend(&pCheck
18c7e 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 ->errMsg, "\n",
18c7f 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 1);. }. if( zM
18c80 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 sg1 ){. sqlit
18c81 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
18c82 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 (&pCheck->errMsg
18c83 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 , zMsg1, -1);.
18c84 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 }. sqlite3VXPri
18c85 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 ntf(&pCheck->err
18c86 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c Msg, 1, zFormat,
18c87 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
18c88 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b p);. if( pCheck
18c89 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 ->errMsg.mallocF
18c8a 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 ailed ){. pCh
18c8b 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
18c8c 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e d = 1;. }.}.#en
18c8d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
18c8e 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
18c8f 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 CK */..#ifndef S
18c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
18c91 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a RITY_CHECK./*.**
18c92 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 Add 1 to the re
18c93 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f ference count fo
18c94 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 r page iPage. I
18c95 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 f this is the se
18c96 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 cond.** referenc
18c97 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 e to the page, a
18c98 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 dd an error mess
18c99 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a age to pCheck->z
18c9a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 ErrMsg..** Retur
18c9b 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 n 1 if there are
18c9c 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 2 ore more refe
18c9d 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
18c9e 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 ge and 0 if.** i
18c9f 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
18ca0 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f rst reference to
18ca1 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
18ca2 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 Also check that
18ca3 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
18ca4 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a is in bounds..*
18ca5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 /.static int che
18ca6 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 ckRef(IntegrityC
18ca7 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 k *pCheck, Pgno
18ca8 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f iPage, char *zCo
18ca9 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 ntext){. if( iP
18caa 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 age==0 ) return
18cab 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 1;. if( iPage>p
18cac 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a Check->nPage ){.
18cad 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18cae 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18caf 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 ext, "invalid pa
18cb0 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 ge number %d", i
18cb1 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
18cb2 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 n 1;. }. if( p
18cb3 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 Check->anRef[iPa
18cb4 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 ge]==1 ){. ch
18cb5 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18cb6 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
18cb7 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f 2nd reference to
18cb8 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 page %d", iPage
18cb9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
18cba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 . }. return (
18cbb 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 pCheck->anRef[iP
18cbc 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 age]++)>1;.}..#i
18cbd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18cbe 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a T_AUTOVACUUM./*.
18cbf 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 ** Check that th
18cc0 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 e entry in the p
18cc1 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 ointer-map for p
18cc2 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20 age iChild maps
18cc3 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 to .** page iPar
18cc4 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 ent, pointer typ
18cc5 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f e ptrType. If no
18cc6 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72 t, append an err
18cc7 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f or message.** to
18cc8 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 pCheck..*/.stat
18cc9 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72 ic void checkPtr
18cca 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79 map(. Integrity
18ccb 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a Ck *pCheck, /*
18ccc 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b Integrity check
18ccd 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 context */. Pg
18cce 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 no iChild,
18ccf 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 /* Child pa
18cd0 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 ge number */. u
18cd1 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 8 eType,
18cd2 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 /* Expecte
18cd3 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 d pointer map ty
18cd4 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 pe */. Pgno iPa
18cd5 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f rent, /
18cd6 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 * Expected point
18cd7 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61 er map parent pa
18cd8 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 ge number */. c
18cd9 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 har *zContext
18cda 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
18cdb 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 description (us
18cdc 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 ed for error msg
18cdd 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 ) */.){. int rc
18cde 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79 ;. u8 ePtrmapTy
18cdf 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d pe;. Pgno iPtrm
18ce0 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 apParent;.. rc
18ce1 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65 = ptrmapGet(pChe
18ce2 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c ck->pBt, iChild,
18ce3 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26 &ePtrmapType, &
18ce4 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a iPtrmapParent);.
18ce5 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18ce6 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 _OK ){. if( r
18ce7 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
18ce8 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f || rc==SQLITE_IO
18ce9 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 ERR_NOMEM ) pChe
18cea 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ck->mallocFailed
18ceb 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 = 1;. checkA
18cec 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18ced 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c zContext, "Fail
18cee 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 ed to read ptrma
18cef 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c p key=%d", iChil
18cf0 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a d);. return;.
18cf1 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d }.. if( ePtrm
18cf2 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c apType!=eType ||
18cf3 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d iPtrmapParent!=
18cf4 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 iParent ){. c
18cf5 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18cf6 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18cf7 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 . "Bad ptr
18cf8 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 map entry key=%d
18cf9 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 expected=(%d,%d
18cfa 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 ) got=(%d,%d)",
18cfb 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 . iChild, e
18cfc 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 Type, iParent, e
18cfd 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 PtrmapType, iPtr
18cfe 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a mapParent);. }.
18cff 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
18d00 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
18d01 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c ity of the freel
18d02 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 ist or of an ove
18d03 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e rflow page list.
18d04 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 .** Verify that
18d05 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
18d06 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ges on the list
18d07 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 is N..*/.static
18d08 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a void checkList(.
18d09 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18d0a 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 Check, /* Integ
18d0b 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f rity checking co
18d0c 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
18d0d 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 sFreeList,
18d0e 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 /* True for a f
18d0f 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 reelist. False
18d10 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 for overflow pag
18d11 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
18d12 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 iPage,
18d13 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
18d14 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 for first page
18d15 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
18d16 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 int N,
18d17 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
18d18 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ed number of pag
18d19 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a es in the list *
18d1a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 /. char *zConte
18d1b 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e xt /* Con
18d1c 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
18d1d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 essages */.){.
18d1e 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 int i;. int exp
18d1f 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 ected = N;. int
18d20 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b iFirst = iPage;
18d21 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
18d22 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 0 && pCheck->mxE
18d23 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 rr ){. DbPage
18d24 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 *pOvflPage;.
18d25 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
18d26 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 pOvflData;. i
18d27 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 f( iPage<1 ){.
18d28 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18d29 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18d2a 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 ext,. "%
18d2b 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 d of %d pages mi
18d2c 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 ssing from overf
18d2d 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e low list startin
18d2e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 g at %d",.
18d2f 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 N+1, expecte
18d30 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 d, iFirst);.
18d31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
18d32 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 if( checkRef(
18d33 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a pCheck, iPage, z
18d34 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b Context) ) break
18d35 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
18d36 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 3PagerGet(pCheck
18d37 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 ->pPager, (Pgno)
18d38 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 iPage, &pOvflPag
18d39 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 e) ){. chec
18d3a 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18d3b 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 k, zContext, "fa
18d3c 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 iled to get page
18d3d 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
18d3e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
18d3f 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d . pOvflData =
18d40 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
18d41 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
18d42 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 tData(pOvflPage)
18d43 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 ;. if( isFree
18d44 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e List ){. in
18d45 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 t n = get4byte(&
18d46 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 pOvflData[4]);.#
18d47 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18d48 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18d49 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e if( pCheck->
18d4a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18d4b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18d4c 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
18d4d 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18d4e 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18d4f 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18d50 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e dif. if( n>
18d51 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 pCheck->pBt->usa
18d52 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 bleSize/4-2 ){.
18d53 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 checkAppe
18d54 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18d55 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18d56 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 "freelist lea
18d57 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 f count too big
18d58 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 on page %d", iPa
18d59 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d ge);. N--
18d5a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
18d5b 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
18d5c 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<n; i++){.
18d5d 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
18d5e 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
18d5f 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d pOvflData[8+i*4]
18d60 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18d61 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18d62 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 M. if(
18d63 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
18d64 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18d65 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18d66 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 ap(pCheck, iFree
18d67 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18d68 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18d69 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d xt);. }
18d6a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
18d6b 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 checkRef(pChec
18d6c 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 k, iFreePage, zC
18d6d 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 ontext);.
18d6e 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 }. N -=
18d6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
18d70 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18d71 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18d72 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
18d73 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
18d74 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
18d75 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 o-vacuum and iPa
18d76 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 ge is not the la
18d77 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 st. ** page
18d78 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f in this overflo
18d79 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 w list, check th
18d7a 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d at the pointer-m
18d7b 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 ap entry for.
18d7c 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 ** the follow
18d7d 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 ing page matches
18d7e 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f iPage.. */
18d7f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 . if( pChec
18d80 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 k->pBt->autoVacu
18d81 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 um && N>0 ){.
18d82 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 i = get4byt
18d83 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 e(pOvflData);.
18d84 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18d85 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 p(pCheck, i, PTR
18d86 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 MAP_OVERFLOW2, i
18d87 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
18d88 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
18d89 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 endif. iPage
18d8a 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c = get4byte(pOvfl
18d8b 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 Data);. sqlit
18d8c 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 e3PagerUnref(pOv
18d8d 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 flPage);. }.}.#
18d8e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
18d8f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
18d90 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 HECK */..#ifndef
18d91 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18d92 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18d93 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 ** Do various sa
18d94 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 nity checks on a
18d95 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 single page of
18d96 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a a tree. Return.
18d97 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 ** the tree dept
18d98 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 h. Root pages r
18d99 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 eturn 0. Parent
18d9a 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a s of root pages.
18d9b 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 ** return 1, and
18d9c 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a so forth..** .*
18d9d 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 * These checks a
18d9e 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 re done:.**.**
18d9f 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 1. Make sur
18da0 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 e that cells and
18da1 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e freeblocks do n
18da2 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 ot overlap.**
18da3 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 but combi
18da4 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 ne to completely
18da5 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e cover the page.
18da6 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b .** NO 2. Mak
18da7 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 e sure cell keys
18da8 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a are in order..*
18da9 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 * NO 3. Make
18daa 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c sure no key is l
18dab 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
18dac 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 l to zLowerBound
18dad 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 ..** NO 4. Ma
18dae 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 ke sure no key i
18daf 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
18db0 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 r equal to zUppe
18db1 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 rBound..**
18db2 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 5. Check the in
18db3 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 tegrity of overf
18db4 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 low pages..**
18db5 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6. Recursive
18db6 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 ly call checkTre
18db7 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 ePage on all chi
18db8 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 ldren..** 7
18db9 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 . Verify that t
18dba 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 he depth of all
18dbb 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 children is the
18dbc 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e same..** 8.
18dbd 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 Make sure this
18dbe 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 page is at leas
18dbf 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c t 33% full or el
18dc0 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 se it is.**
18dc1 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 the root of
18dc2 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 the tree..*/.st
18dc3 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 atic int checkTr
18dc4 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 eePage(. Integr
18dc5 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 ityCk *pCheck,
18dc6 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 /* Context for t
18dc7 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 he sanity check
18dc8 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 */. int iPage,
18dc9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
18dca 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
18dcb 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a page to check *
18dcc 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e /. char *zParen
18dcd 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 tContext /* Par
18dce 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 ent context */.)
18dcf 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
18dd0 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c ge;. int i, rc,
18dd1 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f depth, d2, pgno
18dd2 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 , cnt;. int hdr
18dd3 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 , cellStart;. i
18dd4 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a nt nCell;. u8 *
18dd5 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 data;. BtShared
18dd6 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 *pBt;. int usa
18dd7 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 bleSize;. char
18dd8 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 zContext[100];.
18dd9 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a char *hit = 0;.
18dda 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
18ddb 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 ntf(sizeof(zCont
18ddc 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 ext), zContext,
18ddd 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 "Page %d: ", iPa
18dde 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ge);.. /* Check
18ddf 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 that the page e
18de0 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 xists. */. pBt
18de1 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a = pCheck->pBt;.
18de2 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
18de3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
18de4 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 if( iPage==0 )
18de5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
18de6 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b checkRef(pCheck
18de7 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 , iPage, zParent
18de8 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 Context) ) retur
18de9 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d n 0;. if( (rc =
18dea 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
18deb 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20 t, (Pgno)iPage,
18dec 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29 &pPage, 0))!=0 )
18ded 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e {. checkAppen
18dee 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
18def 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75 ntext,. "u
18df0 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 nable to get the
18df1 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64 page. error cod
18df2 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 e=%d", rc);.
18df3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
18df4 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67 /* Clear MemPag
18df5 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65 e.isInit to make
18df6 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70 sure the corrup
18df7 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63 tion detection c
18df8 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65 ode in. ** btre
18df9 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65 eInitPage() is e
18dfa 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70 xecuted. */. p
18dfb 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
18dfc 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74 ;. if( (rc = bt
18dfd 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
18dfe 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 e))!=0 ){. as
18dff 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
18e00 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20 _CORRUPT ); /*
18e01 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c The only possibl
18e02 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69 e error from Ini
18e03 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65 tPage */. che
18e04 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18e05 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 ck, zContext, .
18e06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e07 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65 "btreeInitPage
18e08 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 () returns error
18e09 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a code %d", rc);.
18e0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18e0b 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 pPage);. retu
18e0c 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 0;. }.. /*
18e0d 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 Check out all th
18e0e 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 e cells.. */.
18e0f 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 depth = 0;. for
18e10 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e (i=0; i<pPage->n
18e11 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e Cell && pCheck->
18e12 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 mxErr; i++){.
18e13 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 u8 *pCell;.
18e14 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c u32 sz;. Cell
18e15 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 Info info;..
18e16 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 /* Check payload
18e17 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a overflow pages.
18e18 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
18e19 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
18e1a 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 of(zContext), zC
18e1b 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18e1c 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 "On tree pa
18e1d 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 ge %d cell %d: "
18e1e 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 , iPage, i);.
18e1f 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
18e20 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 l(pPage,i);.
18e21 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
18e22 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
18e23 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d &info);. sz =
18e24 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 info.nData;.
18e25 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 if( !pPage->int
18e26 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74 Key ) sz += (int
18e27 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 )info.nKey;.
18e28 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f assert( sz==info
18e29 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 .nPayload );.
18e2a 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c if( (sz>info.nL
18e2b 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28 ocal) . && (
18e2c 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
18e2d 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e rflow]<=&pPage->
18e2e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c aData[pBt->usabl
18e2f 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 eSize]). ){.
18e30 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d int nPage =
18e31 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 (sz - info.nLoc
18e32 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 al + usableSize
18e33 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 - 5)/(usableSize
18e34 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e - 4);. Pgn
18e35 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 o pgnoOvfl = get
18e36 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
18e37 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 o.iOverflow]);.#
18e38 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18e39 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18e3a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
18e3b 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18e3c 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
18e3d 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c Check, pgnoOvfl,
18e3e 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
18e3f 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 1, iPage, zConte
18e40 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18e41 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c dif. checkL
18e42 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 ist(pCheck, 0, p
18e43 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 gnoOvfl, nPage,
18e44 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d zContext);. }
18e45 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 .. /* Check s
18e46 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 anity of left ch
18e47 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ild page.. */
18e48 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
18e49 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 >leaf ){. p
18e4a 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 gno = get4byte(p
18e4b 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 Cell);.#ifndef S
18e4c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18e4d 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 ACUUM. if(
18e4e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18e4f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18e50 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
18e51 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
18e52 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 E, iPage, zConte
18e53 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18e54 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 dif. d2 = c
18e55 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 heckTreePage(pCh
18e56 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 eck, pgno, zCont
18e57 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ext);. if(
18e58 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 i>0 && d2!=depth
18e59 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18e5a 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18e5b 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 k, zContext, "Ch
18e5c 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 ild page depth d
18e5d 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 iffers");.
18e5e 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 }. depth =
18e5f 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 d2;. }. }.
18e60 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
18e61 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 ){. pgno = g
18e62 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18e63 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18e64 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
18e65 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18e66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 (sizeof(zContext
18e67 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 ), zContext, .
18e68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e69 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61 "On page %d a
18e6a 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22 t right child: "
18e6b 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 , iPage);.#ifnde
18e6c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18e6d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
18e6e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18e6f 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 ){. checkP
18e70 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 trmap(pCheck, pg
18e71 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 no, PTRMAP_BTREE
18e72 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 , iPage, 0);.
18e73 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 }.#endif. ch
18e74 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 eckTreePage(pChe
18e75 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 ck, pgno, zConte
18e76 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 xt);. }. . /*
18e77 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 Check for comple
18e78 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74 te coverage of t
18e79 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 he page. */. d
18e7a 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
18e7b 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 ta;. hdr = pPag
18e7c 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
18e7d 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67 hit = sqlite3Pag
18e7e 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
18e7f 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 geSize );. if(
18e80 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 hit==0 ){. pC
18e81 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c heck->mallocFail
18e82 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
18e83 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74 . u16 content
18e84 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74 Offset = get2byt
18e85 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
18e86 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e . assert( con
18e87 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62 tentOffset<=usab
18e88 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e leSize ); /* En
18e89 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 forced by btreeI
18e8a 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 nitPage() */.
18e8b 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 memset(hit+cont
18e8c 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 entOffset, 0, us
18e8d 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 ableSize-content
18e8e 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d Offset);. mem
18e8f 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 set(hit, 1, cont
18e90 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 entOffset);.
18e91 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 nCell = get2byte
18e92 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a (&data[hdr+3]);.
18e93 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 cellStart =
18e94 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 hdr + 12 - 4*pPa
18e95 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f ge->leaf;. fo
18e96 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
18e97 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
18e98 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
18e99 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a ata[cellStart+i*
18e9a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 2]);. u16 s
18e9b 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 ize = 1024;.
18e9c 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 int j;. i
18e9d 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a f( pc<=usableSiz
18e9e 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73 e-4 ){. s
18e9f 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ize = cellSizePt
18ea0 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 r(pPage, &data[p
18ea1 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c]);. }.
18ea2 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d if( (pc+size-
18ea3 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 1)>=usableSize )
18ea4 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
18ea5 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18ea6 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
18ea7 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 "Corruption det
18ea8 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 ected in cell %d
18ea9 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 on page %d",i,i
18eaa 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d Page,0);. }
18eab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f else{. fo
18eac 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a r(j=pc+size-1; j
18ead 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a >=pc; j--) hit[j
18eae 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ]++;. }.
18eaf 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62 }. i = get2b
18eb0 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
18eb1 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e );. while( i>
18eb2 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 0 ){. int s
18eb3 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73 ize, j;. as
18eb4 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53 sert( i<=usableS
18eb5 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20 ize-4 ); /*
18eb6 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 Enforced by btre
18eb7 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 eInitPage() */.
18eb8 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 size = get2
18eb9 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29 byte(&data[i+2])
18eba 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18ebb 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 i+size<=usableSi
18ebc 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 ze ); /* Enforc
18ebd 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 ed by btreeInitP
18ebe 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66 age() */. f
18ebf 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a or(j=i+size-1; j
18ec0 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d >=i; j--) hit[j]
18ec1 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65 ++;. j = ge
18ec2 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29 t2byte(&data[i])
18ec3 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18ec4 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 j==0 || j>i+size
18ec5 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 ); /* Enforced
18ec6 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 by btreeInitPag
18ec7 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 e() */. ass
18ec8 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69 ert( j<=usableSi
18ec9 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66 ze-4 ); /* Enf
18eca 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e orced by btreeIn
18ecb 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 itPage() */.
18ecc 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20 i = j;. }.
18ecd 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 for(i=cnt=0;
18ece 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b i<usableSize; i+
18ecf 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 +){. if( hi
18ed0 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 t[i]==0 ){.
18ed1 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
18ed2 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d }else if( hit[i]
18ed3 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 >1 ){. ch
18ed4 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18ed5 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 eck, 0,.
18ed6 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 "Multiple uses
18ed7 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 for byte %d of
18ed8 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 page %d", i, iPa
18ed9 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ge);. bre
18eda 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
18edb 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 }. if( cnt!=d
18edc 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 ata[hdr+7] ){.
18edd 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18ede 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 sg(pCheck, 0, .
18edf 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 "Fragme
18ee0 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79 ntation of %d by
18ee1 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20 tes reported as
18ee2 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a %d on page %d",.
18ee3 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64 cnt, d
18ee4 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 ata[hdr+7], iPag
18ee5 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
18ee6 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
18ee7 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 hit);. releaseP
18ee8 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
18ee9 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a turn depth+1;.}.
18eea 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18eeb 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
18eec 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
18eed 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18eee 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18eef 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
18ef0 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 does a complete
18ef1 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69 check of the gi
18ef2 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20 ven BTree file.
18ef3 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 aRoot[] is.** a
18ef4 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73 n array of pages
18ef5 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61 numbers were ea
18ef6 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 ch page number i
18ef7 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
18ef8 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 of.** a table.
18ef9 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d nRoot is the num
18efa 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
18efb 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41 n aRoot..**.** A
18efc 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 read-only or re
18efd 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 ad-write transac
18efe 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 tion must be ope
18eff 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 ned before calli
18f00 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 ng.** this funct
18f01 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 ion..**.** Write
18f02 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
18f03 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e rror seen in *pn
18f04 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72 Err. Except for
18f05 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 some memory.**
18f06 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
18f07 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 s, an error mes
18f08 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d sage held in mem
18f09 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
18f0a 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72 m.** malloc is r
18f0b 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72 eturned if *pnEr
18f0c 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 r is non-zero.
18f0d 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65 If *pnErr==0 the
18f0e 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 n NULL is.** ret
18f0f 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d urned. If a mem
18f10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
18f11 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c rror occurs, NUL
18f12 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
18f13 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18f14 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
18f15 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
18f16 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 k(. Btree *p,
18f17 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
18f18 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f to be checked */
18f19 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 . int *aRoot,
18f1a 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
18f1b 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 root pages numbe
18f1c 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 rs for individua
18f1d 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 l trees */. int
18f1e 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 nRoot, /* Nu
18f1f 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
18f20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 in aRoot[] */.
18f21 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a int mxErr, /*
18f22 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 Stop reporting
18f23 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69 errors after thi
18f24 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 s many */. int
18f25 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69 *pnErr /* Wri
18f26 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 te number of err
18f27 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73 ors seen to this
18f28 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a variable */.){.
18f29 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 Pgno i;. int
18f2a 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 nRef;. Integrit
18f2b 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 yCk sCheck;. Bt
18f2c 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
18f2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 >pBt;. char zEr
18f2e 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 r[100];.. sqlit
18f2f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18f30 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
18f31 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
18f32 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
18f33 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e action>TRANS_NON
18f34 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71 E );. nRef = sq
18f35 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
18f36 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nt(pBt->pPager);
18f37 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20 . sCheck.pBt =
18f38 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50 pBt;. sCheck.pP
18f39 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
18f3a 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61 er;. sCheck.nPa
18f3b 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
18f3c 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b unt(sCheck.pBt);
18f3d 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20 . sCheck.mxErr
18f3e 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63 = mxErr;. sChec
18f3f 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43 k.nErr = 0;. sC
18f40 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 heck.mallocFaile
18f41 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20 d = 0;. *pnErr
18f42 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63 = 0;. if( sChec
18f43 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 k.nPage==0 ){.
18f44 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18f45 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
18f46 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 rn 0;. }. sChe
18f47 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 ck.anRef = sqlit
18f48 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 e3Malloc( (sChec
18f49 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f k.nPage+1)*sizeo
18f4a 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 f(sCheck.anRef[0
18f4b 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 ]) );. if( !sCh
18f4c 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 eck.anRef ){.
18f4d 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 *pnErr = 1;.
18f4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18f4f 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
18f50 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 n 0;. }. for(i
18f51 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 =0; i<=sCheck.nP
18f52 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 age; i++){ sChec
18f53 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 k.anRef[i] = 0;
18f54 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f }. i = PENDING_
18f55 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a BYTE_PAGE(pBt);.
18f56 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e if( i<=sCheck.
18f57 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 nPage ){. sCh
18f58 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 eck.anRef[i] = 1
18f59 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
18f5a 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68 trAccumInit(&sCh
18f5b 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 eck.errMsg, zErr
18f5c 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 , sizeof(zErr),
18f5d 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 20000);.. /* Ch
18f5e 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 eck the integrit
18f5f 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 y of the freelis
18f60 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 t. */. checkLi
18f61 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 st(&sCheck, 1, g
18f62 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
18f63 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 age1->aData[32])
18f64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65 ,. ge
18f65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
18f66 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c ge1->aData[36]),
18f67 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a "Main freelist:
18f68 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ");.. /* Check
18f69 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e all the tables.
18f6a 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
18f6b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 (int)i<nRoot &&
18f6c 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18f6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f ++){. if( aRo
18f6e 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 ot[i]==0 ) conti
18f6f 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c nue;.#ifndef SQL
18f70 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18f71 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d UUM. if( pBt-
18f72 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 >autoVacuum && a
18f73 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 Root[i]>1 ){.
18f74 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 checkPtrmap(&
18f75 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d sCheck, aRoot[i]
18f76 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 , PTRMAP_ROOTPAG
18f77 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 0, 0);. }.
18f78 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b #endif. check
18f79 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b TreePage(&sCheck
18f7a 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 , aRoot[i], "Lis
18f7b 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a t of tree roots:
18f7c 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d ");. }.. /* M
18f7d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70 ake sure every p
18f7e 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 age in the file
18f7f 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20 is referenced.
18f80 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c */. for(i=1; i<
18f81 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 =sCheck.nPage &&
18f82 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18f83 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 ++){.#ifdef SQLI
18f84 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18f85 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 UM. if( sChec
18f86 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b k.anRef[i]==0 ){
18f87 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 . checkAppe
18f88 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 ndMsg(&sCheck, 0
18f89 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 , "Page %d is ne
18f8a 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 ver used", i);.
18f8b 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f }.#else. /
18f8c 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
18f8d 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
18f8e 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 vacuum, make sur
18f8f 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 e no tables cont
18f90 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 ain. ** refer
18f91 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 ences to pointer
18f92 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20 -map pages..
18f93 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 */. if( sChec
18f94 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 k.anRef[i]==0 &&
18f95 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 . (PTRMAP
18f96 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 _PAGENO(pBt, i)!
18f97 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f =i || !pBt->auto
18f98 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 Vacuum) ){.
18f99 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18f9a 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 &sCheck, 0, "Pag
18f9b 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73 e %d is never us
18f9c 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 ed", i);. }.
18f9d 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e if( sCheck.an
18f9e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 Ref[i]!=0 && .
18f9f 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 (PTRMAP_PAG
18fa0 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 ENO(pBt, i)==i &
18fa1 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 & pBt->autoVacuu
18fa2 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 m) ){. chec
18fa3 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 kAppendMsg(&sChe
18fa4 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 ck, 0, "Pointer
18fa5 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72 map page %d is r
18fa6 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a eferenced", i);.
18fa7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
18fa8 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
18fa9 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 this analysis d
18faa 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 id not leave any
18fab 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a unref() pages..
18fac 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 ** This is an
18fad 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74 internal consist
18fae 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69 ency check; an i
18faf 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20 ntegrity check.
18fb0 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ** of the integ
18fb1 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f rity check.. */
18fb2 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 . if( NEVER(nRe
18fb3 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 f != sqlite3Page
18fb4 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 rRefcount(pBt->p
18fb5 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 Pager)) ){. c
18fb6 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
18fb7 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 Check, 0, .
18fb8 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 "Outstanding pa
18fb9 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 ge count goes fr
18fba 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 om %d to %d duri
18fbb 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 ng this analysis
18fbc 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 ",. nRef, s
18fbd 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
18fbe 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
18fbf 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f . );. }.. /
18fc0 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 * Clean up and
18fc1 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 report errors..
18fc2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 */. sqlite3Btr
18fc3 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 eeLeave(p);. sq
18fc4 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 lite3_free(sChec
18fc5 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 k.anRef);. if(
18fc6 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 sCheck.mallocFai
18fc7 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
18fc8 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
18fc9 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b &sCheck.errMsg);
18fca 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 . *pnErr = sC
18fcb 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 heck.nErr+1;.
18fcc 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18fcd 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b *pnErr = sCheck
18fce 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 .nErr;. if( sCh
18fcf 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 eck.nErr==0 ) sq
18fd0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 lite3StrAccumRes
18fd1 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 et(&sCheck.errMs
18fd2 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c g);. return sql
18fd3 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
18fd4 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 sh(&sCheck.errMs
18fd5 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 g);.}.#endif /*
18fd6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
18fd7 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a GRITY_CHECK */..
18fd8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
18fd9 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f full pathname o
18fda 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
18fdb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
18fdc 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18fdd 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 filename is inva
18fde 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 riant as long as
18fdf 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a the pager is.**
18fe0 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 open so it is s
18fe1 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 afe to access wi
18fe2 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 thout the BtShar
18fe3 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c ed mutex..*/.SQL
18fe4 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
18fe5 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 t char *sqlite3B
18fe6 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 treeGetFilename(
18fe7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
18fe8 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 ert( p->pBt->pPa
18fe9 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 ger!=0 );. retu
18fea 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 rn sqlite3PagerF
18feb 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e ilename(p->pBt->
18fec 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pPager);.}../*.*
18fed 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 * Return the pat
18fee 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 hname of the jou
18fef 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 rnal file for th
18ff0 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 is database. The
18ff1 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
18ff2 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
18ff3 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 is the same reg
18ff4 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
18ff5 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 er the journal f
18ff6 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ile.** has been
18ff7 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a created or not..
18ff8 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18ff9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
18ffa 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 is invariant as
18ffb 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 long as the pag
18ffc 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f er is.** open so
18ffd 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 it is safe to a
18ffe 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 ccess without th
18fff 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 e BtShared mutex
19000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19001 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
19002 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a sqlite3BtreeGetJ
19003 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 ournalname(Btree
19004 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
19005 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d p->pBt->pPager!=
19006 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 0 );. return sq
19007 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
19008 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 lname(p->pBt->pP
19009 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
1900a 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 Return non-zero
1900b 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e if a transaction
1900c 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 is active..*/.S
1900d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1900e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
1900f 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 InTrans(Btree *p
19010 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d ){. assert( p==
19011 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
19012 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
19013 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
19014 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 n (p && (p->inTr
19015 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
19016 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ));.}../*.** Ret
19017 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 urn non-zero if
19018 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 a read (or write
19019 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 ) transaction is
1901a 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 active..*/.SQLI
1901b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1901c 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 qlite3BtreeIsInR
1901d 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a eadTrans(Btree *
1901e 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
1901f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
19020 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19021 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
19022 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 . return p->inT
19023 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 rans!=TRANS_NONE
19024 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
19025 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
19026 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 treeIsInBackup(B
19027 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
19028 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 rt( p );. asser
19029 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1902a 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
1902b 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ex) );. return
1902c 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d p->nBackup!=0;.}
1902d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1902e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 ction returns a
1902f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f pointer to a blo
19030 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f b of memory asso
19031 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 ciated with.** a
19032 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 single shared-b
19033 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 tree. The memory
19034 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 is used by clie
19035 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 nt code for its
19036 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 own.** purposes
19037 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f (for example, to
19038 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 store a high-le
19039 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 vel schema assoc
1903a 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 iated with .** t
1903b 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 he shared-btree)
1903c 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 . The btree laye
1903d 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 r manages refere
1903e 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 nce counting iss
1903f 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ues..**.** The f
19040 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 irst time this i
19041 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 s called on a sh
19042 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 ared-btree, nByt
19043 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f es bytes of memo
19044 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 ry.** are alloca
19045 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 ted, zeroed, and
19046 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
19047 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 caller. For eac
19048 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a h subsequent .**
19049 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 call the nBytes
1904a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 parameter is ig
1904b 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e nored and a poin
1904c 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ter to the same
1904d 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 blob.** of memor
1904e 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a y returned. .**.
1904f 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 ** If the nBytes
19050 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 parameter is 0
19051 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 and the blob of
19052 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 memory has not y
19053 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 et been.** alloc
19054 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 ated, a null poi
19055 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
19056 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 . If the blob ha
19057 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a s already been.*
19058 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 * allocated, it
19059 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e is returned as n
1905a 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 ormal..**.** Jus
1905b 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 t before the sha
1905c 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f red-btree is clo
1905d 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f sed, the functio
1905e 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 n passed as the
1905f 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 .** xFree argume
19060 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f nt when the memo
19061 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 ry allocation wa
19062 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 s made is invoke
19063 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f d on the .** blo
19064 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d b of allocated m
19065 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 emory. This func
19066 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 tion should not
19067 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 call sqlite3_fre
19068 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 e().** on the me
19069 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 mory, the btree
1906a 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e layer does that.
1906b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1906c 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
1906d 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 BtreeSchema(Btre
1906e 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 e *p, int nBytes
1906f 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 , void(*xFree)(v
19070 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 oid *)){. BtSha
19071 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
19072 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 t;. sqlite3Btre
19073 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
19074 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 !pBt->pSchema &
19075 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 & nBytes ){.
19076 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 pBt->pSchema = s
19077 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
19078 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42 (nBytes);. pB
19079 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d t->xFreeSchema =
1907a 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 xFree;. }. sq
1907b 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1907c 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 p);. return pBt
1907d 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a ->pSchema;.}../*
1907e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
1907f 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
19080 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20 ACHE if another
19081 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 user of the same
19082 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65 shared .** btre
19083 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e e as the argumen
19084 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 t handle holds a
19085 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
19086 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 on the .** sqli
19087 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e te_master table.
19088 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 Otherwise SQLIT
19089 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
1908a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1908b 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f te3BtreeSchemaLo
1908c 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a cked(Btree *p){.
1908d 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
1908e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1908f 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
19090 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
19091 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
19092 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 rc = queryShar
19093 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
19094 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c (p, MASTER_ROOT,
19095 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 READ_LOCK);. a
19096 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
19097 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
19098 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
19099 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74 CACHE );. sqlit
1909a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1909b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1909c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1909d 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1909e 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 HE./*.** Obtain
1909f 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 a lock on the ta
190a0 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 ble whose root p
190a1 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 age is iTab. Th
190a2 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 e.** lock is a w
190a3 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 rite lock if isW
190a4 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 ritelock is true
190a5 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a or a read lock.
190a6 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 ** if it is fals
190a7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
190a8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
190a9 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 BtreeLockTable(B
190aa 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 tree *p, int iTa
190ab 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 b, u8 isWriteLoc
190ac 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 k){. int rc = S
190ad 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
190ae 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d rt( p->inTrans!=
190af 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
190b0 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
190b1 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 ){. u8 lockTy
190b2 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b pe = READ_LOCK +
190b3 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 isWriteLock;.
190b4 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c assert( READ_L
190b5 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 OCK+1==WRITE_LOC
190b6 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 K );. assert(
190b7 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 isWriteLock==0
190b8 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d || isWriteLock==
190b9 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 1 );.. sqlite
190ba 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
190bb 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 rc = querySh
190bc 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
190bd 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
190be 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Type);. if( r
190bf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
190c0 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68 rc = setSh
190c1 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
190c2 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
190c3 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Type);. }.
190c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
190c5 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ve(p);. }. ret
190c6 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
190c7 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
190c8 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f _OMIT_INCRBLOB./
190c9 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 *.** Argument pC
190ca 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72 sr must be a cur
190cb 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 sor opened for w
190cc 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a riting on an .**
190cd 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75 INTKEY table cu
190ce 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
190cf 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c at a valid tabl
190d0 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 e entry. .** Thi
190d1 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 s function modif
190d2 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f ies the data sto
190d3 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 red as part of t
190d4 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a hat entry..**.**
190d5 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63 Only the data c
190d6 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 ontent may only
190d7 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20 be modified, it
190d8 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
190d9 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 to .** change th
190da 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
190db 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20 data stored. If
190dc 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
190dd 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20 called with.**
190de 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 parameters that
190df 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
190e0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
190e1 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 the existing da
190e2 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69 ta,.** no modifi
190e3 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65 cations are made
190e4 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52 and SQLITE_CORR
190e5 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e UPT is returned.
190e6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
190e7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
190e8 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72 reePutData(BtCur
190e9 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f sor *pCsr, u32 o
190ea 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
190eb 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 void *z){. int
190ec 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 rc;. assert( cu
190ed 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
190ee 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Csr) );. assert
190ef 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
190f0 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65 held(pCsr->pBtre
190f1 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
190f2 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d . assert( pCsr-
190f3 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c >isIncrblobHandl
190f4 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 e );.. rc = res
190f5 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
190f6 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20 on(pCsr);. if(
190f7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
190f8 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
190f9 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
190fa 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 sr->eState!=CURS
190fb 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
190fc 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 ;. if( pCsr->eS
190fd 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
190fe 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ID ){. return
190ff 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 SQLITE_ABORT;.
19100 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 }.. /* Check s
19101 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a ome assumptions:
19102 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 . ** (a) the
19103 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 cursor is open
19104 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a for writing,. *
19105 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73 * (b) there is
19106 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72 a read/write tr
19107 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a ansaction open,.
19108 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63 ** (c) the c
19109 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20 onnection holds
1910a 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
1910b 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65 the table (if re
1910c 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20 quired),. **
1910d 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f (d) there are no
1910e 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61 conflicting rea
1910f 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a d-locks, and. *
19110 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73 * (e) the curs
19111 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76 or points at a v
19112 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 alid row of an i
19113 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a ntKey table.. *
19114 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77 /. if( !pCsr->w
19115 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 rFlag ){. ret
19116 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
19117 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 NLY;. }. asser
19118 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 t( !pCsr->pBt->r
19119 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d eadOnly && pCsr-
1911a 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
1911b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1911c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 );. assert( ha
1911d 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
1911e 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72 eLock(pCsr->pBtr
1911f 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f ee, pCsr->pgnoRo
19120 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61 ot, 0, 2) );. a
19121 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43 ssert( !hasReadC
19122 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70 onflicts(pCsr->p
19123 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e Btree, pCsr->pgn
19124 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65 oRoot) );. asse
19125 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65 rt( pCsr->apPage
19126 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 [pCsr->iPage]->i
19127 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75 ntKey );.. retu
19128 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 rn accessPayload
19129 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 (pCsr, offset, a
1912a 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 mt, (unsigned ch
1912b 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f ar *)z, 1);.}../
1912c 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 * .** Set a flag
1912d 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
1912e 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63 to cache the loc
1912f 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 ations of pages
19130 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 from the .** ove
19131 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 rflow list for t
19132 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 he current row.
19133 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
19134 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
19135 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 * for incrementa
19136 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a l blob IO only..
19137 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
19138 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 ion sets a flag
19139 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c only. The actual
1913a 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 page location c
1913b 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 ache.** (stored
1913c 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 in BtCursor.aOve
1913d 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f rflow[]) is allo
1913e 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62 cated and used b
1913f 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 y function.** ac
19140 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74 cessPayload() (t
19141 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 he worker functi
19142 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74 on for sqlite3Bt
19143 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a reeData() and.**
19144 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
19145 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 Data())..*/.SQLI
19146 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19147 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
19148 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 eOverflow(BtCurs
19149 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
1914a 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1914b 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1914c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1914d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
1914e 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
1914f 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19150 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c (!pCur->isIncrbl
19151 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 obHandle);. ass
19152 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
19153 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 flow);. pCur->i
19154 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
19155 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 1;.}.#endif../
19156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
19157 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
19158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1915a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1915b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1915c 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 egin file backup
1915d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1915e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1915f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
19160 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72 *.** 2009 Januar
19161 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 28.**.** The a
19162 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
19163 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
19164 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
19165 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
19166 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
19167 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
19168 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
19169 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
1916a 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1916b 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1916c 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1916d 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1916e 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1916f 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
19170 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
19171 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
19172 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
19173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19177 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
19178 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
19179 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1917a 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
1917b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49 up_XXX() .** API
1917c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74 functions and t
1917d 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75 he related featu
1917e 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72 res..*/../* Macr
1917f 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 o to find the mi
19180 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d nimum of two num
19181 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a eric values..*/.
19182 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 #ifndef MIN.# de
19183 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 fine MIN(x,y) ((
19184 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a x)<(y)?(x):(y)).
19185 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74 #endif../*.** St
19186 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 ructure allocate
19187 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b 75 d for each backu
19188 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a p operation..*/.
19189 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 struct sqlite3_b
1918a 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 ackup {. sqlite
1918b 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20 20 3* pDestDb;
1918c 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f /* Destinatio
1918d 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c n database handl
1918e 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 44 e */. Btree *pD
1918f 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 est;
19190 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 62 /* Destination b
19191 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20 -tree file */.
19192 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61 3b u32 iDestSchema;
19193 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 /* Orig
19194 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b inal schema cook
19195 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69 6f ie in destinatio
19196 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73 74 n */. int bDest
19197 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 Locked;
19198 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20 77 /* True once a w
19199 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1919a 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73 is open on pDes
1919b 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65 t */.. Pgno iNe
1919c 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
1919d 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
1919e 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75 72 of the next sour
1919f 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79 20 ce page to copy
191a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 */. sqlite3* pS
191a1 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a rcDb; /*
191a2 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 Source database
191a3 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 handle */. Btr
191a4 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 ee *pSrc;
191a5 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
191a6 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a b-tree file */..
191a7 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
191a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 /* Ba
191a9 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72 72 ckup process err
191aa 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a or code */.. /*
191ab 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69 61 These two varia
191ac 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79 20 bles are set by
191ad 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62 61 every call to ba
191ae 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65 ckup_step(). The
191af 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64 20 y are. ** read
191b0 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b by calls to back
191b1 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 up_remaining() a
191b2 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f nd backup_pageco
191b3 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67 unt().. */. Pg
191b4 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20 no nRemaining;
191b5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
191b6 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20 74 of pages left t
191b7 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f o copy */. Pgno
191b8 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20 20 nPagecount;
191b9 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
191ba 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f mber of pages to
191bb 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20 copy */.. int
191bc 69 73 41 74 74 61 63 68 65 64 3b 20 20 20 20 20 isAttached;
191bd 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 /* True onc
191be 65 20 62 61 63 6b 75 70 20 68 61 73 20 62 65 65 e backup has bee
191bf 6e 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 n registered wit
191c0 68 20 70 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c h pager */. sql
191c1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65 ite3_backup *pNe
191c2 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61 xt; /* Next ba
191c3 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20 ckup associated
191c4 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65 with source page
191c5 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 r */.};../*.** T
191c6 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54 HREAD SAFETY NOT
191c7 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65 ES:.**.** Once
191c8 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65 it has been cre
191c9 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75 ated using backu
191ca 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67 p_init(), a sing
191cb 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 le sqlite3_backu
191cc 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 p.** structure
191cd 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 may be accessed
191ce 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20 via two groups
191cf 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65 of thread-safe e
191d0 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a ntry points:.**.
191d1 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 ** * Via the
191d2 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
191d3 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 XXX() API functi
191d4 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 on backup_step()
191d5 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62 and .** b
191d6 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 ackup_finish().
191d7 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74 Both these funct
191d8 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20 ions obtain the
191d9 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a source database.
191da 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20 ** handle
191db 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75 mutex and the mu
191dc 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 tex associated w
191dd 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42 ith the source B
191de 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20 tShared .**
191df 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20 structure, in
191e0 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a that order..**.*
191e1 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20 * * Via the
191e2 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61 BackupUpdate() a
191e3 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 nd BackupRestart
191e4 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68 () functions, wh
191e5 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 ich are.**
191e6 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 invoked by the
191e7 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72 pager layer to r
191e8 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74 eport various st
191e9 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a ate changes in.*
191ea 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65 * the page
191eb 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65 cache associate
191ec 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 d with the sourc
191ed 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 e database. The
191ee 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61 mutex.** a
191ef 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
191f0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
191f1 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 se BtShared stru
191f2 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79 cture will alway
191f3 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68 s .** be h
191f4 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20 eld when either
191f5 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
191f6 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a ns are invoked..
191f7 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 **.** The othe
191f8 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 r sqlite3_backup
191f9 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 _XXX() API funct
191fa 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d ions, backup_rem
191fb 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20 aining() and.**
191fc 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 backup_pagecou
191fd 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72 nt() are not thr
191fe 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f ead-safe functio
191ff 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 ns. If they are
19200 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c called.** whil
19201 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 e some other thr
19202 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62 ead is calling b
19203 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20 ackup_step() or
19204 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c backup_finish(),
19205 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73 .** the values
19206 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65 returned may be
19207 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20 invalid. There
19208 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 is no way for a
19209 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63 call to.** Bac
1920a 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42 kupUpdate() or B
1920b 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74 ackupRestart() t
1920c 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 o interfere with
1920d 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
1920e 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b g().** or back
1920f 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a up_pagecount()..
19210 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e **.** Dependin
19211 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 g on the SQLite
19212 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74 configuration, t
19213 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
19214 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20 les and/or.**
19215 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 the Btree object
19216 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72 s may have their
19217 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61 own mutexes tha
19218 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e t require lockin
19219 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72 g..** Non-shar
1921a 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d able Btrees (in-
1921b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 memory databases
1921c 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64 for example), d
1921d 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 o not have.**
1921e 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78 associated mutex
1921f 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 es..*/../*.** Re
19220 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63 turn a pointer c
19221 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
19222 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e database zDb (i.
19223 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e. "main", "temp
19224 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 ").** in connect
19225 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 ion handle pDb.
19226 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61 If such a databa
19227 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 se cannot be fou
19228 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 nd, return.** a
19229 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 NULL pointer and
1922a 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 write an error
1922b 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f message to pErro
1922c 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 rDb..**.** If th
1922d 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73 e "temp" databas
1922e 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 e is requested,
1922f 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 it may need to b
19230 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 e opened by this
19231 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 .** function. I
19232 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
19233 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
19234 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77 , return 0 and w
19235 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f rite an .** erro
19236 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 r message to pEr
19237 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 rorDb..*/.static
19238 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65 Btree *findBtre
19239 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f e(sqlite3 *pErro
1923a 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44 rDb, sqlite3 *pD
1923b 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
1923c 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73 Db){. int i = s
1923d 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 qlite3FindDbName
1923e 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69 (pDb, zDb);.. i
1923f 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50 f( i==1 ){. P
19240 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 arse *pParse;.
19241 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 int rc = 0;.
19242 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 pParse = sqlit
19243 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f e3StackAllocZero
19244 28 70 45 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f (pErrorDb, sizeo
19245 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 20 f(*pParse));.
19246 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29 if( pParse==0 )
19247 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
19248 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53 rror(pErrorDb, S
19249 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75 QLITE_NOMEM, "ou
1924a 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 t of memory");.
1924b 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1924c 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 _NOMEM;. }els
1924d 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d e{. pParse-
1924e 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 20 >db = pDb;.
1924f 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 65 6e if( sqlite3Open
19250 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 TempDatabase(pPa
19251 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 rse) ){.
19252 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 sqlite3ErrorClea
19253 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 r(pParse);.
19254 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
19255 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73 65 pErrorDb, pParse
19256 2d 3e 72 63 2c 20 22 25 73 22 2c 20 70 50 61 72 ->rc, "%s", pPar
19257 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 se->zErrMsg);.
19258 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
19259 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d E_ERROR;. }
1925a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
1925b 61 63 6b 46 72 65 65 28 70 45 72 72 6f 72 44 62 ackFree(pErrorDb
1925c 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d , pParse);. }
1925d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
1925e 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
1925f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
19260 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 i<0 ){. sqlit
19261 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 e3Error(pErrorDb
19262 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 , SQLITE_ERROR,
19263 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 "unknown databas
19264 65 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 e %s", zDb);.
19265 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
19266 20 20 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 return pDb->aD
19267 62 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a b[i].pBt;.}../*.
19268 2a 2a 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c ** Create an sql
19269 69 74 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 ite3_backup proc
1926a 65 73 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20 ess to copy the
1926b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 contents of zSrc
1926c 44 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 Db from.** conne
1926d 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 ction handle pSr
1926e 63 44 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69 cDb to zDestDb i
1926f 6e 20 70 44 65 73 74 44 62 2e 20 49 66 20 73 75 n pDestDb. If su
19270 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e ccessful, return
19271 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
19272 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33 the new sqlite3
19273 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a _backup object..
19274 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
19275 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 r occurs, NULL i
19276 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 s returned and a
19277 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 n error code and
19278 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a error message.*
19279 2a 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 * stored in data
1927a 62 61 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73 base handle pDes
1927b 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 tDb..*/.SQLITE_A
1927c 50 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 PI sqlite3_backu
1927d 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 p *sqlite3_backu
1927e 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 p_init(. sqlite
1927f 33 2a 20 70 44 65 73 74 44 62 2c 20 20 20 20 20 3* pDestDb,
19280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19281 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 77 /* Database to w
19282 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e rite to */. con
19283 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62 st char *zDestDb
19284 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19285 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 /* Name of da
19286 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 44 tabase within pD
19287 65 73 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 estDb */. sqlit
19288 65 33 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20 e3* pSrcDb,
19289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1928a 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e /* Database con
1928b 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 nection to read
1928c 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 from */. const
1928d 63 68 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 char *zSrcDb
1928e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1928f 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 /* Name of datab
19290 61 73 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 ase within pSrcD
19291 62 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 b */.){. sqlite
19292 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 3_backup *p;
19293 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19294 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
19295 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b rn */.. /* Lock
19296 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
19297 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 base handle. The
19298 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
19299 61 62 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c abase. ** handl
1929a 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 e is not locked
1929b 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c in this routine,
1929c 20 62 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 but it is locke
1929d 64 20 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 d in. ** sqlite
1929e 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 3_backup_step().
1929f 20 54 68 65 20 75 73 65 72 20 69 73 20 72 65 71 The user is req
192a0 75 69 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 uired to ensure
192a1 74 68 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 that no. ** oth
192a2 65 72 20 74 68 72 65 61 64 20 61 63 63 65 73 73 er thread access
192a3 65 73 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 es the destinati
192a4 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 on handle for th
192a5 65 20 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 e duration. **
192a6 6f 66 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 of the backup op
192a7 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 eration. Any at
192a8 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 tempt to use the
192a9 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a destination. *
192aa 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
192ab 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 ction while a ba
192ac 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 ckup is in progr
192ad 65 73 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20 ess may cause.
192ae 2a 2a 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e ** a malfunction
192af 20 6f 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a or a deadlock..
192b0 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d */. sqlite3_m
192b1 75 74 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44 utex_enter(pSrcD
192b2 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
192b3 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
192b4 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 (pDestDb->mutex)
192b5 3b 0a 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d ;.. if( pSrcDb=
192b6 3d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 =pDestDb ){.
192b7 73 71 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20 sqlite3Error(.
192b8 20 20 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 pDestDb, S
192b9 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f QLITE_ERROR, "so
192ba 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 urce and destina
192bb 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 tion must be dis
192bc 74 69 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 tinct". );.
192bd 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 p = 0;. }else
192be 20 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 {. /* Alloca
192bf 74 65 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e te space for a n
192c0 65 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ew sqlite3_backu
192c1 70 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 p object */.
192c2 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63 p = (sqlite3_bac
192c3 6b 75 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 kup *)sqlite3_ma
192c4 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 lloc(sizeof(sqli
192c5 74 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 te3_backup));.
192c6 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 if( !p ){.
192c7 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
192c8 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e DestDb, SQLITE_N
192c9 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a OMEM, 0);. }.
192ca 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
192cb 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 allocation succ
192cc 65 65 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 eeded, populate
192cd 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 the new object.
192ce 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 */. if( p ){.
192cf 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 memset(p, 0, s
192d0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 izeof(sqlite3_ba
192d1 63 6b 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 ckup));. p->p
192d2 53 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 Src = findBtree(
192d3 70 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c pDestDb, pSrcDb,
192d4 20 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d zSrcDb);. p-
192d5 3e 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 >pDest = findBtr
192d6 65 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 ee(pDestDb, pDes
192d7 74 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 tDb, zDestDb);.
192d8 20 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 p->pDestDb =
192d9 70 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e pDestDb;. p->
192da 70 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b pSrcDb = pSrcDb;
192db 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 . p->iNext =
192dc 31 3b 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61 1;. p->isAtta
192dd 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 ched = 0;.. i
192de 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c f( 0==p->pSrc ||
192df 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a 0==p->pDest ){.
192e0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 /* One (or
192e1 20 62 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61 both) of the na
192e2 6d 65 64 20 64 61 74 61 62 61 73 65 73 20 64 69 med databases di
192e3 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 41 6e 20 d not exist. An
192e4 65 72 72 6f 72 20 68 61 73 0a 20 20 20 20 20 20 error has.
192e5 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
192e6 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
192e7 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c 65 2e pDestDb handle.
192e8 20 41 6c 6c 20 74 68 61 74 20 69 73 20 6c 65 66 All that is lef
192e9 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f t. ** to do
192ea 20 68 65 72 65 20 69 73 20 66 72 65 65 20 74 68 here is free th
192eb 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
192ec 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 structure..
192ed 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
192ee 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 e3_free(p);.
192ef 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 p = 0;. }.
192f0 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 }. if( p ){.
192f1 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b p->pSrc->nBack
192f2 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c up++;. }.. sql
192f3 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
192f4 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 (pDestDb->mutex)
192f5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
192f6 78 5f 6c 65 61 76 65 28 70 53 72 63 44 62 2d 3e x_leave(pSrcDb->
192f7 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
192f8 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 p;.}../*.** Arg
192f9 75 6d 65 6e 74 20 72 63 20 69 73 20 61 6e 20 53 ument rc is an S
192fa 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
192fb 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 . Return true if
192fc 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 0a this error is .
192fd 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 ** considered fa
192fe 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65 72 tal if encounter
192ff 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b ed during a back
19300 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c up operation. Al
19301 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20 l errors.** are
19302 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c considered fatal
19303 20 65 78 63 65 70 74 20 66 6f 72 20 53 51 4c 49 except for SQLI
19304 54 45 5f 42 55 53 59 20 61 6e 64 20 53 51 4c 49 TE_BUSY and SQLI
19305 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74 TE_LOCKED..*/.st
19306 61 74 69 63 20 69 6e 74 20 69 73 46 61 74 61 6c atic int isFatal
19307 45 72 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20 Error(int rc){.
19308 20 72 65 74 75 72 6e 20 28 72 63 21 3d 53 51 4c return (rc!=SQL
19309 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
1930a 4c 49 54 45 5f 42 55 53 59 20 26 26 20 41 4c 57 LITE_BUSY && ALW
1930b 41 59 53 28 72 63 21 3d 53 51 4c 49 54 45 5f 4c AYS(rc!=SQLITE_L
1930c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a OCKED));.}../*.*
1930d 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 53 72 63 * Parameter zSrc
1930e 44 61 74 61 20 70 6f 69 6e 74 73 20 74 6f 20 61 Data points to a
1930f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 buffer containi
19310 6e 67 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 ng the data for
19311 0a 2a 2a 20 70 61 67 65 20 69 53 72 63 50 67 20 .** page iSrcPg
19312 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 from the source
19313 64 61 74 61 62 61 73 65 2e 20 43 6f 70 79 20 74 database. Copy t
19314 68 69 73 20 64 61 74 61 20 69 6e 74 6f 20 74 68 his data into th
19315 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f e .** destinatio
19316 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 n database..*/.s
19317 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 tatic int backup
19318 4f 6e 65 50 61 67 65 28 73 71 6c 69 74 65 33 5f OnePage(sqlite3_
19319 62 61 63 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 backup *p, Pgno
1931a 69 53 72 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 iSrcPg, const u8
1931b 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a 20 20 50 *zSrcData){. P
1931c 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 ager * const pDe
1931d 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 stPager = sqlite
1931e 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 3BtreePager(p->p
1931f 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 Dest);. const i
19320 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d 20 73 71 nt nSrcPgsz = sq
19321 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
19322 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a eSize(p->pSrc);.
19323 20 20 69 6e 74 20 6e 44 65 73 74 50 67 73 7a 20 int nDestPgsz
19324 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
19325 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 tPageSize(p->pDe
19326 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 st);. const int
19327 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72 nCopy = MIN(nSr
19328 63 50 67 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a cPgsz, nDestPgsz
19329 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36 34 20 69 );. const i64 i
1932a 45 6e 64 20 3d 20 28 69 36 34 29 69 53 72 63 50 End = (i64)iSrcP
1932b 67 2a 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b g*(i64)nSrcPgsz;
1932c 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c .. int rc = SQL
1932d 49 54 45 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f ITE_OK;. i64 iO
1932e 66 66 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 ff;.. assert( p
1932f 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b ->bDestLocked );
19330 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 46 61 . assert( !isFa
19331 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 talError(p->rc)
19332 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 72 );. assert( iSr
19333 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 cPg!=PENDING_BYT
19334 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e E_PAGE(p->pSrc->
19335 70 42 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 pBt) );. assert
19336 28 20 7a 53 72 63 44 61 74 61 20 29 3b 0a 0a 20 ( zSrcData );..
19337 20 2f 2a 20 43 61 74 63 68 20 74 68 65 20 63 61 /* Catch the ca
19338 73 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73 se where the des
19339 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 tination is an i
1933a 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1933b 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70 e and the. ** p
1933c 61 67 65 20 73 69 7a 65 73 20 6f 66 20 74 68 65 age sizes of the
1933d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 source and dest
1933e 69 6e 61 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 ination differ.
1933f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 . */. if( nSrc
19340 50 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 Pgsz!=nDestPgsz
19341 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 && sqlite3PagerI
19342 73 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42 74 sMemdb(sqlite3Bt
19343 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 reePager(p->pDes
19344 74 29 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 t)) ){. rc =
19345 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
19346 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 . }.. /* This
19347 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 loop runs once f
19348 6f 72 20 65 61 63 68 20 64 65 73 74 69 6e 61 74 or each destinat
19349 69 6f 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64 ion page spanned
1934a 20 62 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a by the source .
1934b 20 20 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 ** page. For e
1934c 61 63 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 ach iteration, v
1934d 61 72 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20 ariable iOff is
1934e 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 set to the byte
1934f 6f 66 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 offset. ** of t
19350 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 he destination p
19351 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 age.. */. for(
19352 69 4f 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e iOff=iEnd-(i64)n
19353 53 72 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c SrcPgsz; rc==SQL
19354 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 ITE_OK && iOff<i
19355 45 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 End; iOff+=nDest
19356 50 67 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 Pgsz){. DbPag
19357 65 20 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a e *pDestPg = 0;.
19358 20 20 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d Pgno iDest =
19359 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 (Pgno)(iOff/nDe
1935a 73 74 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 stPgsz)+1;. i
1935b 66 28 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e f( iDest==PENDIN
1935c 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 G_BYTE_PAGE(p->p
1935d 44 65 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e Dest->pBt) ) con
1935e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 tinue;. if( S
1935f 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
19360 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
19361 70 44 65 73 74 50 61 67 65 72 2c 20 69 44 65 73 pDestPager, iDes
19362 74 2c 20 26 70 44 65 73 74 50 67 29 29 0a 20 20 t, &pDestPg)).
19363 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d && SQLITE_OK=
19364 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 =(rc = sqlite3Pa
19365 67 65 72 57 72 69 74 65 28 70 44 65 73 74 50 67 gerWrite(pDestPg
19366 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 )). ){.
19367 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 const u8 *zIn =
19368 26 7a 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e &zSrcData[iOff%n
19369 53 72 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 SrcPgsz];.
1936a 75 38 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 u8 *zDestData =
1936b 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
1936c 61 74 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20 ata(pDestPg);.
1936d 20 20 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 u8 *zOut = &
1936e 7a 44 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e zDestData[iOff%n
1936f 44 65 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 DestPgsz];..
19370 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61 /* Copy the da
19371 74 61 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 ta from the sour
19372 63 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 ce page into the
19373 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
19374 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e e.. ** Then
19375 20 63 6c 65 61 72 20 74 68 65 20 42 74 72 65 65 clear the Btree
19376 20 6c 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 layer MemPage.i
19377 73 49 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 sInit flag. Both
19378 20 74 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 this module.
19379 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 ** and the pa
1937a 67 65 72 20 63 6f 64 65 20 75 73 65 20 74 68 69 ger code use thi
1937b 73 20 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e s trick (clearin
1937c 67 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 g the first byte
1937d 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 . ** of the
1937e 20 70 61 67 65 20 27 65 78 74 72 61 27 20 73 70 page 'extra' sp
1937f 61 63 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 ace to invalidat
19380 65 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 e the Btree laye
19381 72 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 rs. ** cach
19382 65 64 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 ed parse of the
19383 70 61 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 page). MemPage.i
19384 73 49 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20 sInit is marked
19385 0a 20 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 . ** "MUST
19386 42 45 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 BE FIRST" for th
19387 69 73 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 is purpose..
19388 20 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 */. memcp
19389 79 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f y(zOut, zIn, nCo
1938a 70 79 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20 py);. ((u8
1938b 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
1938c 74 45 78 74 72 61 28 70 44 65 73 74 50 67 29 29 tExtra(pDestPg))
1938d 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 [0] = 0;. }.
1938e 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
1938f 6e 72 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20 nref(pDestPg);.
19390 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
19391 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 .}../*.** If pFi
19392 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 le is currently
19393 6c 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a larger than iSiz
19394 65 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72 e bytes, then tr
19395 75 6e 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 uncate it to.**
19396 65 78 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79 exactly iSize by
19397 74 65 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73 tes. If pFile is
19398 20 6e 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e not larger than
19399 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 iSize bytes, th
1939a 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 en.** this funct
1939b 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
1939c 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
1939d 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
1939e 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 hing is successf
1939f 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 ul, or an SQLite
193a0 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 error .** code
193a1 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
193a2 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e rs..*/.static in
193a3 74 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 t backupTruncate
193a4 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c File(sqlite3_fil
193a5 65 20 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53 e *pFile, i64 iS
193a6 69 7a 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72 ize){. i64 iCur
193a7 72 65 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d rent;. int rc =
193a8 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
193a9 7a 65 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72 ze(pFile, &iCurr
193aa 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ent);. if( rc==
193ab 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 SQLITE_OK && iCu
193ac 72 72 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 rrent>iSize ){.
193ad 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
193ae 73 54 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c sTruncate(pFile,
193af 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 iSize);. }. r
193b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
193b1 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 69 73 ** Register this
193b2 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 77 backup object w
193b3 69 74 68 20 74 68 65 20 61 73 73 6f 63 69 61 74 ith the associat
193b4 65 64 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 ed source pager
193b5 66 6f 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 for.** callbacks
193b6 20 77 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 when pages are
193b7 63 68 61 6e 67 65 64 20 6f 72 20 74 68 65 20 63 changed or the c
193b8 61 63 68 65 20 69 6e 76 61 6c 69 64 61 74 65 64 ache invalidated
193b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
193ba 20 61 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a attachBackupObj
193bb 65 63 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ect(sqlite3_back
193bc 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 up *p){. sqlite
193bd 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20 3_backup **pp;.
193be 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
193bf 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
193c0 70 2d 3e 70 53 72 63 29 20 29 3b 0a 20 20 70 70 p->pSrc) );. pp
193c1 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 = sqlite3PagerB
193c2 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 ackupPtr(sqlite3
193c3 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 BtreePager(p->pS
193c4 72 63 29 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 rc));. p->pNext
193c5 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 = *pp;. *pp =
193c6 70 3b 0a 20 20 70 2d 3e 69 73 41 74 74 61 63 68 p;. p->isAttach
193c7 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 1;.}../*.**
193c8 20 43 6f 70 79 20 6e 50 61 67 65 20 70 61 67 65 Copy nPage page
193c9 73 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 s from the sourc
193ca 65 20 62 2d 74 72 65 65 20 74 6f 20 74 68 65 20 e b-tree to the
193cb 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a destination..*/.
193cc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
193cd 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
193ce 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ep(sqlite3_backu
193cf 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 p *p, int nPage)
193d0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 73 {. int rc;.. s
193d1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
193d2 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 er(p->pSrcDb->mu
193d3 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 tex);. sqlite3B
193d4 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 treeEnter(p->pSr
193d5 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 c);. if( p->pDe
193d6 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 stDb ){. sqli
193d7 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
193d8 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 p->pDestDb->mute
193d9 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 x);. }.. rc =
193da 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21 69 73 p->rc;. if( !is
193db 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29 20 29 FatalError(rc) )
193dc 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63 6f {. Pager * co
193dd 6e 73 74 20 70 53 72 63 50 61 67 65 72 20 3d 20 nst pSrcPager =
193de 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
193df 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20 20 20 r(p->pSrc);
193e0 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 72 20 /* Source pager
193e1 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63 */. Pager * c
193e2 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 20 onst pDestPager
193e3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 = sqlite3BtreePa
193e4 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 20 20 ger(p->pDest);
193e5 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72 20 2a /* Dest pager *
193e6 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 /. int ii;
193e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193e8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
193e9 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
193ea 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50 61 67 . int nSrcPag
193eb 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 e = -1;
193ec 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
193ed 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69 6e 20 of source db in
193ee 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 pages */. int
193ef 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 30 bCloseTrans = 0
193f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
193f1 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 20 64 /* True if src d
193f2 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c 6f 63 b requires unloc
193f3 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 king */.. /*
193f4 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 If the source pa
193f5 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ger is currently
193f6 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e in a write-tran
193f7 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72 6e 0a saction, return.
193f8 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 ** SQLITE_BU
193f9 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a SY immediately..
193fa 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
193fb 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70 2d 3e ->pDestDb && p->
193fc 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 pSrc->pBt->inTra
193fd 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
193fe 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 WRITE ){. r
193ff 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
19400 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
19401 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
19402 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
19403 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69 6e 61 Lock the destina
19404 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 69 tion database, i
19405 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b f it is not lock
19406 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f 0a 20 ed already. */.
19407 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
19408 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65 73 74 ==rc && p->bDest
19409 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20 20 26 Locked==0. &
1940a 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 & SQLITE_OK==(rc
1940b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 = sqlite3BtreeB
1940c 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 44 65 eginTrans(p->pDe
1940d 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29 7b 0a st, 2)) . ){.
1940e 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74 4c 6f p->bDestLo
1940f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 cked = 1;.
19410 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
19411 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 42 54 eta(p->pDest, BT
19412 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 REE_SCHEMA_VERSI
19413 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 53 63 68 ON, &p->iDestSch
19414 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ema);. }..
19415 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
19416 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d 74 72 61 no open read-tra
19417 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 nsaction on the
19418 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2c source database,
19419 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65 open. ** one
1941a 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 now. If a trans
1941b 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 action is opened
1941c 20 68 65 72 65 2c 20 74 68 65 6e 20 69 74 20 77 here, then it w
1941d 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 ill be closed.
1941e 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 ** before this
1941f 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e function exits.
19420 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
19421 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
19422 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 0==sqlite3Btree
19423 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 2d IsInReadTrans(p-
19424 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 20 >pSrc) ){.
19425 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
19426 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 eBeginTrans(p->p
19427 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 Src, 0);. b
19428 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a CloseTrans = 1;.
19429 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1942a 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 69 Now that there i
1942b 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e s a read-lock on
1942c 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1942d 62 61 73 65 2c 20 71 75 65 72 79 20 74 68 65 0a base, query the.
1942e 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 ** source pa
1942f 67 65 72 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 ger for the numb
19430 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
19431 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 he database..
19432 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
19433 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
19434 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
19435 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 53 agerPagecount(pS
19436 72 63 50 61 67 65 72 2c 20 26 6e 53 72 63 50 61 rcPager, &nSrcPa
19437 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 ge);. }. f
19438 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c or(ii=0; (nPage<
19439 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 0 || ii<nPage) &
1943a 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e & p->iNext<=(Pgn
1943b 6f 29 6e 53 72 63 50 61 67 65 20 26 26 20 21 72 o)nSrcPage && !r
1943c 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 c; ii++){.
1943d 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 const Pgno iSrcP
1943e 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20 g = p->iNext;
1943f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19440 20 53 6f 75 72 63 65 20 70 61 67 65 20 6e 75 6d Source page num
19441 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ber */. if(
19442 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 iSrcPg!=PENDING
19443 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 _BYTE_PAGE(p->pS
19444 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 rc->pBt) ){.
19445 20 20 20 20 44 62 50 61 67 65 20 2a 70 53 72 63 DbPage *pSrc
19446 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Pg;
19447 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19448 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20 6f /* Source page o
19449 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 20 20 20 bject */.
1944a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1944b 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72 2c erGet(pSrcPager,
1944c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 67 iSrcPg, &pSrcPg
1944d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1944e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1944f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 rc = b
19450 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 ackupOnePage(p,
19451 69 53 72 63 50 67 2c 20 73 71 6c 69 74 65 33 50 iSrcPg, sqlite3P
19452 61 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63 agerGetData(pSrc
19453 50 67 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Pg));.
19454 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
19455 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 f(pSrcPg);.
19456 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
19457 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20 p->iNext++;.
19458 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
19459 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1945a 20 20 20 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e p->nPagecoun
1945b 74 20 3d 20 6e 53 72 63 50 61 67 65 3b 0a 20 20 t = nSrcPage;.
1945c 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e p->nRemainin
1945d 67 20 3d 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 g = nSrcPage+1-p
1945e 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20 20 20 69 ->iNext;. i
1945f 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e f( p->iNext>(Pgn
19460 6f 29 6e 53 72 63 50 61 67 65 20 29 7b 0a 20 20 o)nSrcPage ){.
19461 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
19462 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 65 E_DONE;. }e
19463 6c 73 65 20 69 66 28 20 21 70 2d 3e 69 73 41 74 lse if( !p->isAt
19464 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 20 20 tached ){.
19465 20 20 61 74 74 61 63 68 42 61 63 6b 75 70 4f 62 attachBackupOb
19466 6a 65 63 74 28 70 29 3b 0a 20 20 20 20 20 20 7d ject(p);. }
19467 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
19468 20 55 70 64 61 74 65 20 74 68 65 20 73 63 68 65 Update the sche
19469 6d 61 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 ma version field
1946a 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 in the destinat
1946b 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68 ion database. Th
1946c 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74 6f 20 is. ** is to
1946d 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
1946e 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f he schema-versio
1946f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 63 68 n really does ch
19470 61 6e 67 65 20 69 6e 0a 20 20 20 20 2a 2a 20 74 ange in. ** t
19471 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 he case where th
19472 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 e source and des
19473 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
19474 65 73 20 68 61 76 65 20 74 68 65 0a 20 20 20 20 es have the.
19475 2a 2a 20 73 61 6d 65 20 73 63 68 65 6d 61 20 76 ** same schema v
19476 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 ersion.. */.
19477 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
19478 45 5f 44 4f 4e 45 20 0a 20 20 20 20 20 26 26 20 E_DONE . &&
19479 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 (rc = sqlite3Btr
1947a 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2d 3e eeUpdateMeta(p->
1947b 70 44 65 73 74 2c 31 2c 70 2d 3e 69 44 65 73 74 pDest,1,p->iDest
1947c 53 63 68 65 6d 61 2b 31 29 29 3d 3d 53 51 4c 49 Schema+1))==SQLI
1947d 54 45 5f 4f 4b 0a 20 20 20 20 29 7b 0a 20 20 20 TE_OK. ){.
1947e 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 const int nSr
1947f 63 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 cPagesize = sqli
19480 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 te3BtreeGetPageS
19481 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 ize(p->pSrc);.
19482 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 const int nD
19483 65 73 74 50 61 67 65 73 69 7a 65 20 3d 20 73 71 estPagesize = sq
19484 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
19485 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b eSize(p->pDest);
19486 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 65 73 74 . int nDest
19487 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20 Truncate;. .
19488 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 if( p->pDestD
19489 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c b ){. sql
1948a 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
1948b 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44 65 73 74 lSchema(p->pDest
1948c 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a Db, 0);. }.
1948d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 . /* Set nD
1948e 65 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74 estTruncate to t
1948f 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 he final number
19490 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
19491 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 destination.
19492 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 ** database. T
19493 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 he complication
19494 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68 65 here is that the
19495 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
19496 65 0a 20 20 20 20 20 20 2a 2a 20 73 69 7a 65 20 e. ** size
19497 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 may be different
19498 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 70 to the source p
19499 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 20 20 20 age size. .
1949a 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 **. ** If
1949b 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20 the source page
1949c 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 size is smaller
1949d 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 than the destina
1949e 74 69 6f 6e 20 70 61 67 65 20 73 69 7a 65 2c 20 tion page size,
1949f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20 . ** round
194a0 75 70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 up. In this case
194a1 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c the call to sql
194a2 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 29 ite3OsTruncate()
194a3 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20 below will.
194a4 20 20 2a 2a 20 66 69 78 20 74 68 65 20 73 69 7a ** fix the siz
194a5 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 48 e of the file. H
194a6 6f 77 65 76 65 72 20 69 74 20 69 73 20 69 6d 70 owever it is imp
194a7 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 ortant to call.
194a8 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 ** sqlite3P
194a9 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 agerTruncateImag
194aa 65 28 29 20 68 65 72 65 20 73 6f 20 74 68 61 74 e() here so that
194ab 20 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68 any pages in th
194ac 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 74 e . ** dest
194ad 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 ination file tha
194ae 74 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65 t lie beyond the
194af 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70 nDestTruncate p
194b0 61 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 age mark are.
194b1 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 ** journalled
194b2 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 by PagerCommitP
194b3 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72 65 haseOne() before
194b4 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72 6f they are destro
194b5 79 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 yed. ** by
194b6 74 68 65 20 66 69 6c 65 20 74 72 75 6e 63 61 74 the file truncat
194b7 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
194b8 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 if( nSrcPage
194b9 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 size<nDestPagesi
194ba 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e ze ){. in
194bb 74 20 72 61 74 69 6f 20 3d 20 6e 44 65 73 74 50 t ratio = nDestP
194bc 61 67 65 73 69 7a 65 2f 6e 53 72 63 50 61 67 65 agesize/nSrcPage
194bd 73 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 6e 44 size;. nD
194be 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 28 6e estTruncate = (n
194bf 53 72 63 50 61 67 65 2b 72 61 74 69 6f 2d 31 29 SrcPage+ratio-1)
194c0 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 20 20 20 /ratio;.
194c1 69 66 28 20 6e 44 65 73 74 54 72 75 6e 63 61 74 if( nDestTruncat
194c2 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f e==(int)PENDING_
194c3 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 BYTE_PAGE(p->pDe
194c4 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 st->pBt) ){.
194c5 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 nDestTrunc
194c6 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d ate--;. }
194c7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
194c8 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 nDestTrunc
194c9 61 74 65 20 3d 20 6e 53 72 63 50 61 67 65 20 2a ate = nSrcPage *
194ca 20 28 6e 53 72 63 50 61 67 65 73 69 7a 65 2f 6e (nSrcPagesize/n
194cb 44 65 73 74 50 61 67 65 73 69 7a 65 29 3b 0a 20 DestPagesize);.
194cc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
194cd 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
194ce 65 49 6d 61 67 65 28 70 44 65 73 74 50 61 67 65 eImage(pDestPage
194cf 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 r, nDestTruncate
194d0 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6e 53 );.. if( nS
194d1 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 rcPagesize<nDest
194d2 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 Pagesize ){.
194d3 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f /* If the so
194d4 75 72 63 65 20 70 61 67 65 2d 73 69 7a 65 20 69 urce page-size i
194d5 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 s smaller than t
194d6 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 he destination p
194d7 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20 age-size,.
194d8 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 61 20 74 ** two extra t
194d9 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 64 20 74 hings may need t
194da 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20 20 20 20 o happen:.
194db 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
194dc 20 20 2a 20 54 68 65 20 64 65 73 74 69 6e 61 74 * The destinat
194dd 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 ion may need to
194de 62 65 20 74 72 75 6e 63 61 74 65 64 2c 20 61 6e be truncated, an
194df 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 d. **.
194e0 20 20 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 ** * Data
194e1 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 stored on the p
194e2 61 67 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 ages immediately
194e3 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a following the .
194e4 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70 ** p
194e5 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 ending-byte page
194e6 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
194e7 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64 atabase may need
194e8 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a to be. *
194e9 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69 6e 74 * copied int
194ea 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
194eb 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 n database..
194ec 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 */. c
194ed 6f 6e 73 74 20 69 36 34 20 69 53 69 7a 65 20 3d onst i64 iSize =
194ee 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 73 69 (i64)nSrcPagesi
194ef 7a 65 20 2a 20 28 69 36 34 29 6e 53 72 63 50 61 ze * (i64)nSrcPa
194f0 67 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ge;. sqli
194f1 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74 te3_file * const
194f2 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 pFile = sqlite3
194f3 50 61 67 65 72 46 69 6c 65 28 70 44 65 73 74 50 PagerFile(pDestP
194f4 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 ager);..
194f5 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
194f6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
194f7 20 28 69 36 34 29 6e 44 65 73 74 54 72 75 6e 63 (i64)nDestTrunc
194f8 61 74 65 2a 28 69 36 34 29 6e 44 65 73 74 50 61 ate*(i64)nDestPa
194f9 67 65 73 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20 gesize >= iSize
194fa 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 || (.
194fb 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
194fc 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f ==(int)(PENDING_
194fd 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 BYTE_PAGE(p->pDe
194fe 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 st->pBt)-1).
194ff 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65 3e && iSize>
19500 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 =PENDING_BYTE &&
19501 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f iSize<=PENDING_
19502 42 59 54 45 2b 6e 44 65 73 74 50 61 67 65 73 69 BYTE+nDestPagesi
19503 7a 65 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20 ze. ));.
19504 20 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 if( SQLIT
19505 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
19506 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
19507 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 aseOne(pDestPage
19508 72 2c 20 30 2c 20 31 29 29 0a 20 20 20 20 20 20 r, 0, 1)).
19509 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d && SQLITE_OK=
1950a 3d 28 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75 =(rc = backupTru
1950b 6e 63 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c ncateFile(pFile,
1950c 20 69 53 69 7a 65 29 29 0a 20 20 20 20 20 20 20 iSize)).
1950d 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d && SQLITE_OK==
1950e 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
1950f 65 72 53 79 6e 63 28 70 44 65 73 74 50 61 67 65 erSync(pDestPage
19510 72 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 r)). ){.
19511 20 20 20 20 20 20 20 20 20 69 36 34 20 69 4f 66 i64 iOf
19512 66 3b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 f;. i64
19513 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44 iEnd = MIN(PEND
19514 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44 65 73 74 ING_BYTE + nDest
19515 50 61 67 65 73 69 7a 65 2c 20 69 53 69 7a 65 29 Pagesize, iSize)
19516 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
19517 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 . iOf
19518 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e f=PENDING_BYTE+n
19519 53 72 63 50 61 67 65 73 69 7a 65 3b 20 0a 20 20 SrcPagesize; .
1951a 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 rc==SQ
1951b 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c LITE_OK && iOff<
1951c 69 45 6e 64 3b 20 0a 20 20 20 20 20 20 20 20 20 iEnd; .
1951d 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67 iOff+=nSrcPag
1951e 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 20 20 esize.
1951f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 50 ){. P
19520 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d 20 30 gHdr *pSrcPg = 0
19521 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f ;. co
19522 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 nst Pgno iSrcPg
19523 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e = (Pgno)((iOff/n
19524 53 72 63 50 61 67 65 73 69 7a 65 29 2b 31 29 3b SrcPagesize)+1);
19525 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
19526 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
19527 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 72 t(pSrcPager, iSr
19528 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 cPg, &pSrcPg);.
19529 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
1952a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1952b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 38 u8
1952c 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c 69 74 65 *zData = sqlite
1952d 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 53 3PagerGetData(pS
1952e 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 rcPg);.
1952f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
19530 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c 20 3OsWrite(pFile,
19531 7a 44 61 74 61 2c 20 6e 53 72 63 50 61 67 65 73 zData, nSrcPages
19532 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 ize, iOff);.
19533 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
19534 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
19535 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b erUnref(pSrcPg);
19536 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
19537 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
19538 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
19539 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
1953a 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 mitPhaseOne(pDes
1953b 74 50 61 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 tPager, 0, 0);.
1953c 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 }. .
1953d 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74 /* Finish commit
1953e 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 ting the transac
1953f 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 74 tion to the dest
19540 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
19541 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 53 . */. if( S
19542 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 QLITE_OK==rc.
19543 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b && SQLITE_OK
19544 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 ==(rc = sqlite3B
19545 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
19546 77 6f 28 70 2d 3e 70 44 65 73 74 29 29 0a 20 20 wo(p->pDest)).
19547 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ){. r
19548 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
19549 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1954a 20 0a 20 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f . /* If bClo
1954b 73 65 54 72 61 6e 73 20 69 73 20 74 72 75 65 2c seTrans is true,
1954c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
1954d 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20 72 65 61 ion opened a rea
1954e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 d transaction.
1954f 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72 ** on the sour
19550 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6c 6f ce database. Clo
19551 73 65 20 74 68 65 20 72 65 61 64 20 74 72 61 6e se the read tran
19552 73 61 63 74 69 6f 6e 20 68 65 72 65 2e 20 54 68 saction here. Th
19553 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f ere is. ** no
19554 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74 need to check t
19555 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 he return values
19556 20 6f 66 20 74 68 65 20 62 74 72 65 65 20 6d 65 of the btree me
19557 74 68 6f 64 73 20 68 65 72 65 2c 20 61 73 0a 20 thods here, as.
19558 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e ** "committin
19559 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 g" a read-only t
1955a 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f ransaction canno
1955b 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 t fail.. */.
1955c 20 20 20 69 66 28 20 62 43 6c 6f 73 65 54 72 61 if( bCloseTra
1955d 6e 73 20 29 7b 0a 20 20 20 20 20 20 54 45 53 54 ns ){. TEST
1955e 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32 20 29 3b ONLY( int rc2 );
1955f 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 . TESTONLY(
19560 20 72 63 32 20 20 3d 20 29 20 73 71 6c 69 74 65 rc2 = ) sqlite
19561 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
19562 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c 20 30 29 eOne(p->pSrc, 0)
19563 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 ;. TESTONLY
19564 28 20 72 63 32 20 7c 3d 20 29 20 73 71 6c 69 74 ( rc2 |= ) sqlit
19565 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
19566 73 65 54 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a seTwo(p->pSrc);.
19567 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
19568 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 2==SQLITE_OK );.
19569 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e }. . p->
1956a 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69 rc = rc;. }. i
1956b 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
1956c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1956d 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 ex_leave(p->pDes
1956e 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
1956f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
19570 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 eave(p->pSrc);.
19571 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19572 65 61 76 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e eave(p->pSrcDb->
19573 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
19574 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
19575 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 lease all resour
19576 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
19577 69 74 68 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 ith an sqlite3_b
19578 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a ackup* handle..*
19579 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1957a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
1957b 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 finish(sqlite3_b
1957c 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c ackup *p){. sql
1957d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 ite3_backup **pp
1957e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1957f 20 20 2f 2a 20 50 74 72 20 74 6f 20 68 65 61 64 /* Ptr to head
19580 20 6f 66 20 70 61 67 65 72 73 20 62 61 63 6b 75 of pagers backu
19581 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69 p list */. sqli
19582 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
19583 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19584 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 70 72 6f /* Mutex to pro
19585 74 65 63 74 20 73 6f 75 72 63 65 20 64 61 74 61 tect source data
19586 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 base */. int rc
19587 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19589 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1958a 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 n */.. /* Enter
1958b 20 74 68 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a the mutexes */.
1958c 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
1958d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1958e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1958f 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e nter(p->pSrcDb->
19590 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
19591 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 3BtreeEnter(p->p
19592 53 72 63 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20 Src);. mutex =
19593 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 p->pSrcDb->mutex
19594 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 ;. if( p->pDest
19595 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Db ){. sqlite
19596 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
19597 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 >pDestDb->mutex)
19598 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 ;. }.. /* Deta
19599 63 68 20 74 68 69 73 20 62 61 63 6b 75 70 20 66 ch this backup f
1959a 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 70 rom the source p
1959b 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ager. */. if( p
1959c 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
1959d 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 p->pSrc->nBacku
1959e 70 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 p--;. }. if( p
1959f 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a ->isAttached ){.
195a0 20 20 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 pp = sqlite3
195a1 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 PagerBackupPtr(s
195a2 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
195a3 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 (p->pSrc));.
195a4 77 68 69 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b while( *pp!=p ){
195a5 0a 20 20 20 20 20 20 70 70 20 3d 20 26 28 2a 70 . pp = &(*p
195a6 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d p)->pNext;. }
195a7 0a 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e . *pp = p->pN
195a8 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 ext;. }.. /* I
195a9 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
195aa 69 73 20 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e is still open on
195ab 20 74 68 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c the Btree, roll
195ac 20 69 74 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 it back. */. s
195ad 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
195ae 61 63 6b 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a ack(p->pDest);..
195af 20 20 2f 2a 20 53 65 74 20 74 68 65 20 65 72 72 /* Set the err
195b0 6f 72 20 63 6f 64 65 20 6f 66 20 74 68 65 20 64 or code of the d
195b1 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
195b2 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 ase handle. */.
195b3 20 72 63 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 rc = (p->rc==SQ
195b4 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c LITE_DONE) ? SQL
195b5 49 54 45 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a ITE_OK : p->rc;.
195b6 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
195b7 2d 3e 70 44 65 73 74 44 62 2c 20 72 63 2c 20 30 ->pDestDb, rc, 0
195b8 29 3b 0a 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 );.. /* Exit th
195b9 65 20 6d 75 74 65 78 65 73 20 61 6e 64 20 66 72 e mutexes and fr
195ba 65 65 20 74 68 65 20 62 61 63 6b 75 70 20 63 6f ee the backup co
195bb 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 2e ntext structure.
195bc 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 */. if( p->pDe
195bd 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 stDb ){. sqli
195be 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
195bf 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 p->pDestDb->mute
195c0 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 x);. }. sqlite
195c1 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 3BtreeLeave(p->p
195c2 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 Src);. if( p->p
195c3 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 DestDb ){. sq
195c4 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
195c5 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
195c6 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b ex_leave(mutex);
195c7 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
195c8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
195c9 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
195ca 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 s still to be ba
195cb 63 6b 65 64 20 75 70 20 61 73 20 6f 66 20 74 68 cked up as of th
195cc 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a e most recent.**
195cd 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
195ce 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a _backup_step()..
195cf 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
195d0 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
195d1 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 _remaining(sqlit
195d2 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 e3_backup *p){.
195d3 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 return p->nRema
195d4 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ining;.}../*.**
195d5 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
195d6 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
195d7 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
195d8 61 74 61 62 61 73 65 20 61 73 20 6f 66 20 74 68 atabase as of th
195d9 65 20 6d 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e e most .** recen
195da 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 t call to sqlite
195db 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 3_backup_step().
195dc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
195dd 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 nt sqlite3_backu
195de 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 p_pagecount(sqli
195df 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a te3_backup *p){.
195e0 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67 return p->nPag
195e1 65 63 6f 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ecount;.}../*.**
195e2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
195e3 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 s called after t
195e4 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 he contents of p
195e5 61 67 65 20 69 50 61 67 65 20 6f 66 20 74 68 65 age iPage of the
195e6 0a 2a 2a 20 73 6f 75 72 63 65 20 64 61 74 61 62 .** source datab
195e7 61 73 65 20 68 61 76 65 20 62 65 65 6e 20 6d 6f ase have been mo
195e8 64 69 66 69 65 64 2e 20 49 66 20 70 61 67 65 20 dified. If page
195e9 69 50 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 iPage has alread
195ea 79 20 62 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 y been .** copie
195eb 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 d into the desti
195ec 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c nation database,
195ed 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 77 then the data w
195ee 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a ritten to the.**
195ef 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 destination is
195f0 6e 6f 77 20 69 6e 76 61 6c 69 64 61 74 65 64 2e now invalidated.
195f1 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e The destination
195f2 20 63 6f 70 79 20 6f 66 20 69 50 61 67 65 20 6e copy of iPage n
195f3 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70 eeds.** to be up
195f4 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e dated with the n
195f5 65 77 20 64 61 74 61 20 62 65 66 6f 72 65 20 74 ew data before t
195f6 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
195f7 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 ion is.** comple
195f8 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 te..**.** It is
195f9 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
195fa 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
195fb 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 d with the BtSha
195fc 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f red object.** co
195fd 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 rresponding to t
195fe 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
195ff 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 se is held when
19600 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
19601 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 .** called..*/.S
19602 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19603 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 id sqlite3Backup
19604 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62 Update(sqlite3_b
19605 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 2c 20 ackup *pBackup,
19606 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73 Pgno iPage, cons
19607 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 t u8 *aData){.
19608 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19609 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
1960a 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
1960b 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 variable */. f
1960c 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b or(p=pBackup; p;
1960d 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
1960e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1960f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
19610 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 pSrc->pBt->mutex
19611 29 20 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73 ) );. if( !is
19612 46 61 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 FatalError(p->rc
19613 29 20 26 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e ) && iPage<p->iN
19614 65 78 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ext ){. /*
19615 54 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 The backup proce
19616 73 73 20 70 20 68 61 73 20 61 6c 72 65 61 64 79 ss p has already
19617 20 63 6f 70 69 65 64 20 70 61 67 65 20 69 50 61 copied page iPa
19618 67 65 2e 20 42 75 74 20 6e 6f 77 20 69 74 0a 20 ge. But now it.
19619 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e ** has been
1961a 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 20 74 modified by a t
1961b 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
1961c 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 e source pager.
1961d 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 Copy. ** th
1961e 65 20 6e 65 77 20 64 61 74 61 20 69 6e 74 6f 20 e new data into
1961f 74 68 65 20 62 61 63 6b 75 70 2e 0a 20 20 20 20 the backup..
19620 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 */. int r
19621 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 c = backupOnePag
19622 65 28 70 2c 20 69 50 61 67 65 2c 20 61 44 61 74 e(p, iPage, aDat
19623 61 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 a);. assert
19624 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc!=SQLITE_BUS
19625 59 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f Y && rc!=SQLITE_
19626 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 LOCKED );.
19627 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
19628 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e K ){. p->
19629 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d rc = rc;. }
1962a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1962b 0a 2a 2a 20 52 65 73 74 61 72 74 20 74 68 65 20 .** Restart the
1962c 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 2e 20 backup process.
1962d 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77 This is called w
1962e 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6c 61 hen the pager la
1962f 79 65 72 0a 2a 2a 20 64 65 74 65 63 74 73 20 74 yer.** detects t
19630 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
19631 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 has been modifi
19632 65 64 20 62 79 20 61 6e 20 65 78 74 65 72 6e 61 ed by an externa
19633 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f l database.** co
19634 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 nnection. In thi
19635 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 s case there is
19636 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e no way of knowin
19637 67 20 77 68 69 63 68 20 6f 66 20 74 68 65 0a 2a g which of the.*
19638 2a 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76 * pages that hav
19639 65 20 62 65 65 6e 20 63 6f 70 69 65 64 20 69 6e e been copied in
1963a 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 to the destinati
1963b 6f 6e 20 64 61 74 61 62 61 73 65 20 61 72 65 20 on database are
1963c 73 74 69 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 still .** valid
1963d 61 6e 64 20 77 68 69 63 68 20 61 72 65 20 6e 6f and which are no
1963e 74 2c 20 73 6f 20 74 68 65 20 65 6e 74 69 72 65 t, so the entire
1963f 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74 process needs t
19640 6f 20 62 65 20 72 65 73 74 61 72 74 65 64 2e 0a o be restarted..
19641 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 **.** It is assu
19642 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 med that the mut
19643 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ex associated wi
19644 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20 th the BtShared
19645 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 object.** corres
19646 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 ponding to the s
19647 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
19648 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 s held when this
19649 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 function is.**
1964a 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 called..*/.SQLIT
1964b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1964c 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 qlite3BackupRest
1964d 61 72 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b art(sqlite3_back
1964e 75 70 20 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20 up *pBackup){.
1964f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19650 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
19651 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
19652 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 variable */. f
19653 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b or(p=pBackup; p;
19654 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
19655 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
19656 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
19657 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 pSrc->pBt->mutex
19658 29 20 29 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 ) );. p->iNex
19659 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 t = 1;. }.}..#i
1965a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1965b 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 T_VACUUM./*.** C
1965c 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 opy the complete
1965d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 content of pBtF
1965e 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 rom into pBtTo.
1965f 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a A transaction.*
19660 2a 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 * must be active
19661 20 66 6f 72 20 62 6f 74 68 20 66 69 6c 65 73 2e for both files.
19662 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 .**.** The size
19663 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 of file pTo may
19664 62 65 20 72 65 64 75 63 65 64 20 62 79 20 74 68 be reduced by th
19665 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 is operation. If
19666 20 61 6e 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f anything .** go
19667 65 73 20 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 es wrong, the tr
19668 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f ansaction on pTo
19669 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e is rolled back.
1966a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
1966b 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 the .** transact
1966c 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
1966d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1966e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
1966f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
19670 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 BtreeCopyFile(Bt
19671 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 ree *pTo, Btree
19672 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 *pFrom){. int r
19673 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 c;. sqlite3_bac
19674 6b 75 70 20 62 3b 0a 20 20 73 71 6c 69 74 65 33 kup b;. sqlite3
19675 42 74 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b BtreeEnter(pTo);
19676 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
19677 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 nter(pFrom);..
19678 2f 2a 20 53 65 74 20 75 70 20 61 6e 20 73 71 6c /* Set up an sql
19679 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 ite3_backup obje
1967a 63 74 2e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ct. sqlite3_back
1967b 75 70 2e 70 44 65 73 74 44 62 20 6d 75 73 74 20 up.pDestDb must
1967c 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 be set. ** to 0
1967d 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 . This is used b
1967e 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 y the implementa
1967f 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 tions of sqlite3
19680 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 _backup_step().
19681 20 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f ** and sqlite3_
19682 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 backup_finish()
19683 74 6f 20 64 65 74 65 63 74 20 74 68 61 74 20 74 to detect that t
19684 68 65 79 20 61 72 65 20 62 65 69 6e 67 20 63 61 hey are being ca
19685 6c 6c 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 lled. ** from t
19686 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f his function, no
19687 74 20 64 69 72 65 63 74 6c 79 20 62 79 20 74 68 t directly by th
19688 65 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d e user.. */. m
19689 65 6d 73 65 74 28 26 62 2c 20 30 2c 20 73 69 7a emset(&b, 0, siz
1968a 65 6f 66 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 eof(b));. b.pSr
1968b 63 44 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b cDb = pFrom->db;
1968c 0a 20 20 62 2e 70 53 72 63 20 3d 20 70 46 72 6f . b.pSrc = pFro
1968d 6d 3b 0a 20 20 62 2e 70 44 65 73 74 20 3d 20 70 m;. b.pDest = p
1968e 54 6f 3b 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 To;. b.iNext =
1968f 31 3b 0a 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 1;.. /* 0x7FFFF
19690 46 46 46 20 69 73 20 74 68 65 20 68 61 72 64 20 FFF is the hard
19691 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6e 75 limit for the nu
19692 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
19693 20 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a a database. **
19694 20 66 69 6c 65 2e 20 42 79 20 70 61 73 73 69 6e file. By passin
19695 67 20 74 68 69 73 20 61 73 20 74 68 65 20 6e 75 g this as the nu
19696 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f mber of pages to
19697 20 63 6f 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 copy to. ** sq
19698 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
19699 70 28 29 2c 20 77 65 20 63 61 6e 20 67 75 61 72 p(), we can guar
1969a 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 antee that the c
1969b 6f 70 79 20 66 69 6e 69 73 68 65 73 20 0a 20 20 opy finishes .
1969c 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 69 6e 67 ** within a sing
1969d 6c 65 20 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 le call (unless
1969e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 29 an error occurs)
1969f 2e 20 54 68 65 20 61 73 73 65 72 74 28 29 20 73 . The assert() s
196a0 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 tatement. ** ch
196a1 65 63 6b 73 20 74 68 69 73 20 61 73 73 75 6d 70 ecks this assump
196a2 74 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 tion - (p->rc) s
196a3 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 hould be set to
196a4 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 4f either SQLITE_DO
196a5 4e 45 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 NE . ** or an e
196a6 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a rror code.. */.
196a7 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
196a8 5f 73 74 65 70 28 26 62 2c 20 30 78 37 46 46 46 _step(&b, 0x7FFF
196a9 46 46 46 46 29 3b 0a 20 20 61 73 73 65 72 74 28 FFFF);. assert(
196aa 20 62 2e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b b.rc!=SQLITE_OK
196ab 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
196ac 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
196ad 28 26 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d (&b);. if( rc==
196ae 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
196af 20 70 54 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53 pTo->pBt->pageS
196b0 69 7a 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 izeFixed = 0;.
196b1 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 }.. sqlite3Btre
196b2 65 4c 65 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 eLeave(pFrom);.
196b3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
196b4 76 65 28 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 ve(pTo);. retur
196b5 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
196b6 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 * SQLITE_OMIT_VA
196b7 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a CUUM */../******
196b8 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
196b9 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a backup.c *******
196ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196bc 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
196bd 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
196be 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a ile vdbemem.c **
196bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196c1 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
196c2 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 004 May 26.**.**
196c3 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
196c4 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
196c5 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
196c6 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
196c7 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
196c8 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
196c9 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
196ca 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
196cb 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
196cc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
196cd 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
196ce 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
196cf 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
196d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
196d1 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
196d2 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
196d3 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
196d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
196d9 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
196da 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 6d ns code use to m
196db 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d 22 20 anipulate "Mem"
196dc 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 22 4d structure. A "M
196dd 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20 em".** stores a
196de 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69 6e 20 single value in
196df 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d 20 69 the VDBE. Mem i
196e0 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 s an opaque stru
196e1 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a cture visible.**
196e2 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74 68 65 only within the
196e3 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66 61 63 VDBE. Interfac
196e4 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66 65 72 e routines refer
196e5 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 to a Mem using
196e6 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 the.** name sqli
196e7 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a 2f 2a 0a te_value.*/../*.
196e8 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 ** Call sqlite3V
196e9 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
196ea 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 () on the suppli
196eb 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d ed value (type M
196ec 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 em*).** P if req
196ed 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e uired..*/.#defin
196ee 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 e expandBlob(P)
196ef 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d (((P)->flags&MEM
196f0 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 _Zero)?sqlite3Vd
196f1 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 beMemExpandBlob(
196f2 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 P):0)../*.** If
196f3 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63 pMem is an objec
196f4 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 73 t with a valid s
196f5 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
196f6 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 tion, this routi
196f7 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68 ne.** ensures th
196f8 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64 e internal encod
196f9 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 69 ing for the stri
196fa 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
196fb 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64 n is.** 'desired
196fc 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c Enc', one of SQL
196fd 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
196fe 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 _UTF16LE or SQLI
196ff 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a TE_UTF16BE..**.*
19700 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74 * If pMem is not
19701 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74 a string object
19702 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e , or the encodin
19703 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a g of the string.
19704 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ** representatio
19705 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f n is already sto
19706 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 red using the re
19707 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67 quested encoding
19708 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 , then this.** r
19709 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
1970a 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f p..**.** SQLITE_
1970b 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
1970c 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
1970d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28 is successful (
1970e 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29 or not required)
1970f 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ..** SQLITE_NOME
19710 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 M may be returne
19711 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 d if a malloc()
19712 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e fails during con
19713 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 version.** betwe
19714 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 en formats..*/.S
19715 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19716 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 t sqlite3VdbeCha
19717 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 ngeEncoding(Mem
19718 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72 *pMem, int desir
19719 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 edEnc){. int rc
1971a 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
1971b 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 m->flags&MEM_Row
1971c 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
1971d 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d ert( desiredEnc=
1971e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 =SQLITE_UTF8 ||
1971f 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
19720 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20 TE_UTF16LE.
19721 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64 || desired
19722 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 Enc==SQLITE_UTF1
19723 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70 6BE );. if( !(p
19724 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Mem->flags&MEM_S
19725 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 tr) || pMem->enc
19726 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a ==desiredEnc ){.
19727 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
19728 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 E_OK;. }. asse
19729 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
1972a 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
1972b 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
1972c 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 mutex) );.#ifdef
1972d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1972e 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 16. return SQLI
1972f 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a TE_ERROR;.#else.
19730 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 . /* MemTransla
19731 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 te() may return
19732 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c SQLITE_OK or SQL
19733 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f ITE_NOMEM. If NO
19734 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c MEM is returned,
19735 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 . ** then the e
19736 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 ncoding of the v
19737 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 alue may not hav
19738 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a e changed.. */.
19739 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1973a 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 beMemTranslate(p
1973b 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64 Mem, (u8)desired
1973c 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 Enc);. assert(r
1973d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 c==SQLITE_OK
1973e 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f || rc==SQLITE_NO
1973f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72 MEM);. assert(r
19740 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 c==SQLITE_OK
19741 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 || pMem->enc!=de
19742 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 siredEnc);. ass
19743 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e ert(rc==SQLITE_N
19744 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e OMEM || pMem->en
19745 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a c==desiredEnc);.
19746 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
19747 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b dif.}../*.** Mak
19748 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 e sure pMem->z p
19749 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61 oints to a writa
1974a 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f ble allocation o
1974b 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e f at least .** n
1974c 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 bytes..**.** If
1974d 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1974e 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 currently conta
1974f 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c ins string or bl
19750 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74 ob data.** and t
19751 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
19752 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 t passed to this
19753 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 function is tru
19754 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 65 e, the .** curre
19755 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 nt content of th
19756 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 72 e cell is preser
19757 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ved. Otherwise,
19758 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 73 it may.** be dis
19759 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 carded. .**.**
1975a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 This function se
1975b 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66 ts the MEM_Dyn f
1975c 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61 lag and clears a
1975d 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b ny xDel callback
1975e 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65 ..** It also cle
1975f 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e ars MEM_Ephem an
19760 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66 d MEM_Static. If
19761 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c the preserve fl
19762 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 ag is .** not se
19763 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f t, Mem.n is zero
19764 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
19765 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19766 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 3VdbeMemGrow(Mem
19767 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 *pMem, int n, i
19768 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20 nt preserve){.
19769 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20 assert( 1 >=.
1976a 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 ((pMem->zMalloc
1976b 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f && pMem->zMallo
1976c 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 c==pMem->z) ? 1
1976d 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d : 0) +. (((pM
1976e 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
1976f 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 n)&&pMem->xDel)
19770 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 ? 1 : 0) + .
19771 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
19772 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30 M_Ephem) ? 1 : 0
19773 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d ) + . ((pMem-
19774 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 >flags&MEM_Stati
19775 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b c) ? 1 : 0). );
19776 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
19777 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
19778 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 et)==0 );.. if(
19779 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a n<32 ) n = 32;.
1977a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d if( sqlite3DbM
1977b 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e allocSize(pMem->
1977c 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f db, pMem->zMallo
1977d 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 c)<n ){. if(
1977e 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d preserve && pMem
1977f 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c ->z==pMem->zMall
19780 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d oc ){. pMem
19781 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ->z = pMem->zMal
19782 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 loc = sqlite3DbR
19783 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65 eallocOrFree(pMe
19784 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 m->db, pMem->z,
19785 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72 n);. preser
19786 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ve = 0;. }els
19787 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
19788 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c DbFree(pMem->db,
19789 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b pMem->zMalloc);
1978a 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 . pMem->zMa
1978b 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
1978c 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e MallocRaw(pMem->
1978d 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 db, n);. }.
1978e 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a }.. if( pMem->z
1978f 20 26 26 20 70 72 65 73 65 72 76 65 20 26 26 20 && preserve &&
19790 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 pMem->zMalloc &&
19791 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e pMem->z!=pMem->
19792 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d zMalloc ){. m
19793 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c emcpy(pMem->zMal
19794 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d loc, pMem->z, pM
19795 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 em->n);. }. if
19796 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
19797 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 M_Dyn && pMem->x
19798 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d Del ){. pMem-
19799 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 >xDel((void *)(p
1979a 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 Mem->z));. }..
1979b 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d pMem->z = pMem-
1979c 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 >zMalloc;. if(
1979d 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 pMem->z==0 ){.
1979e 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1979f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 MEM_Null;. }els
197a0 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 e{. pMem->fla
197a1 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 gs &= ~(MEM_Ephe
197a2 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 m|MEM_Static);.
197a3 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 }. pMem->xDel
197a4 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 = 0;. return (p
197a5 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f Mem->z ? SQLITE_
197a6 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 OK : SQLITE_NOME
197a7 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b M);.}../*.** Mak
197a8 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 e the given Mem
197a9 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 object MEM_Dyn.
197aa 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
197ab 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 make it so.** t
197ac 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 hat any TEXT or
197ad 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 BLOB content is
197ae 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
197af 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
197b0 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 * malloc(). In
197b1 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f this way, we kno
197b2 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 w that the memor
197b3 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a y is safe to be.
197b4 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f ** overwritten o
197b5 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a r altered..**.**
197b6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
197b7 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
197b8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
197b9 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f malloc fails..*/
197ba 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
197bb 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
197bc 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
197bd 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e Mem *pMem){. in
197be 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t f;. assert( p
197bf 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
197c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
197c1 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
197c2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
197c3 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
197c4 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
197c5 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 expandBlob(pMem)
197c6 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c ;. f = pMem->fl
197c7 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d ags;. if( (f&(M
197c8 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
197c9 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d ) && pMem->z!=pM
197ca 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 em->zMalloc ){.
197cb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
197cc 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
197cd 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 pMem->n + 2, 1)
197ce 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
197cf 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
197d0 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b }. pMem->z[
197d1 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 pMem->n] = 0;.
197d2 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
197d3 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d n+1] = 0;. pM
197d4 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
197d5 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65 _Term;. }.. re
197d6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
197d7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
197d8 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20 61 given Mem* has a
197d9 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
197da 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f 20 l, turn it into
197db 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20 62 an ordinary.** b
197dc 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64 79 lob stored in dy
197dd 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
197de 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69 ted space..*/.#i
197df 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
197e0 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 T_INCRBLOB.SQLIT
197e1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
197e2 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
197e3 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d ndBlob(Mem *pMem
197e4 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 ){. if( pMem->f
197e5 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
197e6 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 ){. int nByte
197e7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d ;. assert( pM
197e8 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c em->flags&MEM_Bl
197e9 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ob );. assert
197ea 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ( (pMem->flags&M
197eb 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
197ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
197ed 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
197ee 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
197ef 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
197f0 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 6e );.. /* Set n
197f1 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 Byte to the numb
197f2 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 er of bytes requ
197f3 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ired to store th
197f4 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 2e e expanded blob.
197f5 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 */. nByte =
197f6 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e pMem->n + pMem->
197f7 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66 28 u.nZero;. if(
197f8 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20 20 nByte<=0 ){.
197f9 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 20 nByte = 1;.
197fa 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
197fb 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
197fc 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 Mem, nByte, 1) )
197fd 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
197fe 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
197ff 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 }.. memset(&
19800 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d pMem->z[pMem->n]
19801 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 , 0, pMem->u.nZe
19802 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e ro);. pMem->n
19803 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 += pMem->u.nZer
19804 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 o;. pMem->fla
19805 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f gs &= ~(MEM_Zero
19806 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a |MEM_Term);. }.
19807 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19808 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f OK;.}.#endif.../
19809 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
1980a 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 he given Mem is
1980b 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 \u0000 terminate
1980c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1980d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1980e 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
1980f 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ate(Mem *pMem){.
19810 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
19811 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
19812 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
19813 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
19814 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 if( (pMem->fla
19815 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d gs & MEM_Term)!=
19816 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 0 || (pMem->flag
19817 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 s & MEM_Str)==0
19818 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
19819 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f LITE_OK; /* No
1981a 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 thing to do */.
1981b 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1981c 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
1981d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 , pMem->n+2, 1)
1981e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1981f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
19820 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
19821 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e n] = 0;. pMem->
19822 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 z[pMem->n+1] = 0
19823 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
19824 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 |= MEM_Term;. r
19825 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
19826 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 .}../*.** Add ME
19827 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74 M_Str to the set
19828 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 of representati
19829 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65 ons for the give
1982a 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a n Mem. Numbers.
1982b 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 ** are converted
1982c 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73 using sqlite3_s
1982d 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76 nprintf(). Conv
1982e 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f erting a BLOB to
1982f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 a string.** is
19830 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 a no-op..**.** E
19831 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e xisting represen
19832 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 tations MEM_Int
19833 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65 and MEM_Real are
19834 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74 *not* invalidat
19835 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f ed..**.** A MEM_
19836 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20 Null value will
19837 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20 never be passed
19838 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
19839 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
1983a 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63 is.** used for c
1983b 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73 onverting values
1983c 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74 to text for ret
1983d 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73 urning to the us
1983e 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 er (i.e. via.**
1983f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
19840 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e xt()), or for en
19841 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75 suring that valu
19842 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 es to be used as
19843 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61 btree.** keys a
19844 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74 re strings. In t
19845 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61 he former case a
19846 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
19847 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a returned the.**
19848 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61 user and the la
19849 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e ter is an intern
1984a 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65 al programming e
1984b 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rror..*/.SQLITE_
1984c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1984d 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
1984e 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 ify(Mem *pMem, i
1984f 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72 nt enc){. int r
19850 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
19851 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e int fg = pMem->
19852 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69 flags;. const i
19853 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a nt nByte = 32;..
19854 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
19855 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
19856 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
19857 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
19858 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d assert( !(fg&M
19859 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73 EM_Zero) );. as
1985a 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f sert( !(fg&(MEM_
1985b 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 Str|MEM_Blob)) )
1985c 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28 ;. assert( fg&(
1985d 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
1985e 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
1985f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
19860 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19861 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
19862 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 TE_ALIGNMENT(pMe
19863 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 20 73 71 m) );... if( sq
19864 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
19865 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 (pMem, nByte, 0)
19866 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
19867 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
19868 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 .. /* For a Rea
19869 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73 l or Integer, us
1986a 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 e sqlite3_mprint
1986b 66 28 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74 f() to produce t
1986c 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 he UTF-8. ** st
1986d 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 ring representat
1986e 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ion of the value
1986f 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 . Then, if the r
19870 65 71 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 equired encoding
19871 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c . ** is UTF-16l
19872 65 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f e or UTF-16be do
19873 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a a translation..
19874 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d ** . ** FIX M
19875 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62 E: It would be b
19876 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65 33 etter if sqlite3
19877 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c _snprintf() coul
19878 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a d do UTF-16.. *
19879 2f 0a 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d /. if( fg & MEM
1987a 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 _Int ){. sqli
1987b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 te3_snprintf(nBy
1987c 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c te, pMem->z, "%l
1987d 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b ld", pMem->u.i);
1987e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1987f 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 sert( fg & MEM_R
19880 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 eal );. sqlit
19881 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 e3_snprintf(nByt
19882 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e e, pMem->z, "%!.
19883 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 15g", pMem->r);.
19884 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
19885 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
19886 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70 4d 65 6d pMem->z);. pMem
19887 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 ->enc = SQLITE_U
19888 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 TF8;. pMem->fla
19889 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 gs |= MEM_Str|ME
1988a 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c 69 74 65 M_Term;. sqlite
1988b 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1988c 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 29 3b 0a ing(pMem, enc);.
1988d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1988e 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /*.** Memory cel
1988f 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 l pMem contains
19890 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 the context of a
19891 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
19892 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f tion..** This ro
19893 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20 utine calls the
19894 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 6f 64 20 finalize method
19895 66 6f 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f for that functio
19896 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c n. The.** resul
19897 74 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 t of the aggrega
19898 74 65 20 69 73 20 73 74 6f 72 65 64 20 62 61 63 te is stored bac
19899 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a 2a 2a 0a k into pMem..**.
1989a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
1989b 5f 45 52 52 4f 52 20 69 66 20 74 68 65 20 66 69 _ERROR if the fi
1989c 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72 74 73 20 nalizer reports
1989d 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 an error. SQLIT
1989e 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72 77 69 73 E_OK.** otherwis
1989f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
198a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
198a1 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
198a2 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75 6e 63 44 Mem *pMem, FuncD
198a3 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20 20 69 6e ef *pFunc){. in
198a4 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
198a5 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 ;. if( ALWAYS(p
198a6 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 Func && pFunc->x
198a7 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a 20 20 20 Finalize) ){.
198a8 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
198a9 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 ctx;. assert
198aa 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
198ab 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c MEM_Null)!=0 ||
198ac 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e pFunc==pMem->u.
198ad 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65 pDef );. asse
198ae 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
198af 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
198b0 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
198b1 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 mutex) );. me
198b2 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 mset(&ctx, 0, si
198b3 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20 zeof(ctx));.
198b4 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
198b5 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e M_Null;. ctx.
198b6 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b s.db = pMem->db;
198b7 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 . ctx.pMem =
198b8 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 pMem;. ctx.pF
198b9 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 unc = pFunc;.
198ba 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a pFunc->xFinaliz
198bb 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73 e(&ctx);. ass
198bc 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 ert( 0==(pMem->f
198bd 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 lags&MEM_Dyn) &&
198be 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a !pMem->xDel );.
198bf 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
198c0 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
198c1 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 ->zMalloc);.
198c2 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 memcpy(pMem, &ct
198c3 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e x.s, sizeof(ctx.
198c4 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 s));. rc = ct
198c5 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 x.isError;. }.
198c6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
198c7 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f *.** If the memo
198c8 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 ry cell contains
198c9 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 a string value
198ca 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65 that must be fre
198cb 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e ed by.** invokin
198cc 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 g an external ca
198cd 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20 llback, free it
198ce 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 now. Calling thi
198cf 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f s function.** do
198d0 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 es not free any
198d1 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 Mem.zMalloc buff
198d2 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
198d3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
198d4 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
198d5 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 External(Mem *p)
198d6 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 {. assert( p->d
198d7 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
198d8 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
198d9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 ->mutex) );. te
198da 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 stcase( p->flags
198db 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b 0a 20 20 & MEM_Agg );.
198dc 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 testcase( p->fla
198dd 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a gs & MEM_Dyn );.
198de 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 testcase( p->f
198df 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
198e0 74 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 t );. testcase(
198e1 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
198e2 46 72 61 6d 65 20 29 3b 0a 20 20 69 66 28 20 70 Frame );. if( p
198e3 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 ->flags&(MEM_Agg
198e4 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77 |MEM_Dyn|MEM_Row
198e5 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65 29 20 29 Set|MEM_Frame) )
198e6 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 {. if( p->fla
198e7 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20 gs&MEM_Agg ){.
198e8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
198e9 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d emFinalize(p, p-
198ea 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20 20 >u.pDef);.
198eb 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 assert( (p->flag
198ec 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 s & MEM_Agg)==0
198ed 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
198ee 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
198ef 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
198f0 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 p->flags&MEM_Dy
198f1 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a n && p->xDel ){.
198f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
198f3 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
198f4 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 et)==0 );.
198f5 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 p->xDel((void *)
198f6 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e p->z);. p->
198f7 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65 xDel = 0;. }e
198f8 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 lse if( p->flags
198f9 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 &MEM_RowSet ){.
198fa 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 sqlite3RowS
198fb 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f etClear(p->u.pRo
198fc 77 53 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 wSet);. }else
198fd 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 if( p->flags&ME
198fe 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 M_Frame ){.
198ff 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19900 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20 20 20 7d etNull(p);. }
19901 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
19902 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 lease any memory
19903 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d held by the Mem
19904 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65 . This may leave
19905 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a the Mem in an.*
19906 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 * inconsistent s
19907 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c tate, for exampl
19908 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 e with (Mem.z==0
19909 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 ) and.** (Mem.ty
1990a 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 pe==SQLITE_TEXT)
1990b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1990c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1990d 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d VdbeMemRelease(M
1990e 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 em *p){. sqlite
1990f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
19910 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 xternal(p);. sq
19911 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
19912 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a b, p->zMalloc);.
19913 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d p->z = 0;. p-
19914 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 >zMalloc = 0;.
19915 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a p->xDel = 0;.}..
19916 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
19917 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62 64-bit IEEE doub
19918 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 le into a 64-bit
19919 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e signed integer.
1991a 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c .** If the doubl
1991b 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 e is too large,
1991c 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 30 return 0x8000000
1991d 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 000000000..**.**
1991e 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70 Most systems ap
1991f 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20 pear to do this
19920 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e simply by assign
19921 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 ing.** variables
19922 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65 and without the
19923 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 73 extra range tes
19924 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 ts. But.** ther
19925 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 68 e are reports th
19926 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 at windows throw
19927 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a s an expection.*
19928 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e * if the floatin
19929 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 g point value is
1992a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 out of range. (
1992b 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 30 See ticket #2880
1992c 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65 .).** Because we
1992d 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 do not complete
1992e 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 ly understand th
1992f 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 e problem, we wi
19930 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 ll.** take the c
19931 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 72 onservative appr
19932 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20 oach and always
19933 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a do range tests.*
19934 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 * before attempt
19935 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69 ing the conversi
19936 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 on..*/.static i6
19937 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 4 doubleToInt64(
19938 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a double r){. /*.
19939 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c ** Many compil
1993a 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 ers we encounter
1993b 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 do not define c
1993c 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 onstants for the
1993d 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e . ** minimum an
1993e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 d maximum 64-bit
1993f 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68 integers, or th
19940 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 ey define them.
19941 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 ** inconsistent
19942 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f ly. And many do
19943 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 not understand
19944 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f the "LL" notatio
19945 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 n.. ** So we de
19946 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 fine our own sta
19947 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 tic constants he
19948 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 re using nothing
19949 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 . ** larger tha
1994a 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 n a 32-bit integ
1994b 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a er constant.. *
1994c 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 /. static const
1994d 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 i64 maxInt = LA
1994e 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 RGEST_INT64;. s
1994f 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 tatic const i64
19950 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 minInt = SMALLES
19951 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 T_INT64;.. if(
19952 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 r<(double)minInt
19953 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
19954 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 inInt;. }else i
19955 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 f( r>(double)max
19956 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 Int ){. /* mi
19957 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20 nInt is correct
19958 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e here - not maxIn
19959 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74 t. It turns out
1995a 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a that assigning.
1995b 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 ** a very la
1995c 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d rge positive num
1995d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 ber to an intege
1995e 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76 r results in a v
1995f 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a ery large. **
19960 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 negative intege
19961 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e r. This makes n
19962 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20 o sense, but it
19963 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72 64 is what x86 hard
19964 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 ware. ** does
19965 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 so for compatib
19966 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f ility we will do
19967 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 the same in sof
19968 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 tware. */. re
19969 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d turn minInt;. }
1996a 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
1996b 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a (i64)r;. }.}..
1996c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d /*.** Return som
1996d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 e kind of intege
1996e 72 20 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 r value which is
1996f 20 74 68 65 20 62 65 73 74 20 77 65 20 63 61 6e the best we can
19970 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 do.** at repres
19971 65 6e 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 enting the value
19972 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 that *pMem desc
19973 72 69 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 ribes as an inte
19974 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 ger..** If pMem
19975 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 is an integer, t
19976 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
19977 20 65 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d exact. If pMem
19978 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e is.** a floatin
19979 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 g-point then the
1997a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
1997b 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 is the integer p
1997c 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 art..** If pMem
1997d 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 is a string or b
1997e 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b lob, then we mak
1997f 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 e an attempt to
19980 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e convert.** it in
19981 74 6f 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 to a integer and
19982 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 return that. I
19983 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 f pMem represent
19984 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e s an.** an SQL-N
19985 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72 ULL value, retur
19986 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d n 0..**.** If pM
19987 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 em represents a
19988 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 string value, it
19989 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 s encoding might
1998a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a be changed..*/.
1998b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1998c 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 64 sqlite3VdbeIn
1998d 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d tValue(Mem *pMem
1998e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a ){. int flags;.
1998f 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
19990 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
19991 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
19992 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
19993 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
19994 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
19995 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 Mem) );. flags
19996 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 = pMem->flags;.
19997 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
19998 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 _Int ){. retu
19999 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 rn pMem->u.i;.
1999a 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 }else if( flags
1999b 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
1999c 20 20 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 return doubleT
1999d 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b oInt64(pMem->r);
1999e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 . }else if( fla
1999f 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 gs & (MEM_Str|ME
199a0 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 M_Blob) ){. i
199a1 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 64 value;. pM
199a2 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
199a3 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 _Str;. if( sq
199a4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
199a5 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 ncoding(pMem, SQ
199a6 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 LITE_UTF8).
199a7 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 || sqlite3Vdbe
199a8 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
199a9 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 pMem) ){. r
199aa 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
199ab 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
199ac 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 >z );. sqlite
199ad 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 3Atoi64(pMem->z,
199ae 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 &value);. re
199af 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 turn value;. }e
199b0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
199b1 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
199b2 52 65 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 Return the best
199b3 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
199b4 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 f pMem that we c
199b5 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a an get into a.**
199b6 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 double. If pMe
199b7 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 m is already a d
199b8 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 ouble or an inte
199b9 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a ger, return its.
199ba 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 ** value. If it
199bb 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 is a string or
199bc 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e blob, try to con
199bd 76 65 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 vert it to a dou
199be 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 ble..** If it is
199bf 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 a NULL, return
199c0 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 0.0..*/.SQLITE_P
199c1 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 RIVATE double sq
199c2 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
199c3 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ue(Mem *pMem){.
199c4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
199c5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
199c6 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
199c7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
199c8 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
199c9 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d YTE_ALIGNMENT(pM
199ca 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 em) );. if( pMe
199cb 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 m->flags & MEM_R
199cc 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 eal ){. retur
199cd 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c n pMem->r;. }el
199ce 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
199cf 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
199d0 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 return (doub
199d1 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 le)pMem->u.i;.
199d2 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e }else if( pMem->
199d3 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 flags & (MEM_Str
199d4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 |MEM_Blob) ){.
199d5 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 /* (double)0 I
199d6 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 n case of SQLITE
199d7 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
199d8 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 OINT... */. d
199d9 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 ouble val = (dou
199da 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d ble)0;. pMem-
199db 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
199dc 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 r;. if( sqlit
199dd 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
199de 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 ding(pMem, SQLIT
199df 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c E_UTF8). |
199e0 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d | sqlite3VdbeMem
199e1 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 NulTerminate(pMe
199e2 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 m) ){. /* (
199e3 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 double)0 In case
199e4 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f of SQLITE_OMIT_
199e5 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e FLOATING_POINT..
199e6 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 . */. retur
199e7 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 n (double)0;.
199e8 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
199e9 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 Mem->z );. sq
199ea 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e lite3AtoF(pMem->
199eb 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 z, &val);. re
199ec 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 turn val;. }els
199ed 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c e{. /* (doubl
199ee 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 e)0 In case of S
199ef 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
199f0 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
199f1 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 return (doub
199f2 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a le)0;. }.}../*.
199f3 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 ** The MEM struc
199f4 74 75 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 ture is already
199f5 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 a MEM_Real. Try
199f6 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 to also make it
199f7 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 a.** MEM_Int if
199f8 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 we can..*/.SQLI
199f9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
199fa 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 sqlite3VdbeInteg
199fb 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a erAffinity(Mem *
199fc 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 pMem){. assert(
199fd 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
199fe 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 EM_Real );. ass
199ff 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
19a00 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
19a01 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
19a02 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
19a03 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
19a04 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
19a05 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
19a06 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
19a07 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 MENT(pMem) );..
19a08 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 pMem->u.i = dou
19a09 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d bleToInt64(pMem-
19a0a 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 >r);.. /* Only
19a0b 6d 61 72 6b 20 74 68 65 20 76 61 6c 75 65 20 61 mark the value a
19a0c 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 0a s an integer if.
19a0d 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 28 31 29 **. ** (1)
19a0e 20 74 68 65 20 72 6f 75 6e 64 2d 74 72 69 70 20 the round-trip
19a0f 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 61 6c 2d conversion real-
19a10 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73 20 61 20 >int->real is a
19a11 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20 2a 2a 20 no-op, and. **
19a12 20 20 20 28 32 29 20 54 68 65 20 69 6e 74 65 67 (2) The integ
19a13 65 72 20 69 73 20 6e 65 69 74 68 65 72 20 74 68 er is neither th
19a14 65 20 6c 61 72 67 65 73 74 20 6e 6f 72 20 74 68 e largest nor th
19a15 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20 2a 2a 20 e smallest. **
19a16 20 20 20 20 20 20 20 70 6f 73 73 69 62 6c 65 20 possible
19a17 69 6e 74 65 67 65 72 20 28 74 69 63 6b 65 74 20 integer (ticket
19a18 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a #3922). **. **
19a19 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 The second and
19a1a 74 68 69 72 64 20 74 65 72 6d 73 20 69 6e 20 74 third terms in t
19a1b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
19a1c 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f 72 63 65 ditional enforce
19a1d 73 0a 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e s. ** the secon
19a1e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75 6e 64 65 d condition unde
19a1f 72 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e r the assumption
19a20 20 74 68 61 74 20 61 64 64 69 74 69 6f 6e 20 6f that addition o
19a21 76 65 72 66 6c 6f 77 20 63 61 75 73 65 73 0a 20 verflow causes.
19a22 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f 20 77 72 ** values to wr
19a23 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f 6e 20 78 ap around. On x
19a24 38 36 20 68 61 72 64 77 61 72 65 2c 20 74 68 65 86 hardware, the
19a25 20 74 68 69 72 64 20 74 65 72 6d 20 69 73 20 61 third term is a
19a26 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72 75 65 20 lways. ** true
19a27 61 6e 64 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69 and could be omi
19a28 74 74 65 64 2e 20 20 42 75 74 20 77 65 20 6c 65 tted. But we le
19a29 61 76 65 20 69 74 20 69 6e 20 62 65 63 61 75 73 ave it in becaus
19a2a 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 61 72 63 e other. ** arc
19a2b 68 69 74 65 63 74 75 72 65 73 20 6d 69 67 68 74 hitectures might
19a2c 20 62 65 68 61 76 65 20 64 69 66 66 65 72 65 6e behave differen
19a2d 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 tly.. */. if(
19a2e 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 pMem->r==(double
19a2f 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 20 70 4d )pMem->u.i && pM
19a30 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c 45 53 54 em->u.i>SMALLEST
19a31 5f 49 4e 54 36 34 0a 20 20 20 20 20 20 26 26 20 _INT64. &&
19a32 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e 75 2e 69 ALWAYS(pMem->u.i
19a33 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20 <LARGEST_INT64)
19a34 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19a35 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 gs |= MEM_Int;.
19a36 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 }.}../*.** Conv
19a37 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 ert pMem to type
19a38 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c integer. Inval
19a39 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
19a3a 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
19a3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19a3c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19a3d 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
19a3e 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
19a3f 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
19a40 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
19a41 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
19a42 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
19a43 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19a44 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
19a45 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
19a46 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
19a47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a NMENT(pMem) );..
19a48 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 pMem->u.i = sq
19a49 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
19a4a 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 e(pMem);. MemSe
19a4b 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
19a4c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75 MEM_Int);. retu
19a4d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
19a4e 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 ./*.** Convert p
19a4f 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69 Mem so that it i
19a50 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65 s of type MEM_Re
19a51 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 al..** Invalidat
19a52 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
19a53 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a esentations..*/.
19a54 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19a55 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
19a56 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d mRealify(Mem *pM
19a57 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
19a58 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19a59 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19a5a 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
19a5b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ) );. assert( E
19a5c 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
19a5d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 ENT(pMem) );..
19a5e 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 pMem->r = sqlite
19a5f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
19a60 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 Mem);. MemSetTy
19a61 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d peFlag(pMem, MEM
19a62 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e _Real);. return
19a63 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
19a64 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 *.** Convert pMe
19a65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73 m so that it has
19a66 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20 types MEM_Real
19a67 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f or MEM_Int or bo
19a68 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 th..** Invalidat
19a69 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
19a6a 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a esentations..*/.
19a6b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19a6c 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
19a6d 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70 mNumerify(Mem *p
19a6e 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 Mem){. double r
19a6f 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a 1, r2;. i64 i;.
19a70 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19a71 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e >flags & (MEM_In
19a72 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e t|MEM_Real|MEM_N
19a73 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 ull))==0 );. as
19a74 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19a75 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d gs & (MEM_Blob|M
19a76 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b 0a 20 EM_Str))!=0 );.
19a77 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19a78 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19a79 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19a7a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19a7b 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 r1 = sqlite3Vdb
19a7c 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29 eRealValue(pMem)
19a7d 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f ;. i = doubleTo
19a7e 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72 32 20 Int64(r1);. r2
19a7f 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69 = (double)i;. i
19a80 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20 f( r1==r2 ){.
19a81 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
19a82 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b ntegerify(pMem);
19a83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d . }else{. pM
19a84 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20 em->r = r1;.
19a85 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
19a86 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a Mem, MEM_Real);.
19a87 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
19a88 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
19a89 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 Delete any prev
19a8a 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 ious value and s
19a8b 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f et the value sto
19a8c 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 red in *pMem to
19a8d 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
19a8e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19a8f 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
19a90 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ll(Mem *pMem){.
19a91 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
19a92 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a & MEM_Frame ){.
19a93 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
19a94 72 61 6d 65 44 65 6c 65 74 65 28 70 4d 65 6d 2d rameDelete(pMem-
19a95 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20 20 7d 0a >u.pFrame);. }.
19a96 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
19a97 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 s & MEM_RowSet )
19a98 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 {. sqlite3Row
19a99 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 SetClear(pMem->u
19a9a 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a 20 .pRowSet);. }.
19a9b 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
19a9c 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b pMem, MEM_Null);
19a9d 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 . pMem->type =
19a9e 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a 0a SQLITE_NULL;.}..
19a9f 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
19aa0 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
19aa1 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
19aa2 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 6f e to be a BLOB o
19aa3 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63 6f f length.** n co
19aa4 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 ntaining all zer
19aa5 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 os..*/.SQLITE_PR
19aa6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19aa7 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
19aa8 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Blob(Mem *pMem,
19aa9 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 int n){. sqlite
19aaa 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
19aab 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 pMem);. pMem->f
19aac 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c lags = MEM_Blob|
19aad 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 6d MEM_Zero;. pMem
19aae 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19aaf 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e 20 BLOB;. pMem->n
19ab0 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 = 0;. if( n<0 )
19ab1 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e n = 0;. pMem->
19ab2 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20 70 u.nZero = n;. p
19ab3 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
19ab4 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64 65 66 20 E_UTF8;..#ifdef
19ab5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
19ab6 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65 33 56 64 BLOB. sqlite3Vd
19ab7 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
19ab8 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 4d 65 n, 0);. if( pMe
19ab9 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 4d 65 6d m->z ){. pMem
19aba 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 6d 65 6d ->n = n;. mem
19abb 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 set(pMem->z, 0,
19abc 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d n);. }.#endif.}
19abd 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
19abe 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
19abf 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 e and set the va
19ac0 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 lue stored in *p
19ac1 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d Mem to val,.** m
19ac2 61 6e 69 66 65 73 74 20 74 79 70 65 20 49 4e 54 anifest type INT
19ac3 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f EGER..*/.SQLITE_
19ac4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19ac5 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e ite3VdbeMemSetIn
19ac6 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 t64(Mem *pMem, i
19ac7 36 34 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74 64 val){. sqlit
19ac8 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19ac9 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e (pMem);. pMem->
19aca 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 u.i = val;. pMe
19acb 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
19acc 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 nt;. pMem->type
19acd 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
19ace 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 R;.}../*.** Dele
19acf 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 te any previous
19ad0 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 value and set th
19ad1 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
19ad2 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a n *pMem to val,.
19ad3 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 ** manifest type
19ad4 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 REAL..*/.SQLITE
19ad5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19ad6 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
19ad7 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c ouble(Mem *pMem,
19ad8 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 double val){.
19ad9 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e if( sqlite3IsNaN
19ada 28 76 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c (val) ){. sql
19adb 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
19adc 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 ll(pMem);. }els
19add 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 e{. sqlite3Vd
19ade 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
19adf 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 m);. pMem->r
19ae0 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d = val;. pMem-
19ae1 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 >flags = MEM_Rea
19ae2 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 l;. pMem->typ
19ae3 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 e = SQLITE_FLOAT
19ae4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
19ae5 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f elete any previo
19ae6 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 us value and set
19ae7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d the value of pM
19ae8 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 em to be an.** e
19ae9 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 mpty boolean ind
19aea 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ex..*/.SQLITE_PR
19aeb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19aec 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
19aed 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 et(Mem *pMem){.
19aee 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
19aef 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 Mem->db;. asser
19af0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 t( db!=0 );. as
19af1 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19af2 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
19af3 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ==0 );. sqlite3
19af4 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19af5 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d Mem);. pMem->zM
19af6 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 alloc = sqlite3D
19af7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 36 bMallocRaw(db, 6
19af8 34 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 4);. if( db->ma
19af9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
19afa 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19afb 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 MEM_Null;. }els
19afc 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
19afd 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a Mem->zMalloc );.
19afe 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 pMem->u.pRow
19aff 53 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 Set = sqlite3Row
19b00 53 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d SetInit(db, pMem
19b01 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 ->zMalloc, .
19b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19b03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19b04 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c sqlite3DbMall
19b05 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d ocSize(db, pMem-
19b06 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 >zMalloc));.
19b07 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e assert( pMem->u.
19b08 70 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 pRowSet!=0 );.
19b09 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19b0a 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a MEM_RowSet;. }.
19b0b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19b0c 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20 true if the Mem
19b0d 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 object contains
19b0e 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 a TEXT or BLOB t
19b0f 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 hat is.** too la
19b10 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 rge - whose size
19b11 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f exceeds SQLITE_
19b12 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 MAX_LENGTH..*/.S
19b13 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19b14 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
19b15 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a TooBig(Mem *p){.
19b16 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 assert( p->db!
19b17 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 =0 );. if( p->f
19b18 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c lags & (MEM_Str|
19b19 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
19b1a 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 int n = p->n;.
19b1b 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 if( p->flags
19b1c 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
19b1d 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a n += p->u.nZ
19b1e 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 ero;. }. r
19b1f 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 eturn n>p->db->a
19b20 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
19b21 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a IT_LENGTH];. }.
19b22 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a return 0; .}..
19b23 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 /*.** Size of st
19b24 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 ruct Mem not inc
19b25 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a luding the Mem.z
19b26 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a Malloc member..*
19b27 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c /.#define MEMCEL
19b28 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 LSIZE (size_t)(&
19b29 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 (((Mem *)0)->zMa
19b2a 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 lloc))../*.** Ma
19b2b 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f ke an shallow co
19b2c 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f py of pFrom into
19b2d 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e pTo. Prior con
19b2e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 tents of.** pTo
19b2f 61 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20 are freed. The
19b30 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 pFrom->z field i
19b31 73 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 s not duplicated
19b32 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e . If.** pFrom->
19b33 7a 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 z is used, then
19b34 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f pTo->z points to
19b35 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 the same thing
19b36 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 as pFrom->z.** a
19b37 6e 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72 nd flags gets sr
19b38 63 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45 cType (either ME
19b39 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 M_Ephem or MEM_S
19b3a 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 tatic)..*/.SQLIT
19b3b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19b3c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
19b3d 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 llowCopy(Mem *pT
19b3e 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 o, const Mem *pF
19b3f 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65 rom, int srcType
19b40 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 ){. assert( (pF
19b41 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d rom->flags & MEM
19b42 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19b43 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19b44 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
19b45 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
19b46 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c o, pFrom, MEMCEL
19b47 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 LSIZE);. pTo->x
19b48 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 Del = 0;. if( (
19b49 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pFrom->flags&MEM
19b4a 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f _Dyn)!=0 || pFro
19b4b 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 m->z==pFrom->zMa
19b4c 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d lloc ){. pTo-
19b4d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
19b4e 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d Dyn|MEM_Static|M
19b4f 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 EM_Ephem);. a
19b50 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d ssert( srcType==
19b51 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 MEM_Ephem || src
19b52 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 Type==MEM_Static
19b53 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 );. pTo->fla
19b54 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 gs |= srcType;.
19b55 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
19b56 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 a full copy of
19b57 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 pFrom into pTo.
19b58 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 Prior contents
19b59 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 of pTo are.** fr
19b5a 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 eed before the c
19b5b 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a opy is made..*/.
19b5c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19b5d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
19b5e 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 mCopy(Mem *pTo,
19b5f 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d const Mem *pFrom
19b60 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
19b61 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
19b62 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 rt( (pFrom->flag
19b63 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
19b64 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 =0 );. sqlite3V
19b65 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
19b66 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 ernal(pTo);. me
19b67 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c mcpy(pTo, pFrom,
19b68 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 MEMCELLSIZE);.
19b69 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pTo->flags &= ~
19b6a 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 MEM_Dyn;.. if(
19b6b 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f pTo->flags&(MEM_
19b6c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
19b6d 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 . if( 0==(pFr
19b6e 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 om->flags&MEM_St
19b6f 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70 atic) ){. p
19b70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d To->flags |= MEM
19b71 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 _Ephem;. rc
19b72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
19b73 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 mMakeWriteable(p
19b74 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a To);. }. }..
19b75 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19b76 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 /*.** Transfer t
19b77 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 he contents of p
19b78 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 From to pTo. Any
19b79 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20 existing value
19b7a 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 in pTo is.** fre
19b7b 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e ed. If pFrom con
19b7c 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 tains ephemeral
19b7d 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20 data, a copy is
19b7e 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f made..**.** pFro
19b7f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 m contains an SQ
19b80 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 L NULL when this
19b81 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
19b82 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19b83 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19b84 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 VdbeMemMove(Mem
19b85 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d *pTo, Mem *pFrom
19b86 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 ){. assert( pFr
19b87 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c om->db==0 || sql
19b88 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19b89 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 pFrom->db->mutex
19b8a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
19b8b 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c To->db==0 || sql
19b8c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19b8d 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 pTo->db->mutex)
19b8e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 );. assert( pFr
19b8f 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f om->db==0 || pTo
19b90 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d ->db==0 || pFrom
19b91 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b ->db==pTo->db );
19b92 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d .. sqlite3VdbeM
19b93 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a emRelease(pTo);.
19b94 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
19b95 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 rom, sizeof(Mem)
19b96 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 );. pFrom->flag
19b97 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19b98 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pFrom->xDel = 0;
19b99 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f . pFrom->zMallo
19b9a 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 c = 0;.}../*.**
19b9b 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
19b9c 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 of a Mem to be
19b9d 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c a string or a BL
19b9e 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 OB..**.** The me
19b9f 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 mory management
19ba0 73 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73 strategy depends
19ba1 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 on the value of
19ba2 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 the xDel.** par
19ba3 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76 ameter. If the v
19ba4 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53 alue passed is S
19ba5 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c QLITE_TRANSIENT,
19ba6 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 then the .** st
19ba7 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 ring is copied i
19ba8 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 nto a (possibly
19ba9 65 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 existing) buffer
19baa 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 managed by the
19bab 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 .** Mem structur
19bac 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e e. Otherwise, an
19bad 79 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 y existing buffe
19bae 72 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 r is freed and t
19baf 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f he.** pointer co
19bb0 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 pied..**.** If t
19bb1 68 65 20 73 74 72 69 6e 67 20 69 73 20 74 6f 6f he string is too
19bb2 20 6c 61 72 67 65 20 28 69 66 20 69 74 20 65 78 large (if it ex
19bb3 63 65 65 64 73 20 74 68 65 20 53 51 4c 49 54 45 ceeds the SQLITE
19bb4 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a 2a 2a _LIMIT_LENGTH.**
19bb5 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74 68 65 size limit) the
19bb6 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f n no memory allo
19bb7 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 cation occurs.
19bb8 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 61 If the string ca
19bb9 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64 20 77 n be.** stored w
19bba 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74 69 6e ithout allocatin
19bbb 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e 20 69 g memory, then i
19bbc 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f t is. If a memo
19bbd 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ry allocation.**
19bbe 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 is required to
19bbf 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 store the string
19bc0 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f 66 20 , then value of
19bc1 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e 67 65 pMem is unchange
19bc2 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68 65 72 d. In.** either
19bc3 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f 54 4f case, SQLITE_TO
19bc4 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e 65 64 OBIG is returned
19bc5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19bc6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19bc7 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20 dbeMemSetStr(.
19bc8 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20 Mem *pMem,
19bc9 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
19bca 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72 ll to set to str
19bcb 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 ing value */. c
19bcc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 onst char *z,
19bcd 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 /* String poi
19bce 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c nter */. int n,
19bcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19bd0 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 Bytes in string
19bd1 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f , or negative */
19bd2 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
19bd3 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 /* Encodi
19bd4 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 ng of z. 0 for
19bd5 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 BLOBs */. void
19bd6 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f (*xDel)(void*) /
19bd7 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e * Destructor fun
19bd8 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e ction */.){. in
19bd9 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 t nByte = n;
19bda 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 /* New value f
19bdb 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20 or pMem->n */.
19bdc 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20 int iLimit;
19bdd 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
19bde 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72 llowed string or
19bdf 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20 blob size */.
19be0 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20 u16 flags = 0;
19be1 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 /* New value
19be2 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 for pMem->flags
19be3 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
19be4 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19be5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19be6 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
19be7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
19be8 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
19be9 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
19bea 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 . /* If z is a
19beb 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 NULL pointer, se
19bec 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 t pMem to contai
19bed 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a n an SQL NULL. *
19bee 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 /. if( !z ){.
19bef 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19bf0 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 SetNull(pMem);.
19bf1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19bf2 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 _OK;. }.. if(
19bf3 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 pMem->db ){.
19bf4 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 iLimit = pMem->d
19bf5 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
19bf6 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
19bf7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 }else{. iLi
19bf8 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 mit = SQLITE_MAX
19bf9 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 _LENGTH;. }. f
19bfa 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d lags = (enc==0?M
19bfb 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 EM_Blob:MEM_Str)
19bfc 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 ;. if( nByte<0
19bfd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ){. assert( e
19bfe 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 nc!=0 );. if(
19bff 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 enc==SQLITE_UTF
19c00 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 8 ){. for(n
19c01 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 Byte=0; nByte<=i
19c02 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 Limit && z[nByte
19c03 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 ]; nByte++){}.
19c04 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
19c05 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 or(nByte=0; nByt
19c06 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b e<=iLimit && (z[
19c07 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 nByte] | z[nByte
19c08 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b +1]); nByte+=2){
19c09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 }. }. flag
19c0a 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
19c0b 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c }.. /* The fol
19c0c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 lowing block set
19c0d 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 s the new values
19c0e 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 of Mem.z and Me
19c0f 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 m.xDel. It. **
19c10 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 also sets a flag
19c11 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
19c12 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e le "flags" to in
19c13 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 dicate the memor
19c14 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e y. ** managemen
19c15 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 t (one of MEM_Dy
19c16 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 n or MEM_Static)
19c17 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 .. */. if( xDe
19c18 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 l==SQLITE_TRANSI
19c19 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ENT ){. int n
19c1a 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 Alloc = nByte;.
19c1b 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d if( flags&MEM
19c1c 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e _Term ){. n
19c1d 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 Alloc += (enc==S
19c1e 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b QLITE_UTF8?1:2);
19c1f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
19c20 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 Byte>iLimit ){.
19c21 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
19c22 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d TE_TOOBIG;. }
19c23 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
19c24 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
19c25 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a , nAlloc, 0) ){.
19c26 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
19c27 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
19c28 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d . memcpy(pMem
19c29 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b ->z, z, nAlloc);
19c2a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 . }else if( xDe
19c2b 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 l==SQLITE_DYNAMI
19c2c 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 C ){. sqlite3
19c2d 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19c2e 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e Mem);. pMem->
19c2f 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e zMalloc = pMem->
19c30 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 z = (char *)z;.
19c31 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 pMem->xDel =
19c32 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
19c33 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19c34 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
19c35 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 pMem->z = (char
19c36 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e *)z;. pMem->
19c37 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 xDel = xDel;.
19c38 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c flags |= ((xDel
19c39 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 ==SQLITE_STATIC)
19c3a 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f ?MEM_Static:MEM_
19c3b 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 Dyn);. }.. pMe
19c3c 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20 m->n = nByte;.
19c3d 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 6c pMem->flags = fl
19c3e 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 ags;. pMem->enc
19c3f 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c = (enc==0 ? SQL
19c40 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29 3b ITE_UTF8 : enc);
19c41 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 . pMem->type =
19c42 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54 45 (enc==0 ? SQLITE
19c43 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f 54 _BLOB : SQLITE_T
19c44 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 EXT);..#ifndef S
19c45 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
19c46 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 . if( pMem->enc
19c47 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 !=SQLITE_UTF8 &&
19c48 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
19c49 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 29 andleBom(pMem) )
19c4a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19c4b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 ITE_NOMEM;. }.#
19c4c 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 42 79 endif.. if( nBy
19c4d 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 te>iLimit ){.
19c4e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54 return SQLITE_T
19c4f 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 72 65 OOBIG;. }.. re
19c50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
19c51 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
19c52 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 the values cont
19c53 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f ained by the two
19c54 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 memory cells, r
19c55 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 eturning.** nega
19c56 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f tive, zero or po
19c57 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 sitive if pMem1
19c58 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
19c59 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
19c5a 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 er.** than pMem2
19c5b 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 . Sorting order
19c5c 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c is NULL's first,
19c5d 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d followed by num
19c5e 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a bers (integers.*
19c5f 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 * and reals) sor
19c60 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c ted numerically,
19c61 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 followed by tex
19c62 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 t ordered by the
19c63 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 collating.** se
19c64 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 quence pColl and
19c65 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 finally blob's
19c66 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d ordered by memcm
19c67 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e p()..**.** Two N
19c68 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 ULL values are c
19c69 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 onsidered equal
19c6a 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
19c6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19c6c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
19c6d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 emCompare(const
19c6e 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 Mem *pMem1, cons
19c6f 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f t Mem *pMem2, co
19c70 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f nst CollSeq *pCo
19c71 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ll){. int rc;.
19c72 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 int f1, f2;. i
19c73 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 nt combined_flag
19c74 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 s;.. f1 = pMem1
19c75 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 ->flags;. f2 =
19c76 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem2->flags;.
19c77 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d combined_flags =
19c78 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 f1|f2;. assert
19c79 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 ( (combined_flag
19c7a 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
19c7b 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 =0 );. . /* If
19c7c 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c one value is NUL
19c7d 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 L, it is less th
19c7e 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 an the other. If
19c7f 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a both values. *
19c80 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 * are NULL, retu
19c81 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rn 0.. */. if(
19c82 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 combined_flags&
19c83 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
19c84 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e return (f2&MEM_N
19c85 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e ull) - (f1&MEM_N
19c86 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ull);. }.. /*
19c87 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 If one value is
19c88 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 a number and the
19c89 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 other is not, t
19c8a 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 he number is les
19c8b 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 s.. ** If both
19c8c 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d are numbers, com
19c8d 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 pare as reals if
19c8e 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 one is a real,
19c8f 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 or as integers.
19c90 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 ** if both valu
19c91 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e es are integers.
19c92 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 . */. if( comb
19c93 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f ined_flags&(MEM_
19c94 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b Int|MEM_Real) ){
19c95 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d . if( !(f1&(M
19c96 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
19c97 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
19c98 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n 1;. }. i
19c99 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 f( !(f2&(MEM_Int
19c9a 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 |MEM_Real)) ){.
19c9b 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a return -1;.
19c9c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 }. if( (f
19c9d 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 1 & f2 & MEM_Int
19c9e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f )==0 ){. do
19c9f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 uble r1, r2;.
19ca0 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 if( (f1&MEM_R
19ca1 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 eal)==0 ){.
19ca2 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 r1 = (double)
19ca3 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 pMem1->u.i;.
19ca4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
19ca5 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a r1 = pMem1->r;.
19ca6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
19ca7 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d ( (f2&MEM_Real)=
19ca8 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 =0 ){. r2
19ca9 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 = (double)pMem2
19caa 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c ->u.i;. }el
19cab 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d se{. r2 =
19cac 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 pMem2->r;.
19cad 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c }. if( r1<
19cae 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a r2 ) return -1;.
19caf 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 if( r1>r2
19cb0 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
19cb1 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
19cb2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
19cb3 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 ert( f1&MEM_Int
19cb4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
19cb5 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 f2&MEM_Int );.
19cb6 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e if( pMem1->
19cb7 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 u.i < pMem2->u.i
19cb8 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 ) return -1;.
19cb9 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 if( pMem1->u
19cba 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 .i > pMem2->u.i
19cbb 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
19cbc 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
19cbd 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f }. }.. /* If o
19cbe 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 ne value is a st
19cbf 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 ring and the oth
19cc0 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 er is a blob, th
19cc1 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 e string is less
19cc2 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 .. ** If both a
19cc3 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 re strings, comp
19cc4 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f are using the co
19cc5 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
19cc6 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f s.. */. if( co
19cc7 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d mbined_flags&MEM
19cc8 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 _Str ){. if(
19cc9 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d (f1 & MEM_Str)==
19cca 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
19ccb 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n 1;. }. i
19ccc 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 f( (f2 & MEM_Str
19ccd 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )==0 ){. re
19cce 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a turn -1;. }..
19ccf 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
19cd0 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 1->enc==pMem2->e
19cd1 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 nc );. assert
19cd2 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 ( pMem1->enc==SQ
19cd3 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 LITE_UTF8 || .
19cd4 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d pMem1-
19cd5 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
19cd6 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 16LE || pMem1->e
19cd7 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
19cd8 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 BE );.. /* Th
19cd9 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
19cda 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 ence must be def
19cdb 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 ined at this poi
19cdc 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 nt, even if.
19cdd 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 ** the user dele
19cde 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f tes the collatio
19cdf 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 n sequence after
19ce0 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
19ce1 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 m is. ** comp
19ce2 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e iled (this was n
19ce3 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 ot always the ca
19ce4 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 se).. */.
19ce5 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c assert( !pColl |
19ce6 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b | pColl->xCmp );
19ce7 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 .. if( pColl
19ce8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 ){. if( pMe
19ce9 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e m1->enc==pColl->
19cea 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f enc ){. /
19ceb 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 * The strings ar
19cec 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 e already in the
19ced 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e correct encodin
19cee 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 g. Call the.
19cef 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 ** comparis
19cf0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 on function dire
19cf1 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 ctly */.
19cf2 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 return pColl->xC
19cf3 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c mp(pColl->pUser,
19cf4 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e pMem1->n,pMem1->
19cf5 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 z,pMem2->n,pMem2
19cf6 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->z);. }els
19cf7 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 e{. const
19cf8 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a void *v1, *v2;.
19cf9 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 int n1,
19cfa 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 n2;. Mem
19cfb 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 c1;. Mem
19cfc 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 c2;. mems
19cfd 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f et(&c1, 0, sizeo
19cfe 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 f(c1));.
19cff 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 memset(&c2, 0, s
19d00 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 izeof(c2));.
19d01 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19d02 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 emShallowCopy(&c
19d03 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 1, pMem1, MEM_Ep
19d04 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 hem);. sq
19d05 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
19d06 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 lowCopy(&c2, pMe
19d07 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a m2, MEM_Ephem);.
19d08 20 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c v1 = sql
19d09 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 ite3ValueText((s
19d0a 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 qlite3_value*)&c
19d0b 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 1, pColl->enc);.
19d0c 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d n1 = v1=
19d0d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 =0 ? 0 : c1.n;.
19d0e 20 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 v2 = sqli
19d0f 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 te3ValueText((sq
19d10 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 lite3_value*)&c2
19d11 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 , pColl->enc);.
19d12 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d n2 = v2==
19d13 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 0 ? 0 : c2.n;.
19d14 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c rc = pColl
19d15 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 ->xCmp(pColl->pU
19d16 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c ser, n1, v1, n2,
19d17 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 v2);. sq
19d18 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19d19 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 ase(&c1);.
19d1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19d1b 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 Release(&c2);.
19d1c 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
19d1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19d1e 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 /* If a NULL
19d1f 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 pointer was pass
19d20 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 ed as the collat
19d21 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c e function, fall
19d22 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 through. **
19d23 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 to the blob case
19d24 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 and use memcmp(
19d25 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f ). */. }. . /
19d26 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 * Both values mu
19d27 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f st be blobs. Co
19d28 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 mpare using memc
19d29 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d mp(). */. rc =
19d2a 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a memcmp(pMem1->z
19d2b 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 , pMem2->z, (pMe
19d2c 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f m1->n>pMem2->n)?
19d2d 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e pMem2->n:pMem1->
19d2e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 n);. if( rc==0
19d2f 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d ){. rc = pMem
19d30 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 1->n - pMem2->n;
19d31 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
19d32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
19d33 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 data out of a bt
19d34 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 ree key or data
19d35 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 field and into a
19d36 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a Mem structure..
19d37 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b ** The data or k
19d38 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d ey is taken from
19d39 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
19d3a 70 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c pCur is currentl
19d3b 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f y pointing.** to
19d3c 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d . offset and am
19d3d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 t determine what
19d3e 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
19d3f 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 data or key to r
19d40 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 etrieve..** key
19d41 69 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 is true to get t
19d42 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 he key or false
19d43 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 to get data. Th
19d44 65 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 e result is writ
19d45 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 ten.** into the
19d46 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a pMem element..**
19d47 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 .** The pMem str
19d48 75 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 ucture is assume
19d49 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 d to be uninitia
19d4a 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f lized. Any prio
19d4b 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 r content.** is
19d4c 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 overwritten with
19d4d 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e out being freed.
19d4e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 .**.** If this r
19d4f 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 outine fails for
19d50 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c any reason (mal
19d51 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c loc returns NULL
19d52 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f or unable.** to
19d53 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
19d54 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d isk) then the pM
19d55 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e em is left in an
19d56 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 inconsistent st
19d57 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ate..*/.SQLITE_P
19d58 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19d59 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
19d5a 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a ee(. BtCursor *
19d5b 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f pCur, /* Curso
19d5c 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 r pointing at re
19d5d 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 cord to retrieve
19d5e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 . */. int offse
19d5f 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 t, /* Offs
19d60 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 et from the star
19d61 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 t of data to ret
19d62 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 urn bytes from.
19d63 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 */. int amt,
19d64 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
19d65 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 of bytes to ret
19d66 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 urn. */. int ke
19d67 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 y, /* I
19d68 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 f true, retrieve
19d69 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 from the btree
19d6a 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a key, not data. *
19d6b 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 /. Mem *pMem
19d6c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 /* OUT: Re
19d6d 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 turn data in thi
19d6e 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e s Mem structure.
19d6f 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a */.){. char *z
19d70 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 Data; /*
19d71 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 Data from the bt
19d72 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 ree layer */. i
19d73 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 nt available = 0
19d74 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 ; /* Number of
19d75 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 bytes available
19d76 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 on the local btr
19d77 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ee page */. int
19d78 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
19d79 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
19d7a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
19d7b 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
19d7c 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b IsValid(pCur) );
19d7d 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 .. /* Note: the
19d7e 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b calls to BtreeK
19d7f 65 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 eyFetch() and Da
19d80 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 taFetch() below
19d81 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 assert() . ** t
19d82 68 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 hat both the BtS
19d83 68 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 hared and databa
19d84 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 se handle mutexe
19d85 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 s are held. */.
19d86 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19d87 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19d88 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 et)==0 );. if(
19d89 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 key ){. zData
19d8a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
19d8b 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
19d8c 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 pCur, &available
19d8d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
19d8e 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 zData = (char *)
19d8f 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
19d90 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 Fetch(pCur, &ava
19d91 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 ilable);. }. a
19d92 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 ssert( zData!=0
19d93 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 );.. if( offset
19d94 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 +amt<=available
19d95 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 && (pMem->flags&
19d96 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 MEM_Dyn)==0 ){.
19d97 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19d98 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
19d99 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a pMem->z = &z
19d9a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 Data[offset];.
19d9b 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19d9c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 MEM_Blob|MEM_Eph
19d9d 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 em;. }else if(
19d9e 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
19d9f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19da0 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c row(pMem, amt+2,
19da1 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 0)) ){. pMem
19da2 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
19da3 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 ob|MEM_Dyn|MEM_T
19da4 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 erm;. pMem->e
19da5 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d nc = 0;. pMem
19da6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19da7 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 BLOB;. if( ke
19da8 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
19da9 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
19daa 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
19dab 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 t, pMem->z);.
19dac 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
19dad 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 = sqlite3BtreeD
19dae 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 ata(pCur, offset
19daf 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b , amt, pMem->z);
19db0 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
19db1 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 >z[amt] = 0;.
19db2 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 pMem->z[amt+1]
19db3 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 = 0;. if( rc!
19db4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19db5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19db6 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
19db7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 . }. }. pMe
19db8 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 m->n = amt;.. r
19db9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
19dba 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
19dbb 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
19dbc 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 internally, it i
19dbd 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 s not part of th
19dbe 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 e.** external AP
19dbf 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 I. It works in a
19dc0 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 similar way to
19dc1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
19dc2 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 xt(),.** except
19dc3 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 the data returne
19dc4 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f d is in the enco
19dc5 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 ding specified b
19dc6 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 y the second.**
19dc7 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 parameter, which
19dc8 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 must be one of
19dc9 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 SQLITE_UTF16BE,
19dca 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f SQLITE_UTF16LE o
19dcb 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 r.** SQLITE_UTF8
19dcc 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 ..**.** (2006-02
19dcd 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 -16:) The enc v
19dce 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 alue can be or-e
19dcf 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 d with SQLITE_UT
19dd0 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 F16_ALIGNED..**
19dd1 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 If that is the c
19dd2 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ase, then the re
19dd3 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 sult must be ali
19dd4 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 gned on an even
19dd5 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 byte.** boundary
19dd6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19dd7 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a ATE const void *
19dd8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
19dd9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
19dda 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 pVal, u8 enc){.
19ddb 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 if( !pVal ) ret
19ddc 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 urn 0;.. assert
19ddd 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c ( pVal->db==0 ||
19dde 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19ddf 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 eld(pVal->db->mu
19de0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19de1 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 ( (enc&3)==(enc&
19de2 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
19de3 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 IGNED) );. asse
19de4 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 rt( (pVal->flags
19de5 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19de6 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 0 );.. if( pVal
19de7 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
19de8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
19de9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
19dea 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d (MEM_Blob>>3) ==
19deb 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 MEM_Str );. pV
19dec 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 al->flags |= (pV
19ded 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f al->flags & MEM_
19dee 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 Blob)>>3;. expa
19def 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 ndBlob(pVal);.
19df0 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 if( pVal->flags&
19df1 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 MEM_Str ){. s
19df2 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
19df3 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 Encoding(pVal, e
19df4 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 nc & ~SQLITE_UTF
19df5 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 16_ALIGNED);.
19df6 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 if( (enc & SQLI
19df7 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19df8 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 )!=0 && 1==(1&SQ
19df9 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
19dfa 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 pVal->z)) ){.
19dfb 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c assert( (pVal
19dfc 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 ->flags & (MEM_E
19dfd 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
19dfe 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 )!=0 );. if
19dff 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
19e00 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 MakeWriteable(pV
19e01 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 al)!=SQLITE_OK )
19e02 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
19e03 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
19e04 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
19e05 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
19e06 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b (pVal);. }else{
19e07 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 . assert( (pV
19e08 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c al->flags&MEM_Bl
19e09 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 ob)==0 );. sq
19e0a 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
19e0b 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 ngify(pVal, enc)
19e0c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d ;. assert( 0=
19e0d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 =(1&SQLITE_PTR_T
19e0e 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 O_INT(pVal->z))
19e0f 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
19e10 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 pVal->enc==(enc
19e11 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f & ~SQLITE_UTF16_
19e12 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c ALIGNED) || pVal
19e13 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 ->db==0.
19e14 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 || pVal->d
19e15 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19e16 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 );. if( pVal->e
19e17 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 nc==(enc & ~SQLI
19e18 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19e19 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
19e1a 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 pVal->z;. }else
19e1b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
19e1c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 }.}../*.** Cre
19e1d 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 ate a new sqlite
19e1e 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 3_value object..
19e1f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19e20 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 E sqlite3_value
19e21 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 *sqlite3ValueNew
19e22 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
19e23 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 Mem *p = sqlite
19e24 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
19e25 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 , sizeof(*p));.
19e26 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d if( p ){. p-
19e27 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
19e28 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d l;. p->type =
19e29 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 SQLITE_NULL;.
19e2a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 p->db = db;.
19e2b 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
19e2c 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
19e2d 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 new sqlite3_valu
19e2e 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 e object, contai
19e2f 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f ning the value o
19e30 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 f pExpr..**.** T
19e31 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 his only works f
19e32 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 or very simple e
19e33 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 xpressions that
19e34 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 consist of one c
19e35 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e onstant.** token
19e36 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 (i.e. "5", "5.1
19e37 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 ", "'a string'")
19e38 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 . If the express
19e39 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f ion can.** be co
19e3a 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 nverted directly
19e3b 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 into a value, t
19e3c 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
19e3d 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a allocated and.*
19e3e 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 * a pointer writ
19e3f 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 ten to *ppVal. T
19e40 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 he caller is res
19e41 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 ponsible for dea
19e42 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 llocating.** the
19e43 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e value by passin
19e44 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 g it to sqlite3V
19e45 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 alueFree() later
19e46 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 on. If the expr
19e47 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 ession.** cannot
19e48 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f be converted to
19e49 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a a value, then *
19e4a 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 ppVal is set to
19e4b 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
19e4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19e4d 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
19e4e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
19e4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19e50 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
19e51 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 nnection */. Ex
19e52 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 pr *pExpr,
19e53 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
19e54 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 xpression to eva
19e55 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e luate */. u8 en
19e56 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
19e57 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
19e58 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 to use */. u8
19e59 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 affinity,
19e5a 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 /* Affini
19e5b 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 ty to use */. s
19e5c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 qlite3_value **p
19e5d 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 pVal /* Writ
19e5e 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 e the new value
19e5f 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
19e60 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 op;. char *zVa
19e61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 l = 0;. sqlite3
19e62 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 _value *pVal = 0
19e63 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 ;.. if( !pExpr
19e64 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 ){. *ppVal =
19e65 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 0;. return SQ
19e66 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f LITE_OK;. }. o
19e67 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 p = pExpr->op;.
19e68 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 if( op==TK_REGI
19e69 53 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d STER ){. op =
19e6a 20 70 45 78 70 72 2d 3e 6f 70 32 3b 20 20 2f 2a pExpr->op2; /*
19e6b 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 This only happe
19e6c 6e 73 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45 ns with SQLITE_E
19e6d 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a 2f 0a 20 NABLE_STAT2 */.
19e6e 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b }.. if( op==TK
19e6f 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 _STRING || op==T
19e70 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 K_FLOAT || op==T
19e71 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 K_INTEGER ){.
19e72 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 pVal = sqlite3V
19e73 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 alueNew(db);.
19e74 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 if( pVal==0 ) g
19e75 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 oto no_mem;.
19e76 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
19e77 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e rty(pExpr, EP_In
19e78 74 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 tValue) ){.
19e79 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19e7a 65 74 49 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 etInt64(pVal, (i
19e7b 36 34 29 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 64)pExpr->u.iVal
19e7c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ue);. }else{.
19e7d 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c zVal = sql
19e7e 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
19e7f 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
19e80 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61 );. if( zVa
19e81 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d l==0 ) goto no_m
19e82 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 em;. sqlite
19e83 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 3ValueSetStr(pVa
19e84 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c l, -1, zVal, SQL
19e85 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
19e86 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 _DYNAMIC);.
19e87 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 if( op==TK_FLOA
19e88 54 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d T ) pVal->type =
19e89 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 SQLITE_FLOAT;.
19e8a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 }. if( (op
19e8b 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 ==TK_INTEGER ||
19e8c 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 op==TK_FLOAT ) &
19e8d 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 & affinity==SQLI
19e8e 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 TE_AFF_NONE ){.
19e8f 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 sqlite3Valu
19e90 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 eApplyAffinity(p
19e91 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f Val, SQLITE_AFF_
19e92 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f NUMERIC, SQLITE_
19e93 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 UTF8);. }else
19e94 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
19e95 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
19e96 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 y(pVal, affinity
19e97 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
19e98 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e }. if( en
19e99 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c!=SQLITE_UTF8 )
19e9a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
19e9b 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
19e9c 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 g(pVal, enc);.
19e9d 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
19e9e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 op==TK_UMINUS )
19e9f 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 {. if( SQLITE
19ea0 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 _OK==sqlite3Valu
19ea1 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 eFromExpr(db,pEx
19ea2 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 pr->pLeft,enc,af
19ea3 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b finity,&pVal) ){
19ea4 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 . pVal->u.i
19ea5 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e = -1 * pVal->u.
19ea6 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 i;. /* (dou
19ea7 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f ble)-1 In case o
19ea8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
19ea9 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 OATING_POINT...
19eaa 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 */. pVal->r
19eab 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 = (double)-1 *
19eac 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 pVal->r;. }.
19ead 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
19eae 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
19eaf 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f RAL. else if( o
19eb0 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 p==TK_BLOB ){.
19eb1 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 int nVal;.
19eb2 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 assert( pExpr->u
19eb3 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 .zToken[0]=='x'
19eb4 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b || pExpr->u.zTok
19eb5 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 en[0]=='X' );.
19eb6 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
19eb7 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c >u.zToken[1]=='\
19eb8 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d '' );. pVal =
19eb9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 sqlite3ValueNew
19eba 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 (db);. if( !p
19ebb 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 Val ) goto no_me
19ebc 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 m;. zVal = &p
19ebd 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 Expr->u.zToken[2
19ebe 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 ];. nVal = sq
19ebf 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 lite3Strlen30(zV
19ec0 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 al)-1;. asser
19ec1 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 t( zVal[nVal]=='
19ec2 5c 27 27 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 \'' );. sqlit
19ec3 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
19ec4 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 pVal, sqlite3Hex
19ec5 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c ToBlob(db, zVal,
19ec6 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a nVal), nVal/2,.
19ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ec8 20 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49 0, SQLI
19ec9 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d TE_DYNAMIC);. }
19eca 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 .#endif.. if( p
19ecb 56 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Val ){. sqlit
19ecc 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
19ecd 70 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 pe(pVal);. }.
19ece 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 *ppVal = pVal;.
19ecf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19ed0 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 K;..no_mem:. db
19ed1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
19ed2 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 1;. sqlite3DbF
19ed3 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 ree(db, zVal);.
19ed4 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 sqlite3ValueFre
19ed5 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 e(pVal);. *ppVa
19ed6 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 l = 0;. return
19ed7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a SQLITE_NOMEM;.}.
19ed8 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
19ed9 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f e string value o
19eda 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c f an sqlite3_val
19edb 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c ue object.*/.SQL
19edc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19edd 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
19ede 53 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 Str(. sqlite3_v
19edf 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 alue *v, /*
19ee0 56 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 Value to be set
19ee1 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 */. int n,
19ee2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
19ee3 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a ngth of string z
19ee4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
19ee5 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 *z, /* T
19ee6 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 ext of the new s
19ee7 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e tring */. u8 en
19ee8 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
19ee9 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 /* Encoding to
19eea 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a use */. void (*
19eeb 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f xDel)(void*) /
19eec 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 * Destructor for
19eed 20 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 the string */.)
19eee 7b 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 {. if( v ) sqli
19eef 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
19ef0 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c ((Mem *)v, z, n,
19ef1 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a enc, xDel);.}..
19ef2 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 /*.** Free an sq
19ef3 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
19ef4 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ct.*/.SQLITE_PRI
19ef5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19ef6 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 3ValueFree(sqlit
19ef7 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 e3_value *v){.
19ef8 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b if( !v ) return;
19ef9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
19efa 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 mRelease((Mem *)
19efb 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 v);. sqlite3DbF
19efc 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 ree(((Mem*)v)->d
19efd 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 b, v);.}../*.**
19efe 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
19eff 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
19f00 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e sqlite3_value
19f01 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a object assuming.
19f02 2a 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20 ** that it uses
19f03 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e the encoding "en
19f04 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 c".*/.SQLITE_PRI
19f05 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
19f06 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 ValueBytes(sqlit
19f07 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 e3_value *pVal,
19f08 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a u8 enc){. Mem *
19f09 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a p = (Mem*)pVal;.
19f0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 if( (p->flags
19f0b 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c & MEM_Blob)!=0 |
19f0c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 | sqlite3ValueTe
19f0d 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b xt(pVal, enc) ){
19f0e 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 . if( p->flag
19f0f 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
19f10 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e return p->
19f11 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a n + p->u.nZero;.
19f12 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19f13 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 return p->n;.
19f14 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
19f15 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../********
19f16 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
19f17 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a bemem.c ********
19f18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f1a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
19f1b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
19f1c 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a e vdbeaux.c ****
19f1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f1f 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
19f20 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 3 September 6.**
19f21 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
19f22 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
19f23 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
19f24 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
19f25 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
19f26 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
19f27 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
19f28 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
19f29 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
19f2a 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
19f2b 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
19f2c 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
19f2d 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
19f2e 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
19f2f 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
19f30 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
19f31 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
19f32 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
19f33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
19f37 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
19f38 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 ns code used for
19f39 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 creating, destr
19f3a 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c oying, and popul
19f3b 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 ating.** a VDBE
19f3c 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f (or an "sqlite3_
19f3d 73 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b stmt" as it is k
19f3e 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 nown to the outs
19f3f 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 ide world.) Pri
19f40 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e or.** to version
19f41 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 2.8.7, all this
19f42 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e code was combin
19f43 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 ed into the vdbe
19f44 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a .c source file..
19f45 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 ** But that file
19f46 20 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f was getting too
19f47 20 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 big so this sub
19f48 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 routines were sp
19f49 6c 69 74 20 6f 75 74 2e 0a 2a 2f 0a 0a 0a 0a 2f lit out..*/..../
19f4a 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67 *.** When debugg
19f4b 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e ing the code gen
19f4c 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 erator in a symb
19f4d 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f olic debugger, o
19f4e 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 ne can.** set th
19f4f 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 e sqlite3VdbeAdd
19f50 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64 opTrace to 1 and
19f51 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c all opcodes wil
19f52 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 l be printed.**
19f53 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65 as they are adde
19f54 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 d to the instruc
19f55 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a tion stream..*/.
19f56 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
19f57 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
19f58 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19f59 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30 beAddopTrace = 0
19f5a 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a ;.#endif.../*.**
19f5b 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 Create a new vi
19f5c 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
19f5d 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ngine..*/.SQLITE
19f5e 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 _PRIVATE Vdbe *s
19f5f 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
19f60 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
19f61 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 Vdbe *p;. p =
19f62 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
19f63 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 ero(db, sizeof(V
19f64 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d dbe) );. if( p=
19f65 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
19f66 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 p->db = db;. i
19f67 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a f( db->pVdbe ){.
19f68 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 db->pVdbe->p
19f69 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 Prev = p;. }.
19f6a 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 p->pNext = db->p
19f6b 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 Vdbe;. p->pPrev
19f6c 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 = 0;. db->pVdb
19f6d 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 e = p;. p->magi
19f6e 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 c = VDBE_MAGIC_I
19f6f 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b NIT;. return p;
19f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 .}../*.** Rememb
19f71 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e er the SQL strin
19f72 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 g for a prepared
19f73 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 statement..*/.S
19f74 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19f75 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
19f76 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f tSql(Vdbe *p, co
19f77 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
19f78 20 6e 2c 20 69 6e 74 20 69 73 50 72 65 70 61 72 n, int isPrepar
19f79 65 56 32 29 7b 0a 20 20 61 73 73 65 72 74 28 20 eV2){. assert(
19f7a 69 73 50 72 65 70 61 72 65 56 32 3d 3d 31 20 7c isPrepareV2==1 |
19f7b 7c 20 69 73 50 72 65 70 61 72 65 56 32 3d 3d 30 | isPrepareV2==0
19f7c 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 );. if( p==0 )
19f7d 20 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 return;.#ifdef
19f7e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
19f7f 45 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61 E. if( !isPrepa
19f80 72 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 reV2 ) return;.#
19f81 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 endif. assert(
19f82 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 p->zSql==0 );.
19f83 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 p->zSql = sqlite
19f84 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 3DbStrNDup(p->db
19f85 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 , z, n);. p->is
19f86 50 72 65 70 61 72 65 56 32 20 3d 20 28 75 38 29 PrepareV2 = (u8)
19f87 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a isPrepareV2;.}..
19f88 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
19f89 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20 SQL associated
19f8a 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20 with a prepared
19f8b 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c statement.*/.SQL
19f8c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
19f8d 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 ar *sqlite3_sql(
19f8e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
19f8f 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 tmt){. Vdbe *p
19f90 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
19f91 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 69 73 . return (p->is
19f92 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a PrepareV2 ? p->z
19f93 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a Sql : 0);.}../*.
19f94 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74 ** Swap all cont
19f95 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f 20 ent between two
19f96 56 44 42 45 20 73 74 72 75 63 74 75 72 65 73 2e VDBE structures.
19f97 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19f98 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
19f99 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 70 41 dbeSwap(Vdbe *pA
19f9a 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 20 56 , Vdbe *pB){. V
19f9b 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a dbe tmp, *pTmp;.
19f9c 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 char *zTmp;.
19f9d 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41 tmp = *pA;. *pA
19f9e 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20 = *pB;. *pB =
19f9f 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 tmp;. pTmp = pA
19fa0 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70 ->pNext;. pA->p
19fa1 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74 Next = pB->pNext
19fa2 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20 ;. pB->pNext =
19fa3 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 pTmp;. pTmp = p
19fa4 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e A->pPrev;. pA->
19fa5 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72 65 pPrev = pB->pPre
19fa6 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 3d v;. pB->pPrev =
19fa7 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20 pTmp;. zTmp =
19fa8 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e pA->zSql;. pA->
19fa9 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b zSql = pB->zSql;
19faa 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54 . pB->zSql = zT
19fab 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 65 70 mp;. pB->isPrep
19fac 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 50 72 areV2 = pA->isPr
19fad 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64 epareV2;.}..#ifd
19fae 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
19faf 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 /*.** Turn traci
19fb0 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a ng on or off.*/.
19fb1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19fb2 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 oid sqlite3VdbeT
19fb3 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49 race(Vdbe *p, FI
19fb4 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d LE *trace){. p-
19fb5 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a >trace = trace;.
19fb6 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
19fb7 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e Resize the Vdbe.
19fb8 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61 aOp array so tha
19fb9 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74 t it is at least
19fba 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74 one op larger t
19fbb 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a han .** it was..
19fbc 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d **.** If an out-
19fbd 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 of-memory error
19fbe 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 occurs while res
19fbf 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c izing the array,
19fc0 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
19fc1 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 E_NOMEM. In this
19fc2 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61 case Vdbe.aOp a
19fc3 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 nd Vdbe.nOpAlloc
19fc4 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 remain .** unch
19fc5 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73 anged (this is s
19fc6 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 o that any opcod
19fc7 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 es already alloc
19fc8 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 ated can be .**
19fc9 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f correctly deallo
19fca 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
19fcb 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
19fcc 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 Vdbe)..*/.stati
19fcd 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 c int growOpArra
19fce 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 y(Vdbe *p){. Vd
19fcf 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e beOp *pNew;. in
19fd0 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 t nNew = (p->nOp
19fd1 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c Alloc ? p->nOpAl
19fd2 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 loc*2 : (int)(10
19fd3 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 24/sizeof(Op)));
19fd4 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
19fd5 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 3DbRealloc(p->db
19fd6 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 , p->aOp, nNew*s
19fd7 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 izeof(Op));. if
19fd8 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d ( pNew ){. p-
19fd9 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 >nOpAlloc = sqli
19fda 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
19fdb 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a p->db, pNew)/siz
19fdc 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e eof(Op);. p->
19fdd 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a aOp = pNew;. }.
19fde 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 20 3f return (pNew ?
19fdf 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
19fe0 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f ITE_NOMEM);.}../
19fe1 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 69 *.** Add a new i
19fe2 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 74 68 nstruction to th
19fe3 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 72 75 e list of instru
19fe4 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 69 ctions current i
19fe5 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 n the.** VDBE.
19fe6 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 Return the addre
19fe7 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 69 6e ss of the new in
19fe8 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a struction..**.**
19fe9 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a Parameters:.**.
19fea 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 20 20 ** p
19feb 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f Pointer to
19fec 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20 the VDBE.**.**
19fed 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 op
19fee 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 66 6f The opcode fo
19fef 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 r this instructi
19ff0 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20 on.**.** p1,
19ff1 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 65 72 p2, p3 Oper
19ff2 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 ands.**.** Use t
19ff3 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 he sqlite3VdbeRe
19ff4 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 75 6e solveLabel() fun
19ff5 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20 ction to fix an
19ff6 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74 address and.** t
19ff7 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 he sqlite3VdbeCh
19ff8 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 69 6f angeP4() functio
19ff9 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 n to change the
19ffa 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 0a value of the P4.
19ffb 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53 ** operand..*/.S
19ffc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19ffd 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
19ffe 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 Op3(Vdbe *p, int
19fff 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 op, int p1, int
1a000 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20 p2, int p3){.
1a001 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f 70 20 int i;. VdbeOp
1a002 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e *pOp;.. i = p->
1a003 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 nOp;. assert( p
1a004 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1a005 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 GIC_INIT );. as
1a006 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 sert( op>0 && op
1a007 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 70 <0xff );. if( p
1a008 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b ->nOpAlloc<=i ){
1a009 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f 70 41 . if( growOpA
1a00a 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 20 rray(p) ){.
1a00b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1a00c 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b . }. p->nOp++;
1a00d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 . pOp = &p->aOp
1a00e 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f [i];. pOp->opco
1a00f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 de = (u8)op;. p
1a010 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f Op->p5 = 0;. pO
1a011 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f p->p1 = p1;. pO
1a012 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f p->p2 = p2;. pO
1a013 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f p->p3 = p3;. pO
1a014 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70 p->p4.p = 0;. p
1a015 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
1a016 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 NOTUSED;. p->ex
1a017 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 pired = 0;.#ifde
1a018 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1a019 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d pOp->zComment =
1a01a 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 0;. if( sqlite
1a01b 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
1a01c 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 ) sqlite3VdbePri
1a01d 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 ntOp(0, i, &p->a
1a01e 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 Op[i]);.#endif.#
1a01f 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
1a020 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 LE. pOp->cycles
1a021 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 = 0;. pOp->cnt
1a022 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 = 0;.#endif. r
1a023 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 eturn i;.}.SQLIT
1a024 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1a025 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
1a026 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 Vdbe *p, int op)
1a027 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1a028 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
1a029 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a op, 0, 0, 0);.}.
1a02a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1a02b 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
1a02c 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp1(Vdbe *p, in
1a02d 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 t op, int p1){.
1a02e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1a02f 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
1a030 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 p1, 0, 0);.}.SQ
1a031 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1a032 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1a033 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p2(Vdbe *p, int
1a034 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 op, int p1, int
1a035 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 p2){. return sq
1a036 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1a037 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 p, op, p1, p2, 0
1a038 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 );.}.../*.** Add
1a039 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20 an opcode that
1a03a 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20 includes the p4
1a03b 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74 value as a point
1a03c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
1a03d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1a03e 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56 3VdbeAddOp4(. V
1a03f 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 dbe *p,
1a040 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 /* Add the op
1a041 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20 code to this VM
1a042 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
1a043 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1a044 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 new opcode */.
1a045 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20 int p1,
1a046 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 /* The P1 op
1a047 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
1a048 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2, /
1a049 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64 * The P2 operand
1a04a 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 */. int p3,
1a04b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1a04c 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P3 operand */.
1a04d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
1a04e 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f , /* The P4 o
1a04f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
1a050 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20 p4type
1a051 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79 /* P4 operand ty
1a052 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 pe */.){. int a
1a053 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
1a054 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 eAddOp3(p, op, p
1a055 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 1, p2, p3);. sq
1a056 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1a057 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 4(p, addr, zP4,
1a058 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 p4type);. retur
1a059 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n addr;.}../*.**
1a05a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 Add an opcode t
1a05b 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 hat includes the
1a05c 20 70 34 20 76 61 6c 75 65 20 61 73 20 61 6e 20 p4 value as an
1a05d 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 integer..*/.SQLI
1a05e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a05f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1a060 49 6e 74 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 Int(. Vdbe *p,
1a061 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
1a062 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f 20 d the opcode to
1a063 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 this VM */. int
1a064 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 op,
1a065 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 6f /* The new opco
1a066 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 de */. int p1,
1a067 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a068 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f he P1 operand */
1a069 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 20 . int p2,
1a06a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 32 /* The P2
1a06b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
1a06c 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 20 t p3,
1a06d 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 72 /* The P3 oper
1a06e 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 34 20 and */. int p4
1a06f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a070 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 61 The P4 operand a
1a071 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a s an integer */.
1a072 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 ){. int addr =
1a073 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1a074 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 3(p, op, p1, p2,
1a075 20 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 p3);. sqlite3V
1a076 64 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 dbeChangeP4(p, a
1a077 64 64 72 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f ddr, SQLITE_INT_
1a078 54 4f 5f 50 54 52 28 70 34 29 2c 20 50 34 5f 49 TO_PTR(p4), P4_I
1a079 4e 54 33 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 NT32);. return
1a07a 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 addr;.}../*.** C
1a07b 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d 62 reate a new symb
1a07c 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20 61 olic label for a
1a07d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 n instruction th
1a07e 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 at has yet to be
1a07f 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65 20 .** coded. The
1a080 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 69 symbolic label i
1a081 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20 s really just a
1a082 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e negative number.
1a083 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 63 The.** label c
1a084 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 an be used as th
1a085 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61 6e e P2 value of an
1a086 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61 74 operation. Lat
1a087 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 er, when.** the
1a088 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76 65 label is resolve
1a089 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 63 20 d to a specific
1a08a 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 44 42 address, the VDB
1a08b 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20 74 E will scan.** t
1a08c 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72 61 hrough its opera
1a08d 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63 68 tion list and ch
1a08e 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73 20 ange all values
1a08f 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 74 63 of P2 which matc
1a090 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69 h.** the label i
1a091 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65 64 nto the resolved
1a092 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 address..**.**
1a093 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20 74 The VDBE knows t
1a094 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 20 69 hat a P2 value i
1a095 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75 73 s a label becaus
1a096 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a 20 e labels are.**
1a097 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65 20 always negative
1a098 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61 72 and P2 values ar
1a099 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20 e suppose to be
1a09a 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a non-negative..**
1a09b 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69 Hence, a negati
1a09c 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 ve P2 value is a
1a09d 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73 20 label that has
1a09e 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76 yet to be resolv
1a09f 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20 69 ed..**.** Zero i
1a0a0 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 s returned if a
1a0a1 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a malloc() fails..
1a0a2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a0a3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1a0a4 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 20 eMakeLabel(Vdbe
1a0a5 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p){. int i;.
1a0a6 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b i = p->nLabel++;
1a0a7 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
1a0a8 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1a0a9 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69 3e INIT );. if( i>
1a0aa 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 =p->nLabelAlloc
1a0ab 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 ){. int n = p
1a0ac 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 32 20 ->nLabelAlloc*2
1a0ad 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 + 5;. p->aLab
1a0ae 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 el = sqlite3DbRe
1a0af 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d 3e 64 allocOrFree(p->d
1a0b0 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20 b, p->aLabel,.
1a0b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0b3 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28 70 2d n*sizeof(p-
1a0b4 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20 >aLabel[0]));.
1a0b5 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 p->nLabelAlloc
1a0b6 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1a0b7 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 2d ocSize(p->db, p-
1a0b8 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f 66 28 >aLabel)/sizeof(
1a0b9 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b 0a 20 p->aLabel[0]);.
1a0ba 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 }. if( p->aLab
1a0bb 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 el ){. p->aLa
1a0bc 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d bel[i] = -1;. }
1a0bd 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a . return -1-i;.
1a0be 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 }../*.** Resolve
1a0bf 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65 label "x" to be
1a0c0 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
1a0c1 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1a0c2 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e tion to.** be in
1a0c3 73 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72 serted. The par
1a0c4 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20 ameter "x" must
1a0c5 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e have been obtain
1a0c6 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 ed from.** a pri
1a0c7 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 or call to sqlit
1a0c8 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
1a0c9 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
1a0ca 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a0cb 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
1a0cc 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 l(Vdbe *p, int x
1a0cd 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d ){. int j = -1-
1a0ce 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e x;. assert( p->
1a0cf 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a0d0 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 C_INIT );. asse
1a0d1 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d rt( j>=0 && j<p-
1a0d2 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 >nLabel );. if(
1a0d3 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 p->aLabel ){.
1a0d4 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d p->aLabel[j] =
1a0d5 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a p->nOp;. }.}..
1a0d6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1a0d7 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 33 41 73 BUG /* sqlite3As
1a0d8 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 6c sertMayAbort() l
1a0d9 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ogic */../*.** T
1a0da 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 he following typ
1a0db 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 e and function a
1a0dc 72 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 re used to itera
1a0dd 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 6f te through all o
1a0de 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61 20 56 pcodes.** in a V
1a0df 64 62 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d dbe main program
1a0e0 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 65 and each of the
1a0e1 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 28 74 sub-programs (t
1a0e2 72 69 67 67 65 72 73 29 20 69 74 20 6d 61 79 20 riggers) it may
1a0e3 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72 65 63 .** invoke direc
1a0e4 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c tly or indirectl
1a0e5 79 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 y. It should be
1a0e6 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a used as follows:
1a0e7 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70 4f 70 .**.** Op *pOp
1a0e8 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49 74 65 ;.** VdbeOpIte
1a0e9 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 r sIter;.**.**
1a0ea 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20 memset(&sIter,
1a0eb 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29 0, sizeof(sIter)
1a0ec 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e 76 20 );.** sIter.v
1a0ed 3d 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 = v;
1a0ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0ef 2f 2f 20 76 20 69 73 20 6f 66 20 74 79 70 65 20 // v is of type
1a0f0 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68 69 6c Vdbe* .** whil
1a0f1 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 e( (pOp = opIter
1a0f2 4e 65 78 74 28 26 73 49 74 65 72 29 29 20 29 7b Next(&sIter)) ){
1a0f3 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 73 6f .** // Do so
1a0f4 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 4f 70 mething with pOp
1a0f5 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73 71 6c .** }.** sql
1a0f6 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 ite3DbFree(v->db
1a0f7 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a , sIter.apSub);.
1a0f8 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ** .*/.typedef s
1a0f9 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 truct VdbeOpIter
1a0fa 20 56 64 62 65 4f 70 49 74 65 72 3b 0a 73 74 72 VdbeOpIter;.str
1a0fb 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 20 7b uct VdbeOpIter {
1a0fc 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 . Vdbe *v;
1a0fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a0fe 20 56 64 62 65 20 74 6f 20 69 74 65 72 61 74 65 Vdbe to iterate
1a0ff 20 74 68 72 6f 75 67 68 20 74 68 65 20 6f 70 63 through the opc
1a100 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53 75 62 odes of */. Sub
1a101 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 3b Program **apSub;
1a102 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 /* Array
1a103 20 6f 66 20 73 75 62 70 72 6f 67 72 61 6d 73 20 of subprograms
1a104 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b 20 20 */. int nSub;
1a105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a106 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
1a107 72 69 65 73 20 69 6e 20 61 70 53 75 62 20 2a 2f ries in apSub */
1a108 0a 20 20 69 6e 74 20 69 41 64 64 72 3b 20 20 20 . int iAddr;
1a109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a10a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 Address of next
1a10b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 instruction to
1a10c 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 return */. int
1a10d 69 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 iSub;
1a10e 20 20 20 20 20 20 20 2f 2a 20 30 20 3d 20 6d 61 /* 0 = ma
1a10f 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20 3d 20 in program, 1 =
1a110 66 69 72 73 74 20 73 75 62 2d 70 72 6f 67 72 61 first sub-progra
1a111 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 m etc. */.};.sta
1a112 74 69 63 20 4f 70 20 2a 6f 70 49 74 65 72 4e 65 tic Op *opIterNe
1a113 78 74 28 56 64 62 65 4f 70 49 74 65 72 20 2a 70 xt(VdbeOpIter *p
1a114 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
1a115 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65 74 20 ->v;. Op *pRet
1a116 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70 3b 0a = 0;. Op *aOp;.
1a117 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20 69 66 int nOp;.. if
1a118 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e 6e 53 ( p->iSub<=p->nS
1a119 75 62 20 29 7b 0a 0a 20 20 20 20 69 66 28 20 70 ub ){.. if( p
1a11a 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20 20 20 ->iSub==0 ){.
1a11b 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f 70 3b aOp = v->aOp;
1a11c 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 76 2d 3e . nOp = v->
1a11d 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a nOp;. }else{.
1a11e 20 20 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 aOp = p->a
1a11f 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d pSub[p->iSub-1]-
1a120 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 >aOp;. nOp
1a121 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 = p->apSub[p->iS
1a122 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 ub-1]->nOp;.
1a123 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d }. assert( p-
1a124 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a 0a 20 >iAddr<nOp );..
1a125 20 20 20 70 52 65 74 20 3d 20 26 61 4f 70 5b 70 pRet = &aOp[p
1a126 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20 70 2d ->iAddr];. p-
1a127 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20 69 66 >iAddr++;. if
1a128 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f 70 20 ( p->iAddr==nOp
1a129 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53 75 62 ){. p->iSub
1a12a 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 41 64 ++;. p->iAd
1a12b 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 dr = 0;. }.
1a12c 0a 20 20 20 20 69 66 28 20 70 52 65 74 2d 3e 70 . if( pRet->p
1a12d 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f 4type==P4_SUBPRO
1a12e 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 69 6e GRAM ){. in
1a12f 74 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e 6e 53 t nByte = (p->nS
1a130 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62 ub+1)*sizeof(Sub
1a131 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 Program*);.
1a132 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f int j;. fo
1a133 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53 75 62 r(j=0; j<p->nSub
1a134 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
1a135 69 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a 5d 3d if( p->apSub[j]=
1a136 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 =pRet->p4.pProgr
1a137 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 am ) break;.
1a138 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3d }. if( j=
1a139 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20 20 20 =p->nSub ){.
1a13a 20 20 20 20 70 2d 3e 61 70 53 75 62 20 3d 20 73 p->apSub = s
1a13b 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
1a13c 72 46 72 65 65 28 76 2d 3e 64 62 2c 20 70 2d 3e rFree(v->db, p->
1a13d 61 70 53 75 62 2c 20 6e 42 79 74 65 29 3b 0a 20 apSub, nByte);.
1a13e 20 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 61 if( !p->a
1a13f 70 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 pSub ){.
1a140 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 20 20 pRet = 0;.
1a141 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a142 20 20 20 20 20 70 2d 3e 61 70 53 75 62 5b 70 2d p->apSub[p-
1a143 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 74 2d >nSub++] = pRet-
1a144 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 >p4.pProgram;.
1a145 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1a146 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
1a147 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a urn pRet;.}../*.
1a148 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 ** Check if the
1a149 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 program stored i
1a14a 6e 20 74 68 65 20 56 4d 20 61 73 73 6f 63 69 61 n the VM associa
1a14b 74 65 64 20 77 69 74 68 20 70 50 61 72 73 65 20 ted with pParse
1a14c 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 may.** throw an
1a14d 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 20 ABORT exception
1a14e 28 63 61 75 73 69 6e 67 20 74 68 65 20 73 74 61 (causing the sta
1a14f 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 tement, but not
1a150 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 entire transacti
1a151 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c on.** to be roll
1a152 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63 ed back). This c
1a153 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65 ondition is true
1a154 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f if the main pro
1a155 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73 gram or any.** s
1a156 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74 ub-programs cont
1a157 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20 ains any of the
1a158 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
1a159 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69 * OP_Halt wi
1a15a 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e th P1=SQLITE_CON
1a15b 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f STRAINT and P2=O
1a15c 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 E_Abort..** *
1a15d 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77 OP_HaltIfNull w
1a15e 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f ith P1=SQLITE_CO
1a15f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d NSTRAINT and P2=
1a160 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a OE_Abort..** *
1a161 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20 OP_Destroy.**
1a162 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a * OP_VUpdate.
1a163 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61 ** * OP_VRena
1a164 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 46 6b me.** * OP_Fk
1a165 43 6f 75 6e 74 65 72 20 77 69 74 68 20 50 32 3d Counter with P2=
1a166 3d 30 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f =0 (immediate fo
1a167 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 reign key constr
1a168 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e aint).**.** Then
1a169 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 check that the
1a16a 76 61 6c 75 65 20 6f 66 20 50 61 72 73 65 2e 6d value of Parse.m
1a16b 61 79 41 62 6f 72 74 20 69 73 20 74 72 75 65 20 ayAbort is true
1a16c 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d if an.** ABORT m
1a16d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72 ay be thrown, or
1a16e 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 false otherwise
1a16f 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 . Return true if
1a170 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63 it does.** matc
1a171 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 h, or false othe
1a172 72 77 69 73 65 2e 20 54 68 69 73 20 66 75 6e 63 rwise. This func
1a173 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 tion is intended
1a174 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 0a 2a to be used as.*
1a175 2a 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 * part of an ass
1a176 65 72 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e ert statement in
1a177 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53 the compiler. S
1a178 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a imilar to:.**.**
1a179 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
1a17a 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 e3VdbeAssertMayA
1a17b 62 6f 72 74 28 70 50 61 72 73 65 2d 3e 70 56 64 bort(pParse->pVd
1a17c 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 be, pParse->mayA
1a17d 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 bort) );.*/.SQLI
1a17e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a17f 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 qlite3VdbeAssert
1a180 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 76 MayAbort(Vdbe *v
1a181 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b , int mayAbort){
1a182 0a 20 20 69 6e 74 20 68 61 73 41 62 6f 72 74 20 . int hasAbort
1a183 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a = 0;. Op *pOp;.
1a184 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 VdbeOpIter sIt
1a185 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 er;. memset(&sI
1a186 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 ter, 0, sizeof(s
1a187 49 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e Iter));. sIter.
1a188 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 v = v;.. while(
1a189 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 (pOp = opIterNe
1a18a 78 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29 xt(&sIter))!=0 )
1a18b 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 {. int opcode
1a18c 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a = pOp->opcode;.
1a18d 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d if( opcode==
1a18e 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 OP_Destroy || op
1a18f 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 code==OP_VUpdate
1a190 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 || opcode==OP_V
1a191 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65 66 20 Rename .#ifndef
1a192 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
1a193 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c 7c 20 IGN_KEY. ||
1a194 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b 43 6f (opcode==OP_FkCo
1a195 75 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e 70 31 unter && pOp->p1
1a196 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d ==0 && pOp->p2==
1a197 31 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 1) .#endif.
1a198 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f || ((opcode==OP_
1a199 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d Halt || opcode==
1a19a 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29 20 0a OP_HaltIfNull) .
1a19b 20 20 20 20 20 20 26 26 20 28 70 4f 70 2d 3e 70 && (pOp->p
1a19c 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 1==SQLITE_CONSTR
1a19d 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 32 3d AINT && pOp->p2=
1a19e 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20 20 20 =OE_Abort)).
1a19f 29 7b 0a 20 20 20 20 20 20 68 61 73 41 62 6f 72 ){. hasAbor
1a1a0 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 t = 1;. bre
1a1a1 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
1a1a2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d sqlite3DbFree(v-
1a1a3 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 >db, sIter.apSub
1a1a4 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 );.. /* Return
1a1a5 74 72 75 65 20 69 66 20 68 61 73 41 62 6f 72 74 true if hasAbort
1a1a6 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72 20 69 ==mayAbort. Or i
1a1a7 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 f a malloc failu
1a1a8 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 2a 2a re occured.. **
1a1a9 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 65 If malloc faile
1a1aa 64 2c 20 74 68 65 6e 20 74 68 65 20 77 68 69 6c d, then the whil
1a1ab 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65 20 6d e() loop above m
1a1ac 61 79 20 6e 6f 74 20 68 61 76 65 20 69 74 65 72 ay not have iter
1a1ad 61 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f 75 67 ated. ** throug
1a1ae 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 61 6e h all opcodes an
1a1af 64 20 68 61 73 41 62 6f 72 74 20 6d 61 79 20 62 d hasAbort may b
1a1b0 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74 6c e set incorrectl
1a1b1 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a 20 74 y. Return. ** t
1a1b2 72 75 65 20 66 6f 72 20 74 68 69 73 20 63 61 73 rue for this cas
1a1b3 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 e to prevent the
1a1b4 20 61 73 73 65 72 74 28 29 20 69 6e 20 74 68 65 assert() in the
1a1b5 20 63 61 6c 6c 65 72 73 20 66 72 61 6d 65 0a 20 callers frame.
1a1b6 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 ** from failing
1a1b7 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 . */. return (
1a1b8 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 v->db->mallocFa
1a1b9 69 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f 72 74 iled || hasAbort
1a1ba 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a 7d 0a ==mayAbort );.}.
1a1bb 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1a1bc 5f 44 45 42 55 47 20 2d 20 74 68 65 20 73 71 6c _DEBUG - the sql
1a1bd 69 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f ite3AssertMayAbo
1a1be 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f rt() function */
1a1bf 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72 ../*.** Loop thr
1a1c0 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d ough the program
1a1c1 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20 looking for P2
1a1c2 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 values that are
1a1c3 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a negative.** on j
1a1c4 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ump instructions
1a1c5 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c . Each such val
1a1c6 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20 ue is a label.
1a1c7 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c Resolve the.** l
1a1c8 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20 abel by setting
1a1c9 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20 the P2 value to
1a1ca 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d its correct non-
1a1cb 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a zero value..**.*
1a1cc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1a1cd 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66 s called once af
1a1ce 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 ter all opcodes
1a1cf 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 have been insert
1a1d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 ed..**.** Variab
1a1d1 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 le *pMaxFuncArgs
1a1d2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d is set to the m
1a1d3 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 aximum value of
1a1d4 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20 any P2 argument
1a1d5 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e .** to an OP_Fun
1a1d6 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65 ction, OP_AggSte
1a1d7 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 p or OP_VFilter
1a1d8 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20 opcode. This is
1a1d9 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 used by .** sqli
1a1da 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1a1db 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56 () to size the V
1a1dc 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 dbe.apArg[] arra
1a1dd 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4f 70 2e y..**.** The Op.
1a1de 6f 70 66 6c 61 67 73 20 66 69 65 6c 64 20 69 73 opflags field is
1a1df 20 73 65 74 20 6f 6e 20 61 6c 6c 20 6f 70 63 6f set on all opco
1a1e0 64 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 des..*/.static v
1a1e1 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c oid resolveP2Val
1a1e2 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ues(Vdbe *p, int
1a1e3 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b *pMaxFuncArgs){
1a1e4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1a1e5 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 nMaxArgs = *pMax
1a1e6 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a FuncArgs;. Op *
1a1e7 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 pOp;. int *aLab
1a1e8 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a el = p->aLabel;.
1a1e9 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 p->readOnly =
1a1ea 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 1;. for(pOp=p->
1a1eb 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b aOp, i=p->nOp-1;
1a1ec 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b i>=0; i--, pOp+
1a1ed 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 +){. u8 opcod
1a1ee 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b e = pOp->opcode;
1a1ef 0a 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 66 6c 61 .. pOp->opfla
1a1f0 67 73 20 3d 20 73 71 6c 69 74 65 33 4f 70 63 6f gs = sqlite3Opco
1a1f1 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 deProperty[opcod
1a1f2 65 5d 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f e];. if( opco
1a1f3 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 de==OP_Function
1a1f4 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 67 || opcode==OP_Ag
1a1f5 67 53 74 65 70 20 29 7b 0a 20 20 20 20 20 20 69 gStep ){. i
1a1f6 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 f( pOp->p5>nMaxA
1a1f7 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d rgs ) nMaxArgs =
1a1f8 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 20 20 7d 65 pOp->p5;. }e
1a1f9 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
1a1fa 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 OP_Transaction &
1a1fb 26 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a & pOp->p2!=0 ){.
1a1fc 20 20 20 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c p->readOnl
1a1fd 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 y = 0;.#ifndef S
1a1fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1a1ff 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 ALTABLE. }els
1a200 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 e if( opcode==OP
1a201 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 _VUpdate ){.
1a202 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d if( pOp->p2>nM
1a203 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 axArgs ) nMaxArg
1a204 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 s = pOp->p2;.
1a205 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 }else if( opcod
1a206 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b e==OP_VFilter ){
1a207 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 . int n;.
1a208 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
1a209 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 Op - i >= 3 );.
1a20a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1a20b 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f [-1].opcode==OP_
1a20c 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20 Integer );.
1a20d 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b n = pOp[-1].p1;
1a20e 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 . if( n>nMa
1a20f 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 xArgs ) nMaxArgs
1a210 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 = n;.#endif.
1a211 20 7d 0a 0a 20 20 20 20 69 66 28 20 28 70 4f 70 }.. if( (pOp
1a212 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c ->opflags & OPFL
1a213 47 5f 4a 55 4d 50 29 21 3d 30 20 26 26 20 70 4f G_JUMP)!=0 && pO
1a214 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 p->p2<0 ){.
1a215 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d assert( -1-pOp-
1a216 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b >p2<p->nLabel );
1a217 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d . pOp->p2 =
1a218 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e aLabel[-1-pOp->
1a219 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 p2];. }. }.
1a21a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1a21b 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 ->db, p->aLabel)
1a21c 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 ;. p->aLabel =
1a21d 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 0;.. *pMaxFuncA
1a21e 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a rgs = nMaxArgs;.
1a21f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1a220 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
1a221 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1a222 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 ion to be insert
1a223 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1a224 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1a225 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 3VdbeCurrentAddr
1a226 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 (Vdbe *p){. ass
1a227 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1a228 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1a229 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f ;. return p->nO
1a22a 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 p;.}../*.** This
1a22b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1a22c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1a22d 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 6f he array of opco
1a22e 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 des associated w
1a22f 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65 20 ith.** the Vdbe
1a230 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
1a231 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49 74 rst argument. It
1a232 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 20 is the callers
1a233 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a responsibility.*
1a234 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f 72 * to arrange for
1a235 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 the returned ar
1a236 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74 75 ray to be eventu
1a237 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e 67 ally freed using
1a238 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72 65 the .** vdbeFre
1a239 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63 74 eOpArray() funct
1a23a 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 ion..**.** Befor
1a23b 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 6e e returning, *pn
1a23c 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 Op is set to the
1a23d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
1a23e 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e es in the return
1a23f 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c 73 ed.** array. Als
1a240 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73 20 o, *pnMaxArg is
1a241 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 65 set to the large
1a242 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74 r of its current
1a243 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20 74 value and .** t
1a244 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 he number of ent
1a245 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62 65 ries in the Vdbe
1a246 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20 72 .apArg[] array r
1a247 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63 75 equired to execu
1a248 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75 72 te the .** retur
1a249 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a ned program..*/.
1a24a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 SQLITE_PRIVATE V
1a24b 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 dbeOp *sqlite3Vd
1a24c 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56 64 beTakeOpArray(Vd
1a24d 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f 70 be *p, int *pnOp
1a24e 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67 29 , int *pnMaxArg)
1a24f 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 20 {. VdbeOp *aOp
1a250 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73 65 = p->aOp;. asse
1a251 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e 64 rt( aOp && !p->d
1a252 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a253 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
1a254 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65 55 hat sqlite3VdbeU
1a255 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20 6e sesBtree() was n
1a256 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 ot called on thi
1a257 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72 74 s VM */. assert
1a258 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75 74 ( p->aMutex.nMut
1a259 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73 6f ex==0 );.. reso
1a25a 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20 70 lveP2Values(p, p
1a25b 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e 4f nMaxArg);. *pnO
1a25c 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70 2d p = p->nOp;. p-
1a25d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74 75 >aOp = 0;. retu
1a25e 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn aOp;.}../*.**
1a25f 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 73 Add a whole lis
1a260 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20 t of operations
1a261 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e to the operation
1a262 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e 20 stack. Return
1a263 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f the.** address o
1a264 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 72 f the first oper
1a265 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a ation added..*/.
1a266 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1a267 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
1a268 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c dOpList(Vdbe *p,
1a269 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 int nOp, VdbeOp
1a26a 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 List const *aOp)
1a26b 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 {. int addr;.
1a26c 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a26d 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
1a26e 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f T );. if( p->nO
1a26f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f 70 p + nOp > p->nOp
1a270 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 41 Alloc && growOpA
1a271 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 72 rray(p) ){. r
1a272 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 eturn 0;. }. a
1a273 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 ddr = p->nOp;.
1a274 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e 30 if( ALWAYS(nOp>0
1a275 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a ) ){. int i;.
1a276 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 63 VdbeOpList c
1a277 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 3b onst *pIn = aOp;
1a278 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1a279 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b 29 nOp; i++, pIn++)
1a27a 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 3d {. int p2 =
1a27b 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 20 pIn->p2;.
1a27c 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 26 VdbeOp *pOut = &
1a27d 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b 0a p->aOp[i+addr];.
1a27e 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 6f pOut->opco
1a27f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 65 de = pIn->opcode
1a280 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 31 ;. pOut->p1
1a281 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 20 = pIn->p1;.
1a282 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 28 73 if( p2<0 && (s
1a283 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 qlite3OpcodeProp
1a284 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70 63 6f 64 erty[pOut->opcod
1a285 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 e] & OPFLG_JUMP)
1a286 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 !=0 ){. p
1a287 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 20 2b Out->p2 = addr +
1a288 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 20 20 ADDR(p2);.
1a289 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1a28a 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 pOut->p2 = p2;.
1a28b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 75 }. pOu
1a28c 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 33 3b t->p3 = pIn->p3;
1a28d 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34 74 . pOut->p4t
1a28e 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
1a28f 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34 ;. pOut->p4
1a290 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f .p = 0;. pO
1a291 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 66 64 ut->p5 = 0;.#ifd
1a292 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a293 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 6f 6d pOut->zCom
1a294 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 ment = 0;.
1a295 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 if( sqlite3VdbeA
1a296 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 20 20 ddopTrace ){.
1a297 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1a298 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 64 64 PrintOp(0, i+add
1a299 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 r, &p->aOp[i+add
1a29a 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e r]);. }.#en
1a29b 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d dif. }. p-
1a29c 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 20 7d >nOp += nOp;. }
1a29d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a . return addr;.
1a29e 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
1a29f 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1a2a0 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 P1 operand for
1a2a1 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 a specific instr
1a2a2 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 uction..** This
1a2a3 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 routine is usefu
1a2a4 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 l when a large p
1a2a5 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 rogram is loaded
1a2a6 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 from a.** stati
1a2a7 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 c array using sq
1a2a8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
1a2a9 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 st but we want t
1a2aa 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 o make a.** few
1a2ab 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
1a2ac 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f the program..*/
1a2ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a2ae 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a2af 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 2a 70 ChangeP1(Vdbe *p
1a2b0 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 , int addr, int
1a2b1 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 val){. assert(
1a2b2 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 p!=0 );. assert
1a2b3 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69 ( addr>=0 );. i
1a2b4 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 f( p->nOp>addr )
1a2b5 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 {. p->aOp[add
1a2b6 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20 20 7d r].p1 = val;. }
1a2b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1a2b8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1a2b9 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66 6f 72 e P2 operand for
1a2ba 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 a specific inst
1a2bb 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 ruction..** This
1a2bc 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 routine is usef
1a2bd 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67 20 61 ul for setting a
1a2be 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f jump destinatio
1a2bf 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1a2c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a2c1 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64 3VdbeChangeP2(Vd
1a2c2 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
1a2c3 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 int val){. ass
1a2c4 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
1a2c5 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 ssert( addr>=0 )
1a2c6 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 ;. if( p->nOp>a
1a2c7 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f ddr ){. p->aO
1a2c8 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c p[addr].p2 = val
1a2c9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
1a2ca 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
1a2cb 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72 61 6e of the P3 operan
1a2cc 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 d for a specific
1a2cd 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f instruction..*/
1a2ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a2cf 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a2d0 43 68 61 6e 67 65 50 33 28 56 64 62 65 20 2a 70 ChangeP3(Vdbe *p
1a2d1 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 , int addr, int
1a2d2 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 val){. assert(
1a2d3 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 p!=0 );. assert
1a2d4 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69 ( addr>=0 );. i
1a2d5 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 f( p->nOp>addr )
1a2d6 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 {. p->aOp[add
1a2d7 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d r].p3 = val;. }
1a2d8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1a2d9 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1a2da 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72 e P5 operand for
1a2db 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1a2dc 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72 ly.** added oper
1a2dd 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
1a2de 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a2df 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1a2e0 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61 5(Vdbe *p, u8 va
1a2e1 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21 l){. assert( p!
1a2e2 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 =0 );. if( p->a
1a2e3 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 Op ){. assert
1a2e4 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 ( p->nOp>0 );.
1a2e5 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d p->aOp[p->nOp-
1a2e6 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 20 7d 1].p5 = val;. }
1a2e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1a2e8 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 the P2 operand
1a2e9 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 of instruction a
1a2ea 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 ddr so that it p
1a2eb 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 oints to.** the
1a2ec 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
1a2ed 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1a2ee 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a to be coded..*/.
1a2ef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a2f0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a oid sqlite3VdbeJ
1a2f1 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70 2c umpHere(Vdbe *p,
1a2f2 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 71 int addr){. sq
1a2f3 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1a2f4 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e 4f 2(p, addr, p->nO
1a2f5 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 p);.}.../*.** If
1a2f6 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63 44 the input FuncD
1a2f7 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ef structure is
1a2f8 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e 20 ephemeral, then
1a2f9 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a 20 free it. If.**
1a2fa 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 20 6e the FuncDef is n
1a2fb 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68 65 ot ephermal, the
1a2fc 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f n do nothing..*/
1a2fd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
1a2fe 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 eEphemeralFuncti
1a2ff 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 on(sqlite3 *db,
1a300 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 7b 0a FuncDef *pDef){.
1a301 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 44 65 if( ALWAYS(pDe
1a302 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66 6c 61 f) && (pDef->fla
1a303 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 gs & SQLITE_FUNC
1a304 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a 20 20 _EPHEM)!=0 ){.
1a305 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a306 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d 0a 7d db, pDef);. }.}
1a307 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
1a308 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e 65 63 P4 value if nec
1a309 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 essary..*/.stati
1a30a 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 73 71 c void freeP4(sq
1a30b 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 70 lite3 *db, int p
1a30c 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 34 29 4type, void *p4)
1a30d 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 20 20 {. if( p4 ){.
1a30e 20 20 73 77 69 74 63 68 28 20 70 34 74 79 70 65 switch( p4type
1a30f 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 50 ){. case P
1a310 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 63 61 4_REAL:. ca
1a311 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 20 20 se P4_INT64:.
1a312 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 49 4e case P4_MPRIN
1a313 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 TF:. case P
1a314 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 20 20 4_DYNAMIC:.
1a315 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f case P4_KEYINFO
1a316 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f :. case P4_
1a317 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 20 20 INTARRAY:.
1a318 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f case P4_KEYINFO_
1a319 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 20 20 HANDOFF: {.
1a31a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1a31b 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 20 20 (db, p4);.
1a31c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1a31d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 56 . case P4_V
1a31e 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 DBEFUNC: {.
1a31f 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 VdbeFunc *pVd
1a320 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75 beFunc = (VdbeFu
1a321 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 20 20 nc *)p4;.
1a322 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 freeEphemeralFu
1a323 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 62 65 nction(db, pVdbe
1a324 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a 20 20 Func->pFunc);.
1a325 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1a326 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 70 eDeleteAuxData(p
1a327 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20 20 VdbeFunc, 0);.
1a328 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a329 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75 6e ree(db, pVdbeFun
1a32a 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 c);. brea
1a32b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
1a32c 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 case P4_FUNCDEF
1a32d 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65 65 : {. free
1a32e 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f EphemeralFunctio
1a32f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a 29 n(db, (FuncDef*)
1a330 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 p4);. bre
1a331 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1a332 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b case P4_MEM: {
1a333 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a334 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c 69 74 ValueFree((sqlit
1a335 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b 0a 20 e3_value*)p4);.
1a336 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1a337 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
1a338 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20 20 20 P4_VTAB : {.
1a339 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 sqlite3Vtab
1a33a 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65 20 2a Unlock((VTable *
1a33b 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 )p4);. br
1a33c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1a33d 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52 case P4_SUBPR
1a33e 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20 20 20 OGRAM : {.
1a33f 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f sqlite3VdbePro
1a340 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c 20 28 gramDelete(db, (
1a341 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70 34 2c SubProgram *)p4,
1a342 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 1);. bre
1a343 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1a344 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 }. }.}../*.** F
1a345 72 65 65 20 74 68 65 20 73 70 61 63 65 20 61 6c ree the space al
1a346 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f 70 20 located for aOp
1a347 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c 75 65 and any p4 value
1a348 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
1a349 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 20 63 the.** opcodes c
1a34a 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 2e ontained within.
1a34b 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 20 4e If aOp is not N
1a34c 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75 6d 65 ULL it is assume
1a34d 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a 2a 2a d to contain .**
1a34e 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 0a 2a nOp entries. .*
1a34f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 /.static void vd
1a350 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 73 71 beFreeOpArray(sq
1a351 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 2a 61 lite3 *db, Op *a
1a352 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a 20 20 Op, int nOp){.
1a353 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 20 4f if( aOp ){. O
1a354 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f 72 28 p *pOp;. for(
1a355 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 61 4f pOp=aOp; pOp<&aO
1a356 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 7b 0a p[nOp]; pOp++){.
1a357 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c freeP4(db,
1a358 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f pOp->p4type, pO
1a359 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 p->p4.p);.#ifdef
1a35a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1a35b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a35c 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d e(db, pOp->zComm
1a35d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 ent);.#endif
1a35e 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 . }. }. sq
1a35f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1a360 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 aOp);.}../*.** D
1a361 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 ecrement the ref
1a362 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 53 75 -count on the Su
1a363 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 bProgram structu
1a364 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 re passed as the
1a365 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d .** second argum
1a366 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65 66 2d ent. If the ref-
1a367 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 count reaches ze
1a368 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73 74 72 ro, free the str
1a369 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
1a36a 65 20 61 72 72 61 79 20 6f 66 20 56 44 42 45 20 e array of VDBE
1a36b 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64 20 61 opcodes stored a
1a36c 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 s SubProgram.aOp
1a36d 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a 2a 20 is freed if.**
1a36e 65 69 74 68 65 72 20 74 68 65 20 72 65 66 2d 63 either the ref-c
1a36f 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 ount reaches zer
1a370 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72 20 66 o or parameter f
1a371 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 reeop is non-zer
1a372 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74 o..**.** Since t
1a373 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 6f he array of opco
1a374 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 des pointed to b
1a375 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 y SubProgram.aOp
1a376 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a 2a 2a may directly.**
1a377 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 20 63 or indirectly c
1a378 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e ontain a referen
1a379 63 65 20 74 6f 20 74 68 65 20 53 75 62 50 72 6f ce to the SubPro
1a37a 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 20 69 gram structure i
1a37b 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70 61 73 tself..** By pas
1a37c 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 sing a non-zero
1a37d 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74 65 72 freeop parameter
1a37e 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 , the caller may
1a37f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c ensure that all
1a380 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 73 .** SubProgram s
1a381 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 74 68 tructures and th
1a382 65 69 72 20 61 4f 70 20 61 72 72 61 79 73 20 61 eir aOp arrays a
1a383 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e 20 77 re freed, even w
1a384 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 hen there.** are
1a385 20 73 75 63 68 20 63 69 72 63 75 6c 61 72 20 72 such circular r
1a386 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 eferences..*/.SQ
1a387 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a388 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f d sqlite3VdbePro
1a389 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 gramDelete(sqlit
1a38a 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f 67 72 e3 *db, SubProgr
1a38b 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65 65 6f am *p, int freeo
1a38c 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 p){. if( p ){.
1a38d 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 assert( p->nR
1a38e 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 ef>0 );. if(
1a38f 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e 52 65 freeop || p->nRe
1a390 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 4f 70 f==1 ){. Op
1a391 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a *aOp = p->aOp;.
1a392 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 30 p->aOp = 0
1a393 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72 65 65 ;. vdbeFree
1a394 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f 70 2c OpArray(db, aOp,
1a395 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20 20 20 p->nOp);.
1a396 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20 20 20 p->nOp = 0;.
1a397 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b }. p->nRef--;
1a398 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 . if( p->nRef
1a399 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
1a39a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a39b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
1a39c 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 4e 20 ./*.** Change N
1a39d 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69 6e 67 opcodes starting
1a39e 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f 2d 6f at addr to No-o
1a39f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ps..*/.SQLITE_PR
1a3a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a3a1 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f e3VdbeChangeToNo
1a3a2 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 op(Vdbe *p, int
1a3a3 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 addr, int N){.
1a3a4 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 if( p->aOp ){.
1a3a5 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 VdbeOp *pOp =
1a3a6 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 &p->aOp[addr];.
1a3a7 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
1a3a8 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 69 6c p->db;. whil
1a3a9 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 e( N-- ){.
1a3aa 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e freeP4(db, pOp->
1a3ab 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e p4type, pOp->p4.
1a3ac 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 p);. memset
1a3ad 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (pOp, 0, sizeof(
1a3ae 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 pOp[0]));.
1a3af 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 pOp->opcode = OP
1a3b0 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f 70 _Noop;. pOp
1a3b1 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ++;. }. }.}.
1a3b2 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
1a3b3 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
1a3b4 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 4 operand for a
1a3b5 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 specific instruc
1a3b6 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f tion..** This ro
1a3b7 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20 utine is useful
1a3b8 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72 6f when a large pro
1a3b9 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20 66 gram is loaded f
1a3ba 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63 20 rom a.** static
1a3bb 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c 69 array using sqli
1a3bc 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
1a3bd 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20 but we want to
1a3be 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d 69 make a.** few mi
1a3bf 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 nor changes to t
1a3c0 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a he program..**.*
1a3c1 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74 * If n>=0 then t
1a3c2 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 69 73 he P4 operand is
1a3c3 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e dynamic, meanin
1a3c4 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f 66 g that a copy of
1a3c5 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 .** the string i
1a3c6 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f s made into memo
1a3c7 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
1a3c8 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1a3c9 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 )..** A value of
1a3ca 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79 n==0 means copy
1a3cb 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 75 70 bytes of zP4 up
1a3cc 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e to and includin
1a3cd 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6e g the.** first n
1a3ce 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e ull byte. If n>
1a3cf 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20 0 then copy n+1
1a3d0 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a bytes of zP4..**
1a3d1 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b 45 59 .** If n==P4_KEY
1a3d2 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 74 68 INFO it means th
1a3d3 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f 69 6e at zP4 is a poin
1a3d4 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f ter to a KeyInfo
1a3d5 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 41 structure..** A
1a3d6 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66 copy is made of
1a3d7 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 the KeyInfo str
1a3d8 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d 6f ucture into memo
1a3d9 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
1a3da 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c .** sqlite3_mall
1a3db 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 64 20 oc, to be freed
1a3dc 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 when the Vdbe is
1a3dd 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e finalized..** n
1a3de 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e ==P4_KEYINFO_HAN
1a3df 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 20 74 DOFF indicates t
1a3e0 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 20 74 hat zP4 points t
1a3e1 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 o a KeyInfo stru
1a3e2 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 64 20 cture.** stored
1a3e3 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 74 in memory that t
1a3e4 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 he caller has ob
1a3e5 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
1a3e6 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 65 20 te3_malloc. The
1a3e7 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
1a3e8 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 20 61 d not free the a
1a3e9 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 77 69 llocation, it wi
1a3ea 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 65 6e ll be freed when
1a3eb 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a 2a 20 the Vdbe is.**
1a3ec 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a finalized..** .*
1a3ed 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 6f * Other values o
1a3ee 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 2c 20 f n (P4_STATIC,
1a3ef 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 2e 29 P4_COLLSEQ etc.)
1a3f0 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 7a indicate that z
1a3f1 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 P4 points.** to
1a3f2 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 72 75 a string or stru
1a3f3 63 74 75 72 65 20 74 68 61 74 20 69 73 20 67 75 cture that is gu
1a3f4 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73 aranteed to exis
1a3f5 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69 t for the lifeti
1a3f6 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 64 62 me of.** the Vdb
1a3f7 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 e. In these case
1a3f8 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 63 6f s we can just co
1a3f9 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 0a py the pointer..
1a3fa 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30 20 **.** If addr<0
1a3fb 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 20 6f then change P4 o
1a3fc 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e n the most recen
1a3fd 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 73 tly inserted ins
1a3fe 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c truction..*/.SQL
1a3ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a400 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a401 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 69 6e geP4(Vdbe *p, in
1a402 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68 t addr, const ch
1a403 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e 29 7b ar *zP4, int n){
1a404 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 73 71 . Op *pOp;. sq
1a405 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 73 73 lite3 *db;. ass
1a406 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 64 ert( p!=0 );. d
1a407 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 b = p->db;. ass
1a408 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1a409 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1a40a 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 3d 3d ;. if( p->aOp==
1a40b 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
1a40c 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 66 20 ailed ){. if
1a40d 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 ( n!=P4_KEYINFO
1a40e 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20 29 20 && n!=P4_VTAB )
1a40f 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 {. freeP4(d
1a410 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 b, n, (void*)*(c
1a411 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 20 20 har**)&zP4);.
1a412 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 }. return;.
1a413 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
1a414 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 nOp>0 );. asser
1a415 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 t( addr<p->nOp )
1a416 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 ;. if( addr<0 )
1a417 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e {. addr = p->
1a418 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 70 nOp - 1;. }. p
1a419 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 Op = &p->aOp[add
1a41a 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c r];. freeP4(db,
1a41b 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f pOp->p4type, pO
1a41c 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d p->p4.p);. pOp-
1a41d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 >p4.p = 0;. if(
1a41e 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a n==P4_INT32 ){.
1a41f 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 /* Note: thi
1a420 73 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 s cast is safe,
1a421 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 because the orig
1a422 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 in data point wa
1a423 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 s an int. **
1a424 74 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f that was cast to
1a425 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a a (const char *
1a426 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 ). */. pOp->p
1a427 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 4.i = SQLITE_PTR
1a428 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 _TO_INT(zP4);.
1a429 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1a42a 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 P4_INT32;. }els
1a42b 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a e if( zP4==0 ){.
1a42c 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
1a42d 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 0;. pOp->p4ty
1a42e 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b pe = P4_NOTUSED;
1a42f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d . }else if( n==
1a430 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 P4_KEYINFO ){.
1a431 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
1a432 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 nfo;. int nFi
1a433 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 eld, nByte;..
1a434 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 nField = ((KeyI
1a435 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c nfo*)zP4)->nFiel
1a436 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 d;. nByte = s
1a437 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 izeof(*pKeyInfo)
1a438 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 + (nField-1)*si
1a439 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 zeof(pKeyInfo->a
1a43a 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c Coll[0]) + nFiel
1a43b 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 d;. pKeyInfo
1a43c 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
1a43d 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f nByte );. pO
1a43e 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d p->p4.pKeyInfo =
1a43f 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 pKeyInfo;. i
1a440 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 f( pKeyInfo ){.
1a441 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 u8 *aSortOr
1a442 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 der;. memcp
1a443 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c y(pKeyInfo, zP4,
1a444 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 nByte);. a
1a445 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 SortOrder = pKey
1a446 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
1a447 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 ;. if( aSor
1a448 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 tOrder ){.
1a449 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 pKeyInfo->aSor
1a44a 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e tOrder = (unsign
1a44b 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e ed char*)&pKeyIn
1a44c 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 fo->aColl[nField
1a44d 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ];. memcp
1a44e 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 y(pKeyInfo->aSor
1a44f 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 tOrder, aSortOrd
1a450 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 er, nField);.
1a451 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e }. pOp->
1a452 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 p4type = P4_KEYI
1a453 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a NFO;. }else{.
1a454 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c p->db->mal
1a455 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1a456 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 pOp->p4type
1a457 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 = P4_NOTUSED;.
1a458 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
1a459 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 n==P4_KEYINFO_H
1a45a 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f ANDOFF ){. pO
1a45b 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a p->p4.p = (void*
1a45c 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 )zP4;. pOp->p
1a45d 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 4type = P4_KEYIN
1a45e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 FO;. }else if(
1a45f 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a 20 20 n==P4_VTAB ){.
1a460 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 pOp->p4.p = (v
1a461 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f oid*)zP4;. pO
1a462 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 p->p4type = P4_V
1a463 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 TAB;. sqlite3
1a464 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62 6c 65 VtabLock((VTable
1a465 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61 73 73 *)zP4);. ass
1a466 65 72 74 28 20 28 28 56 54 61 62 6c 65 20 2a 29 ert( ((VTable *)
1a467 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 zP4)->db==p->db
1a468 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e );. }else if( n
1a469 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 <0 ){. pOp->p
1a46a 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 4.p = (void*)zP4
1a46b 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
1a46c 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 e = (signed char
1a46d 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 )n;. }else{.
1a46e 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 if( n==0 ) n =
1a46f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a470 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 zP4);. pOp->p
1a471 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 4.z = sqlite3DbS
1a472 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 trNDup(p->db, zP
1a473 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 4, n);. pOp->
1a474 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 p4type = P4_DYNA
1a475 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e MIC;. }.}..#ifn
1a476 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a def NDEBUG./*.**
1a477 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d Change the comm
1a478 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d ent on the the m
1a479 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 ost recently cod
1a47a 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ed instruction.
1a47b 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 Or.** insert a
1a47c 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 No-op and add th
1a47d 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 e comment to tha
1a47e 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f t new instructio
1a47f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65 n. This.** make
1a480 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 s the code easie
1a481 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 r to read during
1a482 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e debugging. Non
1a483 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e e of this happen
1a484 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63 s.** in a produc
1a485 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53 tion build..*/.S
1a486 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a487 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f id sqlite3VdbeCo
1a488 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 mment(Vdbe *p, c
1a489 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
1a48a 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
1a48b 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 ist ap;. if( !p
1a48c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
1a48d 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c ert( p->nOp>0 ||
1a48e 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 p->aOp==0 );.
1a48f 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d assert( p->aOp==
1a490 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 0 || p->aOp[p->n
1a491 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d Op-1].zComment==
1a492 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 0 || p->db->mall
1a493 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 ocFailed );. if
1a494 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 ( p->nOp ){.
1a495 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e char **pz = &p->
1a496 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 aOp[p->nOp-1].zC
1a497 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 omment;. va_s
1a498 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
1a499 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1a49a 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 Free(p->db, *pz)
1a49b 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 ;. *pz = sqli
1a49c 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 te3VMPrintf(p->d
1a49d 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
1a49e 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b . va_end(ap);
1a49f 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
1a4a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a4a1 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e e3VdbeNoopCommen
1a4a2 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 t(Vdbe *p, const
1a4a3 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
1a4a4 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
1a4a5 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29 20 72 ap;. if( !p ) r
1a4a6 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
1a4a7 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50 VdbeAddOp0(p, OP
1a4a8 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 _Noop);. assert
1a4a9 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d ( p->nOp>0 || p-
1a4aa 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 >aOp==0 );. ass
1a4ab 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c ert( p->aOp==0 |
1a4ac 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d | p->aOp[p->nOp-
1a4ad 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 1].zComment==0 |
1a4ae 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
1a4af 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
1a4b0 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 ->nOp ){. cha
1a4b1 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 r **pz = &p->aOp
1a4b2 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d [p->nOp-1].zComm
1a4b3 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 ent;. va_star
1a4b4 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
1a4b5 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a4b6 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 e(p->db, *pz);.
1a4b7 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 *pz = sqlite3
1a4b8 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 VMPrintf(p->db,
1a4b9 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
1a4ba 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
1a4bb 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e }.}.#endif /* N
1a4bc 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
1a4bd 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64 Return the opcod
1a4be 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64 e for a given ad
1a4bf 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 20 61 dress. If the a
1a4c0 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 74 68 ddress is -1, th
1a4c1 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 en.** return the
1a4c2 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 most recently i
1a4c3 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 2e 0a nserted opcode..
1a4c4 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 **.** If a memor
1a4c5 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
1a4c6 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 or has occurred
1a4c7 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 61 6c prior to the cal
1a4c8 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a 20 ling of this.**
1a4c9 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 61 20 routine, then a
1a4ca 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 75 6d pointer to a dum
1a4cb 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c 20 62 my VdbeOp will b
1a4cc 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 61 e returned. Tha
1a4cd 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 72 t opcode.** is r
1a4ce 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 eadable and writ
1a4cf 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68 61 73 able, but it has
1a4d0 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54 68 65 no effect. The
1a4d1 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64 75 6d return of a dum
1a4d2 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6c 6c my.** opcode all
1a4d3 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ows the call to
1a4d4 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74 69 6f continue functio
1a4d5 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f 4f 4d ning after a OOM
1a4d6 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74 0a 2a fault without.*
1a4d7 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68 65 63 * having to chec
1a4d8 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 k to see if the
1a4d9 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69 73 return from this
1a4da 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76 61 routine is a va
1a4db 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a lid pointer..**.
1a4dc 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23 69 66 ** About the #if
1a4dd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a4de 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c 6c 79 TRACE: Normally
1a4df 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
1a4e0 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 0a 2a s never called.*
1a4e1 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f 70 3e * unless p->nOp>
1a4e2 30 2e 20 20 54 68 69 73 20 69 73 20 62 65 63 61 0. This is beca
1a4e3 75 73 65 20 69 6e 20 74 68 65 20 61 62 73 65 6e use in the absen
1a4e4 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 se of SQLITE_OMI
1a4e5 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e 20 4f T_TRACE,.** an O
1a4e6 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75 63 74 P_Trace instruct
1a4e7 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 69 6e ion is always in
1a4e8 73 65 72 74 65 64 20 62 79 20 73 71 6c 69 74 65 serted by sqlite
1a4e9 33 56 64 62 65 47 65 74 28 29 20 61 73 20 73 6f 3VdbeGet() as so
1a4ea 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77 20 56 on as.** a new V
1a4eb 44 42 45 20 69 73 20 63 72 65 61 74 65 64 2e 20 DBE is created.
1a4ec 20 53 6f 20 77 65 20 61 72 65 20 66 72 65 65 20 So we are free
1a4ed 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f 20 70 to set addr to p
1a4ee 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75 74 0a ->nOp-1 without.
1a4ef 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64 6f 75 ** having to dou
1a4f0 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 6b ble-check to mak
1a4f1 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
1a4f2 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d 6e 65 result is non-ne
1a4f3 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a 20 69 gative. But.** i
1a4f4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1a4f5 41 43 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 ACE is defined,
1a4f6 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69 73 20 the OP_Trace is
1a4f7 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65 20 64 omitted and we d
1a4f8 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63 68 65 o need to.** che
1a4f9 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ck the value of
1a4fa 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72 65 20 p->nOp-1 before
1a4fb 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f 0a 53 continuing..*/.S
1a4fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
1a4fd 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62 beOp *sqlite3Vdb
1a4fe 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 2c 20 eGetOp(Vdbe *p,
1a4ff 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 74 61 int addr){. sta
1a500 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d 6d 79 tic VdbeOp dummy
1a501 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
1a502 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1a503 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 61 _INIT );. if( a
1a504 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 ddr<0 ){.#ifdef
1a505 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1a506 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 70 E. if( p->nOp
1a507 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26 64 75 ==0 ) return &du
1a508 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 mmy;.#endif.
1a509 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 addr = p->nOp -
1a50a 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 1;. }. assert(
1a50b 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 (addr>=0 && add
1a50c 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e r<p->nOp) || p->
1a50d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a50e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d );. if( p->db-
1a50f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1a510 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 75 6d . return &dum
1a511 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 my;. }else{.
1a512 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f 70 5b return &p->aOp[
1a513 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 addr];. }.}..#i
1a514 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1a515 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 E_OMIT_EXPLAIN)
1a516 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 || !defined(NDEB
1a517 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 UG) \. || de
1a518 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 fined(VDBE_PROFI
1a519 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 LE) || defined(S
1a51a 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
1a51b 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 ** Compute a str
1a51c 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 ing that describ
1a51d 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 es the P4 parame
1a51e 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 ter for an opcod
1a51f 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 e..** Use zTemp
1a520 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 for any required
1a521 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 temporary buffe
1a522 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 r space..*/.stat
1a523 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 ic char *display
1a524 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 P4(Op *pOp, char
1a525 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 *zTemp, int nTe
1a526 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 mp){. char *zP4
1a527 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 = zTemp;. asse
1a528 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b rt( nTemp>=20 );
1a529 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e . switch( pOp->
1a52a 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 p4type ){. ca
1a52b 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 se P4_KEYINFO_ST
1a52c 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 ATIC:. case P
1a52d 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 4_KEYINFO: {.
1a52e 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 int i, j;.
1a52f 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
1a530 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Info = pOp->p4.p
1a531 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 KeyInfo;. s
1a532 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a533 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b nTemp, zTemp, "k
1a534 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 eyinfo(%d", pKey
1a535 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20 Info->nField);.
1a536 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 i = sqlite3
1a537 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b Strlen30(zTemp);
1a538 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 . for(j=0;
1a539 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 j<pKeyInfo->nFie
1a53a 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ld; j++){.
1a53b 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
1a53c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f = pKeyInfo->aCo
1a53d 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 ll[j];. i
1a53e 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 f( pColl ){.
1a53f 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 int n = sq
1a540 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 lite3Strlen30(pC
1a541 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 oll->zName);.
1a542 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e if( i+n>n
1a543 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
1a544 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 memcpy(&zT
1a545 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 emp[i],",...",4)
1a546 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 ;. br
1a547 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
1a548 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 . zTemp
1a549 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 [i++] = ',';.
1a54a 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 if( pKeyI
1a54b 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 nfo->aSortOrder
1a54c 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f && pKeyInfo->aSo
1a54d 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 rtOrder[j] ){.
1a54e 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b zTemp[
1a54f 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 i++] = '-';.
1a550 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1a551 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b memcpy(&zTemp[
1a552 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 i], pColl->zName
1a553 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ,n+1);.
1a554 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 i += n;.
1a555 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e }else if( i+4<n
1a556 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 Temp-6 ){.
1a557 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d memcpy(&zTem
1a558 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a p[i],",nil",4);.
1a559 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 i += 4
1a55a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1a55b 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b }. zTemp[
1a55c 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 i++] = ')';.
1a55d 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a zTemp[i] = 0;.
1a55e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c assert( i<
1a55f 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 nTemp );. b
1a560 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1a561 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a case P4_COLLSEQ:
1a562 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 {. CollSeq
1a563 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 *pColl = pOp->p
1a564 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 4.pColl;. s
1a565 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a566 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 nTemp, zTemp, "c
1a567 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 ollseq(%.20s)",
1a568 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pColl->zName);.
1a569 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a56a 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 }. case P4_FU
1a56b 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 NCDEF: {. F
1a56c 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 uncDef *pDef = p
1a56d 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
1a56e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a56f 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a570 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 p, "%s(%d)", pDe
1a571 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e f->zName, pDef->
1a572 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 nArg);. bre
1a573 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1a574 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 se P4_INT64: {.
1a575 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1a576 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
1a577 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 mp, "%lld", *pOp
1a578 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 ->p4.pI64);.
1a579 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a57a 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 case P4_INT32
1a57b 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1a57c 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a57d 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 , zTemp, "%d", p
1a57e 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 Op->p4.i);.
1a57f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a580 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 case P4_REAL:
1a581 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1a582 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1a583 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 zTemp, "%.16g",
1a584 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b *pOp->p4.pReal);
1a585 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1a586 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a587 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d MEM: {. Mem
1a588 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 *pMem = pOp->p4
1a589 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 .pMem;. ass
1a58a 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
1a58b 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1a58c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d );. if( pM
1a58d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1a58e 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a Str ){. z
1a58f 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 P4 = pMem->z;.
1a590 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d }else if( pM
1a591 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1a592 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Int ){. s
1a593 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a594 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 nTemp, zTemp, "%
1a595 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 lld", pMem->u.i)
1a596 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1a597 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
1a598 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
1a599 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a59a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a59b 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d p, "%.16g", pMem
1a59c 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->r);. }els
1a59d 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1a59e 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 t( pMem->flags &
1a59f 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 MEM_Blob );.
1a5a0 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62 6c 6f zP4 = "(blo
1a5a1 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 b)";. }.
1a5a2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a5a3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a5a4 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1a5a5 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41 . case P4_VTA
1a5a6 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 B: {. sqlit
1a5a7 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d e3_vtab *pVtab =
1a5a8 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e pOp->p4.pVtab->
1a5a9 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c pVtab;. sql
1a5aa 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a5ab 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61 emp, zTemp, "vta
1a5ac 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 62 2c b:%p:%p", pVtab,
1a5ad 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 pVtab->pModule)
1a5ae 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a5af 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1a5b0 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 case P4_INTARRAY
1a5b1 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1a5b2 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a5b3 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 72 72 , zTemp, "intarr
1a5b4 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ay");. brea
1a5b5 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1a5b6 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 3a e P4_SUBPROGRAM:
1a5b7 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
1a5b8 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
1a5b9 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72 61 6d zTemp, "program
1a5ba 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ");. break;
1a5bb 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1a5bc 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 34 20 lt: {. zP4
1a5bd 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 = pOp->p4.z;.
1a5be 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b if( zP4==0 ){
1a5bf 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a . zP4 = z
1a5c0 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 7a 54 Temp;. zT
1a5c1 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 emp[0] = 0;.
1a5c2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1a5c3 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 20 29 assert( zP4!=0 )
1a5c4 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 3b 0a ;. return zP4;.
1a5c5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1a5c6 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56 Declare to the V
1a5c7 64 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72 dbe that the BTr
1a5c8 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d ee object at db-
1a5c9 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e >aDb[i] is used.
1a5ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a5cb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a5cc 64 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 dbeUsesBtree(Vdb
1a5cd 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 e *p, int i){.
1a5ce 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 int mask;. asse
1a5cf 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1a5d0 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69 >db->nDb && i<si
1a5d1 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20 zeof(u32)*8 );.
1a5d2 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 assert( i<(int)
1a5d3 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d sizeof(p->btreeM
1a5d4 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b ask)*8 );. mask
1a5d5 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a = ((u32)1)<<i;.
1a5d6 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d if( (p->btreeM
1a5d7 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 ask & mask)==0 )
1a5d8 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 {. p->btreeMa
1a5d9 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 sk |= mask;.
1a5da 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
1a5db 78 41 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d xArrayInsert(&p-
1a5dc 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e >aMutex, p->db->
1a5dd 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d aDb[i].pBt);. }
1a5de 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 .}...#if defined
1a5df 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c (VDBE_PROFILE) |
1a5e0 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
1a5e1 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 _DEBUG)./*.** Pr
1a5e2 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 int a single opc
1a5e3 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ode. This routi
1a5e4 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 ne is used for d
1a5e5 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
1a5e6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a5e7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a5e8 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 ePrintOp(FILE *p
1a5e9 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 Out, int pc, Op
1a5ea 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a *pOp){. char *z
1a5eb 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b P4;. char zPtr[
1a5ec 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 50];. static co
1a5ed 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
1a5ee 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 t1 = "%4d %-13s
1a5ef 25 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 %4d %4d %4d %-4s
1a5f0 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 %.2X %s\n";. i
1a5f1 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 f( pOut==0 ) pOu
1a5f2 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 t = stdout;. zP
1a5f3 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 4 = displayP4(pO
1a5f4 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 p, zPtr, sizeof(
1a5f5 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 zPtr));. fprint
1a5f6 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 f(pOut, zFormat1
1a5f7 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c , pc, . sql
1a5f8 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 ite3OpcodeName(p
1a5f9 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 Op->opcode), pOp
1a5fa 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 ->p1, pOp->p2, p
1a5fb 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 Op->p3, zP4, pOp
1a5fc 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c ->p5,.#ifdef SQL
1a5fd 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 ITE_DEBUG.
1a5fe 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 pOp->zComment ?
1a5ff 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 pOp->zComment :
1a600 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 "".#else. "
1a601 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 ".#endif. );.
1a602 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a fflush(pOut);.}.
1a603 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
1a604 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f lease an array o
1a605 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 f N Mem elements
1a606 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a607 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 releaseMemArray(
1a608 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a Mem *p, int N){.
1a609 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a if( p && N ){.
1a60a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 Mem *pEnd;.
1a60b 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
1a60c 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d p->db;. u8 m
1a60d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 alloc_failed = d
1a60e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b b->mallocFailed;
1a60f 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 . for(pEnd=&p
1a610 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b [N]; p<pEnd; p++
1a611 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1a612 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c (&p[1])==pEnd |
1a613 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e | p[0].db==p[1].
1a614 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 db );.. /*
1a615 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 This block is re
1a616 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 ally an inlined
1a617 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 version of sqlit
1a618 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1a619 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 (). ** that
1a61a 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 takes advantage
1a61b 20 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61 of the fact tha
1a61c 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c t the memory cel
1a61d 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 l value is .
1a61e 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 ** being set t
1a61f 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c o NULL after rel
1a620 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d easing any dynam
1a621 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 ic resources..
1a622 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1a623 54 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f The justificatio
1a624 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e n for duplicatin
1a625 67 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 g code is that a
1a626 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 ccording to .
1a627 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c ** callgrind,
1a628 20 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63 this causes a c
1a629 65 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 ertain test case
1a62a 20 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 to hit the CPU
1a62b 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 4.7 . ** pe
1a62c 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 rcent less (x86
1a62d 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 linux, gcc versi
1a62e 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 on 4.1.2, -O6) t
1a62f 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a han if . **
1a630 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 sqlite3MemRelea
1a631 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 se() were called
1a632 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 from here. With
1a633 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 -O2, this jumps
1a634 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 . ** to 6.6
1a635 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 percent. The te
1a636 73 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 st case is inser
1a637 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 ting 1000 rows i
1a638 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 nto a table .
1a639 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e ** with no in
1a63a 64 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 dexes using a si
1a63b 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e ngle prepared IN
1a63c 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 SERT statement,
1a63d 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a bind() . **
1a63e 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e and reset(). In
1a63f 73 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 serts are groupe
1a640 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 d into a transac
1a641 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 tion.. */.
1a642 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 if( p->flag
1a643 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 s&(MEM_Agg|MEM_D
1a644 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d 45 4d yn|MEM_Frame|MEM
1a645 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20 20 20 _RowSet) ){.
1a646 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1a647 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 emRelease(p);.
1a648 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d }else if( p-
1a649 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >zMalloc ){.
1a64a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a64b 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 e(db, p->zMalloc
1a64c 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d );. p->zM
1a64d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 alloc = 0;.
1a64e 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61 }.. p->fla
1a64f 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1a650 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c }. db->mal
1a651 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c locFailed = mall
1a652 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d oc_failed;. }.}
1a653 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
1a654 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 VdbeFrame objec
1a655 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65 6e t and its conten
1a656 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20 6f 62 ts. VdbeFrame ob
1a657 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61 6c 6c jects are.** all
1a658 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 ocated by the OP
1a659 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 _Program opcode
1a65a 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 in sqlite3VdbeEx
1a65b 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ec()..*/.SQLITE_
1a65c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a65d 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c ite3VdbeFrameDel
1a65e 65 74 65 28 56 64 62 65 46 72 61 6d 65 20 2a 70 ete(VdbeFrame *p
1a65f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 ){. int i;. Me
1a660 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65 46 72 m *aMem = VdbeFr
1a661 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56 64 62 ameMem(p);. Vdb
1a662 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 20 eCursor **apCsr
1a663 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20 2a 2a = (VdbeCursor **
1a664 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69 6c 64 )&aMem[p->nChild
1a665 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b Mem];. for(i=0;
1a666 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73 72 3b i<p->nChildCsr;
1a667 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
1a668 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 e3VdbeFreeCursor
1a669 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69 5d 29 (p->v, apCsr[i])
1a66a 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d ;. }. releaseM
1a66b 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20 70 2d emArray(aMem, p-
1a66c 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20 20 73 >nChildMem);. s
1a66d 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
1a66e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a 23 69 v->db, p);.}..#i
1a66f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1a670 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 T_EXPLAIN./*.**
1a671 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67 20 6f Give a listing o
1a672 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 69 6e f the program in
1a673 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 the virtual mac
1a674 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 hine..**.** The
1a675 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 68 65 interface is the
1a676 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33 same as sqlite3
1a677 56 64 62 65 45 78 65 63 28 29 2e 20 20 42 75 74 VdbeExec(). But
1a678 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 instead of.** r
1a679 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64 65 2c unning the code,
1a67a 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 it invokes the
1a67b 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f callback once fo
1a67c 72 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 r each instructi
1a67d 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 on..** This feat
1a67e 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 ure is used to i
1a67f 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 mplement "EXPLAI
1a680 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 N"..**.** When p
1a681 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 ->explain==1, ea
1a682 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 ch instruction i
1a683 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65 6e 0a s listed. When.
1a684 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 ** p->explain==2
1a685 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69 , only OP_Explai
1a686 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 61 n instructions a
1a687 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20 74 68 re listed and th
1a688 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e ese.** are shown
1a689 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 in a different
1a68a 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c format. p->expl
1a68b 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64 20 74 ain==2 is used t
1a68c 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 o implement.** E
1a68d 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 XPLAIN QUERY PLA
1a68e 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 N..*/.SQLITE_PRI
1a68f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1a690 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 62 65 VdbeList(. Vdbe
1a691 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 *p
1a692 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
1a693 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 52 E */.){. int nR
1a694 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ow;
1a695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a696 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
1a697 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75 72 6e f rows to return
1a698 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 20 3d */. int nSub =
1a699 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1a69a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1a69b 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64 62 65 mber of sub-vdbe
1a69c 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f s seen so far */
1a69d 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a . SubProgram **
1a69e 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 apSub = 0;
1a69f 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 /* Array
1a6a0 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20 2a 2f of sub-vdbes */
1a6a1 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d 20 30 . Mem *pSub = 0
1a6a2 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
1a6a3 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 = p->db;. int i
1a6a4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1a6a5 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 ITE_OK;. Mem *p
1a6a6 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 Mem = p->pResult
1a6a7 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 Set = &p->aMem[1
1a6a8 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d ];.. assert( p-
1a6a9 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 61 73 >explain );. as
1a6aa 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1a6ab 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 VDBE_MAGIC_RUN )
1a6ac 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e ;. assert( db->
1a6ad 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 magic==SQLITE_MA
1a6ae 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73 GIC_BUSY );. as
1a6af 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c sert( p->rc==SQL
1a6b0 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d ITE_OK || p->rc=
1a6b1 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 =SQLITE_BUSY ||
1a6b2 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f p->rc==SQLITE_NO
1a6b3 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65 MEM );.. /* Eve
1a6b4 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 6f 70 n though this op
1a6b5 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 code does not us
1a6b6 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 6e 67 e dynamic string
1a6b7 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 s for. ** the r
1a6b8 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f esult, result co
1a6b9 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65 lumns may become
1a6ba 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20 dynamic if the
1a6bb 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20 user calls. **
1a6bc 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1a6bd 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67 ext16(), causing
1a6be 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 a translation t
1a6bf 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e o UTF-16 encodin
1a6c0 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73 g.. */. releas
1a6c1 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20 eMemArray(pMem,
1a6c2 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 63 8);.. if( p->rc
1a6c3 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
1a6c4 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 {. /* This ha
1a6c5 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f ppens if a mallo
1a6c6 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c c() inside a cal
1a6c7 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c l to sqlite3_col
1a6c8 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 umn_text() or.
1a6c9 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c ** sqlite3_col
1a6ca 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 umn_text16() fai
1a6cb 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 62 2d led. */. db-
1a6cc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1a6cd 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
1a6ce 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
1a6cf 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
1a6d0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1a6d1 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c 20 rows that will
1a6d2 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 be returned by t
1a6d3 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c 41 49 his . ** EXPLAI
1a6d4 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 N program. */.
1a6d5 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 3b 0a nRow = p->nOp;.
1a6d6 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e if( p->explain
1a6d7 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75 62 20 ==1 ){. pSub
1a6d8 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b 0a 20 = &p->aMem[9];.
1a6d9 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66 6c 61 if( pSub->fla
1a6da 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 gs&MEM_Blob ){.
1a6db 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53 75 62 nSub = pSub
1a6dc 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62 65 2a ->n/sizeof(Vdbe*
1a6dd 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62 20 3d );. apSub =
1a6de 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 (SubProgram **)
1a6df 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d 0a 20 pSub->z;. }.
1a6e0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 for(i=0; i<nS
1a6e1 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ub; i++){.
1a6e2 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b 69 5d nRow += apSub[i]
1a6e3 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 7d ->nOp;. }. }
1a6e4 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d 20 .. do{. i =
1a6e5 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69 6c p->pc++;. }whil
1a6e6 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70 2d 3e e( i<nRow && p->
1a6e7 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 70 2d explain==2 && p-
1a6e8 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 21 3d >aOp[i].opcode!=
1a6e9 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a 20 20 OP_Explain );.
1a6ea 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b 0a 20 if( i>=nRow ){.
1a6eb 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1a6ec 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 E_OK;. rc = S
1a6ed 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 QLITE_DONE;. }e
1a6ee 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 lse if( db->u1.i
1a6ef 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 7b 0a sInterrupted ){.
1a6f0 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 p->rc = SQLI
1a6f1 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 TE_INTERRUPT;.
1a6f2 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1a6f3 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ROR;. sqlite3
1a6f4 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1a6f5 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1a6f6 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 70 sqlite3ErrStr(p
1a6f7 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73 65 7b ->rc));. }else{
1a6f8 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 . char *z;.
1a6f9 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 69 Op *pOp;. i
1a6fa 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b 0a 20 f( i<p->nOp ){.
1a6fb 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 pOp = &p->a
1a6fc 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 Op[i];. }else
1a6fd 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 {. int j;.
1a6fe 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e 4f 70 i -= p->nOp
1a6ff 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b ;. for(j=0;
1a700 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e 6e 4f i>=apSub[j]->nO
1a701 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 p; j++){.
1a702 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d 2d 3e i -= apSub[j]->
1a703 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nOp;. }.
1a704 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75 62 5b pOp = &apSub[
1a705 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 j]->aOp[i];.
1a706 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 }. if( p->exp
1a707 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 lain==1 ){.
1a708 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a709 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d EM_Int;. pM
1a70a 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1a70b 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 E_INTEGER;.
1a70c 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20 pMem->u.i = i;
1a70d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a70e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a70f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 * Program counte
1a710 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b r */. pMem+
1a711 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d +;. . pMem
1a712 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 ->flags = MEM_St
1a713 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d atic|MEM_Str|MEM
1a714 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 _Term;. pMe
1a715 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 m->z = (char*)sq
1a716 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 lite3OpcodeName(
1a717 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f pOp->opcode); /
1a718 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 * Opcode */.
1a719 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1a71a 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d z!=0 );. pM
1a71b 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 em->n = sqlite3S
1a71c 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 trlen30(pMem->z)
1a71d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 ;. pMem->ty
1a71e 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 pe = SQLITE_TEXT
1a71f 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e ;. pMem->en
1a720 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1a721 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a . pMem++;..
1a722 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 if( pOp->p
1a723 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f 4type==P4_SUBPRO
1a724 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 GRAM ){.
1a725 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e 53 75 int nByte = (nSu
1a726 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62 50 b+1)*sizeof(SubP
1a727 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 rogram*);.
1a728 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 int j;.
1a729 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53 75 62 for(j=0; j<nSub
1a72a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
1a72b 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d 3d 3d if( apSub[j]==
1a72c 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d pOp->p4.pProgram
1a72d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
1a72e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
1a72f 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c 49 54 j==nSub && SQLIT
1a730 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 E_OK==sqlite3Vdb
1a731 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c 20 6e eMemGrow(pSub, n
1a732 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 Byte, 1) ){.
1a733 20 20 20 20 20 20 61 70 53 75 62 20 3d 20 28 53 apSub = (S
1a734 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70 53 75 ubProgram **)pSu
1a735 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20 20 20 b->z;.
1a736 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20 3d 20 apSub[nSub++] =
1a737 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d pOp->p4.pProgram
1a738 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 75 62 ;. pSub
1a739 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 ->flags |= MEM_B
1a73a 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 lob;. p
1a73b 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a 73 69 Sub->n = nSub*si
1a73c 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a zeof(SubProgram*
1a73d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1a73e 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
1a73f 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1a740 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d M_Int;. pMem-
1a741 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 >u.i = pOp->p1;
1a742 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a743 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 2a /* P1 *
1a744 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 /. pMem->type
1a745 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
1a746 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a R;. pMem++;..
1a747 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1a748 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 = MEM_Int;. p
1a749 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e Mem->u.i = pOp->
1a74a 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p2;
1a74b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a74c 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e P2 */. pMem->
1a74d 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e type = SQLITE_IN
1a74e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b TEGER;. pMem+
1a74f 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 +;.. if( p->e
1a750 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 xplain==1 ){.
1a751 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a752 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1a753 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d pMem->u.i = pOp-
1a754 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 >p3;
1a755 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a756 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 P3 */. pMe
1a757 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a758 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 _INTEGER;.
1a759 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 pMem++;. }..
1a75a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
1a75b 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
1a75c 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 20 20 32, 0) ){
1a75d 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 /* P4 */.
1a75e 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 assert( p->d
1a75f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a760 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1a761 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a762 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c }. pMem->fl
1a763 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 ags = MEM_Dyn|ME
1a764 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
1a765 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61 79 50 z = displayP
1a766 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 4(pOp, pMem->z,
1a767 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 21 3d 32);. if( z!=
1a768 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 pMem->z ){.
1a769 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a76a 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d etStr(pMem, z, -
1a76b 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 1, SQLITE_UTF8,
1a76c 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0);. }else{.
1a76d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 assert( pMe
1a76e 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 m->z!=0 );.
1a76f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 pMem->n = sqlit
1a770 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d e3Strlen30(pMem-
1a771 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d >z);. pMem-
1a772 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 >enc = SQLITE_UT
1a773 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d F8;. }. pM
1a774 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1a775 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d E_TEXT;. pMem
1a776 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e ++;.. if( p->
1a777 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 explain==1 ){.
1a778 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1a779 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
1a77a 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 4, 0) ){.
1a77b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d assert( p->db-
1a77c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
1a77d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1a77e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a77f 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d }. pMem
1a780 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 ->flags = MEM_Dy
1a781 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 n|MEM_Str|MEM_Te
1a782 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e rm;. pMem->
1a783 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 71 6c n = 2;. sql
1a784 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c ite3_snprintf(3,
1a785 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 pMem->z, "%.2x"
1a786 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a , pOp->p5); /*
1a787 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 P5 */. pMe
1a788 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a789 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 _TEXT;. pMe
1a78a 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
1a78b 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d UTF8;. pMem
1a78c 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20 53 51 ++;. .#ifdef SQ
1a78d 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 LITE_DEBUG.
1a78e 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 if( pOp->zComme
1a78f 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d nt ){. pM
1a790 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a791 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 Str|MEM_Term;.
1a792 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 pMem->z =
1a793 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 pOp->zComment;.
1a794 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d pMem->n =
1a795 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a796 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 (pMem->z);.
1a797 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 pMem->enc = S
1a798 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 QLITE_UTF8;.
1a799 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1a79a 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1a79b 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 }else.#endif
1a79c 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 . {.
1a79d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a79e 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 EM_Null;
1a79f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a7a0 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 * Comment */.
1a7a1 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 pMem->type
1a7a2 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 = SQLITE_NULL;.
1a7a3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
1a7a4 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 p->nResColumn
1a7a5 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c = 8 - 5*(p->expl
1a7a6 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 ain-1);. p->r
1a7a7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1a7a8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 rc = SQLITE_R
1a7a9 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OW;. }. return
1a7aa 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a rc;.}.#endif /*
1a7ab 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 SQLITE_OMIT_EXP
1a7ac 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 LAIN */..#ifdef
1a7ad 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
1a7ae 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53 51 4c ** Print the SQL
1a7af 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 that was used t
1a7b0 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56 44 42 o generate a VDB
1a7b1 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 E program..*/.SQ
1a7b2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a7b3 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 d sqlite3VdbePri
1a7b4 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a ntSql(Vdbe *p){.
1a7b5 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e int nOp = p->n
1a7b6 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f Op;. VdbeOp *pO
1a7b7 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 p;. if( nOp<1 )
1a7b8 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d return;. pOp =
1a7b9 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 &p->aOp[0];. i
1a7ba 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1a7bb 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d OP_Trace && pOp-
1a7bc 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 >p4.z!=0 ){.
1a7bd 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 const char *z =
1a7be 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 pOp->p4.z;. w
1a7bf 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
1a7c0 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a pace(*z) ) z++;.
1a7c1 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 4c 3a printf("SQL:
1a7c2 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 [%s]\n", z);.
1a7c3 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 }.}.#endif..#if
1a7c4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1a7c5 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20 64 OMIT_TRACE) && d
1a7c6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
1a7c7 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a ABLE_IOTRACE)./*
1a7c8 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 4f 54 .** Print an IOT
1a7c9 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 68 6f RACE message sho
1a7ca 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 6e 74 wing SQL content
1a7cb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a7cc 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a7cd 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 VdbeIOTraceSql(V
1a7ce 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e dbe *p){. int n
1a7cf 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 Op = p->nOp;. V
1a7d0 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 dbeOp *pOp;. if
1a7d1 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 ( sqlite3IoTrace
1a7d2 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1a7d3 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 if( nOp<1 ) retu
1a7d4 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e rn;. pOp = &p->
1a7d5 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f aOp[0];. if( pO
1a7d6 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 p->opcode==OP_Tr
1a7d7 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a ace && pOp->p4.z
1a7d8 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 !=0 ){. int i
1a7d9 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 7a 5b , j;. char z[
1a7da 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 1000];. sqlit
1a7db 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
1a7dc 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20 of(z), z, "%s",
1a7dd 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 pOp->p4.z);.
1a7de 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74 65 33 for(i=0; sqlite3
1a7df 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 Isspace(z[i]); i
1a7e0 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28 6a 3d ++){}. for(j=
1a7e1 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 0; z[i]; i++){.
1a7e2 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1a7e3 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20 29 7b Isspace(z[i]) ){
1a7e4 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69 . if( z[i
1a7e5 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 20 20 -1]!=' ' ){.
1a7e6 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 z[j++] = '
1a7e7 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ';. }.
1a7e8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a7e9 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d z[j++] = z[i]
1a7ea 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1a7eb 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 z[j] = 0;.
1a7ec 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 sqlite3IoTrace
1a7ed 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b ("SQL %s\n", z);
1a7ee 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
1a7ef 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 !SQLITE_OMIT_TR
1a7f0 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f 45 4e ACE && SQLITE_EN
1a7f1 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0a ABLE_IOTRACE */.
1a7f2 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
1a7f3 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66 69 78 space from a fix
1a7f4 65 64 20 73 69 7a 65 20 62 75 66 66 65 72 2e 20 ed size buffer.
1a7f5 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e 74 20 Make *pp point
1a7f6 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 to the.** alloca
1a7f7 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e 6f 74 ted space. (Not
1a7f8 65 3a 20 20 70 70 20 69 73 20 61 20 63 68 61 72 e: pp is a char
1a7f9 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 * rather than a
1a7fa 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72 void** to.** wor
1a7fb 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6f 69 k around the poi
1a7fc 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20 72 75 nter aliasing ru
1a7fd 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70 70 20 les of C.) *pp
1a7fe 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c 6c 79 should initially
1a7ff 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20 49 66 .** be zero. If
1a800 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65 72 6f *pp is not zero
1a801 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
1a802 74 20 74 68 65 20 73 70 61 63 65 20 68 61 73 20 t the space has
1a803 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 already.** been
1a804 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 allocated and th
1a805 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1a806 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 noop..**.** nByt
1a807 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 e is the number
1a808 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
1a809 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 e needed..**.**
1a80a 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20 74 6f *ppFrom point to
1a80b 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 available space
1a80c 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e 74 73 and pEnd points
1a80d 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 to the end of t
1a80e 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 he.** available
1a80f 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e space..**.** *pn
1a810 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e 74 65 Byte is a counte
1a811 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 r of the number
1a812 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
1a813 65 20 74 68 61 74 20 68 61 76 65 20 66 61 69 6c e that have fail
1a814 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 ed.** to allocat
1a815 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 e. If there is
1a816 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 insufficient spa
1a817 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20 74 6f ce in *ppFrom to
1a818 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 satisfy the.**
1a819 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20 69 6e request, then in
1a81a 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20 crement *pnByte
1a81b 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 by the amount of
1a81c 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a 2a 2f the request..*/
1a81d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c .static void all
1a81e 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61 72 20 ocSpace(. char
1a81f 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *pp,
1a820 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74 20 2a /* IN/OUT: Set *
1a821 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 pp to point to a
1a822 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
1a823 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 */. int nByte,
1a824 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1a825 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
1a826 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38 allocate */. u8
1a827 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20 20 20 **ppFrom,
1a828 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c /* IN/OUT: Al
1a829 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70 70 46 locate from *ppF
1a82a 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e rom */. u8 *pEn
1a82b 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a d, /*
1a82c 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20 62 79 Pointer to 1 by
1a82d 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 te past the end
1a82e 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66 66 65 of *ppFrom buffe
1a82f 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 r */. int *pnBy
1a830 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 te /* I
1a831 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e f allocation can
1a832 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69 6e 63 not be made, inc
1a833 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a rement *pnByte *
1a834 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 45 /.){. assert( E
1a835 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
1a836 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a ENT(*ppFrom) );.
1a837 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a 2a 29 if( (*(void**)
1a838 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 42 pp)==0 ){. nB
1a839 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 yte = ROUND8(nBy
1a83a 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26 28 2a te);. if( &(*
1a83b 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d 20 3c ppFrom)[nByte] <
1a83c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 = pEnd ){.
1a83d 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20 28 76 *(void**)pp = (v
1a83e 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b 0a 20 oid *)*ppFrom;.
1a83f 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b 3d 20 *ppFrom +=
1a840 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65 nByte;. }else
1a841 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 20 {. *pnByte
1a842 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a += nByte;. }.
1a843 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 }.}../*.** Pre
1a844 70 61 72 65 20 61 20 76 69 72 74 75 61 6c 20 6d pare a virtual m
1a845 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 63 75 achine for execu
1a846 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 76 6f tion. This invo
1a847 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75 63 68 lves things such
1a848 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74 69 6e .** as allocatin
1a849 67 20 73 74 61 63 6b 20 73 70 61 63 65 20 61 6e g stack space an
1a84a 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 d initializing t
1a84b 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 he program count
1a84c 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 er..** After the
1a84d 20 56 44 42 45 20 68 61 73 20 62 65 20 70 72 65 VDBE has be pre
1a84e 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62 65 20 pped, it can be
1a84f 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e 65 20 executed by one
1a850 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73 or more.** calls
1a851 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 45 to sqlite3VdbeE
1a852 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 xec(). .**.** T
1a853 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
1a854 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20 56 44 way to move a VD
1a855 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 BE from VDBE_MAG
1a856 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20 56 44 IC_INIT to.** VD
1a857 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a 2a 2a BE_MAGIC_RUN..**
1a858 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1a859 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 n may be called
1a85a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 6f more than once o
1a85b 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72 74 75 n a single virtu
1a85c 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 20 54 al machine..** T
1a85d 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 69 73 he first call is
1a85e 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f 6d 70 made while comp
1a85f 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 iling the SQL st
1a860 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65 71 75 atement. Subsequ
1a861 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61 72 65 ent.** calls are
1a862 20 6d 61 64 65 20 61 73 20 70 61 72 74 20 6f 66 made as part of
1a863 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 the process of
1a864 72 65 73 65 74 74 69 6e 67 20 61 20 73 74 61 74 resetting a stat
1a865 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 ement to be.** r
1a866 65 2d 65 78 65 63 75 74 65 64 20 28 66 72 6f 6d e-executed (from
1a867 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
1a868 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54 68 65 e3_reset()). The
1a869 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e 43 75 nVar, nMem, nCu
1a86a 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69 73 45 rsor .** and isE
1a86b 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74 65 72 xplain parameter
1a86c 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73 73 65 s are only passe
1a86d 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 73 d correct values
1a86e 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a the first time.
1a86f 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ** the function
1a870 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20 73 75 is called. On su
1a871 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 20 bsequent calls,
1a872 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 from sqlite3_res
1a873 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20 69 73 et(), nVar.** is
1a874 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64 20 6e passed -1 and n
1a875 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61 6e 64 Mem, nCursor and
1a876 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65 20 61 isExplain are a
1a877 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f 2e 0a ll passed zero..
1a878 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a879 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a87a 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 20 56 beMakeReady(. V
1a87b 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 dbe *p,
1a87c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a87d 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 20 69 The VDBE */. i
1a87e 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20 20 20 nt nVar,
1a87f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a880 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 73 Number of '?' s
1a881 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 ee in the SQL st
1a882 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 atement */. int
1a883 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20 20 20 nMem,
1a884 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1a885 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
1a886 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 cells to allocat
1a887 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 73 e */. int nCurs
1a888 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 or,
1a889 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1a88a 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20 61 6c of cursors to al
1a88b 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 locate */. int
1a88c 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 nArg,
1a88d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
1a88e 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1a88f 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f 67 72 args in SubProgr
1a890 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 ams */. int isE
1a891 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20 20 20 xplain,
1a892 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1a893 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 6b if the EXPLAIN k
1a894 65 79 77 6f 72 64 73 20 69 73 20 70 72 65 73 65 eywords is prese
1a895 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 73 nt */. int uses
1a896 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20 20 20 StmtJournal
1a897 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
1a898 6f 20 73 65 74 20 56 64 62 65 2e 75 73 65 73 53 o set Vdbe.usesS
1a899 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 29 7b tmtJournal */.){
1a89a 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
1a89b 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1a89c 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 .. assert( p!=0
1a89d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1a89e 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a89f 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a IC_INIT );.. /*
1a8a0 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 There should be
1a8a1 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 at least one op
1a8a2 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 code.. */. ass
1a8a3 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b ert( p->nOp>0 );
1a8a4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d .. /* Set the m
1a8a5 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 agic to VDBE_MAG
1a8a6 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 IC_RUN sooner ra
1a8a7 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e ther than later.
1a8a8 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d */. p->magic =
1a8a9 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b VDBE_MAGIC_RUN;
1a8aa 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 .. /* For each
1a8ab 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c cursor required,
1a8ac 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 also allocate a
1a8ad 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 memory cell. Me
1a8ae 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 mory. ** cells
1a8af 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 (nMem+1-nCursor)
1a8b0 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 ..nMem, inclusiv
1a8b1 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 e, will never be
1a8b2 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 used by. ** th
1a8b3 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 e vdbe program.
1a8b4 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 Instead they are
1a8b5 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 used to allocat
1a8b6 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a e space for. **
1a8b7 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 VdbeCursor/BtCu
1a8b8 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e rsor structures.
1a8b9 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d The blob of mem
1a8ba 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 ory associated w
1a8bb 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 ith . ** cursor
1a8bc 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 0 is stored in
1a8bd 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d memory cell nMem
1a8be 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e . Memory cell (n
1a8bf 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 Mem-1). ** stor
1a8c0 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d es the blob of m
1a8c1 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 emory associated
1a8c2 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 with cursor 1,
1a8c3 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 etc.. **. ** S
1a8c4 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 ee also: allocat
1a8c5 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a eCursor().. */.
1a8c6 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f nMem += nCurso
1a8c7 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 r;.. /* Allocat
1a8c8 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f e space for memo
1a8c9 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53 51 ry registers, SQ
1a8ca 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44 42 L variables, VDB
1a8cb 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a 20 E cursors and .
1a8cc 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f 20 ** an array to
1a8cd 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e 63 marshal SQL func
1a8ce 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 69 tion arguments i
1a8cf 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 n. This is only
1a8d0 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20 66 69 done the. ** fi
1a8d1 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 66 75 rst time this fu
1a8d2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1a8d3 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56 44 42 for a given VDB
1a8d4 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74 20 69 E, not when it i
1a8d5 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63 61 6c s. ** being cal
1a8d6 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 led from sqlite3
1a8d7 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65 73 65 _reset() to rese
1a8d8 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 t the virtual ma
1a8d9 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 chine.. */. if
1a8da 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 41 4c 57 ( nVar>=0 && ALW
1a8db 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 AYS(db->mallocFa
1a8dc 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 20 20 20 iled==0) ){.
1a8dd 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 38 20 2a u8 *zCsr = (u8 *
1a8de 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 5d )&p->aOp[p->nOp]
1a8df 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e 64 20 3d ;. u8 *zEnd =
1a8e0 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 (u8 *)&p->aOp[p
1a8e1 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a 20 20 20 ->nOpAlloc];.
1a8e2 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 int nByte;.
1a8e3 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 resolveP2Values(
1a8e4 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 20 20 70 p, &nArg);. p
1a8e5 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 ->usesStmtJourna
1a8e6 6c 20 3d 20 28 75 38 29 75 73 65 73 53 74 6d 74 l = (u8)usesStmt
1a8e7 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69 66 28 Journal;. if(
1a8e8 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d isExplain && nM
1a8e9 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e em<10 ){. n
1a8ea 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a Mem = 10;. }.
1a8eb 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c memset(zCsr,
1a8ec 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 0, zEnd-zCsr);.
1a8ed 20 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a 43 73 zCsr += (zCs
1a8ee 72 20 2d 20 28 75 38 2a 29 30 29 26 37 3b 0a 20 r - (u8*)0)&7;.
1a8ef 20 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 assert( EIGHT
1a8f0 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
1a8f1 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20 20 64 6f zCsr) );.. do
1a8f2 20 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d {. nByte =
1a8f3 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 0;. allocS
1a8f4 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e pace((char*)&p->
1a8f5 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f aMem, nMem*sizeo
1a8f6 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a f(Mem), &zCsr, z
1a8f7 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 End, &nByte);.
1a8f8 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 allocSpace((
1a8f9 63 68 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 char*)&p->aVar,
1a8fa 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 nVar*sizeof(Mem)
1a8fb 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 , &zCsr, zEnd, &
1a8fc 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c nByte);. al
1a8fd 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 locSpace((char*)
1a8fe 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a &p->apArg, nArg*
1a8ff 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a sizeof(Mem*), &z
1a900 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 Csr, zEnd, &nByt
1a901 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 e);. allocS
1a902 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e pace((char*)&p->
1a903 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 azVar, nVar*size
1a904 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 of(char*), &zCsr
1a905 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b , zEnd, &nByte);
1a906 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 . allocSpac
1a907 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 e((char*)&p->apC
1a908 73 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 sr, .
1a909 20 20 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 nCursor*si
1a90a 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a zeof(VdbeCursor*
1a90b 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 ), &zCsr, zEnd,
1a90c 26 6e 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a &nByte. );.
1a90d 20 20 20 20 20 20 69 66 28 20 6e 42 79 74 65 20 if( nByte
1a90e 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 ){. p->pF
1a90f 72 65 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ree = sqlite3DbM
1a910 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 allocZero(db, nB
1a911 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 yte);. }.
1a912 20 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46 zCsr = p->pF
1a913 72 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20 ree;. zEnd
1a914 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a = &zCsr[nByte];.
1a915 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 }while( nByt
1a916 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 e && !db->malloc
1a917 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70 Failed );.. p
1a918 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 ->nCursor = (u16
1a919 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 )nCursor;. if
1a91a 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20 20 20 ( p->aVar ){.
1a91b 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 79 6e p->nVar = (yn
1a91c 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20 20 20 Var)nVar;.
1a91d 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b for(n=0; n<nVar;
1a91e 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 n++){. p
1a91f 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 ->aVar[n].flags
1a920 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
1a921 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 p->aVar[n].d
1a922 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a b = db;. }.
1a923 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d }. if( p-
1a924 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 >aMem ){. p
1a925 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 ->aMem--;
1a926 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a927 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 * aMem[] goes fr
1a928 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 om 1..nMem */.
1a929 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d p->nMem = nM
1a92a 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1a92b 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 /* not
1a92c 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 from 0..nMem-1
1a92d 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 */. for(n=1
1a92e 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b ; n<=nMem; n++){
1a92f 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d . p->aMem
1a930 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f [n].flags = MEM_
1a931 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d Null;. p-
1a932 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 >aMem[n].db = db
1a933 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1a934 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
1a935 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d E_DEBUG. for(n=
1a936 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 1; n<p->nMem; n+
1a937 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
1a938 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 p->aMem[n].db==d
1a939 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a b );. }.#endif.
1a93a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 . p->pc = -1;.
1a93b 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
1a93c 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 OK;. p->errorAc
1a93d 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b tion = OE_Abort;
1a93e 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d . p->explain |=
1a93f 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d isExplain;. p-
1a940 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
1a941 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 GIC_RUN;. p->nC
1a942 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e hange = 0;. p->
1a943 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 cacheCtr = 1;.
1a944 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
1a945 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 ormat = 255;. p
1a946 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 ->iStatement = 0
1a947 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ;.#ifdef VDBE_PR
1a948 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e OFILE. {. in
1a949 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
1a94a 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 ; i<p->nOp; i++)
1a94b 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 {. p->aOp[i
1a94c 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 ].cnt = 0;.
1a94d 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 p->aOp[i].cycle
1a94e 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d s = 0;. }. }
1a94f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
1a950 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75 Close a VDBE cu
1a951 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65 rsor and release
1a952 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63 all the resourc
1a953 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a es that cursor .
1a954 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f ** happens to ho
1a955 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ld..*/.SQLITE_PR
1a956 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a957 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 e3VdbeFreeCursor
1a958 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75 (Vdbe *p, VdbeCu
1a959 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 rsor *pCx){. if
1a95a 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 ( pCx==0 ){.
1a95b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 return;. }. if
1a95c 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 ( pCx->pBt ){.
1a95d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
1a95e 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 ose(pCx->pBt);.
1a95f 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 /* The pCx->p
1a960 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 Cursor will be c
1a961 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c lose automatical
1a962 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 ly, if it exists
1a963 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 , by. ** the
1a964 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 call above. */.
1a965 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e }else if( pCx->
1a966 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 pCursor ){. s
1a967 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
1a968 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 Cursor(pCx->pCur
1a969 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 sor);. }.#ifnde
1a96a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1a96b 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 RTUALTABLE. if(
1a96c 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f pCx->pVtabCurso
1a96d 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
1a96e 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
1a96f 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d tabCursor = pCx-
1a970 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 >pVtabCursor;.
1a971 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1a972 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 module *pModule
1a973 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a = pCx->pModule;.
1a974 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
1a975 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f hod = 1;. (vo
1a976 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1a977 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 Off(p->db);.
1a978 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 pModule->xClose(
1a979 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 pVtabCursor);.
1a97a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1a97b 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a afetyOn(p->db);.
1a97c 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 p->inVtabMet
1a97d 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e hod = 0;. }.#en
1a97e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 dif.}../*.** Cop
1a97f 79 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f y the values sto
1a980 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 46 red in the VdbeF
1a981 72 61 6d 65 20 73 74 72 75 63 74 75 72 65 20 74 rame structure t
1a982 6f 20 69 74 73 20 56 64 62 65 2e 20 54 68 69 73 o its Vdbe. This
1a983 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 66 6f 72 .** is used, for
1a984 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61 example, when a
1a985 20 74 72 69 67 67 65 72 20 73 75 62 2d 70 72 6f trigger sub-pro
1a986 67 72 61 6d 20 69 73 20 68 61 6c 74 65 64 20 74 gram is halted t
1a987 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20 63 6f 6e o restore.** con
1a988 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d 61 69 6e trol to the main
1a989 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c program..*/.SQL
1a98a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a98b 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 sqlite3VdbeFrame
1a98c 52 65 73 74 6f 72 65 28 56 64 62 65 46 72 61 6d Restore(VdbeFram
1a98d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20 20 56 64 e *pFrame){. Vd
1a98e 62 65 20 2a 76 20 3d 20 70 46 72 61 6d 65 2d 3e be *v = pFrame->
1a98f 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 70 46 v;. v->aOp = pF
1a990 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d 3e rame->aOp;. v->
1a991 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4f nOp = pFrame->nO
1a992 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20 70 p;. v->aMem = p
1a993 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20 76 Frame->aMem;. v
1a994 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d ->nMem = pFrame-
1a995 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43 73 >nMem;. v->apCs
1a996 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70 43 73 r = pFrame->apCs
1a997 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f 72 20 r;. v->nCursor
1a998 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f = pFrame->nCurso
1a999 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61 73 74 r;. v->db->last
1a99a 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65 2d 3e Rowid = pFrame->
1a99b 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76 2d 3e lastRowid;. v->
1a99c 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61 6d 65 nChange = pFrame
1a99d 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72 65 74 ->nChange;. ret
1a99e 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63 3b 0a urn pFrame->pc;.
1a99f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
1a9a0 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a ll cursors..**.*
1a9a1 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65 20 61 * Also release a
1a9a2 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 ny dynamic memor
1a9a3 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 56 4d y held by the VM
1a9a4 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 4d 65 in the Vdbe.aMe
1a9a5 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c m memory .** cel
1a9a6 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20 69 73 l array. This is
1a9a7 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74 68 necessary as th
1a9a8 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 e memory cell ar
1a9a9 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 0a ray may contain.
1a9aa 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 56 ** pointers to V
1a9ab 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 dbeFrame objects
1a9ac 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e 20 74 , which may in t
1a9ad 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e urn contain poin
1a9ae 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 ters to.** open
1a9af 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74 cursors..*/.stat
1a9b0 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41 6c 6c ic void closeAll
1a9b1 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a 70 29 Cursors(Vdbe *p)
1a9b2 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d {. if( p->pFram
1a9b3 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46 72 61 e ){. VdbeFra
1a9b4 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e me *pFrame = p->
1a9b5 70 46 72 61 6d 65 3b 0a 20 20 20 20 66 6f 72 28 pFrame;. for(
1a9b6 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 pFrame=p->pFrame
1a9b7 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e ; pFrame->pParen
1a9b8 74 3b 20 70 46 72 61 6d 65 3d 70 46 72 61 6d 65 t; pFrame=pFrame
1a9b9 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 ->pParent);.
1a9ba 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 sqlite3VdbeFrame
1a9bb 52 65 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b Restore(pFrame);
1a9bc 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72 61 6d 65 . }. p->pFrame
1a9bd 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 61 6d = 0;. p->nFram
1a9be 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 2d e = 0;.. if( p-
1a9bf 3e 61 70 43 73 72 20 29 7b 0a 20 20 20 20 69 6e >apCsr ){. in
1a9c0 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
1a9c1 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 ; i<p->nCursor;
1a9c2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 64 62 65 i++){. Vdbe
1a9c3 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70 2d 3e Cursor *pC = p->
1a9c4 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 20 20 20 apCsr[i];.
1a9c5 69 66 28 20 70 43 20 29 7b 0a 20 20 20 20 20 20 if( pC ){.
1a9c6 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
1a9c7 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a eCursor(p, pC);.
1a9c8 20 20 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 p->apCsr
1a9c9 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d [i] = 0;. }
1a9ca 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1a9cb 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 p->aMem ){.
1a9cc 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 releaseMemArray(
1a9cd 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e &p->aMem[1], p->
1a9ce 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a nMem);. }.}../*
1a9cf 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65 .** Clean up the
1a9d0 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75 74 VM after execut
1a9d1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion..**.** This
1a9d2 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 74 routine will aut
1a9d3 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 omatically close
1a9d4 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c 69 any cursors, li
1a9d5 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 sts, and/or.** s
1a9d6 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72 65 orters that were
1a9d7 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 20 left open. It
1a9d8 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68 65 also deletes the
1a9d9 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61 values of.** va
1a9da 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 61 riables in the a
1a9db 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a Var[] array..*/.
1a9dc 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61 static void Clea
1a9dd 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 nup(Vdbe *p){.
1a9de 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1a9df 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c >db;..#ifdef SQL
1a9e0 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 45 ITE_DEBUG. /* E
1a9e1 78 65 63 75 74 65 20 61 73 73 65 72 74 28 29 20 xecute assert()
1a9e2 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 65 6e statements to en
1a9e3 73 75 72 65 20 74 68 61 74 20 74 68 65 20 56 64 sure that the Vd
1a9e4 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e 64 20 0a be.apCsr[] and .
1a9e5 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65 6d 5b 5d ** Vdbe.aMem[]
1a9e6 20 61 72 72 61 79 73 20 68 61 76 65 20 61 6c 72 arrays have alr
1a9e7 65 61 64 79 20 62 65 65 6e 20 63 6c 65 61 6e 65 eady been cleane
1a9e8 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 d up. */. int
1a9e9 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
1a9ea 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 p->nCursor; i++)
1a9eb 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 assert( p->apCs
1a9ec 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70 43 73 72 r==0 || p->apCsr
1a9ed 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 [i]==0 );. for(
1a9ee 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b i=1; i<=p->nMem;
1a9ef 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d i++) assert( p-
1a9f0 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70 2d 3e 61 >aMem==0 || p->a
1a9f1 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 Mem[i].flags==ME
1a9f2 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e 64 69 66 M_Null );.#endif
1a9f3 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 .. sqlite3DbFre
1a9f4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1a9f5 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 );. p->zErrMsg
1a9f6 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c = 0;. p->pResul
1a9f7 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a tSet = 0;.}../*.
1a9f8 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
1a9f9 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 r of result colu
1a9fa 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 mns that will be
1a9fb 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
1a9fc 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 s SQL.** stateme
1a9fd 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 nt. This is now
1a9fe 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 set at compile t
1a9ff 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e ime, rather than
1aa00 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 during.** execu
1aa01 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65 tion of the vdbe
1aa02 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 program so that
1aa03 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1aa04 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 count() can.** b
1aa05 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 e called on an S
1aa06 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66 QL statement bef
1aa07 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ore sqlite3_step
1aa08 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
1aa09 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1aa0a 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
1aa0b 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 (Vdbe *p, int nR
1aa0c 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d esColumn){. Mem
1aa0d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e *pColName;. in
1aa0e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a t n;. sqlite3 *
1aa0f 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 db = p->db;.. r
1aa10 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
1aa11 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e ->aColName, p->n
1aa12 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d ResColumn*COLNAM
1aa13 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 E_N);. sqlite3D
1aa14 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f bFree(db, p->aCo
1aa15 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 lName);. n = nR
1aa16 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 esColumn*COLNAME
1aa17 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c _N;. p->nResCol
1aa18 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52 65 73 43 umn = (u16)nResC
1aa19 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c olumn;. p->aCol
1aa1a 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 Name = pColName
1aa1b 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44 = (Mem*)sqlite3D
1aa1c 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1aa1d 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b sizeof(Mem)*n );
1aa1e 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 . if( p->aColNa
1aa1f 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a me==0 ) return;.
1aa20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 while( n-- > 0
1aa21 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 ){. pColName
1aa22 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 ->flags = MEM_Nu
1aa23 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 ll;. pColName
1aa24 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
1aa25 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 pColName++;.
1aa26 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 }.}../*.** Set t
1aa27 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 he name of the i
1aa28 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 dx'th column to
1aa29 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 be returned by t
1aa2a 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1aa2b 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20 ..** zName must
1aa2c 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 be a pointer to
1aa2d 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 a nul terminated
1aa2e 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 string..**.** T
1aa2f 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 his call must be
1aa30 20 6d 61 64 65 20 61 66 74 65 72 20 61 20 63 61 made after a ca
1aa31 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ll to sqlite3Vdb
1aa32 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a eSetNumCols()..*
1aa33 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 70 *.** The final p
1aa34 61 72 61 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20 arameter, xDel,
1aa35 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 must be one of S
1aa36 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53 QLITE_DYNAMIC, S
1aa37 51 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 QLITE_STATIC.**
1aa38 6f 72 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 or SQLITE_TRANSI
1aa39 45 4e 54 2e 20 49 66 20 69 74 20 69 73 20 53 51 ENT. If it is SQ
1aa3a 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68 LITE_DYNAMIC, th
1aa3b 65 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 6f en the buffer po
1aa3c 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a inted.** to by z
1aa3d 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20 66 72 65 Name will be fre
1aa3e 65 64 20 62 79 20 73 71 6c 69 74 65 33 44 62 46 ed by sqlite3DbF
1aa3f 72 65 65 28 29 20 77 68 65 6e 20 74 68 65 20 76 ree() when the v
1aa40 64 62 65 20 69 73 20 64 65 73 74 72 6f 79 65 64 dbe is destroyed
1aa41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1aa42 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1aa43 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 dbeSetColName(.
1aa44 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 Vdbe *p,
1aa45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa46 20 20 2f 2a 20 56 64 62 65 20 62 65 69 6e 67 20 /* Vdbe being
1aa47 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a 20 20 configured */.
1aa48 69 6e 74 20 69 64 78 2c 20 20 20 20 20 20 20 20 int idx,
1aa49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa4a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
1aa4b 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65 umn zName applie
1aa4c 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61 s to */. int va
1aa4d 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
1aa4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
1aa4f 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45 e of the COLNAME
1aa50 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a _* constants */.
1aa51 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
1aa52 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ame,
1aa53 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1aa54 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 buffer containi
1aa55 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 ng name */. voi
1aa56 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1aa57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1aa58 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 Memory manageme
1aa59 6e 74 20 73 74 72 61 74 65 67 79 20 66 6f 72 20 nt strategy for
1aa5a 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e zName */.){. in
1aa5b 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f t rc;. Mem *pCo
1aa5c 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 lName;. assert(
1aa5d 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 idx<p->nResColu
1aa5e 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 mn );. assert(
1aa5f 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b var<COLNAME_N );
1aa60 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 . if( p->db->ma
1aa61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1aa62 20 20 61 73 73 65 72 74 28 20 21 7a 4e 61 6d 65 assert( !zName
1aa63 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49 54 45 || xDel!=SQLITE
1aa64 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 _DYNAMIC );.
1aa65 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1aa66 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 MEM;. }. asser
1aa67 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d t( p->aColName!=
1aa68 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 0 );. pColName
1aa69 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b = &(p->aColName[
1aa6a 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 idx+var*p->nResC
1aa6b 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 olumn]);. rc =
1aa6c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1aa6d 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a tStr(pColName, z
1aa6e 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 Name, -1, SQLITE
1aa6f 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20 _UTF8, xDel);.
1aa70 61 73 73 65 72 74 28 20 72 63 21 3d 30 20 7c 7c assert( rc!=0 ||
1aa71 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c !zName || (pCol
1aa72 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f Name->flags&MEM_
1aa73 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65 Term)!=0 );. re
1aa74 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1aa75 2a 20 41 20 72 65 61 64 20 6f 72 20 77 72 69 74 * A read or writ
1aa76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 e transaction ma
1aa77 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 y or may not be
1aa78 61 63 74 69 76 65 20 6f 6e 20 64 61 74 61 62 61 active on databa
1aa79 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e se handle.** db.
1aa7a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f If a transactio
1aa7b 6e 20 69 73 20 61 63 74 69 76 65 2c 20 63 6f 6d n is active, com
1aa7c 6d 69 74 20 69 74 2e 20 49 66 20 74 68 65 72 65 mit it. If there
1aa7d 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74 is a.** write-t
1aa7e 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e ransaction spann
1aa7f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ing more than on
1aa80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1aa81 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
1aa82 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20 74 takes care of t
1aa83 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1aa84 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 l trickery..*/.s
1aa85 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 43 6f tatic int vdbeCo
1aa86 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 mmit(sqlite3 *db
1aa87 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e , Vdbe *p){. in
1aa88 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e t i;. int nTran
1aa89 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 s = 0; /* Numbe
1aa8a 72 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 77 r of databases w
1aa8b 69 74 68 20 61 6e 20 61 63 74 69 76 65 20 77 72 ith an active wr
1aa8c 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
1aa8d 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
1aa8e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e LITE_OK;. int n
1aa8f 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a eedXcommit = 0;.
1aa90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
1aa91 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1aa92 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 69 73 20 . /* With this
1aa93 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 65 33 56 option, sqlite3V
1aa94 74 61 62 53 79 6e 63 28 29 20 69 73 20 64 65 66 tabSync() is def
1aa95 69 6e 65 64 20 74 6f 20 62 65 20 73 69 6d 70 6c ined to be simpl
1aa96 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f y . ** SQLITE_O
1aa97 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74 20 75 73 K so p is not us
1aa98 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 ed. . */. UNUS
1aa99 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b ED_PARAMETER(p);
1aa9a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 .#endif.. /* Be
1aa9b 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 fore doing anyth
1aa9c 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74 ing else, call t
1aa9d 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 he xSync() callb
1aa9e 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a ack for any. **
1aa9f 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 virtual module
1aaa0 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e 20 69 tables written i
1aaa1 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 n this transacti
1aaa2 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74 6f 0a on. This has to.
1aaa3 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62 65 66 ** be done bef
1aaa4 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 ore determining
1aaa5 77 68 65 74 68 65 72 20 61 20 6d 61 73 74 65 72 whether a master
1aaa6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
1aaa7 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2c . ** required,
1aaa8 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29 20 63 as an xSync() c
1aaa9 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64 64 20 allback may add
1aaaa 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 an attached data
1aaab 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 base. ** to the
1aaac 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
1aaad 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1aaae 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20 26 70 3VtabSync(db, &p
1aaaf 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 ->zErrMsg);. if
1aab0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1aab1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1aab2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 ;. }.. /* This
1aab3 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 loop determines
1aab4 20 28 61 29 20 69 66 20 74 68 65 20 63 6f 6d 6d (a) if the comm
1aab5 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 it hook should b
1aab6 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 e invoked and.
1aab7 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 ** (b) how many
1aab8 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68 database files h
1aab9 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65 20 74 ave open write t
1aaba 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 ransactions, not
1aabb 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 . ** including
1aabc 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 the temp databa
1aabd 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70 6f 72 se. (b) is impor
1aabe 74 61 6e 74 20 62 65 63 61 75 73 65 20 69 66 20 tant because if
1aabf 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 more than . **
1aac0 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c one database fil
1aac1 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 e has an open wr
1aac2 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ite transaction,
1aac3 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1aac4 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 l. ** file is r
1aac5 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 61 equired for an a
1aac6 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 tomic commit..
1aac7 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 */ . for(i=0; i
1aac8 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 <db->nDb; i++){
1aac9 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 . Btree *pBt
1aaca 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1aacb 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1aacc 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
1aacd 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 pBt) ){. ne
1aace 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 edXcommit = 1;.
1aacf 20 20 20 20 20 69 66 28 20 69 21 3d 31 20 29 20 if( i!=1 )
1aad0 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a nTrans++;. }.
1aad1 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1aad2 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 74 65 re are any write
1aad3 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 -transactions at
1aad4 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 all, invoke the
1aad5 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a commit hook */.
1aad6 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 if( needXcommi
1aad7 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 t && db->xCommit
1aad8 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Callback ){.
1aad9 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1aada 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 etyOff(db);.
1aadb 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 rc = db->xCommit
1aadc 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f Callback(db->pCo
1aadd 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20 28 76 mmitArg);. (v
1aade 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1aadf 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28 yOn(db);. if(
1aae0 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 rc ){. ret
1aae1 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 urn SQLITE_CONST
1aae2 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d RAINT;. }. }
1aae3 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c .. /* The simpl
1aae4 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 e case - no more
1aae5 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 than one databa
1aae6 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 se file (not cou
1aae7 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 nting the. ** T
1aae8 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 68 61 EMP database) ha
1aae9 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
1aaea 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 active. There
1aaeb 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 is no need for t
1aaec 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a he. ** master-j
1aaed 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a ournal.. **. *
1aaee 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 * If the return
1aaef 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 value of sqlite3
1aaf0 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
1aaf1 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e () is a zero len
1aaf2 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c gth. ** string,
1aaf3 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 it means the ma
1aaf4 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 3a in database is :
1aaf5 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d memory: or a tem
1aaf6 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a p file. In . *
1aaf7 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 20 64 * that case we d
1aaf8 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 o not support at
1aaf9 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 omic multi-file
1aafa 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 commits, so use
1aafb 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 the . ** simple
1aafc 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a case then too..
1aafd 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 */. if( 0==sq
1aafe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 lite3Strlen30(sq
1aaff 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c lite3BtreeGetFil
1ab00 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d ename(db->aDb[0]
1ab01 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 .pBt)). || nTr
1ab02 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 ans<=1. ){.
1ab03 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c for(i=0; rc==SQL
1ab04 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e ITE_OK && i<db->
1ab05 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
1ab06 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
1ab07 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1ab08 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 if( pBt ){.
1ab09 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1ab0a 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1ab0b 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a aseOne(pBt, 0);.
1ab0c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
1ab0d 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d /* Do the com
1ab0e 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 mit only if all
1ab0f 64 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 databases succes
1ab10 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 sfully complete
1ab11 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a phase 1. . **
1ab12 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 If one of the B
1ab13 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1ab14 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 ne() calls fails
1ab15 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 , this indicates
1ab16 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 an. ** IO er
1ab17 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 ror while deleti
1ab18 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 ng or truncating
1ab19 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e a journal file.
1ab1a 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c It is unlikely,
1ab1b 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c . ** but coul
1ab1c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 d happen. In thi
1ab1d 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 s case abandon p
1ab1e 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 rocessing and re
1ab1f 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a turn the error..
1ab20 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
1ab21 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
1ab22 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 K && i<db->nDb;
1ab23 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 i++){. Btre
1ab24 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 e *pBt = db->aDb
1ab25 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 [i].pBt;. i
1ab26 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 f( pBt ){.
1ab27 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1ab28 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 reeCommitPhaseTw
1ab29 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a o(pBt);. }.
1ab2a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
1ab2b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1ab2c 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 sqlite3Vtab
1ab2d 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 Commit(db);.
1ab2e 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 }. }.. /* The
1ab2f 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 complex case - T
1ab30 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d here is a multi-
1ab31 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 file write-trans
1ab32 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 action active..
1ab33 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 ** This require
1ab34 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
1ab35 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 al file to ensur
1ab36 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
1ab37 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 n is. ** commit
1ab38 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 ted atomicly..
1ab39 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1ab3a 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 E_OMIT_DISKIO.
1ab3b 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
1ab3c 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 3_vfs *pVfs = db
1ab3d 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 ->pVfs;. int
1ab3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
1ab3f 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 char *zMaster
1ab40 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e = 0; /* File-n
1ab41 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 ame for the mast
1ab42 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 er journal */.
1ab43 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d char const *zM
1ab44 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 ainFile = sqlite
1ab45 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 3BtreeGetFilenam
1ab46 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 e(db->aDb[0].pBt
1ab47 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
1ab48 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 ile *pMaster = 0
1ab49 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 ;. i64 offset
1ab4a 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 = 0;. int re
1ab4b 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 s;.. /* Selec
1ab4c 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e t a master journ
1ab4d 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a al file name */.
1ab4e 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 do {. u
1ab4f 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 32 iRandom;.
1ab50 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ab51 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
1ab52 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
1ab53 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 omness(sizeof(iR
1ab54 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d andom), &iRandom
1ab55 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 );. zMaster
1ab56 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1ab57 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 f(db, "%s-mj%08X
1ab58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 ", zMainFile, iR
1ab59 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66 andom&0x7fffffff
1ab5a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d );. if( !zM
1ab5b 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20 aster ){.
1ab5c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1ab5d 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OMEM;. }.
1ab5e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ab5f 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a OsAccess(pVfs, z
1ab60 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 Master, SQLITE_A
1ab61 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 CCESS_EXISTS, &r
1ab62 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 es);. }while(
1ab63 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1ab64 26 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 & res );. if(
1ab65 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ab66 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 {. /* Open
1ab67 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1ab68 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 al. */. rc
1ab69 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d = sqlite3OsOpenM
1ab6a 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 alloc(pVfs, zMas
1ab6b 74 65 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a ter, &pMaster, .
1ab6c 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
1ab6d 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c _OPEN_READWRITE|
1ab6e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
1ab6f 54 45 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 TE|. SQ
1ab70 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
1ab71 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f IVE|SQLITE_OPEN_
1ab72 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 MASTER_JOURNAL,
1ab73 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0. );. }
1ab74 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1ab75 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1ab76 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1ab77 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
1ab78 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1ab79 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 }. . /* Writ
1ab7a 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 e the name of ea
1ab7b 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ch database file
1ab7c 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 in the transact
1ab7d 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 ion into the new
1ab7e 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a . ** master j
1ab7f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
1ab80 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1ab81 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c at this point cl
1ab82 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 ose. ** and d
1ab83 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 elete the master
1ab84 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 journal file. A
1ab85 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 ll the individua
1ab86 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a l journal files.
1ab87 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 ** still hav
1ab88 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 e 'null' as the
1ab89 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 master journal p
1ab8a 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20 ointer, so they
1ab8b 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a will roll. **
1ab8c 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e back independen
1ab8d 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75 72 65 tly if a failure
1ab8e 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a occurs.. */.
1ab8f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
1ab90 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
1ab91 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
1ab92 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
1ab93 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 . if( i==1
1ab94 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a ) continue; /*
1ab95 20 49 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 Ignore the TEMP
1ab96 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 database */.
1ab97 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 if( sqlite3Bt
1ab98 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 reeIsInTrans(pBt
1ab99 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 ) ){. cha
1ab9a 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d r const *zFile =
1ab9b 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1ab9c 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 Journalname(pBt)
1ab9d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 ;. if( zF
1ab9e 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 ile[0]==0 ) cont
1ab9f 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 inue; /* Ignore
1aba0 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 :memory: databa
1aba1 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ses */. i
1aba2 66 28 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 f( !needSync &&
1aba3 21 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e !sqlite3BtreeSyn
1aba4 63 44 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 cDisabled(pBt) )
1aba5 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 {. need
1aba6 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 Sync = 1;.
1aba7 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
1aba8 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1aba9 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 pMaster, zFile,
1abaa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1abab 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 zFile)+1, offset
1abac 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 );. offse
1abad 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c t += sqlite3Strl
1abae 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 en30(zFile)+1;.
1abaf 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1abb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1abb1 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
1abb2 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 loseFree(pMaster
1abb3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1abb4 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
1abb5 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a s, zMaster, 0);.
1abb6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1abb7 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1abb8 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ter);.
1abb9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1abba 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1abbb 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 }.. /* Sync
1abbc 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1abbd 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 al file. If the
1abbe 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
1abbf 20 64 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 device. ** f
1abc0 6c 61 67 20 69 73 20 73 65 74 20 74 68 69 73 20 lag is set this
1abc1 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e is not required.
1abc2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1abc3 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26 needSync . &
1abc4 26 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 & 0==(sqlite3OsD
1abc5 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
1abc6 74 69 63 73 28 70 4d 61 73 74 65 72 29 26 53 51 tics(pMaster)&SQ
1abc7 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
1abc8 4e 54 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53 NTIAL). && S
1abc9 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
1abca 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d sqlite3OsSync(pM
1abcb 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 aster, SQLITE_SY
1abcc 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 NC_NORMAL)).
1abcd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1abce 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 OsCloseFree(pMas
1abcf 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ter);. sqli
1abd0 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
1abd1 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 , zMaster, 0);.
1abd2 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1abd3 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1abd4 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1abd5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1abd6 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 Sync all the db
1abd7 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 files involved i
1abd8 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
1abd9 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c n. The same call
1abda 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 . ** sets the
1abdb 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1abdc 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 pointer in each
1abdd 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e individual journ
1abde 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e al. If. ** an
1abdf 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 error occurs he
1abe0 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 re, do not delet
1abe1 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
1abe2 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a rnal file.. *
1abe3 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
1abe4 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
1abe5 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 ing the first ca
1abe6 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c ll to. ** sql
1abe7 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
1abe8 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 haseOne(), then
1abe9 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 there is a chanc
1abea 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a e that the. *
1abeb 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c * master journal
1abec 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 file will be or
1abed 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 phaned. But we c
1abee 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c annot delete it,
1abef 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 . ** in case
1abf0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1abf1 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 al file name was
1abf2 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
1abf3 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a e journal. **
1abf4 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 file before the
1abf5 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 failure occurre
1abf6 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f d.. */. fo
1abf7 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
1abf8 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 E_OK && i<db->nD
1abf9 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 b; i++){ .
1abfa 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
1abfb 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
1abfc 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 if( pBt ){.
1abfd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1abfe 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1abff 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 seOne(pBt, zMast
1ac00 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 er);. }.
1ac01 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 }. sqlite3Os
1ac02 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 CloseFree(pMaste
1ac03 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
1ac04 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ac05 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ac06 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
1ac07 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1ac08 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
1ac09 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
1ac0a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
1ac0b 69 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 is commits the t
1ac0c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 ransaction. Afte
1ac0d 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 r. ** doing t
1ac0e 68 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 his the director
1ac0f 79 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 y is synced agai
1ac10 6e 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 n before any ind
1ac11 69 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 ividual. ** t
1ac12 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 ransaction files
1ac13 20 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 are deleted..
1ac14 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
1ac15 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 lite3OsDelete(pV
1ac16 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b fs, zMaster, 1);
1ac17 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1ac18 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1ac19 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 . zMaster = 0
1ac1a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
1ac1b 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1ac1c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 . }.. /* A
1ac1d 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 ll files and dir
1ac1e 65 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c ectories have al
1ac1f 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 ready been synce
1ac20 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 d, so the follow
1ac21 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 ing. ** calls
1ac22 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
1ac23 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
1ac24 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e are only closin
1ac25 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 g files and.
1ac26 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 ** deleting or t
1ac27 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 runcating journa
1ac28 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 ls. If something
1ac29 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c goes wrong whil
1ac2a 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 e. ** this is
1ac2b 20 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f happening we do
1ac2c 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e n't really care.
1ac2d 20 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f The integrity o
1ac2e 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 f the. ** tra
1ac2f 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 nsaction is alre
1ac30 61 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 ady guaranteed,
1ac31 62 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 but some stray '
1ac32 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 cold' journals.
1ac33 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 ** may be lyi
1ac34 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 ng around. Retur
1ac35 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f ning an error co
1ac36 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 de won't help ma
1ac37 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 tters.. */.
1ac38 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 disable_simula
1ac39 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b ted_io_errors();
1ac3a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 . sqlite3Begi
1ac3b 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1ac3c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1ac3d 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a db->nDb; i++){ .
1ac3e 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1ac3f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1ac40 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 t;. if( pBt
1ac41 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1ac42 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1ac43 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 aseTwo(pBt);.
1ac44 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
1ac45 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1ac46 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 alloc();. ena
1ac47 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
1ac48 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 _errors();..
1ac49 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 sqlite3VtabCommi
1ac4a 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 t(db);. }.#endi
1ac4b 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a f.. return rc;.
1ac4c 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 }../* .** This r
1ac4d 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 outine checks th
1ac4e 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 at the sqlite3.a
1ac4f 63 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 ctiveVdbeCnt cou
1ac50 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d nt variable.** m
1ac51 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 atches the numbe
1ac52 72 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 r of vdbe's in t
1ac53 68 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e he list sqlite3.
1ac54 70 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a pVdbe that are.*
1ac55 2a 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 * currently acti
1ac56 76 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e ve. An assertion
1ac57 20 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 fails if the tw
1ac58 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 o counts do not
1ac59 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 match..** This i
1ac5a 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 s an internal se
1ac5b 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 lf-check only -
1ac5c 69 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 it is not an ess
1ac5d 65 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e ential processin
1ac5e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a g.** step..**.**
1ac5f 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 This is a no-op
1ac60 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 if NDEBUG is de
1ac61 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 fined..*/.#ifnde
1ac62 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 f NDEBUG.static
1ac63 76 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 void checkActive
1ac64 56 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 VdbeCnt(sqlite3
1ac65 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b *db){. Vdbe *p;
1ac66 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a . int cnt = 0;.
1ac67 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 int nWrite = 0
1ac68 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 ;. p = db->pVdb
1ac69 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b e;. while( p ){
1ac6a 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 . if( p->magi
1ac6b 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1ac6c 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b N && p->pc>=0 ){
1ac6d 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 . cnt++;.
1ac6e 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f if( p->readO
1ac6f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b nly==0 ) nWrite+
1ac70 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d +;. }. p =
1ac71 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 p->pNext;. }.
1ac72 20 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 assert( cnt==db
1ac73 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 ->activeVdbeCnt
1ac74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 );. assert( nWr
1ac75 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 ite==db->writeVd
1ac76 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 beCnt );.}.#else
1ac77 0a 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 .#define checkAc
1ac78 74 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 tiveVdbeCnt(x).#
1ac79 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 endif../*.** For
1ac7a 20 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 every Btree tha
1ac7b 74 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f t in database co
1ac7c 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 nnection db whic
1ac7d 68 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d h .** has been m
1ac7e 6f 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 odified, "trip"
1ac7f 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 or invalidate ea
1ac80 63 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 ch cursor in.**
1ac81 74 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 that Btree might
1ac82 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
1ac83 69 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 ied so that the
1ac84 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 cursor.** can ne
1ac85 76 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69 ver be used agai
1ac86 6e 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 n. This happens
1ac87 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b when a rollback
1ac88 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 .*** occurs. We
1ac89 20 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c have to trip al
1ac8a 6c 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 l the other curs
1ac8b 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 ors, even.** cur
1ac8c 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 sor from other V
1ac8d 4d 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 Ms in different
1ac8e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1ac8f 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 ions,.** so that
1ac90 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 none of them tr
1ac91 79 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 y to use the dat
1ac92 61 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a a at which they.
1ac93 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 ** were pointing
1ac94 20 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d and which now m
1ac95 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 ay have been cha
1ac96 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 nged due.** to t
1ac97 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a he rollback..**.
1ac98 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 ** Remember that
1ac99 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 a rollback can
1ac9a 64 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f delete tables co
1ac9b 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 mplete and.** re
1ac9c 6f 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e order rootpages.
1ac9d 20 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 So it is not s
1ac9e 75 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 ufficient just t
1ac9f 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 o save.** the st
1aca0 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f ate of the curso
1aca1 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 r. We have to i
1aca2 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 nvalidate the cu
1aca3 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 rsor.** so that
1aca4 69 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 it is never used
1aca5 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 again..*/.stati
1aca6 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 c void invalidat
1aca7 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 eCursorsOnModifi
1aca8 65 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 edBtrees(sqlite3
1aca9 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *db){. int i;.
1acaa 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
1acab 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
1acac 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 Btree *p = db->a
1acad 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 Db[i].pBt;. i
1acae 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 f( p && sqlite3B
1acaf 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 treeIsInTrans(p)
1acb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1acb1 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 3BtreeTripAllCur
1acb2 73 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 sors(p, SQLITE_A
1acb3 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d BORT);. }. }
1acb4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1acb5 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 Vdbe passed as
1acb6 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
1acb7 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 nt opened a stat
1acb8 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
1acb9 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e n,.** close it n
1acba 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 ow. Argument eOp
1acbb 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
1acbc 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1acbd 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 CK or.** SAVEPOI
1acbe 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 NT_RELEASE. If i
1acbf 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 t is SAVEPOINT_R
1acc0 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 OLLBACK, then th
1acc1 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 e statement.** t
1acc2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
1acc3 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f lled back. If eO
1acc4 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 p is SAVEPOINT_R
1acc5 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 ELEASE, then the
1acc6 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 .** statement t
1acc7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
1acc8 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mmtted..**.** If
1acc9 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
1acca 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 urs, an SQLITE_I
1accb 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 OERR_XXX error c
1accc 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
1accd 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 .** Otherwise S
1acce 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
1accf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1acd0 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 sqlite3VdbeClose
1acd1 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a Statement(Vdbe *
1acd2 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 p, int eOp){. s
1acd3 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 qlite3 *const db
1acd4 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 = p->db;. int
1acd5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1acd6 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61 . /* If p->iSta
1acd7 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61 74 65 tement is greate
1acd8 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 r than zero, the
1acd9 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70 65 6e n this Vdbe open
1acda 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61 74 65 ed a . ** state
1acdb 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1acdc 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
1acdd 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54 68 65 closed here. The
1acde 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a only exception.
1acdf 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61 6e 20 ** is that an
1ace0 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76 IO error may hav
1ace1 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75 73 69 e occured, causi
1ace2 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 ng an emergency
1ace3 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49 rollback.. ** I
1ace4 6e 20 74 68 69 73 20 63 61 73 65 20 28 64 62 2d n this case (db-
1ace5 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c >nStatement==0),
1ace6 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f and there is no
1ace7 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a thing to do.. *
1ace8 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53 74 61 /. if( db->nSta
1ace9 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69 53 74 tement && p->iSt
1acea 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 atement ){. i
1aceb 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 nt i;. const
1acec 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d int iSavepoint =
1aced 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 p->iStatement-1
1acee 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ;.. assert( e
1acef 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f Op==SAVEPOINT_RO
1acf0 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 LLBACK || eOp==S
1acf1 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1acf2 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 );. assert( d
1acf3 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 b->nStatement>0
1acf4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1acf5 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 ->iStatement==(d
1acf6 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 b->nStatement+db
1acf7 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b ->nSavepoint) );
1acf8 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 .. for(i=0; i
1acf9 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 <db->nDb; i++){
1acfa 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d . int rc2 =
1acfb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1acfc 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
1acfd 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
1acfe 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a if( pBt ){.
1acff 20 20 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d if( eOp=
1ad00 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
1ad01 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ACK ){.
1ad02 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 rc2 = sqlite3Bt
1ad03 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 reeSavepoint(pBt
1ad04 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c , SAVEPOINT_ROLL
1ad05 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 BACK, iSavepoint
1ad06 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1ad07 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 if( rc2==SQ
1ad08 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1ad09 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 rc2 = sqlit
1ad0a 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
1ad0b 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f (pBt, SAVEPOINT_
1ad0c 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f RELEASE, iSavepo
1ad0d 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a int);. }.
1ad0e 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1ad0f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ad10 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b rc = rc2;
1ad11 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1ad12 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d }. }. db-
1ad13 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 >nStatement--;.
1ad14 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 p->iStatement
1ad15 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 = 0;.. /* If
1ad16 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 the statement t
1ad17 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 62 65 ransaction is be
1ad18 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c ing rolled back,
1ad19 20 61 6c 73 6f 20 72 65 73 74 6f 72 65 20 74 68 also restore th
1ad1a 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 e . ** databa
1ad1b 73 65 20 68 61 6e 64 6c 65 73 20 64 65 66 65 72 se handles defer
1ad1c 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 red constraint c
1ad1d 6f 75 6e 74 65 72 20 74 6f 20 74 68 65 20 76 61 ounter to the va
1ad1e 6c 75 65 20 69 74 20 68 61 64 20 77 68 65 6e 20 lue it had when
1ad1f 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 61 74 . ** the stat
1ad20 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1ad21 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 20 2a n was opened. *
1ad22 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 /. if( eOp==S
1ad23 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1ad24 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6e K ){. db->n
1ad25 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 70 DeferredCons = p
1ad26 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 0a ->nStmtDefCons;.
1ad27 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1ad28 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1ad29 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
1ad2a 70 69 6c 65 64 20 74 6f 20 73 75 70 70 6f 72 74 piled to support
1ad2b 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
1ad2c 64 65 20 61 6e 64 20 74 6f 20 62 65 20 74 68 72 de and to be thr
1ad2d 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74 68 69 73 eadsafe,.** this
1ad2e 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 69 6e 73 routine obtains
1ad2f 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 the mutex assoc
1ad30 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20 iated with each
1ad31 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
1ad32 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62 re.** that may b
1ad33 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 e accessed by th
1ad34 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 e VM passed as a
1ad35 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 6e 20 64 n argument. In d
1ad36 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a 20 73 65 oing so it.** se
1ad37 74 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e ts the BtShared.
1ad38 64 62 20 6d 65 6d 62 65 72 20 6f 66 20 65 61 63 db member of eac
1ad39 68 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 h of the BtShare
1ad3a 64 20 73 74 72 75 63 74 75 72 65 73 2c 20 65 6e d structures, en
1ad3b 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 suring.** that t
1ad3c 68 65 20 63 6f 72 72 65 63 74 20 62 75 73 79 2d he correct busy-
1ad3d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b handler callback
1ad3e 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 66 20 72 is invoked if r
1ad3f 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 equired..**.** I
1ad40 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 f SQLite is not
1ad41 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 64 threadsafe but d
1ad42 6f 65 73 20 73 75 70 70 6f 72 74 20 73 68 61 72 oes support shar
1ad43 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 ed-cache mode, t
1ad44 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 hen.** sqlite3Bt
1ad45 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 69 73 reeEnterAll() is
1ad46 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 invoked to set
1ad47 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 the BtShared.db
1ad48 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 variables.** of
1ad49 61 6c 6c 20 6f 66 20 42 74 53 68 61 72 65 64 20 all of BtShared
1ad4a 73 74 72 75 63 74 75 72 65 73 20 61 63 63 65 73 structures acces
1ad4b 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 64 61 sible via the da
1ad4c 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 0a 2a tabase handle .*
1ad4d 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1ad4e 68 20 74 68 65 20 56 4d 2e 20 4f 66 20 63 6f 75 h the VM. Of cou
1ad4f 72 73 65 20 6f 6e 6c 79 20 61 20 73 75 62 73 65 rse only a subse
1ad50 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 t of these struc
1ad51 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 tures.** will be
1ad52 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 accessed by the
1ad53 20 56 4d 2c 20 61 6e 64 20 77 65 20 63 6f 75 6c VM, and we coul
1ad54 64 20 75 73 65 20 56 64 62 65 2e 62 74 72 65 65 d use Vdbe.btree
1ad55 4d 61 73 6b 20 74 6f 20 66 69 67 75 72 65 0a 2a Mask to figure.*
1ad56 2a 20 74 68 61 74 20 73 75 62 73 65 74 20 6f 75 * that subset ou
1ad57 74 2c 20 62 75 74 20 74 68 65 72 65 20 69 73 20 t, but there is
1ad58 6e 6f 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 no advantage to
1ad59 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 doing so..**.**
1ad5a 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 If SQLite is not
1ad5b 20 74 68 72 65 61 64 73 61 66 65 20 61 6e 64 20 threadsafe and
1ad5c 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
1ad5d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
1ad5e 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 de, this.** func
1ad5f 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
1ad60 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1ad61 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1ad62 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 ACHE.SQLITE_PRIV
1ad63 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1ad64 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e VdbeMutexArrayEn
1ad65 74 65 72 28 56 64 62 65 20 2a 70 29 7b 0a 23 69 ter(Vdbe *p){.#i
1ad66 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
1ad67 41 46 45 0a 20 20 73 71 6c 69 74 65 33 42 74 72 AFE. sqlite3Btr
1ad68 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 eeMutexArrayEnte
1ad69 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 23 r(&p->aMutex);.#
1ad6a 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 42 74 else. sqlite3Bt
1ad6b 72 65 65 45 6e 74 65 72 41 6c 6c 28 70 2d 3e 64 reeEnterAll(p->d
1ad6c 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 23 65 6e b);.#endif.}.#en
1ad6d 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
1ad6e 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1ad6f 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 ed when a transa
1ad70 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 ction opened by
1ad71 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
1ad72 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 handle associat
1ad73 65 64 20 77 69 74 68 20 74 68 65 20 56 4d 20 70 ed with the VM p
1ad74 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 assed as an argu
1ad75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20 74 6f ment is about to
1ad76 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 be .** committe
1ad77 64 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 d. If there are
1ad78 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65 66 65 outstanding defe
1ad79 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 rred foreign key
1ad7a 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 constraint.** v
1ad7b 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74 75 72 iolations, retur
1ad7c 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 n SQLITE_ERROR.
1ad7d 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 Otherwise, SQLIT
1ad7e 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 E_OK..**.** If t
1ad7f 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e here are outstan
1ad80 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74 69 6f ding FK violatio
1ad81 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 ns and this func
1ad82 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a tion returns .**
1ad83 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 73 SQLITE_ERROR, s
1ad84 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 et the result of
1ad85 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c 49 54 the VM to SQLIT
1ad86 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 E_CONSTRAINT and
1ad87 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65 72 72 write.** an err
1ad88 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 69 74 or message to it
1ad89 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20 53 51 . Then return SQ
1ad8a 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f 0a 23 LITE_ERROR..*/.#
1ad8b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ad8c 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 IT_FOREIGN_KEY.S
1ad8d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ad8e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 t sqlite3VdbeChe
1ad8f 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20 69 6e ckFk(Vdbe *p, in
1ad90 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20 20 73 t deferred){. s
1ad91 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
1ad92 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66 65 72 db;. if( (defer
1ad93 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65 66 65 red && db->nDefe
1ad94 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c 20 28 rredCons>0) || (
1ad95 21 64 65 66 65 72 72 65 64 20 26 26 20 70 2d 3e !deferred && p->
1ad96 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e 30 29 nFkConstraint>0)
1ad97 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1ad98 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1ad99 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 T;. p->errorA
1ad9a 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 ction = OE_Abort
1ad9b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 ;. sqlite3Set
1ad9c 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1ad9d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69 67 6e sg, db, "foreign
1ad9e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 key constraint
1ad9f 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 failed");. re
1ada0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1ada1 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 R;. }. return
1ada2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
1ada3 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
1ada4 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
1ada5 64 20 74 68 65 20 77 68 65 6e 20 61 20 56 44 42 d the when a VDB
1ada6 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c 74 2e E tries to halt.
1ada7 20 20 49 66 20 74 68 65 20 56 44 42 45 0a 2a 2a If the VDBE.**
1ada8 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e 67 65 has made change
1ada9 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75 74 6f s and is in auto
1adaa 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 commit mode, the
1adab 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a n commit those.*
1adac 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66 20 61 * changes. If a
1adad 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 rollback is nee
1adae 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 ded, then do the
1adaf 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
1adb0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1adb1 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f the only way to
1adb2 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74 65 20 move the state
1adb3 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 of a VM from.**
1adb4 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e SQLITE_MAGIC_RUN
1adb5 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 to SQLITE_MAGIC
1adb6 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20 68 61 _HALT. It is ha
1adb7 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c rmless to.** cal
1adb8 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 l this on a VM t
1adb9 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 53 51 hat is in the SQ
1adba 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 LITE_MAGIC_HALT
1adbb 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 state..**.** Ret
1adbc 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 urn an error cod
1adbd 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 e. If the commi
1adbe 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 t could not comp
1adbf 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f 66 0a lete because of.
1adc0 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 ** lock contenti
1adc1 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 on, return SQLIT
1adc2 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51 4c 49 E_BUSY. If SQLI
1adc3 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 TE_BUSY is retur
1adc4 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 ned, it.** means
1adc5 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64 20 6e the close did n
1adc6 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 ot happen and ne
1adc7 65 64 73 20 74 6f 20 62 65 20 72 65 70 65 61 74 eds to be repeat
1adc8 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1adc9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1adca 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 20 2a 3VdbeHalt(Vdbe *
1adcb 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 p){. int rc;
1adcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1adcd 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
1adce 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65 6e 74 store transient
1adcf 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f return codes */
1add0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1add1 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 54 68 p->db;.. /* Th
1add2 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 is function cont
1add3 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63 20 74 ains the logic t
1add4 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20 69 hat determines i
1add5 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 f a statement or
1add6 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f . ** transactio
1add7 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 n will be commit
1add8 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 ted or rolled ba
1add9 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f ck as a result o
1adda 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 f the. ** execu
1addb 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76 69 72 tion of this vir
1addc 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 tual machine. .
1addd 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 **. ** If any
1adde 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
1addf 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 errors occur:.
1ade0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c **. ** SQL
1ade1 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 ITE_NOMEM. **
1ade2 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a SQLITE_IOERR.
1ade3 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
1ade4 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 FULL. ** SQ
1ade5 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 LITE_INTERRUPT.
1ade6 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 **. ** Then th
1ade7 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 e internal cache
1ade8 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e might have been
1ade9 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
1adea 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 nsistent. ** st
1adeb 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20 74 6f ate. We need to
1adec 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 rollback the st
1aded 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1adee 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 ion, if there is
1adef 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 . ** one, or th
1adf0 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 e complete trans
1adf1 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72 65 20 action if there
1adf2 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 is no statement
1adf3 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a transaction.. *
1adf4 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e /.. if( p->db->
1adf5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1adf6 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 p->rc = SQLI
1adf7 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
1adf8 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 closeAllCursors(
1adf9 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 p);. if( p->mag
1adfa 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic!=VDBE_MAGIC_R
1adfb 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e UN ){. return
1adfc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1adfd 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 checkActiveVdb
1adfe 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 eCnt(db);.. /*
1adff 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c No commit or rol
1ae00 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 lback needed if
1ae01 74 68 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 the program neve
1ae02 72 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 r started */. i
1ae03 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 f( p->pc>=0 ){.
1ae04 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a int mrc; /*
1ae05 20 50 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 Primary error c
1ae06 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a ode from p->rc *
1ae07 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 61 74 65 /. int eState
1ae08 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 mentOp = 0;.
1ae09 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 72 72 int isSpecialErr
1ae0a 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f or; /
1ae0b 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 * Set to true if
1ae0c 20 61 20 27 73 70 65 63 69 61 6c 27 20 65 72 72 a 'special' err
1ae0d 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f or */.. /* Lo
1ae0e 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73 ck all btrees us
1ae0f 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d ed by the statem
1ae10 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 ent */. sqlit
1ae11 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 e3VdbeMutexArray
1ae12 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f Enter(p);.. /
1ae13 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 * Check for one
1ae14 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 of the special e
1ae15 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 rrors */. mrc
1ae16 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 66 66 3b = p->rc & 0xff;
1ae17 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1ae18 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc!=SQLITE_IOERR
1ae19 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20 2f 2a 20 _BLOCKED ); /*
1ae1a 54 68 69 73 20 65 72 72 6f 72 20 6e 6f 20 6c 6f This error no lo
1ae1b 6e 67 65 72 20 65 78 69 73 74 73 20 2a 2f 0a 20 nger exists */.
1ae1c 20 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f isSpecialErro
1ae1d 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f r = mrc==SQLITE_
1ae1e 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 NOMEM || mrc==SQ
1ae1f 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 LITE_IOERR.
1ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ae21 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || mrc==SQLITE_I
1ae22 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d NTERRUPT || mrc=
1ae23 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 =SQLITE_FULL;.
1ae24 20 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 if( isSpecialE
1ae25 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a rror ){. /*
1ae26 20 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61 If the query wa
1ae27 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 s read-only, we
1ae28 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 need do no rollb
1ae29 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 ack at all. Othe
1ae2a 72 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 rwise,. **
1ae2b 70 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 proceed with the
1ae2c 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e special handlin
1ae2d 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 g.. */.
1ae2e 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e if( !p->readOn
1ae2f 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 ly || mrc!=SQLIT
1ae30 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 E_INTERRUPT ){.
1ae31 20 20 20 20 20 20 20 69 66 28 20 28 6d 72 63 3d if( (mrc=
1ae32 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c =SQLITE_NOMEM ||
1ae33 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c mrc==SQLITE_FUL
1ae34 4c 29 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d L) && p->usesStm
1ae35 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 tJournal ){.
1ae36 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 eStatement
1ae37 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 Op = SAVEPOINT_R
1ae38 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20 20 20 OLLBACK;.
1ae39 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1ae3a 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63 /* We are forc
1ae3b 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 ed to roll back
1ae3c 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e 73 the active trans
1ae3d 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 action. Before d
1ae3e 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a oing. *
1ae3f 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 * so, abort any
1ae40 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 other statements
1ae41 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72 this handle cur
1ae42 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76 rently has activ
1ae43 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
1ae44 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 invali
1ae45 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 dateCursorsOnMod
1ae46 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 3b ifiedBtrees(db);
1ae47 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1ae48 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
1ae49 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1ae4a 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 ite3CloseSavepoi
1ae4b 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 nts(db);.
1ae4c 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 db->autoCommi
1ae4d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d t = 1;. }
1ae4e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1ae4f 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 /* Check for
1ae50 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 immediate forei
1ae51 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e gn key violation
1ae52 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d s. */. if( p-
1ae53 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 >rc==SQLITE_OK )
1ae54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1ae55 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 30 29 dbeCheckFk(p, 0)
1ae56 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
1ae57 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f * If the auto-co
1ae58 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 mmit flag is set
1ae59 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 65 and this is the
1ae5a 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 77 72 69 only active wri
1ae5b 74 65 72 20 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 ter . ** VM,
1ae5c 74 68 65 6e 20 77 65 20 64 6f 20 65 69 74 68 65 then we do eithe
1ae5d 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f r a commit or ro
1ae5e 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 75 llback of the cu
1ae5f 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
1ae60 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a n. . **. *
1ae61 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62 6c 6f * Note: This blo
1ae62 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69 66 20 ck also runs if
1ae63 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
1ae64 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64 6c 65 al errors handle
1ae65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 d . ** above
1ae66 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 0a 20 has occurred. .
1ae67 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 73 */. if( !s
1ae68 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 qlite3VtabInSync
1ae69 28 64 62 29 20 0a 20 20 20 20 20 26 26 20 64 62 (db) . && db
1ae6a 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 ->autoCommit .
1ae6b 20 20 20 26 26 20 64 62 2d 3e 77 72 69 74 65 56 && db->writeV
1ae6c 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 dbeCnt==(p->read
1ae6d 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20 20 29 7b Only==0) . ){
1ae6e 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 . if( p->rc
1ae6f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 ==SQLITE_OK || (
1ae70 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d p->errorAction==
1ae71 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73 53 70 OE_Fail && !isSp
1ae72 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b 0a 20 ecialError) ){.
1ae73 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1ae74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c e3VdbeCheckFk(p,
1ae75 20 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 1) ){.
1ae76 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
1ae77 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d exArrayLeave(&p-
1ae78 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 >aMutex);.
1ae79 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1ae7a 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 E_ERROR;.
1ae7b 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 }. /* Th
1ae7c 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c e auto-commit fl
1ae7d 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 ag is true, the
1ae7e 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61 73 vdbe program was
1ae7f 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20 20 20 successful .
1ae80 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 74 20 61 ** or hit a
1ae81 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f 6e 73 n 'OR FAIL' cons
1ae82 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65 72 65 traint and there
1ae83 20 61 72 65 20 6e 6f 20 64 65 66 65 72 72 65 64 are no deferred
1ae84 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 20 20 20 foreign.
1ae85 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ** key constrai
1ae86 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75 70 20 74 nts to hold up t
1ae87 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
1ae88 54 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d This means a com
1ae89 6d 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 mit . **
1ae8a 69 73 20 72 65 71 75 69 72 65 64 2e 20 20 2a 2f is required. */
1ae8b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 76 64 . rc = vd
1ae8c 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 70 29 3b beCommit(db, p);
1ae8d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1ae8e 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a =SQLITE_BUSY ){.
1ae8f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1ae90 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
1ae91 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 Leave(&p->aMutex
1ae92 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
1ae93 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
1ae94 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 . }else i
1ae95 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1ae96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
1ae97 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 >rc = rc;.
1ae98 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 sqlite3Rollb
1ae99 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 ackAll(db);.
1ae9a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ae9b 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 db->nDeferr
1ae9c 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20 20 20 20 edCons = 0;.
1ae9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d sqlite3Com
1ae9e 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 mitInternalChang
1ae9f 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 es(db);.
1aea0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1aea1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f sqlite3Ro
1aea2 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 llbackAll(db);.
1aea3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d }. db-
1aea4 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b >nStatement = 0;
1aea5 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 . }else if( e
1aea6 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30 20 29 StatementOp==0 )
1aea7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 {. if( p->r
1aea8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1aea9 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d p->errorAction==
1aeaa 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 20 20 OE_Fail ){.
1aeab 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 eStatementOp
1aeac 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 = SAVEPOINT_RELE
1aead 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ASE;. }else
1aeae 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 63 74 if( p->errorAct
1aeaf 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b ion==OE_Abort ){
1aeb0 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d . eStatem
1aeb1 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e entOp = SAVEPOIN
1aeb2 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 T_ROLLBACK;.
1aeb3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1aeb4 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f invalidateCurso
1aeb5 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 rsOnModifiedBtre
1aeb6 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 es(db);.
1aeb7 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
1aeb8 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
1aeb9 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
1aeba 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 points(db);.
1aebb 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1aebc 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a it = 1;. }.
1aebd 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1aebe 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 If eStatementOp
1aebf 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1aec0 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 n a statement tr
1aec1 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 ansaction needs
1aec2 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 6f 6d to. ** be com
1aec3 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 mitted or rolled
1aec4 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71 6c 69 back. Call sqli
1aec5 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 te3VdbeCloseStat
1aec6 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20 20 2a ement() to. *
1aec7 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68 69 73 * do so. If this
1aec8 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74 75 72 operation retur
1aec9 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64 ns an error, and
1aeca 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 the current sta
1aecb 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 65 72 tement. ** er
1aecc 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c 49 ror code is SQLI
1aecd 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f TE_OK or SQLITE_
1aece 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68 65 6e CONSTRAINT, then
1aecf 20 73 65 74 20 74 68 65 20 65 72 72 6f 72 0a 20 set the error.
1aed0 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 74 68 ** code to th
1aed1 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20 20 20 e new value..
1aed2 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 74 61 */. if( eSta
1aed3 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 20 20 tementOp ){.
1aed4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1aed5 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
1aed6 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 (p, eStatementOp
1aed7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
1aed8 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 && (p->rc==SQLIT
1aed9 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 E_OK || p->rc==S
1aeda 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
1aedb 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e ) ){. p->
1aedc 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 rc = rc;.
1aedd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1aede 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1aedf 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d p->zErrM
1aee0 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a sg = 0;. }.
1aee1 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1aee2 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20 49 If this was an I
1aee3 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72 NSERT, UPDATE or
1aee4 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f 20 73 DELETE and no s
1aee5 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1aee6 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 73 20 tion. ** has
1aee7 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b been rolled back
1aee8 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 61 74 , update the dat
1aee9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1aeea 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e change-counter.
1aeeb 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 . */. if(
1aeec 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 p->changeCntOn
1aeed 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 74 ){. if( eSt
1aeee 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 50 atementOp!=SAVEP
1aeef 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b OINT_ROLLBACK ){
1aef0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1aef1 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
1aef2 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a b, p->nChange);.
1aef3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1aef4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1aef5 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 30 SetChanges(db, 0
1aef6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1aef7 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b p->nChange = 0;
1aef8 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
1aef9 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f 6d Rollback or com
1aefa 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 63 mit any schema c
1aefb 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 75 hanges that occu
1aefc 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 rred. */. if(
1aefd 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f p->rc!=SQLITE_O
1aefe 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 K && db->flags&S
1aeff 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
1af00 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ges ){. sql
1af01 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
1af02 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a lSchema(db, 0);.
1af03 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 db->flags
1af04 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53 = (db->flags | S
1af05 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
1af06 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ges);. }..
1af07 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
1af08 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 6c locks */. sql
1af09 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1af0a 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 rayLeave(&p->aMu
1af0b 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tex);. }.. /*
1af0c 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 66 We have successf
1af0d 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 20 ully halted and
1af0e 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 20 closed the VM.
1af0f 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 74 Record this fact
1af10 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 . */. if( p->pc
1af11 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 61 >=0 ){. db->a
1af12 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b 0a ctiveVdbeCnt--;.
1af13 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64 if( !p->read
1af14 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 62 Only ){. db
1af15 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d 2d ->writeVdbeCnt--
1af16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1af17 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
1af18 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 65 beCnt>=db->write
1af19 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a 20 VdbeCnt );. }.
1af1a 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
1af1b 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 63 _MAGIC_HALT;. c
1af1c 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e heckActiveVdbeCn
1af1d 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d 3e t(db);. if( p->
1af1e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1af1f 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1af20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1af21 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 }.. /* If the a
1af22 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 uto-commit flag
1af23 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c 20 is set to true,
1af24 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 74 then any locks t
1af25 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 20 hat were held.
1af26 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e ** by connection
1af27 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 65 db have now bee
1af28 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c 6c n released. Call
1af29 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
1af2a 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 20 onUnlocked() .
1af2b 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e 79 ** to invoke any
1af2c 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 6b required unlock
1af2d 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
1af2e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 s.. */. if( db
1af2f 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a ->autoCommit ){.
1af30 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 sqlite3Conne
1af31 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 ctionUnlocked(db
1af32 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 );. }.. assert
1af33 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 ( db->activeVdbe
1af34 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74 Cnt>0 || db->aut
1af35 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 oCommit==0 || db
1af36 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 ->nStatement==0
1af37 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
1af38 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
1af39 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 73 Each VDBE holds
1af3a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
1af3b 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 73 he most recent s
1af3c 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 61 qlite3_step() ca
1af3d 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 ll.** in p->rc.
1af3e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
1af3f 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 62 ts that result b
1af40 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b ack to SQLITE_OK
1af41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1af42 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1af43 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 VdbeResetStepRes
1af44 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ult(Vdbe *p){.
1af45 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
1af46 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
1af47 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 65 n up a VDBE afte
1af48 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 20 r execution but
1af49 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 do not delete th
1af4a 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 2e e VDBE just yet.
1af4b 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 .** Write any er
1af4c 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 74 ror messages int
1af4d 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 65 o *pzErrMsg. Re
1af4e 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 20 turn the result
1af4f 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 code..**.** Afte
1af50 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 r this routine i
1af51 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 20 s run, the VDBE
1af52 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 20 should be ready
1af53 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a 2a to be executed.*
1af54 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 * again..**.** T
1af55 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e 6f o look at it ano
1af56 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 72 ther way, this r
1af57 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 68 outine resets th
1af58 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a 2a e state of the.*
1af59 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e * virtual machin
1af5a 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49 e from VDBE_MAGI
1af5b 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d 41 C_RUN or VDBE_MA
1af5c 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 6f GIC_HALT back to
1af5d 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 .** VDBE_MAGIC_I
1af5e 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 NIT..*/.SQLITE_P
1af5f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1af60 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 e3VdbeReset(Vdbe
1af61 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
1af62 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 *db;. db = p->d
1af63 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 b;.. /* If the
1af64 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 74 VM did not run t
1af65 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20 o completion or
1af66 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 65 if it encountere
1af67 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 2c d an. ** error,
1af68 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e then it might n
1af69 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 6c ot have been hal
1af6a 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 53 ted properly. S
1af6b 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 6e o halt. ** it n
1af6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 ow.. */. (void
1af6d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
1af6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 (db);. sqlite3V
1af6f 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 76 dbeHalt(p);. (v
1af70 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1af71 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 yOff(db);.. /*
1af72 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20 If the VDBE has
1af73 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74 be run even part
1af74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e ially, then tran
1af75 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 63 sfer the error c
1af76 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72 ode. ** and err
1af77 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 or message from
1af78 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68 the VDBE into th
1af79 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1af7a 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a structure. But.
1af7b 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45 ** if the VDBE
1af7c 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73 has just been s
1af7d 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61 et to run but ha
1af7e 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65 s not actually e
1af7f 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a xecuted any. **
1af80 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65 instructions ye
1af81 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69 t, leave the mai
1af82 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 n database error
1af83 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63 information unc
1af84 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 hanged.. */. i
1af85 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 f( p->pc>=0 ){.
1af86 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73 if( p->zErrMs
1af87 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 g ){. sqlit
1af88 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
1af89 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c loc();. sql
1af8a 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
1af8b 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e 7a db->pErr,-1,p->z
1af8c 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 54 ErrMsg,SQLITE_UT
1af8d 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 F8,SQLITE_TRANSI
1af8e 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ENT);. sqli
1af8f 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1af90 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e oc();. db->
1af91 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b errCode = p->rc;
1af92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1af93 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1af94 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e 7a Msg);. p->z
1af95 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
1af96 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 }else if( p->rc
1af97 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1af98 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c Error(db, p->rc,
1af99 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0);. }else{.
1af9a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1af9b 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b or(db, SQLITE_OK
1af9c 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 , 0);. }. }e
1af9d 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 26 lse if( p->rc &&
1af9e 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 p->expired ){.
1af9f 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 65 /* The expire
1afa0 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 6f d flag was set o
1afa1 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f 72 n the VDBE befor
1afa2 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c e the first call
1afa3 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 . ** to sqlit
1afa4 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 63 e3_step(). For c
1afa5 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e 63 onsistency (sinc
1afa6 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 e sqlite3_step()
1afa7 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c 6c was. ** call
1afa8 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 74 ed), set the dat
1afa9 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 74 abase error in t
1afaa 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c 6c his case as well
1afab 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
1afac 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d ite3Error(db, p-
1afad 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c >rc, 0);. sql
1afae 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
1afaf 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 2d db->pErr, -1, p-
1afb0 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 45 >zErrMsg, SQLITE
1afb1 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 _UTF8, SQLITE_TR
1afb2 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 ANSIENT);. sq
1afb3 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1afb4 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
1afb5 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b p->zErrMsg = 0;
1afb6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61 . }.. /* Recla
1afb7 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 im all memory us
1afb8 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a 20 ed by the VDBE.
1afb9 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 29 */. Cleanup(p)
1afba 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 6f ;.. /* Save pro
1afbb 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 filing informati
1afbc 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 42 on from this VDB
1afbd 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 64 E run.. */.#ifd
1afbe 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
1afbf 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75 {. FILE *ou
1afc0 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 5f t = fopen("vdbe_
1afc1 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 61 profile.out", "a
1afc2 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 20 ");. if( out
1afc3 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a ){. int i;.
1afc4 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
1afc5 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 20 t, "---- ");.
1afc6 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
1afc7 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nOp; i++){.
1afc8 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1afc9 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 5b "%02x", p->aOp[
1afca 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 20 i].opcode);.
1afcb 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e 74 }. fprint
1afcc 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 f(out, "\n");.
1afcd 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1afce 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nOp; i++){.
1afcf 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
1afd0 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 38 , "%6d %10lld %8
1afd1 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20 lld ",.
1afd2 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c p->aOp[i].cnt,
1afd3 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 . p->a
1afd4 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 20 Op[i].cycles,.
1afd5 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b p->aOp[
1afd6 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 4f i].cnt>0 ? p->aO
1afd7 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e 61 p[i].cycles/p->a
1afd8 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 20 Op[i].cnt : 0.
1afd9 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
1afda 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1afdb 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d 3e tOp(out, i, &p->
1afdc 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d aOp[i]);. }
1afdd 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 . fclose(ou
1afde 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 t);. }. }.#e
1afdf 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 20 ndif. p->magic
1afe0 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 = VDBE_MAGIC_INI
1afe1 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72 T;. return p->r
1afe2 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b c & db->errMask;
1afe3 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e .}. ./*.** Clean
1afe4 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61 up and delete a
1afe5 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63 VDBE after exec
1afe6 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61 ution. Return a
1afe7 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1afe8 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 is.** the result
1afe9 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e code. Write an
1afea 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 y error message
1afeb 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72 text into *pzErr
1afec 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 Msg..*/.SQLITE_P
1afed 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1afee 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 e3VdbeFinalize(V
1afef 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 dbe *p){. int r
1aff0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1aff1 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 if( p->magic==V
1aff2 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c DBE_MAGIC_RUN ||
1aff3 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1aff4 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20 MAGIC_HALT ){.
1aff5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1aff6 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 beReset(p);.
1aff7 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 2d assert( (rc & p-
1aff8 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 >db->errMask)==r
1aff9 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 c );. }. sqlit
1affa 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 29 3b e3VdbeDelete(p);
1affb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1affc 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 ./*.** Call the
1affd 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 65 destructor for e
1affe 61 63 68 20 61 75 78 64 61 74 61 20 65 6e 74 72 ach auxdata entr
1afff 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63 20 66 y in pVdbeFunc f
1b000 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 or which.** the
1b001 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
1b002 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63 6c 65 t in mask is cle
1b003 61 72 2e 20 20 41 75 78 64 61 74 61 20 65 6e 74 ar. Auxdata ent
1b004 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31 0a 2a ries beyond 31.*
1b005 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64 65 73 * are always des
1b006 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65 73 74 troyed. To dest
1b007 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74 61 20 roy all auxdata
1b008 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20 74 68 entries, call th
1b009 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 is.** routine wi
1b00a 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 th mask==0..*/.S
1b00b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1b00c 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 id sqlite3VdbeDe
1b00d 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 65 leteAuxData(Vdbe
1b00e 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 2c Func *pVdbeFunc,
1b00f 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 69 6e int mask){. in
1b010 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1b011 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 i<pVdbeFunc->nAu
1b012 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 x; i++){. str
1b013 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 uct AuxData *pAu
1b014 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e x = &pVdbeFunc->
1b015 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20 69 66 apAux[i];. if
1b016 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d 61 73 ( (i>31 || !(mas
1b017 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69 29 29 k&(((u32)1)<<i))
1b018 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78 20 ) && pAux->pAux
1b019 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41 75 ){. if( pAu
1b01a 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 x->xDelete ){.
1b01b 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65 6c pAux->xDel
1b01c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29 3b ete(pAux->pAux);
1b01d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1b01e 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20 Aux->pAux = 0;.
1b01f 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1b020 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 * Delete an enti
1b021 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 re VDBE..*/.SQLI
1b022 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b023 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1b024 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 e(Vdbe *p){. sq
1b025 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
1b026 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20 ( NEVER(p==0) )
1b027 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 return;. db = p
1b028 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 70 ->db;. if( p->p
1b029 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d 3e 70 Prev ){. p->p
1b02a 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d Prev->pNext = p-
1b02b 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b >pNext;. }else{
1b02c 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d . assert( db-
1b02d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20 >pVdbe==p );.
1b02e 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e db->pVdbe = p->
1b02f 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 pNext;. }. if(
1b030 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 p->pNext ){.
1b031 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 p->pNext->pPrev
1b032 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 7d = p->pPrev;. }
1b033 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 . releaseMemArr
1b034 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e ay(p->aVar, p->n
1b035 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73 65 4d Var);. releaseM
1b036 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e emArray(p->aColN
1b037 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 ame, p->nResColu
1b038 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 mn*COLNAME_N);.
1b039 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 vdbeFreeOpArray
1b03a 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70 2d 3e (db, p->aOp, p->
1b03b 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 nOp);. sqlite3D
1b03c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 4c 61 bFree(db, p->aLa
1b03d 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 bel);. sqlite3D
1b03e 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f bFree(db, p->aCo
1b03f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 lName);. sqlite
1b040 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1b041 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 Sql);. p->magic
1b042 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 = VDBE_MAGIC_DE
1b043 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 AD;. sqlite3DbF
1b044 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 65 65 ree(db, p->pFree
1b045 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1b046 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a ee(db, p);.}../*
1b047 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
1b048 65 20 63 75 72 73 6f 72 20 70 20 69 73 20 72 65 e cursor p is re
1b049 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72 20 77 ady to read or w
1b04a 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 6f 20 rite the row to
1b04b 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61 73 20 which it.** was
1b04c 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65 64 2e last positioned.
1b04d 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f Return an erro
1b04e 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f 4f 4d r code if an OOM
1b04f 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20 65 72 fault or I/O er
1b050 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74 73 20 ror.** prevents
1b051 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69 6f 6e us from position
1b052 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 74 ing the cursor t
1b053 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 70 6f o its correct po
1b054 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 sition..**.** If
1b055 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74 a MoveTo operat
1b056 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f ion is pending o
1b057 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 n the given curs
1b058 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74 or, then do that
1b059 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20 .** MoveTo now.
1b05a 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73 20 70 If no move is p
1b05b 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20 74 6f ending, check to
1b05c 20 73 65 65 20 69 66 20 74 68 65 20 72 6f 77 20 see if the row
1b05d 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65 6c 65 has been.** dele
1b05e 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 ted out from und
1b05f 65 72 20 74 68 65 20 63 75 72 73 6f 72 20 61 6e er the cursor an
1b060 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d 61 72 d if it has, mar
1b061 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a 2a 20 k the row as.**
1b062 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a 0a 2a a NULL row..**.*
1b063 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 * If the cursor
1b064 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 is already point
1b065 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72 72 65 ing to the corre
1b066 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61 74 20 ct row and that
1b067 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 row has.** not b
1b068 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75 74 20 een deleted out
1b069 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 63 from under the c
1b06a 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68 69 73 ursor, then this
1b06b 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1b06c 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 -op..*/.SQLITE_P
1b06d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1b06e 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1b06f 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a 70 to(VdbeCursor *p
1b070 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 65 ){. if( p->defe
1b071 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 rredMoveto ){.
1b072 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a 23 int res, rc;.#
1b073 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1b074 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 T. extern int
1b075 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f sqlite3_search_
1b076 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 count;.#endif.
1b077 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 54 assert( p->isT
1b078 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 3d able );. rc =
1b079 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1b07a 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e 70 etoUnpacked(p->p
1b07b 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f Cursor, 0, p->mo
1b07c 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26 vetoTarget, 0, &
1b07d 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 res);. if( rc
1b07e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1b07f 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d p->lastRowid =
1b080 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 p->movetoTarget
1b081 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 73 ;. p->rowidIs
1b082 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 28 72 Valid = ALWAYS(r
1b083 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20 20 es==0) ?1:0;.
1b084 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 3c 30 if( NEVER(res<0
1b085 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
1b086 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
1b087 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 (p->pCursor, &re
1b088 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 s);. if( rc
1b089 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1b08a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
1b08b 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 E_TEST. sqlit
1b08c 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b e3_search_count+
1b08d 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 2d +;.#endif. p-
1b08e 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1b08f 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68 = 0;. p->cach
1b090 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1b091 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 69 STALE;. }else i
1b092 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70 43 75 f( ALWAYS(p->pCu
1b093 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 rsor) ){. int
1b094 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 20 69 hasMoved;. i
1b095 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 nt rc = sqlite3B
1b096 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 treeCursorHasMov
1b097 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 ed(p->pCursor, &
1b098 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 20 69 hasMoved);. i
1b099 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1b09a 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73 4d 6f c;. if( hasMo
1b09b 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ved ){. p->
1b09c 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1b09d 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 20 CHE_STALE;.
1b09e 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b p->nullRow = 1;
1b09f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1b0a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1b0a1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
1b0a2 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a owing functions:
1b0a3 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 .**.** sqlite3Vd
1b0a4 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a beSerialType().*
1b0a5 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 * sqlite3VdbeSer
1b0a6 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20 ialTypeLen().**
1b0a7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b0a8 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 lLen().** sqlite
1b0a9 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 29 3VdbeSerialPut()
1b0aa 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
1b0ab 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a 2a 2a erialGet().**.**
1b0ac 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65 encapsulate the
1b0ad 20 63 6f 64 65 20 74 68 61 74 20 73 65 72 69 61 code that seria
1b0ae 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66 6f 72 lizes values for
1b0af 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51 4c 69 storage in SQLi
1b0b0 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 20 69 te.** data and i
1b0b1 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20 45 61 ndex records. Ea
1b0b2 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 ch serialized va
1b0b3 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 lue consists of
1b0b4 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 79 70 a.** 'serial-typ
1b0b5 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 6f 66 e' and a blob of
1b0b6 20 64 61 74 61 2e 20 54 68 65 20 73 65 72 69 61 data. The seria
1b0b7 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38 2d 62 l type is an 8-b
1b0b8 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 yte unsigned.**
1b0b9 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 64 20 integer, stored
1b0ba 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a 2a 0a as a varint..**.
1b0bb 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 65 20 ** In an SQLite
1b0bc 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 74 68 index record, th
1b0bd 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 e serial type is
1b0be 20 73 74 6f 72 65 64 20 64 69 72 65 63 74 6c 79 stored directly
1b0bf 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 62 before.** the b
1b0c0 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68 61 74 lob of data that
1b0c1 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 it corresponds
1b0c2 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 20 72 to. In a table r
1b0c3 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 69 61 ecord, all seria
1b0c4 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 20 73 l.** types are s
1b0c5 74 6f 72 65 64 20 61 74 20 74 68 65 20 73 74 61 tored at the sta
1b0c6 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 rt of the record
1b0c7 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 73 20 , and the blobs
1b0c8 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 74 68 of data at.** th
1b0c9 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 68 65 e end. Hence the
1b0ca 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c se functions all
1b0cb 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f ow the caller to
1b0cc 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 73 handle the.** s
1b0cd 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 20 64 erial-type and d
1b0ce 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72 61 74 ata blob seperat
1b0cf 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ely..**.** The f
1b0d0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 ollowing table d
1b0d1 65 73 63 72 69 62 65 73 20 74 68 65 20 76 61 72 escribes the var
1b0d2 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63 6c 61 ious storage cla
1b0d3 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a 0a 2a sses for data:.*
1b0d4 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 74 79 *.** serial ty
1b0d5 70 65 20 20 20 20 20 20 20 20 62 79 74 65 73 20 pe bytes
1b0d6 6f 66 20 64 61 74 61 20 20 20 20 20 20 74 79 70 of data typ
1b0d7 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d e.** ---------
1b0d8 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d ----- ------
1b0d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d --------- ---
1b0da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 ------------.**
1b0db 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 0
1b0dc 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 0
1b0dd 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a 2a 20 NULL.**
1b0de 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 1
1b0df 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20 1
1b0e0 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 signed i
1b0e1 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 32 nteger.** 2
1b0e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0e3 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 2
1b0e4 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 signed integer
1b0e5 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20 .** 3
1b0e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
1b0e7 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
1b0e8 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
1b0e9 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 4
1b0ea 20 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20 4
1b0eb 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 signed int
1b0ec 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 20 20 eger.** 5
1b0ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0ee 20 20 20 36 20 20 20 20 20 20 20 20 20 20 20 20 6
1b0ef 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
1b0f0 2a 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 * 6
1b0f1 20 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 8
1b0f2 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1b0f3 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1b0f4 20 37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7
1b0f5 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 8
1b0f6 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74 0a 2a IEEE float.*
1b0f7 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 * 8
1b0f8 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1b0f9 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 Intege
1b0fa 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a 2a 20 r constant 0.**
1b0fb 20 20 20 20 20 39 20 20 20 20 20 20 20 20 20 20 9
1b0fc 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 0
1b0fd 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 20 Integer
1b0fe 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 20 20 constant 1.**
1b0ff 20 20 31 30 2c 31 31 20 20 20 20 20 20 20 20 20 10,11
1b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b101 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 66 reserved f
1b102 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a 2a 20 or expansion.**
1b103 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 76 65 N>=12 and eve
1b104 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29 2f 32 n (N-12)/2
1b105 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a 2a 20 BLOB.**
1b106 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64 N>=13 and odd
1b107 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29 2f 32 (N-13)/2
1b108 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a 2a 0a text.**.
1b109 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39 20 74 ** The 8 and 9 t
1b10a 79 70 65 73 20 77 65 72 65 20 61 64 64 65 64 20 ypes were added
1b10b 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66 in 3.3.0, file f
1b10c 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f 72 20 ormat 4. Prior
1b10d 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 versions.** of S
1b10e 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75 QLite will not u
1b10f 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73 65 20 nderstand those
1b110 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2f serial types..*/
1b111 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1b112 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65 20 66 he serial-type f
1b113 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f or the value sto
1b114 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a 2f 0a red in pMem..*/.
1b115 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
1b116 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 32 sqlite3VdbeSe
1b117 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a 70 4d rialType(Mem *pM
1b118 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 em, int file_for
1b119 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 mat){. int flag
1b11a 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b s = pMem->flags;
1b11b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 . int n;.. if(
1b11c 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 flags&MEM_Null
1b11d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1b11e 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 . }. if( flags
1b11f 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 &MEM_Int ){.
1b120 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 /* Figure out wh
1b121 65 74 68 65 72 20 74 6f 20 75 73 65 20 31 2c 20 ether to use 1,
1b122 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 79 74 2, 4, 6 or 8 byt
1b123 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e es. */.# defin
1b124 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28 28 28 e MAX_6BYTE ((((
1b125 69 36 34 29 30 78 30 30 30 30 38 30 30 30 29 3c i64)0x00008000)<
1b126 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36 34 20 <32)-1). i64
1b127 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 i = pMem->u.i;.
1b128 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20 69 66 u64 u;. if
1b129 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 ( file_format>=4
1b12a 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29 7b 0a && (i&1)==i ){.
1b12b 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 2b 28 return 8+(
1b12c 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20 u32)i;. }.
1b12d 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20 u = i<0 ? -i :
1b12e 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32 i;. if( u<=12
1b12f 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7 ) return 1;.
1b130 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29 if( u<=32767 )
1b131 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69 return 2;. i
1b132 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20 f( u<=8388607 )
1b133 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66 return 3;. if
1b134 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20 ( u<=2147483647
1b135 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 ) return 4;.
1b136 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45 if( u<=MAX_6BYTE
1b137 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20 ) return 5;.
1b138 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20 return 6;. }.
1b139 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52 if( flags&MEM_R
1b13a 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 eal ){. retur
1b13b 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 7;. }. asser
1b13c 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 61 6c t( pMem->db->mal
1b13d 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 6c 61 locFailed || fla
1b13e 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f gs&(MEM_Str|MEM_
1b13f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d 20 70 Blob) );. n = p
1b140 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 66 6c Mem->n;. if( fl
1b141 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
1b142 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 6d 2d {. n += pMem-
1b143 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 >u.nZero;. }.
1b144 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a assert( n>=0 );.
1b145 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 29 20 return ((n*2)
1b146 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 26 4d + 12 + ((flags&M
1b147 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a 7d 0a EM_Str)!=0));.}.
1b148 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1b149 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
1b14a 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 data correspondi
1b14b 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 ng to the suppli
1b14c 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 2e 0a ed serial-type..
1b14d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b14e 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 E u32 sqlite3Vdb
1b14f 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 eSerialTypeLen(u
1b150 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 29 7b 32 serial_type){
1b151 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 . if( serial_ty
1b152 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 72 65 pe>=12 ){. re
1b153 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 79 70 turn (serial_typ
1b154 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c 73 65 e-12)/2;. }else
1b155 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e {. static con
1b156 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 3d 20 st u8 aSize[] =
1b157 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 2c { 0, 1, 2, 3, 4,
1b158 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 2c 20 6, 8, 8, 0, 0,
1b159 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 74 75 0, 0 };. retu
1b15a 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 6c 5f rn aSize[serial_
1b15b 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a type];. }.}../*
1b15c 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6f 6e .** If we are on
1b15d 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 72 65 an architecture
1b15e 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e 64 69 with mixed-endi
1b15f 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20 an floating .**
1b160 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 4d 37 points (ex: ARM7
1b161 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 65 20 ) then swap the
1b162 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 77 69 lower 4 bytes wi
1b163 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 65 72 th the .** upper
1b164 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 75 72 4 bytes. Retur
1b165 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a n the result..**
1b166 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 72 63 .** For most arc
1b167 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 69 73 hitectures, this
1b168 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
1b169 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 74 20 ** (later): It
1b16a 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f 20 6d is reported to m
1b16b 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 65 64 e that the mixed
1b16c 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 6d 0a -endian problem.
1b16d 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 61 6e ** on ARM7 is an
1b16e 20 69 73 73 75 65 20 77 69 74 68 20 47 43 43 2c issue with GCC,
1b16f 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 41 52 not with the AR
1b170 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 65 65 M7 chip. It see
1b171 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 6c 79 ms.** that early
1b172 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43 versions of GCC
1b173 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 6f 20 stored the two
1b174 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d 62 69 words of a 64-bi
1b175 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 74 68 t.** float in th
1b176 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e 20 20 e wrong order.
1b177 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 20 68 And that error h
1b178 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 61 74 as been propagat
1b179 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e 63 65 ed.** ever since
1b17a 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 73 20 . The blame is
1b17b 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 not necessarily
1b17c 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 67 68 with GCC, though
1b17d 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 20 68 ..** GCC might h
1b17e 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 6e 67 ave just copying
1b17f 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 72 6f the problem fro
1b180 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 69 6c m a prior compil
1b181 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c 73 6f er..** I am also
1b182 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 65 72 told that newer
1b183 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43 versions of GCC
1b184 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 20 64 that follow a d
1b185 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 49 20 ifferent.** ABI
1b186 67 65 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 get the byte ord
1b187 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 er right..**.**
1b188 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 6e 67 Developers using
1b189 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 41 52 SQLite on an AR
1b18a 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 69 6c M7 should compil
1b18b 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 72 0a e and run their.
1b18c 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 ** application u
1b18d 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 44 45 sing -DSQLITE_DE
1b18e 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 20 6f BUG=1 at least o
1b18f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 55 47 nce. With DEBUG
1b190 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 6f 6d .** enabled, som
1b191 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f 77 20 e asserts below
1b192 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 61 74 will ensure that
1b193 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20 the byte order
1b194 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 20 70 of.** floating p
1b195 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 20 63 oint values is c
1b196 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 28 32 orrect..**.** (2
1b197 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72 61 6e 007-08-30) Fran
1b198 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73 20 73 k van Vugt has s
1b199 74 75 64 69 65 64 20 74 68 69 73 20 70 72 6f 62 tudied this prob
1b19a 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a 20 61 lem closely.** a
1b19b 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69 73 20 nd has send his
1b19c 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68 65 20 findings to the
1b19d 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70 65 72 SQLite developer
1b19e 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77 72 69 s. Frank.** wri
1b19f 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20 4c 69 tes that some Li
1b1a0 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66 66 65 nux kernels offe
1b1a1 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 r floating point
1b1a2 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65 6d 75 hardware.** emu
1b1a3 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73 lation that uses
1b1a4 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d 61 6e only 32-bit man
1b1a5 74 69 73 73 61 73 20 69 6e 73 74 65 61 64 20 6f tissas instead o
1b1a6 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34 38 2d f a full .** 48-
1b1a7 62 69 74 73 20 61 73 20 72 65 71 75 69 72 65 64 bits as required
1b1a8 20 62 79 20 74 68 65 20 49 45 45 45 20 73 74 61 by the IEEE sta
1b1a9 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20 69 73 ndard. (This is
1b1aa 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46 the.** CONFIG_F
1b1ab 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 69 6f PE_FASTFPE optio
1b1ac 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 79 73 n.) On such sys
1b1ad 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 tems, floating p
1b1ae 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73 77 61 oint.** byte swa
1b1af 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 76 65 pping becomes ve
1b1b0 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 20 ry complicated.
1b1b1 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 To avoid proble
1b1b2 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63 65 73 ms,.** the neces
1b1b3 73 61 72 79 20 62 79 74 65 20 73 77 61 70 70 69 sary byte swappi
1b1b4 6e 67 20 69 73 20 63 61 72 72 69 65 64 20 6f 75 ng is carried ou
1b1b5 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62 69 74 t using a 64-bit
1b1b6 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61 74 68 integer.** rath
1b1b7 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62 69 74 er than a 64-bit
1b1b8 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b 20 61 float. Frank a
1b1b9 73 73 75 72 65 73 20 75 73 20 74 68 61 74 20 74 ssures us that t
1b1ba 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 he code here.**
1b1bb 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e 20 20 works for him.
1b1bc 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f 70 65 We, the develope
1b1bd 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61 79 20 rs, have no way
1b1be 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 to independently
1b1bf 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69 73 2c .** verify this,
1b1c0 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65 6d 73 but Frank seems
1b1c1 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20 68 65 to know what he
1b1c2 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62 6f 75 is talking abou
1b1c3 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75 73 74 t.** so we trust
1b1c4 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 him..*/.#ifdef
1b1c5 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 SQLITE_MIXED_END
1b1c6 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a IAN_64BIT_FLOAT.
1b1c7 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f 61 74 static u64 float
1b1c8 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a 20 20 Swap(u64 in){.
1b1c9 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36 34 20 union {. u64
1b1ca 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32 5d 3b r;. u32 i[2];
1b1cb 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20 74 3b . } u;. u32 t;
1b1cc 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a 20 20 .. u.r = in;.
1b1cd 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20 75 2e t = u.i[0];. u.
1b1ce 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b 0a 20 i[0] = u.i[1];.
1b1cf 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20 20 72 u.i[1] = t;. r
1b1d0 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23 20 64 eturn u.r;.}.# d
1b1d1 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 efine swapMixedE
1b1d2 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20 20 58 ndianFloat(X) X
1b1d3 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58 29 0a = floatSwap(X).
1b1d4 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
1b1d5 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c wapMixedEndianFl
1b1d6 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f oat(X).#endif../
1b1d7 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 *.** Write the s
1b1d8 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 62 erialized data b
1b1d9 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61 6c 75 lob for the valu
1b1da 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d e stored in pMem
1b1db 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e 20 49 into .** buf. I
1b1dc 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1b1dd 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 t the caller has
1b1de 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66 66 69 allocated suffi
1b1df 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a 2a 20 cient space..**
1b1e0 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1b1e1 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 r of bytes writt
1b1e2 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66 20 69 en..**.** nBuf i
1b1e3 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 s the amount of
1b1e4 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 62 75 space left in bu
1b1e5 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 74 20 f[]. nBuf must
1b1e6 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c 61 72 always be.** lar
1b1e7 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c ge enough to hol
1b1e8 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 69 65 d the entire fie
1b1e9 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 ld. Except, if
1b1ea 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 the field is.**
1b1eb 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20 7a 65 a blob with a ze
1b1ec 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20 ro-filled tail,
1b1ed 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67 68 74 then buf[] might
1b1ee 20 62 65 20 6a 75 73 74 20 74 68 65 20 72 69 67 be just the rig
1b1ef 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20 68 6f ht.** size to ho
1b1f0 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20 65 78 ld everything ex
1b1f1 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a 65 72 cept for the zer
1b1f2 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e 20 20 o-filled tail.
1b1f3 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73 20 6f If buf[].** is o
1b1f4 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68 20 74 nly big enough t
1b1f5 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e 2d 7a o hold the non-z
1b1f6 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68 65 6e ero prefix, then
1b1f7 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 61 74 only write that
1b1f8 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74 6f 20 .** prefix into
1b1f9 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66 20 62 buf[]. But if b
1b1fa 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20 65 6e uf[] is large en
1b1fb 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62 6f 74 ough to hold bot
1b1fc 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 h the.** prefix
1b1fd 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74 68 65 and the tail the
1b1fe 6e 20 77 72 69 74 65 20 74 68 65 20 70 72 65 66 n write the pref
1b1ff 69 78 20 61 6e 64 20 73 65 74 20 74 68 65 20 74 ix and set the t
1b200 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20 7a 65 ail to all.** ze
1b201 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ros..**.** Retur
1b202 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1b203 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 77 bytes actually w
1b204 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75 66 5b ritten into buf[
1b205 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a ]. The number.*
1b206 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 * of bytes in th
1b207 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 e zero-filled ta
1b208 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 il is included i
1b209 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c n the return val
1b20a 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 ue only.** if th
1b20b 6f 73 65 20 62 79 74 65 73 20 77 65 72 65 20 7a ose bytes were z
1b20c 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d 2e 0a eroed in buf[]..
1b20d 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */ .SQLITE_PRIVA
1b20e 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 TE u32 sqlite3Vd
1b20f 62 65 53 65 72 69 61 6c 50 75 74 28 75 38 20 2a beSerialPut(u8 *
1b210 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c 20 4d buf, int nBuf, M
1b211 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 em *pMem, int fi
1b212 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 75 33 le_format){. u3
1b213 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 2 serial_type =
1b214 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1b215 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69 6c 65 lType(pMem, file
1b216 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33 32 20 _format);. u32
1b217 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 67 len;.. /* Integ
1b218 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f 0a 20 er and Real */.
1b219 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 if( serial_type
1b21a 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f 74 79 <=7 && serial_ty
1b21b 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36 34 20 pe>0 ){. u64
1b21c 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a 20 20 v;. u32 i;.
1b21d 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
1b21e 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 61 73 e==7 ){. as
1b21f 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76 29 3d sert( sizeof(v)=
1b220 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 =sizeof(pMem->r)
1b221 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 );. memcpy
1b222 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c 20 73 (&v, &pMem->r, s
1b223 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 20 20 izeof(v));.
1b224 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e swapMixedEndian
1b225 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20 7d 65 Float(v);. }e
1b226 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d 20 70 lse{. v = p
1b227 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a Mem->u.i;. }.
1b228 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 73 71 len = i = sq
1b229 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1b22a 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 ypeLen(serial_ty
1b22b 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pe);. assert(
1b22c 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75 66 20 len<=(u32)nBuf
1b22d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d );. while( i-
1b22e 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69 - ){. buf[i
1b22f 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 ] = (u8)(v&0xFF)
1b230 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b ;. v >>= 8;
1b231 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
1b232 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a n len;. }.. /*
1b233 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 String or blob
1b234 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f */. if( serial_
1b235 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 type>=12 ){.
1b236 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 assert( pMem->n
1b237 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 + ((pMem->flags
1b238 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d & MEM_Zero)?pMem
1b239 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20 ->u.nZero:0).
1b23a 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28 69 6e == (in
1b23b 74 29 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t)sqlite3VdbeSer
1b23c 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
1b23d 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 l_type) );. a
1b23e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d ssert( pMem->n<=
1b23f 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 nBuf );. len
1b240 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d = pMem->n;. m
1b241 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d emcpy(buf, pMem-
1b242 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 >z, len);. if
1b243 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
1b244 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
1b245 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 len += pMem->u
1b246 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 61 73 .nZero;. as
1b247 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20 29 3b sert( nBuf>=0 );
1b248 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 20 3e . if( len >
1b249 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a 20 20 (u32)nBuf ){.
1b24a 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75 33 32 len = (u32
1b24b 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 0a 20 )nBuf;. }.
1b24c 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 75 66 memset(&buf
1b24d 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 [pMem->n], 0, le
1b24e 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 n-pMem->n);.
1b24f 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e }. return len
1b250 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 4c 4c ;. }.. /* NULL
1b251 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 30 20 or constants 0
1b252 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e or 1 */. return
1b253 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 0;.}../*.** Des
1b254 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74 erialize the dat
1b255 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 20 74 a blob pointed t
1b256 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 72 69 o by buf as seri
1b257 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c 5f 74 al type serial_t
1b258 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 ype.** and store
1b259 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 70 the result in p
1b25a 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 Mem. Return the
1b25b 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1b25c 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 read..*/ .SQLIT
1b25d 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
1b25e 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
1b25f 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 et(. const unsi
1b260 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c 20 gned char *buf,
1b261 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f /* Buffer to
1b262 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72 6f deserialize fro
1b263 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 m */. u32 seria
1b264 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 l_type,
1b265 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 /* Serial t
1b266 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c 69 ype to deseriali
1b267 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 ze */. Mem *pMe
1b268 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m
1b269 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
1b26a 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 76 61 cell to write va
1b26b 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 lue into */.){.
1b26c 20 73 77 69 74 63 68 28 20 73 65 72 69 61 6c 5f switch( serial_
1b26d 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 type ){. case
1b26e 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 10: /* Reserv
1b26f 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 ed for future us
1b270 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 31 e */. case 11
1b271 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 : /* Reserved
1b272 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 20 2a for future use *
1b273 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b 20 /. case 0: {
1b274 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 /* NULL */.
1b275 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b276 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 MEM_Null;.
1b277 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1b278 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 31 2d case 1: { /* 1-
1b279 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
1b27a 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ger */. pMe
1b27b 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e 65 64 m->u.i = (signed
1b27c 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a 20 20 char)buf[0];.
1b27d 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b27e 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
1b27f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1b280 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 2f . case 2: { /
1b281 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 2-byte signed
1b282 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 integer */.
1b283 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 pMem->u.i = (((
1b284 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b signed char)buf[
1b285 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 0])<<8) | buf[1]
1b286 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1b287 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1b288 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 return 2;.
1b289 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 33 3a }. case 3:
1b28a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 69 67 { /* 3-byte sig
1b28b 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
1b28c 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d pMem->u.i =
1b28d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 (((signed char)
1b28e 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c 20 28 buf[0])<<16) | (
1b28f 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 75 66 buf[1]<<8) | buf
1b290 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d [2];. pMem-
1b291 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1b292 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 33 ;. return 3
1b293 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1b294 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 65 20 4: { /* 4-byte
1b295 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
1b296 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1b297 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 i = (buf[0]<<24)
1b298 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 | (buf[1]<<16)
1b299 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 | (buf[2]<<8) |
1b29a 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 70 4d buf[3];. pM
1b29b 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1b29c 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1b29d 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 4;. }. c
1b29e 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62 79 ase 5: { /* 6-by
1b29f 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1b2a0 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 r */. u64 x
1b2a1 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 = (((signed cha
1b2a2 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 r)buf[0])<<8) |
1b2a3 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 75 33 buf[1];. u3
1b2a4 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c 32 2 y = (buf[2]<<2
1b2a5 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31 36 4) | (buf[3]<<16
1b2a6 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29 20 ) | (buf[4]<<8)
1b2a7 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 20 20 | buf[5];.
1b2a8 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 3b x = (x<<32) | y;
1b2a9 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1b2aa 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 = *(i64*)&x;.
1b2ab 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b2ac 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
1b2ad 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 20 7d return 6;. }
1b2ae 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 20 2f . case 6: /
1b2af 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 8-byte signed
1b2b0 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 63 integer */. c
1b2b1 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 45 45 ase 7: { /* IEEE
1b2b2 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
1b2b3 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 3b 0a */. u64 x;.
1b2b4 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 69 66 u32 y;.#if
1b2b5 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
1b2b6 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1b2b7 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1b2b8 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 20 20 NG_POINT).
1b2b9 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 69 /* Verify that i
1b2ba 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c 6f 61 ntegers and floa
1b2bb 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
1b2bc 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 20 s use the same.
1b2bd 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f 72 64 ** byte ord
1b2be 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 69 66 er. Or, that if
1b2bf 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e SQLITE_MIXED_EN
1b2c0 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 DIAN_64BIT_FLOAT
1b2c1 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 66 is. ** def
1b2c2 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 69 74 ined that 64-bit
1b2c3 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
1b2c4 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 61 72 values really ar
1b2c5 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 2a 2a e mixed. **
1b2c6 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 20 2a endian.. *
1b2c7 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 /. static c
1b2c8 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28 28 onst u64 t1 = ((
1b2c9 75 36 34 29 30 78 33 66 66 30 30 30 30 30 29 3c u64)0x3ff00000)<
1b2ca 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 74 69 <32;. stati
1b2cb 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 20 72 c const double r
1b2cc 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 75 1 = 1.0;. u
1b2cd 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 20 20 64 t2 = t1;.
1b2ce 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 swapMixedEndia
1b2cf 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 20 20 nFloat(t2);.
1b2d0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
1b2d1 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 32 29 (r1)==sizeof(t2)
1b2d2 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2c 20 && memcmp(&r1,
1b2d3 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 29 29 &t2, sizeof(r1))
1b2d4 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 ==0 );.#endif..
1b2d5 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b 30 5d x = (buf[0]
1b2d6 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c <<24) | (buf[1]<
1b2d7 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c <16) | (buf[2]<<
1b2d8 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 8) | buf[3];.
1b2d9 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d 3c 3c y = (buf[4]<<
1b2da 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c 3c 31 24) | (buf[5]<<1
1b2db 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c 38 29 6) | (buf[6]<<8)
1b2dc 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 20 20 | buf[7];.
1b2dd 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 x = (x<<32) | y
1b2de 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 ;. if( seri
1b2df 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a 20 20 al_type==6 ){.
1b2e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
1b2e1 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 20 = *(i64*)&x;.
1b2e2 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b2e3 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1b2e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1b2e5 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
1b2e6 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66 28 x)==8 && sizeof(
1b2e7 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0a 20 pMem->r)==8 );.
1b2e8 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 swapMixed
1b2e9 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b 0a EndianFloat(x);.
1b2ea 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1b2eb 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69 7a pMem->r, &x, siz
1b2ec 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 20 20 eof(x));.
1b2ed 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 73 pMem->flags = s
1b2ee 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d 65 6d qlite3IsNaN(pMem
1b2ef 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c 6c 20 ->r) ? MEM_Null
1b2f0 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 : MEM_Real;.
1b2f1 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
1b2f2 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 8;. }. ca
1b2f3 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e 74 65 se 8: /* Inte
1b2f4 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 61 73 ger 0 */. cas
1b2f5 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 65 67 e 9: { /* Integ
1b2f6 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 70 4d er 1 */. pM
1b2f7 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 61 6c em->u.i = serial
1b2f8 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 20 70 _type-8;. p
1b2f9 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b2fa 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
1b2fb 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 0;. }.
1b2fc 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
1b2fd 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65 72 69 u32 len = (seri
1b2fe 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 al_type-12)/2;.
1b2ff 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 pMem->z = (
1b300 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 20 20 char *)buf;.
1b301 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b pMem->n = len;
1b302 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 . pMem->xDe
1b303 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 l = 0;. if(
1b304 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 78 30 serial_type&0x0
1b305 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 1 ){. pMe
1b306 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 m->flags = MEM_S
1b307 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a tr | MEM_Ephem;.
1b308 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1b309 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b30a 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 = MEM_Blob | ME
1b30b 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 7d M_Ephem;. }
1b30c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6c 65 . return le
1b30d 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 n;. }. }. r
1b30e 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 0;.}.../*.
1b30f 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 4b 65 ** Given the nKe
1b310 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e 67 20 y-byte encoding
1b311 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e 20 70 of a record in p
1b312 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 68 65 Key[], parse the
1b313 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 .** record into
1b314 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 a UnpackedRecord
1b315 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 74 structure. Ret
1b316 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1b317 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 74 75 .** that structu
1b318 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 re..**.** The ca
1b319 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d lling function m
1b31a 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 7a 53 ight provide szS
1b31b 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 6d 65 pace bytes of me
1b31c 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 61 74 mory.** space at
1b31d 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 20 73 pSpace. This s
1b31e 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 pace can be used
1b31f 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
1b320 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 61 72 urned.** VDbePar
1b321 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 63 74 sedRecord struct
1b322 75 72 65 20 69 66 20 69 74 20 69 73 20 6c 61 72 ure if it is lar
1b323 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 20 69 ge enough. If i
1b324 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 67 20 t is.** not big
1b325 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 69 73 enough, space is
1b326 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
1b327 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
1b328 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1b329 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 73 68 ned structure sh
1b32a 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 62 ould be closed b
1b32b 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 y a call to.** s
1b32c 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1b32d 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 29 UnpackedRecord()
1b32e 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
1b32f 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 VATE UnpackedRec
1b330 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 ord *sqlite3Vdbe
1b331 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a 20 20 RecordUnpack(.
1b332 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 KeyInfo *pKeyInf
1b333 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d o, /* Inform
1b334 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
1b335 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 2a 2f record format */
1b336 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 . int nKey,
1b337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1b338 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 79 20 e of the binary
1b339 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 record */. cons
1b33a 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 t void *pKey,
1b33b 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 72 79 /* The binary
1b33c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 record */. cha
1b33d 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20 20 20 r *pSpace,
1b33e 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e 65 64 /* Unaligned
1b33f 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 space available
1b340 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a to hold the obj
1b341 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 ect */. int szS
1b342 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 pace
1b343 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61 63 /* Size of pSpac
1b344 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a e[] in bytes */.
1b345 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
1b346 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20 3d ned char *aKey =
1b347 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
1b348 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 char *)pKey;.
1b349 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1b34a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70 61 63 p; /* The unpac
1b34b 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 ked record that
1b34c 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2a we will return *
1b34d 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 /. int nByte;
1b34e 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
1b34f 79 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74 y space needed t
1b350 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62 79 74 o hold p, in byt
1b351 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b 0a 20 es */. int d;.
1b352 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31 36 20 u32 idx;. u16
1b353 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 u;
1b354 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 /* Unsigned loop
1b355 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 33 counter */. u3
1b356 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d 20 2a 2 szHdr;. Mem *
1b357 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f 66 66 pMem;. int nOff
1b358 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ; /* I
1b359 6e 63 72 65 61 73 65 20 70 53 70 61 63 65 20 62 ncrease pSpace b
1b35a 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f 20 38 y this much to 8
1b35b 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74 20 2a -byte align it *
1b35c 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 /. . /*. ** W
1b35d 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66 74 20 e want to shift
1b35e 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53 70 61 the pointer pSpa
1b35f 63 65 20 75 70 20 73 75 63 68 20 74 68 61 74 20 ce up such that
1b360 69 74 20 69 73 20 38 2d 62 79 74 65 20 61 6c 69 it is 8-byte ali
1b361 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75 73 2c gned.. ** Thus,
1b362 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61 6c 63 we need to calc
1b363 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c 20 6e ulate a value, n
1b364 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30 20 61 Off, between 0 a
1b365 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74 20 0a nd 7, to shift .
1b366 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 66 20 ** it by. If
1b367 70 53 70 61 63 65 20 69 73 20 61 6c 72 65 61 64 pSpace is alread
1b368 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 y 8-byte aligned
1b369 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 62 65 , nOff should be
1b36a 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 6e 4f zero.. */. nO
1b36b 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c 49 54 ff = (8 - (SQLIT
1b36c 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 53 70 E_PTR_TO_INT(pSp
1b36d 61 63 65 29 20 26 20 37 29 29 20 26 20 37 3b 0a ace) & 7)) & 7;.
1b36e 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f 66 66 pSpace += nOff
1b36f 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d 20 6e ;. szSpace -= n
1b370 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 Off;. nByte = R
1b371 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 OUND8(sizeof(Unp
1b372 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 ackedRecord)) +
1b373 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 sizeof(Mem)*(pKe
1b374 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 29 yInfo->nField+1)
1b375 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a ;. if( nByte>sz
1b376 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d Space ){. p =
1b377 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1b378 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 Raw(pKeyInfo->db
1b379 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 , nByte);. if
1b37a 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
1b37b 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 0;. p->flags
1b37c 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f = UNPACKED_NEED_
1b37d 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f FREE | UNPACKED_
1b37e 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 NEED_DESTROY;.
1b37f 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 28 }else{. p = (
1b380 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 UnpackedRecord*)
1b381 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66 pSpace;. p->f
1b382 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
1b383 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 NEED_DESTROY;.
1b384 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 }. p->pKeyInfo
1b385 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d = pKeyInfo;. p-
1b386 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e >nField = pKeyIn
1b387 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a fo->nField + 1;.
1b388 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d p->aMem = pMem
1b389 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68 61 72 = (Mem*)&((char
1b38a 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 *)p)[ROUND8(size
1b38b 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 of(UnpackedRecor
1b38c 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 d))];. assert(
1b38d 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
1b38e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 MENT(pMem) );.
1b38f 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 idx = getVarint3
1b390 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 0a 2(aKey, szHdr);.
1b391 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 75 d = szHdr;. u
1b392 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 = 0;. while( i
1b393 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c 70 2d dx<szHdr && u<p-
1b394 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d 6e 4b >nField && d<=nK
1b395 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 ey ){. u32 se
1b396 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 rial_type;..
1b397 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 idx += getVarint
1b398 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 32(&aKey[idx], s
1b399 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 erial_type);.
1b39a 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 4b 65 pMem->enc = pKe
1b39b 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 20 20 yInfo->enc;.
1b39c 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 79 49 pMem->db = pKeyI
1b39d 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 4d 65 nfo->db;. pMe
1b39e 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 m->flags = 0;.
1b39f 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 pMem->zMalloc
1b3a0 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 73 71 = 0;. d += sq
1b3a1 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
1b3a2 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 65 72 et(&aKey[d], ser
1b3a3 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d 29 3b ial_type, pMem);
1b3a4 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 . pMem++;.
1b3a5 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 u++;. }. asse
1b3a6 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 6f 2d rt( u<=pKeyInfo-
1b3a7 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b 0a 20 >nField + 1 );.
1b3a8 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 3b 0a p->nField = u;.
1b3a9 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
1b3aa 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 p;.}../*.** This
1b3ab 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f 79 routine destroy
1b3ac 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f s a UnpackedReco
1b3ad 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 rd object..*/.SQ
1b3ae 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1b3af 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
1b3b0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
1b3b1 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d(UnpackedRecord
1b3b2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
1b3b3 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20 20 61 Mem *pMem;.. a
1b3b4 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1b3b5 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 assert( p->flag
1b3b6 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s & UNPACKED_NEE
1b3b7 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20 20 66 D_DESTROY );. f
1b3b8 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 2d 3e or(i=0, pMem=p->
1b3b9 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 65 6c aMem; i<p->nFiel
1b3ba 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b d; i++, pMem++){
1b3bb 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 70 61 . /* The unpa
1b3bc 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73 20 61 cked record is a
1b3bd 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63 74 65 lways constructe
1b3be 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 d by the. **
1b3bf 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70 61 63 sqlite3VdbeUnpac
1b3c0 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63 74 69 kRecord() functi
1b3c1 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63 68 20 on above, which
1b3c2 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20 2a 2a makes all. **
1b3c3 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62 6c 6f strings and blo
1b3c4 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e 64 20 bs static. And
1b3c5 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c 65 6d none of the elem
1b3c6 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 ents are. **
1b3c7 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d 65 64 ever transformed
1b3c8 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 , so there is ne
1b3c9 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 ver anything to
1b3ca 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f 0a 20 delete.. */.
1b3cb 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 4d 65 if( NEVER(pMe
1b3cc 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20 73 71 m->zMalloc) ) sq
1b3cd 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1b3ce 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d 0a 20 ase(pMem);. }.
1b3cf 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 if( p->flags &
1b3d0 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 UNPACKED_NEED_FR
1b3d1 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 EE ){. sqlite
1b3d2 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65 79 49 3DbFree(p->pKeyI
1b3d3 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d nfo->db, p);. }
1b3d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1b3d5 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73 unction compares
1b3d6 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 20 72 the two table r
1b3d7 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72 65 63 ows or index rec
1b3d8 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 ords.** specifie
1b3d9 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 d by {nKey1, pKe
1b3da 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 2e 20 y1} and pPKey2.
1b3db 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 6e 65 It returns a ne
1b3dc 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 gative, zero.**
1b3dd 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 or positive inte
1b3de 67 65 72 20 69 66 20 6b 65 79 31 20 69 73 20 6c ger if key1 is l
1b3df 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 ess than, equal
1b3e0 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 to or .** greate
1b3e1 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 54 68 r than key2. Th
1b3e2 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d e {nKey1, pKey1}
1b3e3 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 62 key must be a b
1b3e4 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 lob.** created b
1b3e5 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f y th OP_MakeReco
1b3e6 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 rd opcode of the
1b3e7 20 56 44 42 45 2e 20 20 54 68 65 20 70 50 4b 65 VDBE. The pPKe
1b3e8 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62 y2.** key must b
1b3e9 65 20 61 20 70 61 72 73 65 64 20 6b 65 79 20 73 e a parsed key s
1b3ea 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65 64 20 uch as obtained
1b3eb 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 from.** sqlite3V
1b3ec 64 62 65 50 61 72 73 65 52 65 63 6f 72 64 2e 0a dbeParseRecord..
1b3ed 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b **.** Key1 and K
1b3ee 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 ey2 do not have
1b3ef 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 to contain the s
1b3f0 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 ame number of fi
1b3f1 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 elds..** The key
1b3f2 20 77 69 74 68 20 66 65 77 65 72 20 66 69 65 6c with fewer fiel
1b3f3 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20 63 6f ds is usually co
1b3f4 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68 61 6e mpares less than
1b3f5 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 the .** longer
1b3f6 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20 69 66 key. However if
1b3f7 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e the UNPACKED_IN
1b3f8 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e 20 70 CRKEY flags in p
1b3f9 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a 2a 20 PKey2 is set.**
1b3fa 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 and the common p
1b3fb 72 65 66 69 78 65 73 20 61 72 65 20 65 71 75 61 refixes are equa
1b3fc 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 73 20 l, then key1 is
1b3fd 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a less than key2..
1b3fe 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55 4e 50 ** Or if the UNP
1b3ff 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 ACKED_MATCH_PREF
1b400 49 58 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 IX flag is set a
1b401 6e 64 20 74 68 65 20 70 72 65 66 69 78 65 73 20 nd the prefixes
1b402 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 are.** equal, th
1b403 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72 65 20 en the keys are
1b404 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 considered to be
1b405 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 equal and.** th
1b406 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64 20 74 e parts beyond t
1b407 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 he common prefix
1b408 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a are ignored..**
1b409 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 41 43 .** If the UNPAC
1b40a 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 KED_IGNORE_ROWID
1b40b 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
1b40c 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 en the last byte
1b40d 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 of.** the heade
1b40e 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20 69 67 r of pKey1 is ig
1b40f 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20 61 73 nored. It is as
1b410 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65 79 31 sumed that pKey1
1b411 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20 is.** an index
1b412 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20 65 6e key, and thus en
1b413 64 73 20 77 69 74 68 20 61 20 72 6f 77 69 64 20 ds with a rowid
1b414 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 73 74 value. The last
1b415 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 byte.** of the
1b416 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68 65 72 header will ther
1b417 65 66 6f 72 65 20 62 65 20 74 68 65 20 73 65 72 efore be the ser
1b418 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 ial type of the
1b419 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 rowid:.** one of
1b41a 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20 1, 2, 3, 4, 5,
1b41b 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 68 65 6, 8, or 9 - the
1b41c 20 69 6e 74 65 67 65 72 20 73 65 72 69 61 6c 20 integer serial
1b41d 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65 types..** The se
1b41e 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 rial type of the
1b41f 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c final rowid wil
1b420 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20 73 69 l always be a si
1b421 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79 ngle byte..** By
1b422 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c ignoring this l
1b423 61 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 ast byte of the
1b424 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72 63 65 header, we force
1b425 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a the comparison.
1b426 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 ** to ignore the
1b427 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e rowid at the en
1b428 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 d of key1..*/.SQ
1b429 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1b42a 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f sqlite3VdbeReco
1b42b 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 rdCompare(. int
1b42c 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f nKey1, const vo
1b42d 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 id *pKey1, /* Le
1b42e 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 ft key */. Unpa
1b42f 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 ckedRecord *pPKe
1b430 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 y2 /* Rig
1b431 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 ht key */.){. i
1b432 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20 20 20 nt d1;
1b433 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
1b434 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 aKey[] of next
1b435 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a data element */.
1b436 20 20 75 33 32 20 69 64 78 31 3b 20 20 20 20 20 u32 idx1;
1b437 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
1b438 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 nto aKey[] of ne
1b439 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e xt header elemen
1b43a 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 t */. u32 szHdr
1b43b 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 1; /* Num
1b43c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1b43d 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 header */. int
1b43e 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 i = 0;. int nFi
1b43f 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 eld;. int rc =
1b440 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 0;. const unsig
1b441 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20 ned char *aKey1
1b442 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 = (const unsigne
1b443 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a d char *)pKey1;.
1b444 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
1b445 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b nfo;. Mem mem1;
1b446 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 .. pKeyInfo = p
1b447 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b PKey2->pKeyInfo;
1b448 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b . mem1.enc = pK
1b449 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d eyInfo->enc;. m
1b44a 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 em1.db = pKeyInf
1b44b 6f 2d 3e 64 62 3b 0a 20 20 2f 2a 20 6d 65 6d 31 o->db;. /* mem1
1b44c 2e 66 6c 61 67 73 20 3d 20 30 3b 20 20 2f 2f 20 .flags = 0; //
1b44d 57 69 6c 6c 20 62 65 20 69 6e 69 74 69 61 6c 69 Will be initiali
1b44e 7a 65 64 20 62 79 20 73 71 6c 69 74 65 33 56 64 zed by sqlite3Vd
1b44f 62 65 53 65 72 69 61 6c 47 65 74 28 29 20 2a 2f beSerialGet() */
1b450 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 6d 65 6d . VVA_ONLY( mem
1b451 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 20 29 1.zMalloc = 0; )
1b452 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 65 64 20 /* Only needed
1b453 62 79 20 61 73 73 65 72 74 28 29 20 73 74 61 74 by assert() stat
1b454 65 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 ements */.. /*
1b455 43 6f 6d 70 69 6c 65 72 73 20 6d 61 79 20 63 6f Compilers may co
1b456 6d 70 6c 61 69 6e 20 74 68 61 74 20 6d 65 6d 31 mplain that mem1
1b457 2e 75 2e 69 20 69 73 20 70 6f 74 65 6e 74 69 61 .u.i is potentia
1b458 6c 6c 79 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 lly uninitialize
1b459 64 2e 0a 20 20 2a 2a 20 57 65 20 63 6f 75 6c 64 d.. ** We could
1b45a 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2c 20 initialize it,
1b45b 61 73 20 73 68 6f 77 6e 20 68 65 72 65 2c 20 74 as shown here, t
1b45c 6f 20 73 69 6c 65 6e 63 65 20 74 68 6f 73 65 20 o silence those
1b45d 63 6f 6d 70 6c 61 69 6e 74 73 2e 0a 20 20 2a 2a complaints.. **
1b45e 20 42 75 74 20 69 6e 20 66 61 63 74 2c 20 6d 65 But in fact, me
1b45f 6d 31 2e 75 2e 69 20 77 69 6c 6c 20 6e 65 76 65 m1.u.i will neve
1b460 72 20 61 63 74 75 61 6c 6c 79 20 62 65 20 75 73 r actually be us
1b461 65 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 ed initialized,
1b462 61 6e 64 20 64 6f 69 6e 67 20 0a 20 20 2a 2a 20 and doing . **
1b463 74 68 65 20 75 6e 6e 65 63 65 73 73 61 72 79 20 the unnecessary
1b464 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 initialization h
1b465 61 73 20 61 20 6d 65 61 73 75 72 61 62 6c 65 20 as a measurable
1b466 6e 65 67 61 74 69 76 65 20 70 65 72 66 6f 72 6d negative perform
1b467 61 6e 63 65 0a 20 20 2a 2a 20 69 6d 70 61 63 74 ance. ** impact
1b468 2c 20 73 69 6e 63 65 20 74 68 69 73 20 72 6f 75 , since this rou
1b469 74 69 6e 65 20 69 73 20 61 20 76 65 72 79 20 68 tine is a very h
1b46a 69 67 68 20 72 75 6e 6e 65 72 2e 20 20 41 6e 64 igh runner. And
1b46b 20 73 6f 2c 20 77 65 20 63 68 6f 6f 73 65 0a 20 so, we choose.
1b46c 20 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 ** to ignore th
1b46d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 e compiler warni
1b46e 6e 67 73 20 61 6e 64 20 6c 65 61 76 65 20 74 68 ngs and leave th
1b46f 69 73 20 76 61 72 69 61 62 6c 65 20 75 6e 69 6e is variable unin
1b470 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a itialized.. */.
1b471 20 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e 69 20 3d /* mem1.u.i =
1b472 20 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e 65 65 64 0; // not need
1b473 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c 65 ed, here to sile
1b474 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 nce compiler war
1b475 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 69 64 78 ning */. . idx
1b476 31 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 1 = getVarint32(
1b477 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a aKey1, szHdr1);.
1b478 20 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 d1 = szHdr1;.
1b479 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 if( pPKey2->fla
1b47a 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 gs & UNPACKED_IG
1b47b 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 NORE_ROWID ){.
1b47c 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a szHdr1--;. }.
1b47d 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 nField = pKeyI
1b47e 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 nfo->nField;. w
1b47f 68 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 hile( idx1<szHdr
1b480 31 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 1 && i<pPKey2->n
1b481 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 Field ){. u32
1b482 20 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a serial_type1;..
1b483 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 /* Read the
1b484 73 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72 serial types for
1b485 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e the next elemen
1b486 74 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a t in each key. *
1b487 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65 /. idx1 += ge
1b488 74 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31 tVarint32( aKey1
1b489 2b 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 +idx1, serial_ty
1b48a 70 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64 pe1 );. if( d
1b48b 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 1>=nKey1 && sqli
1b48c 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1b48d 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1b48e 31 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 1)>0 ) break;..
1b48f 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 /* Extract th
1b490 65 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63 e values to be c
1b491 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a ompared.. */.
1b492 20 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 d1 += sqlite
1b493 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 3VdbeSerialGet(&
1b494 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 aKey1[d1], seria
1b495 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b l_type1, &mem1);
1b496 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 .. /* Do the
1b497 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a comparison. *
1b498 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1b499 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 e3MemCompare(&me
1b49a 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 m1, &pPKey2->aMe
1b49b 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 m[i],.
1b49c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b49d 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 i<nField ? pKey
1b49e 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a Info->aColl[i] :
1b49f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 0);. if( rc!
1b4a0 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1b4a1 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 rt( mem1.zMalloc
1b4a2 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 65 65 20 63 ==0 ); /* See c
1b4a3 6f 6d 6d 65 6e 74 20 62 65 6c 6f 77 20 2a 2f 0a omment below */.
1b4a4 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 65 72 74 . /* Invert
1b4a5 20 74 68 65 20 72 65 73 75 6c 74 20 69 66 20 77 the result if w
1b4a6 65 20 61 72 65 20 75 73 69 6e 67 20 44 45 53 43 e are using DESC
1b4a7 20 73 6f 72 74 20 6f 72 64 65 72 2e 20 2a 2f 0a sort order. */.
1b4a8 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e if( pKeyIn
1b4a9 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 fo->aSortOrder &
1b4aa 26 20 69 3c 6e 46 69 65 6c 64 20 26 26 20 70 4b & i<nField && pK
1b4ab 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
1b4ac 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 er[i] ){.
1b4ad 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 20 20 20 rc = -rc;.
1b4ae 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 2f 2a }. . /*
1b4af 20 49 66 20 74 68 65 20 50 52 45 46 49 58 5f 53 If the PREFIX_S
1b4b0 45 41 52 43 48 20 66 6c 61 67 20 69 73 20 73 65 EARCH flag is se
1b4b1 74 20 61 6e 64 20 61 6c 6c 20 66 69 65 6c 64 73 t and all fields
1b4b2 20 65 78 63 65 70 74 20 74 68 65 20 66 69 6e 61 except the fina
1b4b3 6c 0a 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 l. ** rowid
1b4b4 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 field were equa
1b4b5 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 l, then clear th
1b4b6 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 e PREFIX_SEARCH
1b4b7 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 flag and set .
1b4b8 20 20 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 ** pPKey2->r
1b4b9 6f 77 69 64 20 74 6f 20 74 68 65 20 76 61 6c 75 owid to the valu
1b4ba 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 e of the rowid f
1b4bb 69 65 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c 20 ield in (pKey1,
1b4bc 6e 4b 65 79 31 29 2e 0a 20 20 20 20 20 20 2a 2a nKey1).. **
1b4bd 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
1b4be 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 the OP_IsUnique
1b4bf 20 6f 70 63 6f 64 65 2e 0a 20 20 20 20 20 20 2a opcode.. *
1b4c0 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 50 4b /. if( (pPK
1b4c1 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
1b4c2 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1b4c3 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 RCH) && i==(pPKe
1b4c4 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b y2->nField-1) ){
1b4c5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1b4c6 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 20 26 26 idx1==szHdr1 &&
1b4c7 20 72 63 20 29 3b 0a 20 20 20 20 20 20 20 20 61 rc );. a
1b4c8 73 73 65 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67 ssert( mem1.flag
1b4c9 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 s & MEM_Int );.
1b4ca 20 20 20 20 20 20 20 70 50 4b 65 79 32 2d 3e 66 pPKey2->f
1b4cb 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41 43 4b 45 lags &= ~UNPACKE
1b4cc 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 3b D_PREFIX_SEARCH;
1b4cd 0a 20 20 20 20 20 20 20 20 70 50 4b 65 79 32 2d . pPKey2-
1b4ce 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e >rowid = mem1.u.
1b4cf 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0a i;. }. .
1b4d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1b4d1 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a . }. i++;.
1b4d2 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d }.. /* No mem
1b4d3 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
1b4d4 73 20 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6d s ever used on m
1b4d5 65 6d 31 2e 20 20 50 72 6f 76 65 20 74 68 69 73 em1. Prove this
1b4d6 20 75 73 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 using. ** the
1b4d7 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
1b4d8 28 29 2e 20 20 49 66 20 74 68 65 20 61 73 73 65 (). If the asse
1b4d9 72 74 28 29 20 66 61 69 6c 73 2c 20 69 74 20 69 rt() fails, it i
1b4da 6e 64 69 63 61 74 65 73 20 61 0a 20 20 2a 2a 20 ndicates a. **
1b4db 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 61 6e 64 20 memory leak and
1b4dc 61 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 a need to call s
1b4dd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1b4de 65 61 73 65 28 26 6d 65 6d 31 29 2e 0a 20 20 2a ease(&mem1).. *
1b4df 2f 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31 /. assert( mem1
1b4e0 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 0a .zMalloc==0 );..
1b4e1 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20 /* rc==0 here
1b4e2 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f means that one o
1b4e3 66 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f f the keys ran o
1b4e4 75 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64 ut of fields and
1b4e5 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69 . ** all the fi
1b4e6 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 20 elds up to that
1b4e7 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61 6c point were equal
1b4e8 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 . If the UNPACKE
1b4e9 44 5f 49 4e 43 52 4b 45 59 0a 20 20 2a 2a 20 66 D_INCRKEY. ** f
1b4ea 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e lag is set, then
1b4eb 20 62 72 65 61 6b 20 74 68 65 20 74 69 65 20 62 break the tie b
1b4ec 79 20 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20 y treating key2
1b4ed 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 2a 2a 20 as larger.. **
1b4ee 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 If the UPACKED_P
1b4ef 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 REFIX_MATCH flag
1b4f0 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 is set, then ke
1b4f1 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 ys with common p
1b4f2 72 65 66 69 78 65 73 0a 20 20 2a 2a 20 61 72 65 refixes. ** are
1b4f3 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
1b4f4 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 77 e equal. Otherw
1b4f5 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 20 ise, the longer
1b4f6 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 2a 2a key is the . **
1b4f7 20 6c 61 72 67 65 72 2e 20 20 41 73 20 69 74 20 larger. As it
1b4f8 68 61 70 70 65 6e 73 2c 20 74 68 65 20 70 50 4b happens, the pPK
1b4f9 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 ey2 will always
1b4fa 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20 be the longer.
1b4fb 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ** if there is a
1b4fc 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 2a difference.. *
1b4fd 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d /. assert( rc==
1b4fe 30 20 29 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 0 );. if( pPKey
1b4ff 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 2->flags & UNPAC
1b500 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20 KED_INCRKEY ){.
1b501 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20 7d 65 rc = -1;. }e
1b502 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e lse if( pPKey2->
1b503 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 flags & UNPACKED
1b504 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b _PREFIX_MATCH ){
1b505 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 . /* Leave rc
1b506 3d 3d 30 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 ==0 */. }else i
1b507 66 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 f( idx1<szHdr1 )
1b508 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
1b509 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1b50a 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 . ../*.** pCur p
1b50b 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65 oints at an inde
1b50c 78 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 20 x entry created
1b50d 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b using the OP_Mak
1b50e 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a eRecord opcode..
1b50f 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69 ** Read the rowi
1b510 64 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c d (the last fiel
1b511 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29 d in the record)
1b512 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e and store it in
1b513 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75 *rowid..** Retu
1b514 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
1b515 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 everything works
1b516 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f , or an error co
1b517 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a de otherwise..**
1b518 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 74 20 62 .** pCur might b
1b519 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 65 e pointing to te
1b51a 78 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d xt obtained from
1b51b 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
1b51c 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f 20 ase file..** So
1b51d 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e 6e the content cann
1b51e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 20 ot be trusted.
1b51f 44 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 Do appropriate c
1b520 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 6e hecks on the con
1b521 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tent..*/.SQLITE_
1b522 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1b523 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 te3VdbeIdxRowid(
1b524 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 43 sqlite3 *db, BtC
1b525 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 ursor *pCur, i64
1b526 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69 36 34 20 *rowid){. i64
1b527 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 nCellKey = 0;.
1b528 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 73 7a int rc;. u32 sz
1b529 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 Hdr; /* S
1b52a 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 ize of the heade
1b52b 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 70 65 52 r */. u32 typeR
1b52c 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 65 72 69 owid; /* Seri
1b52d 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 al type of the r
1b52e 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c 65 owid */. u32 le
1b52f 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 53 nRowid; /* S
1b530 69 7a 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 ize of the rowid
1b531 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a */. Mem m, v;.
1b532 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1b533 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 47 TER(db);.. /* G
1b534 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 et the size of t
1b535 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 he index entry.
1b536 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e Only indices en
1b537 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 tries of less.
1b538 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61 72 65 ** than 2GiB are
1b539 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 support - anyth
1b53a 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62 ing large must b
1b53b 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 e database corru
1b53c 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79 20 ption.. ** Any
1b53d 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 corruption is de
1b53e 74 65 63 74 65 64 20 69 6e 20 73 71 6c 69 74 65 tected in sqlite
1b53f 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
1b540 74 72 28 29 2c 20 74 68 6f 75 67 68 2c 20 73 6f tr(), though, so
1b541 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 . ** this code
1b542 63 61 6e 20 73 61 66 65 6c 79 20 61 73 73 75 6d can safely assum
1b543 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79 20 e that nCellKey
1b544 69 73 20 33 32 2d 62 69 74 73 20 20 0a 20 20 2a is 32-bits . *
1b545 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
1b546 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 te3BtreeCursorIs
1b547 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a 20 Valid(pCur) );.
1b548 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1b549 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 eeKeySize(pCur,
1b54a 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73 &nCellKey);. as
1b54b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1b54c 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70 43 _OK ); /* pC
1b54d 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c ur is always val
1b54e 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61 id so KeySize ca
1b54f 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 61 nnot fail */. a
1b550 73 73 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65 79 ssert( (nCellKey
1b551 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 & SQLITE_MAX_U3
1b552 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b 65 2)==(u64)nCellKe
1b553 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 y );.. /* Read
1b554 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 in the complete
1b555 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 69 content of the i
1b556 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 ndex entry */.
1b557 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 memset(&m, 0, si
1b558 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d zeof(m));. rc =
1b559 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
1b55a 72 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 romBtree(pCur, 0
1b55b 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c , (int)nCellKey,
1b55c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 1, &m);. if( r
1b55d 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
1b55e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 rc;. }.. /* Th
1b55f 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 6d 75 e index entry mu
1b560 73 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 st begin with a
1b561 68 65 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a 20 header size */.
1b562 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 (void)getVarint
1b563 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48 32((u8*)m.z, szH
1b564 64 72 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 dr);. testcase(
1b565 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20 74 szHdr==3 );. t
1b566 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d estcase( szHdr==
1b567 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c m.n );. if( unl
1b568 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c 7c ikely(szHdr<3 ||
1b569 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e 29 (int)szHdr>m.n)
1b56a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 ){. goto idx
1b56b 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f _rowid_corruptio
1b56c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 n;. }.. /* The
1b56d 20 6c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 last field of t
1b56e 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 he index should
1b56f 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 be an integer -
1b570 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a 20 the ROWID.. **
1b571 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
1b572 6c 61 73 74 20 65 6e 74 72 79 20 72 65 61 6c 6c last entry reall
1b573 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2e y is an integer.
1b574 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 */. (void)getV
1b575 61 72 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e arint32((u8*)&m.
1b576 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 z[szHdr-1], type
1b577 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61 Rowid);. testca
1b578 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 31 se( typeRowid==1
1b579 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
1b57a 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b 0a typeRowid==2 );.
1b57b 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
1b57c 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74 65 Rowid==3 );. te
1b57d 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
1b57e 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 74 63 61 d==4 );. testca
1b57f 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 35 se( typeRowid==5
1b580 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
1b581 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b 0a typeRowid==6 );.
1b582 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
1b583 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74 65 Rowid==8 );. te
1b584 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
1b585 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 20 75 6e d==9 );. if( un
1b586 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f 77 69 64 likely(typeRowid
1b587 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3e <1 || typeRowid>
1b588 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d 3d 9 || typeRowid==
1b589 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 7) ){. goto i
1b58a 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 dx_rowid_corrupt
1b58b 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52 6f ion;. }. lenRo
1b58c 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 wid = sqlite3Vdb
1b58d 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 eSerialTypeLen(t
1b58e 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 ypeRowid);. tes
1b58f 74 63 61 73 65 28 20 28 75 33 32 29 6d 2e 6e 3d tcase( (u32)m.n=
1b590 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 20 =szHdr+lenRowid
1b591 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c );. if( unlikel
1b592 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64 72 y((u32)m.n<szHdr
1b593 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20 20 +lenRowid) ){.
1b594 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 goto idx_rowid
1b595 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d _corruption;. }
1b596 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 .. /* Fetch the
1b597 20 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 integer off the
1b598 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 end of the inde
1b599 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 x record */. sq
1b59a 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
1b59b 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e et((u8*)&m.z[m.n
1b59c 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 -lenRowid], type
1b59d 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 Rowid, &v);. *r
1b59e 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 owid = v.u.i;.
1b59f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1b5a0 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 lease(&m);. ret
1b5a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a urn SQLITE_OK;..
1b5a2 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 /* Jump here i
1b5a3 66 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 f database corru
1b5a4 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 ption is detecte
1b5a5 64 20 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 d after m has be
1b5a6 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 en. ** allocate
1b5a7 64 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f d. Free the m o
1b5a8 62 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e bject and return
1b5a9 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e SQLITE_CORRUPT.
1b5aa 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f */.idx_rowid_co
1b5ab 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 rruption:. test
1b5ac 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 case( m.zMalloc!
1b5ad 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 =0 );. sqlite3V
1b5ae 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d dbeMemRelease(&m
1b5af 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
1b5b0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1b5b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 .}../*.** Compar
1b5b2 65 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 e the key of the
1b5b3 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 index entry tha
1b5b4 74 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 t cursor pC is p
1b5b5 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 6e ointing to again
1b5b6 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74 st.** the key st
1b5b7 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b 65 ring in pUnpacke
1b5b8 64 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a d. Write into *
1b5b9 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a 2a pRes a number.**
1b5ba 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69 76 that is negativ
1b5bb 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 e, zero, or posi
1b5bc 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c 65 tive if pC is le
1b5bd 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 ss than, equal t
1b5be 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 o,.** or greater
1b5bf 20 74 68 61 6e 20 70 55 6e 70 61 63 6b 65 64 2e than pUnpacked.
1b5c0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
1b5c1 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a OK on success..*
1b5c2 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20 69 *.** pUnpacked i
1b5c3 73 20 65 69 74 68 65 72 20 63 72 65 61 74 65 64 s either created
1b5c4 20 77 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 without a rowid
1b5c5 20 6f 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 or is truncated
1b5c6 20 73 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f so that it.** o
1b5c7 6d 69 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 mits the rowid a
1b5c8 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 t the end. The
1b5c9 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 rowid at the end
1b5ca 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1b5cb 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 try.** is ignore
1b5cc 64 20 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 d as well. Henc
1b5cd 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 e, this routine
1b5ce 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 only compares th
1b5cf 65 20 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f e prefixes .** o
1b5d0 66 20 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 f the keys prior
1b5d1 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f to the final ro
1b5d2 77 69 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 wid, not the ent
1b5d3 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 ire key..*/.SQLI
1b5d4 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1b5d5 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
1b5d6 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43 Compare(. VdbeC
1b5d7 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20 ursor *pC,
1b5d8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 /* The cu
1b5d9 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20 rsor to compare
1b5da 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70 against */. Unp
1b5db 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e ackedRecord *pUn
1b5dc 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61 packed, /* Unpa
1b5dd 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 cked version of
1b5de 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 key to compare a
1b5df 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 gainst */. int
1b5e0 2a 72 65 73 20 20 20 20 20 20 20 20 20 20 20 20 *res
1b5e1 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
1b5e2 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1b5e3 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 result here */.)
1b5e4 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 {. i64 nCellKey
1b5e5 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a = 0;. int rc;.
1b5e6 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
1b5e7 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a = pC->pCursor;.
1b5e8 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73 65 Mem m;.. asse
1b5e9 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1b5ea 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43 CursorIsValid(pC
1b5eb 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ur) );. rc = sq
1b5ec 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a lite3BtreeKeySiz
1b5ed 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 e(pCur, &nCellKe
1b5ee 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 y);. assert( rc
1b5ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 ==SQLITE_OK );
1b5f0 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 /* pCur is alw
1b5f1 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 ays valid so Key
1b5f2 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c Size cannot fail
1b5f3 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65 */. /* nCellKe
1b5f4 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 y will always be
1b5f5 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30 between 0 and 0
1b5f6 78 66 66 66 66 66 66 66 66 20 62 65 63 61 75 73 xffffffff becaus
1b5f7 65 20 6f 66 20 74 68 65 20 73 61 79 0a 20 20 2a e of the say. *
1b5f8 2a 20 74 68 61 74 20 62 74 72 65 65 50 61 72 73 * that btreePars
1b5f9 65 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 73 eCellPtr() and s
1b5fa 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 qlite3GetVarint3
1b5fb 32 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 2() are implemen
1b5fc 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 65 ted */. if( nCe
1b5fd 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c llKey<=0 || nCel
1b5fe 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 lKey>0x7fffffff
1b5ff 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 3b ){. *res = 0;
1b600 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1b601 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d 0a TE_CORRUPT;. }.
1b602 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 memset(&m, 0,
1b603 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 sizeof(m));. rc
1b604 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
1b605 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 mFromBtree(pC->p
1b606 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 Cursor, 0, (int)
1b607 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 nCellKey, 1, &m)
1b608 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1b609 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1b60a 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e 70 61 . assert( pUnpa
1b60b 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e cked->flags & UN
1b60c 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
1b60d 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 WID );. *res =
1b60e 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
1b60f 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e dCompare(m.n, m.
1b610 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 z, pUnpacked);.
1b611 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b612 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
1b613 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1b614 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
1b615 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 76 utine sets the v
1b616 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 75 72 alue to be retur
1b617 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 6e ned by subsequen
1b618 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 t calls to.** sq
1b619 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
1b61a 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1b61b 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f handle 'db'. .*/
1b61c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b61d 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1b61e 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 SetChanges(sqlit
1b61f 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 e3 *db, int nCha
1b620 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 nge){. assert(
1b621 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b622 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
1b623 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d . db->nChange =
1b624 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e nChange;. db->
1b625 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 nTotalChange +=
1b626 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a nChange;.}../*.*
1b627 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20 * Set a flag in
1b628 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64 61 the vdbe to upda
1b629 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f te the change co
1b62a 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 unter when it is
1b62b 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 finalised.** or
1b62c 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 reset..*/.SQLIT
1b62d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1b62e 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 qlite3VdbeCountC
1b62f 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b hanges(Vdbe *v){
1b630 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f . v->changeCntO
1b631 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n = 1;.}../*.**
1b632 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70 61 Mark every prepa
1b633 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73 red statement as
1b634 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
1b635 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1b636 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 ion.** as expire
1b637 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 d..**.** An expi
1b638 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 red statement me
1b639 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69 ans that recompi
1b63a 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 lation of the st
1b63b 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 atement is.** re
1b63c 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d commend. Statem
1b63d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65 6e ents expire when
1b63e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74 things happen t
1b63f 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a hat make their.*
1b640 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c * programs obsol
1b641 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 ete. Removing u
1b642 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ser-defined func
1b643 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 tions or collati
1b644 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c ng.** sequences,
1b645 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 or changing an
1b646 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 authorization fu
1b647 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 nction are the t
1b648 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 ypes of.** thing
1b649 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70 s that make prep
1b64a 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 ared statements
1b64b 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c obsolete..*/.SQL
1b64c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1b64d 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 sqlite3ExpirePr
1b64e 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 eparedStatements
1b64f 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
1b650 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 Vdbe *p;. for(
1b651 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 p = db->pVdbe; p
1b652 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 ; p=p->pNext){.
1b653 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 p->expired =
1b654 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 1;. }.}../*.**
1b655 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 Return the datab
1b656 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ase associated w
1b657 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f ith the Vdbe..*/
1b658 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b659 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 sqlite3 *sqlite3
1b65a 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b VdbeDb(Vdbe *v){
1b65b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b . return v->db;
1b65c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1b65d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
1b65e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73 sqlite3_value s
1b65f 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1b660 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 62 6f ing the value bo
1b661 75 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 und.** parameter
1b662 20 69 56 61 72 20 6f 66 20 56 4d 20 76 2e 20 45 iVar of VM v. E
1b663 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 76 61 xcept, if the va
1b664 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 lue is an SQL NU
1b665 4c 4c 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 30 LL, return .** 0
1b666 20 69 6e 73 74 65 61 64 2e 20 55 6e 6c 65 73 73 instead. Unless
1b667 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 61 70 70 it is NULL, app
1b668 6c 79 20 61 66 66 69 6e 69 74 79 20 61 66 66 20 ly affinity aff
1b669 28 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 (one of the SQLI
1b66a 54 45 5f 41 46 46 5f 2a 0a 2a 2a 20 63 6f 6e 73 TE_AFF_*.** cons
1b66b 74 61 6e 74 73 29 20 74 6f 20 74 68 65 20 76 61 tants) to the va
1b66c 6c 75 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 lue before retur
1b66d 6e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 ning it..**.** T
1b66e 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 he returned valu
1b66f 65 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 e must be freed
1b670 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 75 73 by the caller us
1b671 69 6e 67 20 73 71 6c 69 74 65 33 56 61 6c 75 65 ing sqlite3Value
1b672 46 72 65 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 Free()..*/.SQLIT
1b673 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
1b674 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
1b675 56 64 62 65 47 65 74 56 61 6c 75 65 28 56 64 62 VdbeGetValue(Vdb
1b676 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 72 2c 20 e *v, int iVar,
1b677 75 38 20 61 66 66 29 7b 0a 20 20 61 73 73 65 72 u8 aff){. asser
1b678 74 28 20 69 56 61 72 3e 30 20 29 3b 0a 20 20 69 t( iVar>0 );. i
1b679 66 28 20 76 20 29 7b 0a 20 20 20 20 4d 65 6d 20 f( v ){. Mem
1b67a 2a 70 4d 65 6d 20 3d 20 26 76 2d 3e 61 56 61 72 *pMem = &v->aVar
1b67b 5b 69 56 61 72 2d 31 5d 3b 0a 20 20 20 20 69 66 [iVar-1];. if
1b67c 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 ( 0==(pMem->flag
1b67d 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 20 29 7b s & MEM_Null) ){
1b67e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 . sqlite3_v
1b67f 61 6c 75 65 20 2a 70 52 65 74 20 3d 20 73 71 6c alue *pRet = sql
1b680 69 74 65 33 56 61 6c 75 65 4e 65 77 28 76 2d 3e ite3ValueNew(v->
1b681 64 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 db);. if( p
1b682 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Ret ){. s
1b683 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 qlite3VdbeMemCop
1b684 79 28 28 4d 65 6d 20 2a 29 70 52 65 74 2c 20 70 y((Mem *)pRet, p
1b685 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 Mem);. sq
1b686 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 lite3ValueApplyA
1b687 66 66 69 6e 69 74 79 28 70 52 65 74 2c 20 61 66 ffinity(pRet, af
1b688 66 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b f, SQLITE_UTF8);
1b689 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1b68a 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 VdbeMemStoreType
1b68b 28 28 4d 65 6d 20 2a 29 70 52 65 74 29 3b 0a 20 ((Mem *)pRet);.
1b68c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
1b68d 75 72 6e 20 70 52 65 74 3b 0a 20 20 20 20 7d 0a urn pRet;. }.
1b68e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1b68f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 }../*.** Configu
1b690 72 65 20 53 51 4c 20 76 61 72 69 61 62 6c 65 20 re SQL variable
1b691 69 56 61 72 20 73 6f 20 74 68 61 74 20 62 69 6e iVar so that bin
1b692 64 69 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65 ding a new value
1b693 20 74 6f 20 69 74 20 73 69 67 6e 61 6c 73 0a 2a to it signals.*
1b694 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 6f * to sqlite3_reo
1b695 70 74 69 6d 69 7a 65 28 29 20 74 68 61 74 20 72 ptimize() that r
1b696 65 2d 70 72 65 70 61 72 69 6e 67 20 74 68 65 20 e-preparing the
1b697 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 72 65 statement may re
1b698 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 20 62 65 74 sult.** in a bet
1b699 74 65 72 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a ter query plan..
1b69a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b69b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1b69c 62 65 53 65 74 56 61 72 6d 61 73 6b 28 56 64 62 beSetVarmask(Vdb
1b69d 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 72 29 7b e *v, int iVar){
1b69e 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 72 3e . assert( iVar>
1b69f 30 20 29 3b 0a 20 20 69 66 28 20 69 56 61 72 3e 0 );. if( iVar>
1b6a0 33 32 20 29 7b 0a 20 20 20 20 76 2d 3e 65 78 70 32 ){. v->exp
1b6a1 6d 61 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66 mask = 0xfffffff
1b6a2 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 f;. }else{.
1b6a3 76 2d 3e 65 78 70 6d 61 73 6b 20 7c 3d 20 28 28 v->expmask |= ((
1b6a4 75 33 32 29 31 20 3c 3c 20 28 69 56 61 72 2d 31 u32)1 << (iVar-1
1b6a5 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a ));. }.}../****
1b6a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1b6a7 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a f vdbeaux.c ****
1b6a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1b6ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1b6ac 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20 file vdbeapi.c
1b6ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1b6b0 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2004 May 26.**.
1b6b1 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1b6b2 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1b6b3 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1b6b4 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1b6b5 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1b6b6 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1b6b7 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1b6b8 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1b6b9 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1b6ba 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1b6bb 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1b6bc 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1b6bd 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1b6be 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1b6bf 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1b6c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1b6c1 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1b6c2 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1b6c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
1b6c7 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1b6c8 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f ains code use to
1b6c9 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20 implement APIs
1b6ca 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 that are part of
1b6cb 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2f the.** VDBE..*/
1b6cc 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1b6cd 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
1b6ce 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
1b6cf 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 UE (non-zero) of
1b6d0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 the statement s
1b6d1 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
1b6d2 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 gument needs.**
1b6d3 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 to be recompiled
1b6d4 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e . A statement n
1b6d5 65 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d eeds to be recom
1b6d6 70 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74 piled whenever t
1b6d7 68 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 he.** execution
1b6d8 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e environment chan
1b6d9 67 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61 ges in a way tha
1b6da 74 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 t would alter th
1b6db 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 e program.** tha
1b6dc 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
1b6dd 65 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20 e() generates.
1b6de 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 For example, if
1b6df 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 new functions or
1b6e0 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
1b6e1 71 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69 quences are regi
1b6e2 73 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20 stered or if an
1b6e3 61 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 authorizer funct
1b6e4 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 ion is.** added
1b6e5 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 or changed..*/.S
1b6e6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b6e7 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 lite3_expired(sq
1b6e8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1b6e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 t){. Vdbe *p =
1b6ea 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1b6eb 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
1b6ec 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e ->expired;.}.#en
1b6ed 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
1b6ee 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
1b6ef 20 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74 destroys a virt
1b6f0 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 ual machine that
1b6f1 20 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a is created by.*
1b6f2 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f * the sqlite3_co
1b6f3 6d 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e mpile() routine.
1b6f4 20 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74 The integer ret
1b6f5 75 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49 urned is an SQLI
1b6f6 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 TE_.** success/f
1b6f7 61 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74 ailure code that
1b6f8 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72 describes the r
1b6f9 65 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69 esult of executi
1b6fa 6e 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a ng the virtual.*
1b6fb 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a * machine..**.**
1b6fc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
1b6fd 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ts the error cod
1b6fe 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 e and string ret
1b6ff 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 urned by.** sqli
1b700 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 te3_errcode(), s
1b701 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1b702 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d and sqlite3_errm
1b703 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 sg16()..*/.SQLIT
1b704 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b705 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 3_finalize(sqlit
1b706 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b707 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 . int rc;. if(
1b708 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 pStmt==0 ){.
1b709 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1b70a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 . }else{. Vd
1b70b 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 be *v = (Vdbe*)p
1b70c 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 Stmt;. sqlite
1b70d 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23 3 *db = v->db;.#
1b70e 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
1b70f 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 SAFE. sqlite3
1b710 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
1b711 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 v->db->mutex;.#e
1b712 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 ndif. sqlite3
1b713 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
1b714 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 ex);. rc = sq
1b715 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a lite3VdbeFinaliz
1b716 65 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 e(v);. rc = s
1b717 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 qlite3ApiExit(db
1b718 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 , rc);. sqlit
1b719 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
1b71a 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1b71b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1b71c 20 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 Terminate the c
1b71d 75 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e urrent execution
1b71e 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 of an SQL state
1b71f 6d 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 ment and reset i
1b720 74 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 t.** back to its
1b721 20 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 starting state
1b722 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 so that it can b
1b723 65 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 e reused. A succ
1b724 65 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a ess code from.**
1b725 20 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 the prior execu
1b726 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 tion is returned
1b727 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1b728 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 tine sets the er
1b729 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 ror code and str
1b72a 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a ing returned by.
1b72b 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f ** sqlite3_errco
1b72c 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 de(), sqlite3_er
1b72d 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 rmsg() and sqlit
1b72e 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a e3_errmsg16()..*
1b72f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1b730 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 sqlite3_reset(s
1b731 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b732 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 mt){. int rc;.
1b733 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b if( pStmt==0 ){
1b734 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1b735 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
1b736 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 Vdbe *v = (Vdb
1b737 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 e*)pStmt;. sq
1b738 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b739 72 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b r(v->db->mutex);
1b73a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b73b 33 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20 3VdbeReset(v);.
1b73c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 sqlite3VdbeMa
1b73d 6b 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 keReady(v, -1, 0
1b73e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 0, 0, 0, 0);.
1b73f 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 assert( (rc &
1b740 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b (v->db->errMask
1b741 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 63 ))==rc );. rc
1b742 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 = sqlite3ApiExi
1b743 74 28 76 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 t(v->db, rc);.
1b744 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b745 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 leave(v->db->mut
1b746 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1b747 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1b748 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d et all the param
1b749 65 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d eters in the com
1b74a 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d piled SQL statem
1b74b 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a ent to NULL..*/.
1b74c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b74d 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e qlite3_clear_bin
1b74e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
1b74f 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e mt *pStmt){. in
1b750 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t i;. int rc =
1b751 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 SQLITE_OK;. Vdb
1b752 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 e *p = (Vdbe*)pS
1b753 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f tmt;.#if SQLITE_
1b754 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c THREADSAFE. sql
1b755 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
1b756 78 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d x = ((Vdbe*)pStm
1b757 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 t)->db->mutex;.#
1b758 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
1b759 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
1b75a 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 x);. for(i=0; i
1b75b 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a <p->nVar; i++){.
1b75c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b75d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 emRelease(&p->aV
1b75e 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 ar[i]);. p->a
1b75f 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d Var[i].flags = M
1b760 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 69 EM_Null;. }. i
1b761 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 f( p->isPrepareV
1b762 32 20 26 26 20 70 2d 3e 65 78 70 6d 61 73 6b 20 2 && p->expmask
1b763 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 ){. p->expire
1b764 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c d = 1;. }. sql
1b765 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1b766 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 (mutex);. retur
1b767 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a n rc;.}.../*****
1b768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b769 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f ******* sqlite3_
1b76a 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a value_ ********
1b76b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b76c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 *******.** The f
1b76d 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
1b76e 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d s extract inform
1b76f 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d ation from a Mem
1b770 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 or sqlite3_valu
1b771 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a e.** structure..
1b772 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1b773 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1b774 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 3_value_blob(sql
1b775 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1b776 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d ){. Mem *p = (M
1b777 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 em*)pVal;. if(
1b778 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
1b779 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b Blob|MEM_Str) ){
1b77a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1b77b 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29 MemExpandBlob(p)
1b77c 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 ;. p->flags &
1b77d 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 = ~MEM_Str;.
1b77e 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f p->flags |= MEM_
1b77f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e Blob;. return
1b780 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a p->z;. }else{.
1b781 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
1b782 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 e3_value_text(pV
1b783 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 al);. }.}.SQLIT
1b784 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b785 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 3_value_bytes(sq
1b786 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b787 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1b788 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 ite3ValueBytes(p
1b789 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Val, SQLITE_UTF8
1b78a 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b78b 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
1b78c 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 e_bytes16(sqlite
1b78d 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1b78e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1b78f 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c ValueBytes(pVal,
1b790 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
1b791 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 IVE);.}.SQLITE_A
1b792 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 PI double sqlite
1b793 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 3_value_double(s
1b794 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b795 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b796 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
1b797 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a ue((Mem*)pVal);.
1b798 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
1b799 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
1b79a 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt(sqlite3_value
1b79b 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1b79c 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 n (int)sqlite3Vd
1b79d 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a beIntValue((Mem*
1b79e 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 )pVal);.}.SQLITE
1b79f 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 _API sqlite_int6
1b7a0 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 4 sqlite3_value_
1b7a1 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 int64(sqlite3_va
1b7a2 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 lue *pVal){. re
1b7a3 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 turn sqlite3Vdbe
1b7a4 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 IntValue((Mem*)p
1b7a5 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Val);.}.SQLITE_A
1b7a6 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
1b7a7 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
1b7a8 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 value_text(sqlit
1b7a9 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
1b7aa 0a 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 . return (const
1b7ab 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1b7ac 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 )sqlite3ValueTex
1b7ad 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 t(pVal, SQLITE_U
1b7ae 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 TF8);.}.#ifndef
1b7af 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1b7b0 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1b7b1 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1b7b2 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 _value_text16(sq
1b7b3 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 lite3_value* pVa
1b7b4 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1b7b5 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 ite3ValueText(pV
1b7b6 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 al, SQLITE_UTF16
1b7b7 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 NATIVE);.}.SQLIT
1b7b8 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1b7b9 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
1b7ba 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 text16be(sqlite3
1b7bb 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1b7bc 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1b7bd 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 alueText(pVal, S
1b7be 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a QLITE_UTF16BE);.
1b7bf 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
1b7c0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1b7c1 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 _value_text16le(
1b7c2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b7c3 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
1b7c4 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
1b7c5 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
1b7c6 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 16LE);.}.#endif
1b7c7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 /* SQLITE_OMIT_U
1b7c8 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 TF16 */.SQLITE_A
1b7c9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 PI int sqlite3_v
1b7ca 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 alue_type(sqlite
1b7cb 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 3_value* pVal){.
1b7cc 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 return pVal->t
1b7cd 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a ype;.}../*******
1b7ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b7cf 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 ***** sqlite3_re
1b7d0 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a sult_ *********
1b7d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b7d2 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ******.** The fo
1b7d3 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 llowing routines
1b7d4 20 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65 are used by use
1b7d5 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 r-defined functi
1b7d6 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a ons to specify.*
1b7d7 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 * the function r
1b7d8 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 esult..**.** The
1b7d9 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 29 setStrOrError()
1b7da 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 73 funtion calls s
1b7db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b7dc 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 74 Str() to store t
1b7dd 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73 20 he.** result as
1b7de 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 a string or blob
1b7df 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72 69 but if the stri
1b7e0 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74 6f ng or blob is to
1b7e1 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20 74 o large, it.** t
1b7e2 68 65 6e 20 73 65 74 73 20 74 68 65 20 65 72 72 hen sets the err
1b7e3 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 or code to SQLIT
1b7e4 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61 74 E_TOOBIG.*/.stat
1b7e5 69 63 20 76 6f 69 64 20 73 65 74 52 65 73 75 6c ic void setResul
1b7e6 74 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20 73 tStrOrError(. s
1b7e7 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b7e8 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74 69 pCtx, /* Functi
1b7e9 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 on context */.
1b7ea 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 const char *z,
1b7eb 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e /* Strin
1b7ec 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 g pointer */. i
1b7ed 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 nt n,
1b7ee 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
1b7ef 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 in string, or ne
1b7f0 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 gative */. u8 e
1b7f1 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nc,
1b7f2 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 /* Encoding
1b7f3 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c 4f of z. 0 for BLO
1b7f4 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 Bs */. void (*x
1b7f5 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 Del)(void*)
1b7f6 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 /* Destructor fu
1b7f7 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 nction */.){. i
1b7f8 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
1b7f9 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 mSetStr(&pCtx->s
1b7fa 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 , z, n, enc, xDe
1b7fb 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 l)==SQLITE_TOOBI
1b7fc 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 G ){. sqlite3
1b7fd 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
1b7fe 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d 0a obig(pCtx);. }.
1b7ff 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1b800 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b801 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 _blob(. sqlite3
1b802 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b803 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1b804 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 , . int n, . v
1b805 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1b806 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 *).){. assert(
1b807 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 n>=0 );. asser
1b808 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b809 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b80a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 ->mutex) );. se
1b80b 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f tResultStrOrErro
1b80c 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30 2c r(pCtx, z, n, 0,
1b80d 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 xDel);.}.SQLITE
1b80e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b80f 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
1b810 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b811 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72 56 *pCtx, double rV
1b812 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 al){. assert( s
1b813 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b814 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b815 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1b816 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 3VdbeMemSetDoubl
1b817 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61 6c e(&pCtx->s, rVal
1b818 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b819 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b81a 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 ult_error(sqlite
1b81b 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b81c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
1b81d 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 int n){. assert
1b81e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b81f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b820 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 >mutex) );. pCt
1b821 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
1b822 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c ITE_ERROR;. sql
1b823 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1b824 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e r(&pCtx->s, z, n
1b825 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
1b826 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
1b827 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1b828 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1b829 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b82a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
1b82b 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e or16(sqlite3_con
1b82c 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 text *pCtx, cons
1b82d 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e t void *z, int n
1b82e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b82f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b830 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b831 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 x) );. pCtx->is
1b832 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 Error = SQLITE_E
1b833 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 RROR;. sqlite3V
1b834 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 dbeMemSetStr(&pC
1b835 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c tx->s, z, n, SQL
1b836 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c ITE_UTF16NATIVE,
1b837 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
1b838 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 4c T);.}.#endif.SQL
1b839 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b83a 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
1b83b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b83c 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c 29 *pCtx, int iVal)
1b83d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b83e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b83f 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b840 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1b841 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 beMemSetInt64(&p
1b842 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 61 Ctx->s, (i64)iVa
1b843 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
1b844 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b845 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 sult_int64(sqlit
1b846 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b847 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 61 , i64 iVal){. a
1b848 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b849 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1b84a 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1b84b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b84c 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e SetInt64(&pCtx->
1b84d 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 s, iVal);.}.SQLI
1b84e 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b84f 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 te3_result_null(
1b850 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b851 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 *pCtx){. assert
1b852 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b853 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b854 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
1b855 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1b856 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a ll(&pCtx->s);.}.
1b857 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b858 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
1b859 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 ext(. sqlite3_c
1b85a 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 ontext *pCtx, .
1b85b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
1b85c 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 . int n,. void
1b85d 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 (*xDel)(void *)
1b85e 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 .){. assert( sq
1b85f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b860 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b861 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 ex) );. setResu
1b862 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 ltStrOrError(pCt
1b863 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f x, z, n, SQLITE_
1b864 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 UTF8, xDel);.}.#
1b865 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b866 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1b867 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b868 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a _result_text16(.
1b869 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1b86a 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
1b86b 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e t void *z, . in
1b86c 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 t n, . void (*x
1b86d 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
1b86e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b86f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b870 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b871 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 );. setResultSt
1b872 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a rOrError(pCtx, z
1b873 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 , n, SQLITE_UTF1
1b874 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a 6NATIVE, xDel);.
1b875 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1b876 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b877 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c _text16be(. sql
1b878 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b879 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 tx, . const voi
1b87a 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
1b87b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1b87c 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
1b87d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b87e 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b87f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b880 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 setResultStrOrEr
1b881 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 ror(pCtx, z, n,
1b882 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 SQLITE_UTF16BE,
1b883 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f xDel);.}.SQLITE_
1b884 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b885 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
1b886 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1b887 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f ext *pCtx, . co
1b888 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 nst void *z, .
1b889 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 int n, . void (
1b88a 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 *xDel)(void *).)
1b88b 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b88c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b88d 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b88e 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 ) );. setResult
1b88f 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c StrOrError(pCtx,
1b890 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
1b891 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a F16LE, xDel);.}.
1b892 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1b893 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
1b894 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b895 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 qlite3_result_va
1b896 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 lue(sqlite3_cont
1b897 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 ext *pCtx, sqlit
1b898 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 e3_value *pValue
1b899 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b89a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b89b 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b89c 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
1b89d 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 dbeMemCopy(&pCtx
1b89e 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a ->s, pValue);.}.
1b89f 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b8a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a sqlite3_result_z
1b8a1 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f eroblob(sqlite3_
1b8a2 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 context *pCtx, i
1b8a3 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt n){. assert(
1b8a4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b8a5 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b8a6 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1b8a7 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 te3VdbeMemSetZer
1b8a8 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 oBlob(&pCtx->s,
1b8a9 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 n);.}.SQLITE_API
1b8aa 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b8ab 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 sult_error_code(
1b8ac 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b8ad 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f *pCtx, int errCo
1b8ae 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 de){. pCtx->isE
1b8af 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a rror = errCode;.
1b8b0 20 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66 6c if( pCtx->s.fl
1b8b1 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1b8b2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1b8b3 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
1b8b4 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72 53 ->s, sqlite3ErrS
1b8b5 74 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31 2c tr(errCode), -1,
1b8b6 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b8b7 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
1b8b8 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 E_UTF8, SQLITE_S
1b8b9 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f TATIC);. }.}../
1b8ba 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 54 * Force an SQLIT
1b8bb 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e 20 E_TOOBIG error.
1b8bc 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
1b8bd 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b8be 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 t_error_toobig(s
1b8bf 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b8c0 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 pCtx){. assert(
1b8c1 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b8c2 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b8c3 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 mutex) );. pCtx
1b8c4 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 ->isError = SQLI
1b8c5 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 6c TE_TOOBIG;. sql
1b8c6 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1b8c7 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72 r(&pCtx->s, "str
1b8c8 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 ing or blob too
1b8c9 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 20 big", -1, .
1b8ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b8cb 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 SQLITE_UTF8, S
1b8cc 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d QLITE_STATIC);.}
1b8cd 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4e ../* An SQLITE_N
1b8ce 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 OMEM error. */.S
1b8cf 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b8d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1b8d1 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 ror_nomem(sqlite
1b8d2 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 3_context *pCtx)
1b8d3 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b8d4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b8d5 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b8d6 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1b8d7 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 beMemSetNull(&pC
1b8d8 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d 3e tx->s);. pCtx->
1b8d9 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 isError = SQLITE
1b8da 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d 3e _NOMEM;. pCtx->
1b8db 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c s.db->mallocFail
1b8dc 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 1;.}../*.**
1b8dd 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61 Execute the sta
1b8de 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69 tement pStmt, ei
1b8df 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77 ther until a row
1b8e0 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 of data is read
1b8e1 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d y, the.** statem
1b8e2 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c ent is completel
1b8e3 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e y executed or an
1b8e4 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a error occurs..*
1b8e5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1b8e6 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
1b8e7 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67 bulk of the log
1b8e8 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71 ic behind the sq
1b8e9 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41 lite_step().** A
1b8ea 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68 PI. The only th
1b8eb 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74 ing omitted is t
1b8ec 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63 he automatic rec
1b8ed 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20 ompile if a .**
1b8ee 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61 schema change ha
1b8ef 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61 s occurred. Tha
1b8f0 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64 t detail is hand
1b8f1 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75 led by the.** ou
1b8f2 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ter sqlite3_step
1b8f3 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65 () wrapper proce
1b8f4 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dure..*/.static
1b8f5 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28 int sqlite3Step(
1b8f6 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 Vdbe *p){. sqli
1b8f7 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 te3 *db;. int r
1b8f8 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b c;.. assert(p);
1b8f9 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 . if( p->magic!
1b8fa 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
1b8fb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1b8fc 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
1b8fd 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
1b8fe 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 at malloc() has
1b8ff 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 not failed */.
1b900 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 db = p->db;. if
1b901 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1b902 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
1b903 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1b904 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c }.. if( p->pc<
1b905 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 =0 && p->expired
1b906 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 ){. if( ALWA
1b907 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 YS(p->rc==SQLITE
1b908 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 _OK || p->rc==SQ
1b909 4c 49 54 45 5f 53 43 48 45 4d 41 29 20 29 7b 0a LITE_SCHEMA) ){.
1b90a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 p->rc = SQ
1b90b 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 LITE_SCHEMA;.
1b90c 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 }. rc = SQLI
1b90d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f TE_ERROR;. go
1b90e 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a to end_of_step;.
1b90f 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1b910 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 3SafetyOn(db) ){
1b911 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c . p->rc = SQL
1b912 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 ITE_MISUSE;.
1b913 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
1b914 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 SUSE;. }. if(
1b915 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20 2f p->pc<0 ){. /
1b916 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e * If there are n
1b917 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e o other statemen
1b918 74 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e ts currently run
1b919 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20 2a ning, then. *
1b91a 2a 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 * reset the inte
1b91b 72 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68 69 rrupt flag. Thi
1b91c 73 20 70 72 65 76 65 6e 74 73 20 61 20 63 61 6c s prevents a cal
1b91d 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 l to sqlite3_int
1b91e 65 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66 72 errupt. ** fr
1b91f 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67 20 om interrupting
1b920 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 a statement that
1b921 20 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74 61 has not yet sta
1b922 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 rted.. */.
1b923 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 if( db->activeV
1b924 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 dbeCnt==0 ){.
1b925 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 db->u1.isInte
1b926 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 20 rrupted = 0;.
1b927 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 }.. assert(
1b928 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1b929 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f >0 || db->autoCo
1b92a 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6e mmit==0 || db->n
1b92b 44 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 DeferredCons==0
1b92c 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
1b92d 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 TE_OMIT_TRACE.
1b92e 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 69 if( db->xProfi
1b92f 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e le && !db->init.
1b930 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 6f busy ){. do
1b931 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 20 uble rNow;.
1b932 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
1b933 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 tTime(db->pVfs,
1b934 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 2d &rNow);. p-
1b935 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 28 75 36 >startTime = (u6
1b936 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 4)((rNow - (int)
1b937 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e rNow)*3600.0*24.
1b938 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0*1000000000.0);
1b939 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1b93a 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 db->activeVdb
1b93b 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 eCnt++;. if(
1b93c 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 p->readOnly==0 )
1b93d 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
1b93e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d t++;. p->pc =
1b93f 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 0;. }.#ifndef
1b940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1b941 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 AIN. if( p->exp
1b942 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d lain ){. rc =
1b943 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 sqlite3VdbeList
1b944 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e (p);. }else.#en
1b945 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1b946 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 IT_EXPLAIN */.
1b947 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1b948 65 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20 e3VdbeExec(p);.
1b949 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }.. if( sqlite
1b94a 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1b94b 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1b94c 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23 E_MISUSE;. }..#
1b94d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b94e 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e IT_TRACE. /* In
1b94f 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65 voke the profile
1b950 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 callback if the
1b951 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20 re is one. */.
1b952 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1b953 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66 ROW && db->xProf
1b954 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 ile && !db->init
1b955 2e 62 75 73 79 20 26 26 20 70 2d 3e 7a 53 71 6c .busy && p->zSql
1b956 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 ){. double r
1b957 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61 Now;. u64 ela
1b958 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71 pseTime;.. sq
1b959 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
1b95a 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e me(db->pVfs, &rN
1b95b 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 ow);. elapseT
1b95c 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f ime = (u64)((rNo
1b95d 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 w - (int)rNow)*3
1b95e 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 600.0*24.0*10000
1b95f 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65 6c 00000.0);. el
1b960 61 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e 73 apseTime -= p->s
1b961 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64 62 tartTime;. db
1b962 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70 ->xProfile(db->p
1b963 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 7a ProfileArg, p->z
1b964 53 71 6c 2c 20 65 6c 61 70 73 65 54 69 6d 65 29 Sql, elapseTime)
1b965 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
1b966 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 db->errCode = rc
1b967 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e ;. if( SQLITE_N
1b968 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 41 70 69 OMEM==sqlite3Api
1b969 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 Exit(p->db, p->r
1b96a 63 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 c) ){. p->rc
1b96b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1b96c 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a }.end_of_step:
1b96d 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
1b96e 69 6e 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 int local variab
1b96f 6c 65 20 72 63 20 68 6f 6c 64 73 20 74 68 65 20 le rc holds the
1b970 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c value that shoul
1b971 64 20 62 65 20 0a 20 20 2a 2a 20 72 65 74 75 72 d be . ** retur
1b972 6e 65 64 20 69 66 20 74 68 69 73 20 73 74 61 74 ned if this stat
1b973 65 6d 65 6e 74 20 77 61 73 20 63 6f 6d 70 69 6c ement was compil
1b974 65 64 20 75 73 69 6e 67 20 74 68 65 20 6c 65 67 ed using the leg
1b975 61 63 79 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 acy . ** sqlite
1b976 33 5f 70 72 65 70 61 72 65 28 29 20 69 6e 74 65 3_prepare() inte
1b977 72 66 61 63 65 2e 20 41 63 63 6f 72 64 69 6e 67 rface. According
1b978 20 74 6f 20 74 68 65 20 64 6f 63 73 2c 20 74 68 to the docs, th
1b979 69 73 20 63 61 6e 20 6f 6e 6c 79 0a 20 20 2a 2a is can only. **
1b97a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 76 be one of the v
1b97b 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 69 72 alues in the fir
1b97c 73 74 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f st assert() belo
1b97d 77 2e 20 56 61 72 69 61 62 6c 65 20 70 2d 3e 72 w. Variable p->r
1b97e 63 20 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 c . ** contains
1b97f 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 the value that
1b980 77 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 would be returne
1b981 64 20 69 66 20 73 71 6c 69 74 65 33 5f 66 69 6e d if sqlite3_fin
1b982 61 6c 69 7a 65 28 29 20 0a 20 20 2a 2a 20 77 65 alize() . ** we
1b983 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 73 74 61 re called on sta
1b984 74 65 6d 65 6e 74 20 70 2e 0a 20 20 2a 2f 0a 20 tement p.. */.
1b985 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1b986 49 54 45 5f 52 4f 57 20 20 7c 7c 20 72 63 3d 3d ITE_ROW || rc==
1b987 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 7c 7c SQLITE_DONE ||
1b988 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f rc==SQLITE_ERRO
1b989 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 72 63 3d R . || rc=
1b98a 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 =SQLITE_BUSY ||
1b98b 72 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53 rc==SQLITE_MISUS
1b98c 45 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 E. );. assert(
1b98d 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 52 p->rc!=SQLITE_R
1b98e 4f 57 20 26 26 20 70 2d 3e 72 63 21 3d 53 51 4c OW && p->rc!=SQL
1b98f 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 ITE_DONE );. if
1b990 28 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 ( p->isPrepareV2
1b991 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 && rc!=SQLITE_R
1b992 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 OW && rc!=SQLITE
1b993 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2f 2a 20 _DONE ){. /*
1b994 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e If this statemen
1b995 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75 t was prepared u
1b996 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 sing sqlite3_pre
1b997 70 61 72 65 5f 76 32 28 29 2c 20 61 6e 64 20 61 pare_v2(), and a
1b998 6e 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 68 n. ** error h
1b999 61 73 20 6f 63 63 75 72 65 64 2c 20 74 68 65 6e as occured, then
1b99a 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
1b99b 72 20 63 6f 64 65 20 69 6e 20 70 2d 3e 72 63 20 r code in p->rc
1b99c 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 to the. ** ca
1b99d 6c 6c 65 72 2e 20 53 65 74 20 74 68 65 20 65 72 ller. Set the er
1b99e 72 6f 72 20 63 6f 64 65 20 69 6e 20 74 68 65 20 ror code in the
1b99f 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1b9a0 74 6f 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 to the same valu
1b9a1 65 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 72 e.. */ . r
1b9a2 63 20 3d 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 c = db->errCode
1b9a3 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a 20 20 72 = p->rc;. }. r
1b9a4 65 74 75 72 6e 20 28 72 63 26 64 62 2d 3e 65 72 eturn (rc&db->er
1b9a5 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rMask);.}../*.**
1b9a6 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 This is the top
1b9a7 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e 74 -level implement
1b9a8 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
1b9a9 5f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a 2a _step(). Call.*
1b9aa 2a 20 73 71 6c 69 74 65 33 53 74 65 70 28 29 20 * sqlite3Step()
1b9ab 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74 68 to do most of th
1b9ac 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73 63 e work. If a sc
1b9ad 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75 72 hema error occur
1b9ae 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 s,.** call sqlit
1b9af 65 33 52 65 70 72 65 70 61 72 65 28 29 20 61 6e e3Reprepare() an
1b9b0 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f 0a d try again..*/.
1b9b1 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b9b2 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 qlite3_step(sqli
1b9b3 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1b9b4 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1b9b5 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66 ITE_MISUSE;. if
1b9b6 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69 ( pStmt ){. i
1b9b7 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 nt cnt = 0;.
1b9b8 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a Vdbe *v = (Vdbe*
1b9b9 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 )pStmt;. sqli
1b9ba 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b te3 *db = v->db;
1b9bb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1b9bc 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1b9bd 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ex);. while(
1b9be 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 (rc = sqlite3Ste
1b9bf 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 p(v))==SQLITE_SC
1b9c0 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20 HEMA.
1b9c1 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 && cnt++ < 5.
1b9c2 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20 3d && (rc =
1b9c3 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 sqlite3Reprepar
1b9c4 65 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b e(v))==SQLITE_OK
1b9c5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1b9c6 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 3_reset(pStmt);.
1b9c7 20 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 v->expired
1b9c8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1b9c9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 if( rc==SQLITE_S
1b9ca 43 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 CHEMA && ALWAYS(
1b9cb 76 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 v->isPrepareV2)
1b9cc 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 && ALWAYS(db->pE
1b9cd 72 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 rr) ){. /*
1b9ce 54 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 This case occurs
1b9cf 20 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 after failing t
1b9d0 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 o recompile an s
1b9d1 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 ql statement. .
1b9d2 20 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f ** The erro
1b9d3 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 r message from t
1b9d4 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 he SQL compiler
1b9d5 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1b9d6 20 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a loaded . *
1b9d7 2a 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 * into the datab
1b9d8 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 ase handle. This
1b9d9 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 block copies th
1b9da 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
1b9db 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 . ** from t
1b9dc 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1b9dd 6c 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 le into the stat
1b9de 65 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 ement and sets t
1b9df 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 he statement.
1b9e0 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f ** program co
1b9e1 75 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e unter to 0 to en
1b9e2 73 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 sure that when t
1b9e3 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
1b9e4 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 . ** finali
1b9e5 7a 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 zed or reset the
1b9e6 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 parser error me
1b9e7 73 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 ssage is availab
1b9e8 6c 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 le via. **
1b9e9 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
1b9ea 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 and sqlite3_err
1b9eb 63 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f code().. */
1b9ec 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1b9ed 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 r *zErr = (const
1b9ee 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f char *)sqlite3_
1b9ef 76 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 value_text(db->p
1b9f0 45 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c Err); . sql
1b9f1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 ite3DbFree(db, v
1b9f2 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1b9f3 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f if( !db->mallo
1b9f4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
1b9f5 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 v->zErrMsg =
1b9f6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1b9f7 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 db, zErr);.
1b9f8 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
1b9f9 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 v->zErrMsg = 0
1b9fa 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 ;. v->rc
1b9fb 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1b9fc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1b9fd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 rc = sqlite3Ap
1b9fe 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 iExit(db, rc);.
1b9ff 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1ba00 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1ba01 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1ba02 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 rc;.}../*.** Ext
1ba03 72 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61 ract the user da
1ba04 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 ta from a sqlite
1ba05 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 3_context struct
1ba06 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ure and return a
1ba07 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 .** pointer to i
1ba08 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
1ba09 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 void *sqlite3_u
1ba0a 73 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 ser_data(sqlite3
1ba0b 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 _context *p){.
1ba0c 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e assert( p && p->
1ba0d 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 pFunc );. retur
1ba0e 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 n p->pFunc->pUse
1ba0f 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rData;.}../*.**
1ba10 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72 Extract the user
1ba11 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c data from a sql
1ba12 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 ite3_context str
1ba13 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 ucture and retur
1ba14 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 n a.** pointer t
1ba15 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f o it..*/.SQLITE_
1ba16 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
1ba17 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
1ba18 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 handle(sqlite3_c
1ba19 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 ontext *p){. as
1ba1a 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 sert( p && p->pF
1ba1b 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 unc );. return
1ba1c 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a p->s.db;.}../*.*
1ba1d 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1ba1e 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 is the implement
1ba1f 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 ation of an SQL
1ba20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 6c function that al
1ba21 77 61 79 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69 ways.** fails wi
1ba22 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 th an error mess
1ba23 61 67 65 20 73 74 61 74 69 6e 67 20 74 68 61 74 age stating that
1ba24 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 the function is
1ba25 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 used in the.**
1ba26 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 wrong context.
1ba27 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 The sqlite3_over
1ba28 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20 load_function()
1ba29 41 50 49 20 6d 69 67 68 74 20 63 6f 6e 73 74 72 API might constr
1ba2a 75 63 74 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 uct.** SQL funct
1ba2b 69 6f 6e 20 74 68 61 74 20 75 73 65 20 74 68 69 ion that use thi
1ba2c 73 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 61 s routine so tha
1ba2d 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 t the functions
1ba2e 77 69 6c 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f will exist.** fo
1ba2f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f r name resolutio
1ba30 6e 20 62 75 74 20 61 72 65 20 61 63 74 75 61 6c n but are actual
1ba31 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 ly overloaded by
1ba32 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 the xFindFuncti
1ba33 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 on.** method of
1ba34 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a virtual tables..
1ba35 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1ba36 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e E void sqlite3In
1ba37 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20 validFunction(.
1ba38 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1ba39 20 2a 63 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54 *context, /* T
1ba3a 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c he function call
1ba3b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
1ba3c 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 20 20 int NotUsed,
1ba3d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1ba3e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
1ba3f 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 ts to the functi
1ba40 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f on */. sqlite3_
1ba41 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
1ba42 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65 /* Value of e
1ba43 61 63 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ach argument */.
1ba44 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
1ba45 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74 *zName = context
1ba46 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a ->pFunc->zName;.
1ba47 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 char *zErr;.
1ba48 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1ba49 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
1ba4a 65 64 32 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73 ed2);. zErr = s
1ba4b 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a qlite3_mprintf(.
1ba4c 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f "unable to
1ba4d 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 use function %s
1ba4e 20 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 in the requeste
1ba4f 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d d context", zNam
1ba50 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 e);. sqlite3_re
1ba51 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 sult_error(conte
1ba52 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 xt, zErr, -1);.
1ba53 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 sqlite3_free(zE
1ba54 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c rr);.}../*.** Al
1ba55 6c 6f 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e locate or return
1ba56 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63 the aggregate c
1ba57 6f 6e 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 ontext for a use
1ba58 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e r function. A n
1ba59 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 ew.** context is
1ba5a 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 allocated on th
1ba5b 65 20 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 e first call. S
1ba5c 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
1ba5d 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 return the.** sa
1ba5e 6d 65 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 me context that
1ba5f 77 61 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 was returned on
1ba60 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a prior calls..*/.
1ba61 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1ba62 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 *sqlite3_aggrega
1ba63 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 te_context(sqlit
1ba64 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 e3_context *p, i
1ba65 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d nt nByte){. Mem
1ba66 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 *pMem;. assert
1ba67 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 ( p && p->pFunc
1ba68 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 && p->pFunc->xSt
1ba69 65 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ep );. assert(
1ba6a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1ba6b 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 ld(p->s.db->mute
1ba6c 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 x) );. pMem = p
1ba6d 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 ->pMem;. if( (p
1ba6e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
1ba6f 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 _Agg)==0 ){.
1ba70 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a if( nByte==0 ){.
1ba71 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1ba72 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1ba73 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 nal(pMem);.
1ba74 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1ba75 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 EM_Null;. p
1ba76 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 Mem->z = 0;.
1ba77 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
1ba78 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1ba79 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b pMem, nByte, 0);
1ba7a 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1ba7b 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 gs = MEM_Agg;.
1ba7c 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 pMem->u.pDef
1ba7d 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 = p->pFunc;.
1ba7e 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 if( pMem->z )
1ba7f 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 {. memset
1ba80 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 (pMem->z, 0, nBy
1ba81 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 te);. }.
1ba82 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1ba83 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a (void*)pMem->z;.
1ba84 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1ba85 74 68 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 the auxilary dat
1ba86 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e a pointer, if an
1ba87 79 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 y, for the iArg'
1ba88 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a th argument to.*
1ba89 2a 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 * the user-funct
1ba8a 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 ion defined by p
1ba8b 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 Ctx..*/.SQLITE_A
1ba8c 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 PI void *sqlite3
1ba8d 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c _get_auxdata(sql
1ba8e 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1ba8f 74 78 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 tx, int iArg){.
1ba90 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 VdbeFunc *pVdbe
1ba91 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 Func;.. assert(
1ba92 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1ba93 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1ba94 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 mutex) );. pVdb
1ba95 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 eFunc = pCtx->pV
1ba96 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 dbeFunc;. if( !
1ba97 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 pVdbeFunc || iAr
1ba98 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 g>=pVdbeFunc->nA
1ba99 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a ux || iArg<0 ){.
1ba9a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1ba9b 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 }. return pVdbe
1ba9c 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 Func->apAux[iArg
1ba9d 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ].pAux;.}../*.**
1ba9e 20 53 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 Set the auxilar
1ba9f 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 y data pointer a
1baa0 6e 64 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 nd delete functi
1baa1 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 on, for the iArg
1baa2 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 'th.** argument
1baa3 74 6f 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 to the user-func
1baa4 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 tion defined by
1baa5 70 43 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f pCtx. Any previo
1baa6 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 us value is.** d
1baa7 65 6c 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e eleted by callin
1baa8 67 20 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e g the delete fun
1baa9 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 ction specified
1baaa 77 68 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e when it was set.
1baab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
1baac 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f oid sqlite3_set_
1baad 61 75 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 auxdata(. sqlit
1baae 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1baaf 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a , . int iArg, .
1bab0 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 void *pAux, .
1bab1 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 void (*xDelete)
1bab2 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 (void*).){. str
1bab3 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 uct AuxData *pAu
1bab4 78 44 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e xData;. VdbeFun
1bab5 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 c *pVdbeFunc;.
1bab6 69 66 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 if( iArg<0 ) got
1bab7 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 o failed;.. ass
1bab8 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1bab9 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1baba 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1babb 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 pVdbeFunc = pCtx
1babc 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 ->pVdbeFunc;. i
1babd 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c f( !pVdbeFunc ||
1babe 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 pVdbeFunc->nAux
1babf 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e <=iArg ){. in
1bac0 74 20 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 t nAux = (pVdbeF
1bac1 75 6e 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d unc ? pVdbeFunc-
1bac2 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 >nAux : 0);.
1bac3 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 int nMalloc = si
1bac4 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b zeof(VdbeFunc) +
1bac5 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 sizeof(struct A
1bac6 75 78 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 uxData)*iArg;.
1bac7 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 pVdbeFunc = sq
1bac8 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 lite3DbRealloc(p
1bac9 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 Ctx->s.db, pVdbe
1baca 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a Func, nMalloc);.
1bacb 20 20 20 20 69 66 28 20 21 70 56 64 62 65 46 75 if( !pVdbeFu
1bacc 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f nc ){. goto
1bacd 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 failed;. }.
1bace 20 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 pCtx->pVdbeFu
1bacf 6e 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a nc = pVdbeFunc;.
1bad0 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 memset(&pVdb
1bad1 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 eFunc->apAux[nAu
1bad2 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 x], 0, sizeof(st
1bad3 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 ruct AuxData)*(i
1bad4 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 Arg+1-nAux));.
1bad5 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 pVdbeFunc->nAu
1bad6 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 x = iArg+1;.
1bad7 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 pVdbeFunc->pFunc
1bad8 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a = pCtx->pFunc;.
1bad9 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 }.. pAuxData
1bada 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 = &pVdbeFunc->ap
1badb 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 Aux[iArg];. if(
1badc 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 pAuxData->pAux
1badd 26 26 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 && pAuxData->xDe
1bade 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 lete ){. pAux
1badf 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 Data->xDelete(pA
1bae0 75 78 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 uxData->pAux);.
1bae1 20 7d 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 }. pAuxData->p
1bae2 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 Aux = pAux;. pA
1bae3 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 uxData->xDelete
1bae4 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 = xDelete;. ret
1bae5 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 urn;..failed:.
1bae6 69 66 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 if( xDelete ){.
1bae7 20 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 xDelete(pAux)
1bae8 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 ;. }.}..#ifndef
1bae9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 SQLITE_OMIT_DEP
1baea 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 RECATED./*.** Re
1baeb 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
1baec 6f 66 20 74 69 6d 65 73 20 74 68 65 20 53 74 65 of times the Ste
1baed 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20 p function of a
1baee 61 67 67 72 65 67 61 74 65 20 68 61 73 20 62 65 aggregate has be
1baef 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a en .** called..*
1baf0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1baf1 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 on is deprecated
1baf2 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74 . Do not use it
1baf3 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20 for new code.
1baf4 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 It is.** provide
1baf5 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62 only to avoid b
1baf6 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63 reaking legacy c
1baf7 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67 ode. New aggreg
1baf8 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ate function.**
1baf9 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
1bafa 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69 should keep thei
1bafb 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74 r own counts wit
1bafc 68 69 6e 20 74 68 65 69 72 20 61 67 67 72 65 67 hin their aggreg
1bafd 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a ate.** context..
1bafe 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1baff 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 t sqlite3_aggreg
1bb00 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ate_count(sqlite
1bb01 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 3_context *p){.
1bb02 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d assert( p && p-
1bb03 3e 70 4d 65 6d 20 26 26 20 70 2d 3e 70 46 75 6e >pMem && p->pFun
1bb04 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 c && p->pFunc->x
1bb05 53 74 65 70 20 29 3b 0a 20 20 72 65 74 75 72 6e Step );. return
1bb06 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 7d 0a 23 p->pMem->n;.}.#
1bb07 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
1bb08 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1bb09 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1bb0a 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 result set for
1bb0b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 the statement pS
1bb0c 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 tmt..*/.SQLITE_A
1bb0d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
1bb0e 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 olumn_count(sqli
1bb0f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1bb10 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 {. Vdbe *pVm =
1bb11 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1bb12 20 72 65 74 75 72 6e 20 70 56 6d 20 3f 20 70 56 return pVm ? pV
1bb13 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a 20 m->nResColumn :
1bb14 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 0;.}../*.** Retu
1bb15 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1bb16 20 76 61 6c 75 65 73 20 61 76 61 69 6c 61 62 6c values availabl
1bb17 65 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 e from the curre
1bb18 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a nt row of the.**
1bb19 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 currently execu
1bb1a 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 ting statement p
1bb1b 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Stmt..*/.SQLITE_
1bb1c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bb1d 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 data_count(sqlit
1bb1e 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1bb1f 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 . Vdbe *pVm = (
1bb20 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1bb21 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70 56 if( pVm==0 || pV
1bb22 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d 30 m->pResultSet==0
1bb23 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 ) return 0;. r
1bb24 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43 eturn pVm->nResC
1bb25 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a olumn;.}.../*.**
1bb26 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
1bb27 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 column iCol of
1bb28 74 68 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d the given statem
1bb29 65 6e 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49 ent is valid. I
1bb2a 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75 f.** it is, retu
1bb2b 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1bb2c 74 68 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20 the Mem for the
1bb2d 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f value of that co
1bb2e 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c lumn..** If iCol
1bb2f 20 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72 is not valid, r
1bb30 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1bb31 74 6f 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68 to a Mem which h
1bb32 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 as a value.** of
1bb33 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 NULL..*/.static
1bb34 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 Mem *columnMem(
1bb35 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bb36 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 tmt, int i){. V
1bb37 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20 dbe *pVm;. int
1bb38 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75 vals;. Mem *pOu
1bb39 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62 t;.. pVm = (Vdb
1bb3a 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 e *)pStmt;. if(
1bb3b 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e 70 52 65 pVm && pVm->pRe
1bb3c 73 75 6c 74 53 65 74 21 3d 30 20 26 26 20 69 3c sultSet!=0 && i<
1bb3d 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 pVm->nResColumn
1bb3e 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 73 && i>=0 ){. s
1bb3f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1bb40 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 er(pVm->db->mute
1bb41 78 29 3b 0a 20 20 20 20 76 61 6c 73 20 3d 20 73 x);. vals = s
1bb42 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e qlite3_data_coun
1bb43 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 4f t(pStmt);. pO
1bb44 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75 ut = &pVm->pResu
1bb45 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73 ltSet[i];. }els
1bb46 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 e{. /* If the
1bb47 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 value passed as
1bb48 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
1bb49 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f 66 20 72 ment is out of r
1bb4a 61 6e 67 65 2c 20 72 65 74 75 72 6e 0a 20 20 20 ange, return.
1bb4b 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ** a pointer to
1bb4c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
1bb4d 74 61 74 69 63 20 4d 65 6d 20 6f 62 6a 65 63 74 tatic Mem object
1bb4e 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 which contains
1bb4f 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 the. ** value
1bb50 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 65 6e 20 SQL NULL. Even
1bb51 74 68 6f 75 67 68 20 74 68 65 20 4d 65 6d 20 73 though the Mem s
1bb52 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
1bb53 73 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 20 20 20 s an element.
1bb54 20 2a 2a 20 6f 66 20 74 79 70 65 20 69 36 34 2c ** of type i64,
1bb55 20 6f 6e 20 63 65 72 74 61 69 6e 20 61 72 63 68 on certain arch
1bb56 69 74 65 63 74 75 72 65 20 28 78 38 36 29 20 77 itecture (x86) w
1bb57 69 74 68 20 63 65 72 74 61 69 6e 20 63 6f 6d 70 ith certain comp
1bb58 69 6c 65 72 0a 20 20 20 20 2a 2a 20 73 77 69 74 iler. ** swit
1bb59 63 68 65 73 20 28 2d 4f 73 29 2c 20 67 63 63 20 ches (-Os), gcc
1bb5a 6d 61 79 20 61 6c 69 67 6e 20 74 68 69 73 20 4d may align this M
1bb5b 65 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 61 20 34 em object on a 4
1bb5c 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 0a 20 -byte boundary.
1bb5d 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 ** instead of
1bb5e 20 61 6e 20 38 2d 62 79 74 65 20 6f 6e 65 2e 20 an 8-byte one.
1bb5f 54 68 69 73 20 61 6c 6c 20 77 6f 72 6b 73 20 66 This all works f
1bb60 69 6e 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 ine, except that
1bb61 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 75 6e when. ** run
1bb62 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45 ning with SQLITE
1bb63 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 20 74 _DEBUG defined t
1bb64 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 73 he SQLite code s
1bb65 6f 6d 65 74 69 6d 65 73 20 61 73 73 65 72 74 28 ometimes assert(
1bb66 29 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61 )s. ** that a
1bb67 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 69 Mem structure i
1bb68 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20 s located on an
1bb69 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 8-byte boundary.
1bb6a 20 54 6f 20 70 72 65 76 65 6e 74 0a 20 20 20 20 To prevent.
1bb6b 2a 2a 20 74 68 69 73 20 61 73 73 65 72 74 28 29 ** this assert()
1bb6c 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2c 20 77 from failing, w
1bb6d 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 77 69 74 hen building wit
1bb6e 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 h SQLITE_DEBUG d
1bb6f 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 75 73 efined. ** us
1bb70 69 6e 67 20 67 63 63 2c 20 66 6f 72 63 65 20 6e ing gcc, force n
1bb71 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 38 2d 62 ullMem to be 8-b
1bb72 79 74 65 20 61 6c 69 67 6e 65 64 20 75 73 69 6e yte aligned usin
1bb73 67 20 74 68 65 20 6d 61 67 69 63 61 6c 0a 20 20 g the magical.
1bb74 20 20 2a 2a 20 5f 5f 61 74 74 72 69 62 75 74 65 ** __attribute
1bb75 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 __((aligned(8)))
1bb76 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20 20 20 20 macro. */.
1bb77 73 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d static const Mem
1bb78 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66 20 64 65 nullMem .#if de
1bb79 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
1bb7a 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f UG) && defined(_
1bb7b 5f 47 4e 55 43 5f 5f 29 0a 20 20 20 20 20 20 5f _GNUC__). _
1bb7c 5f 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c _attribute__((al
1bb7d 69 67 6e 65 64 28 38 29 29 29 20 0a 23 65 6e 64 igned(8))) .#end
1bb7e 69 66 0a 20 20 20 20 20 20 3d 20 7b 7b 30 7d 2c if. = {{0},
1bb7f 20 28 64 6f 75 62 6c 65 29 30 2c 20 30 2c 20 22 (double)0, 0, "
1bb80 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 ", 0, MEM_Null,
1bb81 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 SQLITE_NULL, 0,
1bb82 30 2c 20 30 20 7d 3b 0a 0a 20 20 20 20 69 66 28 0, 0 };.. if(
1bb83 20 70 56 6d 20 26 26 20 41 4c 57 41 59 53 28 70 pVm && ALWAYS(p
1bb84 56 6d 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 Vm->db) ){.
1bb85 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1bb86 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 nter(pVm->db->mu
1bb87 74 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 tex);. sqli
1bb88 74 65 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 te3Error(pVm->db
1bb89 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 , SQLITE_RANGE,
1bb8a 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 0);. }. pO
1bb8b 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c ut = (Mem*)&null
1bb8c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Mem;. }. retur
1bb8d 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pOut;.}../*.**
1bb8e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1bb8f 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 s called after i
1bb90 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 nvoking an sqlit
1bb91 65 33 5f 76 61 6c 75 65 5f 58 58 58 20 66 75 6e e3_value_XXX fun
1bb92 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a 2a 20 63 ction on a .** c
1bb93 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28 69 2e 65 olumn value (i.e
1bb94 2e 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e . a value return
1bb95 65 64 20 62 79 20 65 76 61 6c 75 61 74 69 6e 67 ed by evaluating
1bb96 20 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 an SQL expressi
1bb97 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 73 65 6c on in the.** sel
1bb98 65 63 74 20 6c 69 73 74 20 6f 66 20 61 20 53 45 ect list of a SE
1bb99 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 LECT statement)
1bb9a 74 68 61 74 20 6d 61 79 20 63 61 75 73 65 20 61 that may cause a
1bb9b 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
1bb9c 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 e. If .** malloc
1bb9d 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74 () has failed, t
1bb9e 68 65 20 74 68 72 65 61 64 73 20 6d 61 6c 6c 6f he threads mallo
1bb9f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 cFailed flag is
1bba0 63 6c 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 cleared and the
1bba1 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 20 6f result.** code o
1bba2 66 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d f statement pStm
1bba3 74 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f t set to SQLITE_
1bba4 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 NOMEM..**.** Spe
1bba5 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 cifically, this
1bba6 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 is called from w
1bba7 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ithin:.**.**
1bba8 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1bba9 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c int().** sql
1bbaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 ite3_column_int6
1bbab 34 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 4().** sqlit
1bbac 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
1bbad 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
1bbae 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a column_text16().
1bbaf 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1bbb0 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 2a 2a 20 olumn_real().**
1bbb1 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1bbb2 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 20 20 mn_bytes().**
1bbb3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
1bbb4 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a 0a 2a 2a _bytes16().**.**
1bbb5 20 42 75 74 20 6e 6f 74 20 66 6f 72 20 73 71 6c But not for sql
1bbb6 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
1bbb7 28 29 2c 20 77 68 69 63 68 20 6e 65 76 65 72 20 (), which never
1bbb8 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 29 2e 0a calls malloc()..
1bbb9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
1bbba 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1bbbb 72 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 re(sqlite3_stmt
1bbbc 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49 *pStmt).{. /* I
1bbbd 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 f malloc() faile
1bbbe 64 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63 6f d during an enco
1bbbf 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ding conversion
1bbc0 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a 2a 20 73 within an. ** s
1bbc1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58 qlite3_column_XX
1bbc2 58 20 41 50 49 2c 20 74 68 65 6e 20 73 65 74 20 X API, then set
1bbc3 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 the return code
1bbc4 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 of the statement
1bbc5 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f to. ** SQLITE_
1bbc6 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 78 74 20 NOMEM. The next
1bbc7 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70 28 29 20 call to _step()
1bbc8 28 69 66 20 61 6e 79 29 20 77 69 6c 6c 20 72 65 (if any) will re
1bbc9 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1bbca 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 69 6e 61 R. ** and _fina
1bbcb 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72 65 74 75 lize() will retu
1bbcc 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 rn NOMEM.. */.
1bbcd 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1bbce 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 *)pStmt;. if(
1bbcf 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d p ){. p->rc =
1bbd0 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 sqlite3ApiExit(
1bbd1 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 20 p->db, p->rc);.
1bbd2 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1bbd3 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1bbd4 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a tex);. }.}../**
1bbd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bbd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ********** sqlit
1bbd7 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a e3_column_ ****
1bbd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bbd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1bbda 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1bbdb 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 tines are used t
1bbdc 6f 20 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74 o access element
1bbdd 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 s of the current
1bbde 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 72 row.** in the r
1bbdf 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 esult set..*/.SQ
1bbe0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1bbe1 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1bbe2 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 umn_blob(sqlite3
1bbe3 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bbe4 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f t i){. const vo
1bbe5 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20 3d id *val;. val =
1bbe6 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
1bbe7 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 lob( columnMem(p
1bbe8 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a 20 Stmt,i) );. /*
1bbe9 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 72 Even though ther
1bbea 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e 67 e is no encoding
1bbeb 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61 6c conversion, val
1bbec 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74 0a ue_blob() might.
1bbed 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61 6c ** need to cal
1bbee 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65 78 l malloc() to ex
1bbef 70 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 pand the result
1bbf0 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29 20 of a zeroblob()
1bbf1 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e . ** expression
1bbf2 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e . . */. column
1bbf3 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1bbf4 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 tmt);. return v
1bbf5 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 al;.}.SQLITE_API
1bbf6 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
1bbf7 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 umn_bytes(sqlite
1bbf8 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1bbf9 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c nt i){. int val
1bbfa 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1bbfb 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 _bytes( columnMe
1bbfc 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 m(pStmt,i) );.
1bbfd 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
1bbfe 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
1bbff 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 turn val;.}.SQLI
1bc00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bc01 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
1bc02 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 6(sqlite3_stmt *
1bc03 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bc04 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 int val = sqlit
1bc05 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 e3_value_bytes16
1bc06 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d ( columnMem(pStm
1bc07 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e t,i) );. column
1bc08 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1bc09 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 tmt);. return v
1bc0a 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 al;.}.SQLITE_API
1bc0b 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f double sqlite3_
1bc0c 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 column_double(sq
1bc0d 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bc0e 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75 t, int i){. dou
1bc0f 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 ble val = sqlite
1bc10 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20 3_value_double(
1bc11 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c columnMem(pStmt,
1bc12 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 i) );. columnMa
1bc13 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d llocFailure(pStm
1bc14 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c t);. return val
1bc15 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1bc16 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
1bc17 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 n_int(sqlite3_st
1bc18 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bc19 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 ){. int val = s
1bc1a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
1bc1b 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d ( columnMem(pStm
1bc1c 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e t,i) );. column
1bc1d 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1bc1e 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 tmt);. return v
1bc1f 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 al;.}.SQLITE_API
1bc20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 sqlite_int64 sq
1bc21 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 lite3_column_int
1bc22 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 64(sqlite3_stmt
1bc23 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bc24 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 sqlite_int64 v
1bc25 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1bc26 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e ue_int64( column
1bc27 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1bc28 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
1bc29 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
1bc2a 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 return val;.}.SQ
1bc2b 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 LITE_API const u
1bc2c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 nsigned char *sq
1bc2d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1bc2e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1bc2f 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bc30 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1bc31 63 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69 char *val = sqli
1bc32 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20 te3_value_text(
1bc33 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c columnMem(pStmt,
1bc34 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 i) );. columnMa
1bc35 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d llocFailure(pStm
1bc36 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c t);. return val
1bc37 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 ;.}.SQLITE_API s
1bc38 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 qlite3_value *sq
1bc39 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c lite3_column_val
1bc3a 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ue(sqlite3_stmt
1bc3b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bc3c 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 63 6f Mem *pOut = co
1bc3d 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20 69 lumnMem(pStmt, i
1bc3e 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 2d 3e 66 );. if( pOut->f
1bc3f 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 20 lags&MEM_Static
1bc40 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 ){. pOut->fla
1bc41 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 61 74 69 gs &= ~MEM_Stati
1bc42 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 c;. pOut->fla
1bc43 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b gs |= MEM_Ephem;
1bc44 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c . }. columnMal
1bc45 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1bc46 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c );. return (sql
1bc47 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 70 4f 75 ite3_value *)pOu
1bc48 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c t;.}.#ifndef SQL
1bc49 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1bc4a 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1bc4b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
1bc4c 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 lumn_text16(sqli
1bc4d 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bc4e 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 int i){. const
1bc4f 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20 73 71 6c void *val = sql
1bc50 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
1bc51 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6( columnMem(pSt
1bc52 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1bc53 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1bc54 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1bc55 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a val;.}.#endif /*
1bc56 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bc57 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 16 */.SQLITE_API
1bc58 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
1bc59 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 umn_type(sqlite3
1bc5a 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bc5b 74 20 69 29 7b 0a 20 20 69 6e 74 20 69 54 79 70 t i){. int iTyp
1bc5c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 e = sqlite3_valu
1bc5d 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 e_type( columnMe
1bc5e 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 m(pStmt,i) );.
1bc5f 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
1bc60 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
1bc61 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f turn iType;.}../
1bc62 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1bc63 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65 function is expe
1bc64 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 rimental and sub
1bc65 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f ject to change o
1bc66 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a r.** removal */.
1bc67 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f /*int sqlite3_co
1bc68 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79 70 lumn_numeric_typ
1bc69 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1bc6a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a pStmt, int i){.*
1bc6b 2a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 * return sqlite
1bc6c 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 3_value_numeric_
1bc6d 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 type( columnMem(
1bc6e 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a pStmt,i) );.**}.
1bc6f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 */../*.** Conver
1bc70 74 20 74 68 65 20 4e 2d 74 68 20 65 6c 65 6d 65 t the N-th eleme
1bc71 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e 70 43 6f nt of pStmt->pCo
1bc72 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 61 20 73 lName[] into a s
1bc73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a 2a 20 78 tring using.** x
1bc74 46 75 6e 63 28 29 20 74 68 65 6e 20 72 65 74 75 Func() then retu
1bc75 72 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 rn that string.
1bc76 20 49 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 If N is out of
1bc77 72 61 6e 67 65 2c 20 72 65 74 75 72 6e 20 30 2e range, return 0.
1bc78 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
1bc79 20 75 70 20 74 6f 20 35 20 6e 61 6d 65 73 20 66 up to 5 names f
1bc7a 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 or each column.
1bc7b 20 75 73 65 54 79 70 65 20 64 65 74 65 72 6d 69 useType determi
1bc7c 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20 6e 61 6d nes which.** nam
1bc7d 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 e is returned.
1bc7e 48 65 72 65 20 61 72 65 20 74 68 65 20 6e 61 6d Here are the nam
1bc7f 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 es:.**.** 0
1bc80 20 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e The column n
1bc81 61 6d 65 20 61 73 20 69 74 20 73 68 6f 75 6c 64 ame as it should
1bc82 20 62 65 20 64 69 73 70 6c 61 79 65 64 20 66 6f be displayed fo
1bc83 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 31 r output.** 1
1bc84 20 20 20 20 20 20 54 68 65 20 64 61 74 61 74 79 The dataty
1bc85 70 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 pe name for the
1bc86 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 32 20 20 column.** 2
1bc87 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 The name of
1bc88 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 the database tha
1bc89 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 t the column der
1bc8a 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 ives from.**
1bc8b 33 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 3 The name
1bc8c 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 of the table tha
1bc8d 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 t the column der
1bc8e 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 ives from.**
1bc8f 34 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 4 The name
1bc90 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c of the table col
1bc91 75 6d 6e 20 74 68 61 74 20 74 68 65 20 72 65 73 umn that the res
1bc92 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 ult column deriv
1bc93 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 es from.**.** If
1bc94 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e the result is n
1bc95 6f 74 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75 ot a simple colu
1bc96 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 28 69 66 mn reference (if
1bc97 20 69 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 it is an expres
1bc98 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e sion.** or a con
1bc99 73 74 61 6e 74 29 20 74 68 65 6e 20 75 73 65 54 stant) then useT
1bc9a 79 70 65 73 20 32 2c 20 33 2c 20 61 6e 64 20 34 ypes 2, 3, and 4
1bc9b 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f return NULL..*/
1bc9c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 76 6f .static const vo
1bc9d 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a id *columnName(.
1bc9e 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1bc9f 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a pStmt,. int N,.
1bca0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 28 2a const void *(*
1bca1 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c 0a 20 20 xFunc)(Mem*),.
1bca2 69 6e 74 20 75 73 65 54 79 70 65 0a 29 7b 0a 20 int useType.){.
1bca3 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 72 65 74 const void *ret
1bca4 20 3d 20 30 3b 0a 20 20 56 64 62 65 20 2a 70 20 = 0;. Vdbe *p
1bca5 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1bca6 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
1bca7 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1bca8 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 64 62 . . assert( db
1bca9 21 3d 30 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c !=0 );. n = sql
1bcaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
1bcab 74 28 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20 t(pStmt);. if(
1bcac 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 N<n && N>=0 ){.
1bcad 20 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a N += useType*
1bcae 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d n;. sqlite3_m
1bcaf 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d utex_enter(db->m
1bcb0 75 74 65 78 29 3b 0a 20 20 20 20 61 73 73 65 72 utex);. asser
1bcb1 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
1bcb2 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 led==0 );. re
1bcb3 74 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e 61 43 t = xFunc(&p->aC
1bcb4 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20 20 20 20 olName[N]);.
1bcb5 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 /* A malloc may
1bcb6 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 have failed ins
1bcb7 69 64 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63 ide of the xFunc
1bcb8 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73 () call. If this
1bcb9 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 . ** is the c
1bcba 61 73 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d ase, clear the m
1bcbb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
1bcbc 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c and return NULL
1bcbd 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1bcbe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1bcbf 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d d ){. db->m
1bcc0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b allocFailed = 0;
1bcc1 0a 20 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a . ret = 0;.
1bcc2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1bcc3 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 3_mutex_leave(db
1bcc4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1bcc5 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f return ret;.}../
1bcc6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1bcc7 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20 name of the Nth
1bcc8 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 column of the re
1bcc9 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 sult set returne
1bcca 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 d by SQL.** stat
1bccb 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a ement pStmt..*/.
1bccc 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bccd 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1bcce 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 olumn_name(sqlit
1bccf 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bcd0 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1bcd1 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1bcd2 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1bcd3 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1bcd4 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1bcd5 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e _text, COLNAME_N
1bcd6 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 AME);.}.#ifndef
1bcd7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1bcd8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1bcd9 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1bcda 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 _column_name16(s
1bcdb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bcdc 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1bcdd 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1bcde 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1bcdf 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1bce0 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1bce1 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
1bce2 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 NAME_NAME);.}.#e
1bce3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 ndif../*.** Cons
1bce4 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20 traint: If you
1bce5 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 have ENABLE_COLU
1bce6 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e MN_METADATA then
1bce7 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 you must.** not
1bce8 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43 define OMIT_DEC
1bce9 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 LTYPE..*/.#if de
1bcea 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1bceb 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64 T_DECLTYPE) && d
1bcec 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
1bced 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1bcee 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d DATA).# error "M
1bcef 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62 ust not define b
1bcf0 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f oth SQLITE_OMIT_
1bcf1 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20 DECLTYPE \.
1bcf2 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 and SQLITE_E
1bcf3 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 NABLE_COLUMN_MET
1bcf4 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23 ADATA".#endif..#
1bcf5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bcf6 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a IT_DECLTYPE./*.*
1bcf7 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c * Return the col
1bcf8 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 umn declaration
1bcf9 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 type (if applica
1bcfa 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 ble) of the 'i't
1bcfb 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 h column.** of t
1bcfc 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
1bcfd 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 SQL statement p
1bcfe 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Stmt..*/.SQLITE_
1bcff 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bd00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
1bd01 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f ecltype(sqlite3_
1bd02 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bd03 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bd04 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bd05 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bd06 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bd07 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bd08 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c xt, COLNAME_DECL
1bd09 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 TYPE);.}.#ifndef
1bd0a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bd0b 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1bd0c 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1bd0d 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 3_column_decltyp
1bd0e 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
1bd0f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bd10 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bd11 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bd12 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bd13 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bd14 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1bd15 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 , COLNAME_DECLTY
1bd16 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a PE);.}.#endif /*
1bd17 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bd18 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 16 */.#endif /*
1bd19 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
1bd1a 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 TYPE */..#ifdef
1bd1b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f SQLITE_ENABLE_CO
1bd1c 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a LUMN_METADATA./*
1bd1d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1bd1e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1bd1f 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 ase from which a
1bd20 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
1bd21 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 erives..** NULL
1bd22 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
1bd23 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e he result column
1bd24 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
1bd25 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 n or constant or
1bd26 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 .** anything els
1bd27 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 e which is not a
1bd28 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 n unabiguous ref
1bd29 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 erence to a data
1bd2a 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a base column..*/.
1bd2b 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bd2c 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1bd2d 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e olumn_database_n
1bd2e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
1bd2f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bd30 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bd31 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bd32 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bd33 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bd34 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
1bd35 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 COLNAME_DATABASE
1bd36 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
1bd37 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1bd38 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1bd39 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
1bd3a 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
1bd3b 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
1bd3c 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
1bd3d 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
1bd3e 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
1bd3f 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
1bd40 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
1bd41 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
1bd42 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 6, COLNAME_DATAB
1bd43 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ASE);.}.#endif /
1bd44 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
1bd45 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 F16 */../*.** Re
1bd46 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bd47 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 the table from
1bd48 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 which a result c
1bd49 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a olumn derives..*
1bd4a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
1bd4b 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 ed if the result
1bd4c 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 column is an ex
1bd4d 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 pression or cons
1bd4e 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 tant or.** anyth
1bd4f 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 ing else which i
1bd50 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 s not an unabigu
1bd51 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f ous reference to
1bd52 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 a database colu
1bd53 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 mn..*/.SQLITE_AP
1bd54 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
1bd55 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 lite3_column_tab
1bd56 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f le_name(sqlite3_
1bd57 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bd58 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bd59 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bd5a 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bd5b 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bd5c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bd5d 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c xt, COLNAME_TABL
1bd5e 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 E);.}.#ifndef SQ
1bd5f 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
1bd60 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bd61 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
1bd62 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 olumn_table_name
1bd63 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1bd64 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1bd65 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1bd66 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1bd67 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1bd68 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1bd69 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
1bd6a 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b COLNAME_TABLE);
1bd6b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1bd6c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1bd6d 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
1bd6e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1bd6f 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f table column fro
1bd70 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 m which a result
1bd71 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e column derives.
1bd72 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
1bd73 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 rned if the resu
1bd74 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 lt column is an
1bd75 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f expression or co
1bd76 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 nstant or.** any
1bd77 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 thing else which
1bd78 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 is not an unabi
1bd79 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 guous reference
1bd7a 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f to a database co
1bd7b 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lumn..*/.SQLITE_
1bd7c 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bd7d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f sqlite3_column_o
1bd7e 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 rigin_name(sqlit
1bd7f 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bd80 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1bd81 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1bd82 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1bd83 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1bd84 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1bd85 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 _text, COLNAME_C
1bd86 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 OLUMN);.}.#ifnde
1bd87 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
1bd88 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
1bd89 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1bd8a 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
1bd8b 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
1bd8c 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bd8d 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bd8e 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bd8f 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bd90 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bd91 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bd92 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f xt16, COLNAME_CO
1bd93 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 LUMN);.}.#endif
1bd94 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 /* SQLITE_OMIT_U
1bd95 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f TF16 */.#endif /
1bd96 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
1bd97 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 COLUMN_METADATA
1bd98 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
1bd99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd9a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 ***** sqlite3_bi
1bd9b 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nd_ ***********
1bd9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd9d 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 .** .** Routines
1bd9e 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20 used to attach
1bd9f 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61 values to wildca
1bda0 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 rds in a compile
1bda1 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e d SQL statement.
1bda2 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 .*/./*.** Unbind
1bda3 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 the value bound
1bda4 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69 to variable i i
1bda5 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e n virtual machin
1bda6 65 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65 e p. This is the
1bda7 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 .** the same as
1bda8 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 binding a NULL
1bda9 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c value to the col
1bdaa 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20 umn. If the "i"
1bdab 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 parameter is.**
1bdac 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
1bdad 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 en SQLITE_RANGE
1bdae 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 is returned. Oth
1bdaf 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e ewise SQLITE_OK.
1bdb0 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73 .**.** A success
1bdb1 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f ful evaluation o
1bdb2 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 f this routine a
1bdb3 63 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65 cquires the mute
1bdb4 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d x on p..** the m
1bdb5 75 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64 utex is released
1bdb6 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 if any kind of
1bdb7 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a error occurs..**
1bdb8 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f .** The error co
1bdb9 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 de stored in dat
1bdba 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f abase p->db is o
1bdbb 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 verwritten with
1bdbc 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 the return.** va
1bdbd 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e lue in any case.
1bdbe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
1bdbf 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a dbeUnbind(Vdbe *
1bdc0 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d p, int i){. Mem
1bdc1 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d *pVar;. if( p=
1bdc2 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1bdc3 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c TE_MISUSE;. sql
1bdc4 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1bdc5 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1bdc6 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d if( p->magic!=
1bdc7 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c VDBE_MAGIC_RUN |
1bdc8 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 | p->pc>=0 ){.
1bdc9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
1bdca 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53 ->db, SQLITE_MIS
1bdcb 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c USE, 0);. sql
1bdcc 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bdcd 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1bdce 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1bdcf 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
1bdd0 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e if( i<1 || i>p->
1bdd1 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 nVar ){. sqli
1bdd2 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 te3Error(p->db,
1bdd3 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 SQLITE_RANGE, 0)
1bdd4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bdd5 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bdd6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 >mutex);. ret
1bdd7 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 urn SQLITE_RANGE
1bdd8 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70 ;. }. i--;. p
1bdd9 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 Var = &p->aVar[i
1bdda 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ];. sqlite3Vdbe
1bddb 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29 MemRelease(pVar)
1bddc 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 ;. pVar->flags
1bddd 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71 = MEM_Null;. sq
1bdde 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 lite3Error(p->db
1bddf 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b , SQLITE_OK, 0);
1bde0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 69 .. /* If the bi
1bde1 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
1bde2 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 to this variable
1bde3 20 69 6e 20 56 64 62 65 2e 65 78 70 6d 61 73 6b in Vdbe.expmask
1bde4 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 0a 20 is set, then .
1bde5 20 2a 2a 20 62 69 6e 64 69 6e 67 20 61 20 6e 65 ** binding a ne
1bde6 77 20 76 61 6c 75 65 20 74 6f 20 74 68 69 73 20 w value to this
1bde7 76 61 72 69 61 62 6c 65 20 69 6e 76 61 6c 69 64 variable invalid
1bde8 61 74 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 ates the current
1bde9 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 20 20 2a query plan.. *
1bdea 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 /. if( p->isPre
1bdeb 70 61 72 65 56 32 20 26 26 0a 20 20 20 20 20 28 pareV2 &&. (
1bdec 28 69 3c 33 32 20 26 26 20 70 2d 3e 65 78 70 6d (i<32 && p->expm
1bded 61 73 6b 20 26 20 28 28 75 33 32 29 31 20 3c 3c ask & ((u32)1 <<
1bdee 20 69 29 29 20 7c 7c 20 70 2d 3e 65 78 70 6d 61 i)) || p->expma
1bdef 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 29 0a sk==0xffffffff).
1bdf0 20 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 ){. p->expi
1bdf1 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 red = 1;. }. r
1bdf2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1bdf3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 .}../*.** Bind a
1bdf4 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 text or BLOB va
1bdf5 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lue..*/.static i
1bdf6 6e 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 nt bindText(. s
1bdf7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bdf8 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 mt, /* The sta
1bdf9 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 tement to bind a
1bdfa 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 gainst */. int
1bdfb 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i,
1bdfc 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
1bdfd 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 he parameter to
1bdfe 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 bind */. const
1bdff 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 void *zData,
1be00 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
1be01 68 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f he data to be bo
1be02 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 und */. int nDa
1be03 74 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ta,
1be04 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1be05 65 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 es of data to be
1be06 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 bound */. void
1be07 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c (*xDel)(void*),
1be08 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 /* Destructor
1be09 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f for the data */
1be0a 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 . u8 encoding
1be0b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 /* Enc
1be0c 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 oding for the da
1be0d 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 ta */.){. Vdbe
1be0e 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1be0f 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b mt;. Mem *pVar;
1be10 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 . int rc;.. rc
1be11 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
1be12 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
1be13 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1be14 69 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a if( zData!=0 ){.
1be15 20 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d pVar = &p-
1be16 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 >aVar[i-1];.
1be17 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1be18 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 beMemSetStr(pVar
1be19 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 , zData, nData,
1be1a 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b encoding, xDel);
1be1b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
1be1c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f QLITE_OK && enco
1be1d 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 ding!=0 ){.
1be1e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1be1f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1be20 67 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 g(pVar, ENC(p->d
1be21 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 b));. }.
1be22 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1be23 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 p->db, rc, 0);.
1be24 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1be25 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 3ApiExit(p->db,
1be26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 rc);. }. s
1be27 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1be28 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1be29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1be2a 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e c;.}.../*.** Bin
1be2b 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 d a blob value t
1be2c 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 o an SQL stateme
1be2d 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a nt variable..*/.
1be2e 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1be2f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
1be30 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 (. sqlite3_stmt
1be31 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 *pStmt, . int
1be32 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 i, . const void
1be33 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 *zData, . int
1be34 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 nData, . void (
1be35 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b *xDel)(void*).){
1be36 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 . return bindTe
1be37 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 xt(pStmt, i, zDa
1be38 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c ta, nData, xDel,
1be39 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 0);.}.SQLITE_AP
1be3a 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1be3b 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 nd_double(sqlite
1be3c 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1be3d 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 nt i, double rVa
1be3e 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a lue){. int rc;.
1be3f 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1be40 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 e *)pStmt;. rc
1be41 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 = vdbeUnbind(p,
1be42 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 i);. if( rc==SQ
1be43 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
1be44 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1be45 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b Double(&p->aVar[
1be46 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 i-1], rValue);.
1be47 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1be48 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1be49 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1be4a 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
1be4b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1be4c 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 bind_int(sqlite3
1be4d 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c _stmt *p, int i,
1be4e 20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 int iValue){.
1be4f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 return sqlite3_b
1be50 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 ind_int64(p, i,
1be51 28 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a (i64)iValue);.}.
1be52 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1be53 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 qlite3_bind_int6
1be54 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 4(sqlite3_stmt *
1be55 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 pStmt, int i, sq
1be56 6c 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 lite_int64 iValu
1be57 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e){. int rc;.
1be58 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1be59 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 *)pStmt;. rc =
1be5a 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 vdbeUnbind(p, i)
1be5b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1be5c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
1be5d 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e ite3VdbeMemSetIn
1be5e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 t64(&p->aVar[i-1
1be5f 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 ], iValue);.
1be60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1be61 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1be62 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1be63 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 rc;.}.SQLITE_API
1be64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1be65 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 d_null(sqlite3_s
1be66 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1be67 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 i){. int rc;.
1be68 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
1be69 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 )pStmt;. rc = v
1be6a 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b dbeUnbind(p, i);
1be6b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1be6c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
1be6d 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1be6e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1be6f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1be70 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
1be71 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
1be72 78 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 xt( . sqlite3_s
1be73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 tmt *pStmt, . i
1be74 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 nt i, . const c
1be75 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 har *zData, . i
1be76 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 nt nData, . voi
1be77 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1be78 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e .){. return bin
1be79 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 dText(pStmt, i,
1be7a 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 zData, nData, xD
1be7b 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 el, SQLITE_UTF8)
1be7c 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1be7d 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1be7e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1be7f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 ite3_bind_text16
1be80 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 (. sqlite3_stmt
1be81 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 *pStmt, . int
1be82 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 i, . const void
1be83 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 *zData, . int
1be84 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 nData, . void (
1be85 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b *xDel)(void*).){
1be86 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 . return bindTe
1be87 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 xt(pStmt, i, zDa
1be88 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c ta, nData, xDel,
1be89 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
1be8a 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f IVE);.}.#endif /
1be8b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
1be8c 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 F16 */.SQLITE_AP
1be8d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1be8e 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 nd_value(sqlite3
1be8f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1be90 74 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 t i, const sqlit
1be91 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 e3_value *pValue
1be92 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
1be93 77 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 witch( pValue->t
1be94 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ype ){. case
1be95 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 SQLITE_INTEGER:
1be96 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1be97 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
1be98 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 pStmt, i, pValue
1be99 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 ->u.i);. br
1be9a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1be9b 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
1be9c 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 : {. rc = s
1be9d 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 qlite3_bind_doub
1be9e 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 le(pStmt, i, pVa
1be9f 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 lue->r);. b
1bea0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1bea1 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 case SQLITE_BLOB
1bea2 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56 : {. if( pV
1bea3 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 alue->flags & ME
1bea4 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1bea5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 rc = sqlite3_b
1bea6 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 ind_zeroblob(pSt
1bea7 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 mt, i, pValue->u
1bea8 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d .nZero);. }
1bea9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
1beaa 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f = sqlite3_bind_
1beab 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 blob(pStmt, i, p
1beac 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 Value->z, pValue
1bead 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 ->n,SQLITE_TRANS
1beae 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 IENT);. }.
1beaf 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1beb0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
1beb1 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 E_TEXT: {.
1beb2 72 63 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53 rc = bindText(pS
1beb3 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e tmt,i, pValue->
1beb4 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 z, pValue->n, SQ
1beb5 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a LITE_TRANSIENT,.
1beb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1beb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 pV
1beb8 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 alue->enc);.
1beb9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1beba 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1bebb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1bebc 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 _bind_null(pStmt
1bebd 2c 20 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 , i);. brea
1bebe 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 k;. }. }. r
1bebf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
1bec0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bec1 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 e3_bind_zeroblob
1bec2 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bec3 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 Stmt, int i, int
1bec4 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 n){. int rc;.
1bec5 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1bec6 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d *)pStmt;. rc =
1bec7 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
1bec8 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1bec9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 ITE_OK ){. sq
1beca 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
1becb 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 eroBlob(&p->aVar
1becc 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 [i-1], n);. s
1becd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bece 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1becf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1bed0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
1bed1 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1bed2 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 wildcards that
1bed3 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c can be potential
1bed4 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 ly bound to..**
1bed5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1bed6 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 added to support
1bed7 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a DBD::SQLite. .
1bed8 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1bed9 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 t sqlite3_bind_p
1beda 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 arameter_count(s
1bedb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bedc 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d mt){. Vdbe *p =
1bedd 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1bede 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e return p ? p->n
1bedf 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Var : 0;.}../*.*
1bee0 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 * Create a mappi
1bee1 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 ng from variable
1bee2 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 numbers to vari
1bee3 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e able names.** in
1bee4 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b the Vdbe.azVar[
1bee5 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 ] array, if such
1bee6 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 a mapping does
1bee7 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 not already.** e
1bee8 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 xist..*/.static
1bee9 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 void createVarMa
1beea 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 p(Vdbe *p){. if
1beeb 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 ( !p->okVar ){.
1beec 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 int j;. Op
1beed 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 *pOp;. sqlit
1beee 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1beef 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1bef0 20 20 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f /* The race co
1bef1 6e 64 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20 ndition here is
1bef2 68 61 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 harmless. If tw
1bef3 6f 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74 o threads call t
1bef4 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 his. ** routi
1bef5 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 ne on the same V
1bef6 64 62 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 dbe at the same
1bef7 74 69 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20 time, they both
1bef8 6d 69 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a might end. **
1bef9 20 75 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 up initializing
1befa 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b the Vdbe.azVar[
1befb 5d 20 61 72 72 61 79 2e 20 20 54 68 61 74 20 69 ] array. That i
1befc 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 s a little extra
1befd 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 . ** work but
1befe 20 69 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74 it results in t
1beff 68 65 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a he same answer..
1bf00 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a */. for(j
1bf01 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 =0, pOp=p->aOp;
1bf02 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 j<p->nOp; j++, p
1bf03 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 Op++){. if(
1bf04 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1bf05 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 _Variable ){.
1bf06 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1bf07 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p1>0 && pOp->p
1bf08 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 1<=p->nVar );.
1bf09 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 p->azVar[p
1bf0a 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d Op->p1-1] = pOp-
1bf0b 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 >p4.z;. }.
1bf0c 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 }. p->okVa
1bf0d 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 r = 1;. sqlit
1bf0e 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1bf0f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1bf10 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
1bf11 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 n the name of a
1bf12 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 wildcard paramet
1bf13 65 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c er. Return NULL
1bf14 20 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a if the index.**
1bf15 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
1bf16 20 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 or if the wildc
1bf17 61 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a ard is unnamed..
1bf18 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 **.** The result
1bf19 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 is always UTF-8
1bf1a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bf1b 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
1bf1c 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
1bf1d 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f er_name(sqlite3_
1bf1e 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bf1f 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d i){. Vdbe *p =
1bf20 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1bf21 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 if( p==0 || i<1
1bf22 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b || i>p->nVar ){
1bf23 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1bf24 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 }. createVarMa
1bf25 70 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 p(p);. return p
1bf26 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a ->azVar[i-1];.}.
1bf27 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 ./*.** Given a w
1bf28 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 ildcard paramete
1bf29 72 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 r name, return t
1bf2a 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
1bf2b 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 variable.** with
1bf2c 20 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 that name. If
1bf2d 74 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 there is no vari
1bf2e 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 able with the gi
1bf2f 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 ven name,.** ret
1bf30 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urn 0..*/.SQLITE
1bf31 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1bf32 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74 65 ite3VdbeParamete
1bf33 72 49 6e 64 65 78 28 56 64 62 65 20 2a 70 2c 20 rIndex(Vdbe *p,
1bf34 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1bf35 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 e, int nName){.
1bf36 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d int i;. if( p=
1bf37 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1bf38 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 0;. }. create
1bf39 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 VarMap(p); . if
1bf3a 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 ( zName ){. f
1bf3b 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 or(i=0; i<p->nVa
1bf3c 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 r; i++){. c
1bf3d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 onst char *z = p
1bf3e 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 ->azVar[i];.
1bf3f 20 20 69 66 28 20 7a 20 26 26 20 6d 65 6d 63 6d if( z && memcm
1bf40 70 28 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61 6d 65 29 p(z,zName,nName)
1bf41 3d 3d 30 20 26 26 20 7a 5b 6e 4e 61 6d 65 5d 3d ==0 && z[nName]=
1bf42 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
1bf43 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 turn i+1;.
1bf44 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1bf45 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 turn 0;.}.SQLITE
1bf46 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bf47 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
1bf48 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 index(sqlite3_st
1bf49 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 mt *pStmt, const
1bf4a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 char *zName){.
1bf4b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1bf4c 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e 64 65 dbeParameterInde
1bf4d 78 28 28 56 64 62 65 2a 29 70 53 74 6d 74 2c 20 x((Vdbe*)pStmt,
1bf4e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 zName, sqlite3St
1bf4f 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29 3b 0a rlen30(zName));.
1bf50 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 }../*.** Transfe
1bf51 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 66 r all bindings f
1bf52 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 74 rom the first st
1bf53 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f 20 atement over to
1bf54 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 the second..*/.S
1bf55 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1bf56 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 t sqlite3Transfe
1bf57 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 rBindings(sqlite
1bf58 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 3_stmt *pFromStm
1bf59 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 t, sqlite3_stmt
1bf5a 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62 *pToStmt){. Vdb
1bf5b 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65 e *pFrom = (Vdbe
1bf5c 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 *)pFromStmt;. V
1bf5d 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65 dbe *pTo = (Vdbe
1bf5e 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e 74 *)pToStmt;. int
1bf5f 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 i;. assert( pT
1bf60 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62 o->db==pFrom->db
1bf61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 );. assert( pT
1bf62 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e o->nVar==pFrom->
1bf63 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 nVar );. sqlite
1bf64 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 3_mutex_enter(pT
1bf65 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 o->db->mutex);.
1bf66 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72 6f for(i=0; i<pFro
1bf67 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 m->nVar; i++){.
1bf68 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1bf69 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72 mMove(&pTo->aVar
1bf6a 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61 [i], &pFrom->aVa
1bf6b 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c r[i]);. }. sql
1bf6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bf6d 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 (pTo->db->mutex)
1bf6e 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1bf6f 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 E_OK;.}..#ifndef
1bf70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 SQLITE_OMIT_DEP
1bf71 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 RECATED./*.** De
1bf72 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 precated externa
1bf73 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e l interface. In
1bf74 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 ternal/core SQLi
1bf75 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c te code.** shoul
1bf76 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 d call sqlite3Tr
1bf77 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a ansferBindings..
1bf78 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 75 **.** Is is misu
1bf79 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 se to call this
1bf7a 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 61 routine with sta
1bf7b 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 66 tements from dif
1bf7c 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 ferent.** databa
1bf7d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 se connections.
1bf7e 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 20 But as this is
1bf7f 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 a deprecated int
1bf80 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77 69 erface, we.** wi
1bf81 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f ll not bother to
1bf82 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 20 check for that
1bf83 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a condition..**.**
1bf84 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74 If the two stat
1bf85 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61 ements contain a
1bf86 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 different numbe
1bf87 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74 r of bindings, t
1bf88 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45 hen.** an SQLITE
1bf89 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e _ERROR is return
1bf8a 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c 73 ed. Nothing els
1bf8b 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c 20 e can go wrong,
1bf8c 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 so otherwise.**
1bf8d 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1bf8e 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
1bf8f 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bf90 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e _transfer_bindin
1bf91 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
1bf92 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 *pFromStmt, sqli
1bf93 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d te3_stmt *pToStm
1bf94 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f t){. Vdbe *pFro
1bf95 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d m = (Vdbe*)pFrom
1bf96 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 Stmt;. Vdbe *pT
1bf97 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 o = (Vdbe*)pToSt
1bf98 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d mt;. if( pFrom-
1bf99 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 >nVar!=pTo->nVar
1bf9a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1bf9b 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
1bf9c 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73 50 72 . if( pTo->isPr
1bf9d 65 70 61 72 65 56 32 20 26 26 20 70 54 6f 2d 3e epareV2 && pTo->
1bf9e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 expmask ){. p
1bf9f 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b To->expired = 1;
1bfa0 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 6f 6d . }. if( pFrom
1bfa1 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26 ->isPrepareV2 &&
1bfa2 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 6b 20 pFrom->expmask
1bfa3 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 65 78 ){. pFrom->ex
1bfa4 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 pired = 1;. }.
1bfa5 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 54 return sqlite3T
1bfa6 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 ransferBindings(
1bfa7 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 pFromStmt, pToSt
1bfa8 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f mt);.}.#endif../
1bfa9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1bfaa 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 sqlite3* databas
1bfab 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 e handle to whic
1bfac 68 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 h the prepared s
1bfad 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a tatement given.*
1bfae 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e * in the argumen
1bfaf 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 t belongs. This
1bfb0 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 is the same dat
1bfb1 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
1bfb2 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 t was.** the fir
1bfb3 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
1bfb4 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 he sqlite3_prepa
1bfb5 72 65 28 29 20 74 68 61 74 20 77 61 73 20 75 73 re() that was us
1bfb6 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 ed to create.**
1bfb7 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e the statement in
1bfb8 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 the first place
1bfb9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bfba 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 sqlite3 *sqlite3
1bfbb 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 _db_handle(sqlit
1bfbc 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1bfbd 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 . return pStmt
1bfbe 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 ? ((Vdbe*)pStmt)
1bfbf 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a ->db : 0;.}../*.
1bfc0 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
1bfc1 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ter to the next
1bfc2 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1bfc3 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 20 61 nt after pStmt a
1bfc4 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
1bfc5 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 h database conne
1bfc6 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 ction pDb. If p
1bfc7 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 Stmt is NULL, re
1bfc8 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 0a 2a turn the first.*
1bfc9 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 * prepared state
1bfca 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61 74 ment for the dat
1bfcb 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1bfcc 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
1bfcd 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e f there.** are n
1bfce 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 o more..*/.SQLIT
1bfcf 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 E_API sqlite3_st
1bfd0 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 mt *sqlite3_next
1bfd1 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 _stmt(sqlite3 *p
1bfd2 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 Db, sqlite3_stmt
1bfd3 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 *pStmt){. sqli
1bfd4 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b te3_stmt *pNext;
1bfd5 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1bfd6 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 _enter(pDb->mute
1bfd7 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d x);. if( pStmt=
1bfd8 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 =0 ){. pNext
1bfd9 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a = (sqlite3_stmt*
1bfda 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d )pDb->pVdbe;. }
1bfdb 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 else{. pNext
1bfdc 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a = (sqlite3_stmt*
1bfdd 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d )((Vdbe*)pStmt)-
1bfde 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 >pNext;. }. sq
1bfdf 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bfe0 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 e(pDb->mutex);.
1bfe1 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d return pNext;.}
1bfe2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1bfe3 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 he value of a st
1bfe4 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 atus counter for
1bfe5 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 74 a prepared stat
1bfe6 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f ement.*/.SQLITE_
1bfe7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bfe8 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 stmt_status(sqli
1bfe9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bfea 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 int op, int res
1bfeb 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 etFlag){. Vdbe
1bfec 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 2a 29 *pVdbe = (Vdbe*)
1bfed 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d pStmt;. int v =
1bfee 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 pVdbe->aCounter
1bfef 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 [op-1];. if( re
1bff0 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 65 2d setFlag ) pVdbe-
1bff1 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 >aCounter[op-1]
1bff2 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b = 0;. return v;
1bff3 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1bff4 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 *** End of vdbea
1bff5 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a pi.c ***********
1bff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bff8 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1bff9 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 *** Begin file v
1bffa 64 62 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a dbetrace.c *****
1bffb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bffc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bffd 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4e **/./*.** 2009 N
1bffe 6f 76 65 6d 62 65 72 20 32 35 0a 2a 2a 0a 2a 2a ovember 25.**.**
1bfff 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1c000 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1c001 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1c002 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1c003 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1c004 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1c005 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1c006 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1c007 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1c008 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1c009 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1c00a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1c00b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1c00c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1c00d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1c00e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1c00f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1c010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c013 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c014 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1c015 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1c016 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 ns code used to
1c017 69 6e 73 65 72 74 20 74 68 65 20 76 61 6c 75 65 insert the value
1c018 73 20 6f 66 20 68 6f 73 74 20 70 61 72 61 6d 65 s of host parame
1c019 74 65 72 73 0a 2a 2a 20 28 61 6b 61 20 22 77 69 ters.** (aka "wi
1c01a 6c 64 63 61 72 64 73 22 29 20 69 6e 74 6f 20 74 ldcards") into t
1c01b 68 65 20 53 51 4c 20 74 65 78 74 20 6f 75 74 70 he SQL text outp
1c01c 75 74 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72 ut by sqlite3_tr
1c01d 61 63 65 28 29 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 ace()..*/..#ifnd
1c01e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1c01f 52 41 43 45 0a 0a 2f 2a 0a 2a 2a 20 7a 53 71 6c RACE../*.** zSql
1c020 20 69 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 is a zero-termi
1c021 6e 61 74 65 64 20 73 74 72 69 6e 67 20 6f 66 20 nated string of
1c022 55 54 46 2d 38 20 53 51 4c 20 74 65 78 74 2e 20 UTF-8 SQL text.
1c023 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1c024 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 er of.** bytes i
1c025 6e 20 74 68 69 73 20 74 65 78 74 20 75 70 20 74 n this text up t
1c026 6f 20 62 75 74 20 65 78 63 6c 75 64 69 6e 67 20 o but excluding
1c027 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 the first charac
1c028 74 65 72 20 69 6e 0a 2a 2a 20 61 20 68 6f 73 74 ter in.** a host
1c029 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20 parameter. If
1c02a 74 68 65 20 74 65 78 74 20 63 6f 6e 74 61 69 6e the text contain
1c02b 73 20 6e 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 s no host parame
1c02c 74 65 72 73 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 ters, return.**
1c02d 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
1c02e 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
1c02f 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 text..*/.static
1c030 20 69 6e 74 20 66 69 6e 64 4e 65 78 74 48 6f 73 int findNextHos
1c031 74 50 61 72 61 6d 65 74 65 72 28 63 6f 6e 73 74 tParameter(const
1c032 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 69 6e 74 char *zSql, int
1c033 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e *pnToken){. in
1c034 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 69 t tokenType;. i
1c035 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 nt nTotal = 0;.
1c036 20 69 6e 74 20 6e 3b 0a 0a 20 20 2a 70 6e 54 6f int n;.. *pnTo
1c037 6b 65 6e 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 ken = 0;. while
1c038 28 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 ( zSql[0] ){.
1c039 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 n = sqlite3GetT
1c03a 6f 6b 65 6e 28 28 75 38 2a 29 7a 53 71 6c 2c 20 oken((u8*)zSql,
1c03b 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 &tokenType);.
1c03c 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 assert( n>0 &&
1c03d 74 6f 6b 65 6e 54 79 70 65 21 3d 54 4b 5f 49 4c tokenType!=TK_IL
1c03e 4c 45 47 41 4c 20 29 3b 0a 20 20 20 20 69 66 28 LEGAL );. if(
1c03f 20 74 6f 6b 65 6e 54 79 70 65 3d 3d 54 4b 5f 56 tokenType==TK_V
1c040 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 20 ARIABLE ){.
1c041 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e 3b 0a 20 *pnToken = n;.
1c042 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1c043 7d 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20 }. nTotal +=
1c044 6e 3b 0a 20 20 20 20 7a 53 71 6c 20 2b 3d 20 6e n;. zSql += n
1c045 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
1c046 54 6f 74 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Total;.}../*.**
1c047 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1c048 20 74 6f 20 61 20 73 74 72 69 6e 67 20 69 6e 20 to a string in
1c049 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
1c04a 66 6f 72 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 form sqlite3DbMa
1c04b 6c 6c 6f 63 28 29 20 77 68 69 63 68 0a 2a 2a 20 lloc() which.**
1c04c 68 6f 6c 64 73 20 61 20 63 6f 70 79 20 6f 66 20 holds a copy of
1c04d 7a 52 61 77 53 71 6c 20 62 75 74 20 77 69 74 68 zRawSql but with
1c04e 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 host parameters
1c04f 20 65 78 70 61 6e 64 65 64 20 74 6f 20 74 68 65 expanded to the
1c050 69 72 0a 2a 2a 20 63 75 72 72 65 6e 74 20 62 69 ir.** current bi
1c051 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ndings..**.** Th
1c052 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
1c053 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c on is responsibl
1c054 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 e for making sur
1c055 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 e the memory ret
1c056 75 72 6e 65 64 0a 2a 2a 20 69 73 20 65 76 65 6e urned.** is even
1c057 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a tually freed..**
1c058 0a 2a 2a 20 41 4c 47 4f 52 49 54 48 4d 3a 20 20 .** ALGORITHM:
1c059 53 63 61 6e 20 74 68 65 20 69 6e 70 75 74 20 73 Scan the input s
1c05a 74 72 69 6e 67 20 6c 6f 6f 6b 69 6e 67 20 66 6f tring looking fo
1c05b 72 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 r host parameter
1c05c 73 20 69 6e 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 s in any of.** t
1c05d 68 65 73 65 20 66 6f 72 6d 73 3a 20 20 3f 2c 20 hese forms: ?,
1c05e 3f 4e 2c 20 24 41 2c 20 40 41 2c 20 3a 41 2e 20 ?N, $A, @A, :A.
1c05f 20 54 61 6b 65 20 63 61 72 65 20 74 6f 20 61 76 Take care to av
1c060 6f 69 64 20 74 65 78 74 20 77 69 74 68 69 6e 0a oid text within.
1c061 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 ** string litera
1c062 6c 73 2c 20 71 75 6f 74 65 64 20 69 64 65 6e 74 ls, quoted ident
1c063 69 66 69 65 72 20 6e 61 6d 65 73 2c 20 61 6e 64 ifier names, and
1c064 20 63 6f 6d 6d 65 6e 74 73 2e 20 20 46 6f 72 20 comments. For
1c065 74 65 78 74 20 66 6f 72 6d 73 2c 0a 2a 2a 20 74 text forms,.** t
1c066 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 he host paramete
1c067 72 20 69 6e 64 65 78 20 69 73 20 66 6f 75 6e 64 r index is found
1c068 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 by scanning the
1c069 20 70 65 72 70 61 72 65 64 0a 2a 2a 20 73 74 61 perpared.** sta
1c06a 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 63 tement for the c
1c06b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4f 50 5f orresponding OP_
1c06c 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e Variable opcode.
1c06d 20 20 4f 6e 63 65 20 74 68 65 20 68 6f 73 74 0a Once the host.
1c06e 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 ** parameter ind
1c06f 65 78 20 69 73 20 6b 6e 6f 77 6e 2c 20 6c 6f 63 ex is known, loc
1c070 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e ate the value in
1c071 20 70 2d 3e 61 56 61 72 5b 5d 2e 20 20 54 68 65 p->aVar[]. The
1c072 6e 20 72 65 6e 64 65 72 0a 2a 2a 20 74 68 65 20 n render.** the
1c073 76 61 6c 75 65 20 61 73 20 61 20 6c 69 74 65 72 value as a liter
1c074 61 6c 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 al in place of t
1c075 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 he host paramete
1c076 72 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 r name..*/.SQLIT
1c077 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
1c078 73 71 6c 69 74 65 33 56 64 62 65 45 78 70 61 6e sqlite3VdbeExpan
1c079 64 53 71 6c 28 0a 20 20 56 64 62 65 20 2a 70 2c dSql(. Vdbe *p,
1c07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c07b 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 /* The prepared
1c07c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 statement being
1c07d 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 evaluated */.
1c07e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 61 77 const char *zRaw
1c07f 53 71 6c 20 20 20 20 20 20 2f 2a 20 52 61 77 20 Sql /* Raw
1c080 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 text of the SQL
1c081 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a statement */.){.
1c082 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
1c083 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1c084 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1c085 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 ction */. int i
1c086 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 dx = 0;
1c087 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1c088 61 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 a host parameter
1c089 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 49 6e */. int nextIn
1c08a 64 65 78 20 3d 20 31 3b 20 20 20 20 20 20 20 2f dex = 1; /
1c08b 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 * Index of next
1c08c 3f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 ? host parameter
1c08d 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 */. int n;
1c08e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c08f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 74 6f * Length of a to
1c090 6b 65 6e 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 ken prefix */.
1c091 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 int nToken;
1c092 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
1c093 74 68 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 th of the parame
1c094 74 65 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 ter token */. i
1c095 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1c096 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1c097 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4d 65 6d counter */. Mem
1c098 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 20 20 *pVar;
1c099 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
1c09a 66 20 61 20 68 6f 73 74 20 70 61 72 61 6d 65 74 f a host paramet
1c09b 65 72 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d er */. StrAccum
1c09c 20 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 out;
1c09d 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 /* Accumulate t
1c09e 68 65 20 6f 75 74 70 75 74 20 68 65 72 65 20 2a he output here *
1c09f 2f 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 31 /. char zBase[1
1c0a0 30 30 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 00]; /*
1c0a1 49 6e 69 74 69 61 6c 20 77 6f 72 6b 69 6e 67 20 Initial working
1c0a2 73 70 61 63 65 20 2a 2f 0a 0a 20 20 64 62 20 3d space */.. db =
1c0a3 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 p->db;. sqlite
1c0a4 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 6f 3StrAccumInit(&o
1c0a5 75 74 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f ut, zBase, sizeo
1c0a6 66 28 7a 42 61 73 65 29 2c 20 0a 20 20 20 20 20 f(zBase), .
1c0a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0a8 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 db->aLimit[SQLI
1c0a9 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1c0aa 29 3b 0a 20 20 6f 75 74 2e 64 62 20 3d 20 64 62 );. out.db = db
1c0ab 3b 0a 20 20 77 68 69 6c 65 28 20 7a 52 61 77 53 ;. while( zRawS
1c0ac 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 20 3d ql[0] ){. n =
1c0ad 20 66 69 6e 64 4e 65 78 74 48 6f 73 74 50 61 72 findNextHostPar
1c0ae 61 6d 65 74 65 72 28 7a 52 61 77 53 71 6c 2c 20 ameter(zRawSql,
1c0af 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 61 73 &nToken);. as
1c0b0 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 sert( n>0 );.
1c0b1 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
1c0b2 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 7a 52 61 Append(&out, zRa
1c0b3 77 53 71 6c 2c 20 6e 29 3b 0a 20 20 20 20 7a 52 wSql, n);. zR
1c0b4 61 77 53 71 6c 20 2b 3d 20 6e 3b 0a 20 20 20 20 awSql += n;.
1c0b5 61 73 73 65 72 74 28 20 7a 52 61 77 53 71 6c 5b assert( zRawSql[
1c0b6 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e 3d 3d 30 20 0] || nToken==0
1c0b7 29 3b 0a 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 );. if( nToke
1c0b8 6e 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 n==0 ) break;.
1c0b9 20 20 69 66 28 20 7a 52 61 77 53 71 6c 5b 30 5d if( zRawSql[0]
1c0ba 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 20 20 69 =='?' ){. i
1c0bb 66 28 20 6e 54 6f 6b 65 6e 3e 31 20 29 7b 0a 20 f( nToken>1 ){.
1c0bc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 assert( s
1c0bd 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 52 qlite3Isdigit(zR
1c0be 61 77 53 71 6c 5b 31 5d 29 20 29 3b 0a 20 20 20 awSql[1]) );.
1c0bf 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 49 sqlite3GetI
1c0c0 6e 74 33 32 28 26 7a 52 61 77 53 71 6c 5b 31 5d nt32(&zRawSql[1]
1c0c1 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 20 20 7d , &idx);. }
1c0c2 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 64 else{. id
1c0c3 78 20 3d 20 6e 65 78 74 49 6e 64 65 78 3b 0a 20 x = nextIndex;.
1c0c4 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
1c0c5 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1c0c6 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 3a 27 20 zRawSql[0]==':'
1c0c7 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 || zRawSql[0]=='
1c0c8 24 27 20 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d $' || zRawSql[0]
1c0c9 3d 3d 27 40 27 20 29 3b 0a 20 20 20 20 20 20 74 =='@' );. t
1c0ca 65 73 74 63 61 73 65 28 20 7a 52 61 77 53 71 6c estcase( zRawSql
1c0cb 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20 20 [0]==':' );.
1c0cc 20 20 74 65 73 74 63 61 73 65 28 20 7a 52 61 77 testcase( zRaw
1c0cd 53 71 6c 5b 30 5d 3d 3d 27 24 27 20 29 3b 0a 20 Sql[0]=='$' );.
1c0ce 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a testcase( z
1c0cf 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 40 27 20 29 RawSql[0]=='@' )
1c0d0 3b 0a 20 20 20 20 20 20 69 64 78 20 3d 20 73 71 ;. idx = sq
1c0d1 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74 lite3VdbeParamet
1c0d2 65 72 49 6e 64 65 78 28 70 2c 20 7a 52 61 77 53 erIndex(p, zRawS
1c0d3 71 6c 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 ql, nToken);.
1c0d4 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30 assert( idx>0
1c0d5 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 );. }. zR
1c0d6 61 77 53 71 6c 20 2b 3d 20 6e 54 6f 6b 65 6e 3b awSql += nToken;
1c0d7 0a 20 20 20 20 6e 65 78 74 49 6e 64 65 78 20 3d . nextIndex =
1c0d8 20 69 64 78 20 2b 20 31 3b 0a 20 20 20 20 61 73 idx + 1;. as
1c0d9 73 65 72 74 28 20 69 64 78 3e 30 20 26 26 20 69 sert( idx>0 && i
1c0da 64 78 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 dx<=p->nVar );.
1c0db 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 pVar = &p->aV
1c0dc 61 72 5b 69 64 78 2d 31 5d 3b 0a 20 20 20 20 69 ar[idx-1];. i
1c0dd 66 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 f( pVar->flags &
1c0de 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1c0df 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
1c0e0 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22 umAppend(&out, "
1c0e1 4e 55 4c 4c 22 2c 20 34 29 3b 0a 20 20 20 20 7d NULL", 4);. }
1c0e2 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66 else if( pVar->f
1c0e3 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 lags & MEM_Int )
1c0e4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 58 {. sqlite3X
1c0e5 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 6c Printf(&out, "%l
1c0e6 6c 64 22 2c 20 70 56 61 72 2d 3e 75 2e 69 29 3b ld", pVar->u.i);
1c0e7 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1c0e8 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Var->flags & MEM
1c0e9 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 _Real ){. s
1c0ea 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 26 6f qlite3XPrintf(&o
1c0eb 75 74 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 56 ut, "%!.15g", pV
1c0ec 61 72 2d 3e 72 29 3b 0a 20 20 20 20 7d 65 6c 73 ar->r);. }els
1c0ed 65 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61 67 e if( pVar->flag
1c0ee 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 23 s & MEM_Str ){.#
1c0ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1c0f0 49 54 5f 55 54 46 31 36 0a 20 20 20 20 20 20 75 IT_UTF16. u
1c0f1 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 8 enc = ENC(db);
1c0f2 0a 20 20 20 20 20 20 69 66 28 20 65 6e 63 21 3d . if( enc!=
1c0f3 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
1c0f4 20 20 20 20 20 20 20 4d 65 6d 20 75 74 66 38 3b Mem utf8;
1c0f5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
1c0f6 26 75 74 66 38 2c 20 30 2c 20 73 69 7a 65 6f 66 &utf8, 0, sizeof
1c0f7 28 75 74 66 38 29 29 3b 0a 20 20 20 20 20 20 20 (utf8));.
1c0f8 20 75 74 66 38 2e 64 62 20 3d 20 64 62 3b 0a 20 utf8.db = db;.
1c0f9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1c0fa 62 65 4d 65 6d 53 65 74 53 74 72 28 26 75 74 66 beMemSetStr(&utf
1c0fb 38 2c 20 70 56 61 72 2d 3e 7a 2c 20 70 56 61 72 8, pVar->z, pVar
1c0fc 2d 3e 6e 2c 20 65 6e 63 2c 20 53 51 4c 49 54 45 ->n, enc, SQLITE
1c0fd 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 _STATIC);.
1c0fe 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1c0ff 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 74 66 ngeEncoding(&utf
1c100 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 8, SQLITE_UTF8);
1c101 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c102 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 27 XPrintf(&out, "'
1c103 25 2e 2a 71 27 22 2c 20 75 74 66 38 2e 6e 2c 20 %.*q'", utf8.n,
1c104 75 74 66 38 2e 7a 29 3b 0a 20 20 20 20 20 20 20 utf8.z);.
1c105 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1c106 65 6c 65 61 73 65 28 26 75 74 66 38 29 3b 0a 20 elease(&utf8);.
1c107 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 }else.#endi
1c108 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 f. {.
1c109 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 sqlite3XPrintf
1c10a 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 27 22 2c (&out, "'%.*q'",
1c10b 20 70 56 61 72 2d 3e 6e 2c 20 70 56 61 72 2d 3e pVar->n, pVar->
1c10c 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 z);. }.
1c10d 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e }else if( pVar->
1c10e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1c10f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c110 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 3XPrintf(&out, "
1c111 7a 65 72 6f 62 6c 6f 62 28 25 64 29 22 2c 20 70 zeroblob(%d)", p
1c112 56 61 72 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 Var->u.nZero);.
1c113 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1c114 61 73 73 65 72 74 28 20 70 56 61 72 2d 3e 66 6c assert( pVar->fl
1c115 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 ags & MEM_Blob )
1c116 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 ;. sqlite3S
1c117 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f trAccumAppend(&o
1c118 75 74 2c 20 22 78 27 22 2c 20 32 29 3b 0a 20 20 ut, "x'", 2);.
1c119 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1c11a 56 61 72 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 Var->n; i++){.
1c11b 20 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 sqlite3XPr
1c11c 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 30 32 78 intf(&out, "%02x
1c11d 22 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d 26 30 78 ", pVar->z[i]&0x
1c11e 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ff);. }.
1c11f 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
1c120 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22 umAppend(&out, "
1c121 27 22 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 '", 1);. }.
1c122 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
1c123 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
1c124 28 26 6f 75 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 (&out);.}..#endi
1c125 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1c126 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a ITE_OMIT_TRACE *
1c127 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
1c128 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 74 72 ** End of vdbetr
1c129 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ace.c **********
1c12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c12b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c12c 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1c12d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
1c12e 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.c ***********
1c12f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c131 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
1c132 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
1c133 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1c134 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1c135 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1c136 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1c137 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1c138 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1c139 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1c13a 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1c13b 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1c13c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1c13d 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1c13e 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1c13f 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1c140 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1c141 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1c142 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1c143 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1c144 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c145 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c146 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c147 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c148 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
1c149 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 code in this fi
1c14a 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 le implements ex
1c14b 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f ecution method o
1c14c 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 f the .** Virtua
1c14d 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e l Database Engin
1c14e 65 20 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 e (VDBE). A sep
1c14f 61 72 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 arate file ("vdb
1c150 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 eaux.c").** hand
1c151 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 les housekeeping
1c152 20 64 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 details such as
1c153 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 creating and de
1c154 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 leting.** VDBE i
1c155 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 nstances. This
1c156 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 file is solely i
1c157 6e 74 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 nterested in exe
1c158 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 cuting.** the VD
1c159 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a BE program..**.*
1c15a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 * In the externa
1c15b 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 l interface, an
1c15c 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 "sqlite3_stmt*"
1c15d 69 73 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 is an opaque poi
1c15e 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 nter.** to a VDB
1c15f 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c E..**.** The SQL
1c160 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 parser generate
1c161 73 20 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 s a program whic
1c162 68 20 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 h is then execut
1c163 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 ed by.** the VDB
1c164 45 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b E to do the work
1c165 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 of the SQL stat
1c166 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f ement. VDBE pro
1c167 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 grams are .** si
1c168 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f milar in form to
1c169 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 assembly langua
1c16a 67 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d ge. The program
1c16b 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 consists of.**
1c16c 61 20 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 a linear sequenc
1c16d 65 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e e of operations.
1c16e 20 20 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e Each operation
1c16f 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a has an opcode .
1c170 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 ** and 5 operand
1c171 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c s. Operands P1,
1c172 20 50 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 P2, and P3 are
1c173 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 integers. Opera
1c174 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e nd P4 .** is a n
1c175 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 ull-terminated s
1c176 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 tring. Operand
1c177 50 35 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 P5 is an unsigne
1c178 64 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 d character..**
1c179 46 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 Few opcodes use
1c17a 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a all 5 operands..
1c17b 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f **.** Computatio
1c17c 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 n results are st
1c17d 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 ored on a set of
1c17e 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 registers numbe
1c17f 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a red beginning.**
1c180 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e with 1 and goin
1c181 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 g up to Vdbe.nMe
1c182 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73 74 65 m. Each registe
1c183 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 r can store.** e
1c184 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 ither an integer
1c185 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 , a null-termina
1c186 74 65 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c ted string, a fl
1c187 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 oating point.**
1c188 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 number, or the S
1c189 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e QL "NULL" value.
1c18a 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f An implicit co
1c18b 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e nversion from on
1c18c 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 e.** type to the
1c18d 20 6f 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 other occurs as
1c18e 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a necessary..** .
1c18f 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 ** Most of the c
1c190 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ode in this file
1c191 20 69 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 is taken up by
1c192 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 the sqlite3VdbeE
1c193 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f xec().** functio
1c194 6e 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 n which does the
1c195 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 work of interpr
1c196 65 74 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f eting a VDBE pro
1c197 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 gram..** But oth
1c198 65 72 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 er routines are
1c199 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f also provided to
1c19a 20 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e help in buildin
1c19b 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 g up.** a progra
1c19c 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 m instruction by
1c19d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
1c19e 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 .** Various scri
1c19f 70 74 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f pts scan this so
1c1a0 75 72 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 urce file in ord
1c1a1 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 er to generate H
1c1a2 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 TML.** documenta
1c1a3 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 tion, headers fi
1c1a4 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 les, or other de
1c1a5 72 69 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 rived files. Th
1c1a6 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 e formatting.**
1c1a7 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 of the code in t
1c1a8 68 69 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 his file is, the
1c1a9 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e refore, importan
1c1aa 74 2e 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f t. See other co
1c1ab 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 mments.** in thi
1c1ac 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 s file for detai
1c1ad 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 ls. If in doubt
1c1ae 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 , do not deviate
1c1af 20 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a from existing.*
1c1b0 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 * commenting and
1c1b1 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 indentation pra
1c1b2 63 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e ctices when chan
1c1b3 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 ging or adding c
1c1b4 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ode..*/../*.** T
1c1b5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f he following glo
1c1b6 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
1c1b7 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 76 65 72 incremented ever
1c1b8 79 20 74 69 6d 65 20 61 20 63 75 72 73 6f 72 0a y time a cursor.
1c1b9 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 68 65 72 ** moves, either
1c1ba 20 62 79 20 74 68 65 20 4f 50 5f 53 65 65 6b 58 by the OP_SeekX
1c1bb 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f X, OP_Next, or O
1c1bc 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 73 2e 20 P_Prev opcodes.
1c1bd 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f The test.** pro
1c1be 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69 73 cedures use this
1c1bf 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
1c1c0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 69 make sure that i
1c1c1 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f ndices are.** wo
1c1c2 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e rking correctly.
1c1c3 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 This variable
1c1c4 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 has no function
1c1c5 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a other than to.**
1c1c6 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 help verify the
1c1c7 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 correct operati
1c1c8 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 on of the librar
1c1c9 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c y..*/.#ifdef SQL
1c1ca 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
1c1cb 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1c1cc 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30 search_count = 0
1c1cd 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
1c1ce 57 68 65 6e 20 74 68 69 73 20 67 6c 6f 62 61 6c When this global
1c1cf 20 76 61 72 69 61 62 6c 65 20 69 73 20 70 6f 73 variable is pos
1c1d0 69 74 69 76 65 2c 20 69 74 20 67 65 74 73 20 64 itive, it gets d
1c1d1 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e 63 65 20 ecremented once
1c1d2 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 68 20 69 before.** each i
1c1d3 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 nstruction in th
1c1d4 65 20 56 44 42 45 2e 20 20 57 68 65 6e 20 72 65 e VDBE. When re
1c1d5 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 68 65 20 aches zero, the
1c1d6 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
1c1d7 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
1c1d8 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 74 75 sqlite3 structu
1c1d9 72 65 20 69 73 20 73 65 74 20 69 6e 20 6f 72 64 re is set in ord
1c1da 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 er to simulate a
1c1db 6e 64 20 69 6e 74 65 72 72 75 70 74 2e 0a 2a 2a nd interrupt..**
1c1dc 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 6c 69 74 .** This facilit
1c1dd 79 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 y is used for te
1c1de 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f sting purposes o
1c1df 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f nly. It does no
1c1e0 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e t function.** in
1c1e1 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62 75 69 an ordinary bui
1c1e2 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ld..*/.#ifdef SQ
1c1e3 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
1c1e4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1c1e5 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c1e6 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
1c1e7 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f .** The next glo
1c1e8 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
1c1e9 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 incremented each
1c1ea 20 74 79 70 65 20 74 68 65 20 4f 50 5f 53 6f 72 type the OP_Sor
1c1eb 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 t opcode.** is e
1c1ec 78 65 63 75 74 65 64 2e 20 20 54 68 65 20 74 65 xecuted. The te
1c1ed 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 st procedures us
1c1ee 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 e this informati
1c1ef 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 on to make sure
1c1f0 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 that.** sorting
1c1f1 69 73 20 6f 63 63 75 72 72 69 6e 67 20 6f 72 20 is occurring or
1c1f2 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 20 61 74 not occurring at
1c1f3 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 69 6d appropriate tim
1c1f4 65 73 2e 20 20 20 54 68 69 73 20 76 61 72 69 61 es. This varia
1c1f5 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 ble.** has no fu
1c1f6 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 nction other tha
1c1f7 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 66 79 n to help verify
1c1f8 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
1c1f9 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a ration of the.**
1c1fa 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 library..*/.#if
1c1fb 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1c1fc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1c1fd 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e qlite3_sort_coun
1c1fe 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
1c1ff 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c *.** The next gl
1c200 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 72 65 obal variable re
1c201 63 6f 72 64 73 20 74 68 65 20 73 69 7a 65 20 6f cords the size o
1c202 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 4d 45 f the largest ME
1c203 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 4d 45 4d M_Blob.** or MEM
1c204 5f 53 74 72 20 74 68 61 74 20 68 61 73 20 62 65 _Str that has be
1c205 65 6e 20 75 73 65 64 20 62 79 20 61 20 56 44 42 en used by a VDB
1c206 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74 E opcode. The t
1c207 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 0a 2a est procedures.*
1c208 2a 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 * use this infor
1c209 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 mation to make s
1c20a 75 72 65 20 74 68 61 74 20 74 68 65 20 7a 65 72 ure that the zer
1c20b 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 6f 6e 61 o-blob functiona
1c20c 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f 72 6b 69 lity.** is worki
1c20d 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 20 ng correctly.
1c20e 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 This variable ha
1c20f 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 s no function ot
1c210 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 her than to.** h
1c211 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 elp verify the c
1c212 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e orrect operation
1c213 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e of the library.
1c214 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1c215 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
1c216 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61 I int sqlite3_ma
1c217 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 30 3b 0a x_blobsize = 0;.
1c218 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 static void upda
1c219 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 4d 65 teMaxBlobsize(Me
1c21a 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d m *p){. if( (p-
1c21b 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 >flags & (MEM_St
1c21c 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 3d 30 20 r|MEM_Blob))!=0
1c21d 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 65 33 5f && p->n>sqlite3_
1c21e 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 29 7b 0a max_blobsize ){.
1c21f 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f sqlite3_max_
1c220 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d 3e 6e 3b blobsize = p->n;
1c221 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f . }.}.#endif../
1c222 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c *.** The next gl
1c223 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 obal variable is
1c224 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 incremented eac
1c225 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f 46 6f h type the OP_Fo
1c226 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 und opcode.** is
1c227 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 73 20 executed. This
1c228 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 is used to test
1c229 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
1c22a 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a he foreign key.*
1c22b 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c * operation impl
1c22c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 4f 50 emented using OP
1c22d 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 77 6f 72 _FkIsZero is wor
1c22e 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 72 69 61 king. This varia
1c22f 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 ble.** has no fu
1c230 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 nction other tha
1c231 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 66 79 n to help verify
1c232 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 the correct ope
1c233 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a ration of the.**
1c234 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 library..*/.#if
1c235 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1c236 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1c237 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 qlite3_found_cou
1c238 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
1c239 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72 65 67 /*.** Test a reg
1c23a 69 73 74 65 72 20 74 6f 20 73 65 65 20 69 66 20 ister to see if
1c23b 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 63 it exceeds the c
1c23c 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 62 urrent maximum b
1c23d 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 lob size..** If
1c23e 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72 64 20 it does, record
1c23f 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75 6d 20 the new maximum
1c240 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a 23 69 blob size..*/.#i
1c241 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
1c242 5f 54 45 53 54 29 20 26 26 20 21 64 65 66 69 6e _TEST) && !defin
1c243 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ed(SQLITE_OMIT_B
1c244 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23 20 64 UILTIN_TEST).# d
1c245 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 efine UPDATE_MAX
1c246 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20 75 70 _BLOBSIZE(P) up
1c247 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 dateMaxBlobsize(
1c248 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e P).#else.# defin
1c249 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f e UPDATE_MAX_BLO
1c24a 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69 66 0a BSIZE(P).#endif.
1c24b 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 ./*.** Convert t
1c24c 68 65 20 67 69 76 65 6e 20 72 65 67 69 73 74 65 he given registe
1c24d 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 r into a string
1c24e 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e 65 0a if it isn't one.
1c24f 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65 74 75 ** already. Retu
1c250 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 rn non-zero if a
1c251 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e malloc() fails.
1c252 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74 72 69 .*/.#define Stri
1c253 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20 5c 0a ngify(P, enc) \.
1c254 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c 61 67 if(((P)->flag
1c255 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 s&(MEM_Str|MEM_B
1c256 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71 6c 69 lob))==0 && sqli
1c257 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
1c258 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a 20 20 ify(P,enc)) \.
1c259 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d { goto no_mem
1c25a 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70 ; }../*.** An ep
1c25b 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 76 hemeral string v
1c25c 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65 64 20 alue (signified
1c25d 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d by the MEM_Ephem
1c25e 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a flag) contains.
1c25f 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
1c260 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c a dynamically al
1c261 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 20 77 located string w
1c262 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 here some other
1c263 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72 65 73 entity.** is res
1c264 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 ponsible for dea
1c265 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74 20 73 llocating that s
1c266 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73 65 20 tring. Because
1c267 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 the register.**
1c268 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 6f 6c does not control
1c269 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69 74 20 the string, it
1c26a 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74 65 64 might be deleted
1c26b 20 77 69 74 68 6f 75 74 20 74 68 65 20 72 65 67 without the reg
1c26c 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69 6e 67 ister.** knowing
1c26d 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 it..**.** This
1c26e 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72 74 73 routine converts
1c26f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 an ephemeral st
1c270 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79 6e 61 ring into a dyna
1c271 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
1c272 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68 61 74 d.** string that
1c273 20 74 68 65 20 72 65 67 69 73 74 65 72 20 69 74 the register it
1c274 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e 20 20 self controls.
1c275 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1c276 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73 20 61 it.** converts a
1c277 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74 72 69 n MEM_Ephem stri
1c278 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d 5f 44 ng into an MEM_D
1c279 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 23 64 yn string..*/.#d
1c27a 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65 72 61 efine Deephemera
1c27b 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69 66 28 lize(P) \. if(
1c27c 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d ((P)->flags&MEM
1c27d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20 20 20 _Ephem)!=0 \.
1c27e 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 56 64 && sqlite3Vd
1c27f 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1c280 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20 6e 6f le(P) ){ goto no
1c281 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 _mem;}../*.** Ca
1c282 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 ll sqlite3VdbeMe
1c283 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e mExpandBlob() on
1c284 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 76 61 the supplied va
1c285 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a lue (type Mem*).
1c286 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 65 64 ** P if required
1c287 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 ..*/.#define Exp
1c288 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 50 29 andBlob(P) (((P)
1c289 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f ->flags&MEM_Zero
1c28a 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d )?sqlite3VdbeMem
1c28b 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 ExpandBlob(P):0)
1c28c 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 ../*.** Argument
1c28d 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61 74 20 pMem points at
1c28e 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 a register that
1c28f 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 74 will be passed t
1c290 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69 o a.** user-defi
1c291 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 ned function or
1c292 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1c293 75 73 65 72 20 61 73 20 74 68 65 20 72 65 73 75 user as the resu
1c294 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e 0a 2a lt of a query..*
1c295 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
1c296 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e 74 79 ets the pMem->ty
1c297 70 65 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 pe variable used
1c298 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f by the sqlite3_
1c299 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a 20 72 6f value_*() .** ro
1c29a 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 utines..*/.SQLIT
1c29b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1c29c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f qlite3VdbeMemSto
1c29d 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 6d reType(Mem *pMem
1c29e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d ){. int flags =
1c29f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem->flags;.
1c2a0 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f if( flags & MEM_
1c2a1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d Null ){. pMem
1c2a2 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1c2a3 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 NULL;. }. else
1c2a4 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
1c2a5 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d _Int ){. pMem
1c2a6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1c2a7 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 INTEGER;. }. e
1c2a8 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
1c2a9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
1c2aa 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1c2ab 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 ITE_FLOAT;. }.
1c2ac 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 else if( flags
1c2ad 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 & MEM_Str ){.
1c2ae 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1c2af 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c LITE_TEXT;. }el
1c2b0 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 se{. pMem->ty
1c2b1 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 pe = SQLITE_BLOB
1c2b2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
1c2b3 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72 73 llocate VdbeCurs
1c2b4 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20 or number iCur.
1c2b5 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
1c2b6 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e r to it. Return
1c2b7 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72 NULL.** if we r
1c2b8 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 un out of memory
1c2b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 65 ..*/.static Vdbe
1c2ba 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 65 Cursor *allocate
1c2bb 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20 2a Cursor(. Vdbe *
1c2bc 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
1c2bd 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d /* The virtual m
1c2be 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 achine */. int
1c2bf 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 iCur,
1c2c0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1c2c1 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f 72 e new VdbeCursor
1c2c2 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
1c2c3 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
1c2c4 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 umber of fields
1c2c5 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 in the table or
1c2c6 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 index */. int i
1c2c7 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 Db,
1c2c8 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 62 61 73 /* When databas
1c2c9 65 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 6c e the cursor bel
1c2ca 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 2a ongs to, or -1 *
1c2cb 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 43 /. int isBtreeC
1c2cc 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 54 72 75 ursor /* Tru
1c2cd 65 20 66 6f 72 20 42 2d 54 72 65 65 2e 20 20 46 e for B-Tree. F
1c2ce 61 6c 73 65 20 66 6f 72 20 70 73 65 75 64 6f 2d alse for pseudo-
1c2cf 74 61 62 6c 65 20 6f 72 20 76 74 61 62 20 2a 2f table or vtab */
1c2d0 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 .){. /* Find th
1c2d1 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 e memory cell th
1c2d2 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 at will be used
1c2d3 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f to store the blo
1c2d4 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a b of memory. **
1c2d5 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 required for th
1c2d6 69 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 is VdbeCursor st
1c2d7 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 ructure. It is c
1c2d8 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 onvenient to use
1c2d9 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 a . ** vdbe me
1c2da 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e mory cell to man
1c2db 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 age the memory a
1c2dc 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 llocation requir
1c2dd 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 ed for a. ** Vd
1c2de 62 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 beCursor structu
1c2df 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f re for the follo
1c2e0 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 wing reasons:.
1c2e1 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 **. ** * Some
1c2e2 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d times cursor num
1c2e3 62 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f bers are used fo
1c2e4 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 r a couple of di
1c2e5 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 fferent. **
1c2e6 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 purposes in a v
1c2e7 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 dbe program. The
1c2e8 20 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 different uses
1c2e9 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 might require.
1c2ea 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 ** different
1c2eb 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f sized allocatio
1c2ec 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 ns. Memory cells
1c2ed 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c provide growabl
1c2ee 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 e. ** alloc
1c2ef 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a ations.. **. *
1c2f0 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 * * When using
1c2f1 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d ENABLE_MEMORY_M
1c2f2 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 ANAGEMENT, memor
1c2f3 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 y cell buffers c
1c2f4 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 an. ** be f
1c2f5 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 reed lazily via
1c2f6 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 the sqlite3_rele
1c2f7 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 ase_memory() API
1c2f8 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 . This. **
1c2f9 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 minimizes the nu
1c2fa 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 mber of malloc c
1c2fb 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 alls made by the
1c2fc 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 system.. **.
1c2fd 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 ** Memory cells
1c2fe 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 for cursors are
1c2ff 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 allocated at the
1c300 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 top of the addr
1c301 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 ess. ** space.
1c302 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e Memory cell (p->
1c303 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 nMem) correspond
1c304 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 s to cursor 0. S
1c305 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 pace for. ** cu
1c306 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 rsor 1 is manage
1c307 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c d by memory cell
1c308 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 (p->nMem-1), et
1c309 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 c.. */. Mem *p
1c30a 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1c30b 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 ->nMem-iCur];..
1c30c 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 int nByte;. Vd
1c30d 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 beCursor *pCx =
1c30e 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20 0;. nByte = .
1c30f 20 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f ROUND8(sizeo
1c310 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 20 2b f(VdbeCursor)) +
1c311 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 65 . (isBtree
1c312 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 Cursor?sqlite3Bt
1c313 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a reeCursorSize():
1c314 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 0) + . 2*nF
1c315 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 ield*sizeof(u32)
1c316 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 ;.. assert( iCu
1c317 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a r<p->nCursor );.
1c318 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 if( p->apCsr[i
1c319 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 Cur] ){. sqli
1c31a 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
1c31b 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 r(p, p->apCsr[iC
1c31c 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 ur]);. p->apC
1c31d 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 sr[iCur] = 0;.
1c31e 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f }. if( SQLITE_O
1c31f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 K==sqlite3VdbeMe
1c320 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
1c321 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e e, 0) ){. p->
1c322 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 apCsr[iCur] = pC
1c323 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 2a x = (VdbeCursor*
1c324 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 )pMem->z;. me
1c325 6d 73 65 74 28 70 43 78 2c 20 30 2c 20 73 69 7a mset(pCx, 0, siz
1c326 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 eof(VdbeCursor))
1c327 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 3d ;. pCx->iDb =
1c328 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e iDb;. pCx->n
1c329 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a Field = nField;.
1c32a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 29 if( nField )
1c32b 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79 {. pCx->aTy
1c32c 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 pe = (u32 *)&pMe
1c32d 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65 m->z[ROUND8(size
1c32e 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 5d of(VdbeCursor))]
1c32f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c330 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b isBtreeCursor ){
1c331 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72 . pCx->pCur
1c332 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a sor = (BtCursor*
1c333 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65 ). &pMe
1c334 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65 m->z[ROUND8(size
1c335 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 2b of(VdbeCursor))+
1c336 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 2*nField*sizeof(
1c337 75 33 32 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c u32)];. sql
1c338 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a ite3BtreeCursorZ
1c339 65 72 6f 28 70 43 78 2d 3e 70 43 75 72 73 6f 72 ero(pCx->pCursor
1c33a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
1c33b 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a eturn pCx;.}../*
1c33c 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 .** Try to conve
1c33d 72 74 20 61 20 76 61 6c 75 65 20 69 6e 74 6f 20 rt a value into
1c33e 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 a numeric repres
1c33f 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 65 20 63 entation if we c
1c340 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68 an.** do so with
1c341 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f out loss of info
1c342 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 rmation. In oth
1c343 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 er words, if the
1c344 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 string.** looks
1c345 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 like a number,
1c346 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 convert it into
1c347 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 a number. If it
1c348 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f does not.** loo
1c349 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c k like a number,
1c34a 20 6c 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e leave it alone.
1c34b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1c34c 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 applyNumericAffi
1c34d 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b nity(Mem *pRec){
1c34e 0a 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c . if( (pRec->fl
1c34f 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c ags & (MEM_Real|
1c350 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a MEM_Int))==0 ){.
1c351 20 20 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d 3b int realnum;
1c352 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1c353 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
1c354 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 28 pRec);. if( (
1c355 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pRec->flags&MEM_
1c356 53 74 72 29 0a 20 20 20 20 20 20 20 20 20 26 26 Str). &&
1c357 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 sqlite3IsNumber
1c358 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e (pRec->z, &realn
1c359 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20 29 um, pRec->enc) )
1c35a 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 {. i64 valu
1c35b 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e;. sqlite3
1c35c 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1c35d 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 54 45 5f ng(pRec, SQLITE_
1c35e 55 54 46 38 29 3b 0a 20 20 20 20 20 20 69 66 28 UTF8);. if(
1c35f 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 73 71 6c !realnum && sql
1c360 69 74 65 33 41 74 6f 69 36 34 28 70 52 65 63 2d ite3Atoi64(pRec-
1c361 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a 20 >z, &value) ){.
1c362 20 20 20 20 20 20 20 70 52 65 63 2d 3e 75 2e 69 pRec->u.i
1c363 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 = value;.
1c364 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1c365 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pRec, MEM_Int);
1c366 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1c367 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c368 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 52 65 63 eMemRealify(pRec
1c369 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1c36a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 . }.}../*.** Pr
1c36b 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65 74 65 ocessing is dete
1c36c 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61 66 66 rmine by the aff
1c36d 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65 72 3a inity parameter:
1c36e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 .**.** SQLITE_AF
1c36f 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 51 F_INTEGER:.** SQ
1c370 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a LITE_AFF_REAL:.*
1c371 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d * SQLITE_AFF_NUM
1c372 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79 20 ERIC:.** Try
1c373 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65 63 20 to convert pRec
1c374 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 to an integer re
1c375 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72 20 presentation or
1c376 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69 6e a .** floatin
1c377 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73 65 6e g-point represen
1c378 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e 74 tation if an int
1c379 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 eger representat
1c37a 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f 74 ion.** is not
1c37b 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74 65 possible. Note
1c37c 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 67 65 that the intege
1c37d 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e r representation
1c37e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79 73 is.** always
1c37f 20 70 72 65 66 65 72 72 65 64 2c 20 65 76 65 6e preferred, even
1c380 20 69 66 20 74 68 65 20 61 66 66 69 6e 69 74 79 if the affinity
1c381 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61 75 73 is REAL, becaus
1c382 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65 67 e.** an integ
1c383 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f er representatio
1c384 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63 65 20 n is more space
1c385 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64 69 73 efficient on dis
1c386 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f k..**.** SQLITE_
1c387 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 20 AFF_TEXT:.**
1c388 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 Convert pRec to
1c389 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 a text represent
1c38a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c ation..**.** SQL
1c38b 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a ITE_AFF_NONE:.**
1c38c 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 No-op. pRec
1c38d 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
1c38e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 /.static void ap
1c38f 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 4d plyAffinity(. M
1c390 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 20 20 20 em *pRec,
1c391 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 /* The value
1c392 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 to apply affinit
1c393 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 61 y to */. char a
1c394 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 2f 2a ffinity, /*
1c395 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f The affinity to
1c396 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 be applied */.
1c397 20 75 38 20 65 6e 63 20 20 20 20 20 20 20 20 20 u8 enc
1c398 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 69 73 /* Use this
1c399 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a text encoding *
1c39a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 66 69 6e /.){. if( affin
1c39b 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
1c39c 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 4f TEXT ){. /* O
1c39d 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 68 65 20 nly attempt the
1c39e 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54 45 conversion to TE
1c39f 58 54 20 69 66 20 74 68 65 72 65 20 69 73 20 61 XT if there is a
1c3a0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 72 65 61 n integer or rea
1c3a1 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 l. ** represe
1c3a2 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61 6e ntation (blob an
1c3a3 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 d NULL do not ge
1c3a4 74 20 63 6f 6e 76 65 72 74 65 64 29 20 62 75 74 t converted) but
1c3a5 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 20 20 2a no string. *
1c3a6 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e * representation
1c3a7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1c3a8 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 0==(pRec->flags
1c3a9 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 28 70 52 &MEM_Str) && (pR
1c3aa 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52 ec->flags&(MEM_R
1c3ab 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b eal|MEM_Int)) ){
1c3ac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1c3ad 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 beMemStringify(p
1c3ae 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d Rec, enc);. }
1c3af 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 . pRec->flags
1c3b0 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d &= ~(MEM_Real|M
1c3b1 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 EM_Int);. }else
1c3b2 20 69 66 28 20 61 66 66 69 6e 69 74 79 21 3d 53 if( affinity!=S
1c3b3 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 QLITE_AFF_NONE )
1c3b4 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 66 {. assert( af
1c3b5 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1c3b6 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 FF_INTEGER || af
1c3b7 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1c3b8 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 20 20 FF_REAL.
1c3b9 20 20 20 20 20 7c 7c 20 61 66 66 69 6e 69 74 79 || affinity
1c3ba 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d ==SQLITE_AFF_NUM
1c3bb 45 52 49 43 20 29 3b 0a 20 20 20 20 61 70 70 6c ERIC );. appl
1c3bc 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 yNumericAffinity
1c3bd 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 (pRec);. if(
1c3be 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pRec->flags & ME
1c3bf 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 M_Real ){.
1c3c0 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 sqlite3VdbeInteg
1c3c1 65 72 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 erAffinity(pRec)
1c3c2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
1c3c3 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 *.** Try to conv
1c3c4 65 72 74 20 74 68 65 20 74 79 70 65 20 6f 66 20 ert the type of
1c3c5 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d a function argum
1c3c6 65 6e 74 20 6f 72 20 61 20 72 65 73 75 6c 74 20 ent or a result
1c3c7 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 column.** into a
1c3c8 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 numeric represe
1c3c9 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 69 ntation. Use ei
1c3ca 74 68 65 72 20 49 4e 54 45 47 45 52 20 6f 72 20 ther INTEGER or
1c3cb 52 45 41 4c 20 77 68 69 63 68 65 76 65 72 0a 2a REAL whichever.*
1c3cc 2a 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 * is appropriate
1c3cd 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74 . But only do t
1c3ce 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 he conversion if
1c3cf 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
1c3d0 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 without.** loss
1c3d1 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 of information a
1c3d2 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 nd return the re
1c3d3 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74 68 vised type of th
1c3d4 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a e argument..**.*
1c3d5 2a 20 54 68 69 73 20 69 73 20 61 6e 20 45 58 50 * This is an EXP
1c3d6 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 20 61 6e ERIMENTAL api an
1c3d7 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 d is subject to
1c3d8 63 68 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76 61 change or remova
1c3d9 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 l..*/.SQLITE_API
1c3da 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
1c3db 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 ue_numeric_type(
1c3dc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1c3dd 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 Val){. Mem *pMe
1c3de 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a m = (Mem*)pVal;.
1c3df 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 applyNumericAf
1c3e0 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20 finity(pMem);.
1c3e1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
1c3e2 6f 72 65 54 79 70 65 28 70 4d 65 6d 29 3b 0a 20 oreType(pMem);.
1c3e3 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 return pMem->ty
1c3e4 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 pe;.}../*.** Exp
1c3e5 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 orted version of
1c3e6 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 applyAffinity()
1c3e7 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 . This one works
1c3e8 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 on sqlite3_valu
1c3e9 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 e*, .** not the
1c3ea 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 internal Mem* ty
1c3eb 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pe..*/.SQLITE_PR
1c3ec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1c3ed 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 e3ValueApplyAffi
1c3ee 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f nity(. sqlite3_
1c3ef 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 value *pVal, .
1c3f0 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 u8 affinity, .
1c3f1 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c u8 enc.){. appl
1c3f2 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a yAffinity((Mem *
1c3f3 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c )pVal, affinity,
1c3f4 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 enc);.}..#ifdef
1c3f5 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
1c3f6 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 .** Write a nice
1c3f7 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e string represen
1c3f8 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f tation of the co
1c3f9 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 ntents of cell p
1c3fa 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 Mem.** into buff
1c3fb 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 er zBuf, length
1c3fc 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nBuf..*/.SQLITE_
1c3fd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1c3fe 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 ite3VdbeMemPrett
1c3ff 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d yPrint(Mem *pMem
1c400 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
1c401 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 char *zCsr = zB
1c402 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d uf;. int f = pM
1c403 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 em->flags;.. st
1c404 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
1c405 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b *const encnames[
1c406 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 ] = {"(X)", "(8)
1c407 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 ", "(16LE)", "(1
1c408 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 6BE)"};.. if( f
1c409 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 &MEM_Blob ){.
1c40a 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 int i;. char
1c40b 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 c;. if( f &
1c40c 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 MEM_Dyn ){.
1c40d 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 c = 'z';.
1c40e 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 assert( (f & (ME
1c40f 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 M_Static|MEM_Eph
1c410 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d em))==0 );. }
1c411 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1c412 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 _Static ){.
1c413 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 c = 't';.
1c414 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 assert( (f & (ME
1c415 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 M_Dyn|MEM_Ephem)
1c416 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 )==0 );. }els
1c417 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 e if( f & MEM_Ep
1c418 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d hem ){. c =
1c419 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 'e';. asse
1c41a 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 rt( (f & (MEM_St
1c41b 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d atic|MEM_Dyn))==
1c41c 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0 );. }else{.
1c41d 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 c = 's';.
1c41e 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 }.. sqlite
1c41f 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c420 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a zCsr, "%c", c);.
1c421 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 zCsr += sqli
1c422 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 te3Strlen30(zCsr
1c423 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
1c424 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 nprintf(100, zCs
1c425 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e r, "%d[", pMem->
1c426 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 n);. zCsr +=
1c427 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c428 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 zCsr);. for(i
1c429 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d =0; i<16 && i<pM
1c42a 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 em->n; i++){.
1c42b 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1c42c 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 ntf(100, zCsr, "
1c42d 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 %02X", ((int)pMe
1c42e 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 m->z[i] & 0xFF))
1c42f 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 ;. zCsr +=
1c430 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c431 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 zCsr);. }.
1c432 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 for(i=0; i<16 &
1c433 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b & i<pMem->n; i++
1c434 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 20 ){. char z
1c435 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 = pMem->z[i];.
1c436 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c 20 if( z<32 ||
1c437 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b 20 z>126 ) *zCsr++
1c438 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c 73 = '.';. els
1c439 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 e *zCsr++ = z;.
1c43a 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 }.. sqlite
1c43b 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c43c 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e 63 zCsr, "]%s", enc
1c43d 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d names[pMem->enc]
1c43e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 );. zCsr += s
1c43f 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1c440 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66 20 Csr);. if( f
1c441 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
1c442 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1c443 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 22 intf(100, zCsr,"
1c444 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a +%dz",pMem->u.nZ
1c445 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 ero);. zCsr
1c446 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 += sqlite3Strle
1c447 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d n30(zCsr);. }
1c448 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30 . *zCsr = '\0
1c449 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 ';. }else if( f
1c44a 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 & MEM_Str ){.
1c44b 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 int j, k;.
1c44c 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 zBuf[0] = ' ';.
1c44d 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 if( f & MEM_D
1c44e 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 yn ){. zBuf
1c44f 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 [1] = 'z';.
1c450 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1c451 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 EM_Static|MEM_Ep
1c452 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 hem))==0 );.
1c453 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 }else if( f & ME
1c454 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
1c455 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b zBuf[1] = 't';
1c456 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
1c457 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d f & (MEM_Dyn|MEM
1c458 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
1c459 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
1c45a 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 MEM_Ephem ){.
1c45b 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65 zBuf[1] = 'e
1c45c 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ';. assert(
1c45d 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 (f & (MEM_Stati
1c45e 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 c|MEM_Dyn))==0 )
1c45f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1c460 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27 zBuf[1] = 's'
1c461 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 ;. }. k =
1c462 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 2;. sqlite3_s
1c463 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42 nprintf(100, &zB
1c464 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65 uf[k], "%d", pMe
1c465 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 m->n);. k +=
1c466 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c467 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a &zBuf[k]);. z
1c468 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a Buf[k++] = '[';.
1c469 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 for(j=0; j<1
1c46a 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 5 && j<pMem->n;
1c46b 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 j++){. u8 c
1c46c 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 = pMem->z[j];.
1c46d 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 if( c>=0x20
1c46e 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 && c<0x7f ){.
1c46f 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 zBuf[k++]
1c470 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = c;. }else
1c471 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b {. zBuf[k
1c472 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 ++] = '.';.
1c473 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 }. }. zBu
1c474 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 f[k++] = ']';.
1c475 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1c476 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c tf(100,&zBuf[k],
1c477 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e encnames[pMem->
1c478 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 enc]);. k +=
1c479 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c47a 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a &zBuf[k]);. z
1c47b 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 Buf[k++] = 0;.
1c47c 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 }.}.#endif..#ifd
1c47d 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1c47e 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 /*.** Print the
1c47f 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 value of a regis
1c480 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 ter for tracing
1c481 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 purposes:.*/.sta
1c482 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63 tic void memTrac
1c483 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74 ePrint(FILE *out
1c484 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 , Mem *p){. if(
1c485 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
1c486 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 Null ){. fpri
1c487 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 ntf(out, " NULL"
1c488 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 );. }else if( (
1c489 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
1c48a 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 Int|MEM_Str))==(
1c48b 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 MEM_Int|MEM_Str)
1c48c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
1c48d 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c out, " si:%lld",
1c48e 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 p->u.i);. }els
1c48f 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 e if( p->flags &
1c490 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1c491 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69 fprintf(out, " i
1c492 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b :%lld", p->u.i);
1c493 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1c494 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1c495 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 INT. }else if(
1c496 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 p->flags & MEM_R
1c497 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e eal ){. fprin
1c498 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c tf(out, " r:%g",
1c499 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 66 0a 20 p->r);.#endif.
1c49a 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
1c49b 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
1c49c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
1c49d 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 74 29 22 out, " (rowset)"
1c49e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1c49f 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a char zBuf[200];.
1c4a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1c4a1 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c emPrettyPrint(p,
1c4a2 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 zBuf);. fpri
1c4a3 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20 ntf(out, " ");.
1c4a4 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1c4a5 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d "%s", zBuf);. }
1c4a6 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 .}.static void r
1c4a7 65 67 69 73 74 65 72 54 72 61 63 65 28 46 49 4c egisterTrace(FIL
1c4a8 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67 E *out, int iReg
1c4a9 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72 , Mem *p){. fpr
1c4aa 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25 intf(out, "REG[%
1c4ab 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20 d] = ", iReg);.
1c4ac 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f memTracePrint(o
1c4ad 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74 ut, p);. fprint
1c4ae 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a f(out, "\n");.}.
1c4af 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
1c4b0 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64 QLITE_DEBUG.# d
1c4b1 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 efine REGISTER_T
1c4b2 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e RACE(R,M) if(p->
1c4b3 74 72 61 63 65 29 72 65 67 69 73 74 65 72 54 72 trace)registerTr
1c4b4 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d ace(p->trace,R,M
1c4b5 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e ).#else.# defin
1c4b6 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 e REGISTER_TRACE
1c4b7 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 (R,M).#endif...#
1c4b8 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
1c4b9 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d LE../* .** hwtim
1c4ba 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c e.h contains inl
1c4bb 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f ine assembler co
1c4bc 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 de for implement
1c4bd 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 ing .** high-per
1c4be 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 formance timing
1c4bf 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a routines..*/./**
1c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
1c4c1 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e lude hwtime.h in
1c4c2 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 the middle of v
1c4c3 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.c **********
1c4c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1c4c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
1c4c6 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 in file hwtime.h
1c4c7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1c4c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
1c4ca 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a ** 2008 May 27.*
1c4cb 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1c4cc 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1c4cd 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1c4ce 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1c4cf 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
1c4d0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1c4d1 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1c4d2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1c4d3 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1c4d4 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1c4d5 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1c4d6 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1c4d7 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1c4d8 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1c4d9 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1c4da 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1c4db 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1c4dc 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1c4dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4e1 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
1c4e2 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 le contains inli
1c4e3 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 ne asm code for
1c4e4 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 retrieving "high
1c4e5 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a -performance".**
1c4e6 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 counters for x8
1c4e7 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 6 class CPUs..*/
1c4e8 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 .#ifndef _HWTIME
1c4e9 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 _H_.#define _HWT
1c4ea 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 IME_H_../*.** Th
1c4eb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1c4ec 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f ine only works o
1c4ed 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 n pentium-class
1c4ee 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 (or newer) proce
1c4ef 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 ssors..** It use
1c4f0 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f s the RDTSC opco
1c4f1 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 de to read the c
1c4f2 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 ycle count value
1c4f3 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 out of the.** p
1c4f4 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 rocessor and ret
1c4f5 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e urns that value.
1c4f6 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 This can be us
1c4f7 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a ed for high-res.
1c4f8 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f ** profiling..*/
1c4f9 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f .#if (defined(__
1c4fa 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e GNUC__) || defin
1c4fb 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 ed(_MSC_VER)) &&
1c4fc 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 \. (define
1c4fd 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e d(i386) || defin
1c4fe 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 ed(__i386__) ||
1c4ff 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 defined(_M_IX86)
1c500 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 ).. #if defined
1c501 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f (__GNUC__).. __
1c502 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
1c503 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
1c504 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
1c505 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f unsigned int lo
1c506 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d , hi;. __asm
1c507 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
1c508 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 ("rdtsc" : "=a"
1c509 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 (lo), "=d" (hi))
1c50a 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 ;. return (s
1c50b 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 qlite_uint64)hi
1c50c 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a << 32 | lo;. }.
1c50d 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 . #elif defined
1c50e 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f (_MSC_VER).. __
1c50f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 declspec(naked)
1c510 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f __inline sqlite_
1c511 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 uint64 __cdecl s
1c512 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1c513 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b d){. __asm {
1c514 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 . rdtsc.
1c515 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 ret
1c516 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 ; return value
1c517 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 at EDX:EAX.
1c518 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a }. }.. #endif.
1c519 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 .#elif (defined(
1c51a 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 __GNUC__) && def
1c51b 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 ined(__x86_64__)
1c51c 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
1c51d 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
1c51e 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
1c51f 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
1c520 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 d long val;.
1c521 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
1c522 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 tile__ ("rdtsc"
1c523 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 : "=A" (val));.
1c524 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b return val;
1c525 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 . }. .#elif (de
1c526 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
1c527 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 && defined(__ppc
1c528 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
1c529 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
1c52a 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
1c52b 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
1c52c 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 gned long long r
1c52d 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 etval;. uns
1c52e 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b igned long junk;
1c52f 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f . __asm__ _
1c530 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e _volatile__ ("\n
1c531 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 \. 1:
1c532 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e mftbu %1\n
1c533 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
1c534 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c mftb %L0\
1c535 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
1c536 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c mftbu %0\
1c537 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
1c538 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c cmpw %0,
1c539 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
1c53a 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 bne
1c53b 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 1b".
1c53c 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 : "=r" (re
1c53d 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e tval), "=r" (jun
1c53e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 k));. retur
1c53f 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 n retval;. }..#
1c540 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e else.. #error N
1c541 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 eed implementati
1c542 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 on of sqlite3Hwt
1c543 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 ime() for your p
1c544 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 latform... /*.
1c545 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 ** To compile w
1c546 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 ithout implement
1c547 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d ing sqlite3Hwtim
1c548 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 e() for your pla
1c549 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 tform,. ** you
1c54a 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 can remove the a
1c54b 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 bove #error and
1c54c 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e use the followin
1c54d 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 g. ** stub func
1c54e 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 tion. You will
1c54f 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 lose timing supp
1c550 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a ort for many. *
1c551 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 * of the debuggi
1c552 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 ng and testing u
1c553 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 tilities, but it
1c554 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 should at. **
1c555 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e least compile an
1c556 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 d run.. */.SQLI
1c557 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c TE_PRIVATE sql
1c558 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
1c559 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 e3Hwtime(void){
1c55a 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f return ((sqlite_
1c55b 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 uint64)0); }..#e
1c55c 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 ndif..#endif /*
1c55d 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 !defined(_HWTIME
1c55e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
1c55f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1c560 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a hwtime.h *******
1c561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c562 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c563 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1c564 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
1c565 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
1c566 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20 t off in vdbe.c
1c567 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c568 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 *******/..#endif
1c569 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 ../*.** The CHEC
1c56a 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 K_FOR_INTERRUPT
1c56b 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65 macro defined he
1c56c 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 re looks to see
1c56d 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 if the.** sqlite
1c56e 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f 3_interrupt() ro
1c56f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 utine has been c
1c570 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61 alled. If it ha
1c571 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 s been, then.**
1c572 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 processing of th
1c573 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69 e VDBE program i
1c574 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a s interrupted..*
1c575 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 *.** This macro
1c576 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 69 added to every i
1c577 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 nstruction that
1c578 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f does a jump in o
1c579 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 rder to.** imple
1c57a 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 ment a loop. Th
1c57b 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f 20 is test used to
1c57c 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 be on every sing
1c57d 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a le instruction,.
1c57e 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e ** but that mean
1c57f 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e t we more testin
1c580 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 64 g that we needed
1c581 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 . By only testi
1c582 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f ng the.** flag o
1c583 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 n jump instructi
1c584 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 73 ons, we get a (s
1c585 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 mall) speed impr
1c586 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 ovement..*/.#def
1c587 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e ine CHECK_FOR_IN
1c588 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28 TERRUPT \. if(
1c589 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
1c58a 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f upted ) goto abo
1c58b 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 rt_due_to_interr
1c58c 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c upt;..#ifdef SQL
1c58d 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 ITE_DEBUG.static
1c58e 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28 int fileExists(
1c58f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
1c590 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b st char *zFile){
1c591 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a . int res = 0;.
1c592 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1c593 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c E_OK;.#ifdef SQL
1c594 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 ITE_TEST. /* If
1c595 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c we are currentl
1c596 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72 y testing IO err
1c597 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 ors, then do not
1c598 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 call OsAccess()
1c599 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f to. ** test fo
1c59a 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f r the presence o
1c59b 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73 f zFile. This is
1c59c 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20 because any IO
1c59d 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 error that. **
1c59e 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c occurs here will
1c59f 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64 not be reported
1c5a0 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 65 , causing the te
1c5a1 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f st to fail.. */
1c5a2 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 . extern int sq
1c5a3 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
1c5a4 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 ending;. if( sq
1c5a5 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
1c5a6 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 ending<=0 ).#end
1c5a7 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 if. rc = sqli
1c5a8 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e te3OsAccess(db->
1c5a9 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c pVfs, zFile, SQL
1c5aa 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
1c5ab 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75 S, &res);. retu
1c5ac 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53 rn (res && rc==S
1c5ad 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e QLITE_OK);.}.#en
1c5ae 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 dif..#ifndef NDE
1c5af 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 BUG./*.** This f
1c5b0 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
1c5b1 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 called from with
1c5b2 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 65 in an assert() e
1c5b3 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a xpression. It.**
1c5b4 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 checks that the
1c5b5 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 sqlite3.nTransa
1c5b6 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 ction variable i
1c5b7 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20 s correctly set
1c5b8 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 to.** the number
1c5b9 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 of non-transact
1c5ba 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63 ion savepoints c
1c5bb 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 urrently in the
1c5bc 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 .** linked list
1c5bd 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69 starting at sqli
1c5be 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a te3.pSavepoint..
1c5bf 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a ** .** Usage:.**
1c5c0 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 .** assert(
1c5c1 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f checkSavepointCo
1c5c2 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 unt(db) );.*/.st
1c5c3 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61 atic int checkSa
1c5c4 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c vepointCount(sql
1c5c5 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
1c5c6 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f n = 0;. Savepo
1c5c7 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d int *p;. for(p=
1c5c8 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 db->pSavepoint;
1c5c9 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e p; p=p->pNext) n
1c5ca 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d ++;. assert( n=
1c5cb 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 =(db->nSavepoint
1c5cc 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 + db->isTransac
1c5cd 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 tionSavepoint) )
1c5ce 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a ;. return 1;.}.
1c5cf 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 #endif../*.** Ex
1c5d0 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 ecute as much of
1c5d1 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 a VDBE program
1c5d2 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 as we can then r
1c5d3 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c eturn..**.** sql
1c5d4 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 ite3VdbeMakeRead
1c5d5 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c y() must be call
1c5d6 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 ed before this r
1c5d7 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 outine in order
1c5d8 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 to.** close the
1c5d9 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 program with a f
1c5da 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 inal OP_Halt and
1c5db 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 to set up the c
1c5dc 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 allbacks.** and
1c5dd 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
1c5de 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a e pointer..**.**
1c5df 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 Whenever a row
1c5e0 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 or result data i
1c5e1 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 s available, thi
1c5e2 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 s routine will e
1c5e3 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 ither.** invoke
1c5e4 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 the result callb
1c5e5 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 ack (if there is
1c5e6 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 one) or return
1c5e7 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 with.** SQLITE_R
1c5e8 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 OW..**.** If an
1c5e9 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
1c5ea 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 to open a locked
1c5eb 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 database, then
1c5ec 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1c5ed 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f will either invo
1c5ee 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c ke the busy call
1c5ef 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 back (if there i
1c5f0 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c s one) or it wil
1c5f1 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 l.** return SQLI
1c5f2 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 TE_BUSY..**.** I
1c5f3 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1c5f4 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 s, an error mess
1c5f5 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 age is written t
1c5f6 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
1c5f7 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 d.** from sqlite
1c5f8 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 3_malloc() and p
1c5f9 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 ->zErrMsg is mad
1c5fa 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 e to point to th
1c5fb 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 at memory..** Th
1c5fc 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
1c5fd 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 stored in p->rc
1c5fe 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
1c5ff 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1c600 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ERROR..**.** If
1c601 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 the callback eve
1c602 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 r returns non-ze
1c603 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f ro, then the pro
1c604 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d gram exits.** im
1c605 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 mediately. Ther
1c606 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 e will be no err
1c607 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 or message but t
1c608 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 he p->rc field i
1c609 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 s.** set to SQLI
1c60a 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 TE_ABORT and thi
1c60b 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 s routine will r
1c60c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1c60d 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f OR..**.** A memo
1c60e 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
1c60f 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 ror causes p->rc
1c610 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 to be set to SQ
1c611 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 LITE_NOMEM and t
1c612 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 his.** routine t
1c613 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f o return SQLITE_
1c614 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 ERROR..**.** Oth
1c615 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 er fatal errors
1c616 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1c617 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 ROR..**.** After
1c618 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 this routine ha
1c619 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 s finished, sqli
1c61a 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 te3VdbeFinalize(
1c61b 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 ) should be.** u
1c61c 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 sed to clean up
1c61d 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 the mess that wa
1c61e 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a s left behind..*
1c61f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c620 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1c621 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 Exec(. Vdbe *p
1c622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c623 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a /* The VDBE *
1c624 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 /.){. int pc;
1c625 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c626 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d /* The program
1c627 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 counter */. Op
1c628 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 20 *aOp = p->aOp;
1c629 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 /* Copy
1c62a 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f 0a 20 20 of p->aOp */.
1c62b 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 Op *pOp;
1c62c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
1c62d 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 rrent operation
1c62e 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
1c62f 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
1c630 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
1c631 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 rn */. sqlite3
1c632 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 *db = p->db;
1c633 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1c634 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 73 65 74 se */. u8 reset
1c635 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 3d 20 SchemaOnFault =
1c636 30 3b 20 2f 2a 20 52 65 73 65 74 20 73 63 68 65 0; /* Reset sche
1c637 6d 61 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f ma after an erro
1c638 72 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 r if true */. u
1c639 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 8 encoding = ENC
1c63a 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 (db); /* The
1c63b 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 database encodi
1c63c 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ng */.#ifndef SQ
1c63d 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 LITE_OMIT_PROGRE
1c63e 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e SS_CALLBACK. in
1c63f 74 20 63 68 65 63 6b 50 72 6f 67 72 65 73 73 3b t checkProgress;
1c640 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1c641 20 69 66 20 70 72 6f 67 72 65 73 73 20 63 61 6c if progress cal
1c642 6c 62 61 63 6b 73 20 61 72 65 20 65 6e 61 62 6c lbacks are enabl
1c643 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f ed */. int nPro
1c644 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 gressOps = 0;
1c645 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 /* Opcodes ex
1c646 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f ecuted since pro
1c647 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 gress callback.
1c648 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d 65 6d 20 */.#endif. Mem
1c649 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b *aMem = p->aMem;
1c64a 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1c64b 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a 20 20 4d f p->aMem */. M
1c64c 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 em *pIn1 = 0;
1c64d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 /* 1st
1c64e 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a input operand *
1c64f 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 /. Mem *pIn2 =
1c650 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
1c651 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 * 2nd input oper
1c652 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 and */. Mem *pI
1c653 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 n3 = 0;
1c654 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 /* 3rd input
1c655 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 operand */. Me
1c656 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 m *pOut = 0;
1c657 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
1c658 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ut operand */.
1c659 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 int iCompare = 0
1c65a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ; /* Re
1c65b 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f sult of last OP_
1c65c 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f Compare operatio
1c65d 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 n */. int *aPer
1c65e 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 mute = 0;
1c65f 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e /* Permutation
1c660 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 of columns for
1c661 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 OP_Compare */.#i
1c662 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
1c663 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20 E. u64 start;
1c664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c665 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e * CPU clock coun
1c666 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 t at start of op
1c667 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 code */. int or
1c668 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20 igPc;
1c669 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 /* Program
1c66a 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74 counter at start
1c66b 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 of opcode */.#e
1c66c 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a ndif. /********
1c66d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c66f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a ************. *
1c671 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * Automatically
1c672 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a 20 generated code.
1c673 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c **. ** The fol
1c674 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20 lowing union is
1c675 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 automatically ge
1c676 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a 20 nerated by the.
1c677 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 ** vdbe-compres
1c678 73 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54 s.tcl script. T
1c679 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 he purpose of th
1c67a 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a 20 is union is to.
1c67b 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61 ** reduce the a
1c67c 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73 mount of stack s
1c67d 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79 pace required by
1c67e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
1c67f 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 ** See comment
1c680 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 6f s in the vdbe-co
1c681 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 mpress.tcl scrip
1c682 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 t for details..
1c683 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 65 */. union vdbe
1c684 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 20 ExecUnion {.
1c685 73 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 5f struct OP_Yield_
1c686 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c687 20 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 int pcDest;.
1c688 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 72 } aa;. str
1c689 75 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65 5f uct OP_Variable_
1c68a 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c68b 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 int p1;
1c68c 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 /* Variable
1c68d 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a to copy from */.
1c68e 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 int p2;
1c68f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1c690 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f er to copy to */
1c691 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 . int n;
1c692 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1c693 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74 r of values left
1c694 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 to copy */.
1c695 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 Mem *pVar;
1c696 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e /* Value bein
1c697 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f g transferred */
1c698 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20 73 . } ab;. s
1c699 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 truct OP_Move_st
1c69a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c69b 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 char *zMalloc;
1c69c 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 /* Holding var
1c69d 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 iable for alloca
1c69e 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 ted memory */.
1c69f 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 int n;
1c6a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c6a1 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 f registers left
1c6a2 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 to copy */.
1c6a3 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
1c6a4 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 /* Register t
1c6a5 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 o copy from */.
1c6a6 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 int p2;
1c6a7 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c6a8 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a r to copy to */.
1c6a9 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 74 } ac;. st
1c6aa 72 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52 6f ruct OP_ResultRo
1c6ab 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 w_stack_vars {.
1c6ac 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a Mem *pMem;.
1c6ad 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1c6ae 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75 63 } ad;. struc
1c6af 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61 63 t OP_Concat_stac
1c6b0 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c6b1 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 20 64 nByte;. }
1c6b2 61 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ae;. struct O
1c6b3 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61 63 P_Remainder_stac
1c6b4 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c6b5 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f nt flags; /
1c6b6 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a * Combined MEM_*
1c6b7 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68 flags from both
1c6b8 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20 20 inputs */.
1c6b9 20 69 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 i64 iA;
1c6ba 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 /* Integer valu
1c6bb 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e e of left operan
1c6bc 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 d */. i64 i
1c6bd 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e B; /* In
1c6be 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72 teger value of r
1c6bf 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
1c6c0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41 3b double rA;
1c6c1 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 /* Real va
1c6c2 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 lue of left oper
1c6c3 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 and */. dou
1c6c4 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20 ble rB; /*
1c6c5 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69 Real value of ri
1c6c6 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 ght operand */.
1c6c7 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74 72 } af;. str
1c6c8 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f uct OP_Function_
1c6c9 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c6ca 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
1c6cb 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20 20 Mem *pArg;.
1c6cc 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1c6cd 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69 ctx;. sqli
1c6ce 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 te3_value **apVa
1c6cf 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a l;. int n;.
1c6d0 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 74 } ag;. st
1c6d1 72 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69 67 ruct OP_ShiftRig
1c6d2 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ht_stack_vars {.
1c6d3 20 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20 20 i64 a;.
1c6d4 20 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d 20 i64 b;. }
1c6d5 61 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ah;. struct O
1c6d6 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 P_Ge_stack_vars
1c6d7 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b {. int res;
1c6d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1c6d9 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f 6d esult of the com
1c6da 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31 20 parison of pIn1
1c6db 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f 0a against pIn3 */.
1c6dc 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e char affin
1c6dd 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 ity; /* Aff
1c6de 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f 72 inity to use for
1c6df 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 comparison */.
1c6e0 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74 72 } ai;. str
1c6e1 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 uct OP_Compare_s
1c6e2 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c6e3 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 int n;. i
1c6e4 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 nt i;. int
1c6e5 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 p1;. int p2
1c6e6 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b 65 ;. const Ke
1c6e7 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
1c6e8 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b 0a . int idx;.
1c6e9 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 CollSeq *p
1c6ea 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c Coll; /* Coll
1c6eb 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 ating sequence t
1c6ec 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 65 o use on this te
1c6ed 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 rm */. int
1c6ee 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f bRev; /
1c6ef 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 * True for DESCE
1c6f0 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 NDING sort order
1c6f1 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20 20 */. } aj;.
1c6f2 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f 73 struct OP_Or_s
1c6f3 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c6f4 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 int v1; /*
1c6f5 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 Left operand: 0
1c6f6 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 ==FALSE, 1==TRUE
1c6f7 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 , 2==UNKNOWN or
1c6f8 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69 6e NULL */. in
1c6f9 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68 t v2; /* Righ
1c6fa 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41 t operand: 0==FA
1c6fb 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d LSE, 1==TRUE, 2=
1c6fc 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c =UNKNOWN or NULL
1c6fd 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20 20 */. } ak;.
1c6fe 20 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e 6f struct OP_IfNo
1c6ff 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c700 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 int c;.
1c701 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 } al;. struct
1c702 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b OP_Column_stack
1c703 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 33 _vars {. u3
1c704 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 2 payloadSize;
1c705 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c706 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1c707 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 70 d */. i64 p
1c708 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a ayloadSize64; /*
1c709 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1c70a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1c70b 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 /. int p1;
1c70c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 /* P1
1c70d 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 value of the op
1c70e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e code */. in
1c70f 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 t p2;
1c710 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 /* column numbe
1c711 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f r to retrieve */
1c712 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c713 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 65 r *pC; /* The
1c714 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a VDBE cursor */.
1c715 20 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65 63 char *zRec
1c716 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
1c717 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 ter to complete
1c718 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 record-data */.
1c719 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c71a 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 Crsr; /* The B
1c71b 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 Tree cursor */.
1c71c 20 20 20 20 20 75 33 32 20 2a 61 54 79 70 65 3b u32 *aType;
1c71d 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 /* aType
1c71e 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 [i] holds the nu
1c71f 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 meric type of th
1c720 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f e i-th column */
1c721 0a 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 66 . u32 *aOff
1c722 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 set; /* aOf
1c723 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 fset[i] is offse
1c724 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 t to start of da
1c725 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 ta for i-th colu
1c726 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 mn */. int
1c727 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f nField; /
1c728 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * number of fiel
1c729 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1c72a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 */. int le
1c72b 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 n; /*
1c72c 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 The length of th
1c72d 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 e serialized dat
1c72e 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e a for the column
1c72f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b */. int i;
1c730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c731 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
1c732 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 char *zDat
1c733 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 a; /* Part
1c734 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 of the record b
1c735 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a eing decoded */.
1c736 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 Mem *pDest
1c737 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 ; /* Wher
1c738 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 e to write the e
1c739 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a xtracted value *
1c73a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65 6d /. Mem sMem
1c73b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f ; /* Fo
1c73c 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 r storing the re
1c73d 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 cord being decod
1c73e 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a ed */. u8 *
1c73f 7a 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f zIdx; /
1c740 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 * Index into hea
1c741 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 der */. u8
1c742 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 20 *zEndHdr;
1c743 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 /* Pointer to fi
1c744 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 rst byte after t
1c745 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 he header */.
1c746 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 u32 offset;
1c747 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1c748 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f into the data */
1c749 0a 20 20 20 20 20 20 75 36 34 20 6f 66 66 73 65 . u64 offse
1c74a 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d t64; /* 64-
1c74b 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 bit offset. 64
1c74c 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 bits needed to c
1c74d 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f atch overflow */
1c74e 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64 72 . int szHdr
1c74f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a ; /* Siz
1c750 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 e of the header
1c751 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73 74 size field at st
1c752 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f art of record */
1c753 0a 20 20 20 20 20 20 69 6e 74 20 61 76 61 69 6c . int avail
1c754 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
1c755 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1c756 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a available data *
1c757 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 /. Mem *pRe
1c758 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 g; /* Ps
1c759 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 eudoTable input
1c75a 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 register */.
1c75b 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 } am;. struct
1c75c 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74 61 OP_Affinity_sta
1c75d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c75e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66 const char *zAff
1c75f 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 inity; /* The
1c760 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61 affinity to be a
1c761 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 pplied */.
1c762 63 68 61 72 20 63 41 66 66 3b 20 20 20 20 20 20 char cAff;
1c763 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 /* A si
1c764 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 20 6f ngle character o
1c765 66 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 f affinity */.
1c766 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72 75 } an;. stru
1c767 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 ct OP_MakeRecord
1c768 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c769 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f u8 *zNewReco
1c76a 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 rd; /* A
1c76b 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
1c76c 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 he data for the
1c76d 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
1c76e 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 Mem *pRec;
1c76f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1c770 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a e new record */.
1c771 20 20 20 20 20 20 75 36 34 20 6e 44 61 74 61 3b u64 nData;
1c772 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c773 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c774 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a 2f of data space */
1c775 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 64 72 3b . int nHdr;
1c776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c777 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1c778 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 of header space
1c779 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 6e 42 */. i64 nB
1c77a 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 yte;
1c77b 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 /* Data space r
1c77c 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 equired for this
1c77d 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c77e 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 int nZero;
1c77f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1c780 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 r of zero bytes
1c781 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
1c782 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 e record */.
1c783 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 int nVarint;
1c784 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c785 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 er of bytes in a
1c786 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20 20 varint */.
1c787 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1c788 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 ; /* Type
1c789 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20 4d field */. M
1c78a 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 em *pData0;
1c78b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 /* First f
1c78c 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 ield to be combi
1c78d 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 ned into the rec
1c78e 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d ord */. Mem
1c78f 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 *pLast;
1c790 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c /* Last fiel
1c791 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 d of the record
1c792 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 */. int nFi
1c793 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 eld;
1c794 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 /* Number of fie
1c795 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 lds in the recor
1c796 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 d */. char
1c797 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 *zAffinity;
1c798 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 /* The affinit
1c799 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 y string for the
1c79a 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c79b 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 int file_format
1c79c 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 ; /* File
1c79d 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f format to use fo
1c79e 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 r encoding */.
1c79f 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
1c7a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
1c7a1 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 ace used in zNew
1c7a2 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20 20 Record[] */.
1c7a3 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 int len;
1c7a4 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
1c7a5 74 68 20 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f th of a field */
1c7a6 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20 73 . } ao;. s
1c7a7 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f 73 truct OP_Count_s
1c7a8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c7a9 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20 i64 nEntry;.
1c7aa 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c7ab 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a 20 rsr;. } ap;.
1c7ac 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 61 76 struct OP_Sav
1c7ad 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61 72 epoint_stack_var
1c7ae 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 s {. int p1
1c7af 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
1c7b1 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 ue of P1 operand
1c7b2 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a */. char *
1c7b3 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
1c7b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1c7b5 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a e of savepoint *
1c7b6 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d /. int nNam
1c7b7 65 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 e;. Savepoi
1c7b8 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 nt *pNew;.
1c7b9 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
1c7ba 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 53 61 76 point;. Sav
1c7bb 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 epoint *pTmp;.
1c7bc 20 20 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69 int iSavepoi
1c7bd 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 nt;. int ii
1c7be 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 20 20 20 ;. } aq;.
1c7bf 73 74 72 75 63 74 20 4f 50 5f 41 75 74 6f 43 6f struct OP_AutoCo
1c7c0 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 mmit_stack_vars
1c7c1 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73 69 {. int desi
1c7c2 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 redAutoCommit;.
1c7c3 20 20 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 int iRollba
1c7c4 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 75 ck;. int tu
1c7c5 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61 72 rnOnAC;. } ar
1c7c6 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c7c7 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61 63 Transaction_stac
1c7c8 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 k_vars {. B
1c7c9 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d tree *pBt;. }
1c7ca 20 61 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 as;. struct
1c7cb 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73 74 OP_ReadCookie_st
1c7cc 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c7cd 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 20 int iMeta;.
1c7ce 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1c7cf 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20 20 int iCookie;.
1c7d0 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 74 72 75 } at;. stru
1c7d1 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 5f ct OP_SetCookie_
1c7d2 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c7d3 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 Db *pDb;.
1c7d4 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 75 63 74 } au;. struct
1c7d5 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 OP_VerifyCookie
1c7d6 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c7d7 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 int iMeta;.
1c7d8 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b Btree *pBt;
1c7d9 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 20 20 73 . } av;. s
1c7da 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72 69 truct OP_OpenWri
1c7db 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a te_stack_vars {.
1c7dc 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 int nField
1c7dd 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 ;. KeyInfo
1c7de 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 *pKeyInfo;.
1c7df 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 69 int p2;. i
1c7e0 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69 6e nt iDb;. in
1c7e1 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 20 20 20 t wrFlag;.
1c7e2 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 20 20 20 Btree *pX;.
1c7e3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c7e4 72 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 r;. Db *pDb
1c7e5 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 20 20 20 ;. } aw;.
1c7e6 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45 70 struct OP_OpenEp
1c7e7 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76 61 hemeral_stack_va
1c7e8 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c7e9 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 ursor *pCx;.
1c7ea 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 75 63 74 } ax;. struct
1c7eb 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f 73 OP_OpenPseudo_s
1c7ec 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c7ed 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c7ee 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20 20 x;. } ay;.
1c7ef 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 47 struct OP_SeekG
1c7f0 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c7f1 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 int res;.
1c7f2 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 20 int oc;.
1c7f3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c7f4 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 ;. Unpacked
1c7f5 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 Record r;.
1c7f6 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 int nField;.
1c7f7 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 i64 iKey;
1c7f8 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 /* The rowid we
1c7f9 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 are to seek to
1c7fa 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 20 */. } az;.
1c7fb 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 5f struct OP_Seek_
1c7fc 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c7fd 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c7fe 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20 20 C;. } ba;.
1c7ff 20 73 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e 64 struct OP_Found
1c800 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c801 20 20 20 20 69 6e 74 20 61 6c 72 65 61 64 79 45 int alreadyE
1c802 78 69 73 74 73 3b 0a 20 20 20 20 20 20 56 64 62 xists;. Vdb
1c803 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c804 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c805 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1c806 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 20 20 20 *pIdxKey;.
1c807 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c808 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 54 r;. char aT
1c809 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 empRec[ROUND8(si
1c80a 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1c80b 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d ord)) + sizeof(M
1c80c 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20 em)*3 + 7];.
1c80d 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74 } bb;. struct
1c80e 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61 OP_IsUnique_sta
1c80f 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c810 75 31 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64 u16 ii;. Vd
1c811 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1c812 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c813 43 72 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20 Crsr;. u16
1c814 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65 nField;. Me
1c815 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20 20 55 6e m *aMx;. Un
1c816 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 packedRecord r;
1c817 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c818 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 /* B-Tree index
1c819 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 search key */.
1c81a 20 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20 i64 R;
1c81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c81c 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 /* Rowid
1c81d 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1c81e 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d 20 ter P3 */. }
1c81f 62 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bc;. struct O
1c820 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61 63 P_NotExists_stac
1c821 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 k_vars {. V
1c822 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1c823 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c824 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 Crsr;. int
1c825 72 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20 69 res;. u64 i
1c826 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a 20 Key;. } bd;.
1c827 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65 77 struct OP_New
1c828 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73 Rowid_stack_vars
1c829 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b 20 {. i64 v;
1c82a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c82b 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 /* The new rowid
1c82c 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 */. VdbeCu
1c82d 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 rsor *pC;
1c82e 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 /* Cursor of ta
1c82f 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e ble to get the n
1c830 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 ew rowid */.
1c831 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 int res;
1c832 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
1c833 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 lt of an sqlite3
1c834 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20 BtreeLast() */.
1c835 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 int cnt;
1c836 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1c837 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 ounter to limit
1c838 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
1c839 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 arches */.
1c83a 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 Mem *pMem;
1c83b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1c83c 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 er holding large
1c83d 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 st rowid for AUT
1c83e 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 OINCREMENT */.
1c83f 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c840 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f Frame; /* Ro
1c841 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 ot frame of VDBE
1c842 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20 20 */. } be;.
1c843 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73 65 struct OP_Inse
1c844 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 rtInt_stack_vars
1c845 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 {. Mem *pD
1c846 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 ata; /* ME
1c847 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 M cell holding d
1c848 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 63 6f ata for the reco
1c849 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 rd to be inserte
1c84a 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a d */. Mem *
1c84b 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 pKey; /*
1c84c 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 MEM cell holding
1c84d 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 key for the re
1c84e 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 cord */. i6
1c84f 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 4 iKey;
1c850 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 52 /* The integer R
1c851 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72 20 OWID or key for
1c852 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 65 the record to be
1c853 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 20 inserted */.
1c854 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c855 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 C; /* Cursor t
1c856 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 o table into whi
1c857 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 ch insert is wri
1c858 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e tten */. in
1c859 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 t nZero;
1c85a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 /* Number of zer
1c85b 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e o-bytes to appen
1c85c 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 d */. int s
1c85d 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 eekResult; /*
1c85e 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 Result of prior
1c85f 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 seek or 0 if no
1c860 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c USESEEKRESULT fl
1c861 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 ag */. cons
1c862 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f 2a t char *zDb; /*
1c863 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2d database name -
1c864 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70 64 used by the upd
1c865 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20 ate hook */.
1c866 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
1c867 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d bl; /* Table nam
1c868 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 e - used by the
1c869 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 opdate hook */.
1c86a 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 int op;
1c86b 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 /* Opcode
1c86c 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b for update hook
1c86d 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 : SQLITE_UPDATE
1c86e 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 or SQLITE_INSERT
1c86f 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b 0a 20 20 */. } bf;.
1c870 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 6c 65 struct OP_Dele
1c871 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a te_stack_vars {.
1c872 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 0a i64 iKey;.
1c873 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c874 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 67 3b 0a *pC;. } bg;.
1c875 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f struct OP_Ro
1c876 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 61 72 73 wData_stack_vars
1c877 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c878 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c879 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c87a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 u32 n;.
1c87b 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 20 20 i64 n64;.
1c87c 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 75 63 74 } bh;. struct
1c87d 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 63 6b 5f OP_Rowid_stack_
1c87e 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c87f 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c880 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 i64 v;.
1c881 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c882 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 tab;. const
1c883 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c884 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20 *pModule;. }
1c885 62 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bi;. struct O
1c886 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f P_NullRow_stack_
1c887 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c888 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c889 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 72 75 63 } bj;. struc
1c88a 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 63 6b 5f t OP_Last_stack_
1c88b 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c88c 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c88d 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c88e 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 sr;. int re
1c88f 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a 20 20 20 s;. } bk;.
1c890 20 73 74 72 75 63 74 20 4f 50 5f 52 65 77 69 6e struct OP_Rewin
1c891 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 d_stack_vars {.
1c892 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c893 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 *pC;. BtCur
1c894 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 sor *pCrsr;.
1c895 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d int res;. }
1c896 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bl;. struct
1c897 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b 5f 76 61 OP_Next_stack_va
1c898 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c899 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 ursor *pC;.
1c89a 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1c89b 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b ;. int res;
1c89c 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 20 20 73 . } bm;. s
1c89d 74 72 75 63 74 20 4f 50 5f 49 64 78 49 6e 73 65 truct OP_IdxInse
1c89e 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a rt_stack_vars {.
1c89f 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c8a0 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 *pC;. BtCu
1c8a1 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c8a2 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 int nKey;.
1c8a3 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c8a4 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e 3b 0a 20 Key;. } bn;.
1c8a5 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 struct OP_Idx
1c8a6 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 Delete_stack_var
1c8a7 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 s {. VdbeCu
1c8a8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 rsor *pC;.
1c8a9 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c8aa 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a . int res;.
1c8ab 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 UnpackedRe
1c8ac 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 6f cord r;. } bo
1c8ad 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c8ae 49 64 78 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 IdxRowid_stack_v
1c8af 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 43 75 ars {. BtCu
1c8b0 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c8b1 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c8b2 43 3b 0a 20 20 20 20 20 20 69 36 34 20 72 6f 77 C;. i64 row
1c8b3 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b 0a 20 20 id;. } bp;.
1c8b4 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 47 struct OP_IdxG
1c8b5 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 E_stack_vars {.
1c8b6 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c8b7 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 *pC;. int r
1c8b8 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b es;. Unpack
1c8b9 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 edRecord r;.
1c8ba 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 75 63 74 } bq;. struct
1c8bb 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 74 61 63 OP_Destroy_stac
1c8bc 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c8bd 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 20 20 20 nt iMoved;.
1c8be 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 20 20 20 int iCnt;.
1c8bf 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20 Vdbe *pVdbe;.
1c8c0 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 int iDb;.
1c8c1 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 72 75 63 } br;. struc
1c8c2 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 61 63 6b t OP_Clear_stack
1c8c3 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c8c4 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d t nChange;. }
1c8c5 20 62 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bs;. struct
1c8c6 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 5f 73 OP_CreateTable_s
1c8c7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c8c8 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 int pgno;.
1c8c9 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 20 int flags;.
1c8ca 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 Db *pDb;.
1c8cb 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 75 63 74 } bt;. struct
1c8cc 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 5f OP_ParseSchema_
1c8cd 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c8ce 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 int iDb;.
1c8cf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d const char *zM
1c8d0 61 73 74 65 72 3b 0a 20 20 20 20 20 20 63 68 61 aster;. cha
1c8d1 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20 49 r *zSql;. I
1c8d2 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 nitData initData
1c8d3 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 20 20 20 ;. } bu;.
1c8d4 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 65 67 72 struct OP_Integr
1c8d5 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 61 72 73 ityCk_stack_vars
1c8d6 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 52 6f {. int nRo
1c8d7 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ot; /* Numb
1c8d8 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 er of tables to
1c8d9 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20 check. (Number
1c8da 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20 of root pages.)
1c8db 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 52 */. int *aR
1c8dc 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 oot; /* Arra
1c8dd 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 y of rootpage nu
1c8de 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 mbers for tables
1c8df 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a to be checked *
1c8e0 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20 20 /. int j;
1c8e1 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1c8e2 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 counter */.
1c8e3 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 int nErr;
1c8e4 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 /* Number of er
1c8e5 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f rors reported */
1c8e6 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 . char *z;
1c8e7 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f /* Text o
1c8e8 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f f the error repo
1c8e9 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 rt */. Mem
1c8ea 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 *pnErr; /* R
1c8eb 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 egister keeping
1c8ec 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 track of errors
1c8ed 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 20 remaining */.
1c8ee 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 72 75 63 } bv;. struc
1c8ef 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 5f t OP_RowSetRead_
1c8f0 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c8f1 20 20 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20 i64 val;.
1c8f2 7d 20 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74 } bw;. struct
1c8f3 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73 OP_RowSetTest_s
1c8f4 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c8f5 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20 int iSet;.
1c8f6 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 int exists;.
1c8f7 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 74 72 75 } bx;. stru
1c8f8 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74 ct OP_Program_st
1c8f9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c8fa 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 int nMem;
1c8fb 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c8fc 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 er of memory reg
1c8fd 69 73 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70 isters for sub-p
1c8fe 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 rogram */.
1c8ff 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 int nByte;
1c900 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
1c901 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 of runtime spac
1c902 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 e required for s
1c903 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 ub-program */.
1c904 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 Mem *pRt;
1c905 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1c906 65 67 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 egister to alloc
1c907 61 74 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 ate runtime spac
1c908 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a e */. Mem *
1c909 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 pMem;
1c90a 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 /* Used to it
1c90b 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 erate through me
1c90c 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 mory cells */.
1c90d 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 Mem *pEnd;
1c90e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c90f 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 ast memory cell
1c910 69 6e 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a in new array */.
1c911 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 VdbeFrame
1c912 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a *pFrame; /*
1c913 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 New vdbe frame
1c914 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f to execute in */
1c915 0a 20 20 20 20 20 20 53 75 62 50 72 6f 67 72 61 . SubProgra
1c916 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f m *pProgram; /
1c917 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f * Sub-program to
1c918 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20 execute */.
1c919 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 void *t;
1c91a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b /* Tok
1c91b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 en identifying t
1c91c 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20 rigger */. }
1c91d 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f by;. struct O
1c91e 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61 P_Param_stack_va
1c91f 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46 rs {. VdbeF
1c920 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 rame *pFrame;.
1c921 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20 Mem *pIn;.
1c922 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74 72 75 } bz;. stru
1c923 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61 ct OP_MemMax_sta
1c924 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c925 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20 Mem *pIn1;.
1c926 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 VdbeFrame *pFra
1c927 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20 me;. } ca;.
1c928 20 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53 struct OP_AggS
1c929 74 65 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b tep_stack_vars {
1c92a 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 . int n;.
1c92b 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
1c92c 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 Mem *pMem;.
1c92d 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20 Mem *pRec;.
1c92e 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 sqlite3_conte
1c92f 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 xt ctx;. sq
1c930 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 lite3_value **ap
1c931 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 Val;. } cb;.
1c932 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 struct OP_Agg
1c933 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 Final_stack_vars
1c934 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d {. Mem *pM
1c935 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20 em;. } cc;.
1c936 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72 struct OP_Incr
1c937 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72 Vacuum_stack_var
1c938 73 20 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 s {. Btree
1c939 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 64 3b 0a *pBt;. } cd;.
1c93a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42 struct OP_VB
1c93b 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 egin_stack_vars
1c93c 7b 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a {. VTable *
1c93d 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 63 65 3b pVTab;. } ce;
1c93e 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 . struct OP_V
1c93f 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 Open_stack_vars
1c940 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c941 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 or *pCur;.
1c942 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
1c943 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 sor *pVtabCursor
1c944 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1c945 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 vtab *pVtab;.
1c946 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c sqlite3_modul
1c947 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 e *pModule;.
1c948 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 75 63 74 } cf;. struct
1c949 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63 OP_VFilter_stac
1c94a 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c94b 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 nt nArg;. i
1c94c 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20 nt iQuery;.
1c94d 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1c94e 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1c94f 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72 Mem *pQuer
1c950 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41 y;. Mem *pA
1c951 72 67 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 rgc;. sqlit
1c952 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a e3_vtab_cursor *
1c953 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 pVtabCursor;.
1c954 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 sqlite3_vtab
1c955 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64 *pVtab;. Vd
1c956 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a beCursor *pCur;.
1c957 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c958 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1c959 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 Mem **apArg;.
1c95a 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 73 74 72 } cg;. str
1c95b 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73 uct OP_VColumn_s
1c95c 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c95d 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1c95e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e pVtab;. con
1c95f 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1c960 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 e *pModule;.
1c961 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 Mem *pDest;.
1c962 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 sqlite3_cont
1c963 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 ext sContext;.
1c964 20 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 } ch;. stru
1c965 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63 ct OP_VNext_stac
1c966 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 k_vars {. s
1c967 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1c968 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ab;. const
1c969 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1c96a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 pModule;. i
1c96b 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64 nt res;. Vd
1c96c 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a beCursor *pCur;.
1c96d 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 20 73 74 } ci;. st
1c96e 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f ruct OP_VRename_
1c96f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c970 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 sqlite3_vtab
1c971 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65 *pVtab;. Me
1c972 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 m *pName;. }
1c973 63 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cj;. struct O
1c974 50 5f 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f P_VUpdate_stack_
1c975 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c vars {. sql
1c976 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1c977 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1c978 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1c979 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b . int nArg;
1c97a 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1c97b 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 sqlite_int64
1c97c 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65 rowid;. Me
1c97d 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20 m **apArg;.
1c97e 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20 Mem *pX;. }
1c97f 63 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ck;. struct O
1c980 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63 P_Pagecount_stac
1c981 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c982 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 nt p1;. int
1c983 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61 nPage;. Pa
1c984 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 ger *pPager;.
1c985 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 72 75 63 } cl;. struc
1c986 74 20 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b t OP_Trace_stack
1c987 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 _vars {. ch
1c988 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 ar *zTrace;.
1c989 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f } cm;. } u;. /
1c98a 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 * End automatica
1c98b 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f lly generated co
1c98c 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a de. ***********
1c98d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c98e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c98f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 *********/.. as
1c991 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1c992 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 VDBE_MAGIC_RUN )
1c993 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 ; /* sqlite3_st
1c994 65 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68 ep() verifies th
1c995 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 is */. assert(
1c996 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
1c997 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a E_MAGIC_BUSY );.
1c998 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 sqlite3VdbeMut
1c999 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b exArrayEnter(p);
1c99a 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 . if( p->rc==SQ
1c99b 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 LITE_NOMEM ){.
1c99c 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
1c99d 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 s if a malloc()
1c99e 69 6e 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f inside a call to
1c99f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1c9a0 74 65 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a text() or. **
1c9a1 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1c9a2 74 65 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e text16() failed.
1c9a3 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f */. goto no
1c9a4 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 _mem;. }. asse
1c9a5 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 rt( p->rc==SQLIT
1c9a6 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 E_OK || p->rc==S
1c9a7 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 QLITE_BUSY );.
1c9a8 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
1c9a9 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e K;. assert( p->
1c9aa 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 explain==0 );.
1c9ab 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 p->pResultSet =
1c9ac 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 0;. db->busyHan
1c9ad 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a dler.nBusy = 0;.
1c9ae 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1c9af 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 RRUPT;. sqlite3
1c9b0 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 VdbeIOTraceSql(p
1c9b1 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1c9b2 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f E_OMIT_PROGRESS_
1c9b3 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68 65 63 6b CALLBACK. check
1c9b4 50 72 6f 67 72 65 73 73 20 3d 20 64 62 2d 3e 78 Progress = db->x
1c9b5 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a 23 65 6e Progress!=0;.#en
1c9b6 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
1c9b7 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 E_DEBUG. sqlite
1c9b8 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1c9b9 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 oc();. if( p->p
1c9ba 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 70 2d c==0 . && ((p-
1c9bb 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c >db->flags & SQL
1c9bc 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 ITE_VdbeListing)
1c9bd 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 28 64 || fileExists(d
1c9be 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 69 6e b, "vdbe_explain
1c9bf 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 ")). ){. int
1c9c0 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 i;. printf("
1c9c1 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 VDBE Program Lis
1c9c2 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 ting:\n");. s
1c9c3 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 qlite3VdbePrintS
1c9c4 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 ql(p);. for(i
1c9c5 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b =0; i<p->nOp; i+
1c9c6 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
1c9c7 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 3VdbePrintOp(std
1c9c8 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b 69 5d 29 out, i, &aOp[i])
1c9c9 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1c9ca 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c ( fileExists(db,
1c9cb 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29 "vdbe_trace") )
1c9cc 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d {. p->trace =
1c9cd 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 stdout;. }. s
1c9ce 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1c9cf 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a alloc();.#endif.
1c9d0 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20 for(pc=p->pc;
1c9d1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 rc==SQLITE_OK; p
1c9d2 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 c++){. assert
1c9d3 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d ( pc>=0 && pc<p-
1c9d4 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20 >nOp );. if(
1c9d5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1c9d6 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1c9d7 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1c9d8 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d ILE. origPc =
1c9d9 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d pc;. start =
1c9da 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
1c9db 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 ;.#endif. pOp
1c9dc 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 = &aOp[pc];..
1c9dd 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 /* Only allow
1c9de 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54 tracing if SQLIT
1c9df 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 6e E_DEBUG is defin
1c9e0 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 ed.. */.#ifde
1c9e1 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1c9e2 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 if( p->trace
1c9e3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d ){. if( pc=
1c9e4 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 =0 ){. pr
1c9e5 69 6e 74 66 28 22 56 44 42 45 20 45 78 65 63 75 intf("VDBE Execu
1c9e6 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b tion Trace:\n");
1c9e7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c9e8 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b VdbePrintSql(p);
1c9e9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
1c9ea 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
1c9eb 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20 p(p->trace, pc,
1c9ec 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 pOp);. }.
1c9ed 69 66 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20 if( p->trace==0
1c9ee 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 && pc==0 ){.
1c9ef 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1c9f0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1c9f1 20 20 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 if( fileExis
1c9f2 74 73 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c ts(db, "vdbe_sql
1c9f3 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20 trace") ){.
1c9f4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 sqlite3VdbePr
1c9f5 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 intSql(p);.
1c9f6 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1c9f7 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
1c9f8 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1c9f9 20 20 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 .. /* C
1c9fa 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 heck to see if w
1c9fb 65 20 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61 e need to simula
1c9fc 74 65 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e te an interrupt.
1c9fd 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 This only happ
1c9fe 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65 ens. ** if we
1c9ff 20 68 61 76 65 20 61 20 73 70 65 63 69 61 6c 20 have a special
1ca00 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20 test build..
1ca01 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1ca02 5f 54 45 53 54 0a 20 20 20 20 69 66 28 20 73 71 _TEST. if( sq
1ca03 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f lite3_interrupt_
1ca04 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 count>0 ){.
1ca05 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1ca06 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 pt_count--;.
1ca07 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
1ca08 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 terrupt_count==0
1ca09 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1ca0a 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62 te3_interrupt(db
1ca0b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1ca0c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1ca0d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f SQLITE_OMIT_PRO
1ca0e 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 GRESS_CALLBACK.
1ca0f 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 /* Call the p
1ca10 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1ca11 20 69 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 if it is config
1ca12 75 72 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 ured and the req
1ca13 75 69 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20 uired number.
1ca14 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 ** of VDBE ops
1ca15 68 61 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 have been execut
1ca16 65 64 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 ed (either since
1ca17 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e this invocation
1ca18 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 of. ** sqlit
1ca19 65 33 56 64 62 65 45 78 65 63 28 29 20 6f 72 20 e3VdbeExec() or
1ca1a 73 69 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20 since last time
1ca1b 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1ca1c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64 lback was called
1ca1d 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 ).. ** If the
1ca1e 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
1ca1f 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a ck returns non-z
1ca20 65 72 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69 ero, exit the vi
1ca21 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69 rtual machine wi
1ca22 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75 th. ** a retu
1ca23 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41 rn code SQLITE_A
1ca24 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 BORT.. */.
1ca25 20 69 66 28 20 63 68 65 63 6b 50 72 6f 67 72 65 if( checkProgre
1ca26 73 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ss ){. if(
1ca27 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 db->nProgressOps
1ca28 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29 ==nProgressOps )
1ca29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 70 72 {. int pr
1ca2a 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 c;. if( s
1ca2b 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1ca2c 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1ca2d 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1ca2e 20 20 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d prc =db-
1ca2f 3e 78 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70 >xProgress(db->p
1ca30 50 72 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20 ProgressArg);.
1ca31 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1ca32 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1ca33 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1ca34 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 o_misuse;.
1ca35 20 20 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a if( prc!=0 ){.
1ca36 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
1ca37 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b QLITE_INTERRUPT;
1ca38 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1ca39 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b vdbe_error_halt;
1ca3a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1ca3b 20 20 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 nProgressOps
1ca3c 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1ca3d 20 20 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b nProgressOps+
1ca3e 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a +;. }.#endif.
1ca3f 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e 79 20 6f . /* On any o
1ca40 70 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 22 pcode with the "
1ca41 6f 75 74 32 2d 70 72 65 72 65 6c 61 73 65 22 20 out2-prerelase"
1ca42 74 61 67 2c 20 66 72 65 65 20 61 6e 79 0a 20 20 tag, free any.
1ca43 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c 20 61 6c ** external al
1ca44 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74 20 6f 66 locations out of
1ca45 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20 73 65 74 mem[p2] and set
1ca46 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62 65 0a 20 mem[p2] to be.
1ca47 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65 66 69 6e ** an undefin
1ca48 65 64 20 69 6e 74 65 67 65 72 2e 20 20 4f 70 63 ed integer. Opc
1ca49 6f 64 65 73 20 77 69 6c 6c 20 65 69 74 68 65 72 odes will either
1ca4a 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 69 6e 74 fill in the int
1ca4b 65 67 65 72 0a 20 20 20 20 2a 2a 20 76 61 6c 75 eger. ** valu
1ca4c 65 20 6f 72 20 63 6f 6e 76 65 72 74 20 6d 65 6d e or convert mem
1ca4d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66 66 65 72 [p2] to a differ
1ca4e 65 6e 74 20 74 79 70 65 2e 0a 20 20 20 20 2a 2f ent type.. */
1ca4f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1ca50 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71 6c 69 74 ->opflags==sqlit
1ca51 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 e3OpcodeProperty
1ca52 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 20 29 3b [pOp->opcode] );
1ca53 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 . if( pOp->op
1ca54 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 flags & OPFLG_OU
1ca55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b T2_PRERELEASE ){
1ca56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1ca57 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 Op->p2>0 );.
1ca58 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ca59 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
1ca5a 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d pOut = &aMem
1ca5b 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1ca5c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1ca5d 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
1ca5e 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 Out);. pOut
1ca5f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1ca60 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a t;. }.. /*
1ca61 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 Sanity checking
1ca62 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65 72 61 6e on other operan
1ca63 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ds */.#ifdef SQL
1ca64 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 ITE_DEBUG. if
1ca65 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 ( (pOp->opflags
1ca66 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20 & OPFLG_IN1)!=0
1ca67 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1ca68 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 pOp->p1>0 );.
1ca69 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1ca6a 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1ca6b 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 REGISTER_T
1ca6c 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 26 61 RACE(pOp->p1, &a
1ca6d 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 Mem[pOp->p1]);.
1ca6e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f }. if( (pO
1ca6f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 p->opflags & OPF
1ca70 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 LG_IN2)!=0 ){.
1ca71 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1ca72 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p2>0 );. a
1ca73 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d ssert( pOp->p2<=
1ca74 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1ca75 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1ca76 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 pOp->p2, &aMem[p
1ca77 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 7d 0a Op->p2]);. }.
1ca78 20 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 if( (pOp->op
1ca79 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e flags & OPFLG_IN
1ca7a 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 3)!=0 ){. a
1ca7b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1ca7c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1ca7d 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d ( pOp->p3<=p->nM
1ca7e 65 6d 20 29 3b 0a 20 20 20 20 20 20 52 45 47 49 em );. REGI
1ca7f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1ca80 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 p3, &aMem[pOp->p
1ca81 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 3]);. }. i
1ca82 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 f( (pOp->opflags
1ca83 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 29 21 3d & OPFLG_OUT2)!=
1ca84 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1ca85 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a t( pOp->p2>0 );.
1ca86 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1ca87 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
1ca88 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1ca89 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 (pOp->opflags &
1ca8a 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 OPFLG_OUT3)!=0 )
1ca8b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1ca8c 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 pOp->p3>0 );.
1ca8d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1ca8e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1ca8f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 }.#endif. .
1ca90 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e switch( pOp->
1ca91 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a opcode ){../****
1ca92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca96 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 *********.** Wha
1ca97 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d t follows is a m
1ca98 61 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 assive switch st
1ca99 61 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 atement where ea
1ca9a 63 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e ch case implemen
1ca9b 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 ts a.** separate
1ca9c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 instruction in
1ca9d 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
1ca9e 69 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c ine. If we foll
1ca9f 6f 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 ow the usual.**
1caa0 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 indentation conv
1caa1 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 entions, each ca
1caa2 73 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 se should be ind
1caa3 65 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 ented by 6 space
1caa4 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 s. But.** that
1caa5 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 is a lot of wast
1caa6 65 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 ed space on the
1caa7 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f left margin. So
1caa8 20 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e the code within
1caa9 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 .** the switch s
1caaa 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 tatement will br
1caab 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 eak with convent
1caac 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 ion and be flush
1caad 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a -left. Another.*
1caae 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 * big comment (s
1caaf 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f imilar to this o
1cab0 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 ne) will mark th
1cab1 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 e point in the c
1cab2 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 ode where.** we
1cab3 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 transition back
1cab4 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 to normal indent
1cab5 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
1cab6 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 formatting of e
1cab7 61 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f ach case is impo
1cab8 72 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 rtant. The make
1cab9 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a file for SQLite.
1caba 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f ** generates two
1cabb 20 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 C files "opcode
1cabc 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 s.h" and "opcode
1cabd 73 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 s.c" by scanning
1cabe 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f this.** file lo
1cabf 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 oking for lines
1cac0 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 that begin with
1cac1 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 "case OP_". The
1cac2 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 opcodes.h files
1cac3 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c .** will be fill
1cac4 65 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 ed with #defines
1cac5 20 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 that give uniqu
1cac6 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
1cac7 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f to each.** opco
1cac8 64 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 de and the opcod
1cac9 65 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c es.c file is fil
1caca 6c 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 led with an arra
1cacb 79 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 y of strings whe
1cacc 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e re.** each strin
1cacd 67 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 g is the symboli
1cace 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 c name for the c
1cacf 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 orresponding opc
1cad0 6f 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ode. If the.**
1cad1 63 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 case statement i
1cad2 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 s followed by a
1cad3 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 comment of the f
1cad4 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 orm "/# same as
1cad5 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 ... #/".** that
1cad6 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 comment is used
1cad7 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
1cad8 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 particular valu
1cad9 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e e of the opcode.
1cada 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 .**.** Other key
1cadb 77 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d words in the com
1cadc 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 ment that follow
1cadd 73 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 s each case are
1cade 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 used to.** const
1cadf 72 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 ruct the OPFLG_I
1cae0 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 NITIALIZER value
1cae1 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 that initialize
1cae2 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 s opcodeProperty
1cae3 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 []..** Keywords
1cae4 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e include: in1, in
1cae5 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 2, in3, out2_pre
1cae6 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f release, out2, o
1cae7 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 ut3. See.** the
1cae8 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 mkopcodeh.awk s
1cae9 63 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 cript for additi
1caea 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1caeb 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 ..**.** Document
1caec 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 ation about VDBE
1caed 20 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 opcodes is gene
1caee 72 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e rated by scannin
1caef 67 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 g this file.** f
1caf0 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 or lines of that
1caf1 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 contain "Opcode
1caf2 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 :". That line a
1caf3 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e nd all subsequen
1caf4 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e t.** comment lin
1caf5 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 es are used in t
1caf6 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 he generation of
1caf7 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c the opcode.html
1caf8 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a documentation.*
1caf9 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 * file..**.** SU
1cafa 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 MMARY:.**.**
1cafb 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 Formatting is i
1cafc 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 mportant to scri
1cafd 70 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 pts that scan th
1cafe 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 is file..**
1caff 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 Do not deviate f
1cb00 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 rom the formatti
1cb01 6e 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 ng style current
1cb02 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a ly in use..**.**
1cb03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cb04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cb05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cb06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cb07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
1cb08 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a Opcode: Goto *
1cb09 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1cb0a 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c An unconditional
1cb0b 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 jump to address
1cb0c 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 P2..** The next
1cb0d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 instruction exe
1cb0e 63 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a cuted will be .*
1cb0f 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 * the one at ind
1cb10 65 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 ex P2 from the b
1cb11 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 eginning of.** t
1cb12 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 he program..*/.c
1cb13 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 ase OP_Goto: {
1cb14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1cb15 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f mp */. CHECK_FO
1cb16 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 R_INTERRUPT;. p
1cb17 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1cb18 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cb19 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 Opcode: Gosub P
1cb1a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1cb1b 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 Write the curre
1cb1c 6e 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 nt address onto
1cb1d 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 register P1.** a
1cb1e 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 nd then jump to
1cb1f 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 address P2..*/.c
1cb20 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 ase OP_Gosub: {
1cb21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1cb22 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e mp, in1 */. pIn
1cb23 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 1 = &aMem[pOp->p
1cb24 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 1];. assert( (p
1cb25 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1cb26 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 _Dyn)==0 );. pI
1cb27 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f n1->flags = MEM_
1cb28 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 Int;. pIn1->u.i
1cb29 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 = pc;. REGISTE
1cb2a 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1cb2b 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 pIn1);. pc = p
1cb2c 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 Op->p2 - 1;. br
1cb2d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cb2e 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 2a 20 e: Return P1 *
1cb2f 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 * * *.**.** Jump
1cb30 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1cb31 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 20 74 truction after t
1cb32 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 72 65 he address in re
1cb33 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 gister P1..*/.ca
1cb34 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20 se OP_Return: {
1cb35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 /* in1
1cb36 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d */. pIn1 = &aM
1cb37 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 em[pOp->p1];. a
1cb38 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1cb39 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a gs & MEM_Int );.
1cb3a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 pc = (int)pIn1
1cb3b 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a ->u.i;. break;.
1cb3c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 }../* Opcode: Y
1cb3d 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a ield P1 * * * *.
1cb3e 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 **.** Swap the p
1cb3f 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 rogram counter w
1cb40 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e ith the value in
1cb41 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f register P1..*/
1cb42 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 .case OP_Yield:
1cb43 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1cb44 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a in1 */.#if 0 /*
1cb45 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1cb46 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 moved into u.aa
1cb47 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65 73 74 */. int pcDest
1cb48 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1cb49 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1cb4a 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 d into u.aa */.
1cb4b 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn1 = &aMem[pO
1cb4c 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1cb4d 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1cb4e 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a MEM_Dyn)==0 );.
1cb4f 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 pIn1->flags =
1cb50 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e MEM_Int;. u.aa.
1cb51 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49 pcDest = (int)pI
1cb52 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d n1->u.i;. pIn1-
1cb53 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 >u.i = pc;. REG
1cb54 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1cb55 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 >p1, pIn1);. pc
1cb56 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a = u.aa.pcDest;.
1cb57 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cb58 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 pcode: HaltIfNu
1cb59 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20 ll P1 P2 P3 P4
1cb5a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 *.**.** Check th
1cb5b 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1cb5c 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69 ter P3. If is i
1cb5d 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 s NULL then Halt
1cb5e 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 using.** parame
1cb5f 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 ter P1, P2, and
1cb60 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65 P4 as if this we
1cb61 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75 re a Halt instru
1cb62 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a ction. If the.*
1cb63 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 * value in regis
1cb64 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 ter P3 is not NU
1cb65 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f LL, then this ro
1cb66 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
1cb67 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c ..*/.case OP_Hal
1cb68 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 tIfNull: {
1cb69 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70 49 6e 33 /* in3 */. pIn3
1cb6a 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1cb6b 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 33 2d 3e ];. if( (pIn3->
1cb6c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1cb6d 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 )==0 ) break;.
1cb6e 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
1cb6f 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a into OP_Halt */.
1cb70 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 }../* Opcode: H
1cb71 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a alt P1 P2 * P4 *
1cb72 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 .**.** Exit imme
1cb73 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 diately. All op
1cb74 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20 en cursors, etc
1cb75 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 are closed.** au
1cb76 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a tomatically..**.
1cb77 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73 ** P1 is the res
1cb78 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 ult code returne
1cb79 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 d by sqlite3_exe
1cb7a 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 c(), sqlite3_res
1cb7b 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 et(),.** or sqli
1cb7c 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 te3_finalize().
1cb7d 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 For a normal ha
1cb7e 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 lt, this should
1cb7f 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 be SQLITE_OK (0)
1cb80 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c ..** For errors,
1cb81 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 it can be some
1cb82 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 other value. If
1cb83 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 P1!=0 then P2 w
1cb84 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a ill determine.**
1cb85 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1cb86 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
1cb87 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
1cb88 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c ion. Do not rol
1cb89 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d lback.** if P2==
1cb8a 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 OE_Fail. Do the
1cb8b 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d rollback if P2==
1cb8c 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 OE_Rollback. If
1cb8d 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a P2==OE_Abort,.*
1cb8e 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 * then back out
1cb8f 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 all changes that
1cb90 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 have occurred d
1cb91 75 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 uring this execu
1cb92 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 tion of the.** V
1cb93 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 DBE, but do not
1cb94 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 rollback the tra
1cb95 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a nsaction. .**.**
1cb96 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 If P4 is not nu
1cb97 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e ll then it is an
1cb98 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 error message s
1cb99 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tring..**.** The
1cb9a 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 re is an implied
1cb9b 20 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e "Halt 0 0 0" in
1cb9c 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 struction insert
1cb9d 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 ed at the very e
1cb9e 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 nd of.** every p
1cb9f 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 rogram. So a ju
1cba0 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 mp past the last
1cba1 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
1cba2 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 the program.** i
1cba3 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 s the same as ex
1cba4 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f ecuting Halt..*/
1cba5 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b .case OP_Halt: {
1cba6 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d . if( pOp->p1==
1cba7 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e SQLITE_OK && p->
1cba8 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a pFrame ){. /*
1cba9 20 48 61 6c 74 20 74 68 65 20 73 75 62 2d 70 72 Halt the sub-pr
1cbaa 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e 20 63 6f ogram. Return co
1cbab 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 70 61 72 ntrol to the par
1cbac 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 ent frame. */.
1cbad 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 VdbeFrame *pFr
1cbae 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b ame = p->pFrame;
1cbaf 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d . p->pFrame =
1cbb0 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 pFrame->pParent
1cbb1 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d 65 2d ;. p->nFrame-
1cbb2 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 -;. sqlite3Vd
1cbb3 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c beSetChanges(db,
1cbb4 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 p->nChange);.
1cbb5 20 20 70 63 20 3d 20 73 71 6c 69 74 65 33 56 64 pc = sqlite3Vd
1cbb6 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 beFrameRestore(p
1cbb7 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 Frame);. if(
1cbb8 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f pOp->p2==OE_Igno
1cbb9 72 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 re ){. /* I
1cbba 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 20 69 73 nstruction pc is
1cbbb 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 the OP_Program
1cbbc 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 that invoked the
1cbbd 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a 20 20 sub-program .
1cbbe 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79 ** currently
1cbbf 20 62 65 69 6e 67 20 68 61 6c 74 65 64 2e 20 49 being halted. I
1cbc0 66 20 74 68 65 20 70 32 20 69 6e 73 74 72 75 63 f the p2 instruc
1cbc1 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f 50 5f tion of this OP_
1cbc2 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e Halt. ** in
1cbc3 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73 65 74 struction is set
1cbc4 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 to OE_Ignore, t
1cbc5 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f 67 hen the sub-prog
1cbc6 72 61 6d 20 69 73 20 74 68 72 6f 77 69 6e 67 0a ram is throwing.
1cbc7 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f ** an IGNO
1cbc8 52 45 20 65 78 63 65 70 74 69 6f 6e 2e 20 49 6e RE exception. In
1cbc9 20 74 68 69 73 20 63 61 73 65 20 6a 75 6d 70 20 this case jump
1cbca 74 6f 20 74 68 65 20 61 64 64 72 65 73 73 20 73 to the address s
1cbcb 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 2a pecified. *
1cbcc 2a 20 61 73 20 74 68 65 20 70 32 20 6f 66 20 74 * as the p2 of t
1cbcd 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 he calling OP_Pr
1cbce 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 ogram. */.
1cbcf 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d pc = p->aOp[pc]
1cbd0 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 .p2-1;. }.
1cbd1 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 aOp = p->aOp;.
1cbd2 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 aMem = p->aMe
1cbd3 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 m;. break;.
1cbd4 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f 70 }.. p->rc = pOp
1cbd5 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 ->p1;. p->error
1cbd6 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f 70 Action = (u8)pOp
1cbd7 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 ->p2;. p->pc =
1cbd8 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 pc;. if( pOp->p
1cbd9 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 4.z ){. sqlit
1cbda 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1cbdb 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1cbdc 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 ", pOp->p4.z);.
1cbdd 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }. rc = sqlite
1cbde 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 3VdbeHalt(p);.
1cbdf 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1cbe0 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 TE_BUSY || rc==S
1cbe1 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d QLITE_OK || rc==
1cbe2 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a SQLITE_ERROR );.
1cbe3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1cbe4 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e _BUSY ){. p->
1cbe5 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = rc = SQLITE
1cbe6 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a _BUSY;. }else{.
1cbe7 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1cbe8 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
1cbe9 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 rc==SQLITE_CONST
1cbea 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 61 73 73 RAINT );. ass
1cbeb 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1cbec 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 OK || db->nDefer
1cbed 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a 20 20 20 redCons>0 );.
1cbee 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51 rc = p->rc ? SQ
1cbef 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c LITE_ERROR : SQL
1cbf0 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
1cbf1 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1cbf2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cbf3 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20 Integer P1 P2 *
1cbf4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 * *.**.** The 32
1cbf5 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c -bit integer val
1cbf6 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e ue P1 is written
1cbf7 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1cbf8 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 2..*/.case OP_In
1cbf9 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20 teger: {
1cbfa 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1cbfb 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 75 ase */. pOut->u
1cbfc 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 .i = pOp->p1;.
1cbfd 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1cbfe 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20 ode: Int64 * P2
1cbff 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1cc00 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1cc01 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 a 64-bit integer
1cc02 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 value..** Write
1cc03 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f that value into
1cc04 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
1cc05 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20 .case OP_Int64:
1cc06 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1cc07 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1cc08 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1cc09 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20 >p4.pI64!=0 );.
1cc0a 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f pOut->u.i = *pO
1cc0b 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72 p->p4.pI64;. br
1cc0c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cc0d 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50 e: Real * P2 * P
1cc0e 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 4 *.**.** P4 is
1cc0f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 a pointer to a 6
1cc10 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 4-bit floating p
1cc11 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 oint value..** W
1cc12 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 rite that value
1cc13 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1cc14 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 ..*/.case OP_Rea
1cc15 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 l: {
1cc16 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c /* same as TK_FL
1cc17 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c OAT, out2-prerel
1cc18 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e ease */. pOut->
1cc19 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c flags = MEM_Real
1cc1a 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c ;. assert( !sql
1cc1b 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e ite3IsNaN(*pOp->
1cc1c 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70 p4.pReal) );. p
1cc1d 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70 Out->r = *pOp->p
1cc1e 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b 4.pReal;. break
1cc1f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cc20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50 String8 * P2 * P
1cc21 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 4 *.**.** P4 poi
1cc22 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 nts to a nul ter
1cc23 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
1cc24 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64 ring. This opcod
1cc25 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 e is transformed
1cc26 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f .** into an OP_
1cc27 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74 String before it
1cc28 20 69 73 20 65 78 65 63 75 74 65 64 20 66 6f 72 is executed for
1cc29 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e the first time.
1cc2a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 .*/.case OP_Stri
1cc2b 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f ng8: { /
1cc2c 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 * same as TK_STR
1cc2d 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c ING, out2-prerel
1cc2e 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 ease */. assert
1cc2f 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 ( pOp->p4.z!=0 )
1cc30 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ;. pOp->opcode
1cc31 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 = OP_String;. p
1cc32 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 Op->p1 = sqlite3
1cc33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 Strlen30(pOp->p4
1cc34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 .z);..#ifndef SQ
1cc35 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
1cc36 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d if( encoding!=
1cc37 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
1cc38 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1cc39 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 dbeMemSetStr(pOu
1cc3a 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 t, pOp->p4.z, -1
1cc3b 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
1cc3c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
1cc3d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1cc3e 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f 74 6f 20 E_TOOBIG ) goto
1cc3f 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 69 66 28 too_big;. if(
1cc40 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 SQLITE_OK!=sqli
1cc41 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1cc42 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f oding(pOut, enco
1cc43 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f ding) ) goto no_
1cc44 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 mem;. assert(
1cc45 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d pOut->zMalloc==
1cc46 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 20 20 61 pOut->z );. a
1cc47 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 66 6c 61 ssert( pOut->fla
1cc48 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a gs & MEM_Dyn );.
1cc49 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f pOut->zMallo
1cc4a 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d c = 0;. pOut-
1cc4b 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
1cc4c 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e atic;. pOut->
1cc4d 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 flags &= ~MEM_Dy
1cc4e 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e n;. if( pOp->
1cc4f 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d p4type==P4_DYNAM
1cc50 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 IC ){. sqli
1cc51 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f te3DbFree(db, pO
1cc52 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a p->p4.z);. }.
1cc53 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1cc54 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 = P4_DYNAMIC;.
1cc55 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f pOp->p4.z = pO
1cc56 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e ut->z;. pOp->
1cc57 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 p1 = pOut->n;.
1cc58 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 }.#endif. if( p
1cc59 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 Op->p1>db->aLimi
1cc5a 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1cc5b 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f ENGTH] ){. go
1cc5c 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1cc5d 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
1cc5e 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 h to the next ca
1cc5f 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f se, OP_String */
1cc60 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a .}. ./* Opcode:
1cc61 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20 String P1 P2 *
1cc62 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 P4 *.**.** The s
1cc63 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f tring value P4 o
1cc64 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74 f length P1 (byt
1cc65 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e es) is stored in
1cc66 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
1cc67 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a .case OP_String:
1cc68 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1cc69 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1cc6a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1cc6b 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f >p4.z!=0 );. pO
1cc6c 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1cc6d 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d Str|MEM_Static|M
1cc6e 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d EM_Term;. pOut-
1cc6f 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a >z = pOp->p4.z;.
1cc70 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d pOut->n = pOp-
1cc71 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 >p1;. pOut->enc
1cc72 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 = encoding;. U
1cc73 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1cc74 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 ZE(pOut);. brea
1cc75 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1cc76 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a Null * P2 * * *
1cc77 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e .**.** Write a N
1cc78 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ULL into registe
1cc79 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1cc7a 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 _Null: {
1cc7b 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1cc7c 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d lease */. pOut-
1cc7d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
1cc7e 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a l;. break;.}...
1cc7f 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20 /* Opcode: Blob
1cc80 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a P1 P2 * P4.**.**
1cc81 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 P4 points to a
1cc82 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20 blob of data P1
1cc83 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f bytes long. Sto
1cc84 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 re this.** blob
1cc85 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1cc86 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1cc87 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69 is not coded di
1cc88 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65 rectly.** by the
1cc89 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65 compiler. Inste
1cc8a 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 ad, the compiler
1cc8b 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65 73 layer specifies
1cc8c 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f .** an OP_HexBlo
1cc8d 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74 b opcode, with t
1cc8e 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72 65 he hex string re
1cc8f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a presentation of.
1cc90 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50 ** the blob as P
1cc91 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 4. This opcode i
1cc92 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f s transformed to
1cc93 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 an OP_Blob.** t
1cc94 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 he first time it
1cc95 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f is executed..*/
1cc96 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b .case OP_Blob: {
1cc97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc98 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1cc99 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 se */. assert(
1cc9a 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54 pOp->p1 <= SQLIT
1cc9b 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a E_MAX_LENGTH );.
1cc9c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cc9d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 SetStr(pOut, pOp
1cc9e 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c ->p4.z, pOp->p1,
1cc9f 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 0, 0);. pOut->
1cca0 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a enc = encoding;.
1cca1 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1cca2 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1cca3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cca4 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 de: Variable P1
1cca5 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1cca6 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61 Transfer the va
1cca7 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61 lues of bound pa
1cca8 72 61 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b rameters P1..P1+
1cca9 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74 P3-1 into regist
1ccaa 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 ers.** P2..P2+P3
1ccab 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 -1..**.** If the
1ccac 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61 parameter is na
1ccad 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61 med, then its na
1ccae 6d 65 20 61 70 70 65 61 72 73 20 69 6e 20 50 34 me appears in P4
1ccaf 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 and P3==1..** T
1ccb0 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 75 he P4 value is u
1ccb1 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 sed by sqlite3_b
1ccb2 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 ind_parameter_na
1ccb3 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 me()..*/.case OP
1ccb4 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66 _Variable: {.#if
1ccb5 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1ccb6 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ccb7 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20 o u.ab */. int
1ccb8 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p1; /*
1ccb9 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 Variable to copy
1ccba 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 from */. int p
1ccbb 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2; /* R
1ccbc 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 egister to copy
1ccbd 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 to */. int n;
1ccbe 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1ccbf 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 er of values lef
1ccc0 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d t to copy */. M
1ccc1 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 em *pVar;
1ccc2 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 /* Value being t
1ccc3 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65 ransferred */.#e
1ccc4 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ccc5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ccc6 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e to u.ab */.. u.
1ccc7 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20 ab.p1 = pOp->p1
1ccc8 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d - 1;. u.ab.p2 =
1ccc9 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62 pOp->p2;. u.ab
1ccca 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 .n = pOp->p3;.
1cccb 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e assert( u.ab.p1>
1cccc 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e =0 && u.ab.p1+u.
1cccd 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b ab.n<=p->nVar );
1ccce 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e . assert( u.ab.
1cccf 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32 p2>=1 && u.ab.p2
1ccd0 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d +u.ab.n-1<=p->nM
1ccd1 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 em );. assert(
1ccd2 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 pOp->p4.z==0 ||
1ccd3 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c 20 70 4f pOp->p3==1 || pO
1ccd4 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a 20 20 77 p->p3==0 );.. w
1ccd5 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d 20 3e hile( u.ab.n-- >
1ccd6 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 62 2e 70 0 ){. u.ab.p
1ccd7 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 75 Var = &p->aVar[u
1ccd8 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20 20 69 .ab.p1++];. i
1ccd9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
1ccda 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e 70 56 61 mTooBig(u.ab.pVa
1ccdb 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f r) ){. goto
1ccdc 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a too_big;. }.
1ccdd 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d pOut = &aMem
1ccde 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20 [u.ab.p2++];.
1ccdf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1cce0 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
1cce1 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e Out);. pOut->
1cce2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1cce3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1cce4 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 eMemShallowCopy(
1cce5 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 61 72 2c pOut, u.ab.pVar,
1cce6 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 MEM_Static);.
1cce7 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1cce8 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 7d BSIZE(pOut);. }
1cce9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ccea 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20 Opcode: Move P1
1cceb 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1ccec 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73 20 Move the values
1cced 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 2e in register P1..
1ccee 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e 74 P1+P3-1 over int
1ccef 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 50 o.** registers P
1ccf0 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65 67 2..P2+P3-1. Reg
1ccf1 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 31 isters P1..P1+P1
1ccf2 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68 -1 are.** left h
1ccf3 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 olding a NULL.
1ccf4 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 It is an error f
1ccf5 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e 67 or register rang
1ccf6 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d es.** P1..P1+P3-
1ccf7 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d 1 and P2..P2+P3-
1ccf8 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f 1 to overlap..*/
1ccf9 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b .case OP_Move: {
1ccfa 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ccfb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ccfc 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 20 20 into u.ac */.
1ccfd 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 char *zMalloc;
1ccfe 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 /* Holding vari
1ccff 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
1cd00 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 ed memory */. i
1cd01 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 nt n;
1cd02 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 /* Number of reg
1cd03 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 63 isters left to c
1cd04 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b opy */. int p1;
1cd05 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1cd06 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66 72 ister to copy fr
1cd07 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 om */. int p2;
1cd08 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 /* Regi
1cd09 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 ster to copy to
1cd0a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1cd0b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cd0c 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a ed into u.ac */.
1cd0d 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d . u.ac.n = pOp-
1cd0e 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 31 20 3d >p3;. u.ac.p1 =
1cd0f 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 63 pOp->p1;. u.ac
1cd10 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 .p2 = pOp->p2;.
1cd11 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 6e 3e assert( u.ac.n>
1cd12 30 20 26 26 20 75 2e 61 63 2e 70 31 3e 30 20 26 0 && u.ac.p1>0 &
1cd13 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 3b 0a 20 & u.ac.p2>0 );.
1cd14 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 70 31 assert( u.ac.p1
1cd15 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32 +u.ac.n<=u.ac.p2
1cd16 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 2e 61 63 || u.ac.p2+u.ac
1cd17 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 3b 0a 0a .n<=u.ac.p1 );..
1cd18 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 75 pIn1 = &aMem[u
1cd19 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 .ac.p1];. pOut
1cd1a 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e 70 32 5d = &aMem[u.ac.p2]
1cd1b 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 63 2e ;. while( u.ac.
1cd1c 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 n-- ){. asser
1cd1d 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65 6d 5b 70 t( pOut<=&aMem[p
1cd1e 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 ->nMem] );. a
1cd1f 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26 61 4d ssert( pIn1<=&aM
1cd20 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 em[p->nMem] );.
1cd21 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 20 u.ac.zMalloc
1cd22 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b = pOut->zMalloc;
1cd23 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c . pOut->zMall
1cd24 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 oc = 0;. sqli
1cd25 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 te3VdbeMemMove(p
1cd26 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 Out, pIn1);.
1cd27 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 pIn1->zMalloc =
1cd28 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 u.ac.zMalloc;.
1cd29 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cd2a 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 4f 75 74 (u.ac.p2++, pOut
1cd2b 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 );. pIn1++;.
1cd2c 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20 pOut++;. }.
1cd2d 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cd2e 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 32 code: Copy P1 P2
1cd2f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b * * *.**.** Mak
1cd30 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 69 e a copy of regi
1cd31 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 ster P1 into reg
1cd32 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1cd33 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1cd34 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 6f makes a deep co
1cd35 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e py of the value.
1cd36 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a A duplicate.**
1cd37 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 is made of any
1cd38 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 string or blob c
1cd39 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 6c onstant. See al
1cd3a 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a so OP_SCopy..*/.
1cd3b 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 20 case OP_Copy: {
1cd3c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1cd3d 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 70 49 n1, out2 */. pI
1cd3e 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n1 = &aMem[pOp->
1cd3f 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 p1];. pOut = &a
1cd40 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1cd41 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 assert( pOut!=pI
1cd42 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 n1 );. sqlite3V
1cd43 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
1cd44 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 y(pOut, pIn1, ME
1cd45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 M_Ephem);. Deep
1cd46 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 hemeralize(pOut)
1cd47 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1cd48 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 CE(pOp->p2, pOut
1cd49 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1cd4a 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 * Opcode: SCopy
1cd4b 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1cd4c 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 * Make a shallow
1cd4d 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 copy of registe
1cd4e 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 r P1 into regist
1cd4f 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 er P2..**.** Thi
1cd50 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 s instruction ma
1cd51 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f kes a shallow co
1cd52 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e py of the value.
1cd53 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1cd54 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 * is a string or
1cd55 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 blob, then the
1cd56 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 copy is only a p
1cd57 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a ointer to the.**
1cd58 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 original and he
1cd59 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 nce if the origi
1cd5a 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 nal changes so w
1cd5b 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a ill the copy..**
1cd5c 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f Worse, if the o
1cd5d 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c riginal is deall
1cd5e 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 ocated, the copy
1cd5f 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 becomes invalid
1cd60 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 ..** Thus the pr
1cd61 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 ogram must guara
1cd62 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 ntee that the or
1cd63 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 iginal will not
1cd64 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 change.** during
1cd65 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
1cd66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 the copy. Use
1cd67 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 OP_Copy to make
1cd68 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f a complete.** co
1cd69 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 py..*/.case OP_S
1cd6a 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 Copy: {
1cd6b 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75 74 32 20 /* in1, out2
1cd6c 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1cd6d 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f m[pOp->p1];. pO
1cd6e 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e ut = &aMem[pOp->
1cd6f 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p2];. assert( p
1cd70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73 Out!=pIn1 );. s
1cd71 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1cd72 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 llowCopy(pOut, p
1cd73 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b In1, MEM_Ephem);
1cd74 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1cd75 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1cd76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cd77 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c 74 52 Opcode: ResultR
1cd78 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ow P1 P2 * * *.*
1cd79 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 *.** The registe
1cd7a 72 73 20 50 31 20 74 68 72 6f 75 67 68 20 50 31 rs P1 through P1
1cd7b 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20 61 20 +P2-1 contain a
1cd7c 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a single row of.**
1cd7d 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73 20 6f results. This o
1cd7e 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 pcode causes the
1cd7f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
1cd80 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e 61 74 call to terminat
1cd81 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53 51 4c e.** with an SQL
1cd82 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e 20 63 ITE_ROW return c
1cd83 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74 73 20 ode and it sets
1cd84 75 70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 up the sqlite3_s
1cd85 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 tmt.** structure
1cd86 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63 63 65 to provide acce
1cd87 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20 50 31 ss to the top P1
1cd88 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 72 values as the r
1cd89 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f esult.** row..*/
1cd8a 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c 74 52 .case OP_ResultR
1cd8b 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 ow: {.#if 0 /*
1cd8c 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cd8d 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 moved into u.ad
1cd8e 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a */. Mem *pMem;.
1cd8f 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 69 66 20 int i;.#endif
1cd90 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cd91 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cd92 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ad */. assert(
1cd93 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 p->nResColumn==p
1cd94 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 Op->p2 );. asse
1cd95 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1cd96 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1cd97 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e p1+pOp->p2<=p->n
1cd98 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 Mem+1 );.. /* I
1cd99 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 f this statement
1cd9a 20 68 61 73 20 76 69 6f 6c 61 74 65 64 20 69 6d has violated im
1cd9b 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 mediate foreign
1cd9c 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c key constraints,
1cd9d 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 74 do. ** not ret
1cd9e 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1cd9f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 65 64 2e f rows modified.
1cda0 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 45 4c 45 And do not RELE
1cda1 41 53 45 20 74 68 65 20 73 74 61 74 65 6d 65 6e ASE the statemen
1cda2 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 t. ** transacti
1cda3 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20 on. It needs to
1cda4 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
1cda5 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 */. if( SQLITE
1cda6 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
1cda7 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c e3VdbeCheckFk(p,
1cda8 20 30 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 0)) ){. asse
1cda9 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 rt( db->flags&SQ
1cdaa 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 LITE_CountRows )
1cdab 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
1cdac 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c >usesStmtJournal
1cdad 20 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 );. break;.
1cdae 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
1cdaf 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 SQLITE_CountRows
1cdb0 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 flag is set in
1cdb1 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61 sqlite3.flags ma
1cdb2 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 44 4d sk, then. ** DM
1cdb3 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76 L statements inv
1cdb4 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 oke this opcode
1cdb5 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 to return the nu
1cdb6 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 2a mber of rows. *
1cdb7 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 74 68 * modified to th
1cdb8 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 e user. This is
1cdb9 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 68 61 the only way tha
1cdba 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 2a 2a t a VM that. **
1cdbb 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 6d 65 opens a stateme
1cdbc 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d nt transaction m
1cdbd 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f ay invoke this o
1cdbe 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a pcode.. **. **
1cdbf 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 69 73 In case this is
1cdc0 20 73 75 63 68 20 61 20 73 74 61 74 65 6d 65 6e such a statemen
1cdc1 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 74 61 t, close any sta
1cdc2 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1cdc3 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 62 on. ** opened b
1cdc4 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f 72 65 y this VM before
1cdc5 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 returning contr
1cdc6 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ol to the user.
1cdc7 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a 2a 20 This is to. **
1cdc8 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 61 74 ensure that stat
1cdc9 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
1cdca 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65 ns are always ne
1cdcb 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 6c 61 sted, not overla
1cdcc 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 20 74 pping.. ** If t
1cdcd 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d 65 6e he open statemen
1cdce 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t-transaction is
1cdcf 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 72 65 not closed here
1cdd0 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 0a , then the user.
1cdd1 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 61 6e ** may step an
1cdd2 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 6f 70 other VM that op
1cdd3 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 61 74 ens its own stat
1cdd4 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1cdd5 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 61 79 n. This. ** may
1cdd6 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c 61 70 lead to overlap
1cdd7 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 ping statement t
1cdd8 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a ransactions.. *
1cdd9 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 74 65 *. ** The state
1cdda 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1cddb 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f 70 2d is never a top-
1cddc 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f level transactio
1cddd 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74 n. Hence. ** t
1cdde 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c 6c 20 he RELEASE call
1cddf 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65 72 20 below can never
1cde0 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 fail.. */. ass
1cde1 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 ert( p->iStateme
1cde2 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 nt==0 || db->fla
1cde3 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 gs&SQLITE_CountR
1cde4 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ows );. rc = sq
1cde5 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
1cde6 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 atement(p, SAVEP
1cde7 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20 OINT_RELEASE);.
1cde8 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 if( NEVER(rc!=S
1cde9 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 QLITE_OK) ){.
1cdea 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f break;. }.. /
1cdeb 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c * Invalidate all
1cdec 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f ephemeral curso
1cded 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a r row caches */.
1cdee 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 p->cacheCtr =
1cdef 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32 (p->cacheCtr + 2
1cdf0 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 )|1;.. /* Make
1cdf1 73 75 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 sure the results
1cdf2 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
1cdf3 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74 65 72 row are \000 ter
1cdf4 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64 minated. ** and
1cdf5 20 68 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65 have an assigne
1cdf6 64 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 73 d type. The res
1cdf7 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70 68 65 ults are de-ephe
1cdf8 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a meralized as. *
1cdf9 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 * as side effect
1cdfa 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e 70 4d .. */. u.ad.pM
1cdfb 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 em = p->pResultS
1cdfc 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e et = &aMem[pOp->
1cdfd 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e p1];. for(u.ad.
1cdfe 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d i=0; u.ad.i<pOp-
1cdff 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a >p2; u.ad.i++){.
1ce00 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1ce01 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 emNulTerminate(&
1ce02 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 u.ad.pMem[u.ad.i
1ce03 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ]);. sqlite3V
1ce04 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 dbeMemStoreType(
1ce05 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e &u.ad.pMem[u.ad.
1ce06 69 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 i]);. REGISTE
1ce07 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b R_TRACE(pOp->p1+
1ce08 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d u.ad.i, &u.ad.pM
1ce09 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d em[u.ad.i]);. }
1ce0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1ce0b 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e cFailed ) goto n
1ce0c 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 o_mem;.. /* Ret
1ce0d 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 urn SQLITE_ROW.
1ce0e 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 */. p->pc = pc
1ce0f 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c + 1;. rc = SQL
1ce10 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 ITE_ROW;. goto
1ce11 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a vdbe_return;.}..
1ce12 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 /* Opcode: Conca
1ce13 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
1ce14 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 *.** Add the tex
1ce15 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 t in register P1
1ce16 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 onto the end of
1ce17 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 the text in.**
1ce18 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 register P2 and
1ce19 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1ce1a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1ce1b 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 .** If either th
1ce1c 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 e P1 or P2 text
1ce1d 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 are NULL then st
1ce1e 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a ore NULL in P3..
1ce1f 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 **.** P3 = P2
1ce20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 || P1.**.** It i
1ce21 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 s illegal for P1
1ce22 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 and P3 to be th
1ce23 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e e same register.
1ce24 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 Sometimes,.** i
1ce25 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 f P3 is the same
1ce26 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c register as P2,
1ce27 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
1ce28 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 ion is able.** t
1ce29 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 o avoid a memcpy
1ce2a 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 ()..*/.case OP_C
1ce2b 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 oncat: {
1ce2c 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1ce2d 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e _CONCAT, in1, in
1ce2e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 2, out3 */.#if 0
1ce2f 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1ce30 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ce31 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 u.ae */. i64 nB
1ce32 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c yte;.#endif /* l
1ce33 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ce34 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a oved into u.ae *
1ce35 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 /.. pIn1 = &aMe
1ce36 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 m[pOp->p1];. pI
1ce37 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n2 = &aMem[pOp->
1ce38 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 p2];. pOut = &a
1ce39 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1ce3a 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f assert( pIn1!=pO
1ce3b 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e ut );. if( (pIn
1ce3c 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 1->flags | pIn2-
1ce3d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 >flags) & MEM_Nu
1ce3e 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ll ){. sqlite
1ce3f 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1ce40 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b pOut);. break
1ce41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78 70 61 ;. }. if( Expa
1ce42 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c 7c 20 ndBlob(pIn1) ||
1ce43 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 ExpandBlob(pIn2)
1ce44 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1ce45 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31 Stringify(pIn1
1ce46 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 53 , encoding);. S
1ce47 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65 tringify(pIn2, e
1ce48 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e 61 65 ncoding);. u.ae
1ce49 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e .nByte = pIn1->n
1ce4a 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 + pIn2->n;. if
1ce4b 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64 62 2d ( u.ae.nByte>db-
1ce4c 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1ce4d 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1ce4e 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1ce4f 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 ;. }. MemSetTy
1ce50 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1ce51 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c _Str);. if( sql
1ce52 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1ce53 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 65 2e pOut, (int)u.ae.
1ce54 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 nByte+2, pOut==p
1ce55 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f In2) ){. goto
1ce56 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 no_mem;. }. i
1ce57 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b f( pOut!=pIn2 ){
1ce58 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 . memcpy(pOut
1ce59 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 ->z, pIn2->z, pI
1ce5a 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 n2->n);. }. me
1ce5b 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 mcpy(&pOut->z[pI
1ce5c 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c n2->n], pIn1->z,
1ce5d 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 pIn1->n);. pOu
1ce5e 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 5d t->z[u.ae.nByte]
1ce5f 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b = 0;. pOut->z[
1ce60 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20 3d 20 u.ae.nByte+1] =
1ce61 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 0;. pOut->flags
1ce62 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
1ce63 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 pOut->n = (int)u
1ce64 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 .ae.nByte;. pOu
1ce65 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1ce66 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1ce67 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1ce68 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1ce69 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32 pcode: Add P1 P2
1ce6a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 P3 * *.**.** Ad
1ce6b 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 d the value in r
1ce6c 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68 egister P1 to th
1ce6d 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1ce6e 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 ter P2.** and st
1ce6f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1ce70 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1ce71 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1ce72 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1ce73 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1ce74 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c /./* Opcode: Mul
1ce75 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a tiply P1 P2 P3 *
1ce76 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 *.**.**.** Mult
1ce77 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69 iply the value i
1ce78 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 n register P1 by
1ce79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ce7a 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1ce7b 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1ce7c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1ce7d 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1ce7e 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1ce7f 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1ce80 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1ce81 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50 Subtract P1 P2 P
1ce82 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74 3 * *.**.** Subt
1ce83 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69 ract the value i
1ce84 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72 n register P1 fr
1ce85 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 om the value in
1ce86 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
1ce87 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1ce88 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1ce89 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1ce8a 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1ce8b 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1ce8c 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1ce8d 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50 : Divide P1 P2 P
1ce8e 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69 3 * *.**.** Divi
1ce8f 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 de the value in
1ce90 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 register P1 by t
1ce91 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1ce92 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 ster P2.** and s
1ce93 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1ce94 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 28 in register P3 (
1ce95 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 74 68 P3=P2/P1). If th
1ce96 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a 20 72 e value in .** r
1ce97 65 67 69 73 74 65 72 20 50 31 20 69 73 20 7a 65 egister P1 is ze
1ce98 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ro, then the res
1ce99 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 ult is NULL. If
1ce9a 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1ce9b 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 .** NULL, the re
1ce9c 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1ce9d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 ./* Opcode: Rema
1ce9e 69 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a inder P1 P2 P3 *
1ce9f 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 *.**.** Compute
1cea0 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 the remainder a
1cea1 66 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 fter integer div
1cea2 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c ision of the val
1cea3 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 ue in.** registe
1cea4 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 r P1 by the valu
1cea5 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1cea6 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1cea7 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a esult in P3. .**
1cea8 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1cea9 20 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20 register P2 is
1ceaa 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 zero the result
1ceab 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 is NULL..** If e
1ceac 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1cead 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1ceae 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 t is NULL..*/.ca
1ceaf 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 se OP_Add:
1ceb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ceb1 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c same as TK_PLUS,
1ceb2 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1ceb3 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 */.case OP_Subtr
1ceb4 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 act:
1ceb5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1ceb6 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c MINUS, in1, in2,
1ceb7 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1ceb8 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 _Multiply:
1ceb9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1ceba 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c as TK_STAR, in1,
1cebb 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 in2, out3 */.ca
1cebc 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 se OP_Divide:
1cebd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cebe 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 same as TK_SLASH
1cebf 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1cec0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 */.case OP_Rema
1cec1 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 inder: {
1cec2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cec3 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 _REM, in1, in2,
1cec4 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1cec5 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1cec6 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1cec7 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 f */. int flags
1cec8 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e ; /* Combin
1cec9 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 ed MEM_* flags f
1ceca 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20 rom both inputs
1cecb 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20 20 20 */. i64 iA;
1cecc 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
1cecd 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 value of left op
1cece 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 69 erand */. i64 i
1cecf 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e B; /* In
1ced0 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72 teger value of r
1ced1 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
1ced2 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20 double rA;
1ced3 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 /* Real value
1ced4 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 of left operand
1ced5 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42 3b 20 */. double rB;
1ced6 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1ced7 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 ue of right oper
1ced8 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a and */.#endif /*
1ced9 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ceda 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 66 moved into u.af
1cedb 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 */.. pIn1 = &a
1cedc 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1cedd 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 applyNumericAffi
1cede 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 nity(pIn1);. pI
1cedf 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n2 = &aMem[pOp->
1cee0 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 p2];. applyNume
1cee1 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32 ricAffinity(pIn2
1cee2 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 );. pOut = &aMe
1cee3 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e m[pOp->p3];. u.
1cee4 61 66 2e 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d af.flags = pIn1-
1cee5 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 >flags | pIn2->f
1cee6 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 75 2e 61 lags;. if( (u.a
1cee7 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 f.flags & MEM_Nu
1cee8 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 ll)!=0 ) goto ar
1cee9 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f ithmetic_result_
1ceea 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 is_null;. if( (
1ceeb 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 pIn1->flags & pI
1ceec 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n2->flags & MEM_
1ceed 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b Int)==MEM_Int ){
1ceee 0a 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 70 . u.af.iA = p
1ceef 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e In1->u.i;. u.
1cef0 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e af.iB = pIn2->u.
1cef1 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 i;. switch( p
1cef2 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 Op->opcode ){.
1cef3 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a case OP_Add:
1cef4 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 u.af.iB
1cef5 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 += u.af.iA;
1cef6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cef7 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 case OP_Subtract
1cef8 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20 : u.af.iB -=
1cef9 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 u.af.iA; b
1cefa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
1cefb 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 OP_Multiply:
1cefc 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66 u.af.iB *= u.af
1cefd 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b .iA; break
1cefe 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
1ceff 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 Divide: {.
1cf00 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 if( u.af.iA==0
1cf01 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 ) goto arithmet
1cf02 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1cf03 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 l;. /* Di
1cf04 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 viding the large
1cf05 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 st possible nega
1cf06 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65 tive 64-bit inte
1cf07 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 0a 20 ger (1<<63) by.
1cf08 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 ** -1 ret
1cf09 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 urns an integer
1cf0a 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74 6f too large to sto
1cf0b 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20 64 re in a 64-bit d
1cf0c 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 ata-type. On.
1cf0d 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 ** some arc
1cf0e 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 65 20 hitectures, the
1cf0f 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 value overflows
1cf10 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f to (1<<63). On o
1cf11 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20 2a thers,. *
1cf12 2a 20 61 20 53 49 47 46 50 45 20 69 73 20 69 73 * a SIGFPE is is
1cf13 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 sued. The follow
1cf14 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f ing statement no
1cf15 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 rmalizes this.
1cf16 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f ** behavio
1cf17 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 r so that all ar
1cf18 63 68 69 74 65 63 74 75 72 65 73 20 62 65 68 61 chitectures beha
1cf19 76 65 20 61 73 20 69 66 20 69 6e 74 65 67 65 72 ve as if integer
1cf1a 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 . ** over
1cf1b 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 flow occurred..
1cf1c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1cf1d 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d if( u.af.iA==-
1cf1e 31 20 26 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d 1 && u.af.iB==SM
1cf1f 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 75 ALLEST_INT64 ) u
1cf20 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 .af.iA = 1;.
1cf21 20 20 20 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75 u.af.iB /= u
1cf22 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20 20 .af.iA;.
1cf23 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1cf24 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
1cf25 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 if( u.af
1cf26 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 .iA==0 ) goto ar
1cf27 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f ithmetic_result_
1cf28 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 is_null;.
1cf29 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 if( u.af.iA==-1
1cf2a 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a ) u.af.iA = 1;.
1cf2b 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 u.af.iB
1cf2c 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 %= u.af.iA;.
1cf2d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1cf2e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 }. }. pOu
1cf2f 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42 t->u.i = u.af.iB
1cf30 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1cf31 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1cf32 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 nt);. }else{.
1cf33 20 20 75 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69 u.af.rA = sqli
1cf34 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 te3VdbeRealValue
1cf35 28 70 49 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66 (pIn1);. u.af
1cf36 2e 72 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 .rB = sqlite3Vdb
1cf37 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 eRealValue(pIn2)
1cf38 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f ;. switch( pO
1cf39 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
1cf3a 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 case OP_Add:
1cf3b 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 u.af.rB
1cf3c 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 += u.af.rA;
1cf3d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1cf3e 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a ase OP_Subtract:
1cf3f 20 20 20 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75 u.af.rB -= u
1cf40 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 .af.rA; br
1cf41 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
1cf42 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 OP_Multiply:
1cf43 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e u.af.rB *= u.af.
1cf44 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b rA; break;
1cf45 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 . case OP_D
1cf46 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 ivide: {.
1cf47 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e /* (double)0 In
1cf48 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f case of SQLITE_
1cf49 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1cf4a 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 INT... */.
1cf4b 20 20 69 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28 if( u.af.rA==(
1cf4c 64 6f 75 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 double)0 ) goto
1cf4d 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c arithmetic_resul
1cf4e 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 t_is_null;.
1cf4f 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e u.af.rB /= u.
1cf50 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 62 af.rA;. b
1cf51 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1cf52 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1cf53 20 20 20 20 20 20 20 75 2e 61 66 2e 69 41 20 3d u.af.iA =
1cf54 20 28 69 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20 (i64)u.af.rA;.
1cf55 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 3d u.af.iB =
1cf56 20 28 69 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20 (i64)u.af.rB;.
1cf57 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1cf58 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 iA==0 ) goto ari
1cf59 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1cf5a 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1cf5b 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 if( u.af.iA==-1
1cf5c 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 ) u.af.iA = 1;.
1cf5d 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 3d u.af.rB =
1cf5e 20 28 64 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69 (double)(u.af.i
1cf5f 42 20 25 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20 B % u.af.iA);.
1cf60 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cf61 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1cf62 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 f( sqlite3IsNaN(
1cf63 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20 20 20 20 u.af.rB) ){.
1cf64 20 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 goto arithmeti
1cf65 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1cf66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 ;. }. pOut
1cf67 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20 ->r = u.af.rB;.
1cf68 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1cf69 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c g(pOut, MEM_Real
1cf6a 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 61 66 );. if( (u.af
1cf6b 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 .flags & MEM_Rea
1cf6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 l)==0 ){. s
1cf6d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 qlite3VdbeIntege
1cf6e 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b rAffinity(pOut);
1cf6f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
1cf70 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f ak;..arithmetic_
1cf71 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a result_is_null:.
1cf72 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cf73 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
1cf74 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cf75 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 code: CollSeq *
1cf76 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 * P4.**.** P4 is
1cf77 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1cf78 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 CollSeq struct.
1cf79 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c If the next call
1cf7a 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 to a user funct
1cf7b 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 ion.** or aggreg
1cf7c 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 ate calls sqlite
1cf7d 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 3GetFuncCollSeq(
1cf7e 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f ), this collatio
1cf7f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a n sequence will.
1cf80 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ** be returned.
1cf81 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1cf82 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e the built-in min
1cf83 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 (), max() and nu
1cf84 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 llif().** functi
1cf85 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ons..**.** The i
1cf86 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79 nterface used by
1cf87 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
1cf88 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 ion of the afore
1cf89 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 mentioned functi
1cf8a 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 ons.** to retrie
1cf8b 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ve the collation
1cf8c 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 sequence set by
1cf8d 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 this opcode is
1cf8e 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a not available.**
1cf8f 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 publicly, only
1cf90 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e to user function
1cf91 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e s defined in fun
1cf92 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f c.c..*/.case OP_
1cf93 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 CollSeq: {. ass
1cf94 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1cf95 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a ==P4_COLLSEQ );.
1cf96 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cf97 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 pcode: Function
1cf98 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1cf99 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 *.** Invoke a us
1cf9a 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 er function (P4
1cf9b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1cf9c 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 a Function struc
1cf9d 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 ture that.** def
1cf9e 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f ines the functio
1cf9f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d n) with P5 argum
1cfa0 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 ents taken from
1cfa1 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a register P2 and.
1cfa2 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20 ** successors.
1cfa3 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 The result of th
1cfa4 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 e function is st
1cfa5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1cfa6 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 P3..** Register
1cfa7 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 P3 must not be
1cfa8 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 one of the funct
1cfa9 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a ion inputs..**.*
1cfaa 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74 * P1 is a 32-bit
1cfab 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 bitmask indicat
1cfac 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e ing whether or n
1cfad 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 ot each argument
1cfae 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 to the .** func
1cfaf 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69 tion was determi
1cfb0 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 ned to be consta
1cfb1 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 nt at compile ti
1cfb2 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74 me. If the first
1cfb3 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73 .** argument was
1cfb4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 constant then b
1cfb5 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65 it 0 of P1 is se
1cfb6 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 t. This is used
1cfb7 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 to determine.**
1cfb8 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74 whether meta dat
1cfb9 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
1cfba 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f h a user functio
1cfbb 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 n argument using
1cfbc 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
1cfbd 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50 set_auxdata() AP
1cfbe 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 I may be safely
1cfbf 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 retained until t
1cfc0 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 he next.** invoc
1cfc1 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 ation of this op
1cfc2 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 code..**.** See
1cfc3 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e also: AggStep an
1cfc4 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 d AggFinal.*/.ca
1cfc5 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 se OP_Function:
1cfc6 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1cfc7 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1cfc8 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20 d into u.ag */.
1cfc9 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 int i;. Mem *p
1cfca 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 Arg;. sqlite3_c
1cfcb 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 ontext ctx;. sq
1cfcc 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 lite3_value **ap
1cfcd 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65 Val;. int n;.#e
1cfce 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1cfcf 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1cfd0 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e to u.ag */.. u.
1cfd1 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a ag.n = pOp->p5;.
1cfd2 20 20 75 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70 u.ag.apVal = p
1cfd3 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 ->apArg;. asser
1cfd4 74 28 20 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c t( u.ag.apVal ||
1cfd5 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20 u.ag.n==0 );..
1cfd6 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d assert( u.ag.n=
1cfd7 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 =0 || (pOp->p2>0
1cfd8 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 && pOp->p2+u.ag
1cfd9 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 .n<=p->nMem+1) )
1cfda 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1cfdb 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 >p3<pOp->p2 || p
1cfdc 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b Op->p3>=pOp->p2+
1cfdd 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67 u.ag.n );. u.ag
1cfde 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d 5b 70 4f .pArg = &aMem[pO
1cfdf 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p2];. for(u.
1cfe0 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69 3c 75 ag.i=0; u.ag.i<u
1cfe1 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b 2b 2c .ag.n; u.ag.i++,
1cfe2 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b 0a 20 u.ag.pArg++){.
1cfe3 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b 75 2e u.ag.apVal[u.
1cfe4 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70 41 72 ag.i] = u.ag.pAr
1cfe5 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 g;. sqlite3Vd
1cfe6 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 beMemStoreType(u
1cfe7 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 20 20 52 .ag.pArg);. R
1cfe8 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1cfe9 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41 72 67 p->p2, u.ag.pArg
1cfea 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 );. }.. assert
1cfeb 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1cfec 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70 4_FUNCDEF || pOp
1cfed 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 42 ->p4type==P4_VDB
1cfee 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 70 EFUNC );. if( p
1cfef 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 Op->p4type==P4_F
1cff0 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 75 2e UNCDEF ){. u.
1cff1 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 ag.ctx.pFunc = p
1cff2 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
1cff3 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 u.ag.ctx.pVdbe
1cff4 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Func = 0;. }els
1cff5 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e e{. u.ag.ctx.
1cff6 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 pVdbeFunc = (Vdb
1cff7 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 eFunc*)pOp->p4.p
1cff8 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 75 2e VdbeFunc;. u.
1cff9 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 75 ag.ctx.pFunc = u
1cffa 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1cffb 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 c->pFunc;. }..
1cffc 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1cffd 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1cffe 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 ->nMem );. pOut
1cfff 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1d000 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e ];. u.ag.ctx.s.
1d001 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d002 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64 ;. u.ag.ctx.s.d
1d003 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63 b = db;. u.ag.c
1d004 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 tx.s.xDel = 0;.
1d005 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c u.ag.ctx.s.zMal
1d006 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 loc = 0;.. /* T
1d007 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d he output cell m
1d008 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 ay already have
1d009 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 a buffer allocat
1d00a 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 ed. Move. ** th
1d00b 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61 e pointer to u.a
1d00c 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 g.ctx.s so in ca
1d00d 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 se the user-func
1d00e 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a tion can use. *
1d00f 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c * the already al
1d010 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 69 located buffer i
1d011 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 nstead of alloca
1d012 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a ting a new one..
1d013 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1d014 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e beMemMove(&u.ag.
1d015 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 ctx.s, pOut);.
1d016 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 MemSetTypeFlag(&
1d017 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f u.ag.ctx.s, MEM_
1d018 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63 Null);.. u.ag.c
1d019 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a tx.isError = 0;.
1d01a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 if( u.ag.ctx.p
1d01b 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 Func->flags & SQ
1d01c 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
1d01d 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 LL ){. assert
1d01e 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a 20 20 20 ( pOp>aOp );.
1d01f 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d assert( pOp[-1]
1d020 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c .p4type==P4_COLL
1d021 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 SEQ );. asser
1d022 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 t( pOp[-1].opcod
1d023 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b e==OP_CollSeq );
1d024 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43 . u.ag.ctx.pC
1d025 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 oll = pOp[-1].p4
1d026 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 .pColl;. }. if
1d027 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1d028 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1d029 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1d02a 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e e;. (*u.ag.ctx.
1d02b 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75 pFunc->xFunc)(&u
1d02c 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c .ag.ctx, u.ag.n,
1d02d 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20 u.ag.apVal);.
1d02e 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1d02f 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 yOn(db) ){. s
1d030 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1d031 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 ease(&u.ag.ctx.s
1d032 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 );. goto abor
1d033 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1d034 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d . }. if( db->m
1d035 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1d036 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 /* Even thoug
1d037 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 h a malloc() has
1d038 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 failed, the imp
1d039 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
1d03a 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 he. ** user f
1d03b 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 unction may have
1d03c 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 called an sqlit
1d03d 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 e3_result_XXX()
1d03e 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 function. **
1d03f 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 to return a valu
1d040 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 e. The following
1d041 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 call releases a
1d042 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 ny resources.
1d043 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 ** associated w
1d044 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 ith such a value
1d045 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1d046 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 Note: Maybe MemR
1d047 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 elease() should
1d048 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c be called if sql
1d049 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 ite3SafetyOn().
1d04a 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f ** fails also
1d04b 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 (the if(...) st
1d04c 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 atement above).
1d04d 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 But if people ar
1d04e 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e e. ** misusin
1d04f 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 g sqlite, they h
1d050 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c ave bigger probl
1d051 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 ems than a leake
1d052 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a d value.. */.
1d053 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d054 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e emRelease(&u.ag.
1d055 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f ctx.s);. goto
1d056 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 no_mem;. }..
1d057 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 /* If any auxili
1d058 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f ary data functio
1d059 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c ns have been cal
1d05a 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72 led by this user
1d05b 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 function,. **
1d05c 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c immediately call
1d05d 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
1d05e 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 for any non-stat
1d05f 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a ic values.. */.
1d060 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 if( u.ag.ctx.p
1d061 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 VdbeFunc ){.
1d062 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1d063 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74 eAuxData(u.ag.ct
1d064 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 x.pVdbeFunc, pOp
1d065 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e ->p1);. pOp->
1d066 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75 p4.pVdbeFunc = u
1d067 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1d068 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 c;. pOp->p4ty
1d069 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 pe = P4_VDBEFUNC
1d06a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1d06b 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
1d06c 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 rned an error, t
1d06d 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f hrow an exceptio
1d06e 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e n */. if( u.ag.
1d06f 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 ctx.isError ){.
1d070 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1d071 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1d072 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1d073 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75 e3_value_text(&u
1d074 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20 .ag.ctx.s));.
1d075 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69 rc = u.ag.ctx.i
1d076 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f sError;. }.. /
1d077 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c * Copy the resul
1d078 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f t of the functio
1d079 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 n into register
1d07a 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 P3 */. sqlite3V
1d07b 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1d07c 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65 g(&u.ag.ctx.s, e
1d07d 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69 ncoding);. sqli
1d07e 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 te3VdbeMemMove(p
1d07f 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73 Out, &u.ag.ctx.s
1d080 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1d081 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f VdbeMemTooBig(pO
1d082 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ut) ){. goto
1d083 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 too_big;. }. R
1d084 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1d085 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p3, pOut);.
1d086 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1d087 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1d088 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1d089 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 : BitAnd P1 P2 P
1d08a 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 3 * *.**.** Take
1d08b 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e the bit-wise AN
1d08c 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 D of the values
1d08d 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 in register P1 a
1d08e 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f nd P2 and.** sto
1d08f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1d090 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1d091 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 If either input
1d092 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1d093 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1d094 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f ./* Opcode: BitO
1d095 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a r P1 P2 P3 * *.*
1d096 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 *.** Take the bi
1d097 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 t-wise OR of the
1d098 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1d099 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e ter P1 and P2 an
1d09a 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 d.** store the r
1d09b 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1d09c 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1d09d 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1d09e 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1d09f 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1d0a0 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 de: ShiftLeft P1
1d0a1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d0a2 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 Shift the integ
1d0a3 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 er value in regi
1d0a4 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c ster P2 to the l
1d0a5 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 eft by the.** nu
1d0a6 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 mber of bits spe
1d0a7 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e cified by the in
1d0a8 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 teger in regiser
1d0a9 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 P1..** Store th
1d0aa 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1d0ab 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1d0ac 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1d0ad 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1d0ae 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1d0af 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 pcode: ShiftRigh
1d0b0 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
1d0b1 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 *.** Shift the i
1d0b2 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 nteger value in
1d0b3 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 register P2 to t
1d0b4 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 0a he right by the.
1d0b5 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 ** number of bit
1d0b6 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 s specified by t
1d0b7 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 he integer in re
1d0b8 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 gister P1..** St
1d0b9 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1d0ba 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1d0bb 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1d0bc 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1d0bd 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1d0be 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 /.case OP_BitAnd
1d0bf 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1d0c0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d0c1 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 BITAND, in1, in2
1d0c2 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1d0c3 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 P_BitOr:
1d0c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1d0c5 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 e as TK_BITOR, i
1d0c6 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1d0c7 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 .case OP_ShiftLe
1d0c8 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 ft:
1d0c9 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
1d0ca 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c SHIFT, in1, in2,
1d0cb 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1d0cc 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 _ShiftRight: {
1d0cd 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d0ce 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 as TK_RSHIFT, i
1d0cf 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1d0d0 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d0d1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d0d2 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20 into u.ah */.
1d0d3 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a i64 a;. i64 b;.
1d0d4 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1d0d5 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d0d6 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20 into u.ah */..
1d0d7 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1d0d8 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20 3d 20 ->p1];. pIn2 =
1d0d9 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a &aMem[pOp->p2];.
1d0da 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 pOut = &aMem[p
1d0db 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 Op->p3];. if( (
1d0dc 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1d0dd 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d n2->flags) & MEM
1d0de 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1d0df 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1d0e0 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 ll(pOut);. br
1d0e1 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 61 68 2e eak;. }. u.ah.
1d0e2 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 a = sqlite3VdbeI
1d0e3 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 ntValue(pIn2);.
1d0e4 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c 69 74 65 u.ah.b = sqlite
1d0e5 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1d0e6 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 n1);. switch( p
1d0e7 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 Op->opcode ){.
1d0e8 20 20 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 case OP_BitAnd
1d0e9 3a 20 20 20 20 20 20 75 2e 61 68 2e 61 20 26 3d : u.ah.a &=
1d0ea 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 62 72 65 u.ah.b; bre
1d0eb 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f ak;. case OP_
1d0ec 42 69 74 4f 72 3a 20 20 20 20 20 20 20 75 2e 61 BitOr: u.a
1d0ed 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62 3b 20 20 h.a |= u.ah.b;
1d0ee 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
1d0ef 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a se OP_ShiftLeft:
1d0f0 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d 20 75 2e u.ah.a <<= u.
1d0f1 61 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a ah.b; break;.
1d0f2 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 default: as
1d0f3 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 sert( pOp->opcod
1d0f4 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 e==OP_ShiftRight
1d0f5 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
1d0f6 20 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 61 u.a
1d0f7 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e 62 3b 20 h.a >>= u.ah.b;
1d0f8 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1d0f9 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 68 pOut->u.i = u.ah
1d0fa 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 .a;. MemSetType
1d0fb 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1d0fc 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a nt);. break;.}.
1d0fd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 ./* Opcode: AddI
1d0fe 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a mm P1 P2 * * *.
1d0ff 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 ** .** Add the c
1d100 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68 onstant P2 to th
1d101 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d102 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 ter P1..** The r
1d103 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 esult is always
1d104 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a an integer..**.*
1d105 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 * To force any r
1d106 65 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e egister to be an
1d107 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61 integer, just a
1d108 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 dd 0..*/.case OP
1d109 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 _AddImm: {
1d10a 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a /* in1 */.
1d10b 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1d10c 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71 6c 69 74 Op->p1];. sqlit
1d10d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1d10e 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e ify(pIn1);. pIn
1d10f 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 1->u.i += pOp->p
1d110 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2;. break;.}../
1d111 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74 42 65 * Opcode: MustBe
1d112 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Int P1 P2 * * *.
1d113 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 ** .** Force the
1d114 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1d115 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e 20 69 er P1 to be an i
1d116 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 nteger. If the
1d117 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 value.** in P1 i
1d118 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 s not an integer
1d119 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65 20 63 and cannot be c
1d11a 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e onverted into an
1d11b 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74 68 integer.** with
1d11c 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c 20 74 out data loss, t
1d11d 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 hen jump immedia
1d11e 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72 20 69 tely to P2, or i
1d11f 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 f P2==0.** raise
1d120 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 an SQLITE_MISMA
1d121 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a TCH exception..*
1d122 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74 42 65 /.case OP_MustBe
1d123 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 Int: {
1d124 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a /* jump, in1 *
1d125 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1d126 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 70 70 [pOp->p1];. app
1d127 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c lyAffinity(pIn1,
1d128 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 SQLITE_AFF_NUME
1d129 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a RIC, encoding);.
1d12a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1d12b 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 gs & MEM_Int)==0
1d12c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d ){. if( pOp-
1d12d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 >p2==0 ){.
1d12e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d rc = SQLITE_MISM
1d12f 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f ATCH;. goto
1d130 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1d131 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ror;. }else{.
1d132 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1d133 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 p2 - 1;. }.
1d134 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65 }else{. MemSe
1d135 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 tTypeFlag(pIn1,
1d136 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 MEM_Int);. }.
1d137 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d138 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 ode: RealAffinit
1d139 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a y P1 * * * *.**.
1d13a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 20 50 ** If register P
1d13b 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 1 holds an integ
1d13c 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f er convert it to
1d13d 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a a real value..*
1d13e 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 *.** This opcode
1d13f 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 65 78 is used when ex
1d140 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 tracting informa
1d141 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 tion from a colu
1d142 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 mn that.** has R
1d143 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20 20 53 EAL affinity. S
1d144 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 uch column value
1d145 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 73 s may still be s
1d146 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 tored as.** inte
1d147 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63 65 20 gers, for space
1d148 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75 74 20 efficiency, but
1d149 61 66 74 65 72 20 65 78 74 72 61 63 74 69 6f 6e after extraction
1d14a 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a we want them.**
1d14b 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 to have only a
1d14c 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 real value..*/.c
1d14d 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e ase OP_RealAffin
1d14e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ity: {
1d14f 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a /* in1 *
1d150 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1d151 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 [pOp->p1];. if(
1d152 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1d153 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 EM_Int ){. sq
1d154 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
1d155 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 ify(pIn1);. }.
1d156 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 break;.}..#ifnd
1d157 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1d158 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 AST./* Opcode: T
1d159 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a oText P1 * * * *
1d15a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 .**.** Force the
1d15b 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1d15c 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74 er P1 to be text
1d15d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 ..** If the valu
1d15e 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f e is numeric, co
1d15f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 nvert it to a st
1d160 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a ring using the.*
1d161 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 * equivalent of
1d162 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20 printf(). Blob
1d163 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61 values are uncha
1d164 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20 nged and.** are
1d165 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c afterwards simpl
1d166 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 y interpreted as
1d167 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e text..**.** A N
1d168 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 ULL value is not
1d169 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
1d16a 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 routine. It re
1d16b 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 mains NULL..*/.c
1d16c 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b ase OP_ToText: {
1d16d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d16e 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d16f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a TO_TEXT, in1 */.
1d170 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1d171 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 Op->p1];. if( p
1d172 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d173 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 _Null ) break;.
1d174 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72 assert( MEM_Str
1d175 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 ==(MEM_Blob>>3)
1d176 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1d177 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 |= (pIn1->flags
1d178 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 &MEM_Blob)>>3;.
1d179 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1d17a 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f In1, SQLITE_AFF_
1d17b 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b TEXT, encoding);
1d17c 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c . rc = ExpandBl
1d17d 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 ob(pIn1);. asse
1d17e 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 rt( pIn1->flags
1d17f 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d & MEM_Str || db-
1d180 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
1d181 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 . pIn1->flags &
1d182 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f = ~(MEM_Int|MEM_
1d183 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 Real|MEM_Blob|ME
1d184 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 54 M_Zero);. UPDAT
1d185 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1d186 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d In1);. break;.}
1d187 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 ../* Opcode: ToB
1d188 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a lob P1 * * * *.*
1d189 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 *.** Force the v
1d18a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1d18b 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 P1 to be a BLOB
1d18c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 ..** If the valu
1d18d 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f e is numeric, co
1d18e 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 nvert it to a st
1d18f 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53 ring first..** S
1d190 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c trings are simpl
1d191 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20 y reinterpreted
1d192 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f as blobs with no
1d193 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 change.** to th
1d194 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
1d195 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 a..**.** A NULL
1d196 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
1d197 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
1d198 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
1d199 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1d19a 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 OP_ToBlob: {
1d19b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d19c 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 same as TK_TO_B
1d19d 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 LOB, in1 */. pI
1d19e 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n1 = &aMem[pOp->
1d19f 70 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d p1];. if( pIn1-
1d1a0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d1a1 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 l ) break;. if(
1d1a2 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d1a3 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a MEM_Blob)==0 ){.
1d1a4 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1d1a5 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 y(pIn1, SQLITE_A
1d1a6 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e FF_TEXT, encodin
1d1a7 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 g);. assert(
1d1a8 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1d1a9 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c M_Str || db->mal
1d1aa 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
1d1ab 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1d1ac 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b pIn1, MEM_Blob);
1d1ad 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
1d1ae 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d n1->flags &= ~(M
1d1af 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d EM_TypeMask&~MEM
1d1b0 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 _Blob);. }. UP
1d1b1 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1d1b2 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b E(pIn1);. break
1d1b3 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d1b4 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a ToNumeric P1 * *
1d1b5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 * *.**.** Force
1d1b6 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1d1b7 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
1d1b8 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 numeric (either
1d1b9 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 an.** integer or
1d1ba 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e a floating-poin
1d1bb 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 t number.).** If
1d1bc 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 the value is te
1d1bd 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 xt or blob, try
1d1be 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f to convert it to
1d1bf 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a an using the.**
1d1c0 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 equivalent of a
1d1c1 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 toi() or atof()
1d1c2 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e and store 0 if n
1d1c3 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f o such conversio
1d1c4 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c n .** is possibl
1d1c5 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 e..**.** A NULL
1d1c6 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
1d1c7 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
1d1c8 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
1d1c9 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1d1ca 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 OP_ToNumeric: {
1d1cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d1cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1d1cd 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a O_NUMERIC, in1 *
1d1ce 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1d1cf 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 [pOp->p1];. if(
1d1d0 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d1d1 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e (MEM_Null|MEM_In
1d1d2 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 t|MEM_Real))==0
1d1d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1d1d4 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 beMemNumerify(pI
1d1d5 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1d1d6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1d1d7 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a LITE_OMIT_CAST *
1d1d8 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f /../* Opcode: To
1d1d9 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a Int P1 * * * *.*
1d1da 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 *.** Force the v
1d1db 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1d1dc 20 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65 P1 be an intege
1d1dd 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 r. If.** The va
1d1de 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 lue is currently
1d1df 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 a real number,
1d1e0 64 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f drop its fractio
1d1e1 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 nal part..** If
1d1e2 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 the value is tex
1d1e3 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 t or blob, try t
1d1e4 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
1d1e5 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 an integer using
1d1e6 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 the.** equivale
1d1e7 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 nt of atoi() and
1d1e8 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 store 0 if no s
1d1e9 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 uch conversion i
1d1ea 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a s possible..**.*
1d1eb 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1d1ec 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1d1ed 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1d1ee 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1d1ef 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e .*/.case OP_ToIn
1d1f0 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 t: {
1d1f1 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1d1f2 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 TK_TO_INT, in1
1d1f3 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1d1f4 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 m[pOp->p1];. if
1d1f5 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1d1f6 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b MEM_Null)==0 ){
1d1f7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1d1f8 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 MemIntegerify(pI
1d1f9 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1d1fa 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1d1fb 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a ITE_OMIT_CAST./*
1d1fc 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20 Opcode: ToReal
1d1fd 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1d1fe 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1d1ff 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d200 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 to be a floating
1d201 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a point number..*
1d202 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20 69 * If The value i
1d203 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69 s currently an i
1d204 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20 nteger, convert
1d205 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 it..** If the va
1d206 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 lue is text or b
1d207 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 lob, try to conv
1d208 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 ert it to an int
1d209 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a eger using the.*
1d20a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 * equivalent of
1d20b 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 atoi() and store
1d20c 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20 0.0 if no such
1d20d 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
1d20e 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 ssible..**.** A
1d20f 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f NULL value is no
1d210 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 t changed by thi
1d211 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 s routine. It r
1d212 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a emains NULL..*/.
1d213 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 case OP_ToReal:
1d214 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1d215 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1d216 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f _TO_REAL, in1 */
1d217 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1d218 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 pOp->p1];. if(
1d219 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1d21a 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 EM_Null)==0 ){.
1d21b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1d21c 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a mRealify(pIn1);.
1d21d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1d21e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1d21f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a OMIT_CAST */../*
1d220 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 31 20 50 Opcode: Lt P1 P
1d221 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1d222 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c Compare the val
1d223 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 ues in register
1d224 50 31 20 61 6e 64 20 50 33 2e 20 20 49 66 20 72 P1 and P3. If r
1d225 65 67 28 50 33 29 3c 72 65 67 28 50 31 29 20 74 eg(P3)<reg(P1) t
1d226 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 hen.** jump to a
1d227 64 64 72 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a ddress P2. .**.
1d228 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 ** If the SQLITE
1d229 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 _JUMPIFNULL bit
1d22a 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 6e 64 of P5 is set and
1d22b 20 65 69 74 68 65 72 20 72 65 67 28 50 31 29 20 either reg(P1)
1d22c 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 20 69 73 or.** reg(P3) is
1d22d 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 NULL then take
1d22e 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20 74 68 the jump. If th
1d22f 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e e SQLITE_JUMPIFN
1d230 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 73 20 63 ULL .** bit is c
1d231 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74 lear then fall t
1d232 68 72 75 20 69 66 20 65 69 74 68 65 72 20 6f 70 hru if either op
1d233 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a erand is NULL..*
1d234 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
1d235 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e AFF_MASK portion
1d236 20 6f 66 20 50 35 20 6d 75 73 74 20 62 65 20 61 of P5 must be a
1d237 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 72 61 n affinity chara
1d238 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45 cter -.** SQLITE
1d239 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 4c 49 54 _AFF_TEXT, SQLIT
1d23a 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 2c 20 61 E_AFF_INTEGER, a
1d23b 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20 nd so forth. An
1d23c 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
1d23d 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 20 62 6f .** to coerce bo
1d23e 74 68 20 69 6e 70 75 74 73 20 61 63 63 6f 72 64 th inputs accord
1d23f 69 6e 67 20 74 6f 20 74 68 69 73 20 61 66 66 69 ing to this affi
1d240 6e 69 74 79 20 62 65 66 6f 72 65 20 74 68 65 0a nity before the.
1d241 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 ** comparison is
1d242 20 6d 61 64 65 2e 20 49 66 20 74 68 65 20 53 51 made. If the SQ
1d243 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73 LITE_AFF_MASK is
1d244 20 30 78 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 0x00, then nume
1d245 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 ric.** affinity
1d246 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 20 74 68 is used. Note th
1d247 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 at the affinity
1d248 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20 conversions are
1d249 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69 stored.** back i
1d24a 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20 72 65 nto the input re
1d24b 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 gisters P1 and P
1d24c 33 2e 20 20 53 6f 20 74 68 69 73 20 6f 70 63 6f 3. So this opco
1d24d 64 65 20 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20 de can cause.**
1d24e 70 65 72 73 69 73 74 65 6e 74 20 63 68 61 6e 67 persistent chang
1d24f 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 20 es to registers
1d250 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a P1 and P3..**.**
1d251 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 Once any conver
1d252 73 69 6f 6e 73 20 68 61 76 65 20 74 61 6b 65 6e sions have taken
1d253 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 place, and neit
1d254 68 65 72 20 76 61 6c 75 65 20 69 73 20 4e 55 4c her value is NUL
1d255 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 L, .** the value
1d256 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64 2e 20 s are compared.
1d257 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 If both values a
1d258 72 65 20 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65 re blobs then me
1d259 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65 mcmp() is.** use
1d25a 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 d to determine t
1d25b 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1d25c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 e comparison. I
1d25d 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a f both values.**
1d25e 20 61 72 65 20 74 65 78 74 2c 20 74 68 65 6e 20 are text, then
1d25f 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
1d260 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
1d261 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a on specified in.
1d262 2a 2a 20 50 34 20 69 73 20 20 75 73 65 64 20 74 ** P4 is used t
1d263 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 o do the compari
1d264 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 73 20 6e son. If P4 is n
1d265 6f 74 20 73 70 65 63 69 66 69 65 64 20 74 68 65 ot specified the
1d266 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73 n.** memcmp() is
1d267 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 used to compare
1d268 20 74 65 78 74 20 73 74 72 69 6e 67 2e 20 20 49 text string. I
1d269 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 f both values ar
1d26a 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68 e.** numeric, th
1d26b 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d en a numeric com
1d26c 70 61 72 69 73 6f 6e 20 69 73 20 75 73 65 64 2e parison is used.
1d26d 20 49 66 20 74 68 65 20 74 77 6f 20 76 61 6c 75 If the two valu
1d26e 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 64 69 66 es.** are of dif
1d26f 66 65 72 65 6e 74 20 74 79 70 65 73 2c 20 74 68 ferent types, th
1d270 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 65 20 63 en numbers are c
1d271 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 onsidered less t
1d272 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61 han.** strings a
1d273 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 63 nd strings are c
1d274 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 20 74 onsidered less t
1d275 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a han blobs..**.**
1d276 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 If the SQLITE_S
1d277 54 4f 52 45 50 32 20 62 69 74 20 6f 66 20 50 35 TOREP2 bit of P5
1d278 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 64 6f is set, then do
1d279 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74 not jump. Inst
1d27a 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20 ead,.** store a
1d27b 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28 boolean result (
1d27c 65 69 74 68 65 72 20 30 2c 20 6f 72 20 31 2c 20 either 0, or 1,
1d27d 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69 or NULL) in regi
1d27e 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f ster P2..*/./* O
1d27f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 50 32 20 pcode: Ne P1 P2
1d280 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 P4 P5.**.** T
1d281 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c his works just l
1d282 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 ike the Lt opcod
1d283 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 e except that th
1d284 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 e jump is taken
1d285 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e if.** the operan
1d286 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 ds in registers
1d287 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 6e 6f P1 and P3 are no
1d288 74 20 65 71 75 61 6c 2e 20 20 53 65 65 20 74 68 t equal. See th
1d289 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a e Lt opcode for.
1d28a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
1d28b 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
1d28c 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 If SQLITE_NULLE
1d28d 51 20 69 73 20 73 65 74 20 69 6e 20 50 35 20 74 Q is set in P5 t
1d28e 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f hen the result o
1d28f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 f comparison is
1d290 61 6c 77 61 79 73 20 65 69 74 68 65 72 0a 2a 2a always either.**
1d291 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 61 true or false a
1d292 6e 64 20 69 73 20 6e 65 76 65 72 20 4e 55 4c 4c nd is never NULL
1d293 2e 20 20 49 66 20 62 6f 74 68 20 6f 70 65 72 61 . If both opera
1d294 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 nds are NULL the
1d295 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 n the result.**
1d296 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 of comparison is
1d297 20 66 61 6c 73 65 2e 20 20 49 66 20 65 69 74 68 false. If eith
1d298 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 er operand is NU
1d299 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 LL then the resu
1d29a 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 49 lt is true..** I
1d29b 66 20 6e 65 69 74 68 65 72 20 6f 70 65 72 61 6e f neither operan
1d29c 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 20 74 68 d is NULL the th
1d29d 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 e result is the
1d29e 73 61 6d 65 20 61 73 20 69 74 20 77 6f 75 6c 64 same as it would
1d29f 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 20 53 51 be if.** the SQ
1d2a0 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 LITE_NULLEQ flag
1d2a1 20 77 65 72 65 20 6f 6d 69 74 74 65 64 20 66 72 were omitted fr
1d2a2 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 om P5..*/./* Opc
1d2a3 6f 64 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 ode: Eq P1 P2 P3
1d2a4 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 P4 P5.**.** Thi
1d2a5 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b s works just lik
1d2a6 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1d2a7 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 except that the
1d2a8 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 jump is taken if
1d2a9 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 .** the operands
1d2aa 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 in registers P1
1d2ab 20 61 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 and P3 are equa
1d2ac 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 l..** See the Lt
1d2ad 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
1d2ae 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1d2af 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c on..**.** If SQL
1d2b0 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73 65 ITE_NULLEQ is se
1d2b1 74 20 69 6e 20 50 35 20 74 68 65 6e 20 74 68 65 t in P5 then the
1d2b2 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 result of compa
1d2b3 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 rison is always
1d2b4 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 65 20 6f either.** true o
1d2b5 72 20 66 61 6c 73 65 20 61 6e 64 20 69 73 20 6e r false and is n
1d2b6 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 66 20 62 ever NULL. If b
1d2b7 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 oth operands are
1d2b8 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1d2b9 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f 6d 70 esult.** of comp
1d2ba 61 72 69 73 6f 6e 20 69 73 20 74 72 75 65 2e 20 arison is true.
1d2bb 20 49 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 If either opera
1d2bc 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 nd is NULL then
1d2bd 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 the result is fa
1d2be 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 69 74 68 lse..** If neith
1d2bf 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 er operand is NU
1d2c0 4c 4c 20 74 68 65 20 74 68 65 20 72 65 73 75 6c LL the the resul
1d2c1 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 t is the same as
1d2c2 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69 66 0a it would be if.
1d2c3 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55 ** the SQLITE_NU
1d2c4 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 65 20 6f LLEQ flag were o
1d2c5 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 35 2e 0a mitted from P5..
1d2c6 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 */./* Opcode: Le
1d2c7 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1d2c8 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1d2c9 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
1d2ca 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
1d2cb 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
1d2cc 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
1d2cd 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1d2ce 73 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 ster P3 is less
1d2cf 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1d2d0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a the content of.
1d2d1 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 ** register P1.
1d2d2 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1d2d3 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1d2d4 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1d2d5 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 /./* Opcode: Gt
1d2d6 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1d2d7 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
1d2d8 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 just like the Lt
1d2d9 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 opcode except t
1d2da 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 hat the jump is
1d2db 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 taken if.** the
1d2dc 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1d2dd 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65 ter P3 is greate
1d2de 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 r than the conte
1d2df 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 nt of.** registe
1d2e0 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c r P1. See the L
1d2e1 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 t opcode for add
1d2e2 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1d2e3 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ion..*/./* Opcod
1d2e4 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 e: Ge P1 P2 P3 P
1d2e5 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 4 P5.**.** This
1d2e6 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
1d2e7 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 the Lt opcode ex
1d2e8 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 cept that the ju
1d2e9 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a mp is taken if.*
1d2ea 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
1d2eb 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 register P3 is
1d2ec 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1d2ed 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e equal to the con
1d2ee 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 tent of.** regis
1d2ef 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 ter P1. See the
1d2f0 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
1d2f1 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1d2f2 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f ation..*/.case O
1d2f3 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 P_Eq:
1d2f4 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d2f5 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c K_EQ, jump, in1,
1d2f6 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1d2f7 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 Ne:
1d2f8 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d2f9 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 NE, jump, in1, i
1d2fa 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 n3 */.case OP_Lt
1d2fb 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1d2fc 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 /* same as TK_LT
1d2fd 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 , jump, in1, in3
1d2fe 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 */.case OP_Le:
1d2ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d300 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 same as TK_LE,
1d301 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1d302 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 /.case OP_Gt:
1d303 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d304 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 ame as TK_GT, ju
1d305 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1d306 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 case OP_Ge: {
1d307 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1d308 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 e as TK_GE, jump
1d309 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 , in1, in3 */.#i
1d30a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1d30b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d30c 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 to u.ai */. int
1d30d 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 res;
1d30e 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 /* Result of th
1d30f 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 e comparison of
1d310 70 49 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e pIn1 against pIn
1d311 33 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 3 */. char affi
1d312 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 nity; /* Af
1d313 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f finity to use fo
1d314 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a r comparison */.
1d315 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1d316 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d317 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20 20 into u.ai */..
1d318 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1d319 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 20 3d 20 ->p1];. pIn3 =
1d31a 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1d31b 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1d31c 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c 61 67 73 gs | pIn3->flags
1d31d 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 )&MEM_Null ){.
1d31e 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 6f 74 68 /* One or both
1d31f 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55 operands are NU
1d320 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f LL */. if( pO
1d321 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 4e p->p5 & SQLITE_N
1d322 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 20 20 2f ULLEQ ){. /
1d323 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c * If SQLITE_NULL
1d324 45 51 20 69 73 20 73 65 74 20 28 77 68 69 63 68 EQ is set (which
1d325 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 will only happe
1d326 6e 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 6f n if the operato
1d327 72 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 4f 50 r is. ** OP
1d328 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 20 74 68 _Eq or OP_Ne) th
1d329 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
1d32a 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69 6e or not dependin
1d32b 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 20 20 20 g on whether.
1d32c 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 62 6f 74 ** or not bot
1d32d 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 6e h operands are n
1d32e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ull.. */.
1d32f 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1d330 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 71 20 7c >opcode==OP_Eq |
1d331 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f | pOp->opcode==O
1d332 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 75 2e P_Ne );. u.
1d333 61 69 2e 72 65 73 20 3d 20 28 70 49 6e 31 2d 3e ai.res = (pIn1->
1d334 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 3e 66 6c flags & pIn3->fl
1d335 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d ags & MEM_Null)=
1d336 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 =0;. }else{.
1d337 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e /* SQLITE_N
1d338 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 72 20 61 ULLEQ is clear a
1d339 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 nd at least one
1d33a 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c operand is NULL,
1d33b 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
1d33c 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 he result is alw
1d33d 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 ays NULL..
1d33e 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73 20 74 ** The jump is t
1d33f 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51 4c 49 aken if the SQLI
1d340 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 TE_JUMPIFNULL bi
1d341 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20 20 20 t is set..
1d342 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 */. if( pOp
1d343 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 ->p5 & SQLITE_ST
1d344 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 20 OREP2 ){.
1d345 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1d346 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 p->p2];.
1d347 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1d348 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a Out, MEM_Null);.
1d349 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 REGISTER
1d34a 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 _TRACE(pOp->p2,
1d34b 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c pOut);. }el
1d34c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 se if( pOp->p5 &
1d34d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 SQLITE_JUMPIFNU
1d34e 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 LL ){. pc
1d34f 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
1d350 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1d351 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 k;. }. }else
1d352 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 {. /* Neither
1d353 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d354 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61 72 69 73 . Do a comparis
1d355 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e 61 69 2e on. */. u.ai.
1d356 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e affinity = pOp->
1d357 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f p5 & SQLITE_AFF_
1d358 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28 20 75 2e MASK;. if( u.
1d359 61 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 ai.affinity ){.
1d35a 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 applyAffini
1d35b 74 79 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66 ty(pIn1, u.ai.af
1d35c 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 finity, encoding
1d35d 29 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 );. applyAf
1d35e 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 75 2e 61 finity(pIn3, u.a
1d35f 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f i.affinity, enco
1d360 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 ding);. if(
1d361 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1d362 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b d ) goto no_mem;
1d363 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 . }.. asse
1d364 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1d365 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 =P4_COLLSEQ || p
1d366 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 Op->p4.pColl==0
1d367 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f );. ExpandBlo
1d368 62 28 70 49 6e 31 29 3b 0a 20 20 20 20 45 78 70 b(pIn1);. Exp
1d369 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 andBlob(pIn3);.
1d36a 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71 u.ai.res = sq
1d36b 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 lite3MemCompare(
1d36c 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d pIn3, pIn1, pOp-
1d36d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a >p4.pColl);. }.
1d36e 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1d36f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 pcode ){. cas
1d370 65 20 4f 50 5f 45 71 3a 20 20 20 20 75 2e 61 69 e OP_Eq: u.ai
1d371 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3d .res = u.ai.res=
1d372 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 =0; break;.
1d373 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 case OP_Ne:
1d374 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 u.ai.res = u.a
1d375 69 2e 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72 i.res!=0; br
1d376 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1d377 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 _Lt: u.ai.res
1d378 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30 3b 20 20 = u.ai.res<0;
1d379 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
1d37a 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 75 2e ase OP_Le: u.
1d37b 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 ai.res = u.ai.re
1d37c 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b s<=0; break;
1d37d 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a . case OP_Gt:
1d37e 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 u.ai.res = u
1d37f 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20 20 20 20 .ai.res>0;
1d380 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 break;. defau
1d381 6c 74 3a 20 20 20 20 20 20 20 75 2e 61 69 2e 72 lt: u.ai.r
1d382 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3e 3d 30 es = u.ai.res>=0
1d383 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d ; break;. }
1d384 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 .. if( pOp->p5
1d385 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 & SQLITE_STOREP2
1d386 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 ){. pOut = &
1d387 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1d388 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1d389 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1d38a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 ;. pOut->u.i
1d38b 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20 = u.ai.res;.
1d38c 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1d38d 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 Op->p2, pOut);.
1d38e 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e }else if( u.ai.
1d38f 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 res ){. pc =
1d390 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 pOp->p2-1;. }.
1d391 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d392 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f code: Permutatio
1d393 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a n * * * P4 *.**.
1d394 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75 ** Set the permu
1d395 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 tation used by t
1d396 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 he OP_Compare op
1d397 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 erator to be the
1d398 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 array.** of int
1d399 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a egers in P4..**.
1d39a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 ** The permutati
1d39b 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 on is only valid
1d39c 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 until the next
1d39d 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 OP_Permutation,
1d39e 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f OP_Compare,.** O
1d39f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 P_Halt, or OP_Re
1d3a0 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 sultRow. Typica
1d3a1 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 lly the OP_Permu
1d3a2 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 tation should oc
1d3a3 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 cur.** immediate
1d3a4 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ly prior to the
1d3a5 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 OP_Compare..*/.c
1d3a6 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 ase OP_Permutati
1d3a7 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 on: {. assert(
1d3a8 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1d3a9 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 INTARRAY );. as
1d3aa 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 sert( pOp->p4.ai
1d3ab 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d );. aPermute =
1d3ac 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 pOp->p4.ai;. b
1d3ad 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d3ae 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 de: Compare P1 P
1d3af 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1d3b0 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f Compare to vecto
1d3b1 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 rs of registers
1d3b2 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 in reg(P1)..reg(
1d3b3 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 P1+P3-1) (all th
1d3b4 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 is.** one "A") a
1d3b5 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 nd in reg(P2)..r
1d3b6 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 eg(P2+P3-1) ("B"
1d3b7 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 ). Save the res
1d3b8 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f ult of.** the co
1d3b9 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 mparison for use
1d3ba 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f by the next OP_
1d3bb 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a Jump instruct..*
1d3bc 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 *.** P4 is a Key
1d3bd 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 Info structure t
1d3be 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c hat defines coll
1d3bf 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 ating sequences
1d3c0 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 and sort.** orde
1d3c1 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 rs for the compa
1d3c2 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d rison. The perm
1d3c3 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 utation applies
1d3c4 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 to registers.**
1d3c5 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e only. The KeyIn
1d3c6 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 fo elements are
1d3c7 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c used sequentiall
1d3c8 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d y..**.** The com
1d3c9 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 parison is a sor
1d3ca 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f t comparison, so
1d3cb 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 NULLs compare e
1d3cc 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 qual,.** NULLs a
1d3cd 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d re less than num
1d3ce 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 bers, numbers ar
1d3cf 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 e less than stri
1d3d0 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 ngs,.** and stri
1d3d1 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 ngs are less tha
1d3d2 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 n blobs..*/.case
1d3d3 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 OP_Compare: {.#
1d3d4 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1d3d5 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d3d6 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e nto u.aj */. in
1d3d7 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 t n;. int i;.
1d3d8 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 int p1;. int p2
1d3d9 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 ;. const KeyInf
1d3da 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 o *pKeyInfo;. i
1d3db 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 nt idx;. CollSe
1d3dc 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 q *pColl; /*
1d3dd 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e Collating sequen
1d3de 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 ce to use on thi
1d3df 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 s term */. int
1d3e0 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f bRev; /
1d3e1 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 * True for DESCE
1d3e2 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 NDING sort order
1d3e3 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f */.#endif /* lo
1d3e4 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1d3e5 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f ved into u.aj */
1d3e6 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70 .. u.aj.n = pOp
1d3e7 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65 ->p3;. u.aj.pKe
1d3e8 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e yInfo = pOp->p4.
1d3e9 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 pKeyInfo;. asse
1d3ea 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a rt( u.aj.n>0 );.
1d3eb 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 assert( u.aj.p
1d3ec 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 KeyInfo!=0 );.
1d3ed 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 u.aj.p1 = pOp->p
1d3ee 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d 20 70 1;. u.aj.p2 = p
1d3ef 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 4c 49 Op->p2;.#if SQLI
1d3f0 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 61 TE_DEBUG. if( a
1d3f1 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 69 Permute ){. i
1d3f2 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a 20 20 nt k, mx = 0;.
1d3f3 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61 for(k=0; k<u.a
1d3f4 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50 j.n; k++) if( aP
1d3f5 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d ermute[k]>mx ) m
1d3f6 78 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b x = aPermute[k];
1d3f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 . assert( u.a
1d3f8 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 j.p1>0 && u.aj.p
1d3f9 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 1+mx<=p->nMem+1
1d3fa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 );. assert( u
1d3fb 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a .aj.p2>0 && u.aj
1d3fc 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b .p2+mx<=p->nMem+
1d3fd 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 1 );. }else{.
1d3fe 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 assert( u.aj.p
1d3ff 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 2b 75 1>0 && u.aj.p1+u
1d400 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 .aj.n<=p->nMem+1
1d401 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1d402 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 u.aj.p2>0 && u.a
1d403 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e j.p2+u.aj.n<=p->
1d404 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a 23 65 nMem+1 );. }.#e
1d405 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 ndif /* SQLITE_D
1d406 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 75 2e EBUG */. for(u.
1d407 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75 aj.i=0; u.aj.i<u
1d408 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29 .aj.n; u.aj.i++)
1d409 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d {. u.aj.idx =
1d40a 20 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 aPermute ? aPer
1d40b 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75 mute[u.aj.i] : u
1d40c 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53 .aj.i;. REGIS
1d40d 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 TER_TRACE(u.aj.p
1d40e 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 4d 65 1+u.aj.idx, &aMe
1d40f 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 m[u.aj.p1+u.aj.i
1d410 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54 dx]);. REGIST
1d411 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 32 ER_TRACE(u.aj.p2
1d412 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 4d 65 6d +u.aj.idx, &aMem
1d413 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 [u.aj.p2+u.aj.id
1d414 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 x]);. assert(
1d415 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65 u.aj.i<u.aj.pKe
1d416 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b yInfo->nField );
1d417 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20 . u.aj.pColl
1d418 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d = u.aj.pKeyInfo-
1d419 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a >aColl[u.aj.i];.
1d41a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20 u.aj.bRev =
1d41b 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 u.aj.pKeyInfo->a
1d41c 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69 SortOrder[u.aj.i
1d41d 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 72 65 20 ];. iCompare
1d41e 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 = sqlite3MemComp
1d41f 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70 are(&aMem[u.aj.p
1d420 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 61 4d 1+u.aj.idx], &aM
1d421 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e em[u.aj.p2+u.aj.
1d422 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 6f 6c 6c idx], u.aj.pColl
1d423 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70 );. if( iComp
1d424 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 are ){. if(
1d425 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 69 43 6f u.aj.bRev ) iCo
1d426 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72 mpare = -iCompar
1d427 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
1d428 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72 }. }. aPer
1d429 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 mute = 0;. brea
1d42a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d42b 20 4a 75 6d 70 20 50 31 20 50 32 20 50 33 20 2a Jump P1 P2 P3 *
1d42c 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d42d 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e the instruction
1d42e 20 61 74 20 61 64 64 72 65 73 73 20 50 31 2c 20 at address P1,
1d42f 50 32 2c 20 6f 72 20 50 33 20 64 65 70 65 6e 64 P2, or P3 depend
1d430 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a ing on whether.*
1d431 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 * in the most re
1d432 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 cent OP_Compare
1d433 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 65 20 instruction the
1d434 50 31 20 76 65 63 74 6f 72 20 77 61 73 20 6c 65 P1 vector was le
1d435 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c ss than.** equal
1d436 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 to, or greater
1d437 74 68 61 6e 20 74 68 65 20 50 32 20 76 65 63 74 than the P2 vect
1d438 6f 72 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 or, respectively
1d439 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d ..*/.case OP_Jum
1d43a 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 p: {
1d43b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 /* jump */. if
1d43c 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a ( iCompare<0 ){.
1d43d 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31 pc = pOp->p1
1d43e 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 - 1;. }else if
1d43f 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b ( iCompare==0 ){
1d440 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1d441 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 2 - 1;. }else{.
1d442 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33 pc = pOp->p3
1d443 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1d444 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d445 20 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 And P1 P2 P3 *
1d446 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 *.**.** Take the
1d447 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f 66 20 logical AND of
1d448 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 the values in re
1d449 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 gisters P1 and P
1d44a 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74 2 and.** write t
1d44b 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 he result into r
1d44c 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a egister P3..**.*
1d44d 2a 20 49 66 20 65 69 74 68 65 72 20 50 31 20 6f * If either P1 o
1d44e 72 20 50 32 20 69 73 20 30 20 28 66 61 6c 73 65 r P2 is 0 (false
1d44f 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1d450 74 20 69 73 20 30 20 65 76 65 6e 20 69 66 0a 2a t is 0 even if.*
1d451 2a 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 * the other inpu
1d452 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 t is NULL. A NU
1d453 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f 72 20 74 LL and true or t
1d454 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a wo NULLs give.**
1d455 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a a NULL output..
1d456 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 */./* Opcode: Or
1d457 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1d458 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 .** Take the log
1d459 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 65 20 76 ical OR of the v
1d45a 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1d45b 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a r P1 and P2 and.
1d45c 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 61 6e 73 ** store the ans
1d45d 77 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 wer in register
1d45e 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 P3..**.** If eit
1d45f 68 65 72 20 50 31 20 6f 72 20 50 32 20 69 73 20 her P1 or P2 is
1d460 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74 nonzero (true) t
1d461 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 hen the result i
1d462 73 20 31 20 28 74 72 75 65 29 0a 2a 2a 20 65 76 s 1 (true).** ev
1d463 65 6e 20 69 66 20 74 68 65 20 6f 74 68 65 72 20 en if the other
1d464 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 input is NULL.
1d465 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 A NULL and false
1d466 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a or two NULLs.**
1d467 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74 give a NULL out
1d468 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f put..*/.case OP_
1d469 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 And:
1d46a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1d46b 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f AND, in1, in2, o
1d46c 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f ut3 */.case OP_O
1d46d 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 r: {
1d46e 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f /* same as TK_O
1d46f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 R, in1, in2, out
1d470 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1d471 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d472 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a oved into u.ak *
1d473 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f /. int v1; /
1d474 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 * Left operand:
1d475 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 0==FALSE, 1==TR
1d476 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f UE, 2==UNKNOWN o
1d477 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 r NULL */. int
1d478 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 v2; /* Right
1d479 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 operand: 0==FALS
1d47a 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 E, 1==TRUE, 2==U
1d47b 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a NKNOWN or NULL *
1d47c 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1d47d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d47e 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 0a d into u.ak */..
1d47f 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1d480 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 Op->p1];. if( p
1d481 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d482 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 _Null ){. u.a
1d483 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 k.v1 = 2;. }els
1d484 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d e{. u.ak.v1 =
1d485 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
1d486 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 20 alue(pIn1)!=0;.
1d487 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 }. pIn2 = &aMe
1d488 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 m[pOp->p2];. if
1d489 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 ( pIn2->flags &
1d48a 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1d48b 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20 7d u.ak.v2 = 2;. }
1d48c 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 else{. u.ak.v
1d48d 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 2 = sqlite3VdbeI
1d48e 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 ntValue(pIn2)!=0
1d48f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d ;. }. if( pOp-
1d490 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 >opcode==OP_And
1d491 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f ){. static co
1d492 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1d493 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 r and_logic[] =
1d494 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c { 0, 0, 0, 0, 1,
1d495 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 2, 0, 2, 2 };.
1d496 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e 64 u.ak.v1 = and
1d497 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 _logic[u.ak.v1*3
1d498 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65 6c +u.ak.v2];. }el
1d499 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 se{. static c
1d49a 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1d49b 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20 ar or_logic[] =
1d49c 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c { 0, 1, 2, 1, 1,
1d49d 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 1, 2, 1, 2 };.
1d49e 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72 5f u.ak.v1 = or_
1d49f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b logic[u.ak.v1*3+
1d4a0 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20 20 u.ak.v2];. }.
1d4a1 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pOut = &aMem[pOp
1d4a2 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 75 2e 61 ->p3];. if( u.a
1d4a3 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d k.v1==2 ){. M
1d4a4 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1d4a5 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 ut, MEM_Null);.
1d4a6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 }else{. pOut
1d4a7 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b ->u.i = u.ak.v1;
1d4a8 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1d4a9 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1d4aa 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1d4ab 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .}../* Opcode: N
1d4ac 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ot P1 P2 * * *.*
1d4ad 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1d4ae 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d4af 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f ster P1 as a boo
1d4b0 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f lean value. Sto
1d4b1 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 re the.** boolea
1d4b2 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 n complement in
1d4b3 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 register P2. If
1d4b4 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1d4b5 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a gister P1 is .**
1d4b6 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 NULL, then a NU
1d4b7 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 LL is stored in
1d4b8 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e P2..*/.case OP_N
1d4b9 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ot: {
1d4ba 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d4bb 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20 6f 75 74 TK_NOT, in1, out
1d4bc 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 2 */. pIn1 = &a
1d4bd 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1d4be 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pOut = &aMem[pOp
1d4bf 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e ->p2];. if( pIn
1d4c0 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 1->flags & MEM_N
1d4c1 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ull ){. sqlit
1d4c2 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
1d4c3 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b (pOut);. }else{
1d4c4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1d4c5 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 MemSetInt64(pOut
1d4c6 2c 20 21 73 71 6c 69 74 65 33 56 64 62 65 49 6e , !sqlite3VdbeIn
1d4c7 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 tValue(pIn1));.
1d4c8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1d4c9 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e 6f 74 * Opcode: BitNot
1d4ca 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1d4cb 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 ** Interpret the
1d4cc 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1d4cd 73 74 65 72 20 50 31 20 61 73 20 61 6e 20 69 6e ster P1 as an in
1d4ce 74 65 67 65 72 2e 20 20 53 74 6f 72 65 20 74 68 teger. Store th
1d4cf 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 e.** ones-comple
1d4d0 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 31 20 76 ment of the P1 v
1d4d1 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 alue into regist
1d4d2 65 72 20 50 32 2e 20 20 49 66 20 50 31 20 68 6f er P2. If P1 ho
1d4d3 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 lds.** a NULL th
1d4d4 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 en store a NULL
1d4d5 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f in P2..*/.case O
1d4d6 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20 P_BitNot: {
1d4d7 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d4d8 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e as TK_BITNOT, in
1d4d9 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 1, out2 */. pIn
1d4da 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 1 = &aMem[pOp->p
1d4db 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 1];. pOut = &aM
1d4dc 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 em[pOp->p2];. i
1d4dd 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1d4de 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1d4df 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d4e0 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 etNull(pOut);.
1d4e1 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1d4e2 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1d4e3 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33 4(pOut, ~sqlite3
1d4e4 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1d4e5 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 1));. }. break
1d4e6 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d4e7 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a If P1 P2 P3 * *.
1d4e8 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 **.** Jump to P2
1d4e9 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e if the value in
1d4ea 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d4eb 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 true. The value
1d4ec 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 is.** is consid
1d4ed 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 ered true if it
1d4ee 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e is numeric and n
1d4ef 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 on-zero. If the
1d4f0 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d4f1 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d4f2 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d4f3 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 is true..*/./*
1d4f4 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 Opcode: IfNot P1
1d4f5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d4f6 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 Jump to P2 if t
1d4f7 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d4f8 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 ster P1 is False
1d4f9 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a . The value is.
1d4fa 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
1d4fb 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 true if it has
1d4fc 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 a numeric value
1d4fd 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 of zero. If the
1d4fe 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d4ff 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d500 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d501 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 is true..*/.cas
1d502 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 e OP_If:
1d503 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1d504 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 , in1 */.case OP
1d505 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 _IfNot: {
1d506 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1d507 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 1 */.#if 0 /* l
1d508 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d509 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a oved into u.al *
1d50a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69 /. int c;.#endi
1d50b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d50c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d50d 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e 31 20 3d u.al */. pIn1 =
1d50e 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1d50f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 . if( pIn1->fla
1d510 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b gs & MEM_Null ){
1d511 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f . u.al.c = pO
1d512 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a p->p3;. }else{.
1d513 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
1d514 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
1d515 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 T. u.al.c = s
1d516 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1d517 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c ue(pIn1)!=0;.#el
1d518 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 se. u.al.c =
1d519 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 sqlite3VdbeRealV
1d51a 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b alue(pIn1)!=0.0;
1d51b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 .#endif. if(
1d51c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1d51d 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d IfNot ) u.al.c =
1d51e 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20 !u.al.c;. }.
1d51f 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20 if( u.al.c ){.
1d520 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1d521 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d522 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e ../* Opcode: IsN
1d523 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ull P1 P2 * * *.
1d524 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 **.** Jump to P2
1d525 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e if the value in
1d526 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d527 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1d528 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 _IsNull: {
1d529 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1d52a 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 TK_ISNULL, jump
1d52b 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 , in1 */. pIn1
1d52c 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d = &aMem[pOp->p1]
1d52d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 ;. if( (pIn1->f
1d52e 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1d52f 21 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 !=0 ){. pc =
1d530 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1d531 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d532 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 Opcode: NotNull
1d533 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1d534 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 * Jump to P2 if
1d535 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1d536 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 ister P1 is not
1d537 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 NULL. .*/.case
1d538 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 OP_NotNull: {
1d539 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d53a 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 as TK_NOTNULL,
1d53b 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 jump, in1 */. p
1d53c 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In1 = &aMem[pOp-
1d53d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e >p1];. if( (pIn
1d53e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 1->flags & MEM_N
1d53f 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 ull)==0 ){. p
1d540 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1d541 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d542 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 ./* Opcode: Colu
1d543 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 mn P1 P2 P3 P4 P
1d544 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 5.**.** Interpre
1d545 74 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20 t the data that
1d546 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 cursor P1 points
1d547 20 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 to as a structu
1d548 72 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a re built using.*
1d549 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 * the MakeRecord
1d54a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 instruction. (
1d54b 53 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f See the MakeReco
1d54c 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 rd opcode for ad
1d54d 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f ditional.** info
1d54e 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
1d54f 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 e format of the
1d550 64 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 data.) Extract
1d551 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e the P2-th column
1d552 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 .** from this re
1d553 63 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 cord. If there
1d554 61 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 are less that (P
1d555 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 2+1) .** values
1d556 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 in the record, e
1d557 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a xtract a NULL..*
1d558 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 *.** The value e
1d559 78 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 xtracted is stor
1d55a 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1d55b 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3..**.** If the
1d55c 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 column contains
1d55d 66 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 fewer than P2 fi
1d55e 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 elds, then extra
1d55f 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a ct a NULL. Or,.
1d560 2a 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67 ** if the P4 arg
1d561 75 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 ument is a P4_ME
1d562 4d 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 M use the value
1d563 6f 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 of the P4 argume
1d564 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 nt as.** the res
1d565 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ult..**.** If th
1d566 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 e OPFLAG_CLEARCA
1d567 43 48 45 20 62 69 74 20 69 73 20 73 65 74 20 6f CHE bit is set o
1d568 6e 20 50 35 20 61 6e 64 20 50 31 20 69 73 20 61 n P5 and P1 is a
1d569 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 pseudo-table cu
1d56a 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 rsor,.** then th
1d56b 65 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 63 e cache of the c
1d56c 75 72 73 6f 72 20 69 73 20 72 65 73 65 74 20 70 ursor is reset p
1d56d 72 69 6f 72 20 74 6f 20 65 78 74 72 61 63 74 69 rior to extracti
1d56e 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a ng the column..*
1d56f 2a 20 54 68 65 20 66 69 72 73 74 20 4f 50 5f 43 * The first OP_C
1d570 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 olumn against a
1d571 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 pseudo-table aft
1d572 65 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 er the value of
1d573 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 the content.** r
1d574 65 67 69 73 74 65 72 20 68 61 73 20 63 68 61 6e egister has chan
1d575 67 65 64 20 73 68 6f 75 6c 64 20 68 61 76 65 20 ged should have
1d576 74 68 69 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f this bit set..*/
1d577 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a .case OP_Column:
1d578 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1d579 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d57a 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a ed into u.am */.
1d57b 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a u32 payloadSiz
1d57c 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f e; /* Number o
1d57d 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 f bytes in the r
1d57e 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 ecord */. i64 p
1d57f 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a ayloadSize64; /*
1d580 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d581 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1d582 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 /. int p1;
1d583 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c /* P1 val
1d584 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 ue of the opcode
1d585 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 */. int p2;
1d586 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 /* colu
1d587 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 mn number to ret
1d588 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 rieve */. VdbeC
1d589 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a ursor *pC; /*
1d58a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
1d58b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 */. char *zRec
1d58c 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
1d58d 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 ter to complete
1d58e 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 record-data */.
1d58f 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1d590 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 ; /* The BTree
1d591 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 cursor */. u32
1d592 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 *aType;
1d593 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 /* aType[i] hold
1d594 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 s the numeric ty
1d595 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 pe of the i-th c
1d596 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a olumn */. u32 *
1d597 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a aOffset; /*
1d598 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f aOffset[i] is o
1d599 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f ffset to start o
1d59a 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 f data for i-th
1d59b 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 column */. int
1d59c 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f nField; /
1d59d 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * number of fiel
1d59e 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1d59f 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 */. int len;
1d5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1d5a1 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 length of the se
1d5a2 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f rialized data fo
1d5a3 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a r the column */.
1d5a4 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
1d5a5 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1d5a6 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a nter */. char *
1d5a7 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 zData; /*
1d5a8 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f Part of the reco
1d5a9 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 rd being decoded
1d5aa 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 */. Mem *pDest
1d5ab 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 ; /* Wher
1d5ac 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 e to write the e
1d5ad 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a xtracted value *
1d5ae 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 /. Mem sMem;
1d5af 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 /* For st
1d5b0 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 oring the record
1d5b1 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a being decoded *
1d5b2 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 /. u8 *zIdx;
1d5b3 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1d5b4 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 into header */.
1d5b5 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 u8 *zEndHdr;
1d5b6 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1d5b7 6f 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 o first byte aft
1d5b8 65 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f er the header */
1d5b9 0a 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20 . u32 offset;
1d5ba 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1d5bb 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f into the data */
1d5bc 0a 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b . u64 offset64;
1d5bd 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 /* 64-bit
1d5be 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73 offset. 64 bits
1d5bf 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68 needed to catch
1d5c0 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 overflow */. i
1d5c1 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 nt szHdr;
1d5c2 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
1d5c3 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65 header size fie
1d5c4 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72 ld at start of r
1d5c5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 ecord */. int a
1d5c6 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a vail; /*
1d5c7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d5c8 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 of available da
1d5c9 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 ta */. Mem *pRe
1d5ca 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 g; /* Ps
1d5cb 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 eudoTable input
1d5cc 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 register */.#end
1d5cd 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1d5ce 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d5cf 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 u.am */... u.a
1d5d0 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a m.p1 = pOp->p1;.
1d5d1 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d u.am.p2 = pOp-
1d5d2 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d >p2;. u.am.pC =
1d5d3 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 0;. memset(&u.
1d5d4 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 am.sMem, 0, size
1d5d5 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a of(u.am.sMem));.
1d5d6 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d5d7 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1d5d8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1d5d9 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1d5da 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 p->nMem );. u.a
1d5db 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d 65 6d 5b m.pDest = &aMem[
1d5dc 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 pOp->p3];. MemS
1d5dd 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e etTypeFlag(u.am.
1d5de 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 pDest, MEM_Null)
1d5df 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 ;. u.am.zRec =
1d5e0 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 0;.. /* This bl
1d5e1 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72 ock sets the var
1d5e2 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f iable u.am.paylo
1d5e3 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65 adSize to be the
1d5e4 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1d5e5 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 . ** bytes in t
1d5e6 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a he record.. **.
1d5e7 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69 ** u.am.zRec i
1d5e8 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 s set to be the
1d5e9 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 complete text of
1d5ea 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 the record if i
1d5eb 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a t is available..
1d5ec 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 ** The complet
1d5ed 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 e record text is
1d5ee 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
1d5ef 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 e for pseudo-tab
1d5f0 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 les. ** If the
1d5f1 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1d5f2 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 in a cursor, th
1d5f3 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 e complete recor
1d5f4 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 d text. ** migh
1d5f5 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 t be available i
1d5f6 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e n the u.am.pC->
1d5f7 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20 aRow cache. Or
1d5f8 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e it might not be.
1d5f9 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 . ** If the dat
1d5fa 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 a is unavailable
1d5fb 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20 , u.am.zRec is
1d5fc 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a set to NULL.. *
1d5fd 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 *. ** We also c
1d5fe 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 ompute the numbe
1d5ff 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1d600 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 the record. For
1d601 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 cursors,. ** t
1d602 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d603 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 umns is stored i
1d604 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 n the VdbeCursor
1d605 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e .nField element.
1d606 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20 . */. u.am.pC
1d607 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e = p->apCsr[u.am.
1d608 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1d609 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66 .am.pC!=0 );.#if
1d60a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d60b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1d60c 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d assert( u.am.pC-
1d60d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 >pVtabCursor==0
1d60e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d );.#endif. u.am
1d60f 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43 .pCrsr = u.am.pC
1d610 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 ->pCursor;. if(
1d611 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29 u.am.pCrsr!=0 )
1d612 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 {. /* The rec
1d613 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e ord is stored in
1d614 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 a B-Tree */.
1d615 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1d616 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e eCursorMoveto(u.
1d617 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 am.pC);. if(
1d618 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f rc ) goto abort_
1d619 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1d61a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e if( u.am.pC->n
1d61b 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 ullRow ){.
1d61c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d61d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
1d61e 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 if( u.am.pC->cac
1d61f 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 heStatus==p->cac
1d620 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75 heCtr ){. u
1d621 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 .am.payloadSize
1d622 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f = u.am.pC->paylo
1d623 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e adSize;. u.
1d624 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a am.zRec = (char*
1d625 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a )u.am.pC->aRow;.
1d626 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }else if( u.
1d627 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 am.pC->isIndex )
1d628 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1d629 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1d62a 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 orIsValid(u.am.p
1d62b 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72 Crsr) );. r
1d62c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d62d 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 KeySize(u.am.pCr
1d62e 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 sr, &u.am.payloa
1d62f 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 dSize64);.
1d630 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1d631 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72 TE_OK ); /* Tr
1d632 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 ue because of Cu
1d633 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c rsorMoveto() cal
1d634 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20 l above */.
1d635 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 /* sqlite3Btree
1d636 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75 ParseCellPtr() u
1d637 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28 ses getVarint32(
1d638 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 ) to extract the
1d639 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61 . ** payloa
1d63a 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73 d size, so it is
1d63b 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 impossible for
1d63c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d63d 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a 64 to be. *
1d63e 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32 * larger than 32
1d63f 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20 bits. */.
1d640 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61 assert( (u.am.pa
1d641 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51 yloadSize64 & SQ
1d642 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 LITE_MAX_U32)==(
1d643 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 u64)u.am.payload
1d644 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20 Size64 );.
1d645 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d646 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79 = (u32)u.am.pay
1d647 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 loadSize64;.
1d648 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1d649 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1d64a 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1d64b 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 .am.pCrsr) );.
1d64c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d64d 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e BtreeDataSize(u.
1d64e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e am.pCrsr, &u.am.
1d64f 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 payloadSize);.
1d650 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1d651 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f SQLITE_OK ); /
1d652 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e * DataSize() can
1d653 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 not fail */.
1d654 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }. }else if( u.
1d655 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 am.pC->pseudoTab
1d656 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75 leReg>0 ){. u
1d657 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61 4d 65 6d .am.pReg = &aMem
1d658 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f [u.am.pC->pseudo
1d659 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 20 20 61 TableReg];. a
1d65a 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 52 65 67 ssert( u.am.pReg
1d65b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
1d65c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 ob );. u.am.p
1d65d 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 ayloadSize = u.a
1d65e 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 20 20 75 m.pReg->n;. u
1d65f 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e .am.zRec = u.am.
1d660 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 75 2e 61 pReg->z;. u.a
1d661 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 m.pC->cacheStatu
1d662 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 4f 50 46 s = (pOp->p5&OPF
1d663 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 29 20 LAG_CLEARCACHE)
1d664 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 20 3a 20 ? CACHE_STALE :
1d665 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 20 20 20 p->cacheCtr;.
1d666 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 61 assert( u.am.pa
1d667 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 yloadSize==0 ||
1d668 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 29 3b 0a u.am.zRec!=0 );.
1d669 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1d66a 43 6f 6e 73 69 64 65 72 20 74 68 65 20 72 6f 77 Consider the row
1d66b 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 to be NULL */.
1d66c 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 u.am.payloadS
1d66d 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 ize = 0;. }..
1d66e 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 79 6c 6f /* If u.am.paylo
1d66f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 74 68 65 adSize is 0, the
1d670 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 61 20 4e n just store a N
1d671 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 ULL */. if( u.a
1d672 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 m.payloadSize==0
1d673 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1d674 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 u.am.pDest->flag
1d675 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 s&MEM_Null );.
1d676 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e goto op_column
1d677 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 _out;. }. asse
1d678 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 rt( db->aLimit[S
1d679 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1d67a 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 TH]>=0 );. if(
1d67b 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d67c 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 4c 69 6d > (u32)db->aLim
1d67d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1d67e 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 LENGTH] ){. g
1d67f 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d oto too_big;. }
1d680 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 .. u.am.nField
1d681 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 69 65 6c = u.am.pC->nFiel
1d682 64 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 d;. assert( u.a
1d683 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 65 6c 64 m.p2<u.am.nField
1d684 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 61 );.. /* Read a
1d685 6e 64 20 70 61 72 73 65 20 74 68 65 20 74 61 62 nd parse the tab
1d686 6c 65 20 68 65 61 64 65 72 2e 20 20 53 74 6f 72 le header. Stor
1d687 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 e the results of
1d688 20 74 68 65 20 70 61 72 73 65 0a 20 20 2a 2a 20 the parse. **
1d689 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 into the record
1d68a 68 65 61 64 65 72 20 63 61 63 68 65 20 66 69 65 header cache fie
1d68b 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f lds of the curso
1d68c 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 61 r.. */. u.am.a
1d68d 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e Type = u.am.pC->
1d68e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 75 2e 61 aType;. if( u.a
1d68f 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 m.pC->cacheStatu
1d690 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 s==p->cacheCtr )
1d691 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 {. u.am.aOffs
1d692 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f et = u.am.pC->aO
1d693 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a ffset;. }else{.
1d694 20 20 20 20 61 73 73 65 72 74 28 75 2e 61 6d 2e assert(u.am.
1d695 61 54 79 70 65 29 3b 0a 20 20 20 20 75 2e 61 6d aType);. u.am
1d696 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 20 20 20 .avail = 0;.
1d697 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74 u.am.pC->aOffset
1d698 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20 = u.am.aOffset
1d699 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e = &u.am.aType[u.
1d69a 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 am.nField];.
1d69b 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f 61 64 u.am.pC->payload
1d69c 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 61 79 6c Size = u.am.payl
1d69d 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 75 2e 61 oadSize;. u.a
1d69e 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 m.pC->cacheStatu
1d69f 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 74 72 3b s = p->cacheCtr;
1d6a0 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 .. /* Figure
1d6a1 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 out how many byt
1d6a2 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 68 65 es are in the he
1d6a3 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 ader */. if(
1d6a4 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a 20 20 20 u.am.zRec ){.
1d6a5 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 u.am.zData =
1d6a6 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 20 20 7d u.am.zRec;. }
1d6a7 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 else{. if(
1d6a8 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 u.am.pC->isIndex
1d6a9 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d ){. u.am
1d6aa 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 .zData = (char*)
1d6ab 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 sqlite3BtreeKeyF
1d6ac 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 73 72 2c etch(u.am.pCrsr,
1d6ad 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 &u.am.avail);.
1d6ae 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d6af 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d u.am.zData =
1d6b0 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 (char*)sqlite3B
1d6b1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 75 2e treeDataFetch(u.
1d6b2 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e am.pCrsr, &u.am.
1d6b3 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d 0a avail);. }.
1d6b4 20 20 20 20 20 20 2f 2a 20 49 66 20 4b 65 79 46 /* If KeyF
1d6b5 65 74 63 68 28 29 2f 44 61 74 61 46 65 74 63 68 etch()/DataFetch
1d6b6 28 29 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 () managed to ge
1d6b7 74 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 t the entire pay
1d6b8 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a 2a 20 73 load,. ** s
1d6b9 61 76 65 20 74 68 65 20 70 61 79 6c 6f 61 64 20 ave the payload
1d6ba 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 43 2d 3e in the u.am.pC->
1d6bb 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68 61 aRow cache. Tha
1d6bc 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20 66 t will save us f
1d6bd 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 rom. ** hav
1d6be 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64 69 ing to make addi
1d6bf 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20 tional calls to
1d6c0 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e fetch the conten
1d6c1 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 20 t portion of.
1d6c2 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 ** the record
1d6c3 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1d6c4 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 61 76 assert( u.am.av
1d6c5 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 ail>=0 );.
1d6c6 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 if( u.am.payload
1d6c7 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 75 2e 61 Size <= (u32)u.a
1d6c8 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 20 20 20 m.avail ){.
1d6c9 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 u.am.zRec = u
1d6ca 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 20 20 20 .am.zData;.
1d6cb 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 u.am.pC->aRow
1d6cc 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61 = (u8*)u.am.zDa
1d6cd 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ta;. }else{
1d6ce 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 . u.am.pC
1d6cf 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 ->aRow = 0;.
1d6d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a }. }. /*
1d6d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
1d6d2 73 73 65 72 74 20 69 73 20 74 72 75 65 20 69 6e ssert is true in
1d6d3 20 61 6c 6c 20 63 61 73 65 73 20 61 63 63 65 70 all cases accep
1d6d4 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 t when. ** th
1d6d5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1d6d6 68 61 73 20 62 65 65 6e 20 63 6f 72 72 75 70 74 has been corrupt
1d6d7 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 ed externally..
1d6d8 20 20 20 2a 2a 20 20 20 20 61 73 73 65 72 74 28 ** assert(
1d6d9 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 7c 7c u.am.zRec!=0 ||
1d6da 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d 75 2e 61 u.am.avail>=u.a
1d6db 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c m.payloadSize ||
1d6dc 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d 39 20 29 u.am.avail>=9 )
1d6dd 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 73 7a ; */. u.am.sz
1d6de 48 64 72 20 3d 20 67 65 74 56 61 72 69 6e 74 33 Hdr = getVarint3
1d6df 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 2((u8*)u.am.zDat
1d6e0 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 74 29 3b a, u.am.offset);
1d6e1 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 .. /* Make su
1d6e2 72 65 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 re a corrupt dat
1d6e3 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20 67 69 abase has not gi
1d6e4 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 72 73 69 ven us an oversi
1d6e5 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a ze header.. *
1d6e6 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 20 74 6f * Do this now to
1d6e7 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 72 73 69 avoid an oversi
1d6e8 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ze memory alloca
1d6e9 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 tion.. **.
1d6ea 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 69 65 73 ** Type entries
1d6eb 20 63 61 6e 20 62 65 20 62 65 74 77 65 65 6e 20 can be between
1d6ec 31 20 61 6e 64 20 35 20 62 79 74 65 73 20 65 61 1 and 5 bytes ea
1d6ed 63 68 2e 20 20 42 75 74 20 34 20 61 6e 64 20 35 ch. But 4 and 5
1d6ee 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 74 79 70 byte. ** typ
1d6ef 65 73 20 75 73 65 20 73 6f 20 6d 75 63 68 20 64 es use so much d
1d6f0 61 74 61 20 73 70 61 63 65 20 74 68 61 74 20 74 ata space that t
1d6f1 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 here can only be
1d6f2 20 34 30 39 36 20 61 6e 64 20 33 32 20 6f 66 0a 4096 and 32 of.
1d6f3 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 72 65 73 ** them, res
1d6f4 70 65 63 74 69 76 65 6c 79 2e 20 20 53 6f 20 74 pectively. So t
1d6f5 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 61 64 65 he maximum heade
1d6f6 72 20 6c 65 6e 67 74 68 20 72 65 73 75 6c 74 73 r length results
1d6f7 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a 2a 20 33 from a. ** 3
1d6f8 2d 62 79 74 65 20 74 79 70 65 20 66 6f 72 20 65 -byte type for e
1d6f9 61 63 68 20 6f 66 20 74 68 65 20 6d 61 78 69 6d ach of the maxim
1d6fa 75 6d 20 6f 66 20 33 32 37 36 38 20 63 6f 6c 75 um of 32768 colu
1d6fb 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 65 0a 20 mns plus three.
1d6fc 20 20 20 2a 2a 20 65 78 74 72 61 20 62 79 74 65 ** extra byte
1d6fd 73 20 66 6f 72 20 74 68 65 20 68 65 61 64 65 72 s for the header
1d6fe 20 6c 65 6e 67 74 68 20 69 74 73 65 6c 66 2e 20 length itself.
1d6ff 20 33 32 37 36 38 2a 33 20 2b 20 33 20 3d 20 39 32768*3 + 3 = 9
1d700 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 8307.. */.
1d701 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 73 65 74 if( u.am.offset
1d702 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 20 20 20 > 98307 ){.
1d703 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
1d704 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
1d705 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e goto op_column
1d706 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _out;. }..
1d707 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 6e 20 75 /* Compute in u
1d708 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e 75 6d 62 .am.len the numb
1d709 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 er of bytes of d
1d70a 61 74 61 20 77 65 20 6e 65 65 64 20 74 6f 20 72 ata we need to r
1d70b 65 61 64 20 69 6e 20 6f 72 64 65 72 0a 20 20 20 ead in order.
1d70c 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e 61 6d 2e ** to get u.am.
1d70d 6e 46 69 65 6c 64 20 74 79 70 65 20 76 61 6c 75 nField type valu
1d70e 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 es. u.am.offset
1d70f 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 is an upper bou
1d710 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 42 75 74 nd on this. But
1d711 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e 6e 46 69 . ** u.am.nFi
1d712 65 6c 64 20 6d 69 67 68 74 20 62 65 20 73 69 67 eld might be sig
1d713 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 73 73 20 nificantly less
1d714 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 6e 75 than the true nu
1d715 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a mber of columns.
1d716 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 61 ** in the ta
1d717 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 68 61 74 ble, and in that
1d718 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d 2e 6e 46 case, 5*u.am.nF
1d719 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 62 65 20 ield+3 might be
1d71a 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 75 2e 61 smaller than u.a
1d71b 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 20 2a 2a m.offset.. **
1d71c 20 57 65 20 77 61 6e 74 20 74 6f 20 6d 69 6e 69 We want to mini
1d71d 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e 20 69 6e mize u.am.len in
1d71e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d 69 74 20 order to limit
1d71f 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1d720 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 61 6c memory. ** al
1d721 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 location, especi
1d722 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 72 75 70 ally if a corrup
1d723 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 t database file
1d724 68 61 73 20 63 61 75 73 65 64 20 75 2e 61 6d 2e has caused u.am.
1d725 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a 20 74 6f offset. ** to
1d726 20 62 65 20 6f 76 65 72 73 69 7a 65 64 2e 20 4f be oversized. O
1d727 66 66 73 65 74 20 69 73 20 6c 69 6d 69 74 65 64 ffset is limited
1d728 20 74 6f 20 39 38 33 30 37 20 61 62 6f 76 65 2e to 98307 above.
1d729 20 20 42 75 74 20 39 38 33 30 37 20 6d 69 67 68 But 98307 migh
1d72a 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 65 t. ** still e
1d72b 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 6d 65 6d xceed Robson mem
1d72c 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c ory allocation l
1d72d 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 20 63 6f imits on some co
1d72e 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e 0a 20 20 nfigurations..
1d72f 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 ** On systems
1d730 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 6f 6c 65 that cannot tole
1d731 72 61 74 65 20 6c 61 72 67 65 20 6d 65 6d 6f 72 rate large memor
1d732 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 75 y allocations, u
1d733 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b 33 0a 20 .am.nField*5+3.
1d734 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 6b 65 6c ** will likel
1d735 79 20 62 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65 y be much smalle
1d736 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e 6e 46 69 r since u.am.nFi
1d737 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 eld will likely
1d738 62 65 20 6c 65 73 73 20 74 68 61 6e 0a 20 20 20 be less than.
1d739 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e 20 20 54 ** 20 or so. T
1d73a 68 69 73 20 69 6e 73 75 72 65 73 20 74 68 61 74 his insures that
1d73b 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61 Robson memory a
1d73c 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73 llocation limits
1d73d 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 are. ** not
1d73e 65 78 63 65 65 64 65 64 20 65 76 65 6e 20 66 6f exceeded even fo
1d73f 72 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 r corrupt databa
1d740 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 20 2a 2f se files.. */
1d741 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 . u.am.len =
1d742 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 20 2b 20 u.am.nField*5 +
1d743 33 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 3;. if( u.am.
1d744 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e 61 6d 2e len > (int)u.am.
1d745 6f 66 66 73 65 74 20 29 20 75 2e 61 6d 2e 6c 65 offset ) u.am.le
1d746 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66 n = (int)u.am.of
1d747 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 fset;.. /* Th
1d748 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f 72 20 e KeyFetch() or
1d749 44 61 74 61 46 65 74 63 68 28 29 20 61 62 6f 76 DataFetch() abov
1d74a 65 20 61 72 65 20 66 61 73 74 20 61 6e 64 20 77 e are fast and w
1d74b 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e 74 69 ill get the enti
1d74c 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 re. ** record
1d74d 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73 74 20 header in most
1d74e 63 61 73 65 73 2e 20 20 42 75 74 20 74 68 65 79 cases. But they
1d74f 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20 67 65 will fail to ge
1d750 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20 t the complete.
1d751 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 ** record hea
1d752 64 65 72 20 69 66 20 74 68 65 20 72 65 63 6f 72 der if the recor
1d753 64 20 68 65 61 64 65 72 20 64 6f 65 73 20 6e 6f d header does no
1d754 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c t fit on a singl
1d755 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e e page. ** in
1d756 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20 57 68 the B-Tree. Wh
1d757 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c en that happens,
1d758 20 75 73 65 20 73 71 6c 69 74 65 33 56 64 62 65 use sqlite3Vdbe
1d759 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 74 MemFromBtree() t
1d75a 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 72 65 o. ** acquire
1d75b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 65 the complete he
1d75c 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20 20 2a ader text.. *
1d75d 2f 0a 20 20 20 20 69 66 28 20 21 75 2e 61 6d 2e /. if( !u.am.
1d75e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e 61 76 61 zRec && u.am.ava
1d75f 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 7b 0a 20 il<u.am.len ){.
1d760 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 66 u.am.sMem.f
1d761 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 lags = 0;.
1d762 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 3d 20 30 u.am.sMem.db = 0
1d763 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1d764 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
1d765 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 72 2c tree(u.am.pCrsr,
1d766 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 0, u.am.len, u.
1d767 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 am.pC->isIndex,
1d768 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 &u.am.sMem);.
1d769 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1d76a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1d76b 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f goto op_column_o
1d76c 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
1d76d 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75 u.am.zData = u
1d76e 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 .am.sMem.z;.
1d76f 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 6e 64 48 }. u.am.zEndH
1d770 64 72 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d dr = (u8 *)&u.am
1d771 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c 65 6e 5d .zData[u.am.len]
1d772 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78 20 ;. u.am.zIdx
1d773 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44 = (u8 *)&u.am.zD
1d774 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 72 5d 3b ata[u.am.szHdr];
1d775 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 .. /* Scan th
1d776 65 20 68 65 61 64 65 72 20 61 6e 64 20 75 73 65 e header and use
1d777 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 it to fill in t
1d778 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 5b 5d 20 he u.am.aType[]
1d779 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 and u.am.aOffset
1d77a 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 73 []. ** arrays
1d77b 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e . u.am.aType[u.
1d77c 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 am.i] will conta
1d77d 69 6e 20 74 68 65 20 74 79 70 65 20 69 6e 74 65 in the type inte
1d77e 67 65 72 20 66 6f 72 20 74 68 65 20 75 2e 61 6d ger for the u.am
1d77f 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c .i-th. ** col
1d780 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 umn and u.am.aOf
1d781 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c fset[u.am.i] wil
1d782 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 75 2e l contain the u.
1d783 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 am.offset from t
1d784 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 he beginning.
1d785 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 ** of the recor
1d786 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f d to the start o
1d787 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 f the data for t
1d788 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 63 6f 6c he u.am.i-th col
1d789 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 umn. */. u
1d78a 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3d 20 75 .am.offset64 = u
1d78b 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 20 20 20 .am.offset;.
1d78c 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b 20 75 2e for(u.am.i=0; u.
1d78d 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 65 6c 64 am.i<u.am.nField
1d78e 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a 20 20 20 ; u.am.i++){.
1d78f 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a 49 64 78 if( u.am.zIdx
1d790 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 29 7b <u.am.zEndHdr ){
1d791 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 61 4f . u.am.aO
1d792 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 3d 20 ffset[u.am.i] =
1d793 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 73 65 74 (u32)u.am.offset
1d794 36 34 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 64;. u.am
1d795 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72 69 .zIdx += getVari
1d796 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 78 2c 20 nt32(u.am.zIdx,
1d797 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e u.am.aType[u.am.
1d798 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 i]);. u.a
1d799 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d 20 73 71 m.offset64 += sq
1d79a 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1d79b 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70 ypeLen(u.am.aTyp
1d79c 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 e[u.am.i]);.
1d79d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1d79e 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 20 69 73 /* If u.am.i is
1d79f 20 6c 65 73 73 20 74 68 61 74 20 75 2e 61 6d 2e less that u.am.
1d7a0 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 74 68 65 nField, then the
1d7a1 72 65 20 61 72 65 20 6c 65 73 73 20 66 69 65 6c re are less fiel
1d7a2 64 73 20 69 6e 20 74 68 69 73 0a 20 20 20 20 20 ds in this.
1d7a3 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 74 68 61 ** record tha
1d7a4 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 n SetNumColumns
1d7a5 69 6e 64 69 63 61 74 65 64 20 74 68 65 72 65 20 indicated there
1d7a6 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 are columns in t
1d7a7 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 he. ** ta
1d7a8 62 6c 65 2e 20 53 65 74 20 74 68 65 20 75 2e 61 ble. Set the u.a
1d7a9 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 61 6e 79 m.offset for any
1d7aa 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20 6e extra columns n
1d7ab 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a 20 20 ot present in.
1d7ac 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 ** the rec
1d7ad 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73 20 74 ord to 0. This t
1d7ae 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f 77 20 ells code below
1d7af 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 0a to store a NULL.
1d7b0 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 65 ** inste
1d7b1 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c 69 7a ad of deserializ
1d7b2 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72 6f 6d ing a value from
1d7b3 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20 the record..
1d7b4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1d7b5 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 u.am.aOffset[u.a
1d7b6 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 m.i] = 0;.
1d7b7 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1d7b8 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1d7b9 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 e(&u.am.sMem);.
1d7ba 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61 u.am.sMem.fla
1d7bb 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a gs = MEM_Null;..
1d7bc 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 /* If we hav
1d7bd 65 20 72 65 61 64 20 6d 6f 72 65 20 68 65 61 64 e read more head
1d7be 65 72 20 64 61 74 61 20 74 68 61 6e 20 77 61 73 er data than was
1d7bf 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 contained in th
1d7c0 65 20 68 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a e header,. **
1d7c1 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f or if the end o
1d7c2 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 f the last field
1d7c3 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 70 appears to be p
1d7c4 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
1d7c5 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 he. ** record
1d7c6 2c 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 , or if the end
1d7c7 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c of the last fiel
1d7c8 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 d appears to be
1d7c9 62 65 66 6f 72 65 20 74 68 65 20 65 6e 64 0a 20 before the end.
1d7ca 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 ** of the rec
1d7cb 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 69 ord (when all fi
1d7cc 65 6c 64 73 20 70 72 65 73 65 6e 74 29 2c 20 74 elds present), t
1d7cd 68 65 6e 20 77 65 20 6d 75 73 74 20 62 65 20 64 hen we must be d
1d7ce 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69 ealing. ** wi
1d7cf 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 th a corrupt dat
1d7d0 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 abase.. */.
1d7d1 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a 49 64 78 if( (u.am.zIdx
1d7d2 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 29 > u.am.zEndHdr)
1d7d3 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 65 74 36 || (u.am.offset6
1d7d4 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 4 > u.am.payload
1d7d5 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c 20 28 75 Size). || (u
1d7d6 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 6d 2e 7a .am.zIdx==u.am.z
1d7d7 45 6e 64 48 64 72 20 26 26 20 75 2e 61 6d 2e 6f EndHdr && u.am.o
1d7d8 66 66 73 65 74 36 34 21 3d 28 75 36 34 29 75 2e ffset64!=(u64)u.
1d7d9 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 am.payloadSize)
1d7da 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1d7db 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1d7dc 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 T;. goto op
1d7dd 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 _column_out;.
1d7de 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 }. }.. /* Get
1d7df 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f the column info
1d7e0 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 2e 61 6d rmation. If u.am
1d7e1 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 .aOffset[u.am.p2
1d7e2 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 ] is non-zero, t
1d7e3 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 72 69 61 hen. ** deseria
1d7e4 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66 lize the value f
1d7e5 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20 rom the record.
1d7e6 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b If u.am.aOffset[
1d7e7 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a 65 72 6f u.am.p2] is zero
1d7e8 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 ,. ** then ther
1d7e9 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f 75 67 68 e are not enough
1d7ea 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 fields in the r
1d7eb 65 63 6f 72 64 20 74 6f 20 73 61 74 69 73 66 79 ecord to satisfy
1d7ec 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 65 73 the. ** reques
1d7ed 74 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 t. In this case
1d7ee 2c 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 , set the value
1d7ef 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 20 69 66 NULL or to P4 if
1d7f0 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 20 70 6f P4 is. ** a po
1d7f1 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 6f inter to a Mem o
1d7f2 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 bject.. */. if
1d7f3 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 ( u.am.aOffset[u
1d7f4 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 20 20 61 .am.p2] ){. a
1d7f5 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1d7f6 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 E_OK );. if(
1d7f7 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a 20 20 20 u.am.zRec ){.
1d7f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1d7f9 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c mReleaseExternal
1d7fa 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 (u.am.pDest);.
1d7fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 sqlite3VdbeS
1d7fc 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 26 erialGet((u8 *)&
1d7fd 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 6d 2e 61 u.am.zRec[u.am.a
1d7fe 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 5d Offset[u.am.p2]]
1d7ff 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 , u.am.aType[u.a
1d800 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 m.p2], u.am.pDes
1d801 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 t);. }else{.
1d802 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 u.am.len =
1d803 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d804 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 54 lTypeLen(u.am.aT
1d805 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 3b 0a 20 ype[u.am.p2]);.
1d806 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1d807 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d 2e 73 4d MemMove(&u.am.sM
1d808 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b em, u.am.pDest);
1d809 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1d80a 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1d80b 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 ree(u.am.pCrsr,
1d80c 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 u.am.aOffset[u.a
1d80d 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c 65 6e 2c m.p2], u.am.len,
1d80e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d80f 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a x, &u.am.sMem);.
1d810 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1d811 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d812 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1d813 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1d814 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 u.am.zData
1d815 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 = u.am.sMem.z;.
1d816 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1d817 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a 29 75 SerialGet((u8*)u
1d818 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e 61 6d 2e .am.zData, u.am.
1d819 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 2c 20 aType[u.am.p2],
1d81a 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 u.am.pDest);.
1d81b 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 }. u.am.pDes
1d81c 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1d81d 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 g;. }else{.
1d81e 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d if( pOp->p4type=
1d81f 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 =P4_MEM ){.
1d820 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d821 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e 61 6d 2e hallowCopy(u.am.
1d822 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 34 2e 70 pDest, pOp->p4.p
1d823 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 Mem, MEM_Static)
1d824 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1d825 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e assert( u.am.
1d826 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d pDest->flags&MEM
1d827 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 _Null );. }.
1d828 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 }.. /* If we d
1d829 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
1d82a 61 74 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f ated space to ho
1d82b 6c 64 20 74 68 65 20 64 61 74 61 20 28 69 6e 20 ld the data (in
1d82c 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 the. ** sqlite3
1d82d 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 VdbeMemFromBtree
1d82e 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 () call above) t
1d82f 68 65 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e hen transfer con
1d830 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a trol of that. *
1d831 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c * dynamically al
1d832 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 6f 76 located space ov
1d833 65 72 20 74 6f 20 74 68 65 20 75 2e 61 6d 2e 70 er to the u.am.p
1d834 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a Dest structure..
1d835 20 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e ** This preven
1d836 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 ts a memory copy
1d837 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 .. */. if( u.a
1d838 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 m.sMem.zMalloc )
1d839 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e {. assert( u.
1d83a 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e am.sMem.z==u.am.
1d83b 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a sMem.zMalloc );.
1d83c 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 2e assert( !(u.
1d83d 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 am.pDest->flags
1d83e 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b 0a 20 20 & MEM_Dyn) );.
1d83f 20 20 61 73 73 65 72 74 28 20 21 28 75 2e 61 6d assert( !(u.am
1d840 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 .pDest->flags &
1d841 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 (MEM_Blob|MEM_St
1d842 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 44 65 73 r)) || u.am.pDes
1d843 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e t->z==u.am.sMem.
1d844 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 z );. u.am.pD
1d845 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 est->flags &= ~(
1d846 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 MEM_Ephem|MEM_St
1d847 61 74 69 63 29 3b 0a 20 20 20 20 75 2e 61 6d 2e atic);. u.am.
1d848 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pDest->flags |=
1d849 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 75 2e MEM_Term;. u.
1d84a 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d 20 75 2e am.pDest->z = u.
1d84b 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 75 am.sMem.z;. u
1d84c 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c .am.pDest->zMall
1d84d 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a oc = u.am.sMem.z
1d84e 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 Malloc;. }.. r
1d84f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1d850 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
1d851 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 0a 6f 70 u.am.pDest);..op
1d852 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a 20 20 55 _column_out:. U
1d853 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1d854 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a ZE(u.am.pDest);.
1d855 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1d856 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 6d 2e 70 (pOp->p3, u.am.p
1d857 44 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a Dest);. break;.
1d858 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 }../* Opcode: Af
1d859 66 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 finity P1 P2 * P
1d85a 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 4 *.**.** Apply
1d85b 61 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 affinities to a
1d85c 72 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 range of P2 regi
1d85d 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 sters starting w
1d85e 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 ith P1..**.** P4
1d85f 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 is a string tha
1d860 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 t is P2 characte
1d861 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 rs long. The nth
1d862 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 character of th
1d863 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 e.** string indi
1d864 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e cates the column
1d865 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 affinity that s
1d866 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
1d867 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d r the nth.** mem
1d868 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 ory cell in the
1d869 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f range..*/.case O
1d86a 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 23 69 P_Affinity: {.#i
1d86b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1d86c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d86d 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 63 6f 6e to u.an */. con
1d86e 73 74 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 st char *zAffini
1d86f 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66 ty; /* The aff
1d870 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c inity to be appl
1d871 69 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 63 41 ied */. char cA
1d872 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
1d873 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 68 /* A single ch
1d874 61 72 61 63 74 65 72 20 6f 66 20 61 66 66 69 6e aracter of affin
1d875 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a ity */.#endif /*
1d876 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d877 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6e moved into u.an
1d878 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41 66 66 */.. u.an.zAff
1d879 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e inity = pOp->p4.
1d87a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 z;. assert( u.a
1d87b 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d 30 20 29 n.zAffinity!=0 )
1d87c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6e ;. assert( u.an
1d87d 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f 70 2d 3e .zAffinity[pOp->
1d87e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 p2]==0 );. pIn1
1d87f 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 = &aMem[pOp->p1
1d880 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 75 2e 61 ];. while( (u.a
1d881 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e 61 6e 2e n.cAff = *(u.an.
1d882 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29 21 3d 30 zAffinity++))!=0
1d883 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1d884 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61 4d 65 6d pIn1 <= &p->aMem
1d885 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 [p->nMem] );.
1d886 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 ExpandBlob(pIn1
1d887 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 );. applyAffi
1d888 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 6e 2e nity(pIn1, u.an.
1d889 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e 67 29 3b cAff, encoding);
1d88a 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d . pIn1++;. }
1d88b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d88c 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f Opcode: MakeReco
1d88d 72 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a rd P1 P2 P3 P4 *
1d88e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 .**.** Convert P
1d88f 32 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 2 registers begi
1d890 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e nning with P1 in
1d891 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 to a single entr
1d892 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f y.** suitable fo
1d893 72 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20 r use as a data
1d894 72 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 record in a data
1d895 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 base table or as
1d896 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 a key.** in an
1d897 69 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 index. The deta
1d898 69 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 ils of the forma
1d899 74 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 t are irrelevant
1d89a 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 as long as.** t
1d89b 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 he OP_Column opc
1d89c 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 ode can decode t
1d89d 68 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e he record later.
1d89e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 .** Refer to sou
1d89f 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 rce code comment
1d8a0 73 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c s for the detail
1d8a1 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a s of the record.
1d8a2 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a ** format..**.**
1d8a3 20 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 P4 may be a str
1d8a4 69 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 ing that is P2 c
1d8a5 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 haracters long.
1d8a6 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 The nth charact
1d8a7 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 er of the.** str
1d8a8 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 ing indicates th
1d8a9 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 e column affinit
1d8aa 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 y that should be
1d8ab 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 used for the nt
1d8ac 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 h.** field of th
1d8ad 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a e index key..**.
1d8ae 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 ** The mapping f
1d8af 72 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f rom character to
1d8b0 20 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 affinity is giv
1d8b1 65 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 en by the SQLITE
1d8b2 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 _AFF_.** macros
1d8b3 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 defined in sqlit
1d8b4 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 eInt.h..**.** If
1d8b5 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e P4 is NULL then
1d8b6 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 all index field
1d8b7 73 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e s have the affin
1d8b8 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 ity NONE..*/.cas
1d8b9 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a e OP_MakeRecord:
1d8ba 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1d8bb 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d8bc 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a ed into u.ao */.
1d8bd 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 u8 *zNewRecord
1d8be 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 ; /* A bu
1d8bf 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 ffer to hold the
1d8c0 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 data for the ne
1d8c1 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 w record */. Me
1d8c2 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 m *pRec;
1d8c3 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 /* The new
1d8c4 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 record */. u64
1d8c5 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 nData;
1d8c6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d8c7 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 bytes of data sp
1d8c8 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 ace */. int nHd
1d8c9 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1d8ca 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1d8cb 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 es of header spa
1d8cc 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 ce */. i64 nByt
1d8cd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
1d8ce 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 * Data space req
1d8cf 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 uired for this r
1d8d0 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1d8d1 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 Zero;
1d8d2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a /* Number of z
1d8d3 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 ero bytes at the
1d8d4 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f end of the reco
1d8d5 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 rd */. int nVar
1d8d6 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f int; /
1d8d7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1d8d8 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f s in a varint */
1d8d9 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 . u32 serial_ty
1d8da 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 pe; /* Typ
1d8db 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d e field */. Mem
1d8dc 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 *pData0;
1d8dd 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 /* First fie
1d8de 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 ld to be combine
1d8df 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 d into the recor
1d8e0 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 d */. Mem *pLas
1d8e1 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
1d8e2 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 Last field of t
1d8e3 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1d8e4 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1d8e5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d8e6 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
1d8e7 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 record */. cha
1d8e8 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 r *zAffinity;
1d8e9 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e /* The affin
1d8ea 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 ity string for t
1d8eb 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1d8ec 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 nt file_format;
1d8ed 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f /* File fo
1d8ee 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 rmat to use for
1d8ef 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e encoding */. in
1d8f0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
1d8f1 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 /* Space us
1d8f2 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 ed in zNewRecord
1d8f3 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b [] */. int len;
1d8f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d8f5 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 * Length of a fi
1d8f6 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a eld */.#endif /*
1d8f7 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d8f8 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f moved into u.ao
1d8f9 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69 */.. /* Assumi
1d8fa 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f ng the record co
1d8fb 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c ntains N fields,
1d8fc 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d the record form
1d8fd 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 at looks. ** li
1d8fe 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 ke this:. **.
1d8ff 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -------------
1d900 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d901 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d902 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d903 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a -----------. **
1d904 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79 | hdr-size | ty
1d905 70 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20 pe 0 | type 1 |
1d906 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c ... | type N-1 |
1d907 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 data0 | ... | d
1d908 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d ata N-1 |. ** -
1d909 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d90a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d90b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d90c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d90d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a -------. **. *
1d90e 2a 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b * Data(0) is tak
1d90f 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1d910 20 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f P1. Data(1) co
1d911 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65 mes from registe
1d912 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 r P1+1. ** and
1d913 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 so froth.. **.
1d914 20 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69 ** Each type fi
1d915 65 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20 eld is a varint
1d916 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 representing the
1d917 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 serial type of
1d918 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 the. ** corresp
1d919 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d onding data elem
1d91a 65 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 ent (see sqlite3
1d91b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 VdbeSerialType()
1d91c 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d ). The. ** hdr-
1d91d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c size field is al
1d91e 73 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 so a varint whic
1d91f 68 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 h is the offset
1d920 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 from the beginni
1d921 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 ng. ** of the r
1d922 65 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a ecord to data0..
1d923 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74 */. u.ao.nDat
1d924 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f a = 0; /
1d925 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1d926 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 s of data space
1d927 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d */. u.ao.nHdr =
1d928 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
1d929 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1d92a 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 of header space
1d92b 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 */. u.ao.nByte
1d92c 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
1d92d 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 Data space requi
1d92e 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 red for this rec
1d92f 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a ord */. u.ao.nZ
1d930 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ero = 0;
1d931 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 /* Number of ze
1d932 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 ro bytes at the
1d933 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 end of the recor
1d934 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 d */. u.ao.nFie
1d935 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 ld = pOp->p1;.
1d936 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d u.ao.zAffinity =
1d937 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 pOp->p4.z;. as
1d938 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c sert( u.ao.nFiel
1d939 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 d>0 && pOp->p2>0
1d93a 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f && pOp->p2+u.ao
1d93b 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d .nField<=p->nMem
1d93c 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61 +1 );. u.ao.pDa
1d93d 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 6f ta0 = &aMem[u.ao
1d93e 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e 61 6f .nField];. u.ao
1d93f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 .nField = pOp->p
1d940 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73 74 20 2;. u.ao.pLast
1d941 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30 5b 75 = &u.ao.pData0[u
1d942 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20 .ao.nField-1];.
1d943 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 u.ao.file_forma
1d944 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 t = p->minWriteF
1d945 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a ileFormat;.. /*
1d946 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 Loop through th
1d947 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 e elements that
1d948 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 will make up the
1d949 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72 record to figur
1d94a 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d e. ** out how m
1d94b 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71 uch space is req
1d94c 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65 uired for the ne
1d94d 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 w record.. */.
1d94e 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 for(u.ao.pRec=u
1d94f 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f .ao.pData0; u.ao
1d950 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 .pRec<=u.ao.pLas
1d951 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b t; u.ao.pRec++){
1d952 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 7a 41 . if( u.ao.zA
1d953 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 ffinity ){.
1d954 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75 applyAffinity(u
1d955 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 7a .ao.pRec, u.ao.z
1d956 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e 70 52 Affinity[u.ao.pR
1d957 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30 5d 2c ec-u.ao.pData0],
1d958 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 encoding);.
1d959 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 70 }. if( u.ao.p
1d95a 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a Rec->flags&MEM_Z
1d95b 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52 65 63 ero && u.ao.pRec
1d95c 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 ->n>0 ){. s
1d95d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
1d95e 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70 52 65 andBlob(u.ao.pRe
1d95f 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e c);. }. u.
1d960 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d ao.serial_type =
1d961 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1d962 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63 alType(u.ao.pRec
1d963 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d , u.ao.file_form
1d964 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6c 65 at);. u.ao.le
1d965 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 n = sqlite3VdbeS
1d966 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 erialTypeLen(u.a
1d967 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a o.serial_type);.
1d968 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61 20 2b u.ao.nData +
1d969 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20 20 20 = u.ao.len;.
1d96a 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73 71 6c u.ao.nHdr += sql
1d96b 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e ite3VarintLen(u.
1d96c 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 3b ao.serial_type);
1d96d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 70 52 . if( u.ao.pR
1d96e 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ec->flags & MEM_
1d96f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 2f 2a Zero ){. /*
1d970 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72 6f 2d Only pure zero-
1d971 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63 61 6e filled BLOBs can
1d972 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69 be input to thi
1d973 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20 20 20 s Opcode..
1d974 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61 6c 6c ** We do not all
1d975 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20 61 20 ow blobs with a
1d976 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a 65 72 prefix and a zer
1d977 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e 20 2a o-filled tail. *
1d978 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e 5a 65 /. u.ao.nZe
1d979 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65 63 2d ro += u.ao.pRec-
1d97a 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 >u.nZero;. }e
1d97b 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c 65 6e lse if( u.ao.len
1d97c 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e ){. u.ao.n
1d97d 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Zero = 0;. }.
1d97e 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 }.. /* Add th
1d97f 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 e initial header
1d980 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 varint and tota
1d981 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 l the size */.
1d982 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75 2e 61 u.ao.nHdr += u.a
1d983 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 o.nVarint = sqli
1d984 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61 te3VarintLen(u.a
1d985 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 75 o.nHdr);. if( u
1d986 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71 6c 69 .ao.nVarint<sqli
1d987 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61 te3VarintLen(u.a
1d988 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 75 o.nHdr) ){. u
1d989 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a .ao.nHdr++;. }.
1d98a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d 20 75 u.ao.nByte = u
1d98b 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e 6e 44 .ao.nHdr+u.ao.nD
1d98c 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a ata-u.ao.nZero;.
1d98d 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79 74 65 if( u.ao.nByte
1d98e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1d98f 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1d990 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
1d991 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 _big;. }.. /*
1d992 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75 Make sure the ou
1d993 74 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61 tput register ha
1d994 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 s a buffer large
1d995 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 enough to store
1d996 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72 65 . ** the new re
1d997 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75 74 cord. The output
1d998 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d 3e register (pOp->
1d999 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 p3) is not allow
1d99a 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f 6e ed to. ** be on
1d99b 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 72 e of the input r
1d99c 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75 73 egisters (becaus
1d99d 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
1d99e 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71 6c call to. ** sql
1d99f 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1d9a0 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72 20 ) could clobber
1d9a1 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 the value before
1d9a2 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20 20 it is used)..
1d9a3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1d9a4 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c 20 ->p3<pOp->p1 ||
1d9a5 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 31 pOp->p3>=pOp->p1
1d9a6 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70 4f +pOp->p2 );. pO
1d9a7 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e ut = &aMem[pOp->
1d9a8 70 33 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 p3];. if( sqlit
1d9a9 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f e3VdbeMemGrow(pO
1d9aa 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 ut, (int)u.ao.nB
1d9ab 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 yte, 0) ){. g
1d9ac 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1d9ad 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 u.ao.zNewRecor
1d9ae 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e d = (u8 *)pOut->
1d9af 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 z;.. /* Write t
1d9b0 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 he record */. u
1d9b1 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61 72 69 6e .ao.i = putVarin
1d9b2 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 t32(u.ao.zNewRec
1d9b3 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b ord, u.ao.nHdr);
1d9b4 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 . for(u.ao.pRec
1d9b5 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e =u.ao.pData0; u.
1d9b6 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c ao.pRec<=u.ao.pL
1d9b7 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b ast; u.ao.pRec++
1d9b8 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72 69 ){. u.ao.seri
1d9b9 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 al_type = sqlite
1d9ba 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
1d9bb 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e u.ao.pRec, u.ao.
1d9bc 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
1d9bd 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56 u.ao.i += putV
1d9be 61 72 69 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e arint32(&u.ao.zN
1d9bf 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d ewRecord[u.ao.i]
1d9c0 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 , u.ao.serial_ty
1d9c1 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 pe); /* ser
1d9c2 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a ial type */. }.
1d9c3 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d for(u.ao.pRec=
1d9c4 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 u.ao.pData0; u.a
1d9c5 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 o.pRec<=u.ao.pLa
1d9c6 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 st; u.ao.pRec++)
1d9c7 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 { /* serial dat
1d9c8 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20 a */. u.ao.i
1d9c9 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 += sqlite3VdbeSe
1d9ca 72 69 61 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e rialPut(&u.ao.zN
1d9cb 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d ewRecord[u.ao.i]
1d9cc 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79 , (int)(u.ao.nBy
1d9cd 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f te-u.ao.i), u.ao
1d9ce 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f .pRec,u.ao.file_
1d9cf 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 format);. }. a
1d9d0 73 73 65 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75 ssert( u.ao.i==u
1d9d1 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20 .ao.nByte );..
1d9d2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1d9d3 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1d9d4 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d >nMem );. pOut-
1d9d5 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e >n = (int)u.ao.n
1d9d6 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c Byte;. pOut->fl
1d9d7 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c ags = MEM_Blob |
1d9d8 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 MEM_Dyn;. pOut
1d9d9 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 ->xDel = 0;. if
1d9da 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a ( u.ao.nZero ){.
1d9db 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 pOut->u.nZer
1d9dc 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a o = u.ao.nZero;.
1d9dd 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 pOut->flags
1d9de 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d |= MEM_Zero;. }
1d9df 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 . pOut->enc = S
1d9e0 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 QLITE_UTF8; /*
1d9e1 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 In case the blob
1d9e2 20 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 74 is ever convert
1d9e3 65 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 ed to text */.
1d9e4 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1d9e5 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 Op->p3, pOut);.
1d9e6 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1d9e7 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 SIZE(pOut);. br
1d9e8 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d9e9 65 3a 20 43 6f 75 6e 74 20 50 31 20 50 32 20 2a e: Count P1 P2 *
1d9ea 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 * *.**.** Store
1d9eb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1d9ec 6e 74 72 69 65 73 20 28 61 6e 20 69 6e 74 65 67 ntries (an integ
1d9ed 65 72 20 76 61 6c 75 65 29 20 69 6e 20 74 68 65 er value) in the
1d9ee 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1d9ef 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 63 75 .** opened by cu
1d9f0 72 73 6f 72 20 50 31 20 69 6e 20 72 65 67 69 73 rsor P1 in regis
1d9f1 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65 ter P2.*/.#ifnde
1d9f2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 f SQLITE_OMIT_BT
1d9f3 52 45 45 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50 REECOUNT.case OP
1d9f4 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 _Count: {
1d9f5 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1d9f6 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f ease */.#if 0 /
1d9f7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d9f8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d9f9 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 p */. i64 nEntr
1d9fa 79 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 y;. BtCursor *p
1d9fb 43 72 73 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 Crsr;.#endif /*
1d9fc 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d9fd 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 moved into u.ap
1d9fe 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43 72 73 72 */.. u.ap.pCrsr
1d9ff 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1da00 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 >p1]->pCursor;.
1da01 20 69 66 28 20 75 2e 61 70 2e 70 43 72 73 72 20 if( u.ap.pCrsr
1da02 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1da03 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 75 2e te3BtreeCount(u.
1da04 61 70 2e 70 43 72 73 72 2c 20 26 75 2e 61 70 2e ap.pCrsr, &u.ap.
1da05 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65 nEntry);. }else
1da06 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72 {. u.ap.nEntr
1da07 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 y = 0;. }. pOu
1da08 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45 t->u.i = u.ap.nE
1da09 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d ntry;. break;.}
1da0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f .#endif../* Opco
1da0b 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31 de: Savepoint P1
1da0c 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1da0d 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 Open, release or
1da0e 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61 rollback the sa
1da0f 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79 vepoint named by
1da10 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64 parameter P4, d
1da11 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 epending.** on t
1da12 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20 he value of P1.
1da13 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61 To open a new sa
1da14 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20 vepoint, P1==0.
1da15 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d To release (comm
1da16 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69 it) an.** existi
1da17 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 ng savepoint, P1
1da18 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 ==1, or to rollb
1da19 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20 ack an existing
1da1a 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e savepoint P1==2.
1da1b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 .*/.case OP_Save
1da1c 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 point: {.#if 0
1da1d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1da1e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1da1f 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 aq */. int p1;
1da20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da21 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
1da22 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a of P1 operand *
1da23 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b /. char *zName;
1da24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da25 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 /* Name of s
1da26 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e avepoint */. in
1da27 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70 t nName;. Savep
1da28 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61 oint *pNew;. Sa
1da29 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
1da2a 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 int;. Savepoint
1da2b 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53 *pTmp;. int iS
1da2c 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20 avepoint;. int
1da2d 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f ii;.#endif /* lo
1da2e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1da2f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f ved into u.aq */
1da30 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f .. u.aq.p1 = pO
1da31 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e p->p1;. u.aq.zN
1da32 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b ame = pOp->p4.z;
1da33 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
1da34 61 74 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70 at the u.aq.p1 p
1da35 61 72 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69 arameter is vali
1da36 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20 d. Also that if
1da37 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e there is no open
1da38 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f . ** transactio
1da39 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 n, then there ca
1da3a 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 nnot be any save
1da3b 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 points.. */. a
1da3c 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 ssert( db->pSave
1da3d 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e point==0 || db->
1da3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b autoCommit==0 );
1da3f 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e . assert( u.aq.
1da40 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 p1==SAVEPOINT_BE
1da41 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 GIN||u.aq.p1==SA
1da42 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c VEPOINT_RELEASE|
1da43 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f |u.aq.p1==SAVEPO
1da44 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a INT_ROLLBACK );.
1da45 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 assert( db->pS
1da46 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e avepoint || db->
1da47 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 isTransactionSav
1da48 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 epoint==0 );. a
1da49 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 ssert( checkSave
1da4a 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 pointCount(db) )
1da4b 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31 ;.. if( u.aq.p1
1da4c 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 ==SAVEPOINT_BEGI
1da4d 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d N ){. if( db-
1da4e 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 >writeVdbeCnt>0
1da4f 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 ){. /* A ne
1da50 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e w savepoint cann
1da51 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 66 ot be created if
1da52 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
1da53 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a e write. **
1da54 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65 statements (i.e
1da55 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74 . open read/writ
1da56 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c e incremental bl
1da57 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20 ob handles)..
1da58 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
1da59 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1da5a 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 >zErrMsg, db, "c
1da5b 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70 annot open savep
1da5c 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20 oint - ".
1da5d 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 "SQL statements
1da5e 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a in progress");.
1da5f 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1da60 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 E_BUSY;. }els
1da61 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e e{. u.aq.nN
1da62 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ame = sqlite3Str
1da63 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65 len30(u.aq.zName
1da64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 );.. /* Cre
1da65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f ate a new savepo
1da66 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a int structure. *
1da67 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 /. u.aq.pNe
1da68 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
1da69 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f locRaw(db, sizeo
1da6a 66 28 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61 f(Savepoint)+u.a
1da6b 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 q.nName+1);.
1da6c 20 20 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20 if( u.aq.pNew
1da6d 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e ){. u.aq.
1da6e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 pNew->zName = (c
1da6f 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77 har *)&u.aq.pNew
1da70 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d [1];. mem
1da71 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a cpy(u.aq.pNew->z
1da72 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 Name, u.aq.zName
1da73 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b , u.aq.nName+1);
1da74 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 .. /* If
1da75 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e there is no open
1da76 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
1da77 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 en mark this as
1da78 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 a special.
1da79 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f ** "transactio
1da7a 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f n savepoint". */
1da7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d . if( db-
1da7c 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 >autoCommit ){.
1da7d 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 db->aut
1da7e 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 oCommit = 0;.
1da7f 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 db->isTra
1da80 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1da81 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d t = 1;. }
1da82 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1da83 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b db->nSavepoint++
1da84 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
1da85 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 /* Link the
1da86 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69 new savepoint i
1da87 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
1da88 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 handle's list.
1da89 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e */. u.aq.
1da8a 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 pNew->pNext = db
1da8b 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 ->pSavepoint;.
1da8c 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 db->pSavep
1da8d 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77 oint = u.aq.pNew
1da8e 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 ;. u.aq.p
1da8f 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f New->nDeferredCo
1da90 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 ns = db->nDeferr
1da91 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a edCons;. }.
1da92 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1da93 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 u.aq.iSavepoi
1da94 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 nt = 0;.. /*
1da95 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73 Find the named s
1da96 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65 avepoint. If the
1da97 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61 re is no such sa
1da98 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e vepoint, then an
1da99 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 . ** an error
1da9a 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
1da9b 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20 the user. */.
1da9c 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61 for(. u.a
1da9d 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 q.pSavepoint = d
1da9e 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 b->pSavepoint;.
1da9f 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 u.aq.pSavep
1daa0 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53 oint && sqlite3S
1daa1 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76 trICmp(u.aq.pSav
1daa2 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75 epoint->zName, u
1daa3 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 .aq.zName);.
1daa4 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e u.aq.pSavepoin
1daa5 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f t = u.aq.pSavepo
1daa6 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 int->pNext. )
1daa7 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 {. u.aq.iSa
1daa8 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d vepoint++;. }
1daa9 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70 . if( !u.aq.p
1daaa 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
1daab 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1daac 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1daad 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61 db, "no such sa
1daae 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e vepoint: %s", u.
1daaf 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 aq.zName);.
1dab0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1dab1 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 OR;. }else if
1dab2 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72 (. db->wr
1dab3 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 iteVdbeCnt>0 ||
1dab4 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f (u.aq.p1==SAVEPO
1dab5 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 INT_ROLLBACK &&
1dab6 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1dab7 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 t>1). ){.
1dab8 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 /* It is not p
1dab9 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 ossible to relea
1daba 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 se (commit) a sa
1dabb 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 vepoint if there
1dabc 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 are. ** ac
1dabd 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74 65 tive write state
1dabe 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74 ments. It is not
1dabf 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c possible to rol
1dac0 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e lback a savepoin
1dac1 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68 t. ** if th
1dac2 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 ere are any acti
1dac3 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74 ve statements at
1dac4 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 all.. */.
1dac5 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 sqlite3SetS
1dac6 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1dac7 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22 g, db,. "
1dac8 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f cannot %s savepo
1dac9 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d int - SQL statem
1daca 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 ents in progress
1dacb 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71 ",. (u.aq
1dacc 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 .p1==SAVEPOINT_R
1dacd 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 OLLBACK ? "rollb
1dace 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 ack": "release")
1dacf 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
1dad0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1dad1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 ;. }else{..
1dad2 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 /* Determine
1dad3 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1dad4 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 this is a transa
1dad5 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e ction savepoint.
1dad6 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a If so,. **
1dad7 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 and this is a R
1dad8 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 ELEASE command,
1dad9 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 then the current
1dada 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 transaction.
1dadb 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 ** is committ
1dadc 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
1dadd 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63 int isTransac
1dade 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76 tion = u.aq.pSav
1dadf 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 epoint->pNext==0
1dae0 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 && db->isTransa
1dae1 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a ctionSavepoint;.
1dae2 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61 6e if( isTran
1dae3 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e saction && u.aq.
1dae4 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 p1==SAVEPOINT_RE
1dae5 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 LEASE ){.
1dae6 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 if( (rc = sqlit
1dae7 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c e3VdbeCheckFk(p,
1dae8 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 1))!=SQLITE_OK
1dae9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
1daea 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 o vdbe_return;.
1daeb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1daec 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1daed 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 1;. if(
1daee 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
1daef 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 (p)==SQLITE_BUSY
1daf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
1daf1 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 >pc = pc;.
1daf2 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1daf3 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 it = 0;.
1daf4 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 p->rc = rc = S
1daf5 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
1daf6 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1daf7 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 return;.
1daf8 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 }. db->is
1daf9 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1dafa 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 oint = 0;.
1dafb 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 rc = p->rc;.
1dafc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dafd 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 u.aq.iSavepoi
1dafe 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f nt = db->nSavepo
1daff 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65 int - u.aq.iSave
1db00 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 point - 1;.
1db01 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30 for(u.aq.ii=0
1db02 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44 ; u.aq.ii<db->nD
1db03 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20 b; u.aq.ii++){.
1db04 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
1db05 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f lite3BtreeSavepo
1db06 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71 int(db->aDb[u.aq
1db07 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70 .ii].pBt, u.aq.p
1db08 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 1, u.aq.iSavepoi
1db09 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nt);. i
1db0a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1db0b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1db0c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1db0d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1db0e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1db0f 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e if( u.aq.
1db10 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f p1==SAVEPOINT_RO
1db11 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66 LLBACK && (db->f
1db12 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 lags&SQLITE_Inte
1db13 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b rnChanges)!=0 ){
1db14 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1db15 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
1db16 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
1db17 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1db18 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1db19 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 hema(db, 0);.
1db1a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a }. }..
1db1b 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c /* Regardl
1db1c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 ess of whether t
1db1d 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 his is a RELEASE
1db1e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 or ROLLBACK, de
1db1f 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20 stroy all.
1db20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 ** savepoints ne
1db21 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 sted inside of t
1db22 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 he savepoint bei
1db23 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 ng operated on.
1db24 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
1db25 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d db->pSavepoint!=
1db26 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1db27 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e ){. u.aq.
1db28 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 pTmp = db->pSave
1db29 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 point;. d
1db2a 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 b->pSavepoint =
1db2b 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74 u.aq.pTmp->pNext
1db2c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1db2d 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 3DbFree(db, u.aq
1db2e 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 .pTmp);.
1db2f 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d db->nSavepoint--
1db30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1db31 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 /* If it is a R
1db32 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 ELEASE, then des
1db33 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 troy the savepoi
1db34 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 nt being operate
1db35 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 6f d on. ** to
1db36 6f 2e 20 49 66 20 69 74 20 69 73 20 61 20 52 4f o. If it is a RO
1db37 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e 20 LLBACK TO, then
1db38 73 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f set the number o
1db39 66 20 64 65 66 65 72 72 65 64 0a 20 20 20 20 20 f deferred.
1db3a 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 ** constraint v
1db3b 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 6e iolations presen
1db3c 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 t in the databas
1db3d 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 e to the value s
1db3e 74 6f 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 tored. ** w
1db3f 68 65 6e 20 74 68 65 20 73 61 76 65 70 6f 69 6e hen the savepoin
1db40 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 20 20 t was created.
1db41 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 */. if( u.a
1db42 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f q.p1==SAVEPOINT_
1db43 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 RELEASE ){.
1db44 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e assert( u.aq.
1db45 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e pSavepoint==db->
1db46 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 pSavepoint );.
1db47 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 db->pSavep
1db48 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 oint = u.aq.pSav
1db49 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 epoint->pNext;.
1db4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1db4b 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 53 Free(db, u.aq.pS
1db4c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1db4d 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 61 if( !isTransa
1db4e 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 ction ){.
1db4f 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1db50 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 t--;. }.
1db51 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1db52 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 db->nDeferre
1db53 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53 61 dCons = u.aq.pSa
1db54 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72 72 vepoint->nDeferr
1db55 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a edCons;. }.
1db56 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65 }. }.. bre
1db57 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1db58 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20 : AutoCommit P1
1db59 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1db5a 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 et the database
1db5b 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
1db5c 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e to P1 (1 or 0).
1db5d 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c 20 If P2 is true,
1db5e 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 roll.** back any
1db5f 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
1db60 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 74 e btree transact
1db61 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61 ions. If there a
1db62 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a re any active.**
1db63 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d VMs (apart from
1db64 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e this one), then
1db65 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c a ROLLBACK fail
1db66 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69 s. A COMMIT fai
1db67 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61 ls if.** there a
1db68 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 6e re active writin
1db69 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20 g VMs or active
1db6a 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 61 VMs that use sha
1db6b 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a red cache..**.**
1db6c 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1db6d 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 n causes the VM
1db6e 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 to halt..*/.case
1db6f 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 OP_AutoCommit:
1db70 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1db71 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1db72 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20 d into u.ar */.
1db73 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f int desiredAuto
1db74 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52 Commit;. int iR
1db75 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74 ollback;. int t
1db76 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20 urnOnAC;.#endif
1db77 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1db78 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1db79 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65 ar */.. u.ar.de
1db7a 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 siredAutoCommit
1db7b 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 = pOp->p1;. u.a
1db7c 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f r.iRollback = pO
1db7d 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75 p->p2;. u.ar.tu
1db7e 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65 rnOnAC = u.ar.de
1db7f 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 siredAutoCommit
1db80 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d && !db->autoComm
1db81 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e it;. assert( u.
1db82 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f ar.desiredAutoCo
1db83 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e mmit==1 || u.ar.
1db84 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1db85 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t==0 );. assert
1db86 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 ( u.ar.desiredAu
1db87 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 toCommit==1 || u
1db88 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30 .ar.iRollback==0
1db89 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1db8a 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1db8b 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 0 ); /* At leas
1db8c 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 t this one VM is
1db8d 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 active */.. if
1db8e 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 ( u.ar.turnOnAC
1db8f 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 && u.ar.iRollbac
1db90 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 k && db->activeV
1db91 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 dbeCnt>1 ){.
1db92 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 /* If this instr
1db93 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 uction implement
1db94 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 s a ROLLBACK and
1db95 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20 other VMs are.
1db96 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e ** still runn
1db97 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 ing, and a trans
1db98 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1db99 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f , return an erro
1db9a 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 r indicating.
1db9b 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68 ** that the oth
1db9c 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 er VMs must comp
1db9d 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20 lete first..
1db9e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 */. sqlite3Se
1db9f 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1dba0 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
1dba1 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 rollback transa
1dba2 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 ction - ".
1dba3 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 "SQL statement
1dba4 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b s in progress");
1dba5 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1dba6 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 _BUSY;. }else i
1dba7 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 f( u.ar.turnOnAC
1dba8 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62 && !u.ar.iRollb
1dba9 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65 ack && db->write
1dbaa 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 VdbeCnt>0 ){.
1dbab 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 /* If this inst
1dbac 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e ruction implemen
1dbad 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 ts a COMMIT and
1dbae 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 72 other VMs are wr
1dbaf 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74 iting. ** ret
1dbb0 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 urn an error ind
1dbb1 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 icating that the
1dbb2 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 other VMs must
1dbb3 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a complete first..
1dbb4 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
1dbb5 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1dbb6 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 zErrMsg, db, "ca
1dbb7 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e nnot commit tran
1dbb8 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 saction - ".
1dbb9 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 "SQL stateme
1dbba 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 nts in progress"
1dbbb 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1dbbc 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 TE_BUSY;. }else
1dbbd 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65 if( u.ar.desire
1dbbe 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d dAutoCommit!=db-
1dbbf 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 >autoCommit ){.
1dbc0 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c if( u.ar.iRol
1dbc1 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 lback ){. a
1dbc2 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 ssert( u.ar.desi
1dbc3 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 redAutoCommit==1
1dbc4 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1dbc5 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
1dbc6 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f ;. db->auto
1dbc7 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
1dbc8 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20 }else if( (rc =
1dbc9 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b sqlite3VdbeCheck
1dbca 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 Fk(p, 1))!=SQLIT
1dbcb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
1dbcc 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1dbcd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dbce 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1dbcf 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 73 69 72 = (u8)u.ar.desir
1dbd0 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 edAutoCommit;.
1dbd1 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
1dbd2 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 dbeHalt(p)==SQLI
1dbd3 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
1dbd4 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 p->pc = pc;.
1dbd5 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1dbd6 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 31 2d 75 ommit = (u8)(1-u
1dbd7 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 .ar.desiredAutoC
1dbd8 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 ommit);.
1dbd9 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1dbda 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
1dbdb 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 goto vdbe_retu
1dbdc 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
1dbdd 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 }. assert( db
1dbde 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 ->nStatement==0
1dbdf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c );. sqlite3Cl
1dbe0 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 oseSavepoints(db
1dbe1 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 );. if( p->rc
1dbe2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1dbe3 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1dbe4 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 _DONE;. }else
1dbe5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1dbe6 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d ITE_ERROR;. }
1dbe7 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 . goto vdbe_r
1dbe8 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a eturn;. }else{.
1dbe9 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1dbea 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1dbeb 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 , db,. (!
1dbec 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f u.ar.desiredAuto
1dbed 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 Commit)?"cannot
1dbee 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
1dbef 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 ion within a tra
1dbf0 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 nsaction":(.
1dbf1 20 20 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c 62 (u.ar.iRollb
1dbf2 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c ack)?"cannot rol
1dbf3 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e 73 lback - no trans
1dbf4 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1dbf5 22 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ":.
1dbf6 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 6f "cannot co
1dbf7 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 61 mmit - no transa
1dbf8 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22 ction is active"
1dbf9 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53 51 ));.. rc = SQ
1dbfa 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
1dbfb 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1dbfc 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 pcode: Transacti
1dbfd 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a on P1 P2 * * *.*
1dbfe 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 *.** Begin a tra
1dbff 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 nsaction. The t
1dc00 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 ransaction ends
1dc01 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 when a Commit or
1dc02 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 Rollback.** opc
1dc03 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ode is encounter
1dc04 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f ed. Depending o
1dc05 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 n the ON CONFLIC
1dc06 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a T setting, the.*
1dc07 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 * transaction mi
1dc08 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c ght also be roll
1dc09 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 ed back if an er
1dc0a 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ror is encounter
1dc0b 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 ed..**.** P1 is
1dc0c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
1dc0d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f database file o
1dc0e 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61 6e n which the tran
1dc0f 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 saction is.** st
1dc10 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 arted. Index 0
1dc11 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 is the main data
1dc12 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e base file and in
1dc13 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 dex 1 is the.**
1dc14 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 file used for te
1dc15 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 mporary tables.
1dc16 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 Indices of 2 or
1dc17 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 more are used f
1dc18 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 or.** attached d
1dc19 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 atabases..**.**
1dc1a 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 If P2 is non-zer
1dc1b 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d o, then a write-
1dc1c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1dc1d 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 tarted. A RESER
1dc1e 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f VED lock is.** o
1dc1f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 btained on the d
1dc20 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 atabase file whe
1dc21 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 n a write-transa
1dc22 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 ction is started
1dc23 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 . No.** other p
1dc24 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 rocess can start
1dc25 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 another write t
1dc26 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 ransaction while
1dc27 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
1dc28 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 n is.** underway
1dc29 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 . Starting a wr
1dc2a 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
1dc2b 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 also creates a r
1dc2c 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
1dc2d 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e A.** write tran
1dc2e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
1dc2f 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 started before a
1dc30 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 ny changes can b
1dc31 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a e made to the.**
1dc32 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 database. If P
1dc33 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 2 is 2 or greate
1dc34 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 r then an EXCLUS
1dc35 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f IVE lock is also
1dc36 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 obtained.** on
1dc37 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 the file..**.**
1dc38 49 66 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 If a write-trans
1dc39 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 action is starte
1dc3a 64 20 61 6e 64 20 74 68 65 20 56 64 62 65 2e 75 d and the Vdbe.u
1dc3b 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 sesStmtJournal f
1dc3c 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 28 lag is.** true (
1dc3d 74 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 this flag is set
1dc3e 20 69 66 20 74 68 65 20 56 64 62 65 20 6d 61 79 if the Vdbe may
1dc3f 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 61 modify more tha
1dc40 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d 61 n one row and ma
1dc41 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 y.** throw an AB
1dc42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 29 2c 20 ORT exception),
1dc43 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e a statement tran
1dc44 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f saction may also
1dc45 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d be opened..** M
1dc46 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79 ore specifically
1dc47 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 , a statement tr
1dc48 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
1dc49 6e 65 64 20 69 66 66 20 74 68 65 20 64 61 74 61 ned iff the data
1dc4a 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 base.** connecti
1dc4b 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 on is currently
1dc4c 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 not in autocommi
1dc4d 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74 68 t mode, or if th
1dc4e 65 72 65 20 61 72 65 20 6f 74 68 65 72 0a 2a 2a ere are other.**
1dc4f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e active statemen
1dc50 74 73 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 20 ts. A statement
1dc51 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c 6f transaction allo
1dc52 77 73 20 74 68 65 20 61 66 66 65 63 74 73 20 6f ws the affects o
1dc53 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 45 20 74 f this.** VDBE t
1dc54 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
1dc55 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 after an error
1dc56 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
1dc57 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a o roll back the.
1dc58 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 ** entire transa
1dc59 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 72 ction. If no err
1dc5a 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
1dc5b 64 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 d, the statement
1dc5c 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
1dc5d 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
1dc5e 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 ly commit when t
1dc5f 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a he VDBE halts..*
1dc60 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65 *.** If P2 is ze
1dc61 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d ro, then a read-
1dc62 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
1dc63 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1dc64 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f file..*/.case O
1dc65 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b P_Transaction: {
1dc66 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1dc67 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dc68 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 20 into u.as */.
1dc69 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 Btree *pBt;.#end
1dc6a 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1dc6b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dc6c 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73 65 u.as */.. asse
1dc6d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1dc6e 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1dc6f 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1dc70 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1dc71 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1dc72 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d 20 );. u.as.pBt =
1dc73 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d db->aDb[pOp->p1]
1dc74 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e 61 .pBt;.. if( u.a
1dc75 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 s.pBt ){. rc
1dc76 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 = sqlite3BtreeBe
1dc77 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 ginTrans(u.as.pB
1dc78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 t, pOp->p2);.
1dc79 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1dc7a 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d BUSY ){. p-
1dc7b 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 >pc = pc;.
1dc7c 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1dc7d 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
1dc7e 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1dc7f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1dc80 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1dc81 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1dc82 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1dc83 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 }.. if( p
1dc84 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73 65 Op->p2 && p->use
1dc85 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20 20 sStmtJournal.
1dc86 20 20 26 26 20 28 64 62 2d 3e 61 75 74 6f 43 6f && (db->autoCo
1dc87 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 mmit==0 || db->a
1dc88 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 29 0a ctiveVdbeCnt>1).
1dc89 20 20 20 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ){. ass
1dc8a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1dc8b 65 49 73 49 6e 54 72 61 6e 73 28 75 2e 61 73 2e eIsInTrans(u.as.
1dc8c 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 pBt) );. if
1dc8d 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d ( p->iStatement=
1dc8e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 =0 ){. as
1dc8f 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 sert( db->nState
1dc90 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e ment>=0 && db->n
1dc91 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a Savepoint>=0 );.
1dc92 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 db->nSta
1dc93 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 tement++;.
1dc94 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 p->iStatement
1dc95 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 = db->nSavepoint
1dc96 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e + db->nStatemen
1dc97 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
1dc98 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1dc99 65 65 42 65 67 69 6e 53 74 6d 74 28 75 2e 61 73 eeBeginStmt(u.as
1dc9a 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d .pBt, p->iStatem
1dc9b 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ent);.. /*
1dc9c 53 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e Store the curren
1dc9d 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 t value of the d
1dc9e 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 atabase handles
1dc9f 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 deferred constra
1dca0 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 75 int. ** cou
1dca1 6e 74 65 72 2e 20 49 66 20 74 68 65 20 73 74 61 nter. If the sta
1dca2 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1dca3 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 on needs to be r
1dca4 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20 20 olled back,.
1dca5 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f ** the value o
1dca6 66 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6e f this counter n
1dca7 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f eeds to be resto
1dca8 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20 20 red too. */.
1dca9 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f p->nStmtDefCo
1dcaa 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 ns = db->nDeferr
1dcab 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 edCons;. }.
1dcac 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1dcad 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f 6f Opcode: ReadCoo
1dcae 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a kie P1 P2 P3 * *
1dcaf 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f 6b .**.** Read cook
1dcb0 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 6f ie number P3 fro
1dcb1 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 6e m database P1 an
1dcb2 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 d write it into
1dcb3 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 register P2..**
1dcb4 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 P3==1 is the sch
1dcb5 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 33 ema version. P3
1dcb6 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61 62 ==2 is the datab
1dcb7 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50 ase format..** P
1dcb8 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 3==3 is the reco
1dcb9 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 mmended pager ca
1dcba 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f che size, and so
1dcbb 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69 forth. P1==0 i
1dcbc 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 s.** the main da
1dcbd 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
1dcbe 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 P1==1 is the dat
1dcbf 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 abase file used
1dcc0 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 to store.** temp
1dcc1 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a orary tables..**
1dcc2 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 .** There must b
1dcc3 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e e a read-lock on
1dcc4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 65 the database (e
1dcc5 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 ither a transact
1dcc6 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 ion.** must be s
1dcc7 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 20 tarted or there
1dcc8 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 must be an open
1dcc9 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a cursor) before.*
1dcca 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 * executing this
1dccb 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f instruction..*/
1dccc 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f .case OP_ReadCoo
1dccd 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 kie: {
1dcce 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1dccf 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1dcd0 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1dcd1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1dcd2 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4d u.at */. int iM
1dcd3 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a eta;. int iDb;.
1dcd4 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 23 int iCookie;.#
1dcd5 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1dcd6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dcd7 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20 75 nto u.at */.. u
1dcd8 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 .at.iDb = pOp->p
1dcd9 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b 69 1;. u.at.iCooki
1dcda 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 e = pOp->p3;. a
1dcdb 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 ssert( pOp->p3<S
1dcdc 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 QLITE_N_BTREE_ME
1dcdd 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TA );. assert(
1dcde 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26 20 75 u.at.iDb>=0 && u
1dcdf 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 .at.iDb<db->nDb
1dce0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d );. assert( db-
1dce1 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 >aDb[u.at.iDb].p
1dce2 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 Bt!=0 );. asser
1dce3 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1dce4 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62 29 & (1<<u.at.iDb)
1dce5 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74 )!=0 );.. sqlit
1dce6 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64 e3BtreeGetMeta(d
1dce7 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d b->aDb[u.at.iDb]
1dce8 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f 6b .pBt, u.at.iCook
1dce9 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 74 ie, (u32 *)&u.at
1dcea 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d .iMeta);. pOut-
1dceb 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65 74 >u.i = u.at.iMet
1dcec 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f a;. break;.}../
1dced 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f * Opcode: SetCoo
1dcee 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a kie P1 P2 P3 * *
1dcef 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 .**.** Write the
1dcf0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1dcf1 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 72 ster P3 (interpr
1dcf2 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 67 eted as an integ
1dcf3 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b er).** into cook
1dcf4 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 20 ie number P2 of
1dcf5 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 50 32 database P1. P2
1dcf6 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d ==1 is the schem
1dcf7 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a 2a 20 a version. .**
1dcf8 50 32 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 P2==2 is the dat
1dcf9 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 50 32 abase format. P2
1dcfa 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d ==3 is the recom
1dcfb 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63 mended pager cac
1dcfc 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64 he .** size, and
1dcfd 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d so forth. P1==
1dcfe 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 0 is the main da
1dcff 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
1dd00 50 31 3d 3d 31 20 69 73 20 74 68 65 20 0a 2a 2a P1==1 is the .**
1dd01 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 database file u
1dd02 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d sed to store tem
1dd03 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1dd04 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 *.** A transacti
1dd05 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 on must be start
1dd06 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 ed before execut
1dd07 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1dd08 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43 .*/.case OP_SetC
1dd09 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f ookie: { /
1dd0a 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 * in3 */.#if 0
1dd0b 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1dd0c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1dd0d 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b au */. Db *pDb;
1dd0e 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1dd0f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dd10 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 into u.au */.
1dd11 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c assert( pOp->p2<
1dd12 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d SQLITE_N_BTREE_M
1dd13 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ETA );. assert(
1dd14 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1dd15 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 Op->p1<db->nDb )
1dd16 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1dd17 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1dd18 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a pOp->p1))!=0 );.
1dd19 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 26 64 62 u.au.pDb = &db
1dd1a 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a ->aDb[pOp->p1];.
1dd1b 20 20 61 73 73 65 72 74 28 20 75 2e 61 75 2e 70 assert( u.au.p
1dd1c 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 Db->pBt!=0 );.
1dd1d 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn3 = &aMem[pOp
1dd1e 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69 74 65 33 ->p3];. sqlite3
1dd1f 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1dd20 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65 y(pIn3);. /* Se
1dd21 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 e note about ind
1dd22 65 78 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f ex shifting on O
1dd23 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a P_ReadCookie */.
1dd24 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dd25 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e reeUpdateMeta(u.
1dd26 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70 au.pDb->pBt, pOp
1dd27 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d ->p2, (int)pIn3-
1dd28 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 >u.i);. if( pOp
1dd29 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45 ->p2==BTREE_SCHE
1dd2a 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20 MA_VERSION ){.
1dd2b 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 /* When the sc
1dd2c 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e hema cookie chan
1dd2d 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 ges, record the
1dd2e 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 new cookie inter
1dd2f 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 2e 61 nally */. u.a
1dd30 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e u.pDb->pSchema->
1dd31 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 schema_cookie =
1dd32 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a (int)pIn3->u.i;.
1dd33 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d db->flags |=
1dd34 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
1dd35 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 anges;. }else i
1dd36 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 f( pOp->p2==BTRE
1dd37 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b E_FILE_FORMAT ){
1dd38 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 . /* Record c
1dd39 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 hanges in the fi
1dd3a 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 le format */.
1dd3b 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 u.au.pDb->pSche
1dd3c 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 ma->file_format
1dd3d 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 2e 69 3b = (u8)pIn3->u.i;
1dd3e 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e . }. if( pOp->
1dd3f 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 p1==1 ){. /*
1dd40 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 Invalidate all p
1dd41 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1dd42 74 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 ts whenever the
1dd43 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 TEMP database.
1dd44 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 ** schema is c
1dd45 68 61 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 hanged. Ticket
1dd46 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c #1644 */. sql
1dd47 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 ite3ExpirePrepar
1dd48 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 edStatements(db)
1dd49 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 ;. p->expired
1dd4a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 = 0;. }. brea
1dd4b 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1dd4c 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 VerifyCookie P1
1dd4d 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 P2 *.**.** Chec
1dd4e 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 k the value of g
1dd4f 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 lobal database p
1dd50 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 arameter number
1dd51 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 0 (the.** schema
1dd52 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 version) and ma
1dd53 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71 ke sure it is eq
1dd54 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 ual to P2. .**
1dd55 50 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 P1 is the databa
1dd56 73 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 se number which
1dd57 69 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 is 0 for the mai
1dd58 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a n database file.
1dd59 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 ** and 1 for the
1dd5a 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 file holding te
1dd5b 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 mporary tables a
1dd5c 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e nd some higher n
1dd5d 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 umber.** for aux
1dd5e 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 iliary databases
1dd5f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b ..**.** The cook
1dd60 69 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 ie changes its v
1dd61 61 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 alue whenever th
1dd62 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
1dd63 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 a changes..** Th
1dd64 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 is operation is
1dd65 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 used to detect w
1dd66 68 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f hen that the coo
1dd67 6b 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a kie has changed.
1dd68 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 ** and that the
1dd69 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 current process
1dd6a 6e 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 needs to reread
1dd6b 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a the schema..**.*
1dd6c 2a 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 * Either a trans
1dd6d 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 action needs to
1dd6e 68 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 have been starte
1dd6f 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 d or an OP_Open
1dd70 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 needs.** to be e
1dd71 78 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 xecuted (to esta
1dd72 62 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 blish a read loc
1dd73 6b 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f k) before this o
1dd74 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f pcode is.** invo
1dd75 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ked..*/.case OP_
1dd76 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a VerifyCookie: {.
1dd77 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1dd78 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1dd79 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69 into u.av */. i
1dd7a 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 nt iMeta;. Btre
1dd7b 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f e *pBt;.#endif /
1dd7c 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1dd7d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1dd7e 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 v */. assert( p
1dd7f 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1dd80 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p1<db->nDb );.
1dd81 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1dd82 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1dd83 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 p->p1))!=0 );.
1dd84 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61 u.av.pBt = db->a
1dd85 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b Db[pOp->p1].pBt;
1dd86 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20 . if( u.av.pBt
1dd87 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
1dd88 72 65 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e reeGetMeta(u.av.
1dd89 70 42 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d pBt, BTREE_SCHEM
1dd8a 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 A_VERSION, (u32
1dd8b 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a *)&u.av.iMeta);.
1dd8c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 }else{. u.a
1dd8d 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d v.iMeta = 0;. }
1dd8e 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74 . if( u.av.iMet
1dd8f 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 a!=pOp->p2 ){.
1dd90 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1dd91 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1dd92 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
1dd93 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
1dd94 70 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 p(db, "database
1dd95 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 schema has chang
1dd96 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 ed");. /* If
1dd97 74 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 the schema-cooki
1dd98 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 e from the datab
1dd99 61 73 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 ase file matches
1dd9a 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20 the cookie.
1dd9b 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 ** stored with t
1dd9c 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 he in-memory rep
1dd9d 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1dd9e 68 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 he schema, do.
1dd9f 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 ** not reload
1dda0 74 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 the schema from
1dda1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1dda2 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
1dda3 20 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c If virtual-tabl
1dda4 65 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 es are in use, t
1dda5 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 his is not just
1dda6 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e an optimization.
1dda7 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 . ** Often, v
1dda8 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 -tables store th
1dda9 65 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 eir data in othe
1ddaa 72 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c r SQLite tables,
1ddab 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 which. ** ar
1ddac 65 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 e queried from w
1ddad 69 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e ithin xNext() an
1ddae 64 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 d other v-table
1ddaf 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 methods using.
1ddb0 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 ** prepared qu
1ddb1 65 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 eries. If such a
1ddb2 20 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 query is out-of
1ddb3 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 -date, we do not
1ddb4 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 want to. **
1ddb5 64 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 discard the data
1ddb6 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 base schema, as
1ddb7 74 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d the user code im
1ddb8 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 plementing the.
1ddb9 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f ** v-table wo
1ddba 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 uld have to be r
1ddbb 65 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c eady for the sql
1ddbc 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
1ddbd 75 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a ure itself. *
1ddbe 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 * to be invalida
1ddbf 74 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c ted whenever sql
1ddc0 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 ite3_step() is c
1ddc1 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 alled from withi
1ddc2 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 n. ** a v-tab
1ddc3 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a le method.. *
1ddc4 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 /. if( db->aD
1ddc5 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 b[pOp->p1].pSche
1ddc6 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 ma->schema_cooki
1ddc7 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b e!=u.av.iMeta ){
1ddc8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
1ddc9 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d setInternalSchem
1ddca 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a a(db, pOp->p1);.
1ddcb 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1ddcc 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
1ddcd 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
1ddce 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1ddcf 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72 SCHEMA;. }. br
1ddd0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ddd1 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50 e: OpenRead P1 P
1ddd2 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1ddd3 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c Open a read-onl
1ddd4 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 y cursor for the
1ddd5 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
1ddd6 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 whose root page
1ddd7 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 is.** P2 in a da
1ddd8 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
1ddd9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1ddda 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
1dddb 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d P3. .** P3==0 m
1dddc 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 eans the main da
1dddd 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 tabase, P3==1 me
1ddde 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ans the database
1dddf 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65 used for .** te
1dde0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 mporary tables,
1dde1 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 and P3>1 means u
1dde2 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f sed the correspo
1dde3 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a nding attached.*
1dde4 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 69 76 * database. Giv
1dde5 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72 e the new cursor
1dde6 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f an identifier o
1dde7 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a f P1. The P1.**
1dde8 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74 values need not
1dde9 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 be contiguous b
1ddea 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73 ut all P1 values
1ddeb 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c should be small
1ddec 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74 integers..** It
1dded 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 is an error for
1ddee 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69 P1 to be negati
1ddef 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21 ve..**.** If P5!
1ddf0 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 =0 then use the
1ddf1 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1ddf2 74 65 72 20 50 32 20 61 73 20 74 68 65 20 72 6f ter P2 as the ro
1ddf3 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20 ot page, not.**
1ddf4 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20 the value of P2
1ddf5 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 itself..**.** Th
1ddf6 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 ere will be a re
1ddf7 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ad lock on the d
1ddf8 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 atabase whenever
1ddf9 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 there is an.**
1ddfa 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 open cursor. If
1ddfb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61 the database wa
1ddfc 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 s unlocked prior
1ddfd 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 to this instruc
1ddfe 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 tion.** then a r
1ddff 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 ead lock is acqu
1de00 69 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 ired as part of
1de01 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1de02 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 . A read.** loc
1de03 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 k allows other p
1de04 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 rocesses to read
1de05 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 the database bu
1de06 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 t prohibits.** a
1de07 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
1de08 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 from modifying
1de09 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
1de0a 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a he read lock is.
1de0b 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e ** released when
1de0c 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 all cursors are
1de0d 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 closed. If thi
1de0e 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 s instruction at
1de0f 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 tempts.** to get
1de10 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 a read lock but
1de11 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 fails, the scri
1de12 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 pt terminates wi
1de13 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f th an.** SQLITE_
1de14 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e BUSY error code.
1de15 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 .**.** The P4 va
1de16 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 lue may be eithe
1de17 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 r an integer (P4
1de18 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 _INT32) or a poi
1de19 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 nter to.** a Key
1de1a 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 Info structure (
1de1b 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 P4_KEYINFO). If
1de1c 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 it is a pointer
1de1d 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a to a KeyInfo .**
1de1e 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e structure, then
1de1f 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65 20 said structure
1de20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 defines the cont
1de21 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e ent and collatin
1de22 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f g .** sequence o
1de23 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e f the index bein
1de24 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 g opened. Otherw
1de25 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e ise, if P4 is an
1de26 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c integer .** val
1de27 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f ue, it is set to
1de28 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
1de29 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 olumns in the ta
1de2a 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ble..**.** See a
1de2b 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a lso OpenWrite..*
1de2c 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 /./* Opcode: Ope
1de2d 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33 20 nWrite P1 P2 P3
1de2e 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e P4 P5.**.** Open
1de2f 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 a read/write cu
1de30 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e rsor named P1 on
1de31 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1de32 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a dex whose root.*
1de33 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f * page is P2. O
1de34 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20 74 r if P5!=0 use t
1de35 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1de36 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e gister P2 to fin
1de37 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 d the.** root pa
1de38 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 ge..**.** The P4
1de39 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 value may be ei
1de3a 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 ther an integer
1de3b 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 (P4_INT32) or a
1de3c 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 pointer to.** a
1de3d 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1de3e 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 e (P4_KEYINFO).
1de3f 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 If it is a point
1de40 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 er to a KeyInfo
1de41 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 .** structure, t
1de42 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 hen said structu
1de43 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 re defines the c
1de44 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 ontent and colla
1de45 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 ting .** sequenc
1de46 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 e of the index b
1de47 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 eing opened. Oth
1de48 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 erwise, if P4 is
1de49 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 an integer .**
1de4a 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 value, it is set
1de4b 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
1de4c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1de4d 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68 table, or to th
1de4e 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64 e.** largest ind
1de4f 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e ex of any column
1de50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 of the table th
1de51 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75 at is actually u
1de52 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 sed..**.** This
1de53 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b instruction work
1de54 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e s just like Open
1de55 52 65 61 64 20 65 78 63 65 70 74 20 74 68 61 74 Read except that
1de56 20 69 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 it opens the cu
1de57 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f rsor.** in read/
1de58 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 write mode. For
1de59 20 61 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 a given table,
1de5a 74 68 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 there can be one
1de5b 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e or more read-on
1de5c 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 ly.** cursors or
1de5d 20 61 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 a single read/w
1de5e 72 69 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 rite cursor but
1de5f 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 not both..**.**
1de60 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 See also OpenRea
1de61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 d..*/.case OP_Op
1de62 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f enRead:.case OP_
1de63 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66 OpenWrite: {.#if
1de64 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1de65 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1de66 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20 o u.aw */. int
1de67 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66 nField;. KeyInf
1de68 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 o *pKeyInfo;. i
1de69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 nt p2;. int iDb
1de6a 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a ;. int wrFlag;.
1de6b 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 Btree *pX;. V
1de6c 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b dbeCursor *pCur;
1de6d 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 . Db *pDb;.#end
1de6e 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1de6f 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1de70 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69 66 28 20 u.aw */.. if(
1de71 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 p->expired ){.
1de72 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 rc = SQLITE_AB
1de73 4f 52 54 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a ORT;. break;.
1de74 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e 46 69 65 }.. u.aw.nFie
1de75 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 ld = 0;. u.aw.p
1de76 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 75 KeyInfo = 0;. u
1de77 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .aw.p2 = pOp->p2
1de78 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20 3d 20 70 ;. u.aw.iDb = p
1de79 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 Op->p3;. assert
1de7a 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30 20 26 26 ( u.aw.iDb>=0 &&
1de7b 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d 3e 6e 44 u.aw.iDb<db->nD
1de7c 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1de7d 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1de7e 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29 21 3d 30 1<<u.aw.iDb))!=0
1de7f 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44 62 20 3d );. u.aw.pDb =
1de80 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61 77 2e 69 &db->aDb[u.aw.i
1de81 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70 58 20 3d Db];. u.aw.pX =
1de82 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42 74 3b 0a u.aw.pDb->pBt;.
1de83 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 assert( u.aw.p
1de84 58 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f X!=0 );. if( pO
1de85 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 p->opcode==OP_Op
1de86 65 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 75 enWrite ){. u
1de87 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 31 3b 0a .aw.wrFlag = 1;.
1de88 20 20 20 20 69 66 28 20 75 2e 61 77 2e 70 44 62 if( u.aw.pDb
1de89 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f ->pSchema->file_
1de8a 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 format < p->minW
1de8b 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 riteFileFormat )
1de8c 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 {. p->minWr
1de8d 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 iteFileFormat =
1de8e 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d u.aw.pDb->pSchem
1de8f 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a a->file_format;.
1de90 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1de91 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d u.aw.wrFlag =
1de92 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 0;. }. if( pO
1de93 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 61 73 73 p->p5 ){. ass
1de94 65 72 74 28 20 75 2e 61 77 2e 70 32 3e 30 20 29 ert( u.aw.p2>0 )
1de95 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e ;. assert( u.
1de96 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 aw.p2<=p->nMem )
1de97 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61 4d ;. pIn2 = &aM
1de98 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20 20 em[u.aw.p2];.
1de99 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1de9a 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b ntegerify(pIn2);
1de9b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 3d 20 28 . u.aw.p2 = (
1de9c 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 int)pIn2->u.i;.
1de9d 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e 70 /* The u.aw.p
1de9e 32 20 76 61 6c 75 65 20 61 6c 77 61 79 73 20 63 2 value always c
1de9f 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 72 69 6f omes from a prio
1dea0 72 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 r OP_CreateTable
1dea1 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 opcode and.
1dea2 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 65 20 77 ** that opcode w
1dea3 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 74 20 74 ill always set t
1dea4 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 65 he u.aw.p2 value
1dea5 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f 72 to 2 or more or
1dea6 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20 20 else fail..
1dea7 2a 2a 20 49 66 20 74 68 65 72 65 20 77 65 72 65 ** If there were
1dea8 20 61 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 a failure, the
1dea9 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1deaa 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 20 68 61 nt would have ha
1deab 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f lted. ** befo
1deac 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 re reaching this
1dead 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a 2f instruction. */
1deae 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 75 . if( NEVER(u
1deaf 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20 20 .aw.p2<2) ) {.
1deb0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1deb1 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1deb2 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1deb3 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1deb4 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 }. }. if( pOp
1deb5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 ->p4type==P4_KEY
1deb6 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61 77 INFO ){. u.aw
1deb7 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d .pKeyInfo = pOp-
1deb8 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 >p4.pKeyInfo;.
1deb9 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d u.aw.pKeyInfo-
1deba 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 >enc = ENC(p->db
1debb 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 );. u.aw.nFie
1debc 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e ld = u.aw.pKeyIn
1debd 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20 fo->nField+1;.
1debe 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 }else if( pOp->p
1debf 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 4type==P4_INT32
1dec0 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65 ){. u.aw.nFie
1dec1 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a ld = pOp->p4.i;.
1dec2 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4f }. assert( pO
1dec3 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e p->p1>=0 );. u.
1dec4 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 aw.pCur = alloca
1dec5 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d teCursor(p, pOp-
1dec6 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c 64 >p1, u.aw.nField
1dec7 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b 0a , u.aw.iDb, 1);.
1dec8 20 20 69 66 28 20 75 2e 61 77 2e 70 43 75 72 3d if( u.aw.pCur=
1dec9 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1deca 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 6e ;. u.aw.pCur->n
1decb 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 ullRow = 1;. rc
1decc 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
1decd 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20 75 ursor(u.aw.pX, u
1dece 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72 46 .aw.p2, u.aw.wrF
1decf 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49 6e lag, u.aw.pKeyIn
1ded0 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 fo, u.aw.pCur->p
1ded1 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77 2e Cursor);. u.aw.
1ded2 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCur->pKeyInfo =
1ded3 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a u.aw.pKeyInfo;.
1ded4 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 74 20 70 . /* Since it p
1ded5 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 erforms no memor
1ded6 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 y allocation or
1ded7 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c IO, the only val
1ded8 75 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 71 ues that. ** sq
1ded9 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
1deda 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 61 72 () may return ar
1dedb 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 61 e SQLITE_EMPTY a
1dedc 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 nd SQLITE_OK..
1dedd 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 ** SQLITE_EMPTY
1dede 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65 64 is only returned
1dedf 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 when attempting
1dee0 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 74 61 62 to open the tab
1dee1 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20 61 le. ** rooted a
1dee2 74 20 70 61 67 65 20 31 20 6f 66 20 61 20 7a 65 t page 1 of a ze
1dee3 72 6f 2d 62 79 74 65 20 64 61 74 61 62 61 73 65 ro-byte database
1dee4 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1dee5 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 rc==SQLITE_EMPTY
1dee6 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
1dee7 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 K );. if( rc==S
1dee8 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 QLITE_EMPTY ){.
1dee9 20 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43 u.aw.pCur->pC
1deea 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 72 ursor = 0;. r
1deeb 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1deec 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
1deed 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 61 VdbeCursor.isTa
1deee 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 65 78 20 ble and isIndex
1deef 76 61 72 69 61 62 6c 65 73 2e 20 50 72 65 76 69 variables. Previ
1def0 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 0a ous versions of.
1def1 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 64 ** SQLite used
1def2 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 to check if the
1def3 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67 73 root-page flags
1def4 20 77 65 72 65 20 73 61 6e 65 20 61 74 20 74 68 were sane at th
1def5 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e is point. ** an
1def6 64 20 72 65 70 6f 72 74 20 64 61 74 61 62 61 73 d report databas
1def7 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 e corruption if
1def8 74 68 65 79 20 77 65 72 65 20 6e 6f 74 2c 20 62 they were not, b
1def9 75 74 20 74 68 69 73 20 63 68 65 63 6b 20 68 61 ut this check ha
1defa 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f 76 s. ** since mov
1defb 65 64 20 69 6e 74 6f 20 74 68 65 20 62 74 72 65 ed into the btre
1defc 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20 75 e layer. */. u
1defd 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c .aw.pCur->isTabl
1defe 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21 e = pOp->p4type!
1deff 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75 =P4_KEYINFO;. u
1df00 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64 65 .aw.pCur->isInde
1df01 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d 3e x = !u.aw.pCur->
1df02 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b isTable;. break
1df03 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1df04 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31 OpenEphemeral P1
1df05 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1df06 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 Open a new curs
1df07 6f 72 20 50 31 20 74 6f 20 61 20 74 72 61 6e 73 or P1 to a trans
1df08 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 ient table..** T
1df09 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 he cursor is alw
1df0a 61 79 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f ays opened read/
1df0b 77 72 69 74 65 20 65 76 65 6e 20 69 66 20 0a 2a write even if .*
1df0c 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 * the main datab
1df0d 61 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 ase is read-only
1df0e 2e 20 20 54 68 65 20 74 72 61 6e 73 69 65 6e 74 . The transient
1df0f 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 or virtual.** t
1df10 61 62 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 able is deleted
1df11 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 automatically wh
1df12 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 en the cursor is
1df13 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 closed..**.** P
1df14 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 2 is the number
1df15 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
1df16 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e e virtual table.
1df17 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 .** The cursor p
1df18 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 72 65 65 oints to a BTree
1df19 20 74 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 20 table if P4==0
1df1a 61 6e 64 20 74 6f 20 61 20 42 54 72 65 65 20 69 and to a BTree i
1df1b 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 73 ndex.** if P4 is
1df1c 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 34 20 69 not 0. If P4 i
1df1d 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 s not NULL, it p
1df1e 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e oints to a KeyIn
1df1f 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 fo structure.**
1df20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 that defines the
1df21 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 20 format of keys
1df22 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a in the index..**
1df23 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1df24 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20 was once called
1df25 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74 OpenTemp. But t
1df26 68 61 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 63 hat created.** c
1df27 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65 onfusion because
1df28 20 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20 the term "temp
1df29 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65 table", might re
1df2a 66 65 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 6f fer either.** to
1df2b 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74 a TEMP table at
1df2c 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 the SQL level,
1df2d 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70 or to a table op
1df2e 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 20 ened by.** this
1df2f 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 68 opcode. Then th
1df30 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63 61 is opcode was ca
1df31 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e 20 ll OpenVirtual.
1df32 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 65 But.** that cre
1df33 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 ated confusion w
1df34 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20 76 69 ith the whole vi
1df35 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 61 rtual-table idea
1df36 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 ..*/.case OP_Ope
1df37 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 nEphemeral: {.#i
1df38 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1df39 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1df3a 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64 62 to u.ax */. Vdb
1df3b 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 eCursor *pCx;.#e
1df3c 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1df3d 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1df3e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74 61 to u.ax */. sta
1df3f 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 tic const int op
1df40 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 20 enFlags =.
1df41 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
1df42 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 WRITE |. SQ
1df43 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
1df44 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1df45 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c OPEN_EXCLUSIVE |
1df46 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1df47 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
1df48 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1df49 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 OPEN_TRANSIENT_D
1df4a 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f B;.. assert( pO
1df4b 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e p->p1>=0 );. u.
1df4c 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 ax.pCx = allocat
1df4d 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e eCursor(p, pOp->
1df4e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c p1, pOp->p2, -1,
1df4f 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 78 2e 1);. if( u.ax.
1df50 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f pCx==0 ) goto no
1df51 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43 78 _mem;. u.ax.pCx
1df52 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1df53 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1df54 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c eeFactory(db, 0,
1df55 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 1, SQLITE_DEFAU
1df56 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 LT_TEMP_CACHE_SI
1df57 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 ZE, openFlags,.
1df58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1df59 20 20 20 20 20 20 20 20 20 20 26 75 2e 61 78 2e &u.ax.
1df5a 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 pCx->pBt);. if(
1df5b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1df5c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1df5d 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e e3BtreeBeginTran
1df5e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c s(u.ax.pCx->pBt,
1df5f 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 1);. }. if( r
1df60 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1df61 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e /* If a tran
1df62 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20 72 sient index is r
1df63 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65 20 equired, create
1df64 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 20 it by calling.
1df65 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 ** sqlite3Btre
1df66 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20 77 eCreateTable() w
1df67 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a 45 ith the BTREE_ZE
1df68 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66 6f RODATA flag befo
1df69 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e re. ** openin
1df6a 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e 73 g it. If a trans
1df6b 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72 65 ient table is re
1df6c 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73 65 quired, just use
1df6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 6f the. ** auto
1df6e 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 matically create
1df6f 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f d table with roo
1df70 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e 54 t-page 1 (an INT
1df71 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 KEY table)..
1df72 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e */. if( pOp->
1df73 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 p4.pKeyInfo ){.
1df74 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 int pgno;.
1df75 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1df76 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 ->p4type==P4_KEY
1df77 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 63 INFO );. rc
1df78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
1df79 72 65 61 74 65 54 61 62 6c 65 28 75 2e 61 78 2e reateTable(u.ax.
1df7a 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c pCx->pBt, &pgno,
1df7b 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 BTREE_ZERODATA)
1df7c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
1df7d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1df7e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e assert( pgn
1df7f 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 o==MASTER_ROOT+1
1df80 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
1df81 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1df82 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 sor(u.ax.pCx->pB
1df83 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20 20 t, pgno, 1,.
1df84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1df85 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 79 (Key
1df86 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c Info*)pOp->p4.z,
1df87 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73 u.ax.pCx->pCurs
1df88 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 or);. u.a
1df89 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 x.pCx->pKeyInfo
1df8a 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e = pOp->p4.pKeyIn
1df8b 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78 fo;. u.ax
1df8c 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e .pCx->pKeyInfo->
1df8d 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 enc = ENC(p->db)
1df8e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1df8f 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c u.ax.pCx->isTabl
1df90 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
1df91 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1df92 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 ite3BtreeCursor(
1df93 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 4d u.ax.pCx->pBt, M
1df94 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 ASTER_ROOT, 1, 0
1df95 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 , u.ax.pCx->pCur
1df96 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 2e 61 78 sor);. u.ax
1df97 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 .pCx->isTable =
1df98 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 1;. }. }. u
1df99 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 .ax.pCx->isIndex
1df9a 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69 73 = !u.ax.pCx->is
1df9b 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a Table;. break;.
1df9c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 }../* Opcode: Op
1df9d 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 50 enPseudo P1 P2 P
1df9e 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 3 * *.**.** Open
1df9f 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68 a new cursor th
1dfa0 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 at points to a f
1dfa1 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 ake table that c
1dfa2 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 ontains a single
1dfa3 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e .** row of data.
1dfa4 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 The content of
1dfa5 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e that one row in
1dfa6 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1dfa7 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 74 memory.** regist
1dfa8 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 72 er P2. In other
1dfa9 20 77 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 50 words, cursor P
1dfaa 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c 69 1 becomes an ali
1dfab 61 73 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 4d as for the .** M
1dfac 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 20 EM_Blob content
1dfad 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 72 65 67 contained in reg
1dfae 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1dfaf 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 A pseudo-table c
1dfb0 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20 6f reated by this o
1dfb1 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f pcode is used to
1dfb2 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 hold the a sing
1dfb3 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 le.** row output
1dfb4 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 from the sorter
1dfb5 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 so that the row
1dfb6 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 can be decompos
1dfb7 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 ed into.** indiv
1dfb8 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 idual columns us
1dfb9 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d ing the OP_Colum
1dfba 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 4f n opcode. The O
1dfbb 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a P_Column opcode.
1dfbc 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 63 ** is the only c
1dfbd 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74 68 61 ursor opcode tha
1dfbe 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61 20 70 t works with a p
1dfbf 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a seudo-table..**.
1dfc0 2a 2a 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d ** P3 is the num
1dfc1 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e ber of fields in
1dfc2 20 74 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 the records tha
1dfc3 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 t will be stored
1dfc4 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 by.** the pseud
1dfc5 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 o-table..*/.case
1dfc6 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 OP_OpenPseudo:
1dfc7 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1dfc8 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1dfc9 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 20 d into u.ay */.
1dfca 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1dfcb 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1dfcc 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1dfcd 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a d into u.ay */..
1dfce 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1dfcf 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e 70 1>=0 );. u.ay.p
1dfd0 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 Cx = allocateCur
1dfd1 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 sor(p, pOp->p1,
1dfd2 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 3b pOp->p3, -1, 0);
1dfd3 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 43 78 3d . if( u.ay.pCx=
1dfd4 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1dfd5 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e 75 ;. u.ay.pCx->nu
1dfd6 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 61 llRow = 1;. u.a
1dfd7 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 62 y.pCx->pseudoTab
1dfd8 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 3b leReg = pOp->p2;
1dfd9 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54 . u.ay.pCx->isT
1dfda 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61 79 able = 1;. u.ay
1dfdb 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 .pCx->isIndex =
1dfdc 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 0;. break;.}../
1dfdd 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20 * Opcode: Close
1dfde 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1dfdf 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 Close a cursor
1dfe0 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 previously opene
1dfe1 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31 20 d as P1. If P1
1dfe2 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e is not.** curren
1dfe3 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69 tly open, this i
1dfe4 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20 nstruction is a
1dfe5 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f no-op..*/.case O
1dfe6 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73 73 P_Close: {. ass
1dfe7 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1dfe8 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1dfe9 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 ursor );. sqlit
1dfea 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 e3VdbeFreeCursor
1dfeb 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 (p, p->apCsr[pOp
1dfec 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70 43 ->p1]);. p->apC
1dfed 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b sr[pOp->p1] = 0;
1dfee 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1dfef 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 65 20 50 Opcode: SeekGe P
1dff0 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1dff1 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 ** If cursor P1
1dff2 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c refers to an SQL
1dff3 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 table (B-Tree t
1dff4 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 hat uses integer
1dff5 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 keys), .** use
1dff6 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1dff7 69 73 74 65 72 20 50 33 20 61 73 20 74 68 65 20 ister P3 as the
1dff8 6b 65 79 2e 20 20 49 66 20 63 75 72 73 6f 72 20 key. If cursor
1dff9 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f P1 refers .** to
1dffa 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 an SQL index, t
1dffb 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 hen P3 is the fi
1dffc 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 rst in an array
1dffd 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 of P4 registers
1dffe 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 .** that are use
1dfff 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 d as an unpacked
1e000 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a index key. .**.
1e001 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 ** Reposition cu
1e002 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 rsor P1 so that
1e003 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1e004 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 e smallest entry
1e005 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 that .** is gre
1e006 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
1e007 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 al to the key va
1e008 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 lue. If there ar
1e009 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a e no records .**
1e00a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
1e00b 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 equal to the ke
1e00c 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 y and P2 is not
1e00d 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 zero, then jump
1e00e 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 to P2..**.** See
1e00f 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1e010 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 tFound, Distinct
1e011 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 74 , SeekLt, SeekGt
1e012 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f , SeekLe.*/./* O
1e013 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 31 pcode: SeekGt P1
1e014 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1e015 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 * If cursor P1 r
1e016 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 efers to an SQL
1e017 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 table (B-Tree th
1e018 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 at uses integer
1e019 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 keys), .** use t
1e01a 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1e01b 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 ster P3 as a key
1e01c 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 . If cursor P1 r
1e01d 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 efers .** to an
1e01e 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 SQL index, then
1e01f 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P3 is the first
1e020 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 in an array of P
1e021 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 4 registers .**
1e022 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 that are used as
1e023 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1e024 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 ex key. .**.** R
1e025 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 eposition cursor
1e026 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 P1 so that it
1e027 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d points to the sm
1e028 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 allest entry tha
1e029 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 t .** is greater
1e02a 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 than the key va
1e02b 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 lue. If there ar
1e02c 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67 72 65 e no records gre
1e02d 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74 68 ater than .** th
1e02e 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 e key and P2 is
1e02f 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a not zero, then j
1e030 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a ump to P2..**.**
1e031 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 See also: Found
1e032 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 , NotFound, Dist
1e033 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 inct, SeekLt, Se
1e034 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a ekGe, SeekLe.*/.
1e035 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c /* Opcode: SeekL
1e036 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 20 t P1 P2 P3 P4 *
1e037 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1e038 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1e039 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1e03a 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1e03b 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1e03c 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1e03d 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1e03e 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1e03f 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1e040 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1e041 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1e042 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1e043 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1e044 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1e045 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1e046 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1e047 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1e048 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1e049 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 it points to t
1e04a 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 he largest entry
1e04b 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 that .** is les
1e04c 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 s than the key v
1e04d 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 alue. If there a
1e04e 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65 re no records le
1e04f 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 ss than .** the
1e050 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f key and P2 is no
1e051 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d t zero, then jum
1e052 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 p to P2..**.** S
1e053 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1e054 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e NotFound, Distin
1e055 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b ct, SeekGt, Seek
1e056 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a Ge, SeekLe.*/./*
1e057 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65 20 Opcode: SeekLe
1e058 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1e059 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 .** If cursor P1
1e05a 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 refers to an SQ
1e05b 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 L table (B-Tree
1e05c 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 that uses intege
1e05d 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 r keys), .** use
1e05e 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1e05f 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b gister P3 as a k
1e060 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 ey. If cursor P1
1e061 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 refers .** to a
1e062 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 n SQL index, the
1e063 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 n P3 is the firs
1e064 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 t in an array of
1e065 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a P4 registers .*
1e066 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 * that are used
1e067 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 as an unpacked i
1e068 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a ndex key. .**.**
1e069 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 Reposition curs
1e06a 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 or P1 so that it
1e06b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c points to the l
1e06c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 argest entry tha
1e06d 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 t .** is less th
1e06e 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1e06f 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 he key value. If
1e070 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 there are no re
1e071 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20 74 cords .** less t
1e072 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1e073 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 the key and P2 i
1e074 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e s not zero, then
1e075 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a jump to P2..**.
1e076 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 ** See also: Fou
1e077 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 nd, NotFound, Di
1e078 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 stinct, SeekGt,
1e079 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a 2a SeekGe, SeekLt.*
1e07a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 74 /.case OP_SeekLt
1e07b 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d : /* jum
1e07c 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1e07d 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 20 20 20 P_SeekLe:
1e07e 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1e07f 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 65 /.case OP_SeekGe
1e080 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d : /* jum
1e081 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1e082 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 20 20 20 P_SeekGt: {
1e083 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1e084 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e085 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e086 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20 d into u.az */.
1e087 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 int res;. int
1e088 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 oc;. VdbeCursor
1e089 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65 64 *pC;. Unpacked
1e08a 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74 20 Record r;. int
1e08b 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 20 69 4b nField;. i64 iK
1e08c 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ey; /* The
1e08d 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 rowid we are to
1e08e 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 seek to */.#endi
1e08f 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e090 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e091 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.az */.. asser
1e092 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e093 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e094 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 sor );. assert(
1e095 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20 pOp->p2!=0 );.
1e096 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61 70 u.az.pC = p->ap
1e097 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e098 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 21 assert( u.az.pC!
1e099 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e09a 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f 54 u.az.pC->pseudoT
1e09b 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 ableReg==0 );.
1e09c 61 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 4c assert( OP_SeekL
1e09d 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 31 e == OP_SeekLt+1
1e09e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4f 50 );. assert( OP
1e09f 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50 5f 53 65 _SeekGe == OP_Se
1e0a0 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61 73 73 65 ekLt+2 );. asse
1e0a1 72 74 28 20 4f 50 5f 53 65 65 6b 47 74 20 3d 3d rt( OP_SeekGt ==
1e0a2 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20 29 3b 0a OP_SeekLt+3 );.
1e0a3 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 if( u.az.pC->p
1e0a4 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 Cursor!=0 ){.
1e0a5 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e u.az.oc = pOp->
1e0a6 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75 2e 61 7a opcode;. u.az
1e0a7 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 .pC->nullRow = 0
1e0a8 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 70 ;. if( u.az.p
1e0a9 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20 20 C->isTable ){.
1e0aa 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 /* The input
1e0ab 20 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69 67 value in P3 mig
1e0ac 68 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79 70 ht be of any typ
1e0ad 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61 6c e: integer, real
1e0ae 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20 20 , string,.
1e0af 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c ** blob, or NULL
1e0b0 2e 20 20 42 75 74 20 69 74 20 6e 65 65 64 73 20 . But it needs
1e0b1 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 to be an integer
1e0b2 20 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20 64 before we can d
1e0b3 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 o. ** the s
1e0b4 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20 69 eek, so covert i
1e0b5 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e 33 t. */. pIn3
1e0b6 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1e0b7 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 4e 75 ];. applyNu
1e0b8 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 mericAffinity(pI
1e0b9 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e n3);. u.az.
1e0ba 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 iKey = sqlite3Vd
1e0bb 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 beIntValue(pIn3)
1e0bc 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d ;. u.az.pC-
1e0bd 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1e0be 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 0;.. /* If
1e0bf 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 6f 75 the P3 value cou
1e0c0 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 ld not be conver
1e0c1 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 ted into an inte
1e0c2 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20 20 20 ger without.
1e0c3 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 ** loss of inf
1e0c4 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 ormation, then s
1e0c5 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73 69 6e pecial processin
1e0c6 67 20 69 73 20 72 65 71 75 69 72 65 64 2e 2e 2e g is required...
1e0c7 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 */. if( (p
1e0c8 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In3->flags & MEM
1e0c9 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 _Int)==0 ){.
1e0ca 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 if( (pIn3->f
1e0cb 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 lags & MEM_Real)
1e0cc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1e0cd 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 /* If the P3 va
1e0ce 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f lue cannot be co
1e0cf 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 nverted into any
1e0d0 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 kind of a numbe
1e0d1 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 r,. **
1e0d2 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20 69 73 then the seek is
1e0d3 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 not possible, s
1e0d4 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a o jump to P2 */.
1e0d5 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 pc = p
1e0d6 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1e0d7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e0d8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f }. /
1e0d9 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 * If we reach th
1e0da 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 is point, then t
1e0db 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75 73 74 he P3 value must
1e0dc 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 be a floating.
1e0dd 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 ** point
1e0de 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 number. */.
1e0df 20 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 33 assert( (pIn3
1e0e0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
1e0e1 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 al)!=0 );..
1e0e2 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79 if( u.az.iKey
1e0e3 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 ==SMALLEST_INT64
1e0e4 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f && (pIn3->r<(do
1e0e5 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c uble)u.az.iKey |
1e0e6 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a | pIn3->r>0) ){.
1e0e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1e0e8 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 6f P3 value is too
1e0e9 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 large in magnit
1e0ea 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 ude to be expres
1e0eb 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 sed as an.
1e0ec 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 ** integer.
1e0ed 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61 */. u.a
1e0ee 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 z.res = 1;.
1e0ef 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 if( pIn3->r
1e0f0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <0 ){.
1e0f1 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3e 3d 4f if( u.az.oc>=O
1e0f2 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 61 73 73 P_SeekGe ){ ass
1e0f3 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 ert( u.az.oc==OP
1e0f4 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e _SeekGe || u.az.
1e0f5 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b oc==OP_SeekGt );
1e0f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
1e0f7 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e0f8 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 First(u.az.pC->p
1e0f9 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 Cursor, &u.az.re
1e0fa 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 s);.
1e0fb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1e0fc 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 _OK ) goto abort
1e0fd 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1e0fe 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1e0ff 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1e100 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1e101 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65 6b 4c 65 az.oc<=OP_SeekLe
1e102 20 29 7b 20 20 61 73 73 65 72 74 28 20 75 2e 61 ){ assert( u.a
1e103 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 z.oc==OP_SeekLt
1e104 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 || u.az.oc==OP_S
1e105 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 20 eekLe );.
1e106 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1e107 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 61 te3BtreeLast(u.a
1e108 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 z.pC->pCursor, &
1e109 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 u.az.res);.
1e10a 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
1e10b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
1e10c 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1e10d 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 rror;.
1e10e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
1e10f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1e110 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 az.res ){.
1e111 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1e112 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 p2 - 1;.
1e113 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 }. br
1e114 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c eak;. }el
1e115 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d se if( u.az.oc==
1e116 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 OP_SeekLt || u.a
1e117 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 z.oc==OP_SeekGe
1e118 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1e119 55 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67 28 Use the ceiling(
1e11a 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f ) function to co
1e11b 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 nvert real->int
1e11c 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
1e11d 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 pIn3->r > (doub
1e11e 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 le)u.az.iKey ) u
1e11f 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 .az.iKey++;.
1e120 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e121 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 /* Use the
1e122 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e floor() function
1e123 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c to convert real
1e124 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 ->int */.
1e125 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e assert( u.az.
1e126 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c oc==OP_SeekLe ||
1e127 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1e128 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 kGt );.
1e129 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 if( pIn3->r < (
1e12a 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 double)u.az.iKey
1e12b 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a ) u.az.iKey--;.
1e12c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e12d 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
1e12e 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1e12f 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d npacked(u.az.pC-
1e130 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 >pCursor, 0, (u6
1e131 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20 4)u.az.iKey, 0,
1e132 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 &u.az.res);.
1e133 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1e134 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
1e135 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e136 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a _error;. }.
1e137 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 if( u.az.r
1e138 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 es==0 ){.
1e139 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 u.az.pC->rowidI
1e13a 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 sValid = 1;.
1e13b 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73 u.az.pC->las
1e13c 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b tRowid = u.az.iK
1e13d 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ey;. }.
1e13e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 }else{. u.a
1e13f 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e z.nField = pOp->
1e140 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 p4.i;. asse
1e141 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1e142 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 =P4_INT32 );.
1e143 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e assert( u.az.
1e144 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 nField>0 );.
1e145 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66 u.az.r.pKeyInf
1e146 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65 o = u.az.pC->pKe
1e147 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61 yInfo;. u.a
1e148 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 z.r.nField = (u1
1e149 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 0a 6)u.az.nField;..
1e14a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 /* The nex
1e14b 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64 65 20 63 t line of code c
1e14c 6f 6d 70 75 74 65 73 20 61 73 20 66 6f 6c 6c 6f omputes as follo
1e14d 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74 65 72 3a ws, only faster:
1e14e 0a 20 20 20 20 20 20 2a 2a 20 20 20 69 66 28 20 . ** if(
1e14f 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1e150 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f Gt || u.az.oc==O
1e151 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 P_SeekLe ){.
1e152 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a 2e 72 2e ** u.az.r.
1e153 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 flags = UNPACKED
1e154 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20 20 _INCRKEY;.
1e155 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ** }else{.
1e156 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a 2e 72 2e ** u.az.r.
1e157 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 flags = 0;.
1e158 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20 20 2a 2f ** }. */
1e159 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c . u.az.r.fl
1e15a 61 67 73 20 3d 20 28 75 31 36 29 28 55 4e 50 41 ags = (u16)(UNPA
1e15b 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28 CKED_INCRKEY * (
1e15c 31 20 26 20 28 75 2e 61 7a 2e 6f 63 20 2d 20 4f 1 & (u.az.oc - O
1e15d 50 5f 53 65 65 6b 4c 74 29 29 29 3b 0a 20 20 20 P_SeekLt)));.
1e15e 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e assert( u.az.
1e15f 6f 63 21 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c oc!=OP_SeekGt ||
1e160 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 55 u.az.r.flags==U
1e161 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
1e162 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1e163 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 u.az.oc!=OP_See
1e164 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c kLe || u.az.r.fl
1e165 61 67 73 3d 3d 55 4e 50 41 43 4b 45 44 5f 49 4e ags==UNPACKED_IN
1e166 43 52 4b 45 59 20 29 3b 0a 20 20 20 20 20 20 61 CRKEY );. a
1e167 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d ssert( u.az.oc!=
1e168 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61 OP_SeekGe || u.a
1e169 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20 29 3b 0a z.r.flags==0 );.
1e16a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e16b 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 74 az.oc!=OP_SeekLt
1e16c 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 || u.az.r.flags
1e16d 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 75 2e ==0 );.. u.
1e16e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 az.r.aMem = &aMe
1e16f 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1e170 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 ExpandBlob(u.a
1e171 7a 2e 72 2e 61 4d 65 6d 29 3b 0a 20 20 20 20 20 z.r.aMem);.
1e172 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e173 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1e174 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f (u.az.pC->pCurso
1e175 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30 2c 20 30 r, &u.az.r, 0, 0
1e176 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 , &u.az.res);.
1e177 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1e178 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1e179 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1e17a 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1e17b 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d }. u.az.pC-
1e17c 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1e17d 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 0;. }. u.a
1e17e 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f z.pC->deferredMo
1e17f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e veto = 0;. u.
1e180 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 az.pC->cacheStat
1e181 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e182 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1e183 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 TEST. sqlite3
1e184 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b _search_count++;
1e185 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 .#endif. if(
1e186 75 2e 61 7a 2e 6f 63 3e 3d 4f 50 5f 53 65 65 6b u.az.oc>=OP_Seek
1e187 47 65 20 29 7b 20 20 61 73 73 65 72 74 28 20 75 Ge ){ assert( u
1e188 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 .az.oc==OP_SeekG
1e189 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 e || u.az.oc==OP
1e18a 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 _SeekGt );.
1e18b 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3c 30 20 if( u.az.res<0
1e18c 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 || (u.az.res==0
1e18d 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 && u.az.oc==OP_S
1e18e 65 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 eekGt) ){.
1e18f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e190 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e 70 43 2d reeNext(u.az.pC-
1e191 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e >pCursor, &u.az.
1e192 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 res);. if
1e193 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1e194 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e195 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1e196 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 u.az.pC->rowi
1e197 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e198 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e199 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 30 3b u.az.res = 0;
1e19a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1e19b 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1e19c 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 ( u.az.oc==OP_Se
1e19d 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d ekLt || u.az.oc=
1e19e 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 =OP_SeekLe );.
1e19f 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 if( u.az.res
1e1a0 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d >0 || (u.az.res=
1e1a1 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f =0 && u.az.oc==O
1e1a2 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20 P_SeekLt) ){.
1e1a3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e1a4 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 75 3BtreePrevious(u
1e1a5 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c .az.pC->pCursor,
1e1a6 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 &u.az.res);.
1e1a7 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1e1a8 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1e1a9 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1e1aa 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 ;. u.az.p
1e1ab 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e1ac 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
1e1ad 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 2e 61 {. /* u.a
1e1ae 7a 2e 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e z.res might be n
1e1af 65 67 61 74 69 76 65 20 62 65 63 61 75 73 65 20 egative because
1e1b0 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
1e1b1 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 ty. Check to.
1e1b2 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 ** see if
1e1b3 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
1e1b4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1e1b5 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 u.az.res =
1e1b6 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 sqlite3BtreeEof(
1e1b7 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.az.pC->pCursor
1e1b8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1e1b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1e1ba 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 69 66 ->p2>0 );. if
1e1bb 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 ( u.az.res ){.
1e1bc 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1e1bd 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 - 1;. }. }e
1e1be 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 lse{. /* This
1e1bf 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 74 happens when at
1e1c0 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e tempting to open
1e1c1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 the sqlite3_mas
1e1c2 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a ter table. **
1e1c3 20 66 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 for read access
1e1c4 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1e1c5 45 4d 50 54 59 2e 20 49 6e 20 74 68 69 73 20 63 EMPTY. In this c
1e1c6 61 73 65 20 61 6c 77 61 79 73 0a 20 20 20 20 2a ase always. *
1e1c7 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 * take the jump
1e1c8 28 73 69 6e 63 65 20 74 68 65 72 65 20 61 72 65 (since there are
1e1c9 20 6e 6f 20 72 65 63 6f 72 64 73 20 69 6e 20 74 no records in t
1e1ca 68 65 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a he table).. *
1e1cb 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e /. pc = pOp->
1e1cc 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1e1cd 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e1ce 65 3a 20 53 65 65 6b 20 50 31 20 50 32 20 2a 20 e: Seek P1 P2 *
1e1cf 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 * *.**.** P1 is
1e1d0 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65 20 63 75 an open table cu
1e1d1 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 61 rsor and P2 is a
1e1d2 20 72 6f 77 69 64 20 69 6e 74 65 67 65 72 2e 20 rowid integer.
1e1d3 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 Arrange.** for
1e1d4 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68 P1 to move so th
1e1d5 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1e1d6 74 68 65 20 72 6f 77 69 64 20 67 69 76 65 6e 20 the rowid given
1e1d7 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 by P2..**.** Thi
1e1d8 73 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 s is actually a
1e1d9 64 65 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20 deferred seek.
1e1da 4e 6f 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79 Nothing actually
1e1db 20 68 61 70 70 65 6e 73 20 75 6e 74 69 6c 0a 2a happens until.*
1e1dc 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 * the cursor is
1e1dd 75 73 65 64 20 74 6f 20 72 65 61 64 20 61 20 72 used to read a r
1e1de 65 63 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79 ecord. That way
1e1df 2c 20 69 66 20 6e 6f 20 72 65 61 64 73 0a 2a 2a , if no reads.**
1e1e0 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 occur, no unnec
1e1e1 65 73 73 61 72 79 20 49 2f 4f 20 68 61 70 70 65 essary I/O happe
1e1e2 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ns..*/.case OP_S
1e1e3 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 eek: { /* in2
1e1e4 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e1e5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e1e6 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f ved into u.ba */
1e1e7 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e1e8 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 C;.#endif /* loc
1e1e9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e1ea 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a ed into u.ba */.
1e1eb 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e1ec 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e1ed 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e1ee 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d 3e 61 70 u.ba.pC = p->ap
1e1ef 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e1f0 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70 43 21 assert( u.ba.pC!
1e1f1 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 =0 );. if( ALWA
1e1f2 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70 43 75 72 YS(u.ba.pC->pCur
1e1f3 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 sor!=0) ){. a
1e1f4 73 73 65 72 74 28 20 75 2e 62 61 2e 70 43 2d 3e ssert( u.ba.pC->
1e1f5 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75 isTable );. u
1e1f6 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .ba.pC->nullRow
1e1f7 3d 20 30 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 = 0;. pIn2 =
1e1f8 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a &aMem[pOp->p2];.
1e1f9 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 u.ba.pC->mov
1e1fa 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 etoTarget = sqli
1e1fb 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1e1fc 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e pIn2);. u.ba.
1e1fd 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e1fe 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 = 0;. u.ba.p
1e1ff 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e200 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 o = 1;. }. bre
1e201 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 ak;.}. ../* Opc
1e202 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 ode: Found P1 P2
1e203 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 P3 P4 *.**.** I
1e204 66 20 50 34 3d 3d 30 20 74 68 65 6e 20 72 65 67 f P4==0 then reg
1e205 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 ister P3 holds a
1e206 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 blob constructe
1e207 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e d by MakeRecord.
1e208 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74 68 65 If.** P4>0 the
1e209 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 n register P3 is
1e20a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 50 34 the first of P4
1e20b 20 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 registers that
1e20c 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 form an unpacked
1e20d 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a .** record..**.*
1e20e 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f * Cursor P1 is o
1e20f 6e 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65 n an index btree
1e210 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 . If the record
1e211 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 50 identified by P
1e212 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69 73 20 61 3 and P4.** is a
1e213 20 70 72 65 66 69 78 20 6f 66 20 61 6e 79 20 65 prefix of any e
1e214 6e 74 72 79 20 69 6e 20 50 31 20 74 68 65 6e 20 ntry in P1 then
1e215 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 a jump is made t
1e216 6f 20 50 32 20 61 6e 64 0a 2a 2a 20 50 31 20 69 o P2 and.** P1 i
1e217 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1e218 61 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 at the matching
1e219 65 6e 74 72 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 entry..*/./* Opc
1e21a 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 31 ode: NotFound P1
1e21b 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1e21c 2a 20 49 66 20 50 34 3d 3d 30 20 74 68 65 6e 20 * If P4==0 then
1e21d 72 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 register P3 hold
1e21e 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 s a blob constru
1e21f 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f cted by MakeReco
1e220 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20 rd. If.** P4>0
1e221 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 then register P3
1e222 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 66 is the first of
1e223 20 50 34 20 72 65 67 69 73 74 65 72 73 20 74 68 P4 registers th
1e224 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 at form an unpac
1e225 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a ked.** record..*
1e226 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 * .** Cursor P1
1e227 69 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 is on an index b
1e228 74 72 65 65 2e 20 20 49 66 20 74 68 65 20 72 65 tree. If the re
1e229 63 6f 72 64 20 69 64 65 6e 74 69 66 69 65 64 20 cord identified
1e22a 62 79 20 50 33 20 61 6e 64 20 50 34 0a 2a 2a 20 by P3 and P4.**
1e22b 69 73 20 6e 6f 74 20 74 68 65 20 70 72 65 66 69 is not the prefi
1e22c 78 20 6f 66 20 61 6e 79 20 65 6e 74 72 79 20 69 x of any entry i
1e22d 6e 20 50 31 20 74 68 65 6e 20 61 20 6a 75 6d 70 n P1 then a jump
1e22e 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 2e 20 is made to P2.
1e22f 20 49 66 20 50 31 20 0a 2a 2a 20 64 6f 65 73 20 If P1 .** does
1e230 63 6f 6e 74 61 69 6e 20 61 6e 20 65 6e 74 72 79 contain an entry
1e231 20 77 68 6f 73 65 20 70 72 65 66 69 78 20 6d 61 whose prefix ma
1e232 74 63 68 65 73 20 74 68 65 20 50 33 2f 50 34 20 tches the P3/P4
1e233 72 65 63 6f 72 64 20 74 68 65 6e 20 63 6f 6e 74 record then cont
1e234 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 74 68 72 rol.** falls thr
1e235 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1e236 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 instruction and
1e237 20 50 31 20 69 73 20 6c 65 66 74 20 70 6f 69 6e P1 is left poin
1e238 74 69 6e 67 20 61 74 20 74 68 65 0a 2a 2a 20 6d ting at the.** m
1e239 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e 0a 2a atching entry..*
1e23a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 *.** See also: F
1e23b 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c ound, NotExists,
1e23c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 IsUnique.*/.cas
1e23d 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 e OP_NotFound:
1e23e 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e23f 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 3 */.case OP_Fou
1e240 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 nd: { /*
1e241 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 jump, in3 */.#if
1e242 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e243 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e244 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20 o u.bb */. int
1e245 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 alreadyExists;.
1e246 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e247 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e . int res;. Un
1e248 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 packedRecord *pI
1e249 64 78 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 dxKey;. Unpacke
1e24a 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 63 68 61 dRecord r;. cha
1e24b 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 r aTempRec[ROUND
1e24c 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 8(sizeof(Unpacke
1e24d 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 dRecord)) + size
1e24e 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a of(Mem)*3 + 7];.
1e24f 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e250 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e251 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 0a 23 69 into u.bb */..#i
1e252 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1e253 0a 20 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 . sqlite3_found
1e254 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
1e255 0a 0a 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 .. u.bb.already
1e256 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 61 73 Exists = 0;. as
1e257 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e258 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e259 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1e25a 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1e25b 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 75 =P4_INT32 );. u
1e25c 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .bb.pC = p->apCs
1e25d 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1e25e 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21 3d 30 sert( u.bb.pC!=0
1e25f 20 29 3b 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d );. pIn3 = &aM
1e260 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 em[pOp->p3];. i
1e261 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e 70 f( ALWAYS(u.bb.p
1e262 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 C->pCursor!=0) )
1e263 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 {.. assert( u
1e264 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d .bb.pC->isTable=
1e265 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f =0 );. if( pO
1e266 70 2d 3e 70 34 2e 69 3e 30 20 29 7b 0a 20 20 20 p->p4.i>0 ){.
1e267 20 20 20 75 2e 62 62 2e 72 2e 70 4b 65 79 49 6e u.bb.r.pKeyIn
1e268 66 6f 20 3d 20 75 2e 62 62 2e 70 43 2d 3e 70 4b fo = u.bb.pC->pK
1e269 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e eyInfo;. u.
1e26a 62 62 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 bb.r.nField = (u
1e26b 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 16)pOp->p4.i;.
1e26c 20 20 20 20 75 2e 62 62 2e 72 2e 61 4d 65 6d 20 u.bb.r.aMem
1e26d 3d 20 70 49 6e 33 3b 0a 20 20 20 20 20 20 75 2e = pIn3;. u.
1e26e 62 62 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 bb.r.flags = UNP
1e26f 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
1e270 43 48 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70 CH;. u.bb.p
1e271 49 64 78 4b 65 79 20 3d 20 26 75 2e 62 62 2e 72 IdxKey = &u.bb.r
1e272 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1e273 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d assert( pIn3-
1e274 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1e275 62 20 29 3b 0a 20 20 20 20 20 20 45 78 70 61 6e b );. Expan
1e276 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 dBlob(pIn3);.
1e277 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20 u.bb.pIdxKey
1e278 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
1e279 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62 62 2e 70 ordUnpack(u.bb.p
1e27a 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 70 49 6e C->pKeyInfo, pIn
1e27b 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 3->n, pIn3->z,.
1e27c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e27d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e27e 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54 65 6d u.bb.aTem
1e27f 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75 2e 62 pRec, sizeof(u.b
1e280 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20 b.aTempRec));.
1e281 20 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64 if( u.bb.pId
1e282 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 xKey==0 ){.
1e283 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1e284 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e }. u.
1e285 62 62 2e 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67 bb.pIdxKey->flag
1e286 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 s |= UNPACKED_PR
1e287 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 EFIX_MATCH;.
1e288 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1e289 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1e28a 61 63 6b 65 64 28 75 2e 62 62 2e 70 43 2d 3e 70 acked(u.bb.pC->p
1e28b 43 75 72 73 6f 72 2c 20 75 2e 62 62 2e 70 49 64 Cursor, u.bb.pId
1e28c 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 75 2e 62 xKey, 0, 0, &u.b
1e28d 62 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 b.res);. if(
1e28e 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30 20 29 7b 0a pOp->p4.i==0 ){.
1e28f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1e290 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 eDeleteUnpackedR
1e291 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 64 78 4b ecord(u.bb.pIdxK
1e292 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ey);. }. i
1e293 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1e294 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ){. break;
1e295 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 62 2e . }. u.bb.
1e296 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 alreadyExists =
1e297 28 75 2e 62 62 2e 72 65 73 3d 3d 30 29 3b 0a 20 (u.bb.res==0);.
1e298 20 20 20 75 2e 62 62 2e 70 43 2d 3e 64 65 66 65 u.bb.pC->defe
1e299 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1e29a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 63 61 63 u.bb.pC->cac
1e29b 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1e29c 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 _STALE;. }. if
1e29d 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1e29e 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69 P_Found ){. i
1e29f 66 28 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 f( u.bb.alreadyE
1e2a0 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 xists ) pc = pOp
1e2a1 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1e2a2 65 7b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 62 e{. if( !u.bb
1e2a3 2e 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 .alreadyExists )
1e2a4 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1e2a5 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1e2a6 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 }../* Opcode: Is
1e2a7 55 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20 Unique P1 P2 P3
1e2a8 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f P4 *.**.** Curso
1e2a9 72 20 50 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20 r P1 is open on
1e2aa 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 an index b-tree
1e2ab 2d 20 74 68 61 74 20 69 73 20 74 6f 20 73 61 79 - that is to say
1e2ac 2c 20 61 20 62 74 72 65 65 20 77 68 69 63 68 0a , a btree which.
1e2ad 2a 2a 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 77 ** no data and w
1e2ae 68 65 72 65 20 74 68 65 20 6b 65 79 20 61 72 65 here the key are
1e2af 20 72 65 63 6f 72 64 73 20 67 65 6e 65 72 61 74 records generat
1e2b0 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 ed by OP_MakeRec
1e2b1 6f 72 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 ord with.** the
1e2b2 6c 69 73 74 20 66 69 65 6c 64 20 62 65 69 6e 67 list field being
1e2b3 20 74 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57 the integer ROW
1e2b4 49 44 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 ID of the entry
1e2b5 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 0a 2a that the index.*
1e2b6 2a 20 65 6e 74 72 79 20 72 65 66 65 72 73 20 74 * entry refers t
1e2b7 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 o..**.** The P3
1e2b8 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e register contain
1e2b9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63 s an integer rec
1e2ba 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c ord number. Call
1e2bb 20 74 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a this record .**
1e2bc 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73 number R. Regis
1e2bd 74 65 72 20 50 34 20 69 73 20 74 68 65 20 66 69 ter P4 is the fi
1e2be 72 73 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20 rst in a set of
1e2bf 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67 N contiguous reg
1e2c0 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d isters.** that m
1e2c1 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b ake up an unpack
1e2c2 65 64 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61 ed index key tha
1e2c3 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 t can be used wi
1e2c4 74 68 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a th cursor P1..**
1e2c5 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20 The value of N
1e2c6 63 61 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20 can be inferred
1e2c7 66 72 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e from the cursor.
1e2c8 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 N includes the
1e2c9 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61 rowid.** value a
1e2ca 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 ppended to the e
1e2cb 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1e2cc 72 65 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77 record. This row
1e2cd 69 64 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 id value may.**
1e2ce 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 or may not be th
1e2cf 65 20 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a e same as R..**.
1e2d0 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 ** If any of the
1e2d1 20 4e 20 72 65 67 69 73 74 65 72 73 20 62 65 67 N registers beg
1e2d2 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69 inning with regi
1e2d3 73 74 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73 ster P4 contains
1e2d4 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 a NULL.** value
1e2d5 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 , jump immediate
1e2d6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ly to P2..**.**
1e2d7 4f 74 68 65 72 77 69 73 65 2c 20 74 68 69 73 20 Otherwise, this
1e2d8 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 instruction chec
1e2d9 6b 73 20 69 66 20 63 75 72 73 6f 72 20 50 31 20 ks if cursor P1
1e2da 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 contains an entr
1e2db 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66 y.** where the f
1e2dc 69 72 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64 irst (N-1) field
1e2dd 73 20 6d 61 74 63 68 20 62 75 74 20 74 68 65 20 s match but the
1e2de 72 6f 77 69 64 20 76 61 6c 75 65 20 61 74 20 74 rowid value at t
1e2df 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 he end.** of the
1e2e0 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 index entry is
1e2e1 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 72 65 20 not R. If there
1e2e2 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 is no such entry
1e2e3 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a , control jumps.
1e2e4 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f ** to instructio
1e2e5 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c n P2. Otherwise,
1e2e6 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 the rowid of th
1e2e7 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e e conflicting in
1e2e8 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 dex.** entry is
1e2e9 63 6f 70 69 65 64 20 74 6f 20 72 65 67 69 73 74 copied to regist
1e2ea 65 72 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f er P3 and contro
1e2eb 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 l falls through
1e2ec 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 to the next.** i
1e2ed 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1e2ee 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 * See also: NotF
1e2ef 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c ound, NotExists,
1e2f0 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f Found.*/.case O
1e2f1 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 P_IsUnique: {
1e2f2 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e2f3 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1e2f4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e2f5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a oved into u.bc *
1e2f6 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20 20 56 64 /. u16 ii;. Vd
1e2f7 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1e2f8 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1e2f9 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a ;. u16 nField;.
1e2fa 20 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 55 6e Mem *aMx;. Un
1e2fb 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 packedRecord r;
1e2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e2fd 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78 /* B-Tree index
1e2fe 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 search key */.
1e2ff 20 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 i64 R;
1e300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e301 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f /* Rowid sto
1e302 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1e303 50 33 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 P3 */.#endif /*
1e304 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e305 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 moved into u.bc
1e306 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d */.. pIn3 = &aM
1e307 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 em[pOp->p3];. u
1e308 2e 62 63 2e 61 4d 78 20 3d 20 26 61 4d 65 6d 5b .bc.aMx = &aMem[
1e309 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a pOp->p4.i];. /*
1e30a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 Assert that the
1e30b 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d values of param
1e30c 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20 eters P1 and P4
1e30d 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f are in range. */
1e30e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e30f 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
1e310 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1e311 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70 p->p4.i>0 && pOp
1e312 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 ->p4.i<=p->nMem
1e313 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1e314 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e315 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e316 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 .. /* Find the
1e317 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f index cursor. */
1e318 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d . u.bc.pCx = p-
1e319 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e31a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e . assert( u.bc.
1e31b 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pCx->deferredMov
1e31c 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63 eto==0 );. u.bc
1e31d 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 .pCx->seekResult
1e31e 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78 = 0;. u.bc.pCx
1e31f 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e320 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75 CACHE_STALE;. u
1e321 2e 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63 .bc.pCrsr = u.bc
1e322 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a .pCx->pCursor;..
1e323 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 /* If any of t
1e324 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 he values are NU
1e325 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d LL, take the jum
1e326 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69 p. */. u.bc.nFi
1e327 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e eld = u.bc.pCx->
1e328 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1e329 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d ;. for(u.bc.ii=
1e32a 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e 0; u.bc.ii<u.bc.
1e32b 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b nField; u.bc.ii+
1e32c 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63 +){. if( u.bc
1e32d 2e 61 4d 78 5b 75 2e 62 63 2e 69 69 5d 2e 66 6c .aMx[u.bc.ii].fl
1e32e 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1e32f 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1e330 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 ->p2 - 1;.
1e331 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b 0a u.bc.pCrsr = 0;.
1e332 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e333 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
1e334 20 28 75 2e 62 63 2e 61 4d 78 5b 75 2e 62 63 2e (u.bc.aMx[u.bc.
1e335 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 20 nField].flags &
1e336 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a MEM_Null)==0 );.
1e337 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43 72 73 . if( u.bc.pCrs
1e338 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 50 r!=0 ){. /* P
1e339 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e 64 65 opulate the inde
1e33a 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 2a 2f x search key. */
1e33b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b 65 79 . u.bc.r.pKey
1e33c 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43 78 2d Info = u.bc.pCx-
1e33d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 >pKeyInfo;. u
1e33e 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 75 .bc.r.nField = u
1e33f 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a .bc.nField + 1;.
1e340 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 67 73 u.bc.r.flags
1e341 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 = UNPACKED_PREF
1e342 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 75 IX_SEARCH;. u
1e343 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75 2e 62 .bc.r.aMem = u.b
1e344 63 2e 61 4d 78 3b 0a 0a 20 20 20 20 2f 2a 20 45 c.aMx;.. /* E
1e345 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 xtract the value
1e346 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 6f 6d 20 of u.bc.R from
1e347 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a 2f 0a register P3. */.
1e348 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1e349 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e emIntegerify(pIn
1e34a 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e 52 20 3d 3);. u.bc.R =
1e34b 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20 pIn3->u.i;..
1e34c 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 42 /* Search the B
1e34d 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 49 66 20 -Tree index. If
1e34e 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 no conflicting r
1e34f 65 63 6f 72 64 20 69 73 20 66 6f 75 6e 64 2c 20 ecord is found,
1e350 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 6f 20 50 jump. ** to P
1e351 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 63 6f 2. Otherwise, co
1e352 70 79 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 py the rowid of
1e353 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 the conflicting
1e354 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 20 2a 2a record to. **
1e355 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64 register P3 and
1e356 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f fall through to
1e357 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 the next instru
1e358 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 72 ction. */. r
1e359 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e35a 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 MovetoUnpacked(u
1e35b 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 2e 62 63 .bc.pCrsr, &u.bc
1e35c 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 63 2e .r, 0, 0, &u.bc.
1e35d 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29 pCx->seekResult)
1e35e 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 62 63 2e ;. if( (u.bc.
1e35f 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b r.flags & UNPACK
1e360 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 ED_PREFIX_SEARCH
1e361 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 6f 77 69 ) || u.bc.r.rowi
1e362 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a 20 20 20 d==u.bc.R ){.
1e363 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1e364 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a - 1;. }else{.
1e365 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20 pIn3->u.i
1e366 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3b 0a = u.bc.r.rowid;.
1e367 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1e368 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e369 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20 50 32 NotExists P1 P2
1e36a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 P3 * *.**.** Us
1e36b 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1e36c 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1e36d 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e 20 20 a integer key.
1e36e 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a 2a 20 If a record .**
1e36f 77 69 74 68 20 74 68 61 74 20 6b 65 79 20 64 6f with that key do
1e370 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 es not exist in
1e371 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 68 65 table of P1, the
1e372 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a n jump to P2. .*
1e373 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 * If the record
1e374 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 65 6e does exist, then
1e375 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 fall thru. The
1e376 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 cursor is left
1e377 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 .** pointing to
1e378 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 the record if it
1e379 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 exists..**.** T
1e37a 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 he difference be
1e37b 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65 72 61 tween this opera
1e37c 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e tion and NotFoun
1e37d 64 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a d is that this.*
1e37e 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73 73 75 * operation assu
1e37f 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73 20 61 mes the key is a
1e380 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 74 68 n integer and th
1e381 61 74 20 50 31 20 69 73 20 61 20 74 61 62 6c 65 at P1 is a table
1e382 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46 whereas.** NotF
1e383 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b 65 79 ound assumes key
1e384 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 is a blob const
1e385 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65 ructed from Make
1e386 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31 Record and.** P1
1e387 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a is an index..**
1e388 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1e389 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 und, NotFound, I
1e38a 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 sUnique.*/.case
1e38b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20 OP_NotExists: {
1e38c 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1e38d 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a in3 */.#if 0 /*
1e38e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e38f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 64 moved into u.bd
1e390 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e391 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1e392 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 *pCrsr;. int r
1e393 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 79 3b 0a es;. u64 iKey;.
1e394 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e395 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e396 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 0a 20 20 into u.bd */..
1e397 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn3 = &aMem[pOp
1e398 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p3];. assert(
1e399 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d pIn3->flags & M
1e39a 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73 73 65 EM_Int );. asse
1e39b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e39c 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e39d 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64 2e 70 rsor );. u.bd.p
1e39e 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e39f 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e3a0 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bd.pC!=0 );.
1e3a1 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 assert( u.bd.pC
1e3a2 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 61 ->isTable );. a
1e3a3 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e ssert( u.bd.pC->
1e3a4 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d pseudoTableReg==
1e3a5 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43 72 73 0 );. u.bd.pCrs
1e3a6 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70 43 75 r = u.bd.pC->pCu
1e3a7 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 64 rsor;. if( u.bd
1e3a8 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 .pCrsr!=0 ){.
1e3a9 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b 0a 20 u.bd.res = 0;.
1e3aa 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d 20 70 u.bd.iKey = p
1e3ab 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 72 63 In3->u.i;. rc
1e3ac 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1e3ad 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e ovetoUnpacked(u.
1e3ae 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 bd.pCrsr, 0, u.b
1e3af 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e 62 64 d.iKey, 0, &u.bd
1e3b0 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 64 2e .res);. u.bd.
1e3b1 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 pC->lastRowid =
1e3b2 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 pIn3->u.i;. u
1e3b3 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .bd.pC->rowidIsV
1e3b4 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65 73 3d alid = u.bd.res=
1e3b5 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 =0 ?1:0;. u.b
1e3b6 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 d.pC->nullRow =
1e3b7 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 0;. u.bd.pC->
1e3b8 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e3b9 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75 CHE_STALE;. u
1e3ba 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bd.pC->deferred
1e3bb 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
1e3bc 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d 30 20 if( u.bd.res!=0
1e3bd 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1e3be 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1e3bf 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 assert( u.bd.pC
1e3c0 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d ->rowidIsValid==
1e3c1 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 0 );. }. u
1e3c2 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 .bd.pC->seekResu
1e3c3 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b 0a 20 lt = u.bd.res;.
1e3c4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1e3c5 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e his happens when
1e3c6 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f an attempt to o
1e3c7 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f pen a read curso
1e3c8 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 r on the. **
1e3c9 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1e3ca 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ble returns SQLI
1e3cb 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f TE_EMPTY.. */
1e3cc 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1e3cd 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 2 - 1;. asser
1e3ce 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 t( u.bd.pC->rowi
1e3cf 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 dIsValid==0 );.
1e3d0 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b u.bd.pC->seek
1e3d1 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a Result = 0;. }.
1e3d2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e3d3 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 pcode: Sequence
1e3d4 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e3d5 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 * Find the next
1e3d6 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e available sequen
1e3d7 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75 ce number for cu
1e3d8 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74 rsor P1..** Writ
1e3d9 65 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e e the sequence n
1e3da 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73 umber into regis
1e3db 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73 ter P2..** The s
1e3dc 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f equence number o
1e3dd 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 n the cursor is
1e3de 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65 incremented afte
1e3df 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75 r this.** instru
1e3e0 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65 ction. .*/.case
1e3e1 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20 OP_Sequence: {
1e3e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 /* out
1e3e3 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1e3e4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e3e5 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e3e6 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e3e7 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1e3e8 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29 3b 0a [pOp->p1]!=0 );.
1e3e9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d pOut->u.i = p-
1e3ea 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d >apCsr[pOp->p1]-
1e3eb 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20 20 62 >seqCount++;. b
1e3ec 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 reak;.}.../* Opc
1e3ed 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 20 50 31 ode: NewRowid P1
1e3ee 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1e3ef 20 47 65 74 20 61 20 6e 65 77 20 69 6e 74 65 67 Get a new integ
1e3f0 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 er record number
1e3f1 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 64 22 29 (a.k.a "rowid")
1e3f2 20 75 73 65 64 20 61 73 20 74 68 65 20 6b 65 79 used as the key
1e3f3 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 to a table..**
1e3f4 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 The record numbe
1e3f5 72 20 69 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 r is not previou
1e3f6 73 6c 79 20 75 73 65 64 20 61 73 20 61 20 6b 65 sly used as a ke
1e3f7 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
1e3f8 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20 e.** table that
1e3f9 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 cursor P1 points
1e3fa 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 20 72 65 to. The new re
1e3fb 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 77 cord number is w
1e3fc 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 74 74 65 ritten.** writte
1e3fd 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 n to register P2
1e3fe 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 3e 30 20 ..**.** If P3>0
1e3ff 74 68 65 6e 20 50 33 20 69 73 20 61 20 72 65 67 then P3 is a reg
1e400 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 6f 6f ister in the roo
1e401 74 20 66 72 61 6d 65 20 6f 66 20 74 68 69 73 20 t frame of this
1e402 56 44 42 45 20 74 68 61 74 20 68 6f 6c 64 73 20 VDBE that holds
1e403 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 .** the largest
1e404 70 72 65 76 69 6f 75 73 6c 79 20 67 65 6e 65 72 previously gener
1e405 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 ated record numb
1e406 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 63 6f 72 er. No new recor
1e407 64 20 6e 75 6d 62 65 72 73 20 61 72 65 0a 2a 2a d numbers are.**
1e408 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c allowed to be l
1e409 65 73 73 20 74 68 61 6e 20 74 68 69 73 20 76 61 ess than this va
1e40a 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 73 20 76 lue. When this v
1e40b 61 6c 75 65 20 72 65 61 63 68 65 73 20 69 74 73 alue reaches its
1e40c 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a 20 61 20 maximum, .** a
1e40d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 6f SQLITE_FULL erro
1e40e 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 2e 20 r is generated.
1e40f 54 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20 The P3 register
1e410 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20 is updated with
1e411 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 72 61 74 the '.** generat
1e412 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 ed record number
1e413 2e 20 54 68 69 73 20 50 33 20 6d 65 63 68 61 6e . This P3 mechan
1e414 69 73 6d 20 69 73 20 75 73 65 64 20 74 6f 20 68 ism is used to h
1e415 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 elp implement th
1e416 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45 e.** AUTOINCREME
1e417 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 63 NT feature..*/.c
1e418 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3a ase OP_NewRowid:
1e419 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1e41a 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1e41b 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e41c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e41d 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a ed into u.be */.
1e41e 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 i64 v;
1e41f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1e420 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 56 new rowid */. V
1e421 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 dbeCursor *pC;
1e422 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
1e423 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 of table to get
1e424 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f the new rowid */
1e425 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 . int res;
1e426 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1e427 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 ult of an sqlite
1e428 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 3BtreeLast() */.
1e429 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 int cnt;
1e42a 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e /* Coun
1e42b 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 ter to limit the
1e42c 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 72 63 number of searc
1e42d 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d hes */. Mem *pM
1e42e 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1e42f 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
1e430 69 6e 67 20 6c 61 72 67 65 73 74 20 72 6f 77 69 ing largest rowi
1e431 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 4d d for AUTOINCREM
1e432 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 ENT */. VdbeFra
1e433 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
1e434 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66 /* Root frame of
1e435 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 69 66 20 VDBE */.#endif
1e436 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e437 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e438 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65 2e 76 20 be */.. u.be.v
1e439 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72 65 73 20 = 0;. u.be.res
1e43a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
1e43b 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e43c 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e43d 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20 3d 20 70 );. u.be.pC = p
1e43e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e43f 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 ;. assert( u.be
1e440 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 .pC!=0 );. if(
1e441 4e 45 56 45 52 28 75 2e 62 65 2e 70 43 2d 3e 70 NEVER(u.be.pC->p
1e442 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b 0a 20 20 Cursor==0) ){.
1e443 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 69 6e /* The zero in
1e444 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 62 6f itialization abo
1e445 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 20 69 ve is all that i
1e446 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 7d 65 s needed */. }e
1e447 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 lse{. /* The
1e448 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 72 65 next rowid or re
1e449 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 69 66 cord number (dif
1e44a 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 6f 72 ferent terms for
1e44b 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 2a 2a the same. **
1e44c 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 61 69 thing) is obtai
1e44d 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 74 65 ned in a two-ste
1e44e 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 p algorithm..
1e44f 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 73 74 **. ** First
1e450 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f 20 66 we attempt to f
1e451 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 ind the largest
1e452 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 61 existing rowid a
1e453 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 20 2a nd add one. *
1e454 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 74 20 * to that. But
1e455 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 if the largest e
1e456 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 69 73 xisting rowid is
1e457 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d 61 78 already the max
1e458 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f 73 69 imum. ** posi
1e459 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 77 65 tive integer, we
1e45a 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 74 68 have to fall th
1e45b 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 rough to the sec
1e45c 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f 62 61 ond. ** proba
1e45d 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 69 74 bilistic algorit
1e45e 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a hm. **. **
1e45f 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c 67 6f The second algo
1e460 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 6c 65 rithm is to sele
1e461 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 72 61 ct a rowid at ra
1e462 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 66 0a ndom and see if.
1e463 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 61 64 ** it alread
1e464 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 y exists in the
1e465 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 64 6f table. If it do
1e466 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 77 65 es not exist, we
1e467 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 75 63 have. ** suc
1e468 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 65 20 ceeded. If the
1e469 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 6f 65 random rowid doe
1e46a 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 6c 65 s exist, we sele
1e46b 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 20 20 ct a new one.
1e46c 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 61 69 ** and try agai
1e46d 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 74 69 6d n, up to 100 tim
1e46e 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 es.. */. a
1e46f 73 73 65 72 74 28 20 75 2e 62 65 2e 70 43 2d 3e ssert( u.be.pC->
1e470 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75 isTable );. u
1e471 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 0a 23 69 .be.cnt = 0;..#i
1e472 66 64 65 66 20 53 51 4c 49 54 45 5f 33 32 42 49 fdef SQLITE_32BI
1e473 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 65 66 69 T_ROWID.# defi
1e474 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 30 78 37 ne MAX_ROWID 0x7
1e475 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0a 20 20 fffffff.#else.
1e476 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 70 69 6c /* Some compil
1e477 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f ers complain abo
1e478 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 6f 66 20 ut constants of
1e479 74 68 65 20 66 6f 72 6d 20 30 78 37 66 66 66 66 the form 0x7ffff
1e47a 66 66 66 66 66 66 66 66 66 66 66 2e 0a 20 20 20 fffffffffff..
1e47b 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f 6d 70 6c ** Others compl
1e47c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 66 66 66 ain about 0x7fff
1e47d 66 66 66 66 66 66 66 66 66 66 66 66 66 4c 4c 2e fffffffffffffLL.
1e47e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
1e47f 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 20 20 20 macro seems.
1e480 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 ** to provide th
1e481 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 6c 65 e constant while
1e482 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 6f 6d 70 making all comp
1e483 69 6c 65 72 73 20 68 61 70 70 79 2e 0a 20 20 20 ilers happy..
1e484 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 4d */.# define M
1e485 41 58 5f 52 4f 57 49 44 20 20 28 69 36 34 29 28 AX_ROWID (i64)(
1e486 20 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66 (((u64)0x7fffff
1e487 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 ff)<<32) | (u64)
1e488 30 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 0xffffffff ).#en
1e489 64 69 66 0a 0a 20 20 20 20 69 66 28 20 21 75 2e dif.. if( !u.
1e48a 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d be.pC->useRandom
1e48b 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 75 Rowid ){. u
1e48c 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74 65 33 42 .be.v = sqlite3B
1e48d 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 treeGetCachedRow
1e48e 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 id(u.be.pC->pCur
1e48f 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 sor);. if(
1e490 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a 20 20 20 u.be.v==0 ){.
1e491 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e492 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 65 2e 3BtreeLast(u.be.
1e493 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1e494 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 be.res);.
1e495 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1e496 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1e497 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1e498 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1e499 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 }. if( u
1e49a 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 .be.res ){.
1e49b 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 31 3b u.be.v = 1;
1e49c 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1e49d 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1e49e 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 ( sqlite3BtreeCu
1e49f 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 65 rsorIsValid(u.be
1e4a0 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 20 29 3b .pC->pCursor) );
1e4a1 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1e4a2 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 sqlite3BtreeKeyS
1e4a3 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 ize(u.be.pC->pCu
1e4a4 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76 29 3b 0a rsor, &u.be.v);.
1e4a5 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1e4a6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e4a7 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 66 ); /* Cannot f
1e4a8 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67 20 42 74 ail following Bt
1e4a9 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 20 reeLast() */.
1e4aa 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e if( u.be.
1e4ab 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 29 7b 0a v==MAX_ROWID ){.
1e4ac 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 u.be
1e4ad 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f .pC->useRandomRo
1e4ae 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 wid = 1;.
1e4af 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e4b0 20 20 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a u.be.v++;.
1e4b1 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1e4b2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 }. }..#
1e4b3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1e4b4 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
1e4b5 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e . if( pOp->
1e4b6 70 33 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a p3 ){. /*
1e4b7 20 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 Assert that P3
1e4b8 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 is a valid memor
1e4b9 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 y cell. */.
1e4ba 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e4bb 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 p3>0 );.
1e4bc 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b if( p->pFrame ){
1e4bd 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 75 . for(u
1e4be 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46 .be.pFrame=p->pF
1e4bf 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 72 61 6d rame; u.be.pFram
1e4c0 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 62 65 e->pParent; u.be
1e4c1 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e 70 46 72 .pFrame=u.be.pFr
1e4c2 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 ame->pParent);.
1e4c3 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 /* Asse
1e4c4 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20 rt that P3 is a
1e4c5 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c valid memory cel
1e4c6 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 l. */.
1e4c7 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c assert( pOp->p3<
1e4c8 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 6e 4d =u.be.pFrame->nM
1e4c9 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 em );.
1e4ca 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 75 2e 62 u.be.pMem = &u.b
1e4cb 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 e.pFrame->aMem[p
1e4cc 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 Op->p3];.
1e4cd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1e4ce 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
1e4cf 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d P3 is a valid m
1e4d0 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 emory cell. */.
1e4d1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1e4d2 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1e4d3 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 75 m );. u
1e4d4 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d .be.pMem = &aMem
1e4d5 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 [pOp->p3];.
1e4d6 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 52 45 }.. RE
1e4d7 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1e4d8 2d 3e 70 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29 ->p3, u.be.pMem)
1e4d9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1e4da 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
1e4db 66 79 28 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 fy(u.be.pMem);.
1e4dc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 assert( (
1e4dd 75 2e 62 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 u.be.pMem->flags
1e4de 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 & MEM_Int)!=0 )
1e4df 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f ; /* mem(P3) ho
1e4e0 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a lds an integer *
1e4e1 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e /. if( u.
1e4e2 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 be.pMem->u.i==MA
1e4e3 58 5f 52 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e X_ROWID || u.be.
1e4e4 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 pC->useRandomRow
1e4e5 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 id ){.
1e4e6 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c rc = SQLITE_FULL
1e4e7 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
1e4e8 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1e4e9 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ror;. }.
1e4ea 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e if( u.be.
1e4eb 76 3c 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 v<u.be.pMem->u.i
1e4ec 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +1 ){.
1e4ed 75 2e 62 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d u.be.v = u.be.pM
1e4ee 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 em->u.i + 1;.
1e4ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75 }. u
1e4f0 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 .be.pMem->u.i =
1e4f1 75 2e 62 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a u.be.v;. }.
1e4f2 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 73 71 #endif.. sq
1e4f3 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1e4f4 68 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 hedRowid(u.be.pC
1e4f5 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e ->pCursor, u.be.
1e4f6 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e v<MAX_ROWID ? u.
1e4f7 62 65 2e 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20 be.v+1 : 0);.
1e4f8 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 65 2e }. if( u.be.
1e4f9 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 pC->useRandomRow
1e4fa 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 id ){. asse
1e4fb 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 rt( pOp->p3==0 )
1e4fc 3b 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 ; /* We cannot
1e4fd 62 65 20 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 be in random row
1e4fe 69 64 20 6d 6f 64 65 20 69 66 20 74 68 69 73 20 id mode if this
1e4ff 69 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 is.
1e500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e501 2a 2a 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d ** an AUTOINCREM
1e502 45 4e 54 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 ENT table. */.
1e503 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d u.be.v = db-
1e504 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 >lastRowid;.
1e505 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a u.be.cnt = 0;.
1e506 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 do{.
1e507 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d if( u.be.cnt==
1e508 30 20 26 26 20 28 75 2e 62 65 2e 76 26 30 78 66 0 && (u.be.v&0xf
1e509 66 66 66 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29 fffff)==u.be.v )
1e50a 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 {. u.be
1e50b 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 .v++;. }e
1e50c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
1e50d 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
1e50e 73 28 73 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29 s(sizeof(u.be.v)
1e50f 2c 20 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 , &u.be.v);.
1e510 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63 if( u.be.c
1e511 6e 74 3c 35 20 29 20 75 2e 62 65 2e 76 20 26 3d nt<5 ) u.be.v &=
1e512 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 0xffffff;.
1e513 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 }. rc
1e514 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1e515 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 vetoUnpacked(u.b
1e516 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 e.pC->pCursor, 0
1e517 2c 20 28 75 36 34 29 75 2e 62 65 2e 76 2c 20 30 , (u64)u.be.v, 0
1e518 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 , &u.be.res);.
1e519 20 20 20 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b u.be.cnt++
1e51a 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 ;. }while(
1e51b 75 2e 62 65 2e 63 6e 74 3c 31 30 30 20 26 26 20 u.be.cnt<100 &&
1e51c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1e51d 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a u.be.res==0 );.
1e51e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1e51f 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e LITE_OK && u.be.
1e520 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1e521 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
1e522 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f LL;. goto
1e523 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1e524 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
1e525 20 7d 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e }. u.be.pC->
1e526 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e527 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64 ;. u.be.pC->d
1e528 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e529 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 0;. u.be.pC->
1e52a 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e52b 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 CHE_STALE;. }.
1e52c 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 pOut->u.i = u.b
1e52d 65 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a e.v;. break;.}.
1e52e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 ./* Opcode: Inse
1e52f 72 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 rt P1 P2 P3 P4 P
1e530 35 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 5.**.** Write an
1e531 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 entry into the
1e532 74 61 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 table of cursor
1e533 50 31 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 P1. A new entry
1e534 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 is.** created i
1e535 66 20 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 f it doesn't alr
1e536 65 61 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 eady exist or th
1e537 65 20 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 e data for an ex
1e538 69 73 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 isting.** entry
1e539 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 is overwritten.
1e53a 20 54 68 65 20 64 61 74 61 20 69 73 20 74 68 65 The data is the
1e53b 20 76 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 value MEM_Blob
1e53c 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1e53d 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e er.** number P2.
1e53e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 The key is stor
1e53f 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1e540 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 3. The key must.
1e541 2a 2a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e ** be a MEM_Int.
1e542 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 .**.** If the OP
1e543 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 FLAG_NCHANGE fla
1e544 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 g of P5 is set,
1e545 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 then the row cha
1e546 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 nge count is.**
1e547 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 incremented (oth
1e548 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 erwise not). If
1e549 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 the OPFLAG_LAST
1e54a 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 ROWID flag of P5
1e54b 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e is set,.** then
1e54c 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 rowid is stored
1e54d 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 for subsequent
1e54e 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a return by the.**
1e54f 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e sqlite3_last_in
1e550 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e sert_rowid() fun
1e551 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 ction (otherwise
1e552 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 it is unmodifie
1e553 64 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 d)..**.** If the
1e554 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 OPFLAG_USESEEKR
1e555 45 53 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 ESULT flag of P5
1e556 20 69 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 is set and if t
1e557 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 he result of.**
1e558 74 68 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 the last seek op
1e559 65 72 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 eration (OP_NotE
1e55a 78 69 73 74 73 29 20 77 61 73 20 61 20 73 75 63 xists) was a suc
1e55b 63 65 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a cess, then this.
1e55c 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c ** operation wil
1e55d 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f l not attempt to
1e55e 20 66 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 find the approp
1e55f 72 69 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 riate row before
1e560 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e doing.** the in
1e561 73 65 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e sert but will in
1e562 73 74 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 stead overwrite
1e563 74 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 the row that the
1e564 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 cursor is.** cu
1e565 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1e566 20 74 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 to. Presumably
1e567 2c 20 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e , the prior OP_N
1e568 6f 74 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a otExists opcode.
1e569 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 ** has already p
1e56a 6f 73 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 ositioned the cu
1e56b 72 73 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 rsor correctly.
1e56c 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 This is an opti
1e56d 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 mization.** that
1e56e 20 62 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 boosts performa
1e56f 6e 63 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 nce by avoiding
1e570 72 65 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e redundant seeks.
1e571 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 .**.** If the OP
1e572 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c FLAG_ISUPDATE fl
1e573 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
1e574 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 this opcode is p
1e575 61 72 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 art of an.** UPD
1e576 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 ATE operation.
1e577 4f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 Otherwise (if th
1e578 65 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 e flag is clear)
1e579 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 then this opcod
1e57a 65 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 e.** is part of
1e57b 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 an INSERT operat
1e57c 69 6f 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 ion. The differ
1e57d 65 6e 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 ence is only imp
1e57e 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 ortant to.** the
1e57f 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a update hook..**
1e580 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 .** Parameter P4
1e581 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 may point to a
1e582 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e string containin
1e583 67 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 g the table-name
1e584 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e , or.** may be N
1e585 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f ULL. If it is no
1e586 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 t NULL, then the
1e587 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a update-hook .**
1e588 20 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 (sqlite3.xUpdat
1e589 65 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e eCallback) is in
1e58a 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 voked following
1e58b 61 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 a successful ins
1e58c 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e ert..**.** (WARN
1e58d 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 ING/TODO: If P1
1e58e 69 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 is a pseudo-curs
1e58f 6f 72 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e or and P2 is dyn
1e590 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f amically.** allo
1e591 63 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 cated, then owne
1e592 72 73 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 rship of P2 is t
1e593 72 61 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 ransferred to th
1e594 65 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a e pseudo-cursor.
1e595 2a 2a 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 ** and register
1e596 50 32 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d P2 becomes ephem
1e597 65 72 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 eral. If the cu
1e598 72 73 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c rsor is changed,
1e599 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 the.** value of
1e59a 20 72 65 67 69 73 74 65 72 20 50 32 20 77 69 6c register P2 wil
1e59b 6c 20 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 l then change.
1e59c 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 Make sure this d
1e59d 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 oes not.** cause
1e59e 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a any problems.).
1e59f 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 **.** This instr
1e5a0 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b uction only work
1e5a1 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 s on tables. Th
1e5a2 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 e equivalent ins
1e5a3 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 truction.** for
1e5a4 69 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 indices is OP_Id
1e5a5 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 2f 2a 20 4f xInsert..*/./* O
1e5a6 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 49 6e 74 pcode: InsertInt
1e5a7 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1e5a8 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1e5a9 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 4f 50 exactly like OP
1e5aa 5f 49 6e 73 65 72 74 20 65 78 63 65 70 74 20 74 _Insert except t
1e5ab 68 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 74 hat the key is t
1e5ac 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 he.** integer va
1e5ad 6c 75 65 20 50 33 2c 20 6e 6f 74 20 74 68 65 20 lue P3, not the
1e5ae 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 value of the int
1e5af 65 67 65 72 20 73 74 6f 72 65 64 20 69 6e 20 72 eger stored in r
1e5b0 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 egister P3..*/.c
1e5b1 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0a ase OP_Insert: .
1e5b2 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e case OP_InsertIn
1e5b3 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c t: {.#if 0 /* l
1e5b4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e5b5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a oved into u.bf *
1e5b6 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20 /. Mem *pData;
1e5b7 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c /* MEM cel
1e5b8 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66 l holding data f
1e5b9 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1e5ba 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1e5bb 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 Mem *pKey;
1e5bc 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 /* MEM cell
1e5bd 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 holding key for
1e5be 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1e5bf 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 i64 iKey;
1e5c0 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 /* The intege
1e5c1 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 r ROWID or key f
1e5c2 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1e5c3 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1e5c4 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e5c5 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f ; /* Cursor to
1e5c6 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 table into whic
1e5c7 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 h insert is writ
1e5c8 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 ten */. int nZe
1e5c9 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ro; /* Nu
1e5ca 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74 mber of zero-byt
1e5cb 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a es to append */.
1e5cc 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 int seekResult
1e5cd 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 ; /* Result of
1e5ce 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 prior seek or 0
1e5cf 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 if no USESEEKRE
1e5d0 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63 SULT flag */. c
1e5d1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 onst char *zDb;
1e5d2 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d /* database nam
1e5d3 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 e - used by the
1e5d4 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 update hook */.
1e5d5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 const char *zTb
1e5d6 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 l; /* Table name
1e5d7 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 6f - used by the o
1e5d8 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 pdate hook */.
1e5d9 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 int op;
1e5da 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20 /* Opcode for
1e5db 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c update hook: SQL
1e5dc 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51 ITE_UPDATE or SQ
1e5dd 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23 LITE_INSERT */.#
1e5de 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e5df 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e5e0 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75 nto u.bf */.. u
1e5e1 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 61 4d 65 .bf.pData = &aMe
1e5e2 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 m[pOp->p2];. as
1e5e3 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e5e4 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e5e5 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 66 Cursor );. u.bf
1e5e6 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e5e7 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e5e8 74 28 20 75 2e 62 66 2e 70 43 21 3d 30 20 29 3b t( u.bf.pC!=0 );
1e5e9 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e . assert( u.bf.
1e5ea 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 pC->pCursor!=0 )
1e5eb 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 ;. assert( u.bf
1e5ec 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 .pC->pseudoTable
1e5ed 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Reg==0 );. asse
1e5ee 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54 rt( u.bf.pC->isT
1e5ef 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54 able );. REGIST
1e5f0 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1e5f1 2c 20 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a 0a , u.bf.pData);..
1e5f2 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1e5f3 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 29 7b 0a e==OP_Insert ){.
1e5f4 20 20 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20 u.bf.pKey =
1e5f5 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1e5f6 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 assert( u.bf
1e5f7 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d .pKey->flags & M
1e5f8 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 52 45 EM_Int );. RE
1e5f9 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1e5fa 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29 ->p3, u.bf.pKey)
1e5fb 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79 20 ;. u.bf.iKey
1e5fc 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 = u.bf.pKey->u.i
1e5fd 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1e5fe 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f ssert( pOp->opco
1e5ff 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 49 6e 74 de==OP_InsertInt
1e600 20 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 );. u.bf.iKe
1e601 79 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d y = pOp->p3;. }
1e602 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 .. if( pOp->p5
1e603 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 & OPFLAG_NCHANGE
1e604 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b ) p->nChange++;
1e605 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 . if( pOp->p5 &
1e606 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 OPFLAG_LASTROWI
1e607 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 D ) db->lastRowi
1e608 64 20 3d 20 75 2e 62 66 2e 69 4b 65 79 3b 0a 20 d = u.bf.iKey;.
1e609 20 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d if( u.bf.pData-
1e60a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1e60b 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 44 l ){. u.bf.pD
1e60c 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 ata->z = 0;.
1e60d 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 20 3d 20 u.bf.pData->n =
1e60e 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
1e60f 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 44 61 assert( u.bf.pDa
1e610 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d ta->flags & (MEM
1e611 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 _Blob|MEM_Str) )
1e612 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e 73 65 65 ;. }. u.bf.see
1e613 6b 52 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d kResult = ((pOp-
1e614 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 >p5 & OPFLAG_USE
1e615 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e SEEKRESULT) ? u.
1e616 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c bf.pC->seekResul
1e617 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 75 2e t : 0);. if( u.
1e618 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 bf.pData->flags
1e619 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
1e61a 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 75 u.bf.nZero = u
1e61b 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 .bf.pData->u.nZe
1e61c 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ro;. }else{.
1e61d 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 30 3b u.bf.nZero = 0;
1e61e 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
1e61f 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 reeSetCachedRowi
1e620 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 d(u.bf.pC->pCurs
1e621 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 or, 0);. rc = s
1e622 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
1e623 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 t(u.bf.pC->pCurs
1e624 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69 4b 65 79 or, 0, u.bf.iKey
1e625 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1e626 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 66 u.bf
1e627 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e 62 66 2e .pData->z, u.bf.
1e628 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 66 2e 6e pData->n, u.bf.n
1e629 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 Zero,.
1e62a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e62b 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1e62c 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66 2e 73 65 _APPEND, u.bf.se
1e62d 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b 0a 20 20 ekResult. );.
1e62e 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.bf.pC->rowidIs
1e62f 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 Valid = 0;. u.b
1e630 66 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f f.pC->deferredMo
1e631 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 66 veto = 0;. u.bf
1e632 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 .pC->cacheStatus
1e633 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1e634 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
1e635 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 update-hook if
1e636 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
1e637 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1e638 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 && db->xUpdateC
1e639 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e allback && pOp->
1e63a 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75 2e 62 66 p4.z ){. u.bf
1e63b 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75 .zDb = db->aDb[u
1e63c 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 .bf.pC->iDb].zNa
1e63d 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e 7a 54 62 me;. u.bf.zTb
1e63e 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 l = pOp->p4.z;.
1e63f 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20 28 28 70 u.bf.op = ((p
1e640 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e641 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49 ISUPDATE) ? SQLI
1e642 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49 TE_UPDATE : SQLI
1e643 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20 TE_INSERT);.
1e644 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d assert( u.bf.pC-
1e645 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1e646 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
1e647 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 ack(db->pUpdateA
1e648 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 75 2e 62 rg, u.bf.op, u.b
1e649 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a 54 62 6c f.zDb, u.bf.zTbl
1e64a 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b 0a 20 20 , u.bf.iKey);.
1e64b 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 assert( u.bf.p
1e64c 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d C->iDb>=0 );. }
1e64d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e64e 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 Opcode: Delete P
1e64f 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 1 P2 * P4 *.**.*
1e650 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 * Delete the rec
1e651 6f 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65 ord at which the
1e652 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 P1 cursor is cu
1e653 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1e654 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 ..**.** The curs
1e655 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 or will be left
1e656 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 pointing at eith
1e657 65 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 er the next or t
1e658 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 he previous.** r
1e659 65 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 ecord in the tab
1e65a 6c 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 le. If it is lef
1e65b 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 t pointing at th
1e65c 65 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 e next record, t
1e65d 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 hen.** the next
1e65e 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e Next instruction
1e65f 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 will be a no-op
1e660 2e 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f . Hence it is O
1e661 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 K to delete.** a
1e662 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 record from wit
1e663 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 hin an Next loop
1e664 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f ..**.** If the O
1e665 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c PFLAG_NCHANGE fl
1e666 61 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c ag of P2 is set,
1e667 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 then the row ch
1e668 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a ange count is.**
1e669 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 incremented (ot
1e66a 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a herwise not)..**
1e66b 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 .** P1 must not
1e66c 62 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e be pseudo-table.
1e66d 20 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 It has to be a
1e66e 20 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 real table with
1e66f 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 .** multiple row
1e670 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 s..**.** If P4 i
1e671 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
1e672 20 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 it is the name
1e673 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 of the table tha
1e674 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 t P1 is.** point
1e675 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 ing to. The upd
1e676 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 ate hook will be
1e677 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 invoked, if it
1e678 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 exists..** If P4
1e679 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 is not NULL the
1e67a 6e 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 n the P1 cursor
1e67b 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 must have been p
1e67c 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 ositioned.** usi
1e67d 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 ng OP_NotFound p
1e67e 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 rior to invoking
1e67f 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f this opcode..*/
1e680 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a .case OP_Delete:
1e681 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1e682 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e683 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a ed into u.bg */.
1e684 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 56 64 i64 iKey;. Vd
1e685 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 beCursor *pC;.#e
1e686 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e687 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e688 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 20 75 2e to u.bg */.. u.
1e689 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a 20 20 61 bg.iKey = 0;. a
1e68a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e68b 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e68c 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e68d 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b g.pC = p->apCsr[
1e68e 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e68f 72 74 28 20 75 2e 62 67 2e 70 43 21 3d 30 20 29 rt( u.bg.pC!=0 )
1e690 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 ;. assert( u.bg
1e691 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 .pC->pCursor!=0
1e692 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 ); /* Only vali
1e693 64 20 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65 d for real table
1e694 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c s, no pseudotabl
1e695 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 es */.. /* If t
1e696 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77 he update-hook w
1e697 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 ill be invoked,
1e698 73 65 74 20 75 2e 62 67 2e 69 4b 65 79 20 74 6f set u.bg.iKey to
1e699 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 the rowid of th
1e69a 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 e. ** row being
1e69b 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20 deleted.. */.
1e69c 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65 if( db->xUpdate
1e69d 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d Callback && pOp-
1e69e 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73 >p4.z ){. ass
1e69f 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 73 ert( u.bg.pC->is
1e6a0 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 Table );. ass
1e6a1 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 72 6f ert( u.bg.pC->ro
1e6a2 77 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f widIsValid ); /
1e6a3 2a 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20 * lastRowid set
1e6a4 62 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e by previous OP_N
1e6a5 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 75 otFound */. u
1e6a6 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e 62 67 2e .bg.iKey = u.bg.
1e6a7 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 pC->lastRowid;.
1e6a8 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 50 5f }.. /* The OP_
1e6a9 44 65 6c 65 74 65 20 6f 70 63 6f 64 65 20 61 6c Delete opcode al
1e6aa 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20 61 6e 20 ways follows an
1e6ab 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20 OP_NotExists or
1e6ac 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20 2a 2a 20 OP_Last or. **
1e6ad 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 OP_Column on the
1e6ae 20 73 61 6d 65 20 74 61 62 6c 65 20 77 69 74 68 same table with
1e6af 6f 75 74 20 61 6e 79 20 69 6e 74 65 72 76 65 6e out any interven
1e6b0 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 ing operations t
1e6b1 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 6d hat. ** might m
1e6b2 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 ove or invalidat
1e6b3 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48 e the cursor. H
1e6b4 65 6e 63 65 20 63 75 72 73 6f 72 20 75 2e 62 67 ence cursor u.bg
1e6b5 2e 70 43 20 69 73 20 61 6c 77 61 79 73 20 70 6f .pC is always po
1e6b6 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 inting. ** to t
1e6b7 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c he row to be del
1e6b8 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c eted and the sql
1e6b9 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f ite3VdbeCursorMo
1e6ba 76 65 74 6f 28 29 20 6f 70 65 72 61 74 69 6f 6e veto() operation
1e6bb 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69 73 20 61 . ** below is a
1e6bc 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70 20 61 6e lways a no-op an
1e6bd 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 d cannot fail.
1e6be 57 65 20 77 69 6c 6c 20 72 75 6e 20 69 74 20 61 We will run it a
1e6bf 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68 2c 0a 20 nyhow, though,.
1e6c0 20 2a 2a 20 74 6f 20 67 75 61 72 64 20 61 67 61 ** to guard aga
1e6c1 69 6e 73 74 20 66 75 74 75 72 65 20 63 68 61 6e inst future chan
1e6c2 67 65 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 ges to the code
1e6c3 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 2f generator.. **/
1e6c4 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e . assert( u.bg.
1e6c5 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e6c6 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 to==0 );. rc =
1e6c7 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e6c8 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e 70 43 29 rMoveto(u.bg.pC)
1e6c9 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1e6ca 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 !=SQLITE_OK) ) g
1e6cb 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e6cc 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71 6c 69 74 _error;.. sqlit
1e6cd 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 e3BtreeSetCached
1e6ce 52 6f 77 69 64 28 75 2e 62 67 2e 70 43 2d 3e 70 Rowid(u.bg.pC->p
1e6cf 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 Cursor, 0);. rc
1e6d0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 = sqlite3BtreeD
1e6d1 65 6c 65 74 65 28 75 2e 62 67 2e 70 43 2d 3e 70 elete(u.bg.pC->p
1e6d2 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 62 67 2e Cursor);. u.bg.
1e6d3 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e6d4 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a = CACHE_STALE;..
1e6d5 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
1e6d6 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 update-hook if r
1e6d7 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 equired. */. if
1e6d8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e6d9 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 && db->xUpdateCa
1e6da 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 llback && pOp->p
1e6db 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 4.z ){. const
1e6dc 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d char *zDb = db-
1e6dd 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d 3e 69 44 >aDb[u.bg.pC->iD
1e6de 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f b].zName;. co
1e6df 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d nst char *zTbl =
1e6e0 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 pOp->p4.z;.
1e6e1 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
1e6e2 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 ack(db->pUpdateA
1e6e3 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 rg, SQLITE_DELET
1e6e4 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 75 2e E, zDb, zTbl, u.
1e6e5 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 bg.iKey);. as
1e6e6 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 sert( u.bg.pC->i
1e6e7 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 Db>=0 );. }. i
1e6e8 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 f( pOp->p2 & OPF
1e6e9 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d LAG_NCHANGE ) p-
1e6ea 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 >nChange++;. br
1e6eb 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 eak;.}./* Opcode
1e6ec 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 2a 20 2a : ResetCount * *
1e6ed 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1e6ee 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 value of the ch
1e6ef 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 ange counter is
1e6f0 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 copied to the da
1e6f1 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a tabase handle.**
1e6f2 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1e6f3 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 (returned by sub
1e6f4 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
1e6f5 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1e6f6 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 ())..** Then the
1e6f7 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 VMs internal ch
1e6f8 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 72 65 73 ange counter res
1e6f9 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 69 ets to 0..** Thi
1e6fa 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 s is used by tri
1e6fb 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a gger programs..*
1e6fc 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 /.case OP_ResetC
1e6fd 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c 69 74 65 ount: {. sqlite
1e6fe 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1e6ff 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
1e700 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 . p->nChange =
1e701 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 0;. break;.}../
1e702 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74 * Opcode: RowDat
1e703 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a a P1 P2 * * *.**
1e704 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1e705 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 egister P2 the c
1e706 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 61 omplete row data
1e707 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a for cursor P1..
1e708 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 ** There is no i
1e709 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 nterpretation of
1e70a 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 the data. .**
1e70b 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 65 It is just copie
1e70c 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 65 d onto the P2 re
1e70d 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 gister exactly a
1e70e 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e s .** it is foun
1e70f 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 d in the databas
1e710 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
1e711 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d the P1 cursor m
1e712 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 ust be pointing
1e713 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 to a valid row (
1e714 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a not a NULL row).
1e715 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 ** of a real tab
1e716 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f le, not a pseudo
1e717 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 -table..*/./* Op
1e718 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 20 code: RowKey P1
1e719 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1e71a 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 rite into regist
1e71b 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 er P2 the comple
1e71c 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63 te row key for c
1e71d 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 ursor P1..** The
1e71e 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 re is no interpr
1e71f 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 etation of the d
1e720 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b 65 ata. .** The ke
1e721 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f y is copied onto
1e722 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72 the P3 register
1e723 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 exactly as .**
1e724 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 it is found in t
1e725 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1e726 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 ..**.** If the P
1e727 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 1 cursor must be
1e728 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 pointing to a v
1e729 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 alid row (not a
1e72a 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 NULL row).** of
1e72b 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f a real table, no
1e72c 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 t a pseudo-table
1e72d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 ..*/.case OP_Row
1e72e 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77 Key:.case OP_Row
1e72f 44 61 74 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f Data: {.#if 0 /
1e730 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e731 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e732 68 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f h */. VdbeCurso
1e733 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f r *pC;. BtCurso
1e734 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20 r *pCrsr;. u32
1e735 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b 0a 23 65 n;. i64 n64;.#e
1e736 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e737 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e738 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 20 70 4f to u.bh */.. pO
1e739 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e ut = &aMem[pOp->
1e73a 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20 p2];.. /* Note
1e73b 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 that RowKey and
1e73c 52 6f 77 44 61 74 61 20 61 72 65 20 72 65 61 6c RowData are real
1e73d 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 ly exactly the s
1e73e 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ame instruction
1e73f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1e740 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e741 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e742 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20 70 2d 3e . u.bh.pC = p->
1e743 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e744 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e745 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f C->isTable || pO
1e746 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f p->opcode==OP_Ro
1e747 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 wKey );. assert
1e748 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 ( u.bh.pC->isInd
1e749 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 ex || pOp->opcod
1e74a 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b e==OP_RowData );
1e74b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e74c 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1e74d 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e 75 6c 6c t( u.bh.pC->null
1e74e 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Row==0 );. asse
1e74f 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 73 65 rt( u.bh.pC->pse
1e750 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 udoTableReg==0 )
1e751 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 ;. assert( u.bh
1e752 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 .pC->pCursor!=0
1e753 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72 73 72 20 );. u.bh.pCrsr
1e754 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 = u.bh.pC->pCurs
1e755 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 or;. assert( sq
1e756 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
1e757 49 73 56 61 6c 69 64 28 75 2e 62 68 2e 70 43 72 IsValid(u.bh.pCr
1e758 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 sr) );.. /* The
1e759 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64 20 4f OP_RowKey and O
1e75a 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f 64 65 P_RowData opcode
1e75b 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20 s always follow
1e75c 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 0a OP_NotExists or.
1e75d 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 2f 4f ** OP_Rewind/O
1e75e 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f 20 69 p_Next with no i
1e75f 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73 74 72 ntervening instr
1e760 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 uctions that mig
1e761 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0a 20 20 ht invalidate.
1e762 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 ** the cursor.
1e763 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c 6f 77 Hence the follow
1e764 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 43 ing sqlite3VdbeC
1e765 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 ursorMoveto() ca
1e766 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a ll is always. *
1e767 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61 * a no-op and ca
1e768 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 20 42 n never fail. B
1e769 75 74 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 ut we leave it i
1e76a 6e 20 70 6c 61 63 65 20 61 73 20 61 20 73 61 66 n place as a saf
1e76b 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ety.. */. asse
1e76c 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 64 65 66 rt( u.bh.pC->def
1e76d 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1e76e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1e76f 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f VdbeCursorMoveto
1e770 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20 69 66 28 (u.bh.pC);. if(
1e771 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 NEVER(rc!=SQLIT
1e772 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f E_OK) ) goto abo
1e773 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1e774 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d .. if( u.bh.pC-
1e775 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 >isIndex ){.
1e776 61 73 73 65 72 74 28 20 21 75 2e 62 68 2e 70 43 assert( !u.bh.pC
1e777 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1e778 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e779 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 68 2e 70 eeKeySize(u.bh.p
1e77a 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 36 34 29 Crsr, &u.bh.n64)
1e77b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
1e77c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 ==SQLITE_OK );
1e77d 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75 73 /* True becaus
1e77e 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 e of CursorMovet
1e77f 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 2a o() call above *
1e780 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68 2e 6e /. if( u.bh.n
1e781 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 64>db->aLimit[SQ
1e782 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1e783 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f H] ){. goto
1e784 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a too_big;. }.
1e785 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20 28 75 33 u.bh.n = (u3
1e786 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20 20 7d 65 2)u.bh.n64;. }e
1e787 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 lse{. rc = sq
1e788 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 lite3BtreeDataSi
1e789 7a 65 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 ze(u.bh.pCrsr, &
1e78a 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20 61 73 73 u.bh.n);. ass
1e78b 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1e78c 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44 61 74 61 OK ); /* Data
1e78d 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 Size() cannot fa
1e78e 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e il */. if( u.
1e78f 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d 3e 61 4c bh.n>(u32)db->aL
1e790 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1e791 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1e792 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1e793 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1e794 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1e795 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62 68 2e 6e row(pOut, u.bh.n
1e796 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f , 0) ){. goto
1e797 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 no_mem;. }. p
1e798 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e 6e 3b Out->n = u.bh.n;
1e799 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1e79a 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 g(pOut, MEM_Blob
1e79b 29 3b 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43 );. if( u.bh.pC
1e79c 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 ->isIndex ){.
1e79d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e79e 65 65 4b 65 79 28 75 2e 62 68 2e 70 43 72 73 72 eeKey(u.bh.pCrsr
1e79f 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 4f 75 , 0, u.bh.n, pOu
1e7a0 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a t->z);. }else{.
1e7a1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e7a2 42 74 72 65 65 44 61 74 61 28 75 2e 62 68 2e 70 BtreeData(u.bh.p
1e7a3 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c Crsr, 0, u.bh.n,
1e7a4 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 pOut->z);. }.
1e7a5 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c pOut->enc = SQL
1e7a6 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e ITE_UTF8; /* In
1e7a7 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 case the blob i
1e7a8 73 20 65 76 65 72 20 63 61 73 74 20 74 6f 20 74 s ever cast to t
1e7a9 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f ext */. UPDATE_
1e7aa 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
1e7ab 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1e7ac 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64 /* Opcode: Rowid
1e7ad 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e7ae 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69 ** Store in regi
1e7af 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 ster P2 an integ
1e7b0 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
1e7b1 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 key of the table
1e7b2 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 entry that.** P
1e7b3 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 1 is currently p
1e7b4 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 oint to..**.** P
1e7b5 31 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 1 can be either
1e7b6 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c an ordinary tabl
1e7b7 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 e or a virtual t
1e7b8 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75 73 65 able. There use
1e7b9 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73 65 70 d to.** be a sep
1e7ba 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69 64 20 arate OP_VRowid
1e7bb 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65 20 77 opcode for use w
1e7bc 69 74 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c ith virtual tabl
1e7bd 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a 2a 20 es, but this.**
1e7be 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77 20 77 one opcode now w
1e7bf 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 74 61 orks for both ta
1e7c0 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a 63 61 ble types..*/.ca
1e7c1 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 se OP_Rowid: {
1e7c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e7c3 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1e7c4 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1e7c5 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e7c6 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a oved into u.bi *
1e7c7 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e7c8 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 20 20 73 pC;. i64 v;. s
1e7c9 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1e7ca 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 ab;. const sqli
1e7cb 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1e7cc 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ule;.#endif /* l
1e7cd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e7ce 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a oved into u.bi *
1e7cf 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1e7d0 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e7d1 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e7d2 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 70 2d 3e . u.bi.pC = p->
1e7d3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e7d4 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 assert( u.bi.p
1e7d5 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 C!=0 );. assert
1e7d6 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73 65 75 64 ( u.bi.pC->pseud
1e7d7 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a oTableReg==0 );.
1e7d8 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 6e if( u.bi.pC->n
1e7d9 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 70 4f ullRow ){. pO
1e7da 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1e7db 4e 75 6c 6c 3b 0a 20 20 20 20 62 72 65 61 6b 3b Null;. break;
1e7dc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 . }else if( u.b
1e7dd 69 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f i.pC->deferredMo
1e7de 76 65 74 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69 veto ){. u.bi
1e7df 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f .v = u.bi.pC->mo
1e7e0 76 65 74 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e vetoTarget;.#ifn
1e7e1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1e7e2 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d VIRTUALTABLE. }
1e7e3 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43 else if( u.bi.pC
1e7e4 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 7b ->pVtabCursor ){
1e7e5 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61 62 20 . u.bi.pVtab
1e7e6 3d 20 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 = u.bi.pC->pVtab
1e7e7 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 Cursor->pVtab;.
1e7e8 20 20 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 u.bi.pModule
1e7e9 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d = u.bi.pVtab->pM
1e7ea 6f 64 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 odule;. asser
1e7eb 74 28 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d t( u.bi.pModule-
1e7ec 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 69 >xRowid );. i
1e7ed 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1e7ee 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1e7ef 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1e7f0 73 65 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 62 se;. rc = u.b
1e7f1 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 i.pModule->xRowi
1e7f2 64 28 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 d(u.bi.pC->pVtab
1e7f3 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 Cursor, &u.bi.v)
1e7f4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1e7f5 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1e7f6 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 sg);. p->zErr
1e7f7 4d 73 67 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 Msg = u.bi.pVtab
1e7f8 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 ->zErrMsg;. u
1e7f9 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .bi.pVtab->zErrM
1e7fa 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 sg = 0;. if(
1e7fb 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1e7fc 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1e7fd 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1e7fe 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1e7ff 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1e800 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 LE */. }else{.
1e801 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e assert( u.bi.
1e802 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 pC->pCursor!=0 )
1e803 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1e804 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1e805 74 6f 28 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20 to(u.bi.pC);.
1e806 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 if( rc ) goto a
1e807 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e808 72 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 69 2e r;. if( u.bi.
1e809 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e80a 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 69 2e 76 ){. u.bi.v
1e80b 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 = u.bi.pC->last
1e80c 52 6f 77 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 Rowid;. }else
1e80d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1e80e 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1e80f 28 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f (u.bi.pC->pCurso
1e810 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 r, &u.bi.v);.
1e811 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1e812 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 QLITE_OK ); /*
1e813 41 6c 77 61 79 73 20 73 6f 20 62 65 63 61 75 73 Always so becaus
1e814 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 e of CursorMovet
1e815 6f 28 29 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 o() above */.
1e816 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 }. }. pOut->u
1e817 2e 69 20 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 62 .i = u.bi.v;. b
1e818 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1e819 64 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a de: NullRow P1 *
1e81a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 * * *.**.** Mov
1e81b 65 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 e the cursor P1
1e81c 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 to a null row.
1e81d 41 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 Any OP_Column op
1e81e 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 erations.** that
1e81f 20 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 occur while the
1e820 20 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 cursor is on th
1e821 65 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 e null row will
1e822 61 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 always.** write
1e823 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 a NULL..*/.case
1e824 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 OP_NullRow: {.#i
1e825 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e826 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e827 74 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 to u.bj */. Vdb
1e828 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e eCursor *pC;.#en
1e829 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e82a 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e82b 6f 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 o u.bj */.. ass
1e82c 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e82d 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e82e 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e ursor );. u.bj.
1e82f 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e830 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e831 28 20 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a ( u.bj.pC!=0 );.
1e832 20 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 u.bj.pC->nullR
1e833 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 ow = 1;. u.bj.p
1e834 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e835 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e = 0;. if( u.bj.
1e836 70 43 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 pC->pCursor ){.
1e837 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1e838 6c 65 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e learCursor(u.bj.
1e839 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 pC->pCursor);.
1e83a 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1e83b 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 Opcode: Last P1
1e83c 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1e83d 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 The next use of
1e83e 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c the Rowid or Col
1e83f 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 umn or Next inst
1e840 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a ruction for P1 .
1e841 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f ** will refer to
1e842 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
1e843 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1e844 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a table or index..
1e845 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 ** If the table
1e846 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 or index is empt
1e847 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e y and P2>0, then
1e848 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1e849 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 y to P2..** If P
1e84a 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 2 is 0 or if the
1e84b 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1e84c 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 is not empty, fa
1e84d 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f ll through.** to
1e84e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1e84f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1e850 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 ase OP_Last: {
1e851 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1e852 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1e853 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e854 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 into u.bk */.
1e855 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1e856 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1e857 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 r;. int res;.#e
1e858 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e859 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e85a 74 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 to u.bk */.. as
1e85b 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e85c 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e85d 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b Cursor );. u.bk
1e85e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e85f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e860 74 28 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b t( u.bk.pC!=0 );
1e861 0a 20 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 . u.bk.pCrsr =
1e862 75 2e 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bk.pC->pCursor
1e863 3b 0a 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 ;. if( u.bk.pCr
1e864 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 sr==0 ){. u.b
1e865 6b 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c k.res = 1;. }el
1e866 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c se{. rc = sql
1e867 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e ite3BtreeLast(u.
1e868 62 6b 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e bk.pCrsr, &u.bk.
1e869 72 65 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b res);. }. u.bk
1e86a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 .pC->nullRow = (
1e86b 75 38 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 u8)u.bk.res;. u
1e86c 2e 62 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bk.pC->deferred
1e86d 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e Moveto = 0;. u.
1e86e 62 6b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 bk.pC->rowidIsVa
1e86f 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e lid = 0;. u.bk.
1e870 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e871 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e872 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 if( pOp->p2>0 &
1e873 26 20 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 & u.bk.res ){.
1e874 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1e875 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1e876 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 .}.../* Opcode:
1e877 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a Sort P1 P2 * * *
1e878 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1e879 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 de does exactly
1e87a 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 the same thing a
1e87b 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 s OP_Rewind exce
1e87c 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e pt that.** it in
1e87d 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f crements an undo
1e87e 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 cumented global
1e87f 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f variable used fo
1e880 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a r testing..**.**
1e881 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f Sorting is acco
1e882 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 mplished by writ
1e883 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f ing records into
1e884 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 a sorting index
1e885 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 ,.** then rewind
1e886 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 ing that index a
1e887 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 nd playing it ba
1e888 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e ck from beginnin
1e889 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 g to.** end. We
1e88a 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 use the OP_Sort
1e88b 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 opcode instead
1e88c 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 of OP_Rewind to
1e88d 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 do the.** rewind
1e88e 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 ing so that the
1e88f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1e890 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e will be incremen
1e891 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 ted and.** regre
1e892 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 ssion tests can
1e893 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 determine whethe
1e894 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 r or not the opt
1e895 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 imizer is.** cor
1e896 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e rectly optimizin
1e897 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a g out sorts..*/.
1e898 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 case OP_Sort: {
1e899 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1e89a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1e89b 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 TEST. sqlite3_s
1e89c 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 ort_count++;. s
1e89d 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1e89e 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 unt--;.#endif.
1e89f 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 p->aCounter[SQLI
1e8a0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f TE_STMTSTATUS_SO
1e8a1 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 RT-1]++;. /* Fa
1e8a2 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 ll through into
1e8a3 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f OP_Rewind */.}./
1e8a4 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 * Opcode: Rewind
1e8a5 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e8a6 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 ** The next use
1e8a7 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 of the Rowid or
1e8a8 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 Column or Next i
1e8a9 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 nstruction for P
1e8aa 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 1 .** will refer
1e8ab 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e to the first en
1e8ac 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1e8ad 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 ase table or ind
1e8ae 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 ex..** If the ta
1e8af 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 ble or index is
1e8b0 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 empty and P2>0,
1e8b1 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 then jump immedi
1e8b2 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 ately to P2..**
1e8b3 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 If P2 is 0 or if
1e8b4 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1e8b5 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 dex is not empty
1e8b6 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a , fall through.*
1e8b7 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1e8b8 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ng instruction..
1e8b9 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e */.case OP_Rewin
1e8ba 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a d: { /* j
1e8bb 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ump */.#if 0 /*
1e8bc 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e8bd 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c moved into u.bl
1e8be 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e8bf 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1e8c0 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 *pCrsr;. int r
1e8c1 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f es;.#endif /* lo
1e8c2 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e8c3 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f ved into u.bl */
1e8c4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1e8c5 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e8c6 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e8c7 20 20 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 u.bl.pC = p->a
1e8c8 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e8c9 20 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 assert( u.bl.pC
1e8ca 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e !=0 );. if( (u.
1e8cb 62 6c 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e bl.pCrsr = u.bl.
1e8cc 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 pC->pCursor)!=0
1e8cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1e8ce 74 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e te3BtreeFirst(u.
1e8cf 62 6c 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e bl.pCrsr, &u.bl.
1e8d0 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 res);. u.bl.p
1e8d1 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62 C->atFirst = u.b
1e8d2 6c 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 l.res==0 ?1:0;.
1e8d3 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 u.bl.pC->defe
1e8d4 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1e8d5 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 u.bl.pC->cac
1e8d6 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1e8d7 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c _STALE;. u.bl
1e8d8 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e8d9 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a d = 0;. }else{.
1e8da 20 20 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 u.bl.res = 1
1e8db 3b 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d ;. }. u.bl.pC-
1e8dc 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 >nullRow = (u8)u
1e8dd 2e 62 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 .bl.res;. asser
1e8de 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 t( pOp->p2>0 &&
1e8df 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 pOp->p2<p->nOp )
1e8e0 3b 0a 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 ;. if( u.bl.res
1e8e1 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1e8e2 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1e8e3 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e8e4 6f 64 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20 ode: Next P1 P2
1e8e5 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 * * *.**.** Adva
1e8e6 6e 63 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f nce cursor P1 so
1e8e7 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1e8e8 74 6f 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f to the next key/
1e8e9 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 data pair in its
1e8ea 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 .** table or ind
1e8eb 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 ex. If there ar
1e8ec 65 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 e no more key/va
1e8ed 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 lue pairs then f
1e8ee 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 all through.** t
1e8ef 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1e8f0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 instruction. Bu
1e8f1 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 t if the cursor
1e8f2 61 64 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 advance was succ
1e8f3 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 essful,.** jump
1e8f4 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 immediately to P
1e8f5 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 2..**.** The P1
1e8f6 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 cursor must be f
1e8f7 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c or a real table,
1e8f8 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 not a pseudo-ta
1e8f9 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ble..**.** See a
1e8fa 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 lso: Prev.*/./*
1e8fb 4f 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20 Opcode: Prev P1
1e8fc 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 P2 * * *.**.** B
1e8fd 61 63 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 ack up cursor P1
1e8fe 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
1e8ff 74 73 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f ts to the previo
1e900 75 73 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 us key/data pair
1e901 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 in its.** table
1e902 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 or index. If t
1e903 68 65 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 here is no previ
1e904 6f 75 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 ous key/value pa
1e905 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 irs then fall th
1e906 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 rough.** to the
1e907 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 following instru
1e908 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 ction. But if t
1e909 68 65 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 he cursor backup
1e90a 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c was successful,
1e90b 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 .** jump immedia
1e90c 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a tely to P2..**.*
1e90d 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 * The P1 cursor
1e90e 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 must be for a re
1e90f 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 al table, not a
1e910 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f pseudo-table..*/
1e911 0a 63 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 .case OP_Prev:
1e912 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 /* jump
1e913 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a */.case OP_Next:
1e914 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1e915 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1e916 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e917 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a oved into u.bm *
1e918 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e919 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a pC;. BtCursor *
1e91a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 pCrsr;. int res
1e91b 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e91c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e91d 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a d into u.bm */..
1e91e 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1e91f 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 RRUPT;. assert(
1e920 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1e921 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1e922 72 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d r );. u.bm.pC =
1e923 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e924 31 5d 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 1];. if( u.bm.p
1e925 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61 C==0 ){. brea
1e926 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 k; /* See ticke
1e927 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 t #2273 */. }.
1e928 20 75 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e u.bm.pCrsr = u.
1e929 62 6d 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a bm.pC->pCursor;.
1e92a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 if( u.bm.pCrsr
1e92b 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e ==0 ){. u.bm.
1e92c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b pC->nullRow = 1;
1e92d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1e92e 20 20 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a u.bm.res = 1;.
1e92f 20 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 assert( u.bm.p
1e930 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e931 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 o==0 );. rc = p
1e932 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e Op->opcode==OP_N
1e933 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 ext ? sqlite3Btr
1e934 65 65 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 eeNext(u.bm.pCrs
1e935 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a r, &u.bm.res) :.
1e936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e937 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
1e938 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
1e939 75 73 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 us(u.bm.pCrsr, &
1e93a 75 2e 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 u.bm.res);. u.b
1e93b 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 m.pC->nullRow =
1e93c 28 75 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 (u8)u.bm.res;.
1e93d 75 2e 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bm.pC->cacheSt
1e93e 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e93f 4c 45 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 LE;. if( u.bm.r
1e940 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 es==0 ){. pc
1e941 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1e942 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 if( pOp->p5 )
1e943 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 p->aCounter[pOp
1e944 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 ->p5-1]++;.#ifde
1e945 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1e946 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
1e947 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
1e948 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e . }. u.bm.pC->
1e949 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e94a 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1e94b 20 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 Opcode: IdxInse
1e94c 72 74 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 rt P1 P2 P3 * P5
1e94d 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 .**.** Register
1e94e 50 32 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 P2 holds a SQL i
1e94f 6e 64 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 ndex key made us
1e950 69 6e 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 ing the.** MakeR
1e951 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f ecord instructio
1e952 6e 73 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 ns. This opcode
1e953 20 77 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 writes that key
1e954 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 .** into the ind
1e955 65 78 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 ex P1. Data for
1e956 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 the entry is ni
1e957 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 l..**.** P3 is a
1e958 20 66 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 flag that provi
1e959 64 65 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 des a hint to th
1e95a 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 e b-tree layer t
1e95b 68 61 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 hat this.** inse
1e95c 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 rt is likely to
1e95d 62 65 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a be an append..**
1e95e 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1e95f 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tion only works
1e960 66 6f 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 for indices. Th
1e961 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 e equivalent ins
1e962 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 truction.** for
1e963 74 61 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 tables is OP_Ins
1e964 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ert..*/.case OP_
1e965 49 64 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 IdxInsert: {
1e966 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 /* in2 */.#i
1e967 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e968 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e969 74 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 to u.bn */. Vdb
1e96a 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1e96b 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e96c 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f int nKey;. co
1e96d 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a nst char *zKey;.
1e96e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e96f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e970 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 into u.bn */..
1e971 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1e972 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1e973 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e >nCursor );. u.
1e974 62 6e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 bn.pC = p->apCsr
1e975 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1e976 65 72 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 ert( u.bn.pC!=0
1e977 29 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 );. pIn2 = &aMe
1e978 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 m[pOp->p2];. as
1e979 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 sert( pIn2->flag
1e97a 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a s & MEM_Blob );.
1e97b 20 20 75 2e 62 6e 2e 70 43 72 73 72 20 3d 20 75 u.bn.pCrsr = u
1e97c 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b .bn.pC->pCursor;
1e97d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e . if( ALWAYS(u.
1e97e 62 6e 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a bn.pCrsr!=0) ){.
1e97f 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e assert( u.bn
1e980 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 .pC->isTable==0
1e981 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61 );. rc = Expa
1e982 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 ndBlob(pIn2);.
1e983 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e984 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 _OK ){. u.b
1e985 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e n.nKey = pIn2->n
1e986 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 7a 4b 65 ;. u.bn.zKe
1e987 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 y = pIn2->z;.
1e988 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1e989 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62 6e 2e treeInsert(u.bn.
1e98a 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79 pCrsr, u.bn.zKey
1e98b 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c , u.bn.nKey, "",
1e98c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a 0, 0, pOp->p3,.
1e98d 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70 2d ((pOp-
1e98e 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 >p5 & OPFLAG_USE
1e98f 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e SEEKRESULT) ? u.
1e990 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c bn.pC->seekResul
1e991 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29 3b 0a t : 0). );.
1e992 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e993 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d bn.pC->deferredM
1e994 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 oveto==0 );.
1e995 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61 63 68 65 u.bn.pC->cache
1e996 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e997 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TALE;. }. }.
1e998 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e999 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74 65 pcode: IdxDelete
1e99a 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1e99b 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 .** The content
1e99c 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73 20 of P3 registers
1e99d 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 69 starting at regi
1e99e 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 ster P2 form.**
1e99f 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1e9a0 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63 6f x key. This opco
1e9a1 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 20 de removes that
1e9a2 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 0a entry from the .
1e9a3 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64 20 ** index opened
1e9a4 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f by cursor P1..*/
1e9a5 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c 65 .case OP_IdxDele
1e9a6 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 te: {.#if 0 /*
1e9a7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e9a8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20 moved into u.bo
1e9a9 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1e9aa 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1e9ab 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 *pCrsr;. int re
1e9ac 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 s;. UnpackedRec
1e9ad 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a ord r;.#endif /*
1e9ae 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e9af 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f moved into u.bo
1e9b0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e9b1 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 73 Op->p3>0 );. as
1e9b2 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1e9b3 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e && pOp->p2+pOp->
1e9b4 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b p3<=p->nMem+1 );
1e9b5 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e9b6 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e9b7 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e9b8 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bo.pC = p->ap
1e9b9 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e9ba 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 21 assert( u.bo.pC!
1e9bb 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 72 =0 );. u.bo.pCr
1e9bc 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 43 sr = u.bo.pC->pC
1e9bd 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 ursor;. if( ALW
1e9be 41 59 53 28 75 2e 62 6f 2e 70 43 72 73 72 21 3d AYS(u.bo.pCrsr!=
1e9bf 30 29 20 29 7b 0a 20 20 20 20 75 2e 62 6f 2e 72 0) ){. u.bo.r
1e9c0 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 6f .pKeyInfo = u.bo
1e9c1 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 .pC->pKeyInfo;.
1e9c2 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64 u.bo.r.nField
1e9c3 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b = (u16)pOp->p3;
1e9c4 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66 6c 61 67 . u.bo.r.flag
1e9c5 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6f 2e s = 0;. u.bo.
1e9c6 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 r.aMem = &aMem[p
1e9c7 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72 63 20 Op->p2];. rc
1e9c8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1e9c9 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 vetoUnpacked(u.b
1e9ca 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62 6f 2e 72 o.pCrsr, &u.bo.r
1e9cb 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f 2e 72 65 , 0, 0, &u.bo.re
1e9cc 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d s);. if( rc==
1e9cd 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 SQLITE_OK && u.b
1e9ce 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 o.res==0 ){.
1e9cf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e9d0 72 65 65 44 65 6c 65 74 65 28 75 2e 62 6f 2e 70 reeDelete(u.bo.p
1e9d1 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Crsr);. }.
1e9d2 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 assert( u.bo.pC
1e9d3 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1e9d4 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 6f 2e ==0 );. u.bo.
1e9d5 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e9d6 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e9d7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e9d8 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 * Opcode: IdxRow
1e9d9 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a id P1 P2 * * *.*
1e9da 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 *.** Write into
1e9db 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 register P2 an i
1e9dc 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
1e9dd 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
1e9de 6e 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a n the record at.
1e9df 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 ** the end of th
1e9e0 65 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e e index key poin
1e9e1 74 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 ted to by cursor
1e9e2 20 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 P1. This integ
1e9e3 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 er should be.**
1e9e4 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
1e9e5 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 table entry to
1e9e6 77 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 which this index
1e9e7 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a entry points..*
1e9e8 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 *.** See also: R
1e9e9 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64 owid, MakeRecord
1e9ea 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1e9eb 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 Rowid: {
1e9ec 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e9ed 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e9ee 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e9ef 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e9f0 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74 43 75 72 u.bp */. BtCur
1e9f1 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 sor *pCrsr;. Vd
1e9f2 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1e9f3 69 36 34 20 72 6f 77 69 64 3b 0a 23 65 6e 64 69 i64 rowid;.#endi
1e9f4 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e9f5 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e9f6 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bp */.. asser
1e9f7 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e9f8 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e9f9 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 sor );. u.bp.pC
1e9fa 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e9fb 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e9fc 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bp.pC!=0 );.
1e9fd 75 2e 62 70 2e 70 43 72 73 72 20 3d 20 75 2e 62 u.bp.pCrsr = u.b
1e9fe 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 p.pC->pCursor;.
1e9ff 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1ea00 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 41 EM_Null;. if( A
1ea01 4c 57 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72 LWAYS(u.bp.pCrsr
1ea02 21 3d 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d !=0) ){. rc =
1ea03 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 sqlite3VdbeCurs
1ea04 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 orMoveto(u.bp.pC
1ea05 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 );. if( NEVER
1ea06 28 72 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (rc) ) goto abor
1ea07 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1ea08 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 assert( u.bp
1ea09 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1ea0a 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 eto==0 );. as
1ea0b 73 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 sert( u.bp.pC->i
1ea0c 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 sTable==0 );.
1ea0d 20 69 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e if( !u.bp.pC->n
1ea0e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 ullRow ){.
1ea0f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1ea10 49 64 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 IdxRowid(db, u.b
1ea11 70 2e 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 p.pCrsr, &u.bp.r
1ea12 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 owid);. if(
1ea13 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1ea14 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 {. goto a
1ea15 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1ea16 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
1ea17 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 pOut->u.i = u.b
1ea18 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 20 20 70 p.rowid;. p
1ea19 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1ea1a 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a _Int;. }. }.
1ea1b 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1ea1c 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50 31 20 pcode: IdxGE P1
1ea1d 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1ea1e 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65 * The P4 registe
1ea1f 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 r values beginni
1ea20 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 ng with P3 form
1ea21 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1ea22 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f x .** key that o
1ea23 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 mits the ROWID.
1ea24 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 Compare this ke
1ea25 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 y value against
1ea26 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 the index .** th
1ea27 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 at P1 is current
1ea28 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 ly pointing to,
1ea29 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 ignoring the ROW
1ea2a 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 ID on the P1 ind
1ea2b 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ex..**.** If the
1ea2c 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 P1 index entry
1ea2d 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1ea2e 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1ea2f 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65 key value.** the
1ea30 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f n jump to P2. O
1ea31 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 therwise fall th
1ea32 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1ea33 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1ea34 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f *.** If P5 is no
1ea35 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 n-zero then the
1ea36 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 key value is inc
1ea37 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 reased by an eps
1ea38 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74 ilon .** prior t
1ea39 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e o the comparison
1ea3a 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74 68 65 . This make the
1ea3b 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b opcode work lik
1ea3c 65 20 49 64 78 47 54 20 65 78 63 65 70 74 0a 2a e IdxGT except.*
1ea3d 2a 20 74 68 61 74 20 69 66 20 74 68 65 20 6b 65 * that if the ke
1ea3e 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 y from register
1ea3f 50 33 20 69 73 20 61 20 70 72 65 66 69 78 20 6f P3 is a prefix o
1ea40 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74 68 65 f the key in the
1ea41 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20 cursor,.** the
1ea42 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 20 result is false
1ea43 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 6c 64 whereas it would
1ea44 20 62 65 20 74 72 75 65 20 77 69 74 68 20 49 64 be true with Id
1ea45 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 xGT..*/./* Opcod
1ea46 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32 20 50 e: IdxLT P1 P2 P
1ea47 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 3 * P5.**.** The
1ea48 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c P4 register val
1ea49 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 ues beginning wi
1ea4a 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e th P3 form an un
1ea4b 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a packed index .**
1ea4c 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20 key that omits
1ea4d 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 the ROWID. Comp
1ea4e 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c are this key val
1ea4f 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 ue against the i
1ea50 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 ndex .** that P1
1ea51 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
1ea52 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 inting to, ignor
1ea53 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e ing the ROWID on
1ea54 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a the P1 index..*
1ea55 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69 *.** If the P1 i
1ea56 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6c 65 ndex entry is le
1ea57 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 ss than the key
1ea58 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d 70 20 value then jump
1ea59 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 to P2..** Otherw
1ea5a 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 ise fall through
1ea5b 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1ea5c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 truction..**.**
1ea5d 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 If P5 is non-zer
1ea5e 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76 o then the key v
1ea5f 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 alue is increase
1ea60 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 d by an epsilon
1ea61 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65 prior .** to the
1ea62 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 comparison. Th
1ea63 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 is makes the opc
1ea64 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 ode work like Id
1ea65 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f xLE..*/.case OP_
1ea66 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20 20 20 IdxLT:
1ea67 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 /* jump */.case
1ea68 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 20 20 OP_IdxGE: {
1ea69 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1ea6a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ea6b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ea6c 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62 to u.bq */. Vdb
1ea6d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 eCursor *pC;. i
1ea6e 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b nt res;. Unpack
1ea6f 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 edRecord r;.#end
1ea70 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1ea71 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ea72 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bq */.. asse
1ea73 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1ea74 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1ea75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70 rsor );. u.bq.p
1ea76 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1ea77 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1ea78 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bq.pC!=0 );.
1ea79 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71 if( ALWAYS(u.bq
1ea7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 .pC->pCursor!=0)
1ea7b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1ea7c 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bq.pC->deferre
1ea7d 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1ea7e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ea7f 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 5==0 || pOp->p5=
1ea80 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =1 );. assert
1ea81 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1ea82 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75 4_INT32 );. u
1ea83 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bq.r.pKeyInfo =
1ea84 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e u.bq.pC->pKeyIn
1ea85 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e fo;. u.bq.r.n
1ea86 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 Field = (u16)pOp
1ea87 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20 ->p4.i;. if(
1ea88 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20 pOp->p5 ){.
1ea89 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 u.bq.r.flags =
1ea8a 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1ea8b 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f | UNPACKED_IGNO
1ea8c 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 RE_ROWID;. }e
1ea8d 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e lse{. u.bq.
1ea8e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b r.flags = UNPACK
1ea8f 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b ED_IGNORE_ROWID;
1ea90 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e . }. u.bq.
1ea91 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 r.aMem = &aMem[p
1ea92 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 Op->p3];. rc
1ea93 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 = sqlite3VdbeIdx
1ea94 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e 62 71 2e KeyCompare(u.bq.
1ea95 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 26 75 2e pC, &u.bq.r, &u.
1ea96 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 bq.res);. if(
1ea97 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1ea98 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20 _IdxLT ){.
1ea99 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 2e 62 71 u.bq.res = -u.bq
1ea9a 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b .res;. }else{
1ea9b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1ea9c 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1ea9d 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20 75 2e dxGE );. u.
1ea9e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 20 7d 0a bq.res++;. }.
1ea9f 20 20 20 20 69 66 28 20 75 2e 62 71 2e 72 65 73 if( u.bq.res
1eaa0 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d >0 ){. pc =
1eaa1 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 pOp->p2 - 1 ;.
1eaa2 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1eaa3 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1eaa4 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 50 33 Destroy P1 P2 P3
1eaa5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 * *.**.** Delet
1eaa6 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61 e an entire data
1eaa7 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e base table or in
1eaa8 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 dex whose root p
1eaa9 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
1eaaa 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67 ase.** file is g
1eaab 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a iven by P1..**.*
1eaac 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e * The table bein
1eaad 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 20 69 g destroyed is i
1eaae 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 n the main datab
1eaaf 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 3d 3d ase file if P3==
1eab0 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 0. If.** P3==1
1eab1 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 then the table t
1eab2 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e o be clear is in
1eab3 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 the auxiliary d
1eab4 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 atabase file.**
1eab5 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 that is used to
1eab6 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 store tables cre
1eab7 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 ate using CREATE
1eab8 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 TEMPORARY TABLE
1eab9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 ..**.** If AUTOV
1eaba 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 ACUUM is enabled
1eabb 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73 then it is poss
1eabc 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 ible that anothe
1eabd 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d r root page.** m
1eabe 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 69 6e ight be moved in
1eabf 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 65 6c to the newly del
1eac0 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 20 69 eted root page i
1eac1 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1eac2 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 all.** root page
1eac3 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 s contiguous at
1eac4 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
1eac5 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1eac6 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 The former.** va
1eac7 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 lue of the root
1eac8 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 64 20 page that moved
1eac9 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 66 6f - its value befo
1eaca 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 re the move occu
1eacb 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f rred -.** is sto
1eacc 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1eacd 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 P2. If no page
1eace 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73 .** movement was
1eacf 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61 75 required (becau
1ead0 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 se the table bei
1ead1 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20 61 ng dropped was a
1ead2 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c lready .** the l
1ead3 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20 64 ast one in the d
1ead4 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61 20 atabase) then a
1ead5 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 zero is stored i
1ead6 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a n register P2..*
1ead7 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 * If AUTOVACUUM
1ead8 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 65 6e is disabled then
1ead9 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 a zero is store
1eada 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 d in register P2
1eadb 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1eadc 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 : Clear.*/.case
1eadd 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 OP_Destroy: {
1eade 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1eadf 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f ease */.#if 0 /
1eae0 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eae1 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1eae2 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 r */. int iMove
1eae3 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 d;. int iCnt;.
1eae4 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20 Vdbe *pVdbe;.
1eae5 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 69 66 20 int iDb;.#endif
1eae6 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1eae7 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1eae8 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 br */.#ifndef SQ
1eae9 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1eaea 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 2e 69 43 LTABLE. u.br.iC
1eaeb 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 75 2e nt = 0;. for(u.
1eaec 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 br.pVdbe=db->pVd
1eaed 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 3b 20 be; u.br.pVdbe;
1eaee 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 75 2e 62 u.br.pVdbe = u.b
1eaef 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b r.pVdbe->pNext){
1eaf0 0a 20 20 20 20 69 66 28 20 75 2e 62 72 2e 70 56 . if( u.br.pV
1eaf1 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 dbe->magic==VDBE
1eaf2 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 75 2e _MAGIC_RUN && u.
1eaf3 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 74 61 62 br.pVdbe->inVtab
1eaf4 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 2e 62 72 Method<2 && u.br
1eaf5 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b .pVdbe->pc>=0 ){
1eaf6 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 43 6e 74 . u.br.iCnt
1eaf7 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 ++;. }. }.#e
1eaf8 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20 lse. u.br.iCnt
1eaf9 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 = db->activeVdbe
1eafa 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4f Cnt;.#endif. pO
1eafb 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1eafc 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 75 2e 62 72 Null;. if( u.br
1eafd 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72 .iCnt>1 ){. r
1eafe 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 c = SQLITE_LOCKE
1eaff 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 D;. p->errorA
1eb00 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 ction = OE_Abort
1eb01 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1eb02 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 .br.iDb = pOp->p
1eb03 33 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 3;. assert( u
1eb04 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 .br.iCnt==1 );.
1eb05 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 assert( (p->b
1eb06 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 treeMask & (1<<u
1eb07 2e 62 72 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a .br.iDb))!=0 );.
1eb08 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1eb09 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 BtreeDropTable(d
1eb0a 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d b->aDb[u.br.iDb]
1eb0b 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 .pBt, pOp->p1, &
1eb0c 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b 0a 20 20 u.br.iMoved);.
1eb0d 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1eb0e 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75 MEM_Int;. pOu
1eb0f 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69 4d t->u.i = u.br.iM
1eb10 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53 51 oved;.#ifndef SQ
1eb11 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1eb12 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63 3d CUUM. if( rc=
1eb13 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e =SQLITE_OK && u.
1eb14 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a br.iMoved!=0 ){.
1eb15 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f sqlite3Roo
1eb16 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e tPageMoved(&db->
1eb17 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20 75 aDb[u.br.iDb], u
1eb18 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d .br.iMoved, pOp-
1eb19 3e 70 31 29 3b 0a 20 20 20 20 20 20 72 65 73 65 >p1);. rese
1eb1a 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 3d tSchemaOnFault =
1eb1b 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 1;. }.#endif
1eb1c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1eb1d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 ./* Opcode: Clea
1eb1e 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a r P1 P2 P3.**.**
1eb1f 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 Delete all cont
1eb20 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 ents of the data
1eb21 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e base table or in
1eb22 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 dex whose root p
1eb23 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 age.** in the da
1eb24 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 tabase file is g
1eb25 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 iven by P1. But
1eb26 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 , unlike Destroy
1eb27 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f , do not.** remo
1eb28 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 ve the table or
1eb29 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 index from the d
1eb2a 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1eb2b 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 .** The table be
1eb2c 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 ing clear is in
1eb2d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1eb2e 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e e file if P2==0.
1eb2f 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 If.** P2==1 th
1eb30 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 en the table to
1eb31 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 be clear is in t
1eb32 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
1eb33 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 abase file.** th
1eb34 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 at is used to st
1eb35 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 ore tables creat
1eb36 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 e using CREATE T
1eb37 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a EMPORARY TABLE..
1eb38 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20 **.** If the P3
1eb39 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 value is non-zer
1eb3a 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c o, then the tabl
1eb3b 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75 e referred to mu
1eb3c 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b st be an.** intk
1eb3d 65 79 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c ey table (an SQL
1eb3e 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 table, not an i
1eb3f 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 ndex). In this c
1eb40 61 73 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e ase the row chan
1eb41 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 ge .** count is
1eb42 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 incremented by t
1eb43 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
1eb44 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 s in the table b
1eb45 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a eing cleared. .*
1eb46 2a 20 49 66 20 50 33 20 69 73 20 67 72 65 61 74 * If P3 is great
1eb47 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 er than zero, th
1eb48 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f en the value sto
1eb49 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1eb4a 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e P3 is.** also in
1eb4b 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 cremented by the
1eb4c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 number of rows
1eb4d 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 in the table bei
1eb4e 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a ng cleared..**.*
1eb4f 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 * See also: Dest
1eb50 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 roy.*/.case OP_C
1eb51 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 20 20 2f lear: {.#if 0 /
1eb52 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eb53 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1eb54 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e s */. int nChan
1eb55 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f ge;.#endif /* lo
1eb56 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1eb57 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f ved into u.bs */
1eb58 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 .. u.bs.nChange
1eb59 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
1eb5a 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1eb5b 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 (1<<pOp->p2))!=0
1eb5c 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1eb5d 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
1eb5e 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 e(. db->aDb
1eb5f 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 [pOp->p2].pBt, p
1eb60 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 Op->p1, (pOp->p3
1eb61 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 6e 67 65 ? &u.bs.nChange
1eb62 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 : 0). );. if(
1eb63 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 pOp->p3 ){.
1eb64 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 75 2e p->nChange += u.
1eb65 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 bs.nChange;.
1eb66 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b if( pOp->p3>0 ){
1eb67 0a 20 20 20 20 20 20 61 4d 65 6d 5b 70 4f 70 2d . aMem[pOp-
1eb68 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62 73 >p3].u.i += u.bs
1eb69 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a .nChange;. }.
1eb6a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1eb6b 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 /* Opcode: Creat
1eb6c 65 54 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a eTable P1 P2 * *
1eb6d 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 *.**.** Allocat
1eb6e 65 20 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e e a new table in
1eb6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1eb70 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 se file if P1==0
1eb71 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 or in the.** au
1eb72 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1eb73 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f file if P1==1 o
1eb74 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 r in an attached
1eb75 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 database if.**
1eb76 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 P1>1. Write the
1eb77 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
1eb78 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 r of the new tab
1eb79 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 le into.** regis
1eb7a 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 ter P2.**.** The
1eb7b 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 difference betw
1eb7c 65 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 een a table and
1eb7d 61 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 an index is this
1eb7e 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a : A table must.
1eb7f 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 ** have a 4-byte
1eb80 20 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 integer key and
1eb81 20 63 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 can have arbitr
1eb82 61 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e ary data. An in
1eb83 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 dex.** has an ar
1eb84 62 69 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 bitrary key but
1eb85 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 no data..**.** S
1eb86 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 ee also: CreateI
1eb87 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ndex.*/./* Opcod
1eb88 65 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 e: CreateIndex P
1eb89 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1eb8a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
1eb8b 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69 index in the mai
1eb8c 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
1eb8d 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 if P1==0 or in t
1eb8e 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 he.** auxiliary
1eb8f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1eb90 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 P1==1 or in an
1eb91 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1eb92 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 e if.** P1>1. W
1eb93 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 rite the root pa
1eb94 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
1eb95 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a new table into.
1eb96 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a ** register P2..
1eb97 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 **.** See docume
1eb98 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 ntation on OP_Cr
1eb99 65 61 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 eateTable for ad
1eb9a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1eb9b 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1eb9c 5f 43 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20 _CreateIndex:
1eb9d 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1eb9e 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 -prerelease */.c
1eb9f 61 73 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 ase OP_CreateTab
1eba0 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f le: { /
1eba1 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1eba2 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1eba3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1eba4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a oved into u.bt *
1eba5 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 /. int pgno;.
1eba6 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 int flags;. Db
1eba7 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pDb;.#endif /*
1eba8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1eba9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 moved into u.bt
1ebaa 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67 6e 6f 20 */.. u.bt.pgno
1ebab 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
1ebac 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1ebad 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p1<db->nDb );.
1ebae 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1ebaf 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1ebb0 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 p->p1))!=0 );.
1ebb1 75 2e 62 74 2e 70 44 62 20 3d 20 26 64 62 2d 3e u.bt.pDb = &db->
1ebb2 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 aDb[pOp->p1];.
1ebb3 61 73 73 65 72 74 28 20 75 2e 62 74 2e 70 44 62 assert( u.bt.pDb
1ebb4 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 ->pBt!=0 );. if
1ebb5 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1ebb6 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 7b P_CreateTable ){
1ebb7 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e 66 6c 61 . /* u.bt.fla
1ebb8 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 gs = BTREE_INTKE
1ebb9 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 74 2e 66 Y; */. u.bt.f
1ebba 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 lags = BTREE_LEA
1ebbb 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b FDATA|BTREE_INTK
1ebbc 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 EY;. }else{.
1ebbd 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 u.bt.flags = BT
1ebbe 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 REE_ZERODATA;.
1ebbf 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 }. rc = sqlite3
1ebc0 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
1ebc1 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42 74 2c 20 (u.bt.pDb->pBt,
1ebc2 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75 2e 62 74 &u.bt.pgno, u.bt
1ebc3 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d .flags);. pOut-
1ebc4 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70 67 6e 6f >u.i = u.bt.pgno
1ebc5 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ebc6 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 Opcode: ParseSc
1ebc7 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 hema P1 P2 * P4
1ebc8 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 *.**.** Read and
1ebc9 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 parse all entri
1ebca 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 es from the SQLI
1ebcb 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1ebcc 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a of database P1.*
1ebcd 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 * that match the
1ebce 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 WHERE clause P4
1ebcf 2e 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f . P2 is the "fo
1ebd0 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 rce" flag. Alw
1ebd1 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 ays do.** the pa
1ebd2 72 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 rsing if P2 is t
1ebd3 72 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 rue. If P2 is f
1ebd4 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 alse, then this
1ebd5 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 routine is a.**
1ebd6 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 no-op if the sch
1ebd7 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ema is not curre
1ebd8 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e ntly loaded. In
1ebd9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 other words, if
1ebda 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c P2.** is false,
1ebdb 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 the SQLITE_MAST
1ebdc 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 ER table is only
1ebdd 20 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72 parsed if the r
1ebde 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 est of the.** sc
1ebdf 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 hema is already
1ebe0 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 loaded into the
1ebe1 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a symbol table..**
1ebe2 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1ebe3 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 invokes the pars
1ebe4 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e er to create a n
1ebe5 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ew virtual machi
1ebe6 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 ne,.** then runs
1ebe7 20 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c the new virtual
1ebe8 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 machine. It is
1ebe9 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 thus a re-entra
1ebea 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 nt opcode..*/.ca
1ebeb 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d se OP_ParseSchem
1ebec 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c a: {.#if 0 /* l
1ebed 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ebee 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a oved into u.bu *
1ebef 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 63 /. int iDb;. c
1ebf0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
1ebf1 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c er;. char *zSql
1ebf2 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 ;. InitData ini
1ebf3 74 44 61 74 61 3b 0a 23 65 6e 64 69 66 20 2f 2a tData;.#endif /*
1ebf4 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ebf5 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 moved into u.bu
1ebf6 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69 44 62 20 */.. u.bu.iDb
1ebf7 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 = pOp->p1;. ass
1ebf8 65 72 74 28 20 75 2e 62 75 2e 69 44 62 3e 3d 30 ert( u.bu.iDb>=0
1ebf9 20 26 26 20 75 2e 62 75 2e 69 44 62 3c 64 62 2d && u.bu.iDb<db-
1ebfa 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 >nDb );.. /* If
1ebfb 20 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74 pOp->p2 is 0, t
1ebfc 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 hen this opcode
1ebfd 69 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 is being execute
1ebfe 64 20 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a d to read a. **
1ebff 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72 single row, for
1ec00 20 65 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77 example the row
1ec01 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1ec02 6f 20 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20 o a new index.
1ec03 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68 ** created by th
1ec04 69 73 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68 is VDBE, from th
1ec05 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
1ec06 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20 table. It only.
1ec07 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66 ** does this if
1ec08 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1ec09 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 ng in-memory sch
1ec0a 65 6d 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ema is currently
1ec0b 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74 . ** loaded. Ot
1ec0c 68 65 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77 herwise, the new
1ec0d 20 69 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f index definitio
1ec0e 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 n can be loaded
1ec0f 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20 along. ** with
1ec10 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 the rest of the
1ec11 73 63 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69 schema when it i
1ec12 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a s required.. **
1ec13 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74 . ** Although t
1ec14 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 he mutex on the
1ec15 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
1ec16 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 that corresponds
1ec17 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 to. ** databas
1ec18 65 20 75 2e 62 75 2e 69 44 62 20 28 74 68 65 20 e u.bu.iDb (the
1ec19 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1ec1a 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d ing the sqlite_m
1ec1b 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a aster table. **
1ec1c 20 72 65 61 64 20 62 79 20 74 68 69 73 20 69 6e read by this in
1ec1d 73 74 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75 struction) is cu
1ec1e 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74 rrently held, it
1ec1f 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f is necessary to
1ec20 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 . ** obtain the
1ec21 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 mutexes on all
1ec22 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1ec23 65 73 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 es before checki
1ec24 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73 ng if. ** the s
1ec25 63 68 65 6d 61 20 6f 66 20 75 2e 62 75 2e 69 44 chema of u.bu.iD
1ec26 62 20 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69 b is loaded. Thi
1ec27 73 20 69 73 20 62 65 63 61 75 73 65 2c 20 61 74 s is because, at
1ec28 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20 the start of.
1ec29 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 ** the sqlite3_e
1ec2a 78 65 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 xec() call below
1ec2b 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e , SQLite will in
1ec2c 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 voke. ** sqlite
1ec2d 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 3BtreeEnterAll()
1ec2e 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73 . If all mutexes
1ec2f 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 are not already
1ec30 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 held, the. **
1ec31 75 2e 62 75 2e 69 44 62 20 6d 75 74 65 78 20 6d u.bu.iDb mutex m
1ec32 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72 69 6c ay be temporaril
1ec33 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20 61 76 y released to av
1ec34 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49 66 oid deadlock. If
1ec35 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 . ** this happe
1ec36 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20 6f 74 ns, then some ot
1ec37 68 65 72 20 74 68 72 65 61 64 20 6d 61 79 20 64 her thread may d
1ec38 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d elete the in-mem
1ec39 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 ory. ** schema
1ec3a 6f 66 20 64 61 74 61 62 61 73 65 20 75 2e 62 75 of database u.bu
1ec3b 2e 69 44 62 20 62 65 66 6f 72 65 20 74 68 65 20 .iDb before the
1ec3c 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75 SQL statement ru
1ec3d 6e 73 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20 ns. The schema.
1ec3e 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 ** will not be
1ec3f 72 65 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65 reloaded becuase
1ec40 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 the db->init.bu
1ec41 73 79 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 sy flag is set.
1ec42 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65 This. ** can re
1ec43 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75 sult in a "no su
1ec44 63 68 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65 ch table: sqlite
1ec45 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c _master" or "mal
1ec46 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61 formed. ** data
1ec47 62 61 73 65 20 73 63 68 65 6d 61 22 20 65 72 72 base schema" err
1ec48 6f 72 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 or being returne
1ec49 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20 d to the user..
1ec4a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 */. assert( sq
1ec4b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
1ec4c 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 75 2e 62 utex(db->aDb[u.b
1ec4d 75 2e 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20 u.iDb].pBt) );.
1ec4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1ec4f 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 erAll(db);. if(
1ec50 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61 pOp->p2 || DbHa
1ec51 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 75 2e sProperty(db, u.
1ec52 62 75 2e 69 44 62 2c 20 44 42 5f 53 63 68 65 6d bu.iDb, DB_Schem
1ec53 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 aLoaded) ){.
1ec54 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20 3d 20 53 u.bu.zMaster = S
1ec55 43 48 45 4d 41 5f 54 41 42 4c 45 28 75 2e 62 75 CHEMA_TABLE(u.bu
1ec56 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e 62 75 2e .iDb);. u.bu.
1ec57 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 initData.db = db
1ec58 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 ;. u.bu.initD
1ec59 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 ata.iDb = pOp->p
1ec5a 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 1;. u.bu.init
1ec5b 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 Data.pzErrMsg =
1ec5c 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 &p->zErrMsg;.
1ec5d 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20 73 71 6c u.bu.zSql = sql
1ec5e 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a ite3MPrintf(db,.
1ec5f 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e "SELECT n
1ec60 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 ame, rootpage, s
1ec61 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 ql FROM '%q'.%s
1ec62 57 48 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 WHERE %s",.
1ec63 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e 69 db->aDb[u.bu.i
1ec64 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62 75 2e Db].zName, u.bu.
1ec65 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 zMaster, pOp->p4
1ec66 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 .z);. if( u.b
1ec67 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 u.zSql==0 ){.
1ec68 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
1ec69 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b OMEM;. }else{
1ec6a 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c . (void)sql
1ec6b 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1ec6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1ec6d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d db->init.busy==
1ec6e 30 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 0 );. db->i
1ec6f 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 nit.busy = 1;.
1ec70 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 u.bu.initDat
1ec71 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b a.rc = SQLITE_OK
1ec72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ec73 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
1ec74 64 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 d );. rc =
1ec75 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c sqlite3_exec(db,
1ec76 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73 71 6c 69 u.bu.zSql, sqli
1ec77 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c te3InitCallback,
1ec78 20 26 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2c &u.bu.initData,
1ec79 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
1ec7a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c==SQLITE_OK ) r
1ec7b 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 c = u.bu.initDat
1ec7c 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 a.rc;. sqli
1ec7d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e te3DbFree(db, u.
1ec7e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 bu.zSql);.
1ec7f 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 db->init.busy =
1ec80 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 0;. (void)s
1ec81 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1ec82 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 b);. }. }.
1ec83 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1ec84 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 eAll(db);. if(
1ec85 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1ec86 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f ){. goto no_
1ec87 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b mem;. }. break
1ec88 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 ;.}..#if !define
1ec89 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e d(SQLITE_OMIT_AN
1ec8a 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f 64 65 ALYZE)./* Opcode
1ec8b 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 : LoadAnalysis P
1ec8c 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1ec8d 52 65 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f Read the sqlite_
1ec8e 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 stat1 table for
1ec8f 64 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 database P1 and
1ec90 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 load the content
1ec91 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c .** of that tabl
1ec92 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 e into the inter
1ec93 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 nal index hash t
1ec94 61 62 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c able. This will
1ec95 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e cause.** the an
1ec96 61 6c 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 alysis to be use
1ec97 64 20 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 d when preparing
1ec98 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 all subsequent
1ec99 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 queries..*/.case
1ec9a 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 OP_LoadAnalysis
1ec9b 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f : {. assert( pO
1ec9c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1ec9d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 >p1<db->nDb );.
1ec9e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 rc = sqlite3Ana
1ec9f 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 70 4f lysisLoad(db, pO
1eca0 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65 61 6b 3b p->p1);. break;
1eca1 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 .}.#endif /* !
1eca2 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1eca3 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 2a 2f 0a MIT_ANALYZE) */.
1eca4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 ./* Opcode: Drop
1eca5 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 Table P1 * * P4
1eca6 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 *.**.** Remove t
1eca7 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d he internal (in-
1eca8 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 memory) data str
1eca9 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 uctures that des
1ecaa 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 cribe.** the tab
1ecab 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 le named P4 in d
1ecac 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 atabase P1. Thi
1ecad 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 s is called afte
1ecae 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 r a table.** is
1ecaf 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1ecb0 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1ecb1 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1ecb2 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1ecb3 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1ecb4 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1ecb5 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1ecb6 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 P_DropTable: {.
1ecb7 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e sqlite3UnlinkAn
1ecb8 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c dDeleteTable(db,
1ecb9 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1ecba 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 4.z);. break;.}
1ecbb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f ../* Opcode: Dro
1ecbc 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 pIndex P1 * * P4
1ecbd 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 *.**.** Remove
1ecbe 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e the internal (in
1ecbf 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 -memory) data st
1ecc0 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 ructures that de
1ecc1 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e scribe.** the in
1ecc2 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 dex named P4 in
1ecc3 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 database P1. Th
1ecc4 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 is is called aft
1ecc5 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 er an index.** i
1ecc6 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 s dropped in ord
1ecc7 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 er to keep the i
1ecc8 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
1ecc9 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a tation of the.**
1ecca 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 schema consiste
1eccb 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 nt with what is
1eccc 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 on disk..*/.case
1eccd 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b OP_DropIndex: {
1ecce 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1eccf 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 AndDeleteIndex(d
1ecd0 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d b, pOp->p1, pOp-
1ecd1 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b >p4.z);. break;
1ecd2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 .}../* Opcode: D
1ecd3 72 6f 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 ropTrigger P1 *
1ecd4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d * P4 *.**.** Rem
1ecd5 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ove the internal
1ecd6 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 (in-memory) dat
1ecd7 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 a structures tha
1ecd8 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 t describe.** th
1ecd9 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 e trigger named
1ecda 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 P4 in database P
1ecdb 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 1. This is call
1ecdc 65 64 20 61 66 74 65 72 20 61 20 74 72 69 67 67 ed after a trigg
1ecdd 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 er.** is dropped
1ecde 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1ecdf 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 p the internal r
1ece0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1ece1 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 the.** schema c
1ece2 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 onsistent with w
1ece3 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a hat is on disk..
1ece4 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 */.case OP_DropT
1ece5 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 rigger: {. sqli
1ece6 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
1ece7 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f teTrigger(db, pO
1ece8 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a p->p1, pOp->p4.z
1ece9 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a );. break;.}...
1ecea 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1eceb 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
1ecec 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 ECK./* Opcode: I
1eced 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 ntegrityCk P1 P2
1ecee 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 P3 * P5.**.** D
1ecef 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 o an analysis of
1ecf0 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f the currently o
1ecf1 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 pen database. S
1ecf2 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 tore in.** regis
1ecf3 74 65 72 20 50 31 20 74 68 65 20 74 65 78 74 20 ter P1 the text
1ecf4 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 of an error mess
1ecf5 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 age describing a
1ecf6 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 ny problems..**
1ecf7 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 If no problems a
1ecf8 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 re found, store
1ecf9 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 a NULL in regist
1ecfa 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 er P1..**.** The
1ecfb 20 72 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e register P3 con
1ecfc 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 tains the maximu
1ecfd 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f m number of allo
1ecfe 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 wed errors..** A
1ecff 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 t most reg(P3) e
1ed00 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 rrors will be re
1ed01 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 ported..** In ot
1ed02 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 her words, the a
1ed03 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 nalysis stops as
1ed04 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 soon as reg(P1)
1ed05 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 errors are .**
1ed06 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 seen. Reg(P1) i
1ed07 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 s updated with t
1ed08 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 he number of err
1ed09 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a ors remaining..*
1ed0a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 *.** The root pa
1ed0b 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c ge numbers of al
1ed0c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 l tables in the
1ed0d 64 61 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 database are int
1ed0e 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 eger.** stored i
1ed0f 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 n reg(P1), reg(P
1ed10 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 1+1), reg(P1+2),
1ed11 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 .... There are
1ed12 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f P2 tables.** to
1ed13 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 tal..**.** If P5
1ed14 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
1ed15 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 e check is done
1ed16 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 on the auxiliary
1ed17 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
1ed18 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 e, not the main
1ed19 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1ed1a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 *.** This opcode
1ed1b 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c is used to impl
1ed1c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 ement the integr
1ed1d 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 ity_check pragma
1ed1e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 ..*/.case OP_Int
1ed1f 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23 69 66 20 egrityCk: {.#if
1ed20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1ed21 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ed22 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e 74 20 6e u.bv */. int n
1ed23 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 Root; /* Nu
1ed24 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 mber of tables t
1ed25 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 o check. (Numbe
1ed26 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e r of root pages.
1ed27 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f ) */. int *aRoo
1ed28 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 t; /* Array
1ed29 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 of rootpage numb
1ed2a 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 ers for tables t
1ed2b 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a o be checked */.
1ed2c 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 int j;
1ed2d 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1ed2e 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b r */. int nErr;
1ed2f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1ed30 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 of errors repor
1ed31 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ted */. char *z
1ed32 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 ; /* Text
1ed33 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 of the error re
1ed34 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 port */. Mem *p
1ed35 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 nErr; /* Reg
1ed36 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 ister keeping tr
1ed37 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 ack of errors re
1ed38 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 maining */.#endi
1ed39 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1ed3a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ed3b 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e 62 76 2e u.bv */.. u.bv.
1ed3c 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b nRoot = pOp->p2;
1ed3d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e . assert( u.bv.
1ed3e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 2e 62 nRoot>0 );. u.b
1ed3f 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 v.aRoot = sqlite
1ed40 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1ed41 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e sizeof(int)*(u.
1ed42 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 bv.nRoot+1) );.
1ed43 20 69 66 28 20 75 2e 62 76 2e 61 52 6f 6f 74 3d if( u.bv.aRoot=
1ed44 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1ed45 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1ed46 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1ed47 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 <=p->nMem );. u
1ed48 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26 61 4d 65 .bv.pnErr = &aMe
1ed49 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 m[pOp->p3];. as
1ed4a 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72 sert( (u.bv.pnEr
1ed4b 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 r->flags & MEM_I
1ed4c 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 nt)!=0 );. asse
1ed4d 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72 72 2d rt( (u.bv.pnErr-
1ed4e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 >flags & (MEM_St
1ed4f 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 r|MEM_Blob))==0
1ed50 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 );. pIn1 = &aMe
1ed51 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f m[pOp->p1];. fo
1ed52 72 28 75 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 r(u.bv.j=0; u.bv
1ed53 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 .j<u.bv.nRoot; u
1ed54 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e .bv.j++){. u.
1ed55 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d bv.aRoot[u.bv.j]
1ed56 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 = (int)sqlite3V
1ed57 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e dbeIntValue(&pIn
1ed58 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 1[u.bv.j]);. }.
1ed59 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 u.bv.aRoot[u.b
1ed5a 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 v.j] = 0;. asse
1ed5b 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e rt( pOp->p5<db->
1ed5c 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
1ed5d 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1ed5e 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d (1<<pOp->p5))!=
1ed5f 30 20 29 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 0 );. u.bv.z =
1ed60 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 sqlite3BtreeInte
1ed61 67 72 69 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 grityCheck(db->a
1ed62 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c Db[pOp->p5].pBt,
1ed63 20 75 2e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 u.bv.aRoot, u.b
1ed64 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 v.nRoot,.
1ed65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed66 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 75 (int)u
1ed67 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 .bv.pnErr->u.i,
1ed68 26 75 2e 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73 &u.bv.nErr);. s
1ed69 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ed6a 20 75 2e 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 u.bv.aRoot);.
1ed6b 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 u.bv.pnErr->u.i
1ed6c 2d 3d 20 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 -= u.bv.nErr;.
1ed6d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ed6e 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 tNull(pIn1);. i
1ed6f 66 28 20 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 f( u.bv.nErr==0
1ed70 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 ){. assert( u
1ed71 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 .bv.z==0 );. }e
1ed72 6c 73 65 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d lse if( u.bv.z==
1ed73 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 0 ){. goto no
1ed74 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 _mem;. }else{.
1ed75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1ed76 6d 53 65 74 53 74 72 28 70 49 6e 31 2c 20 75 2e mSetStr(pIn1, u.
1ed77 62 76 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 bv.z, -1, SQLITE
1ed78 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 _UTF8, sqlite3_f
1ed79 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 ree);. }. UPDA
1ed7a 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1ed7b 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 pIn1);. sqlite3
1ed7c 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1ed7d 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e ng(pIn1, encodin
1ed7e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 g);. break;.}.#
1ed7f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ed80 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
1ed81 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f HECK */../* Opco
1ed82 64 65 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31 de: RowSetAdd P1
1ed83 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1ed84 49 6e 73 65 72 74 20 74 68 65 20 69 6e 74 65 67 Insert the integ
1ed85 65 72 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79 er value held by
1ed86 20 72 65 67 69 73 74 65 72 20 50 32 20 69 6e 74 register P2 int
1ed87 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 o a boolean inde
1ed88 78 0a 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 x.** held in reg
1ed89 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 ister P1..**.**
1ed8a 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 An assertion fai
1ed8b 6c 73 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20 ls if P2 is not
1ed8c 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 an integer..*/.c
1ed8d 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 ase OP_RowSetAdd
1ed8e 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 : { /* in1
1ed8f 2c 20 69 6e 32 20 2a 2f 0a 20 20 70 49 6e 31 20 , in2 */. pIn1
1ed90 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d = &aMem[pOp->p1]
1ed91 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d ;. pIn2 = &aMem
1ed92 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 [pOp->p2];. ass
1ed93 65 72 74 28 20 28 70 49 6e 32 2d 3e 66 6c 61 67 ert( (pIn2->flag
1ed94 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 s & MEM_Int)!=0
1ed95 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e );. if( (pIn1->
1ed96 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1ed97 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 et)==0 ){. sq
1ed98 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 lite3VdbeMemSetR
1ed99 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 owSet(pIn1);.
1ed9a 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1ed9b 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
1ed9c 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1ed9d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 ;. }. sqlite3R
1ed9e 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 6e 31 owSetInsert(pIn1
1ed9f 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e ->u.pRowSet, pIn
1eda0 32 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b 2->u.i);. break
1eda1 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1eda2 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32 RowSetRead P1 P2
1eda3 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 P3 * *.**.** Ex
1eda4 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 tract the smalle
1eda5 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f st value from bo
1eda6 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61 olean index P1 a
1eda7 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75 nd put that valu
1eda8 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 e into.** regist
1eda9 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 er P3. Or, if b
1edaa 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 oolean index P1
1edab 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 is initially emp
1edac 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20 ty, leave P3.**
1edad 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 unchanged and ju
1edae 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1edaf 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P2..*/.case OP
1edb0 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20 _RowSetRead: {
1edb1 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1edb2 31 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 1, out3 */.#if 0
1edb3 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1edb4 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1edb5 75 2e 62 77 20 2a 2f 0a 20 20 69 36 34 20 76 61 u.bw */. i64 va
1edb6 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 l;.#endif /* loc
1edb7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1edb8 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a ed into u.bw */.
1edb9 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1edba 52 52 55 50 54 3b 0a 20 20 70 49 6e 31 20 3d 20 RRUPT;. pIn1 =
1edbb 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1edbc 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1edbd 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
1edbe 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 ==0. || sqlite
1edbf 33 52 6f 77 53 65 74 4e 65 78 74 28 70 49 6e 31 3RowSetNext(pIn1
1edc0 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75 2e ->u.pRowSet, &u.
1edc1 62 77 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a bw.val)==0. ){.
1edc2 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 /* The boole
1edc3 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 an index is empt
1edc4 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 y */. sqlite3
1edc5 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
1edc6 49 6e 31 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 In1);. pc = p
1edc7 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 Op->p2 - 1;. }e
1edc8 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61 lse{. /* A va
1edc9 6c 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20 66 lue was pulled f
1edca 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a 2f rom the index */
1edcb 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1edcc 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 61 4d 65 MemSetInt64(&aMe
1edcd 6d 5b 70 4f 70 2d 3e 70 33 5d 2c 20 75 2e 62 77 m[pOp->p3], u.bw
1edce 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65 .val);. }. bre
1edcf 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1edd0 3a 20 52 6f 77 53 65 74 54 65 73 74 20 50 31 20 : RowSetTest P1
1edd1 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52 P2 P3 P4.**.** R
1edd2 65 67 69 73 74 65 72 20 50 33 20 69 73 20 61 73 egister P3 is as
1edd3 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64 20 61 20 sumed to hold a
1edd4 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 64-bit integer v
1edd5 61 6c 75 65 2e 20 49 66 20 72 65 67 69 73 74 65 alue. If registe
1edd6 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 r P1.** contains
1edd7 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
1edd8 20 61 6e 64 20 74 68 61 74 20 52 6f 77 53 65 74 and that RowSet
1edd9 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 object contains
1edda 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 68 65 .** the value he
1eddb 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74 ld in P3, jump t
1eddc 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 4f o register P2. O
1eddd 74 68 65 72 77 69 73 65 2c 20 69 6e 73 65 72 74 therwise, insert
1edde 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 the.** integer
1eddf 69 6e 20 50 33 20 69 6e 74 6f 20 74 68 65 20 52 in P3 into the R
1ede0 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e owSet and contin
1ede1 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 ue on to the.**
1ede2 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a next opcode..**.
1ede3 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 ** The RowSet ob
1ede4 6a 65 63 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 ject is optimize
1ede5 64 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 d for the case w
1ede6 68 65 72 65 20 73 75 63 63 65 73 73 69 76 65 20 here successive
1ede7 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 sets.** of integ
1ede8 65 72 73 2c 20 77 68 65 72 65 20 65 61 63 68 20 ers, where each
1ede9 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 set contains no
1edea 64 75 70 6c 69 63 61 74 65 73 2e 20 45 61 63 68 duplicates. Each
1edeb 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65 set.** of value
1edec 73 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 s is identified
1eded 62 79 20 61 20 75 6e 69 71 75 65 20 50 34 20 76 by a unique P4 v
1edee 61 6c 75 65 2e 20 54 68 65 20 66 69 72 73 74 20 alue. The first
1edef 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 set.** must have
1edf0 20 50 34 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61 P4==0, the fina
1edf1 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20 20 50 34 l set P4=-1. P4
1edf2 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
1edf3 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67 -1 or.** non-neg
1edf4 61 74 69 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d ative. For non-
1edf5 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 20 negative values
1edf6 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c of P4 only the l
1edf7 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74 73 20 61 ower 4.** bits a
1edf8 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a re significant..
1edf9 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77 **.** This allow
1edfa 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a s optimizations:
1edfb 20 28 61 29 20 77 68 65 6e 20 50 34 3d 3d 30 20 (a) when P4==0
1edfc 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1edfd 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 65 20 to test.** the
1edfe 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 66 6f rowset object fo
1edff 72 20 50 33 2c 20 61 73 20 69 74 20 69 73 20 67 r P3, as it is g
1ee00 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f uaranteed not to
1ee01 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20 contain it,.**
1ee02 28 62 29 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20 (b) when P4==-1
1ee03 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1ee04 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76 to insert the v
1ee05 61 6c 75 65 2c 20 61 73 20 69 74 20 77 69 6c 6c alue, as it will
1ee06 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 74 65 73 .** never be tes
1ee07 74 65 64 20 66 6f 72 2c 20 61 6e 64 20 28 63 29 ted for, and (c)
1ee08 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 74 68 when a value th
1ee09 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 73 65 at is part of se
1ee0a 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74 t X is.** insert
1ee0b 65 64 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ed, there is no
1ee0c 6e 65 65 64 20 74 6f 20 73 65 61 72 63 68 20 74 need to search t
1ee0d 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 61 6d o see if the sam
1ee0e 65 20 76 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70 e value was.** p
1ee0f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 reviously insert
1ee10 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 65 ed as part of se
1ee11 74 20 58 20 28 6f 6e 6c 79 20 69 66 20 69 74 20 t X (only if it
1ee12 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
1ee13 2a 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 * inserted as pa
1ee14 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72 rt of some other
1ee15 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f set)..*/.case O
1ee16 50 5f 52 6f 77 53 65 74 54 65 73 74 3a 20 7b 20 P_RowSetTest: {
1ee17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee18 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
1ee19 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1ee1a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ee1b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ee1c 62 78 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74 bx */. int iSet
1ee1d 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a ;. int exists;.
1ee1e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ee1f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ee20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 0a 20 20 into u.bx */..
1ee21 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1ee22 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 20 3d 20 ->p1];. pIn3 =
1ee23 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1ee24 20 20 75 2e 62 78 2e 69 53 65 74 20 3d 20 70 4f u.bx.iSet = pO
1ee25 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72 p->p4.i;. asser
1ee26 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d t( pIn3->flags&M
1ee27 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 EM_Int );.. /*
1ee28 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 74 If there is anyt
1ee29 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 hing other than
1ee2a 61 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 a rowset object
1ee2b 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 in memory cell P
1ee2c 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69 1,. ** delete i
1ee2d 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61 t now and initia
1ee2e 6c 69 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20 lize P1 with an
1ee2f 65 6d 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a empty rowset. *
1ee30 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 /. if( (pIn1->f
1ee31 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
1ee32 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c t)==0 ){. sql
1ee33 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f ite3VdbeMemSetRo
1ee34 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20 wSet(pIn1);.
1ee35 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1ee36 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
1ee37 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0 ) goto no_mem;
1ee38 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1ee39 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1ee3a 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 INT32 );. asser
1ee3b 74 28 20 75 2e 62 78 2e 69 53 65 74 3d 3d 2d 31 t( u.bx.iSet==-1
1ee3c 20 7c 7c 20 75 2e 62 78 2e 69 53 65 74 3e 3d 30 || u.bx.iSet>=0
1ee3d 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 78 2e 69 );. if( u.bx.i
1ee3e 53 65 74 20 29 7b 0a 20 20 20 20 75 2e 62 78 2e Set ){. u.bx.
1ee3f 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 65 33 exists = sqlite3
1ee40 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e 31 2d RowSetTest(pIn1-
1ee41 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20 20 20 20 >u.pRowSet,.
1ee42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee43 20 20 20 20 20 20 20 20 20 20 20 28 75 38 29 28 (u8)(
1ee44 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20 3f 20 75 u.bx.iSet>=0 ? u
1ee45 2e 62 78 2e 69 53 65 74 20 26 20 30 78 66 20 3a .bx.iSet & 0xf :
1ee46 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20 20 20 0xff),.
1ee47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee48 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 pIn3->u.i
1ee49 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 78 2e );. if( u.bx.
1ee4a 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 exists ){.
1ee4b 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1ee4c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1ee4d 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 75 }. }. if( u
1ee4e 2e 62 78 2e 69 53 65 74 3e 3d 30 20 29 7b 0a 20 .bx.iSet>=0 ){.
1ee4f 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 sqlite3RowSet
1ee50 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 Insert(pIn1->u.p
1ee51 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e RowSet, pIn3->u.
1ee52 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b i);. }. break;
1ee53 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .}...#ifndef SQL
1ee54 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1ee55 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 6f ../* Opcode: Pro
1ee56 67 72 61 6d 20 50 31 20 50 32 20 50 33 20 50 34 gram P1 P2 P3 P4
1ee57 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 *.**.** Execute
1ee58 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f the trigger pro
1ee59 67 72 61 6d 20 70 61 73 73 65 64 20 61 73 20 50 gram passed as P
1ee5a 34 20 28 74 79 70 65 20 50 34 5f 53 55 42 50 52 4 (type P4_SUBPR
1ee5b 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a 20 50 OGRAM). .**.** P
1ee5c 31 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 1 contains the a
1ee5d 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6d 65 ddress of the me
1ee5e 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 mory cell that c
1ee5f 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 72 73 ontains the firs
1ee60 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c t memory .** cel
1ee61 6c 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 l in an array of
1ee62 20 76 61 6c 75 65 73 20 75 73 65 64 20 61 73 20 values used as
1ee63 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
1ee64 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 50 32 sub-program. P2
1ee65 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 .** contains th
1ee66 65 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d e address to jum
1ee67 70 20 74 6f 20 69 66 20 74 68 65 20 73 75 62 2d p to if the sub-
1ee68 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 program throws a
1ee69 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20 65 78 63 n IGNORE .** exc
1ee6a 65 70 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 eption using the
1ee6b 20 52 41 49 53 45 28 29 20 66 75 6e 63 74 69 6f RAISE() functio
1ee6c 6e 2e 20 52 65 67 69 73 74 65 72 20 50 33 20 63 n. Register P3 c
1ee6d 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 ontains the addr
1ee6e 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20 6d 65 6d ess .** of a mem
1ee6f 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 69 73 ory cell in this
1ee70 20 28 74 68 65 20 70 61 72 65 6e 74 29 20 56 4d (the parent) VM
1ee71 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f that is used to
1ee72 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 0a 2a allocate the .*
1ee73 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 * memory require
1ee74 64 20 62 79 20 74 68 65 20 73 75 62 2d 76 64 62 d by the sub-vdb
1ee75 65 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2a e at runtime..**
1ee76 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1ee77 74 65 72 20 74 6f 20 74 68 65 20 56 4d 20 63 6f ter to the VM co
1ee78 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 ntaining the tri
1ee79 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f gger program..*/
1ee7a 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67 72 61 6d .case OP_Program
1ee7b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1ee7c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 mp */.#if 0 /*
1ee7d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ee7e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 moved into u.by
1ee7f 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 */. int nMem;
1ee80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ee81 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 Number of memory
1ee82 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 registers for s
1ee83 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 ub-program */.
1ee84 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 int nByte;
1ee85 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
1ee86 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 of runtime spac
1ee87 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 e required for s
1ee88 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 ub-program */.
1ee89 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20 20 20 Mem *pRt;
1ee8a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
1ee8b 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ter to allocate
1ee8c 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 2a 2f runtime space */
1ee8d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 . Mem *pMem;
1ee8e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
1ee8f 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 ed to iterate th
1ee90 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c rough memory cel
1ee91 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 45 6e ls */. Mem *pEn
1ee92 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
1ee93 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 /* Last memory c
1ee94 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 ell in new array
1ee95 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 */. VdbeFrame
1ee96 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a *pFrame; /*
1ee97 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 New vdbe frame
1ee98 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f to execute in */
1ee99 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 . SubProgram *p
1ee9a 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75 Program; /* Su
1ee9b 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 b-program to exe
1ee9c 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a cute */. void *
1ee9d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1ee9e 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 /* Token ident
1ee9f 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a ifying trigger *
1eea0 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1eea1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1eea2 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 0a d into u.by */..
1eea3 20 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 20 u.by.pProgram
1eea4 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 = pOp->p4.pProgr
1eea5 61 6d 3b 0a 20 20 75 2e 62 79 2e 70 52 74 20 3d am;. u.by.pRt =
1eea6 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b &aMem[pOp->p3];
1eea7 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e . assert( u.by.
1eea8 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3e 30 20 pProgram->nOp>0
1eea9 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1eeaa 70 35 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 p5 flag is clear
1eeab 2c 20 74 68 65 6e 20 72 65 63 75 72 73 69 76 65 , then recursive
1eeac 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 invocation of t
1eead 72 69 67 67 65 72 73 20 69 73 0a 20 20 2a 2a 20 riggers is. **
1eeae 64 69 73 61 62 6c 65 64 20 66 6f 72 20 62 61 63 disabled for bac
1eeaf 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
1eeb0 6c 69 74 79 20 28 70 35 20 69 73 20 73 65 74 20 lity (p5 is set
1eeb1 69 66 20 74 68 69 73 20 73 75 62 2d 70 72 6f 67 if this sub-prog
1eeb2 72 61 6d 0a 20 20 2a 2a 20 69 73 20 72 65 61 6c ram. ** is real
1eeb3 6c 79 20 61 20 74 72 69 67 67 65 72 2c 20 6e 6f ly a trigger, no
1eeb4 74 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 t a foreign key
1eeb5 61 63 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20 action, and the
1eeb6 66 6c 61 67 20 73 65 74 0a 20 20 2a 2a 20 61 6e flag set. ** an
1eeb7 64 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 d cleared by the
1eeb8 20 22 50 52 41 47 4d 41 20 72 65 63 75 72 73 69 "PRAGMA recursi
1eeb9 76 65 5f 74 72 69 67 67 65 72 73 22 20 63 6f 6d ve_triggers" com
1eeba 6d 61 6e 64 20 69 73 20 63 6c 65 61 72 29 2e 0a mand is clear)..
1eebb 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 **. ** It is
1eebc 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61 recursive invoca
1eebd 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73 tion of triggers
1eebe 2c 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 , at the SQL lev
1eebf 65 6c 2c 20 74 68 61 74 20 69 73 0a 20 20 2a 2a el, that is. **
1eec0 20 64 69 73 61 62 6c 65 64 2e 20 49 6e 20 73 6f disabled. In so
1eec1 6d 65 20 63 61 73 65 73 20 61 20 73 69 6e 67 6c me cases a singl
1eec2 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 67 65 e trigger may ge
1eec3 6e 65 72 61 74 65 20 6d 6f 72 65 20 74 68 61 6e nerate more than
1eec4 20 6f 6e 65 0a 20 20 2a 2a 20 53 75 62 50 72 6f one. ** SubPro
1eec5 67 72 61 6d 20 28 69 66 20 74 68 65 20 74 72 69 gram (if the tri
1eec6 67 67 65 72 20 6d 61 79 20 62 65 20 65 78 65 63 gger may be exec
1eec7 75 74 65 64 20 77 69 74 68 20 6d 6f 72 65 20 74 uted with more t
1eec8 68 61 6e 20 6f 6e 65 20 64 69 66 66 65 72 65 6e han one differen
1eec9 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 t. ** ON CONFLI
1eeca 43 54 20 61 6c 67 6f 72 69 74 68 6d 29 2e 20 53 CT algorithm). S
1eecb 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 ubProgram struct
1eecc 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ures associated
1eecd 77 69 74 68 20 61 0a 20 20 2a 2a 20 73 69 6e 67 with a. ** sing
1eece 6c 65 20 74 72 69 67 67 65 72 20 61 6c 6c 20 68 le trigger all h
1eecf 61 76 65 20 74 68 65 20 73 61 6d 65 20 76 61 6c ave the same val
1eed0 75 65 20 66 6f 72 20 74 68 65 20 53 75 62 50 72 ue for the SubPr
1eed1 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a 20 20 2a 2a ogram.token. **
1eed2 20 76 61 72 69 61 62 6c 65 2e 20 20 2a 2f 0a 20 variable. */.
1eed3 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a if( pOp->p5 ){.
1eed4 20 20 20 20 75 2e 62 79 2e 74 20 3d 20 75 2e 62 u.by.t = u.b
1eed5 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 y.pProgram->toke
1eed6 6e 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 79 2e n;. for(u.by.
1eed7 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 pFrame=p->pFrame
1eed8 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65 20 26 26 ; u.by.pFrame &&
1eed9 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 74 6f u.by.pFrame->to
1eeda 6b 65 6e 21 3d 75 2e 62 79 2e 74 3b 20 75 2e 62 ken!=u.by.t; u.b
1eedb 79 2e 70 46 72 61 6d 65 3d 75 2e 62 79 2e 70 46 y.pFrame=u.by.pF
1eedc 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a rame->pParent);.
1eedd 20 20 20 20 69 66 28 20 75 2e 62 79 2e 70 46 72 if( u.by.pFr
1eede 61 6d 65 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d ame ) break;. }
1eedf 0a 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 61 6d .. if( p->nFram
1eee0 65 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 e>=db->aLimit[SQ
1eee1 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 LITE_LIMIT_TRIGG
1eee2 45 52 5f 44 45 50 54 48 5d 20 29 7b 0a 20 20 20 ER_DEPTH] ){.
1eee3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1eee4 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 OR;. sqlite3S
1eee5 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1eee6 72 4d 73 67 2c 20 64 62 2c 20 22 74 6f 6f 20 6d rMsg, db, "too m
1eee7 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72 any levels of tr
1eee8 69 67 67 65 72 20 72 65 63 75 72 73 69 6f 6e 22 igger recursion"
1eee9 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 );. break;.
1eeea 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 }.. /* Register
1eeeb 20 75 2e 62 79 2e 70 52 74 20 69 73 20 75 73 65 u.by.pRt is use
1eeec 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6d d to store the m
1eeed 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 74 emory required t
1eeee 6f 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65 o save the state
1eeef 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 . ** of the cur
1eef0 72 65 6e 74 20 70 72 6f 67 72 61 6d 2c 20 61 6e rent program, an
1eef1 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 d the memory req
1eef2 75 69 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65 uired at runtime
1eef3 20 74 6f 20 65 78 65 63 75 74 65 0a 20 20 2a 2a to execute. **
1eef4 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f the trigger pro
1eef5 67 72 61 6d 2e 20 49 66 20 74 68 69 73 20 74 72 gram. If this tr
1eef6 69 67 67 65 72 20 68 61 73 20 62 65 65 6e 20 66 igger has been f
1eef7 69 72 65 64 20 62 65 66 6f 72 65 2c 20 74 68 65 ired before, the
1eef8 6e 20 75 2e 62 79 2e 70 52 74 0a 20 20 2a 2a 20 n u.by.pRt. **
1eef9 69 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 is already alloc
1eefa 61 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c ated. Otherwise,
1eefb 20 69 74 20 6d 75 73 74 20 62 65 20 69 6e 69 74 it must be init
1eefc 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 69 ialized. */. i
1eefd 66 28 20 28 75 2e 62 79 2e 70 52 74 2d 3e 66 6c f( (u.by.pRt->fl
1eefe 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 29 3d 3d ags&MEM_Frame)==
1eeff 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 75 62 50 0 ){. /* SubP
1ef00 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20 69 73 20 73 rogram.nMem is s
1ef01 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
1ef02 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 of memory cells
1ef03 20 75 73 65 64 20 62 79 20 74 68 65 0a 20 20 20 used by the.
1ef04 20 2a 2a 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 ** program stor
1ef05 65 64 20 69 6e 20 53 75 62 50 72 6f 67 72 61 6d ed in SubProgram
1ef06 2e 61 4f 70 2e 20 41 73 20 77 65 6c 6c 20 61 73 .aOp. As well as
1ef07 20 74 68 65 73 65 2c 20 6f 6e 65 20 6d 65 6d 6f these, one memo
1ef08 72 79 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 69 ry. ** cell i
1ef09 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 s required for e
1ef0a 61 63 68 20 63 75 72 73 6f 72 20 75 73 65 64 20 ach cursor used
1ef0b 62 79 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 by the program.
1ef0c 53 65 74 20 6c 6f 63 61 6c 0a 20 20 20 20 2a 2a Set local. **
1ef0d 20 76 61 72 69 61 62 6c 65 20 75 2e 62 79 2e 6e variable u.by.n
1ef0e 4d 65 6d 20 28 61 6e 64 20 6c 61 74 65 72 2c 20 Mem (and later,
1ef0f 56 64 62 65 46 72 61 6d 65 2e 6e 43 68 69 6c 64 VdbeFrame.nChild
1ef10 4d 65 6d 29 20 74 6f 20 74 68 69 73 20 76 61 6c Mem) to this val
1ef11 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 ue.. */. u
1ef12 2e 62 79 2e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e .by.nMem = u.by.
1ef13 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 2b pProgram->nMem +
1ef14 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e u.by.pProgram->
1ef15 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62 79 2e 6e nCsr;. u.by.n
1ef16 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 Byte = ROUND8(si
1ef17 7a 65 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 zeof(VdbeFrame))
1ef18 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b . +
1ef19 20 75 2e 62 79 2e 6e 4d 65 6d 20 2a 20 73 69 7a u.by.nMem * siz
1ef1a 65 6f 66 28 4d 65 6d 29 0a 20 20 20 20 20 20 20 eof(Mem).
1ef1b 20 20 20 20 20 20 20 2b 20 75 2e 62 79 2e 70 50 + u.by.pP
1ef1c 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 2a 20 73 rogram->nCsr * s
1ef1d 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1ef1e 20 2a 29 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 *);. u.by.pF
1ef1f 72 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 rame = sqlite3Db
1ef20 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 75 MallocZero(db, u
1ef21 2e 62 79 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 .by.nByte);.
1ef22 69 66 28 20 21 75 2e 62 79 2e 70 46 72 61 6d 65 if( !u.by.pFrame
1ef23 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
1ef24 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
1ef25 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1ef26 65 6c 65 61 73 65 28 75 2e 62 79 2e 70 52 74 29 elease(u.by.pRt)
1ef27 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52 74 2d 3e ;. u.by.pRt->
1ef28 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 46 72 61 6d flags = MEM_Fram
1ef29 65 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52 74 2d e;. u.by.pRt-
1ef2a 3e 75 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79 >u.pFrame = u.by
1ef2b 2e 70 46 72 61 6d 65 3b 0a 0a 20 20 20 20 75 2e .pFrame;.. u.
1ef2c 62 79 2e 70 46 72 61 6d 65 2d 3e 76 20 3d 20 70 by.pFrame->v = p
1ef2d 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d ;. u.by.pFram
1ef2e 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 3d 20 75 e->nChildMem = u
1ef2f 2e 62 79 2e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e .by.nMem;. u.
1ef30 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c by.pFrame->nChil
1ef31 64 43 73 72 20 3d 20 75 2e 62 79 2e 70 50 72 6f dCsr = u.by.pPro
1ef32 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 gram->nCsr;.
1ef33 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 63 20 u.by.pFrame->pc
1ef34 3d 20 70 63 3b 0a 20 20 20 20 75 2e 62 79 2e 70 = pc;. u.by.p
1ef35 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 3d 20 70 2d Frame->aMem = p-
1ef36 3e 61 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 79 2e >aMem;. u.by.
1ef37 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3d 20 70 pFrame->nMem = p
1ef38 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 79 ->nMem;. u.by
1ef39 2e 70 46 72 61 6d 65 2d 3e 61 70 43 73 72 20 3d .pFrame->apCsr =
1ef3a 20 70 2d 3e 61 70 43 73 72 3b 0a 20 20 20 20 75 p->apCsr;. u
1ef3b 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 75 72 .by.pFrame->nCur
1ef3c 73 6f 72 20 3d 20 70 2d 3e 6e 43 75 72 73 6f 72 sor = p->nCursor
1ef3d 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d ;. u.by.pFram
1ef3e 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b e->aOp = p->aOp;
1ef3f 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 . u.by.pFrame
1ef40 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a ->nOp = p->nOp;.
1ef41 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d u.by.pFrame-
1ef42 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62 79 2e 70 50 >token = u.by.pP
1ef43 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 0a rogram->token;..
1ef44 20 20 20 20 75 2e 62 79 2e 70 45 6e 64 20 3d 20 u.by.pEnd =
1ef45 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e &VdbeFrameMem(u.
1ef46 62 79 2e 70 46 72 61 6d 65 29 5b 75 2e 62 79 2e by.pFrame)[u.by.
1ef47 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 pFrame->nChildMe
1ef48 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 79 m];. for(u.by
1ef49 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61 6d 65 4d .pMem=VdbeFrameM
1ef4a 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d 65 29 3b em(u.by.pFrame);
1ef4b 20 75 2e 62 79 2e 70 4d 65 6d 21 3d 75 2e 62 79 u.by.pMem!=u.by
1ef4c 2e 70 45 6e 64 3b 20 75 2e 62 79 2e 70 4d 65 6d .pEnd; u.by.pMem
1ef4d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 62 79 2e ++){. u.by.
1ef4e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1ef4f 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 75 2e M_Null;. u.
1ef50 62 79 2e 70 4d 65 6d 2d 3e 64 62 20 3d 20 64 62 by.pMem->db = db
1ef51 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1ef52 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 . u.by.pFrame
1ef53 20 3d 20 75 2e 62 79 2e 70 52 74 2d 3e 75 2e 70 = u.by.pRt->u.p
1ef54 46 72 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72 Frame;. asser
1ef55 74 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d t( u.by.pProgram
1ef56 2d 3e 6e 4d 65 6d 2b 75 2e 62 79 2e 70 50 72 6f ->nMem+u.by.pPro
1ef57 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 79 gram->nCsr==u.by
1ef58 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d .pFrame->nChildM
1ef59 65 6d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 em );. assert
1ef5a 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d ( u.by.pProgram-
1ef5b 3e 6e 43 73 72 3d 3d 75 2e 62 79 2e 70 46 72 61 >nCsr==u.by.pFra
1ef5c 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 29 3b me->nChildCsr );
1ef5d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3d . assert( pc=
1ef5e 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 63 =u.by.pFrame->pc
1ef5f 20 29 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 6e 46 );. }.. p->nF
1ef60 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e 62 79 2e 70 rame++;. u.by.p
1ef61 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 20 3d Frame->pParent =
1ef62 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e p->pFrame;. u.
1ef63 62 79 2e 70 46 72 61 6d 65 2d 3e 6c 61 73 74 52 by.pFrame->lastR
1ef64 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 owid = db->lastR
1ef65 6f 77 69 64 3b 0a 20 20 75 2e 62 79 2e 70 46 72 owid;. u.by.pFr
1ef66 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 ame->nChange = p
1ef67 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70 2d 3e ->nChange;. p->
1ef68 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 nChange = 0;. p
1ef69 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79 2e ->pFrame = u.by.
1ef6a 70 46 72 61 6d 65 3b 0a 20 20 70 2d 3e 61 4d 65 pFrame;. p->aMe
1ef6b 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56 64 62 65 m = aMem = &Vdbe
1ef6c 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79 2e 70 46 FrameMem(u.by.pF
1ef6d 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e rame)[-1];. p->
1ef6e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e 70 46 72 61 nMem = u.by.pFra
1ef6f 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20 me->nChildMem;.
1ef70 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 p->nCursor = (u
1ef71 31 36 29 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 16)u.by.pFrame->
1ef72 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e nChildCsr;. p->
1ef73 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 apCsr = (VdbeCur
1ef74 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e sor **)&aMem[p->
1ef75 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 4f nMem+1];. p->aO
1ef76 70 20 3d 20 61 4f 70 20 3d 20 75 2e 62 79 2e 70 p = aOp = u.by.p
1ef77 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20 Program->aOp;.
1ef78 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 79 2e 70 50 p->nOp = u.by.pP
1ef79 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 rogram->nOp;. p
1ef7a 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b c = -1;.. break
1ef7b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ef7c 50 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 Param P1 P2 * *
1ef7d 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 *.**.** This opc
1ef7e 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 ode is only ever
1ef7f 20 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d present in sub-
1ef80 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20 programs called
1ef81 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50 via the .** OP_P
1ef82 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 rogram instructi
1ef83 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65 on. Copy a value
1ef84 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 currently store
1ef85 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a d in a memory .*
1ef86 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61 * cell of the ca
1ef87 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66 lling (parent) f
1ef88 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20 rame to cell P2
1ef89 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 in the current f
1ef8a 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 rames .** addres
1ef8b 73 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73 s space. This is
1ef8c 20 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 used by trigger
1ef8d 20 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63 programs to acc
1ef8e 65 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a ess the new.* .*
1ef8f 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75 * and old.* valu
1ef90 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64 es..**.** The ad
1ef91 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c dress of the cel
1ef92 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 l in the parent
1ef93 66 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69 frame is determi
1ef94 6e 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a ned by adding.**
1ef95 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1ef96 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
1ef97 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1ef98 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
1ef99 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 the.** calling
1ef9a 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 OP_Program instr
1ef9b 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 uction..*/.case
1ef9c 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20 OP_Param: {
1ef9d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1ef9e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1ef9f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1efa0 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1efa1 20 75 2e 62 7a 20 2a 2f 0a 20 20 56 64 62 65 46 u.bz */. VdbeF
1efa2 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 rame *pFrame;.
1efa3 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66 Mem *pIn;.#endif
1efa4 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1efa5 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1efa6 2e 62 7a 20 2a 2f 0a 20 20 75 2e 62 7a 2e 70 46 .bz */. u.bz.pF
1efa7 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 rame = p->pFrame
1efa8 3b 0a 20 20 75 2e 62 7a 2e 70 49 6e 20 3d 20 26 ;. u.bz.pIn = &
1efa9 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 u.bz.pFrame->aMe
1efaa 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 62 7a m[pOp->p1 + u.bz
1efab 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 62 .pFrame->aOp[u.b
1efac 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31 z.pFrame->pc].p1
1efad 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ];. sqlite3Vdbe
1efae 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 MemShallowCopy(p
1efaf 4f 75 74 2c 20 75 2e 62 7a 2e 70 49 6e 2c 20 4d Out, u.bz.pIn, M
1efb0 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65 EM_Ephem);. bre
1efb1 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a ak;.}..#endif /*
1efb2 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f #ifndef SQLITE_
1efb3 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a OMIT_TRIGGER */.
1efb4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1efb5 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1efb6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f ./* Opcode: FkCo
1efb7 75 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20 unter P1 P2 * *
1efb8 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e *.**.** Incremen
1efb9 74 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 t a "constraint
1efba 63 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28 counter" by P2 (
1efbb 50 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 P2 may be negati
1efbc 76 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e ve or positive).
1efbd 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e .** If P1 is non
1efbe 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62 -zero, the datab
1efbf 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 ase constraint c
1efc0 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d ounter is increm
1efc1 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72 ented .** (defer
1efc2 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 red foreign key
1efc3 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 constraints). Ot
1efc4 68 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69 herwise, if P1 i
1efc5 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20 s zero, the .**
1efc6 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 statement counte
1efc7 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 r is incremented
1efc8 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 (immediate fore
1efc9 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ign key constrai
1efca 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 nts)..*/.case OP
1efcb 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20 _FkCounter: {.
1efcc 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 if( pOp->p1 ){.
1efcd 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 db->nDeferred
1efce 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b Cons += pOp->p2;
1efcf 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d . }else{. p-
1efd0 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b >nFkConstraint +
1efd1 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 = pOp->p2;. }.
1efd2 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1efd3 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50 code: FkIfZero P
1efd4 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1efd5 20 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73 This opcode tes
1efd6 74 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20 ts if a foreign
1efd7 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 key constraint-c
1efd8 6f 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e ounter is curren
1efd9 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20 tly zero..** If
1efda 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 so, jump to inst
1efdb 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 ruction P2. Othe
1efdc 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f rwise, fall thro
1efdd 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1efde 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e .** instruction.
1efdf 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 .**.** If P1 is
1efe0 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
1efe1 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1efe2 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
1efe3 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e constraint-coun
1efe4 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28 ter.** is zero (
1efe5 74 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75 the one that cou
1efe6 6e 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e nts deferred con
1efe7 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f straint violatio
1efe8 6e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a ns). If P1 is.**
1efe9 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20 zero, the jump
1efea 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 is taken if the
1efeb 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72 statement constr
1efec 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 aint-counter is
1efed 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61 zero.** (immedia
1efee 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 te foreign key c
1efef 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 onstraint violat
1eff0 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f ions)..*/.case O
1eff1 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20 P_FkIfZero: {
1eff2 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1eff3 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 . if( pOp->p1 )
1eff4 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 {. if( db->nD
1eff5 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29 eferredCons==0 )
1eff6 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1eff7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1eff8 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 ( p->nFkConstrai
1eff9 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 nt==0 ) pc = pOp
1effa 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 ->p2-1;. }. br
1effb 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1effc 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f #ifndef SQLITE_
1effd 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1effe 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1efff 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
1f000 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 REMENT./* Opcode
1f001 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a : MemMax P1 P2 *
1f002 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 * *.**.** P1 is
1f003 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 a register in t
1f004 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 he root frame of
1f005 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f this VM (the ro
1f006 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 ot frame is.** d
1f007 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 ifferent from th
1f008 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 e current frame
1f009 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 if this instruct
1f00a 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 ion is being exe
1f00b 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 cuted.** within
1f00c 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 a sub-program).
1f00d 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
1f00e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1f00f 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a the maximum of .
1f010 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 ** its current v
1f011 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c alue and the val
1f012 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1f013 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
1f014 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 struction throws
1f015 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 an error if the
1f016 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 memory cell is
1f017 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a not initially.**
1f018 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a an integer..*/.
1f019 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 case OP_MemMax:
1f01a 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1f01b 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1f01c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f01d 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a ed into u.ca */.
1f01e 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 Mem *pIn1;. V
1f01f 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1f020 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1f021 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f022 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a 20 d into u.ca */.
1f023 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 if( p->pFrame )
1f024 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 61 2e 70 {. for(u.ca.p
1f025 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b Frame=p->pFrame;
1f026 20 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 70 50 u.ca.pFrame->pP
1f027 61 72 65 6e 74 3b 20 75 2e 63 61 2e 70 46 72 61 arent; u.ca.pFra
1f028 6d 65 3d 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e me=u.ca.pFrame->
1f029 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e pParent);. u.
1f02a 63 61 2e 70 49 6e 31 20 3d 20 26 75 2e 63 61 2e ca.pIn1 = &u.ca.
1f02b 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 pFrame->aMem[pOp
1f02c 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a ->p1];. }else{.
1f02d 20 20 20 20 75 2e 63 61 2e 70 49 6e 31 20 3d 20 u.ca.pIn1 =
1f02e 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1f02f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1f030 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 75 eMemIntegerify(u
1f031 2e 63 61 2e 70 49 6e 31 29 3b 0a 20 20 70 49 6e .ca.pIn1);. pIn
1f032 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 2 = &aMem[pOp->p
1f033 32 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 2];. sqlite3Vdb
1f034 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1f035 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 2e 63 61 In2);. if( u.ca
1f036 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d .pIn1->u.i<pIn2-
1f037 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e 63 61 2e >u.i){. u.ca.
1f038 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32 pIn1->u.i = pIn2
1f039 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 ->u.i;. }. bre
1f03a 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1f03b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1f03c 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a INCREMENT */../*
1f03d 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 50 Opcode: IfPos P
1f03e 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1f03f 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 If the value of
1f040 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1f041 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 75 1 or greater, ju
1f042 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1f043 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f It is illegal to
1f044 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 use this instru
1f045 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 ction on a regis
1f046 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a ter that does.**
1f047 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 not contain an
1f048 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 integer. An ass
1f049 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c ertion fault wil
1f04a 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 l result if you
1f04b 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f try..*/.case OP_
1f04c 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 20 IfPos: {
1f04d 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a /* jump, in1 */.
1f04e 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1f04f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1f050 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d t( pIn1->flags&M
1f051 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 EM_Int );. if(
1f052 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20 pIn1->u.i>0 ){.
1f053 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1f054 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1f055 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1f056 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 2a IfNeg P1 P2 * *
1f057 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 *.**.** If the
1f058 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1f059 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 61 r P1 is less tha
1f05a 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f 20 n zero, jump to
1f05b 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 P2. .**.** It is
1f05c 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 illegal to use
1f05d 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1f05e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 on a register t
1f05f 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 hat does.** not
1f060 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 contain an integ
1f061 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f er. An assertio
1f062 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 n fault will res
1f063 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a ult if you try..
1f064 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 67 */.case OP_IfNeg
1f065 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1f066 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e mp, in1 */. pIn
1f067 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 1 = &aMem[pOp->p
1f068 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 1];. assert( pI
1f069 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e n1->flags&MEM_In
1f06a 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d t );. if( pIn1-
1f06b 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 >u.i<0 ){. p
1f06c 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1f06d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1f06e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 ./* Opcode: IfZe
1f06f 72 6f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ro P1 P2 P3 * *.
1f070 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 **.** The regist
1f071 65 72 20 50 31 20 6d 75 73 74 20 63 6f 6e 74 61 er P1 must conta
1f072 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 in an integer.
1f073 41 64 64 20 6c 69 74 65 72 61 6c 20 50 33 20 74 Add literal P3 t
1f074 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 o the.** value i
1f075 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 n register P1.
1f076 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 If the result is
1f077 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70 exactly 0, jump
1f078 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 to P2. .**.** I
1f079 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
1f07a 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 use this instruc
1f07b 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 tion on a regist
1f07c 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 er that does.**
1f07d 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 not contain an i
1f07e 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 nteger. An asse
1f07f 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c rtion fault will
1f080 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 result if you t
1f081 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 ry..*/.case OP_I
1f082 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20 fZero: {
1f083 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a /* jump, in1 */.
1f084 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1f085 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1f086 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d t( pIn1->flags&M
1f087 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 49 6e 31 EM_Int );. pIn1
1f088 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 33 ->u.i += pOp->p3
1f089 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e ;. if( pIn1->u.
1f08a 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 20 i==0 ){. pc
1f08b 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1f08c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1f08d 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 65 * Opcode: AggSte
1f08e 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 0a p * P2 P3 P4 P5.
1f08f 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 **.** Execute th
1f090 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 e step function
1f091 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 for an aggregate
1f092 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69 . The.** functi
1f093 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d 65 on has P5 argume
1f094 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 70 nts. P4 is a p
1f095 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 ointer to the Fu
1f096 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 ncDef.** structu
1f097 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 re that specifie
1f098 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 s the function.
1f099 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a 2a Use register.**
1f09a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 6d P3 as the accum
1f09b 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ulator..**.** Th
1f09c 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 61 e P5 arguments a
1f09d 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 re taken from re
1f09e 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 74 gister P2 and it
1f09f 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e s.** successors.
1f0a0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 53 .*/.case OP_AggS
1f0a1 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a tep: {.#if 0 /*
1f0a2 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f0a3 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 62 moved into u.cb
1f0a4 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 */. int n;. i
1f0a5 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 nt i;. Mem *pMe
1f0a6 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a m;. Mem *pRec;.
1f0a7 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1f0a8 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 t ctx;. sqlite3
1f0a9 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a _value **apVal;.
1f0aa 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1f0ab 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f0ac 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 0a 20 20 into u.cb */..
1f0ad 75 2e 63 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 u.cb.n = pOp->p5
1f0ae 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 62 ;. assert( u.cb
1f0af 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e 63 62 2e .n>=0 );. u.cb.
1f0b0 70 52 65 63 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pRec = &aMem[pOp
1f0b1 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 62 2e 61 70 ->p2];. u.cb.ap
1f0b2 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Val = p->apArg;.
1f0b3 20 20 61 73 73 65 72 74 28 20 75 2e 63 62 2e 61 assert( u.cb.a
1f0b4 70 56 61 6c 20 7c 7c 20 75 2e 63 62 2e 6e 3d 3d pVal || u.cb.n==
1f0b5 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e 63 62 2e 0 );. for(u.cb.
1f0b6 69 3d 30 3b 20 75 2e 63 62 2e 69 3c 75 2e 63 62 i=0; u.cb.i<u.cb
1f0b7 2e 6e 3b 20 75 2e 63 62 2e 69 2b 2b 2c 20 75 2e .n; u.cb.i++, u.
1f0b8 63 62 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 cb.pRec++){.
1f0b9 75 2e 63 62 2e 61 70 56 61 6c 5b 75 2e 63 62 2e u.cb.apVal[u.cb.
1f0ba 69 5d 20 3d 20 75 2e 63 62 2e 70 52 65 63 3b 0a i] = u.cb.pRec;.
1f0bb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1f0bc 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e 63 62 emStoreType(u.cb
1f0bd 2e 70 52 65 63 29 3b 0a 20 20 7d 0a 20 20 75 2e .pRec);. }. u.
1f0be 63 62 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 cb.ctx.pFunc = p
1f0bf 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
1f0c0 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1f0c1 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1f0c2 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 62 2e >nMem );. u.cb.
1f0c3 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e 63 62 2e ctx.pMem = u.cb.
1f0c4 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pMem = &aMem[pOp
1f0c5 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 62 2e 70 4d ->p3];. u.cb.pM
1f0c6 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 62 2e em->n++;. u.cb.
1f0c7 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
1f0c8 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 62 2e 63 M_Null;. u.cb.c
1f0c9 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e tx.s.z = 0;. u.
1f0ca 63 62 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 cb.ctx.s.zMalloc
1f0cb 20 3d 20 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78 = 0;. u.cb.ctx
1f0cc 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 .s.xDel = 0;. u
1f0cd 2e 63 62 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 .cb.ctx.s.db = d
1f0ce 62 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 69 73 b;. u.cb.ctx.is
1f0cf 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 Error = 0;. u.c
1f0d0 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b b.ctx.pColl = 0;
1f0d1 0a 20 20 69 66 28 20 75 2e 63 62 2e 63 74 78 2e . if( u.cb.ctx.
1f0d2 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 pFunc->flags & S
1f0d3 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
1f0d4 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OLL ){. asser
1f0d5 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b t( pOp>p->aOp );
1f0d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1f0d7 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f [-1].p4type==P4_
1f0d8 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 COLLSEQ );. a
1f0d9 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f ssert( pOp[-1].o
1f0da 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 pcode==OP_CollSe
1f0db 71 20 29 3b 0a 20 20 20 20 75 2e 63 62 2e 63 74 q );. u.cb.ct
1f0dc 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 x.pColl = pOp[-1
1f0dd 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a ].p4.pColl;. }.
1f0de 20 20 28 75 2e 63 62 2e 63 74 78 2e 70 46 75 6e (u.cb.ctx.pFun
1f0df 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 62 2e c->xStep)(&u.cb.
1f0e0 63 74 78 2c 20 75 2e 63 62 2e 6e 2c 20 75 2e 63 ctx, u.cb.n, u.c
1f0e1 62 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 b.apVal);. if(
1f0e2 75 2e 63 62 2e 63 74 78 2e 69 73 45 72 72 6f 72 u.cb.ctx.isError
1f0e3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
1f0e4 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1f0e5 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 rMsg, db, "%s",
1f0e6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1f0e7 78 74 28 26 75 2e 63 62 2e 63 74 78 2e 73 29 29 xt(&u.cb.ctx.s))
1f0e8 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 62 2e ;. rc = u.cb.
1f0e9 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d ctx.isError;. }
1f0ea 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1f0eb 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 62 2e 63 mRelease(&u.cb.c
1f0ec 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a tx.s);. break;.
1f0ed 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 }../* Opcode: Ag
1f0ee 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 gFinal P1 P2 * P
1f0ef 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 4 *.**.** Execut
1f0f0 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 e the finalizer
1f0f1 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 function for an
1f0f2 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 aggregate. P1 i
1f0f3 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 s.** the memory
1f0f4 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 location that is
1f0f5 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1f0f6 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 for the aggrega
1f0f7 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 te..**.** P2 is
1f0f8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 the number of ar
1f0f9 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 guments that the
1f0fa 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 step function t
1f0fb 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 akes and.** P4 i
1f0fc 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1f0fd 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 he FuncDef for t
1f0fe 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 his function. T
1f0ff 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e he P2.** argumen
1f100 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 t is not used by
1f101 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 this opcode. I
1f102 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 t is only there
1f103 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a to disambiguate.
1f104 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ** functions tha
1f105 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 t can take varyi
1f106 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 ng numbers of ar
1f107 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a guments. The.**
1f108 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 P4 argument is
1f109 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 only needed for
1f10a 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 the degenerate c
1f10b 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 ase where.** the
1f10c 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 step function w
1f10d 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c as not previousl
1f10e 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 y called..*/.cas
1f10f 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b e OP_AggFinal: {
1f110 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1f111 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f112 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 into u.cc */.
1f113 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69 Mem *pMem;.#endi
1f114 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1f115 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1f116 75 2e 63 63 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.cc */. assert
1f117 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1f118 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1f119 29 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 6d 20 3d );. u.cc.pMem =
1f11a 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1f11b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 63 63 . assert( (u.cc
1f11c 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e .pMem->flags & ~
1f11d 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 (MEM_Null|MEM_Ag
1f11e 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d g))==0 );. rc =
1f11f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
1f120 69 6e 61 6c 69 7a 65 28 75 2e 63 63 2e 70 4d 65 inalize(u.cc.pMe
1f121 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 m, pOp->p4.pFunc
1f122 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
1f123 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1f124 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1f125 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1f126 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 75 2e e3_value_text(u.
1f127 63 63 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20 cc.pMem));. }.
1f128 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f129 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e 63 63 2e geEncoding(u.cc.
1f12a 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b pMem, encoding);
1f12b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1f12c 4f 42 53 49 5a 45 28 75 2e 63 63 2e 70 4d 65 6d OBSIZE(u.cc.pMem
1f12d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1f12e 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e VdbeMemTooBig(u.
1f12f 63 63 2e 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 cc.pMem) ){.
1f130 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 goto too_big;.
1f131 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 }. break;.}...#
1f132 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
1f133 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 TE_OMIT_VACUUM)
1f134 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
1f135 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a TE_OMIT_ATTACH).
1f136 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75 /* Opcode: Vacuu
1f137 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a m * * * * *.**.*
1f138 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 * Vacuum the ent
1f139 69 72 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 ire database. T
1f13a 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 his opcode will
1f13b 63 61 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 cause other virt
1f13c 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 ual.** machines
1f13d 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e to be created an
1f13e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e d run. It may n
1f13f 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f ot be called fro
1f140 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 m within.** a tr
1f141 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 ansaction..*/.ca
1f142 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a se OP_Vacuum: {.
1f143 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f144 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1f145 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f146 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73 isuse; . rc = s
1f147 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 qlite3RunVacuum(
1f148 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29 &p->zErrMsg, db)
1f149 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 ;. if( sqlite3S
1f14a 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f afetyOn(db) ) go
1f14b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f14c 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b misuse;. break;
1f14d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 .}.#endif..#if !
1f14e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1f14f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a MIT_AUTOVACUUM).
1f150 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56 /* Opcode: IncrV
1f151 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20 acuum P1 P2 * *
1f152 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 *.**.** Perform
1f153 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 a single step of
1f154 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c the incremental
1f155 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 vacuum procedur
1f156 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 e on.** the P1 d
1f157 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 atabase. If the
1f158 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 vacuum has finis
1f159 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 hed, jump to ins
1f15a 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 truction.** P2.
1f15b 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 Otherwise, fall
1f15c 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1f15d 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1f15e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 .*/.case OP_Incr
1f15f 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 Vacuum: {
1f160 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 /* jump */.#if
1f161 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f162 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f163 20 75 2e 63 64 20 2a 2f 0a 20 20 42 74 72 65 65 u.cd */. Btree
1f164 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a *pBt;.#endif /*
1f165 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f166 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64 moved into u.cd
1f167 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1f168 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1f169 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p1<db->nDb );.
1f16a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1f16b 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1f16c 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 p->p1))!=0 );.
1f16d 75 2e 63 64 2e 70 42 74 20 3d 20 64 62 2d 3e 61 u.cd.pBt = db->a
1f16e 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b Db[pOp->p1].pBt;
1f16f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1f170 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75 treeIncrVacuum(u
1f171 2e 63 64 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 .cd.pBt);. if(
1f172 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
1f173 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1f174 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 >p2 - 1;. rc
1f175 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
1f176 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1f177 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 if../* Opcode: E
1f178 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a xpire P1 * * * *
1f179 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 .**.** Cause pre
1f17a 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 compiled stateme
1f17b 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 nts to become ex
1f17c 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 pired. An expire
1f17d 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 d statement.** f
1f17e 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 ails with an err
1f17f 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 or code of SQLIT
1f180 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 E_SCHEMA if it i
1f181 73 20 65 76 65 72 20 65 78 65 63 75 74 65 64 20 s ever executed
1f182 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 .** (via sqlite3
1f183 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a _step())..** .**
1f184 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 If P1 is 0, the
1f185 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d n all SQL statem
1f186 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 ents become expi
1f187 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f red. If P1 is no
1f188 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 n-zero,.** then
1f189 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 only the current
1f18a 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 ly executing sta
1f18b 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 tement is affect
1f18c 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ed. .*/.case OP_
1f18d 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 Expire: {. if(
1f18e 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 !pOp->p1 ){.
1f18f 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1f190 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1f191 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 db);. }else{.
1f192 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 p->expired = 1
1f193 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1f194 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f195 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1f196 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 HE./* Opcode: Ta
1f197 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 bleLock P1 P2 P3
1f198 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 P4 *.**.** Obta
1f199 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 in a lock on a p
1f19a 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e articular table.
1f19b 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1f19c 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 n is only used w
1f19d 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 hen.** the share
1f19e 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 d-cache feature
1f19f 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a is enabled. .**.
1f1a0 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69 6e 64 ** P1 is the ind
1f1a1 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ex of the databa
1f1a2 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 se in sqlite3.aD
1f1a3 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 b[] of the datab
1f1a4 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 ase.** on which
1f1a5 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 the lock is acqu
1f1a6 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63 ired. A readloc
1f1a7 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66 k is obtained if
1f1a8 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77 P3==0 or.** a w
1f1a9 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d rite lock if P3=
1f1aa 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e =1..**.** P2 con
1f1ab 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 tains the root-p
1f1ac 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 age of the table
1f1ad 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 to lock..**.**
1f1ae 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f P4 contains a po
1f1af 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d inter to the nam
1f1b0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 e of the table b
1f1b1 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 eing locked. Thi
1f1b2 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 s is only.** use
1f1b3 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e d to generate an
1f1b4 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
1f1b5 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f f the lock canno
1f1b6 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a t be obtained..*
1f1b7 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c /.case OP_TableL
1f1b8 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 73 57 72 ock: {. u8 isWr
1f1b9 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f iteLock = (u8)pO
1f1ba 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 69 73 57 p->p3;. if( isW
1f1bb 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d 3d 28 riteLock || 0==(
1f1bc 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
1f1bd 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
1f1be 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 31 20 ) ){. int p1
1f1bf 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 20 20 = pOp->p1; .
1f1c0 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 assert( p1>=0 &&
1f1c1 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 p1<db->nDb );.
1f1c2 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 assert( (p->b
1f1c3 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1f1c4 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 1))!=0 );. as
1f1c5 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 sert( isWriteLoc
1f1c6 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c k==0 || isWriteL
1f1c7 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 72 63 ock==1 );. rc
1f1c8 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c = sqlite3BtreeL
1f1c9 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 ockTable(db->aDb
1f1ca 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 [p1].pBt, pOp->p
1f1cb 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 2, isWriteLock);
1f1cc 0a 20 20 20 20 69 66 28 20 28 72 63 26 30 78 46 . if( (rc&0xF
1f1cd 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 F)==SQLITE_LOCKE
1f1ce 44 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 D ){. const
1f1cf 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e char *z = pOp->
1f1d0 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 p4.z;. sqli
1f1d1 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1f1d2 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 >zErrMsg, db, "d
1f1d3 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 atabase table is
1f1d4 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 locked: %s", z)
1f1d5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1f1d6 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1f1d7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1f1d8 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
1f1d9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f1da 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1f1db 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e * Opcode: VBegin
1f1dc 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1f1dd 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f * P4 may be a po
1f1de 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 inter to an sqli
1f1df 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1f1e0 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20 re. If so, call
1f1e1 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d the .** xBegin m
1f1e2 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74 ethod for that t
1f1e3 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f able..**.** Also
1f1e4 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 , whether or not
1f1e5 20 50 34 20 69 73 20 73 65 74 2c 20 63 68 65 63 P4 is set, chec
1f1e6 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 6e k that this is n
1f1e7 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 ot being called
1f1e8 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61 from.** within a
1f1e9 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76 callback to a v
1f1ea 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79 irtual table xSy
1f1eb 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20 nc() method. If
1f1ec 69 74 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72 it is, the error
1f1ed 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 .** code will be
1f1ee 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c set to SQLITE_L
1f1ef 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f OCKED..*/.case O
1f1f0 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 69 66 20 P_VBegin: {.#if
1f1f1 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f1f2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f1f3 20 75 2e 63 65 20 2a 2f 0a 20 20 56 54 61 62 6c u.ce */. VTabl
1f1f4 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e 64 69 66 e *pVTab;.#endif
1f1f5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f1f6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f1f7 2e 63 65 20 2a 2f 0a 20 20 75 2e 63 65 2e 70 56 .ce */. u.ce.pV
1f1f8 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 Tab = pOp->p4.pV
1f1f9 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 tab;. rc = sqli
1f1fa 74 65 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c te3VtabBegin(db,
1f1fb 20 75 2e 63 65 2e 70 56 54 61 62 29 3b 0a 20 20 u.ce.pVTab);.
1f1fc 69 66 28 20 75 2e 63 65 2e 70 56 54 61 62 20 29 if( u.ce.pVTab )
1f1fd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
1f1fe 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1f1ff 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 sg);. p->zErr
1f200 4d 73 67 20 3d 20 75 2e 63 65 2e 70 56 54 61 62 Msg = u.ce.pVTab
1f201 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 ->pVtab->zErrMsg
1f202 3b 0a 20 20 20 20 75 2e 63 65 2e 70 56 54 61 62 ;. u.ce.pVTab
1f203 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 ->pVtab->zErrMsg
1f204 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 = 0;. }. brea
1f205 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1f206 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1f207 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1f208 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f209 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 VIRTUALTABLE./*
1f20a 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20 Opcode: VCreate
1f20b 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a P1 * * P4 *.**.*
1f20c 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 * P4 is the name
1f20d 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 of a virtual ta
1f20e 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 ble in database
1f20f 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72 P1. Call the xCr
1f210 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 eate method.** f
1f211 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a or that table..*
1f212 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74 /.case OP_VCreat
1f213 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 e: {. rc = sqli
1f214 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 te3VtabCallCreat
1f215 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 e(db, pOp->p1, p
1f216 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 Op->p4.z, &p->zE
1f217 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b rrMsg);. break;
1f218 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1f219 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f21a 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1f21b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1f21c 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1f21d 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50 code: VDestroy P
1f21e 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1f21f 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 P4 is the name
1f220 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 of a virtual tab
1f221 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 le in database P
1f222 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65 1. Call the xDe
1f223 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 stroy method.**
1f224 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a of that table..*
1f225 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72 /.case OP_VDestr
1f226 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 oy: {. p->inVta
1f227 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72 bMethod = 2;. r
1f228 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 c = sqlite3VtabC
1f229 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70 allDestroy(db, p
1f22a 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e Op->p1, pOp->p4.
1f22b 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d z);. p->inVtabM
1f22c 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65 ethod = 0;. bre
1f22d 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1f22e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f22f 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1f230 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f231 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1f232 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50 Opcode: VOpen P
1f233 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1f234 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1f235 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1f236 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 ble object, an s
1f237 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1f238 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20 cture..** P1 is
1f239 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e a cursor number.
1f23a 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70 This opcode op
1f23b 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20 ens a cursor to
1f23c 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 the virtual.** t
1f23d 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20 able and stores
1f23e 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50 that cursor in P
1f23f 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 1..*/.case OP_VO
1f240 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a pen: {.#if 0 /*
1f241 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f242 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 moved into u.cf
1f243 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1f244 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c 69 74 65 *pCur;. sqlite
1f245 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 3_vtab_cursor *p
1f246 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71 VtabCursor;. sq
1f247 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1f248 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 b;. sqlite3_mod
1f249 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 ule *pModule;.#e
1f24a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f24b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f24c 74 6f 20 75 2e 63 66 20 2a 2f 0a 0a 20 20 75 2e to u.cf */.. u.
1f24d 63 66 2e 70 43 75 72 20 3d 20 30 3b 0a 20 20 75 cf.pCur = 0;. u
1f24e 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 20 .cf.pVtabCursor
1f24f 3d 20 30 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61 = 0;. u.cf.pVta
1f250 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 b = pOp->p4.pVta
1f251 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 66 b->pVtab;. u.cf
1f252 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 .pModule = (sqli
1f253 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 te3_module *)u.c
1f254 66 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 f.pVtab->pModule
1f255 3b 0a 20 20 61 73 73 65 72 74 28 75 2e 63 66 2e ;. assert(u.cf.
1f256 70 56 74 61 62 20 26 26 20 75 2e 63 66 2e 70 4d pVtab && u.cf.pM
1f257 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71 odule);. if( sq
1f258 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1f259 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1f25a 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1f25b 20 72 63 20 3d 20 75 2e 63 66 2e 70 4d 6f 64 75 rc = u.cf.pModu
1f25c 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 66 2e 70 le->xOpen(u.cf.p
1f25d 56 74 61 62 2c 20 26 75 2e 63 66 2e 70 56 74 61 Vtab, &u.cf.pVta
1f25e 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69 bCursor);. sqli
1f25f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1f260 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e >zErrMsg);. p->
1f261 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 zErrMsg = u.cf.p
1f262 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 Vtab->zErrMsg;.
1f263 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e 7a 45 72 u.cf.pVtab->zEr
1f264 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 rMsg = 0;. if(
1f265 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1f266 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1f267 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1f268 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
1f269 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e =rc ){. /* In
1f26a 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 itialize sqlite3
1f26b 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 _vtab_cursor bas
1f26c 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 75 e class */. u
1f26d 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 2d .cf.pVtabCursor-
1f26e 3e 70 56 74 61 62 20 3d 20 75 2e 63 66 2e 70 56 >pVtab = u.cf.pV
1f26f 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 tab;.. /* Ini
1f270 74 69 61 6c 69 73 65 20 76 64 62 65 20 63 75 72 tialise vdbe cur
1f271 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 sor object */.
1f272 20 20 75 2e 63 66 2e 70 43 75 72 20 3d 20 61 6c u.cf.pCur = al
1f273 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 locateCursor(p,
1f274 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 pOp->p1, 0, -1,
1f275 30 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 63 66 0);. if( u.cf
1f276 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 20 20 75 .pCur ){. u
1f277 2e 63 66 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 .cf.pCur->pVtabC
1f278 75 72 73 6f 72 20 3d 20 75 2e 63 66 2e 70 56 74 ursor = u.cf.pVt
1f279 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 abCursor;.
1f27a 75 2e 63 66 2e 70 43 75 72 2d 3e 70 4d 6f 64 75 u.cf.pCur->pModu
1f27b 6c 65 20 3d 20 75 2e 63 66 2e 70 56 74 61 62 43 le = u.cf.pVtabC
1f27c 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d ursor->pVtab->pM
1f27d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 odule;. }else
1f27e 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c {. db->mall
1f27f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
1f280 20 20 20 20 75 2e 63 66 2e 70 4d 6f 64 75 6c 65 u.cf.pModule
1f281 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 66 2e 70 56 ->xClose(u.cf.pV
1f282 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 tabCursor);.
1f283 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d }. }. break;.}
1f284 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f285 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1f286 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1f287 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f288 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1f289 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50 de: VFilter P1 P
1f28a 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1f28b 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f P1 is a cursor o
1f28c 70 65 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65 pened using VOpe
1f28d 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 61 64 64 n. P2 is an add
1f28e 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 ress to jump to
1f28f 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 if.** the filter
1f290 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 ed result set is
1f291 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 empty..**.** P4
1f292 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 is either NULL
1f293 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 or a string that
1f294 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 was generated b
1f295 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 y the xBestIndex
1f296 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 .** method of th
1f297 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 e module. The i
1f298 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 nterpretation of
1f299 20 74 68 65 20 50 34 20 73 74 72 69 6e 67 20 69 the P4 string i
1f29a 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 s left.** to the
1f29b 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
1f29c 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 tation..**.** Th
1f29d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 is opcode invoke
1f29e 73 20 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65 s the xFilter me
1f29f 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 thod on the virt
1f2a0 75 61 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66 ual table specif
1f2a1 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 ied.** by P1. T
1f2a2 68 65 20 69 6e 74 65 67 65 72 20 71 75 65 72 79 he integer query
1f2a3 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20 plan parameter
1f2a4 74 6f 20 78 46 69 6c 74 65 72 20 69 73 20 73 74 to xFilter is st
1f2a5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1f2a6 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72 .** P3. Register
1f2a7 20 50 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65 P3+1 stores the
1f2a8 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 20 argc parameter
1f2a9 74 6f 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 to be passed to
1f2aa 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d the.** xFilter m
1f2ab 65 74 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 ethod. Registers
1f2ac 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 P3+2..P3+1+argc
1f2ad 20 61 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a are the argc.**
1f2ae 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 additional para
1f2af 6d 65 74 65 72 73 20 77 68 69 63 68 20 61 72 65 meters which are
1f2b0 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 passed to.** xF
1f2b1 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e 20 52 ilter as argv. R
1f2b2 65 67 69 73 74 65 72 20 50 33 2b 32 20 62 65 63 egister P3+2 bec
1f2b3 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65 omes argv[0] whe
1f2b4 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c n passed to xFil
1f2b5 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d ter..**.** A jum
1f2b6 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20 p is made to P2
1f2b7 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 if the result se
1f2b8 74 20 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e t after filterin
1f2b9 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 g would be empty
1f2ba 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 ..*/.case OP_VFi
1f2bb 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d lter: { /* jum
1f2bc 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1f2bd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f2be 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a oved into u.cg *
1f2bf 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 /. int nArg;.
1f2c0 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f int iQuery;. co
1f2c1 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1f2c2 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d le *pModule;. M
1f2c3 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 4d 65 em *pQuery;. Me
1f2c4 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 71 6c 69 m *pArgc;. sqli
1f2c5 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1f2c6 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 *pVtabCursor;.
1f2c7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1f2c8 74 61 62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f tab;. VdbeCurso
1f2c9 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 r *pCur;. int r
1f2ca 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d es;. int i;. M
1f2cb 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 65 6e 64 em **apArg;.#end
1f2cc 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1f2cd 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f2ce 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75 2e 63 67 u.cg */.. u.cg
1f2cf 2e 70 51 75 65 72 79 20 3d 20 26 61 4d 65 6d 5b .pQuery = &aMem[
1f2d0 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 67 pOp->p3];. u.cg
1f2d1 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 67 2e 70 .pArgc = &u.cg.p
1f2d2 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 2e 63 67 Query[1];. u.cg
1f2d3 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 .pCur = p->apCsr
1f2d4 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45 47 [pOp->p1];. REG
1f2d5 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1f2d6 3e 70 33 2c 20 75 2e 63 67 2e 70 51 75 65 72 79 >p3, u.cg.pQuery
1f2d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 );. assert( u.c
1f2d8 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 g.pCur->pVtabCur
1f2d9 73 6f 72 20 29 3b 0a 20 20 75 2e 63 67 2e 70 56 sor );. u.cg.pV
1f2da 74 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 tabCursor = u.cg
1f2db 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 .pCur->pVtabCurs
1f2dc 6f 72 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 or;. u.cg.pVtab
1f2dd 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 = u.cg.pVtabCur
1f2de 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e sor->pVtab;. u.
1f2df 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 cg.pModule = u.c
1f2e0 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 g.pVtab->pModule
1f2e1 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 ;.. /* Grab the
1f2e2 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 6e index number an
1f2e3 64 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 d argc parameter
1f2e4 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 s */. assert( (
1f2e5 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e 66 6c 61 u.cg.pQuery->fla
1f2e6 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26 gs&MEM_Int)!=0 &
1f2e7 26 20 75 2e 63 67 2e 70 41 72 67 63 2d 3e 66 6c & u.cg.pArgc->fl
1f2e8 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a ags==MEM_Int );.
1f2e9 20 20 75 2e 63 67 2e 6e 41 72 67 20 3d 20 28 69 u.cg.nArg = (i
1f2ea 6e 74 29 75 2e 63 67 2e 70 41 72 67 63 2d 3e 75 nt)u.cg.pArgc->u
1f2eb 2e 69 3b 0a 20 20 75 2e 63 67 2e 69 51 75 65 72 .i;. u.cg.iQuer
1f2ec 79 20 3d 20 28 69 6e 74 29 75 2e 63 67 2e 70 51 y = (int)u.cg.pQ
1f2ed 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a uery->u.i;.. /*
1f2ee 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c Invoke the xFil
1f2ef 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ter method */.
1f2f0 7b 0a 20 20 20 20 75 2e 63 67 2e 72 65 73 20 3d {. u.cg.res =
1f2f1 20 30 3b 0a 20 20 20 20 75 2e 63 67 2e 61 70 41 0;. u.cg.apA
1f2f2 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 rg = p->apArg;.
1f2f3 20 20 20 66 6f 72 28 75 2e 63 67 2e 69 20 3d 20 for(u.cg.i =
1f2f4 30 3b 20 75 2e 63 67 2e 69 3c 75 2e 63 67 2e 6e 0; u.cg.i<u.cg.n
1f2f5 41 72 67 3b 20 75 2e 63 67 2e 69 2b 2b 29 7b 0a Arg; u.cg.i++){.
1f2f6 20 20 20 20 20 20 75 2e 63 67 2e 61 70 41 72 67 u.cg.apArg
1f2f7 5b 75 2e 63 67 2e 69 5d 20 3d 20 26 75 2e 63 67 [u.cg.i] = &u.cg
1f2f8 2e 70 41 72 67 63 5b 75 2e 63 67 2e 69 2b 31 5d .pArgc[u.cg.i+1]
1f2f9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f2fa 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 dbeMemStoreType(
1f2fb 75 2e 63 67 2e 61 70 41 72 67 5b 75 2e 63 67 2e u.cg.apArg[u.cg.
1f2fc 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 i]);. }..
1f2fd 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1f2fe 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1f2ff 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1f300 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 use;. p->inVt
1f301 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 abMethod = 1;.
1f302 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d 6f 64 rc = u.cg.pMod
1f303 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63 ule->xFilter(u.c
1f304 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75 g.pVtabCursor, u
1f305 2e 63 67 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d .cg.iQuery, pOp-
1f306 3e 70 34 2e 7a 2c 20 75 2e 63 67 2e 6e 41 72 67 >p4.z, u.cg.nArg
1f307 2c 20 75 2e 63 67 2e 61 70 41 72 67 29 3b 0a 20 , u.cg.apArg);.
1f308 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 p->inVtabMeth
1f309 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 od = 0;. sqli
1f30a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1f30b 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 >zErrMsg);. p
1f30c 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 67 ->zErrMsg = u.cg
1f30d 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1f30e 0a 20 20 20 20 75 2e 63 67 2e 70 56 74 61 62 2d . u.cg.pVtab-
1f30f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1f310 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1f311 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 _OK ){. u.c
1f312 67 2e 72 65 73 20 3d 20 75 2e 63 67 2e 70 4d 6f g.res = u.cg.pMo
1f313 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 67 2e dule->xEof(u.cg.
1f314 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 pVtabCursor);.
1f315 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
1f316 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1f317 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1f318 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 _to_misuse;..
1f319 20 69 66 28 20 75 2e 63 67 2e 72 65 73 20 29 7b if( u.cg.res ){
1f31a 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1f31b 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 >p2 - 1;. }.
1f31c 20 7d 0a 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e }. u.cg.pCur->
1f31d 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 nullRow = 0;..
1f31e 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1f31f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1f320 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1f321 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f322 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f323 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c ./* Opcode: VCol
1f324 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a umn P1 P2 P3 * *
1f325 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 .**.** Store the
1f326 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 value of the P2
1f327 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a -th column of.**
1f328 20 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 the row of the
1f329 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 virtual-table th
1f32a 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 at the .** P1 cu
1f32b 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 rsor is pointing
1f32c 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 to into registe
1f32d 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P3..*/.case OP
1f32e 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 _VColumn: {.#if
1f32f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f330 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f331 20 75 2e 63 68 20 2a 2f 0a 20 20 73 71 6c 69 74 u.ch */. sqlit
1f332 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1f333 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1f334 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1f335 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 . Mem *pDest;.
1f336 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1f337 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69 sContext;.#endi
1f338 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1f339 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1f33a 75 2e 63 68 20 2a 2f 0a 0a 20 20 56 64 62 65 43 u.ch */.. VdbeC
1f33b 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d ursor *pCur = p-
1f33c 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1f33d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1f33e 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a >pVtabCursor );.
1f33f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1f340 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1f341 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 p->nMem );. u.c
1f342 68 2e 70 44 65 73 74 20 3d 20 26 61 4d 65 6d 5b h.pDest = &aMem[
1f343 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1f344 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b pCur->nullRow ){
1f345 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f346 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63 68 2e MemSetNull(u.ch.
1f347 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 65 61 pDest);. brea
1f348 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 68 2e 70 56 k;. }. u.ch.pV
1f349 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 tab = pCur->pVta
1f34a 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a bCursor->pVtab;.
1f34b 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 20 3d u.ch.pModule =
1f34c 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 4d 6f u.ch.pVtab->pMo
1f34d 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 dule;. assert(
1f34e 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 u.ch.pModule->xC
1f34f 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 65 olumn );. memse
1f350 74 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 t(&u.ch.sContext
1f351 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e 63 68 , 0, sizeof(u.ch
1f352 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 .sContext));..
1f353 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 /* The output ce
1f354 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 ll may already h
1f355 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c ave a buffer all
1f356 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a ocated. Move. *
1f357 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f * the current co
1f358 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 68 2e 73 ntents to u.ch.s
1f359 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e 20 Context.s so in
1f35a 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 case the user-fu
1f35b 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61 6e 20 nction. ** can
1f35c 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79 20 use the already
1f35d 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
1f35e 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f instead of allo
1f35f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20 6e 65 cating a. ** ne
1f360 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 w one.. */. sq
1f361 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1f362 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e (&u.ch.sContext.
1f363 73 2c 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a s, u.ch.pDest);.
1f364 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1f365 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e (&u.ch.sContext.
1f366 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 s, MEM_Null);..
1f367 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f368 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1f369 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1f36a 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 suse;. rc = u.c
1f36b 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 h.pModule->xColu
1f36c 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 mn(pCur->pVtabCu
1f36d 72 73 6f 72 2c 20 26 75 2e 63 68 2e 73 43 6f 6e rsor, &u.ch.sCon
1f36e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a text, pOp->p2);.
1f36f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f370 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1f371 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 . p->zErrMsg =
1f372 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72 u.ch.pVtab->zErr
1f373 4d 73 67 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61 Msg;. u.ch.pVta
1f374 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a b->zErrMsg = 0;.
1f375 20 20 69 66 28 20 75 2e 63 68 2e 73 43 6f 6e 74 if( u.ch.sCont
1f376 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 ext.isError ){.
1f377 20 20 20 72 63 20 3d 20 75 2e 63 68 2e 73 43 6f rc = u.ch.sCo
1f378 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b 0a 20 ntext.isError;.
1f379 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 }.. /* Copy th
1f37a 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 e result of the
1f37b 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 function to the
1f37c 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 0a P3 register. We.
1f37d 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 67 ** do this reg
1f37e 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
1f37f 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 er or not an err
1f380 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20 65 or occurred to e
1f381 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 64 nsure any. ** d
1f382 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f ynamic allocatio
1f383 6e 20 69 6e 20 75 2e 63 68 2e 73 43 6f 6e 74 65 n in u.ch.sConte
1f384 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75 xt.s (a Mem stru
1f385 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64 ct) is released
1f386 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
1f387 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
1f388 6e 67 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 ng(&u.ch.sContex
1f389 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a t.s, encoding);.
1f38a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1f38b 4d 6f 76 65 28 75 2e 63 68 2e 70 44 65 73 74 2c Move(u.ch.pDest,
1f38c 20 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e &u.ch.sContext.
1f38d 73 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 s);. REGISTER_T
1f38e 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e RACE(pOp->p3, u.
1f38f 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 55 50 44 ch.pDest);. UPD
1f390 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1f391 28 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 0a 20 (u.ch.pDest);..
1f392 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f393 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 tyOn(db) ){.
1f394 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f395 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 o_misuse;. }.
1f396 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
1f397 65 6d 54 6f 6f 42 69 67 28 75 2e 63 68 2e 70 44 emTooBig(u.ch.pD
1f398 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f est) ){. goto
1f399 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1f39a 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1f39b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1f39c 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1f39d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f39e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f39f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78 ./* Opcode: VNex
1f3a0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1f3a1 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74 .** Advance virt
1f3a2 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20 ual table P1 to
1f3a3 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 the next row in
1f3a4 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20 61 its result set a
1f3a5 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e nd.** jump to in
1f3a6 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f struction P2. O
1f3a7 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61 r, if the virtua
1f3a8 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 63 l table has reac
1f3a9 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f hed.** the end o
1f3aa 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 f its result set
1f3ab 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f , then fall thro
1f3ac 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1f3ad 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1f3ae 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b case OP_VNext: {
1f3af 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1f3b0 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1f3b1 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f3b2 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c to u.ci */. sql
1f3b3 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1f3b4 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ;. const sqlite
1f3b5 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1f3b6 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 e;. int res;.
1f3b7 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 VdbeCursor *pCur
1f3b8 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1f3b9 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f3ba 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a d into u.ci */..
1f3bb 20 20 75 2e 63 69 2e 72 65 73 20 3d 20 30 3b 0a u.ci.res = 0;.
1f3bc 20 20 75 2e 63 69 2e 70 43 75 72 20 3d 20 70 2d u.ci.pCur = p-
1f3bd 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1f3be 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 69 2e . assert( u.ci.
1f3bf 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1f3c0 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e r );. if( u.ci.
1f3c1 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b pCur->nullRow ){
1f3c2 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1f3c3 20 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 75 u.ci.pVtab = u
1f3c4 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 .ci.pCur->pVtabC
1f3c5 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 ursor->pVtab;.
1f3c6 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 u.ci.pModule = u
1f3c7 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .ci.pVtab->pModu
1f3c8 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e le;. assert( u.
1f3c9 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 ci.pModule->xNex
1f3ca 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b t );.. /* Invok
1f3cb 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d 65 e the xNext() me
1f3cc 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 thod of the modu
1f3cd 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 le. There is no
1f3ce 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a way for the. **
1f3cf 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d 70 6c underlying impl
1f3d0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 72 65 ementation to re
1f3d1 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 turn an error if
1f3d2 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 72 69 one occurs duri
1f3d3 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 29 2e ng. ** xNext().
1f3d4 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 6e 20 Instead, if an
1f3d5 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 72 error occurs, tr
1f3d6 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 ue is returned (
1f3d7 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 0a indicating that.
1f3d8 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61 76 61 ** data is ava
1f3d9 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68 65 20 ilable) and the
1f3da 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 error code retur
1f3db 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e ned when xColumn
1f3dc 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 or. ** some ot
1f3dd 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 her method is ne
1f3de 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 xt invoked on th
1f3df 65 20 73 61 76 65 20 76 69 72 74 75 61 6c 20 74 e save virtual t
1f3e0 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a able cursor.. *
1f3e1 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 /. if( sqlite3S
1f3e2 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1f3e3 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1f3e4 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e 69 6e _misuse;. p->in
1f3e5 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a VtabMethod = 1;.
1f3e6 20 20 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 rc = u.ci.pMod
1f3e7 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63 69 2e ule->xNext(u.ci.
1f3e8 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1f3e9 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d r);. p->inVtabM
1f3ea 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c ethod = 0;. sql
1f3eb 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f3ec 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d ->zErrMsg);. p-
1f3ed 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e >zErrMsg = u.ci.
1f3ee 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1f3ef 20 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 u.ci.pVtab->zE
1f3f0 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 rrMsg = 0;. if(
1f3f1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1f3f2 7b 0a 20 20 20 20 75 2e 63 69 2e 72 65 73 20 3d {. u.ci.res =
1f3f3 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.ci.pModule->x
1f3f4 45 6f 66 28 75 2e 63 69 2e 70 43 75 72 2d 3e 70 Eof(u.ci.pCur->p
1f3f5 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d VtabCursor);. }
1f3f6 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1f3f7 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1f3f8 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f3f9 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21 75 isuse;.. if( !u
1f3fa 2e 63 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 2f .ci.res ){. /
1f3fb 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61 * If there is da
1f3fc 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a ta, jump to P2 *
1f3fd 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e /. pc = pOp->
1f3fe 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1f3ff 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1f400 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f401 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1f402 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f403 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1f404 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d * Opcode: VRenam
1f405 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a e P1 * * P4 *.**
1f406 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1f407 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c ter to a virtual
1f408 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 table object, a
1f409 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 n sqlite3_vtab s
1f40a 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 tructure..** Thi
1f40b 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 s opcode invokes
1f40c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1f40d 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f ng xRename metho
1f40e 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 d. The value.**
1f40f 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1f410 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
1f411 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 zName argument t
1f412 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 o the xRename me
1f413 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 thod..*/.case OP
1f414 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69 66 20 _VRename: {.#if
1f415 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f416 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f417 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71 6c 69 74 u.cj */. sqlit
1f418 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1f419 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 23 65 Mem *pName;.#e
1f41a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f41b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f41c 74 6f 20 75 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e to u.cj */.. u.
1f41d 63 6a 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e cj.pVtab = pOp->
1f41e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b p4.pVtab->pVtab;
1f41f 0a 20 20 75 2e 63 6a 2e 70 4e 61 6d 65 20 3d 20 . u.cj.pName =
1f420 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1f421 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a 2e 70 assert( u.cj.p
1f422 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 Vtab->pModule->x
1f423 52 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 Rename );. REGI
1f424 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1f425 70 31 2c 20 75 2e 63 6a 2e 70 4e 61 6d 65 29 3b p1, u.cj.pName);
1f426 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a 2e . assert( u.cj.
1f427 70 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 26 20 4d pName->flags & M
1f428 45 4d 5f 53 74 72 20 29 3b 0a 20 20 69 66 28 20 EM_Str );. if(
1f429 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1f42a 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1f42b 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1f42c 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 56 74 . rc = u.cj.pVt
1f42d 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 ab->pModule->xRe
1f42e 6e 61 6d 65 28 75 2e 63 6a 2e 70 56 74 61 62 2c name(u.cj.pVtab,
1f42f 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e 7a 29 3b u.cj.pName->z);
1f430 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1f431 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1f432 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
1f433 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 u.cj.pVtab->zEr
1f434 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e 70 56 74 rMsg;. u.cj.pVt
1f435 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1f436 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1f437 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1f438 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f439 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b isuse;.. break;
1f43a 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
1f43b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f43c 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1f43d 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50 pcode: VUpdate P
1f43e 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1f43f 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1f440 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 er to a virtual
1f441 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e table object, an
1f442 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 sqlite3_vtab st
1f443 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73 ructure..** This
1f444 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1f445 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1f446 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 g xUpdate method
1f447 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61 . P2 values.** a
1f448 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65 re contiguous me
1f449 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74 mory cells start
1f44a 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 61 73 ing at P3 to pas
1f44b 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 74 65 s to the xUpdate
1f44c 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e .** invocation.
1f44d 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 The value in re
1f44e 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29 gister (P3+P2-1)
1f44f 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
1f450 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65 the .** p2th ele
1f451 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76 ment of the argv
1f452 20 61 72 72 61 79 20 70 61 73 73 65 64 20 74 6f array passed to
1f453 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 xUpdate..**.**
1f454 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 The xUpdate meth
1f455 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c od will do a DEL
1f456 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54 ETE or an INSERT
1f457 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 or both..** The
1f458 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 argv[0] element
1f459 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f (which correspo
1f45a 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65 nds to memory ce
1f45b 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65 ll P3).** is the
1f45c 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20 rowid of a row
1f45d 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61 to delete. If a
1f45e 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74 rgv[0] is NULL t
1f45f 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 hen no .** delet
1f460 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65 ion occurs. The
1f461 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 argv[1] element
1f462 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 is the rowid of
1f463 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 the new .** row
1f464 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e . This can be N
1f465 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20 ULL to have the
1f466 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65 virtual table se
1f467 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a lect the new .**
1f468 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c rowid for itsel
1f469 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65 f. The subseque
1f46a 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 nt elements in t
1f46b 68 65 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a he array are .**
1f46c 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63 the values of c
1f46d 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65 olumns in the ne
1f46e 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 w row..**.** If
1f46f 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e P2==1 then no in
1f470 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65 sert is performe
1f471 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 d. argv[0] is t
1f472 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 he rowid of.** a
1f473 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a row to delete..
1f474 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f **.** P1 is a bo
1f475 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69 olean flag. If i
1f476 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 t is set to true
1f477 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74 65 and the xUpdate
1f478 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 call.** is succ
1f479 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65 essful, then the
1f47a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
1f47b 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f by sqlite3_last_
1f47c 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a insert_rowid() .
1f47d 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ** is set to the
1f47e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1f47f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20 wid for the row
1f480 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a just inserted..*
1f481 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74 /.case OP_VUpdat
1f482 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c e: {.#if 0 /* l
1f483 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f484 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a oved into u.ck *
1f485 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 /. sqlite3_vtab
1f486 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 *pVtab;. sqlit
1f487 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1f488 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a le;. int nArg;.
1f489 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 int i;. sqlit
1f48a 65 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 e_int64 rowid;.
1f48b 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 Mem **apArg;.
1f48c 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e 64 69 66 20 Mem *pX;.#endif
1f48d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f48e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f48f 63 6b 20 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56 ck */.. u.ck.pV
1f490 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 tab = pOp->p4.pV
1f491 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e tab->pVtab;. u.
1f492 63 6b 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 ck.pModule = (sq
1f493 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 lite3_module *)u
1f494 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .ck.pVtab->pModu
1f495 6c 65 3b 0a 20 20 75 2e 63 6b 2e 6e 41 72 67 20 le;. u.ck.nArg
1f496 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 = pOp->p2;. ass
1f497 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1f498 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 69 ==P4_VTAB );. i
1f499 66 28 20 41 4c 57 41 59 53 28 75 2e 63 6b 2e 70 f( ALWAYS(u.ck.p
1f49a 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 29 Module->xUpdate)
1f49b 20 29 7b 0a 20 20 20 20 75 2e 63 6b 2e 61 70 41 ){. u.ck.apA
1f49c 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 rg = p->apArg;.
1f49d 20 20 20 75 2e 63 6b 2e 70 58 20 3d 20 26 61 4d u.ck.pX = &aM
1f49e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1f49f 20 66 6f 72 28 75 2e 63 6b 2e 69 3d 30 3b 20 75 for(u.ck.i=0; u
1f4a0 2e 63 6b 2e 69 3c 75 2e 63 6b 2e 6e 41 72 67 3b .ck.i<u.ck.nArg;
1f4a1 20 75 2e 63 6b 2e 69 2b 2b 29 7b 0a 20 20 20 20 u.ck.i++){.
1f4a2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1f4a3 53 74 6f 72 65 54 79 70 65 28 75 2e 63 6b 2e 70 StoreType(u.ck.p
1f4a4 58 29 3b 0a 20 20 20 20 20 20 75 2e 63 6b 2e 61 X);. u.ck.a
1f4a5 70 41 72 67 5b 75 2e 63 6b 2e 69 5d 20 3d 20 75 pArg[u.ck.i] = u
1f4a6 2e 63 6b 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e .ck.pX;. u.
1f4a7 63 6b 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20 ck.pX++;. }.
1f4a8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1f4a9 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1f4aa 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f4ab 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d misuse;. rc =
1f4ac 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.ck.pModule->x
1f4ad 55 70 64 61 74 65 28 75 2e 63 6b 2e 70 56 74 61 Update(u.ck.pVta
1f4ae 62 2c 20 75 2e 63 6b 2e 6e 41 72 67 2c 20 75 2e b, u.ck.nArg, u.
1f4af 63 6b 2e 61 70 41 72 67 2c 20 26 75 2e 63 6b 2e ck.apArg, &u.ck.
1f4b0 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 rowid);. sqli
1f4b1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1f4b2 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 >zErrMsg);. p
1f4b3 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6b ->zErrMsg = u.ck
1f4b4 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1f4b5 0a 20 20 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d . u.ck.pVtab-
1f4b6 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1f4b7 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f4b8 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1f4b9 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1f4ba 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63 suse;. if( rc
1f4bb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
1f4bc 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20 Op->p1 ){.
1f4bd 61 73 73 65 72 74 28 20 75 2e 63 6b 2e 6e 41 72 assert( u.ck.nAr
1f4be 67 3e 31 20 26 26 20 75 2e 63 6b 2e 61 70 41 72 g>1 && u.ck.apAr
1f4bf 67 5b 30 5d 20 26 26 20 28 75 2e 63 6b 2e 61 70 g[0] && (u.ck.ap
1f4c0 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 Arg[0]->flags&ME
1f4c1 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 M_Null) );.
1f4c2 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d db->lastRowid =
1f4c3 20 75 2e 63 6b 2e 72 6f 77 69 64 3b 0a 20 20 20 u.ck.rowid;.
1f4c4 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 }. p->nChang
1f4c5 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b e++;. }. break
1f4c6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1f4c7 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f4c8 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1f4c9 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ef SQLITE_OMIT_
1f4ca 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a PAGER_PRAGMAS./*
1f4cb 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 Opcode: Pagecou
1f4cc 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a nt P1 P2 * * *.*
1f4cd 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 *.** Write the c
1f4ce 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
1f4cf 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 pages in databa
1f4d0 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 se P1 to memory
1f4d1 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 cell P2..*/.case
1f4d2 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b OP_Pagecount: {
1f4d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1f4d4 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1f4d5 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1f4d6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f4d7 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20 d into u.cl */.
1f4d8 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e int p1;. int n
1f4d9 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 Page;. Pager *p
1f4da 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a Pager;.#endif /*
1f4db 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f4dc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c moved into u.cl
1f4dd 20 2a 2f 0a 0a 20 20 75 2e 63 6c 2e 70 31 20 3d */.. u.cl.p1 =
1f4de 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6c pOp->p1;. u.cl
1f4df 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 .pPager = sqlite
1f4e0 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 3BtreePager(db->
1f4e1 61 44 62 5b 75 2e 63 6c 2e 70 31 5d 2e 70 42 74 aDb[u.cl.p1].pBt
1f4e2 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1f4e3 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
1f4e4 75 2e 63 6c 2e 70 50 61 67 65 72 2c 20 26 75 2e u.cl.pPager, &u.
1f4e5 63 6c 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20 cl.nPage);. /*
1f4e6 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20 OP_Pagecount is
1f4e7 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72 always called fr
1f4e8 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64 om within a read
1f4e9 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 transaction. T
1f4ea 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75 he. ** page cou
1f4eb 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 nt has already b
1f4ec 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 een successfully
1f4ed 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64 read and cached
1f4ee 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73 . So the. ** s
1f4ef 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
1f4f0 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76 ount() call abov
1f4f1 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a e cannot fail. *
1f4f2 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72 /. if( ALWAYS(r
1f4f3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b c==SQLITE_OK) ){
1f4f4 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1f4f5 20 75 2e 63 6c 2e 6e 50 61 67 65 3b 0a 20 20 7d u.cl.nPage;. }
1f4f6 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1f4f7 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
1f4f8 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a TE_OMIT_TRACE./*
1f4f9 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 20 2a Opcode: Trace *
1f4fa 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1f4fb 49 66 20 74 72 61 63 69 6e 67 20 69 73 20 65 6e If tracing is en
1f4fc 61 62 6c 65 64 20 28 62 79 20 74 68 65 20 73 71 abled (by the sq
1f4fd 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29 20 69 lite3_trace()) i
1f4fe 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e 0a 2a nterface, then.*
1f4ff 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 * the UTF-8 stri
1f500 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 ng contained in
1f501 50 34 20 69 73 20 65 6d 69 74 74 65 64 20 6f 6e P4 is emitted on
1f502 20 74 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62 the trace callb
1f503 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ack..*/.case OP_
1f504 54 72 61 63 65 3a 20 7b 0a 23 69 66 20 30 20 20 Trace: {.#if 0
1f505 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f506 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f507 63 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 cm */. char *zT
1f508 72 61 63 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 race;.#endif /*
1f509 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1f50a 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20 moved into u.cm
1f50b 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 7a 54 72 61 63 */.. u.cm.zTrac
1f50c 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f e = (pOp->p4.z ?
1f50d 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e pOp->p4.z : p->
1f50e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 75 2e 63 zSql);. if( u.c
1f50f 6d 2e 7a 54 72 61 63 65 20 29 7b 0a 20 20 20 20 m.zTrace ){.
1f510 69 66 28 20 64 62 2d 3e 78 54 72 61 63 65 20 29 if( db->xTrace )
1f511 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20 {. char *z
1f512 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 70 = sqlite3VdbeExp
1f513 61 6e 64 53 71 6c 28 70 2c 20 75 2e 63 6d 2e 7a andSql(p, u.cm.z
1f514 54 72 61 63 65 29 3b 0a 20 20 20 20 20 20 64 62 Trace);. db
1f515 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72 ->xTrace(db->pTr
1f516 61 63 65 41 72 67 2c 20 7a 29 3b 0a 20 20 20 20 aceArg, z);.
1f517 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f518 64 62 2c 20 7a 29 3b 0a 20 20 20 20 7d 0a 23 69 db, z);. }.#i
1f519 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1f51a 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66 G. if( (db->f
1f51b 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71 lags & SQLITE_Sq
1f51c 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 lTrace)!=0 ){.
1f51d 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
1f51e 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 Printf("SQL-trac
1f51f 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6d 2e 7a e: %s\n", u.cm.z
1f520 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 Trace);. }.#e
1f521 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 ndif /* SQLITE_D
1f522 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 EBUG */. }. br
1f523 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a eak;.}.#endif...
1f524 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 /* Opcode: Noop
1f525 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 * * * * *.**.**
1f526 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 Do nothing. Thi
1f527 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 s instruction is
1f528 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61 73 often useful as
1f529 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 a jump.** desti
1f52a 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a nation..*/./*.**
1f52b 20 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c 61 The magic Expla
1f52c 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e in opcode are on
1f52d 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e ly inserted when
1f52e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 explain==2 (whi
1f52f 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 ch.** is to say
1f530 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e when the EXPLAIN
1f531 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 QUERY PLAN synt
1f532 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 ax is used.).**
1f533 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f This opcode reco
1f534 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 rds information
1f535 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a from the optimiz
1f536 65 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a 2a er. It is the.*
1f537 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 * the same as a
1f538 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 no-op. This opc
1f539 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 72 odesnever appear
1f53a 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 s in a real VM p
1f53b 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 rogram..*/.defau
1f53c 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f lt: { /
1f53d 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79 * This is really
1f53e 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f OP_Noop and OP_
1f53f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 Explain */. bre
1f540 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ak;.}../********
1f541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f543 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f544 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f545 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 *****.** The cas
1f546 65 73 20 6f 66 20 74 68 65 20 73 77 69 74 63 68 es of the switch
1f547 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 statement above
1f548 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c this line shoul
1f549 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 d all be indente
1f54a 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65 73 d.** by 6 spaces
1f54b 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66 74 2d . But the left-
1f54c 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20 68 61 most 6 spaces ha
1f54d 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 ve been removed
1f54e 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a to improve the.*
1f54f 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20 20 * readability.
1f550 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 From this point
1f551 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 on down, the nor
1f552 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 mal indentation
1f553 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 rules are.** res
1f554 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a tored..*********
1f555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f556 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f557 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f559 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 ****/. }..#if
1f55a 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
1f55b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36 34 . {. u64
1f55c 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 74 elapsed = sqlit
1f55d 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74 61 e3Hwtime() - sta
1f55e 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 rt;. pOp->c
1f55f 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 ycles += elapsed
1f560 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74 ;. pOp->cnt
1f561 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 ++;.#if 0.
1f562 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74 fprintf(stdout
1f563 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61 , "%10llu ", ela
1f564 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 73 psed);. s
1f565 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
1f566 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 p(stdout, origPc
1f567 2c 20 26 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b , &aOp[origPc]);
1f568 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 .#endif. }.#e
1f569 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ndif.. /* The
1f56a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 following code
1f56b 61 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 adds nothing to
1f56c 74 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 the actual funct
1f56d 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 ionality. **
1f56e 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 of the program.
1f56f 20 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 It is only here
1f570 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
1f571 20 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 debugging..
1f572 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ** On the other
1f573 68 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 hand, it does bu
1f574 72 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 rn CPU cycles ev
1f575 65 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 ery time through
1f576 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c . ** the eval
1f577 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 uator loop. So
1f578 77 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 we can leave it
1f579 6f 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 out when NDEBUG
1f57a 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 is defined..
1f57b 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
1f57c 47 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 G. assert( pc
1f57d 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f >=-1 && pc<p->nO
1f57e 70 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c p );..#ifdef SQL
1f57f 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 ITE_DEBUG. if
1f580 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 ( p->trace ){.
1f581 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 if( rc!=0 )
1f582 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 fprintf(p->trace
1f583 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a ,"rc=%d\n",rc);.
1f584 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f if( pOp->o
1f585 70 66 6c 61 67 73 20 26 20 28 4f 50 46 4c 47 5f pflags & (OPFLG_
1f586 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 7c OUT2_PRERELEASE|
1f587 4f 50 46 4c 47 5f 4f 55 54 32 29 20 29 7b 0a 20 OPFLG_OUT2) ){.
1f588 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54 registerT
1f589 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 race(p->trace, p
1f58a 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 4f Op->p2, &aMem[pO
1f58b 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 20 20 7d p->p2]);. }
1f58c 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e . if( pOp->
1f58d 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f opflags & OPFLG_
1f58e 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20 OUT3 ){.
1f58f 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d registerTrace(p-
1f590 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c >trace, pOp->p3,
1f591 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 29 &aMem[pOp->p3])
1f592 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f593 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 #endif /* SQLIT
1f594 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 E_DEBUG */.#endi
1f595 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a f /* NDEBUG */.
1f596 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 } /* The end
1f597 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c of the for(;;) l
1f598 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 oop the loops th
1f599 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f rough opcodes */
1f59a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 .. /* If we rea
1f59b 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 ch this point, i
1f59c 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 t means that exe
1f59d 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 cution is finish
1f59e 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 ed with. ** an
1f59f 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 error of some ki
1f5a0 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 nd.. */.vdbe_er
1f5a1 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 ror_halt:. asse
1f5a2 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 rt( rc );. p->r
1f5a3 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 c = rc;. sqlite
1f5a4 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 3VdbeHalt(p);.
1f5a5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 if( rc==SQLITE_I
1f5a6 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d OERR_NOMEM ) db-
1f5a7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1f5a8 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 1;. rc = SQLITE
1f5a9 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 65 _ERROR;. if( re
1f5aa 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 setSchemaOnFault
1f5ab 20 29 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 ) sqlite3ResetI
1f5ac 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 nternalSchema(db
1f5ad 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 , 0);.. /* This
1f5ae 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 is the only way
1f5af 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72 6f out of this pro
1f5b0 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 76 65 cedure. We have
1f5b1 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 to. ** release
1f5b2 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 the mutexes on
1f5b3 62 74 72 65 65 73 20 74 68 61 74 20 77 65 72 65 btrees that were
1f5b4 20 61 63 71 75 69 72 65 64 20 61 74 20 74 68 65 acquired at the
1f5b5 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64 . ** top. */.vd
1f5b6 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c be_return:. sql
1f5b7 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1f5b8 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 rayLeave(&p->aMu
1f5b9 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
1f5ba 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f c;.. /* Jump to
1f5bb 20 68 65 72 65 20 69 66 20 61 20 73 74 72 69 6e here if a strin
1f5bc 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65 72 g or blob larger
1f5bd 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 than SQLITE_MAX
1f5be 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20 _LENGTH. ** is
1f5bf 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a encountered.. *
1f5c0 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c /.too_big:. sql
1f5c1 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1f5c2 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1f5c3 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 string or blob t
1f5c4 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20 3d oo big");. rc =
1f5c5 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a SQLITE_TOOBIG;.
1f5c6 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1f5c7 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 r_halt;.. /* Ju
1f5c8 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 mp to here if a
1f5c9 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a malloc() fails..
1f5ca 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 */.no_mem:. d
1f5cb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1f5cc 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 = 1;. sqlite3Se
1f5cd 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1f5ce 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66 Msg, db, "out of
1f5cf 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20 memory");. rc
1f5d0 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1f5d1 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1f5d2 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 r_halt;.. /* Ju
1f5d3 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61 mp to here for a
1f5d4 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 n SQLITE_MISUSE
1f5d5 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 error.. */.abor
1f5d6 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a t_due_to_misuse:
1f5d7 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d . rc = SQLITE_M
1f5d8 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c ISUSE;. /* Fall
1f5d9 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72 74 thru into abort
1f5da 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f _due_to_error */
1f5db 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 .. /* Jump to h
1f5dc 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 ere for any othe
1f5dd 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c 20 r kind of fatal
1f5de 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63 22 error. The "rc"
1f5df 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73 variable. ** s
1f5e0 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 hould hold the e
1f5e1 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a rror number.. *
1f5e2 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 /.abort_due_to_e
1f5e3 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 20 rror:. assert(
1f5e4 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b p->zErrMsg==0 );
1f5e5 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1f5e6 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20 53 cFailed ) rc = S
1f5e7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 QLITE_NOMEM;. i
1f5e8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f f( rc!=SQLITE_IO
1f5e9 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 ERR_NOMEM ){.
1f5ea 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1f5eb 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1f5ec 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 b, "%s", sqlite3
1f5ed 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 7d ErrStr(rc));. }
1f5ee 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 . goto vdbe_err
1f5ef 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a or_halt;.. /* J
1f5f0 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 74 ump to here if t
1f5f1 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
1f5f2 72 75 70 74 28 29 20 41 50 49 20 73 65 74 73 20 rupt() API sets
1f5f3 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 20 the interrupt.
1f5f4 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 ** flag.. */.ab
1f5f5 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 ort_due_to_inter
1f5f6 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 20 rupt:. assert(
1f5f7 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 db->u1.isInterru
1f5f8 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 53 pted );. rc = S
1f5f9 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b QLITE_INTERRUPT;
1f5fa 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 . p->rc = rc;.
1f5fb 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1f5fc 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1f5fd 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 b, "%s", sqlite3
1f5fe 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 67 ErrStr(rc));. g
1f5ff 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 oto vdbe_error_h
1f600 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a alt;.}../*******
1f601 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 ******* End of v
1f602 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.c **********
1f603 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f604 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f605 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1f606 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
1f607 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a le vdbeblob.c **
1f608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f60a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
1f60b 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54 07 May 1.**.** T
1f60c 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1f60d 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1f60e 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1f60f 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1f610 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1f611 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1f612 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1f613 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1f614 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1f615 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1f616 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1f617 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1f618 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1f619 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1f61a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1f61b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1f61c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1f61d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f61e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f61f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f621 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
1f622 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1f623 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d code used to im
1f624 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e plement incremen
1f625 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2f tal BLOB I/O..*/
1f626 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1f627 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
1f628 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c ./*.** Valid sql
1f629 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c ite3_blob* handl
1f62a 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 es point to Incr
1f62b 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e blob structures.
1f62c 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
1f62d 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 ct Incrblob Incr
1f62e 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 blob;.struct Inc
1f62f 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c rblob {. int fl
1f630 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
1f631 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c /* Copy of "fl
1f632 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 ags" passed to s
1f633 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1f634 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 () */. int nByt
1f635 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1f636 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 /* Size of open
1f637 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a blob, in bytes *
1f638 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b /. int iOffset;
1f639 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1f63a 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c yte offset of bl
1f63b 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 ob in cursor dat
1f63c 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 a */. BtCursor
1f63d 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f *pCsr; /
1f63e 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
1f63f 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f g at blob row */
1f640 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
1f641 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 *pStmt; /* St
1f642 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 atement holding
1f643 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 cursor open */.
1f644 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
1f645 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f646 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 associated datab
1f647 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ase */.};../*.**
1f648 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e Open a blob han
1f649 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1f64a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f64b 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 lob_open(. sqli
1f64c 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 te3* db,
1f64d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1f64e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1f64f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1f650 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 zDb, /* T
1f651 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 he attached data
1f652 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
1f653 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f the blob */. co
1f654 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
1f655 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 , /* The tab
1f656 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
1f657 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 e blob */. cons
1f658 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c t char *zColumn,
1f659 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d /* The colum
1f65a 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 n containing the
1f65b 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 blob */. sqlit
1f65c 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 e_int64 iRow,
1f65d 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f /* The row co
1f65e 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f ntaining the glo
1f65f 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 b */. int flags
1f660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1f661 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 * True -> read/w
1f662 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c rite access, fal
1f663 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 se -> read-only
1f664 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f */. sqlite3_blo
1f665 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 b **ppBlob /*
1f666 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 Handle for acces
1f667 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 sing the blob re
1f668 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 turned here */.)
1f669 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 {. int nAttempt
1f66a 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c = 0;. int iCol
1f66b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f66c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c /* Index of zCol
1f66d 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 umn in row-recor
1f66e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 d */.. /* This
1f66f 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 VDBE program see
1f670 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f ks a btree curso
1f671 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 r to the identif
1f672 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 ied . ** db/tab
1f673 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 le/row entry. Th
1f674 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 e reason for usi
1f675 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 ng a vdbe progra
1f676 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f m instead. ** o
1f677 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 f writing code t
1f678 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 o use the b-tree
1f679 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 layer directly
1f67a 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a is that the. **
1f67b 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 vdbe program wi
1f67c 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 ll take advantag
1f67d 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 e of the various
1f67e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 transaction,.
1f67f 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 ** locking and e
1f680 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e rror handling in
1f681 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69 frastructure bui
1f682 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 lt into the vdbe
1f683 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 .. **. ** Afte
1f684 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 r seeking the cu
1f685 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 rsor, the vdbe e
1f686 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 xecutes an OP_Re
1f687 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f sultRow.. ** Co
1f688 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 de external to t
1f689 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f he Vdbe then "bo
1f68a 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 rrows" the b-tre
1f68b 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a e cursor and. *
1f68c 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 * uses it to imp
1f68d 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f lement the blob_
1f68e 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 read(), blob_wri
1f68f 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 te() and . ** b
1f690 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 lob_bytes() func
1f691 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
1f692 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f The sqlite3_blo
1f693 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 b_close() functi
1f694 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 on finalizes the
1f695 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 vdbe program,.
1f696 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 ** which closes
1f697 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 the b-tree curs
1f698 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 or and (possibly
1f699 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 ) commits the .
1f69a 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ** transaction.
1f69b 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 . */. static c
1f69c 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 onst VdbeOpList
1f69d 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 openBlob[] = {.
1f69e 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 {OP_Transacti
1f69f 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 on, 0, 0, 0},
1f6a0 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 /* 0: Start a
1f6a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
1f6a2 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f {OP_VerifyCoo
1f6a3 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 kie, 0, 0, 0},
1f6a4 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 /* 1: Check th
1f6a5 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 e schema cookie
1f6a6 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 */. {OP_Table
1f6a7 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 Lock, 0, 0, 0},
1f6a8 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 /* 2: Acqu
1f6a9 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72 ire a read or wr
1f6aa 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 ite lock */..
1f6ab 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 /* One of the f
1f6ac 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 ollowing two ins
1f6ad 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70 tructions is rep
1f6ae 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e laced by an OP_N
1f6af 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f oop. */. {OP_
1f6b0 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 OpenRead, 0, 0,
1f6b1 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 0}, /* 3:
1f6b2 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 Open cursor 0 f
1f6b3 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 or reading */.
1f6b4 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c {OP_OpenWrite,
1f6b5 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
1f6b6 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 /* 4: Open curs
1f6b7 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 or 0 for read/wr
1f6b8 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f ite */.. {OP_
1f6b9 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 Variable, 1, 1,
1f6ba 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 1}, /* 5:
1f6bb 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 Push the rowid
1f6bc 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a to the stack */.
1f6bd 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 {OP_NotExist
1f6be 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 s, 0, 9, 1},
1f6bf 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 /* 6: Seek th
1f6c0 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 e cursor */.
1f6c1 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 {OP_Column, 0, 0
1f6c2 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f , 1}, /
1f6c3 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f * 7 */. {OP_
1f6c4 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c ResultRow, 1, 0,
1f6c5 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 0}, /* 8
1f6c6 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 */. {OP_Clos
1f6c7 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 e, 0, 0, 0},
1f6c8 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a /* 9 */.
1f6c9 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c {OP_Halt, 0,
1f6ca 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 0, 0},
1f6cb 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b /* 10 */. };
1f6cc 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b .. Vdbe *v = 0;
1f6cd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1f6ce 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a TE_OK;. char *z
1f6cf 45 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 Err = 0;. Table
1f6d0 20 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20 *pTab;. Parse
1f6d1 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 *pParse;.. *ppB
1f6d2 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 lob = 0;. sqlit
1f6d3 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f6d4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 b->mutex);. pPa
1f6d5 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 rse = sqlite3Sta
1f6d6 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 ckAllocRaw(db, s
1f6d7 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b izeof(*pParse));
1f6d8 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 . if( pParse==0
1f6d9 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1f6da 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 ITE_NOMEM;. g
1f6db 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f6dc 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 t;. }. do {.
1f6dd 20 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c memset(pParse,
1f6de 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 0, sizeof(Parse
1f6df 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e ));. pParse->
1f6e0 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 db = db;.. if
1f6e1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1f6e2 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 n(db) ){. s
1f6e3 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f6e4 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71 zErr);. sq
1f6e5 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 lite3StackFree(d
1f6e6 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 b, pParse);.
1f6e7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1f6e8 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
1f6e9 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
1f6ea 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1f6eb 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
1f6ec 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 BtreeEnterAll(db
1f6ed 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 );. pTab = sq
1f6ee 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
1f6ef 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 (pParse, 0, zTab
1f6f0 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 le, zDb);. if
1f6f1 28 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 ( pTab && IsVirt
1f6f2 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 ual(pTab) ){.
1f6f3 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 pTab = 0;.
1f6f4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f6f5 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e sg(pParse, "cann
1f6f6 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 ot open virtual
1f6f7 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 table: %s", zTab
1f6f8 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 le);. }.#ifnd
1f6f9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f6fa 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 IEW. if( pTab
1f6fb 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 && pTab->pSelec
1f6fc 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 t ){. pTab
1f6fd 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
1f6fe 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1f6ff 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 e, "cannot open
1f700 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c view: %s", zTabl
1f701 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
1f702 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 . if( !pTab )
1f703 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 {. if( pPar
1f704 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 se->zErrMsg ){.
1f705 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1f706 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a Free(db, zErr);.
1f707 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70 zErr = p
1f708 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a Parse->zErrMsg;.
1f709 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
1f70a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1f70b 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1f70c 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1f70d 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1f70e 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1f70f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1f710 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1f711 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1f712 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1f713 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 . /* Now sear
1f714 63 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 ch pTab for the
1f715 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f exact column. */
1f716 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b . for(iCol=0;
1f717 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 iCol < pTab->nC
1f718 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 ol; iCol++) {.
1f719 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1f71a 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f trICmp(pTab->aCo
1f71b 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a l[iCol].zName, z
1f71c 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 Column)==0 ){.
1f71d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1f71e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1f71f 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e f( iCol==pTab->n
1f720 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Col ){. sql
1f721 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1f722 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 Err);. zErr
1f723 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1f724 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 f(db, "no such c
1f725 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 olumn: \"%s\"",
1f726 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 zColumn);.
1f727 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f728 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 R;. (void)s
1f729 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1f72a 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 db);. sqlit
1f72b 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
1f72c 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 db);. goto
1f72d 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1f72e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1f72f 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 the value is bei
1f730 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 ng opened for wr
1f731 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 iting, check tha
1f732 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c t the. ** col
1f733 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 umn is not index
1f734 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 ed, and that it
1f735 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 is not part of a
1f736 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20 foreign key. .
1f737 20 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69 ** It is agai
1f738 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f nst the rules to
1f739 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 open a column t
1f73a 6f 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f o which either o
1f73b 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64 f these. ** d
1f73c 65 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c escriptions appl
1f73d 69 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e ies for writing.
1f73e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 */. if( fla
1f73f 67 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 gs ){. cons
1f740 74 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d t char *zFault =
1f741 20 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 0;. Index
1f742 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53 *pIdx;.#ifndef S
1f743 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
1f744 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28 GN_KEY. if(
1f745 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 db->flags&SQLIT
1f746 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b E_ForeignKeys ){
1f747 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 . /* Chec
1f748 6b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d k that the colum
1f749 6e 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 n is not part of
1f74a 20 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79 an FK child key
1f74b 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a definition. It.
1f74c 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f ** is no
1f74d 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 t necessary to c
1f74e 68 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61 heck if it is pa
1f74f 72 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b rt of a parent k
1f750 65 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20 ey, as parent.
1f751 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c ** key col
1f752 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64 umns must be ind
1f753 65 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20 exed. The check
1f754 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20 below will pick
1f755 75 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 up this .
1f756 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 ** case. */.
1f757 20 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 FKey *pFKe
1f758 79 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 y;. for(p
1f759 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79 FKey=pTab->pFKey
1f75a 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 ; pFKey; pFKey=p
1f75b 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 FKey->pNextFrom)
1f75c 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
1f75d 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 j;. for
1f75e 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e (j=0; j<pFKey->n
1f75f 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Col; j++){.
1f760 20 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79 if( pFKey
1f761 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d ->aCol[j].iFrom=
1f762 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 =iCol ){.
1f763 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 zFault =
1f764 22 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20 "foreign key";.
1f765 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1f766 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f767 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1f768 66 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 f. for(pIdx
1f769 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
1f76a 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
1f76b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
1f76c 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 int j;. f
1f76d 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e or(j=0; j<pIdx->
1f76e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 nColumn; j++){.
1f76f 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
1f770 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d x->aiColumn[j]==
1f771 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 iCol ){.
1f772 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e zFault = "in
1f773 64 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20 dexed";.
1f774 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1f775 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1f776 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 zFault ){.
1f777 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f778 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 db, zErr);.
1f779 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 zErr = sqlite
1f77a 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61 3MPrintf(db, "ca
1f77b 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c nnot open %s col
1f77c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22 umn for writing"
1f77d 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20 , zFault);.
1f77e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f77f 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76 RROR;. (v
1f780 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1f781 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1f782 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f783 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f784 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f785 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a en_out;. }.
1f786 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 }.. v = s
1f787 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
1f788 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 (db);. if( v
1f789 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 ){. int iDb
1f78a 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
1f78b 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 ToIndex(db, pTab
1f78c 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
1f78d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f78e 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 OpList(v, sizeof
1f78f 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f (openBlob)/sizeo
1f790 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f f(VdbeOpList), o
1f791 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 penBlob);.
1f792 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b flags = !!flags;
1f793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f794 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61 /* flags = (fla
1f795 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a gs ? 1 : 0); */.
1f796 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f797 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 ure the OP_Trans
1f798 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 action */.
1f799 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f79a 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a eP1(v, 0, iDb);.
1f79b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f79c 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 eChangeP2(v, 0,
1f79d 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f flags);.. /
1f79e 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 * Configure the
1f79f 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 OP_VerifyCookie
1f7a0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1f7a1 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 VdbeChangeP1(v,
1f7a2 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 1, iDb);. s
1f7a3 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f7a4 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 P2(v, 1, pTab->p
1f7a5 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
1f7a6 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f ookie);.. /
1f7a7 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 * Make sure a mu
1f7a8 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 tex is held on t
1f7a9 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 he table to be a
1f7aa 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 ccessed */.
1f7ab 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1f7ac 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a Btree(v, iDb); .
1f7ad 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 . /* Config
1f7ae 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65 ure the OP_Table
1f7af 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e Lock instruction
1f7b0 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1f7b1 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 3VdbeChangeP1(v,
1f7b2 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 2, iDb);.
1f7b3 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f7b4 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e eP2(v, 2, pTab->
1f7b5 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c tnum);. sql
1f7b6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
1f7b7 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20 (v, 2, flags);.
1f7b8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f7b9 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70 ChangeP4(v, 2, p
1f7ba 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 Tab->zName, P4_T
1f7bb 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20 RANSIENT);..
1f7bc 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 /* Remove eith
1f7bd 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 er the OP_OpenWr
1f7be 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e ite or OpenRead.
1f7bf 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 Set the P2 .
1f7c0 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 ** parameter
1f7c1 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 of the other to
1f7c2 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a pTab->tnum. */.
1f7c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f7c4 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c eChangeToNoop(v,
1f7c5 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a 4 - flags, 1);.
1f7c6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f7c7 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b eChangeP2(v, 3 +
1f7c8 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e flags, pTab->tn
1f7c9 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 um);. sqlit
1f7ca 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 e3VdbeChangeP3(v
1f7cb 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 , 3 + flags, iDb
1f7cc 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e );.. /* Con
1f7cd 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 figure the numbe
1f7ce 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f r of columns. Co
1f7cf 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 nfigure the curs
1f7d0 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 or to. ** t
1f7d1 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 hink that the ta
1f7d2 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 ble has one more
1f7d3 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 column than it
1f7d4 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 really. **
1f7d5 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 does. An OP_Colu
1f7d6 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 mn to retrieve t
1f7d7 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f his imaginary co
1f7d8 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 lumn will.
1f7d9 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ** always return
1f7da 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 an SQL NULL. Th
1f7db 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 is is useful bec
1f7dc 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 ause it means.
1f7dd 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e ** we can in
1f7de 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 voke OP_Column t
1f7df 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 o fill in the vd
1f7e0 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 be cursors type
1f7e1 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 . ** and of
1f7e2 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f fset cache witho
1f7e3 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 ut causing any I
1f7e4 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 O.. */.
1f7e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f7e6 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73 ngeP4(v, 3+flags
1f7e7 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f , SQLITE_INT_TO_
1f7e8 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 PTR(pTab->nCol+1
1f7e9 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 ),P4_INT32);.
1f7ea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f7eb 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61 angeP2(v, 7, pTa
1f7ec 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b->nCol);.
1f7ed 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1f7ee 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1f7ef 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1f7f0 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 Ready(v, 1, 1, 1
1f7f1 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 , 0, 0, 0);.
1f7f2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 }. }. .
1f7f3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f7f4 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1f7f5 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 rc = sqlite3Safe
1f7f6 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 tyOff(db);. i
1f7f7 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c f( NEVER(rc!=SQL
1f7f8 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d ITE_OK) || db->m
1f7f9 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f7fa 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f7fb 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a pen_out;. }..
1f7fc 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 sqlite3_bind
1f7fd 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f _int64((sqlite3_
1f7fe 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f stmt *)v, 1, iRo
1f7ff 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c w);. rc = sql
1f800 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 ite3_step((sqlit
1f801 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 e3_stmt *)v);.
1f802 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f803 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 _ROW ){. nA
1f804 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 ttempt++;.
1f805 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e rc = sqlite3_fin
1f806 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 alize((sqlite3_s
1f807 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 tmt *)v);.
1f808 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f809 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a , zErr);. z
1f80a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Err = sqlite3MPr
1f80b 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33 intf(db, sqlite3
1f80c 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 _errmsg(db));.
1f80d 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d v = 0;. }
1f80e 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 . } while( nAtt
1f80f 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 empt<5 && rc==SQ
1f810 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a LITE_SCHEMA );..
1f811 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1f812 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 _ROW ){. /* T
1f813 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 he row-record ha
1f814 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 s been opened su
1f815 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 ccessfully. Chec
1f816 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a k that the. *
1f817 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 * column in ques
1f818 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 tion contains te
1f819 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 xt or a blob. If
1f81a 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
1f81b 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 ** text, it is
1f81c 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 up to the caller
1f81d 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f to get the enco
1f81e 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 ding right..
1f81f 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 */. Incrblob
1f820 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 *pBlob;. u32
1f821 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b type = v->apCsr[
1f822 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0]->aType[iCol];
1f823 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 .. if( type<1
1f824 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 2 ){. sqlit
1f825 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1f826 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d r);. zErr =
1f827 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1f828 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e db, "cannot open
1f829 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 value of type %
1f82a 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 s",. ty
1f82b 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 pe==0?"null": ty
1f82c 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 pe==7?"real": "i
1f82d 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b nteger". );
1f82e 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1f82f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1f830 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1f831 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 ut;. }. pB
1f832 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 lob = (Incrblob
1f833 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
1f834 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
1f835 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 (Incrblob));.
1f836 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1f837 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 ailed ){. s
1f838 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f839 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 pBlob);. g
1f83a 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f83b 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c t;. }. pBl
1f83c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ob->flags = flag
1f83d 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 s;. pBlob->pC
1f83e 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 sr = v->apCsr[0
1f83f 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 ]->pCursor;.
1f840 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1f841 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 rCursor(pBlob->p
1f842 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1f843 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 3BtreeCacheOverf
1f844 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 low(pBlob->pCsr)
1f845 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1f846 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 eeLeaveCursor(pB
1f847 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1f848 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 pBlob->pStmt = (
1f849 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 sqlite3_stmt *)v
1f84a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 ;. pBlob->iOf
1f84b 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b fset = v->apCsr[
1f84c 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 0]->aOffset[iCol
1f84d 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 ];. pBlob->nB
1f84e 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 yte = sqlite3Vdb
1f84f 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 eSerialTypeLen(t
1f850 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d ype);. pBlob-
1f851 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 >db = db;. *p
1f852 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 pBlob = (sqlite3
1f853 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 _blob *)pBlob;.
1f854 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1f855 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 K;. }else if( r
1f856 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1f857 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f858 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f859 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d zErr = sqlite3M
1f85a 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 Printf(db, "no s
1f85b 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 uch rowid: %lld"
1f85c 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 , iRow);. rc
1f85d 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f85e 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f }..blob_open_o
1f85f 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28 ut:. if( v && (
1f860 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
1f861 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f862 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
1f863 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3VdbeFinalize(v)
1f864 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
1f865 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 rror(db, rc, zEr
1f866 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1f867 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f868 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
1f869 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 e(db, pParse);.
1f86a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1f86b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1f86c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1f86d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1f86e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f86f 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c /*.** Close a bl
1f870 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 ob handle that w
1f871 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 as previously cr
1f872 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 eated using.** s
1f873 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
1f874 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1f875 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f876 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
1f877 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1f878 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1f879 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1f87a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 ;. int rc;. sq
1f87b 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
1f87c 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 ( p ){. db =
1f87d 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 p->db;. sqlit
1f87e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f87f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
1f880 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 c = sqlite3_fina
1f881 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a lize(p->pStmt);.
1f882 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f883 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 e(db, p);. sq
1f884 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f885 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f886 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1f887 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1f888 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f889 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 *.** Perform a r
1f88a 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 ead or write ope
1f88b 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 ration on a blob
1f88c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
1f88d 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 lobReadWrite(.
1f88e 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 sqlite3_blob *pB
1f88f 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c lob, . void *z,
1f890 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e . int n, . in
1f891 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e t iOffset, . in
1f892 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 t (*xCall)(BtCur
1f893 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 sor*, u32, u32,
1f894 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 void*).){. int
1f895 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a rc;. Incrblob *
1f896 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 p = (Incrblob *)
1f897 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 pBlob;. Vdbe *v
1f898 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1f899 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 .. if( p==0 ) r
1f89a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1f89b 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 USE;. db = p->d
1f89c 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 b;. sqlite3_mut
1f89d 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1f89e 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 ex);. v = (Vdbe
1f89f 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 *)p->pStmt;.. i
1f8a0 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 f( n<0 || iOffse
1f8a1 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b t<0 || (iOffset+
1f8a2 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 n)>p->nByte ){.
1f8a3 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 /* Request is
1f8a4 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 out of range. R
1f8a5 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e eturn a transien
1f8a6 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 t error. */.
1f8a7 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f8a8 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 R;. sqlite3Er
1f8a9 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 ror(db, SQLITE_E
1f8aa 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c RROR, 0);. } el
1f8ab 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 se if( v==0 ){.
1f8ac 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1f8ad 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 s no statement h
1f8ae 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 andle, then the
1f8af 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a blob-handle has.
1f8b0 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 ** already b
1f8b1 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e een invalidated.
1f8b2 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 Return SQLITE_A
1f8b3 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 BORT in this cas
1f8b4 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 e.. */. rc
1f8b5 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b = SQLITE_ABORT;
1f8b6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1f8b7 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 Call either Btr
1f8b8 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 eeData() or Btre
1f8b9 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 ePutData(). If S
1f8ba 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 QLITE_ABORT is.
1f8bb 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 ** returned,
1f8bc 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 clean-up the sta
1f8bd 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 tement handle..
1f8be 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1f8bf 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b ( db == v->db );
1f8c0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f8c1 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e eEnterCursor(p->
1f8c2 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 pCsr);. rc =
1f8c3 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 xCall(p->pCsr, i
1f8c4 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 Offset+p->iOffse
1f8c5 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 t, n, z);. sq
1f8c6 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 lite3BtreeLeaveC
1f8c7 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a ursor(p->pCsr);.
1f8c8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1f8c9 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 TE_ABORT ){.
1f8ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e sqlite3VdbeFin
1f8cb 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 alize(v);.
1f8cc 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 p->pStmt = 0;.
1f8cd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1f8ce 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b b->errCode = rc;
1f8cf 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 . v->rc = r
1f8d0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 c;. }. }. r
1f8d1 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1f8d2 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 it(db, rc);. sq
1f8d3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1f8d4 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1f8d5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f8d6 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 .** Read data fr
1f8d7 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 om a blob handle
1f8d8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f8d9 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f8da 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c _read(sqlite3_bl
1f8db 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 ob *pBlob, void
1f8dc 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 *z, int n, int i
1f8dd 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 Offset){. retur
1f8de 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 n blobReadWrite(
1f8df 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 pBlob, z, n, iOf
1f8e0 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 fset, sqlite3Btr
1f8e1 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eeData);.}../*.*
1f8e2 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1f8e3 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a a blob handle..*
1f8e4 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f8e5 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 sqlite3_blob_wr
1f8e6 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ite(sqlite3_blob
1f8e7 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 *pBlob, const v
1f8e8 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 oid *z, int n, i
1f8e9 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 nt iOffset){. r
1f8ea 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 eturn blobReadWr
1f8eb 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 ite(pBlob, (void
1f8ec 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 *)z, n, iOffset
1f8ed 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 , sqlite3BtreePu
1f8ee 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a tData);.}../*.**
1f8ef 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 Query a blob ha
1f8f0 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a ndle for the siz
1f8f1 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a e of the data..*
1f8f2 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f *.** The Incrblo
1f8f3 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 b.nByte field is
1f8f4 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c fixed for the l
1f8f5 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 ifetime of the I
1f8f6 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f ncrblob.** so no
1f8f7 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 mutex is requir
1f8f8 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a ed for access..*
1f8f9 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1f8fa 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 sqlite3_blob_by
1f8fb 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 tes(sqlite3_blob
1f8fc 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 *pBlob){. Incr
1f8fd 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 blob *p = (Incrb
1f8fe 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 lob *)pBlob;. r
1f8ff 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79 eturn p ? p->nBy
1f900 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 te : 0;.}..#endi
1f901 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c f /* #ifndef SQL
1f902 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1f903 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a B */../*********
1f904 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1f905 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1f906 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f908 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1f909 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1f90a 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a journal.c *****
1f90b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f90c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f90d 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1f90e 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a August 22.**.**
1f90f 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1f910 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1f911 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1f912 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1f913 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1f914 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1f915 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1f916 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1f917 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1f918 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1f919 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1f91a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1f91b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1f91c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1f91d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1f91e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1f91f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1f920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f921 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f924 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1f925 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
1f926 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b ents a special k
1f927 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 ind of sqlite3_f
1f928 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a ile object used.
1f929 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 ** by SQLite to
1f92a 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 create journal f
1f92b 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d iles if the atom
1f92c 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a ic-write optimiz
1f92d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 ation.** is enab
1f92e 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 led..**.** The d
1f92f 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61 istinctive chara
1f930 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69 cteristic of thi
1f931 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 s sqlite3_file i
1f932 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 s that the.** ac
1f933 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c tual on disk fil
1f934 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a e is created laz
1f935 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69 ily. When the fi
1f936 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a le is created,.*
1f937 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65 * the caller spe
1f938 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20 cifies a buffer
1f939 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d size for an in-m
1f93a 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a emory buffer to.
1f93b 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 ** be used to se
1f93c 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64 rvice read() and
1f93d 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74 write() request
1f93e 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 s. The actual fi
1f93f 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 le.** on disk is
1f940 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20 not created or
1f941 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 populated until
1f942 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 either:.**.**
1f943 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 1) The in-memory
1f944 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1f945 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 grows too large
1f946 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 for the allocate
1f947 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65 d .** buffe
1f948 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 r, or.** 2) Th
1f949 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c e sqlite3Journal
1f94a 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f Create() functio
1f94b 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
1f94c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
1f94d 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
1f94e 45 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 E.../*.** A Jour
1f94f 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 nalFile object i
1f950 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 s a subclass of
1f951 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 sqlite3_file use
1f952 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 d by.** as an op
1f953 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 en file handle f
1f954 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 or journal files
1f955 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 ..*/.struct Jour
1f956 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 nalFile {. sqli
1f957 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
1f958 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 pMethod; /* I
1f959 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f /O methods on jo
1f95a 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 urnal files */.
1f95b 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 int nBuf;
1f95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f95d 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 /* Size of zBuf
1f95e 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 [] in bytes */.
1f95f 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 char *zBuf;
1f960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f961 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 /* Space to buf
1f962 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 fer journal writ
1f963 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a es */. int iSiz
1f964 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1f965 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e /* Amoun
1f966 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 t of zBuf[] curr
1f967 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 ently used */.
1f968 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1f969 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f96a 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a /* xOpen flags *
1f96b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 /. sqlite3_vfs
1f96c 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 *pVfs;
1f96d 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c /* The "real
1f96e 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 " underlying VFS
1f96f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1f970 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 le *pReal;
1f971 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 /* The "re
1f972 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 al" underlying f
1f973 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
1f974 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1f975 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 zJournal;
1f976 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1f977 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1f978 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 */.};.typedef st
1f979 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ruct JournalFile
1f97a 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f JournalFile;../
1f97b 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 *.** If it does
1f97c 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
1f97d 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 ts, create and p
1f97e 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 opulate the on-d
1f97f 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 isk file .** for
1f980 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a JournalFile p..
1f981 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 */.static int cr
1f982 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c eateFile(Journal
1f983 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 File *p){. int
1f984 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f985 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 if( !p->pReal
1f986 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
1f987 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 ile *pReal = (sq
1f988 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b lite3_file *)&p[
1f989 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 1];. rc = sql
1f98a 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 ite3OsOpen(p->pV
1f98b 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c fs, p->zJournal,
1f98c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 pReal, p->flags
1f98d 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
1f98e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1f98f 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 p->pReal =
1f990 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 pReal;. if(
1f991 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 p->iSize>0 ){.
1f992 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d assert(p-
1f993 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 >iSize<=p->nBuf)
1f994 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
1f995 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d qlite3OsWrite(p-
1f996 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c >pReal, p->zBuf,
1f997 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 p->iSize, 0);.
1f998 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1f999 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1f99a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 ./*.** Close the
1f99b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1f99c 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 int jrnlClose(s
1f99d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f99e 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c d){. JournalFil
1f99f 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 e *p = (JournalF
1f9a0 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 ile *)pJfd;. if
1f9a1 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 ( p->pReal ){.
1f9a2 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
1f9a3 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a (p->pReal);. }.
1f9a4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1f9a5 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 ->zBuf);. retur
1f9a6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1f9a7 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
1f9a8 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a from the file..*
1f9a9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1f9aa 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1f9ab 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1f9ac 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1f9ad 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1f9ae 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1f9af 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1f9b0 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1f9b1 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1f9b2 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1f9b3 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1f9b4 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1f9b5 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1f9b6 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1f9b7 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1f9b8 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1f9b9 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
1f9ba 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1f9bb 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1f9bc 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1f9bd 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1f9be 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1f9bf 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 ite3OsRead(p->pR
1f9c0 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c eal, zBuf, iAmt,
1f9c1 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 iOfst);. }else
1f9c2 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74 if( (iAmt+iOfst
1f9c3 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 )>p->iSize ){.
1f9c4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
1f9c5 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a ERR_SHORT_READ;.
1f9c6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d }else{. mem
1f9c7 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 cpy(zBuf, &p->zB
1f9c8 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 uf[iOfst], iAmt)
1f9c9 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1f9ca 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 c;.}../*.** Writ
1f9cb 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 e data to the fi
1f9cc 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1f9cd 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 t jrnlWrite(. s
1f9ce 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f9cf 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 d, /* The jou
1f9d0 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 rnal file into w
1f9d1 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f hich to write */
1f9d2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1f9d3 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b Buf, /* Tak
1f9d4 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 e data to be wri
1f9d5 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a tten from here *
1f9d6 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 /. int iAmt,
1f9d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1f9d8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f mber of bytes to
1f9d9 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 write */. sqli
1f9da 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 te_int64 iOfst
1f9db 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 /* Begin writ
1f9dc 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 ing at this offs
1f9dd 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 et into the file
1f9de 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
1f9df 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a = SQLITE_OK;. J
1f9e0 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1f9e1 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1f9e2 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 Jfd;. if( !p->p
1f9e3 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 Real && (iOfst+i
1f9e4 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a Amt)>p->nBuf ){.
1f9e5 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46 rc = createF
1f9e6 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 ile(p);. }. if
1f9e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1f9e8 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 ){. if( p->pR
1f9e9 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eal ){. rc
1f9ea 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
1f9eb 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c (p->pReal, zBuf,
1f9ec 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 iAmt, iOfst);.
1f9ed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1f9ee 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b memcpy(&p->zBuf[
1f9ef 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 iOfst], zBuf, iA
1f9f0 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 mt);. if( p
1f9f1 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 ->iSize<(iOfst+i
1f9f2 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Amt) ){.
1f9f3 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 p->iSize = (iOfs
1f9f4 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d t+iAmt);. }
1f9f5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1f9f6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1f9f7 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 Truncate the fi
1f9f8 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1f9f9 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 t jrnlTruncate(s
1f9fa 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f9fb 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 d, sqlite_int64
1f9fc 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 size){. int rc
1f9fd 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a = SQLITE_OK;. J
1f9fe 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1f9ff 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1fa00 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 Jfd;. if( p->pR
1fa01 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 eal ){. rc =
1fa02 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
1fa03 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 e(p->pReal, size
1fa04 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 );. }else if( s
1fa05 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a ize<p->iSize ){.
1fa06 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 p->iSize = s
1fa07 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ize;. }. retur
1fa08 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1fa09 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f ync the file..*/
1fa0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1fa0b 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c Sync(sqlite3_fil
1fa0c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 e *pJfd, int fla
1fa0d 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 gs){. int rc;.
1fa0e 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1fa0f 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1fa10 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1fa11 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1fa12 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
1fa13 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 p->pReal, flags)
1fa14 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1fa15 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1fa16 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1fa17 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 }../*.** Query t
1fa18 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
1fa19 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f ile in bytes..*/
1fa1a 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1fa1b 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
1fa1c 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c _file *pJfd, sql
1fa1d 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 ite_int64 *pSize
1fa1e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1fa1f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1fa20 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1fa21 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1fa22 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1fa23 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1fa24 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d te3OsFileSize(p-
1fa25 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a >pReal, pSize);.
1fa26 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 }else{. *pS
1fa27 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e ize = (sqlite_in
1fa28 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 t64) p->iSize;.
1fa29 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1fa2a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f }../*.** Table o
1fa2b 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f f methods for Jo
1fa2c 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 urnalFile sqlite
1fa2d 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 3_file object..*
1fa2e 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
1fa2f 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
1fa30 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 ds JournalFileMe
1fa31 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 thods = {. 1,
1fa32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 /* iV
1fa33 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c ersion */. jrnl
1fa34 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 Close, /* xC
1fa35 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 lose */. jrnlRe
1fa36 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 ad, /* xRea
1fa37 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 d */. jrnlWrite
1fa38 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 , /* xWrite
1fa39 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 */. jrnlTruncat
1fa3a 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 e, /* xTruncate
1fa3b 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 */. jrnlSync,
1fa3c 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f /* xSync */
1fa3d 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c . jrnlFileSize,
1fa3e 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a /* xFileSize *
1fa3f 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1fa40 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 /* xLock */.
1fa41 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa42 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 /* xUnlock */.
1fa43 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1fa44 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
1fa45 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Lock */. 0,
1fa46 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c /* xFil
1fa47 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c eControl */. 0,
1fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fa49 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 xSectorSize */.
1fa4a 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
1fa4b 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 /* xDeviceCharac
1fa4c 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a teristics */.};.
1fa4d 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a ./* .** Open a j
1fa4e 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a ournal file..*/.
1fa4f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fa50 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
1fa51 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 lOpen(. sqlite3
1fa52 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
1fa53 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 /* The VFS t
1fa54 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c o use for actual
1fa55 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 file I/O */. c
1fa56 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1fa57 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d , /* Nam
1fa58 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
1fa59 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 file */. sqlit
1fa5a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1fa5b 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f /* Preallo
1fa5c 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c cated, blank fil
1fa5d 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e e handle */. in
1fa5e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
1fa5f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
1fa60 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 ing flags */. i
1fa61 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20 nt nBuf
1fa62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
1fa63 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f es buffered befo
1fa64 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 re opening the f
1fa65 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 ile */.){. Jour
1fa66 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1fa67 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1fa68 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
1fa69 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
1fa6a 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66 ize(pVfs));. if
1fa6b 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 ( nBuf>0 ){.
1fa6c 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 p->zBuf = sqlite
1fa6d 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 3MallocZero(nBuf
1fa6e 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a );. if( !p->z
1fa6f 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Buf ){. ret
1fa70 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1fa71 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1fa72 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1fa73 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
1fa74 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 zName, pJfd, fla
1fa75 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d gs, 0);. }. p-
1fa76 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 >pMethod = &Jour
1fa77 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a nalFileMethods;.
1fa78 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 p->nBuf = nBuf
1fa79 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 ;. p->flags = f
1fa7a 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 lags;. p->zJour
1fa7b 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 nal = zName;. p
1fa7c 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 ->pVfs = pVfs;.
1fa7d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1fa7e 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 K;.}../*.** If t
1fa7f 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f he argument p po
1fa80 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 ints to a Journa
1fa81 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c lFile structure,
1fa82 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 and the underly
1fa83 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 ing.** file has
1fa84 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 not yet been cre
1fa85 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 ated, create it
1fa86 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 now..*/.SQLITE_P
1fa87 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1fa88 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 e3JournalCreate(
1fa89 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 sqlite3_file *p)
1fa8a 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 {. if( p->pMeth
1fa8b 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c ods!=&JournalFil
1fa8c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 eMethods ){.
1fa8d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1fa8e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
1fa8f 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e reateFile((Journ
1fa90 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a alFile *)p);.}..
1fa91 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 /* .** Return th
1fa92 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1fa93 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 s required to st
1fa94 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c ore a JournalFil
1fa95 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a e that uses vfs.
1fa96 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 ** pVfs to creat
1fa97 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 e the underlying
1fa98 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a on-disk files..
1fa99 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1fa9a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 E int sqlite3Jou
1fa9b 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 rnalSize(sqlite3
1fa9c 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 _vfs *pVfs){. r
1fa9d 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f eturn (pVfs->szO
1fa9e 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 sFile+sizeof(Jou
1fa9f 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 rnalFile));.}.#e
1faa0 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
1faa1 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 ***** End of jou
1faa2 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a rnal.c *********
1faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa5 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1faa6 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1faa7 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a memjournal.c **
1faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faaa 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
1faab 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a October 7.**.**
1faac 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1faad 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1faae 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1faaf 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1fab0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1fab1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1fab2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1fab3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1fab4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1fab5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1fab6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1fab7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1fab8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1fab9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1faba 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1fabb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1fabc 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1fac2 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1fac3 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 ns code use to i
1fac4 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d mplement an in-m
1fac5 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a emory rollback j
1fac6 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 ournal..** The i
1fac7 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 n-memory rollbac
1fac8 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 k journal is use
1fac9 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 d to journal tra
1faca 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a nsactions for.**
1facb 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 ":memory:" data
1facc 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74 bases and when t
1facd 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d he journal_mode=
1face 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73 MEMORY pragma is
1facf 20 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a 20 46 6f used..*/../* Fo
1fad0 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 rward references
1fad1 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 to internal str
1fad2 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 uctures */.typed
1fad3 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 ef struct MemJou
1fad4 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b rnal MemJournal;
1fad5 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1fad6 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f FilePoint FilePo
1fad7 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 int;.typedef str
1fad8 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 uct FileChunk Fi
1fad9 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 leChunk;../* Spa
1fada 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
1fadb 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1fadc 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 is allocated in
1fadd 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a increments of.**
1fade 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
1fadf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ..**.** The size
1fae0 20 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74 chosen is a lit
1fae1 74 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20 tle less than a
1fae2 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 power of two. T
1fae3 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20 hat way,.** the
1fae4 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74 FileChunk object
1fae5 20 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a will have a siz
1fae6 65 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78 e that almost ex
1fae7 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 actly fills.** a
1fae8 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c power-of-two al
1fae9 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 location. This
1faea 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64 mimimizes wasted
1faeb 20 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d space in power-
1faec 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 of-two.** memory
1faed 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a allocators..*/.
1faee 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f #define JOURNAL_
1faef 43 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29 CHUNKSIZE ((int)
1faf0 28 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c (1024-sizeof(Fil
1faf1 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d eChunk*)))../* M
1faf2 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 acro to find the
1faf3 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 minimum of two
1faf4 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a numeric values..
1faf5 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 */.#ifndef MIN.#
1faf6 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 define MIN(x,y)
1faf7 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 ((x)<(y)?(x):(y
1faf8 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )).#endif../*.**
1faf9 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f The rollback jo
1fafa 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65 urnal is compose
1fafb 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d of a linked li
1fafc 73 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 st of these stru
1fafd 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 ctures..*/.struc
1fafe 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 t FileChunk {.
1faff 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74 FileChunk *pNext
1fb00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1fb01 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e /* Next chunk in
1fb02 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
1fb03 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 u8 zChunk[JOUR
1fb04 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 NAL_CHUNKSIZE];
1fb05 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
1fb06 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b this chunk */.};
1fb07 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
1fb08 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
1fb09 63 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63 ct serves as a c
1fb0a 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72 ursor into the r
1fb0b 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
1fb0c 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 .** The cursor c
1fb0d 61 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72 an be either for
1fb0e 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 reading or writ
1fb0f 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 ing..*/.struct F
1fb10 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c ilePoint {. sql
1fb11 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 ite3_int64 iOffs
1fb12 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et; /*
1fb13 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 Offset from the
1fb14 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
1fb15 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43 file */. FileC
1fb16 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 hunk *pChunk;
1fb17 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
1fb18 65 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74 ecific chunk int
1fb19 6f 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70 o which cursor p
1fb1a 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a oints */.};../*.
1fb1b 2a 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73 ** This subclass
1fb1c 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f is a subclass o
1fb1d 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20 f sqlite3_file.
1fb1e 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 Each open memor
1fb1f 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 y-journal.** is
1fb20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
1fb21 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 his class..*/.st
1fb22 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ruct MemJournal
1fb23 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d {. sqlite3_io_m
1fb24 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b ethods *pMethod;
1fb25 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c /* Parent cl
1fb26 61 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52 ass. MUST BE FIR
1fb27 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e ST */. FileChun
1fb28 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 k *pFirst;
1fb29 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20 /* Head
1fb2a 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 of in-memory chu
1fb2b 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c nk-list */. Fil
1fb2c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b ePoint endpoint;
1fb2d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fb2e 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 Pointer to the e
1fb2f 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
1fb30 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65 /. FilePoint re
1fb31 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 adpoint;
1fb32 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1fb33 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
1fb34 20 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f last xRead() */
1fb35 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .};../*.** Read
1fb36 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e data from the in
1fb37 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
1fb38 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74 file. This is t
1fb39 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1fb3a 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 n.** of the sqli
1fb3b 74 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65 te3_vfs.xRead me
1fb3c 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 thod..*/.static
1fb3d 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 int memjrnlRead(
1fb3e 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
1fb3f 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 *pJfd, /* The
1fb40 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 journal file fr
1fb41 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 om which to read
1fb42 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 */. void *zBuf
1fb43 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1fb44 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 Put the results
1fb45 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1fb46 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1fb47 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1fb48 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 tes to read */.
1fb49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f sqlite_int64 iO
1fb4a 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e fst /* Begin
1fb4b 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 reading at this
1fb4c 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 offset */.){.
1fb4d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1fb4e 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1fb4f 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d fd;. u8 *zOut =
1fb50 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65 zBuf;. int nRe
1fb51 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74 ad = iAmt;. int
1fb52 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 iChunkOffset;.
1fb53 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 FileChunk *pChu
1fb54 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65 nk;.. /* SQLite
1fb55 20 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20 never tries to
1fb56 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e read past the en
1fb57 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 d of a rollback
1fb58 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1fb59 20 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b assert( iOfst+
1fb5a 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e iAmt<=p->endpoin
1fb5b 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 t.iOffset );..
1fb5c 69 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 if( p->readpoint
1fb5d 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20 .iOffset!=iOfst
1fb5e 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 || iOfst==0 ){.
1fb5f 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 sqlite3_int64
1fb60 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 iOff = 0;. f
1fb61 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 or(pChunk=p->pFi
1fb62 72 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c rst; . AL
1fb63 57 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20 WAYS(pChunk) &&
1fb64 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 (iOff+JOURNAL_CH
1fb65 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b UNKSIZE)<=iOfst;
1fb66 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d . pChunk=
1fb67 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 pChunk->pNext.
1fb68 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 ){. iOff
1fb69 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b += JOURNAL_CHUNK
1fb6a 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 SIZE;. }. }e
1fb6b 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 lse{. pChunk
1fb6c 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 = p->readpoint.p
1fb6d 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 Chunk;. }.. iC
1fb6e 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e hunkOffset = (in
1fb6f 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c t)(iOfst%JOURNAL
1fb70 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 _CHUNKSIZE);. d
1fb71 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 o {. int iSpa
1fb72 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 ce = JOURNAL_CHU
1fb73 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f NKSIZE - iChunkO
1fb74 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e ffset;. int n
1fb75 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 Copy = MIN(nRead
1fb76 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b , (JOURNAL_CHUNK
1fb77 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 SIZE - iChunkOff
1fb78 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 set));. memcp
1fb79 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d y(zOut, &pChunk-
1fb7a 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 >zChunk[iChunkOf
1fb7b 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 fset], nCopy);.
1fb7c 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 zOut += nCopy
1fb7d 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 ;. nRead -= i
1fb7e 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e Space;. iChun
1fb7f 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d kOffset = 0;. }
1fb80 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 while( nRead>=0
1fb81 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 && (pChunk=pChu
1fb82 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 nk->pNext)!=0 &&
1fb83 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d nRead>0 );. p-
1fb84 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 >readpoint.iOffs
1fb85 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b et = iOfst+iAmt;
1fb86 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e . p->readpoint.
1fb87 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b pChunk = pChunk;
1fb88 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
1fb89 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 E_OK;.}../*.** W
1fb8a 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
1fb8b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1fb8c 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 int memjrnlWrit
1fb8d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
1fb8e 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1fb8f 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1fb90 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
1fb91 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ite */. const v
1fb92 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1fb93 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 /* Take data to
1fb94 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 be written from
1fb95 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1fb96 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1fb97 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1fb98 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
1fb99 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1fb9a 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1fb9b 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 n writing at thi
1fb9c 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 s offset into th
1fb9d 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d e file */.){. M
1fb9e 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 emJournal *p = (
1fb9f 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 MemJournal *)pJf
1fba0 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 d;. int nWrite
1fba1 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 = iAmt;. u8 *zW
1fba2 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75 rite = (u8 *)zBu
1fba3 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d f;.. /* An in-m
1fba4 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
1fba5 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 le should only e
1fba6 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20 ver be appended
1fba7 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 to. Random. **
1fba8 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72 access writes ar
1fba9 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62 e not required b
1fbaa 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 y sqlite.. */.
1fbab 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d assert( iOfst==
1fbac 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 p->endpoint.iOff
1fbad 73 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f set );. UNUSED_
1fbae 50 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 PARAMETER(iOfst)
1fbaf 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 ;.. while( nWri
1fbb0 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 te>0 ){. File
1fbb1 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 Chunk *pChunk =
1fbb2 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 p->endpoint.pChu
1fbb3 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 nk;. int iChu
1fbb4 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 nkOffset = (int)
1fbb5 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 (p->endpoint.iOf
1fbb6 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 fset%JOURNAL_CHU
1fbb7 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 NKSIZE);. int
1fbb8 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 iSpace = MIN(nW
1fbb9 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 rite, JOURNAL_CH
1fbba 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b UNKSIZE - iChunk
1fbbb 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 Offset);.. if
1fbbc 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d ( iChunkOffset==
1fbbd 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 0 ){. /* Ne
1fbbe 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 w chunk is requi
1fbbf 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 red to extend th
1fbc0 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 e file. */.
1fbc1 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 FileChunk *pNew
1fbc2 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
1fbc3 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 c(sizeof(FileChu
1fbc4 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nk));. if(
1fbc5 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 !pNew ){.
1fbc6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
1fbc7 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 OERR_NOMEM;.
1fbc8 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e }. pNew->
1fbc9 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 pNext = 0;.
1fbca 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 if( pChunk ){.
1fbcb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1fbcc 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ->pFirst );.
1fbcd 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 pChunk->pNex
1fbce 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 t = pNew;.
1fbcf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
1fbd0 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 ssert( !p->pFirs
1fbd1 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e t );. p->
1fbd2 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 pFirst = pNew;.
1fbd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e }. p->
1fbd4 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 endpoint.pChunk
1fbd5 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 = pNew;. }..
1fbd6 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e memcpy(&p->en
1fbd7 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a dpoint.pChunk->z
1fbd8 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 Chunk[iChunkOffs
1fbd9 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 et], zWrite, iSp
1fbda 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 ace);. zWrite
1fbdb 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 += iSpace;.
1fbdc 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 nWrite -= iSpace
1fbdd 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e ;. p->endpoin
1fbde 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 t.iOffset += iSp
1fbdf 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ace;. }.. retu
1fbe0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1fbe1 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
1fbe2 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1fbe3 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 tic int memjrnlT
1fbe4 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f runcate(sqlite3_
1fbe5 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 file *pJfd, sqli
1fbe6 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a te_int64 size){.
1fbe7 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 MemJournal *p
1fbe8 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 = (MemJournal *)
1fbe9 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e pJfd;. FileChun
1fbea 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 k *pChunk;. ass
1fbeb 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 ert(size==0);.
1fbec 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1fbed 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b (size);. pChunk
1fbee 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 = p->pFirst;.
1fbef 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b while( pChunk ){
1fbf0 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a . FileChunk *
1fbf1 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 pTmp = pChunk;.
1fbf2 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 pChunk = pChu
1fbf3 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 nk->pNext;. s
1fbf4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 qlite3_free(pTmp
1fbf5 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1fbf6 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 MemJournalOpen(p
1fbf7 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 Jfd);. return S
1fbf8 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1fbf9 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
1fbfa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1fbfb 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 memjrnlClose(sq
1fbfc 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1fbfd 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e ){. memjrnlTrun
1fbfe 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 cate(pJfd, 0);.
1fbff 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1fc00 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e K;.}.../*.** Syn
1fc01 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a c the file..**.*
1fc02 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d * Syncing an in-
1fc03 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 memory journal i
1fc04 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c s a no-op. And,
1fc05 20 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72 in fact, this r
1fc06 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 outine.** is nev
1fc07 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77 er called in a w
1fc08 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 orking implement
1fc09 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70 ation. This imp
1fc0a 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 lementation.** e
1fc0b 78 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20 xists purely as
1fc0c 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69 a contingency, i
1fc0d 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66 n case some malf
1fc0e 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 unction in some
1fc0f 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66 other.** part of
1fc10 20 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53 SQLite causes S
1fc11 79 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 ync to be called
1fc12 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a by mistake..*/.
1fc13 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1fc14 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 nlSync(sqlite3_f
1fc15 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
1fc16 74 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f t NotUsed2){ /
1fc17 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55 *NO_TEST*/. UNU
1fc18 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
1fc19 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
1fc1a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
1fc1b 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 /*NO_T
1fc1c 45 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 EST*/. assert(
1fc1d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 0 );
1fc1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f /*NO_TEST*/
1fc21 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1fc22 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1fc23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc25 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 /*NO_TEST*/.}
1fc26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1fc2a 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 TEST*/../*.** Qu
1fc2b 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ery the size of
1fc2c 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 the file in byte
1fc2d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
1fc2e 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 memjrnlFileSize
1fc2f 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1fc30 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 Jfd, sqlite_int6
1fc31 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4 *pSize){. Mem
1fc32 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 Journal *p = (Me
1fc33 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b mJournal *)pJfd;
1fc34 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c . *pSize = (sql
1fc35 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e ite_int64) p->en
1fc36 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a dpoint.iOffset;.
1fc37 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1fc38 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 OK;.}../*.** Tab
1fc39 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f le of methods fo
1fc3a 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c r MemJournal sql
1fc3b 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 ite3_file object
1fc3c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
1fc3d 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ct sqlite3_io_me
1fc3e 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c thods MemJournal
1fc3f 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c Methods = {. 1,
1fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc41 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
1fc42 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 memjrnlClose,
1fc43 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a /* xClose */.
1fc44 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 memjrnlRead,
1fc45 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a /* xRead */.
1fc46 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 memjrnlWrite,
1fc47 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f /* xWrite */
1fc48 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 . memjrnlTrunca
1fc49 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 te, /* xTruncat
1fc4a 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 e */. memjrnlSy
1fc4b 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e nc, /* xSyn
1fc4c 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 c */. memjrnlFi
1fc4d 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c leSize, /* xFil
1fc4e 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 eSize */. 0,
1fc4f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fc50 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 xLock */. 0,
1fc51 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fc52 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 xUnlock */. 0,
1fc53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fc54 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
1fc55 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Lock */. 0,
1fc56 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1fc57 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 FileControl */.
1fc58 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fc59 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a /* xSectorSiz
1fc5a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 e */. 0
1fc5b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 /* xDev
1fc5c 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
1fc5d 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a cs */.};../* .**
1fc5e 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 Open a journal
1fc5f 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
1fc60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1fc61 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 ite3MemJournalOp
1fc62 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 en(sqlite3_file
1fc63 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 *pJfd){. MemJou
1fc64 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f rnal *p = (MemJo
1fc65 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 urnal *)pJfd;.
1fc66 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1fc67 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 TE_ALIGNMENT(p)
1fc68 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 );. memset(p, 0
1fc69 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 , sqlite3MemJour
1fc6a 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d nalSize());. p-
1fc6b 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a >pMethod = &MemJ
1fc6c 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d ournalMethods;.}
1fc6d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1fc6e 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d rue if the file-
1fc6f 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 handle passed as
1fc70 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 an argument is
1fc71 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 .** an in-memory
1fc72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c journal .*/.SQL
1fc73 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1fc74 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
1fc75 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
1fc76 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72 *pJfd){. retur
1fc77 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 n pJfd->pMethods
1fc78 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 ==&MemJournalMet
1fc79 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 hods;.}../* .**
1fc7a 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1fc7b 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
1fc7c 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d red to store a M
1fc7d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75 emJournal that u
1fc7e 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 ses vfs.** pVfs
1fc7f 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e to create the un
1fc80 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b derlying on-disk
1fc81 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
1fc82 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fc83 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1fc84 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 ize(void){. ret
1fc85 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f urn sizeof(MemJo
1fc86 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a urnal);.}../****
1fc87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1fc88 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a f memjournal.c *
1fc89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1fc8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1fc8d 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a file walker.c *
1fc8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1fc91 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a 2008 August 16.
1fc92 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1fc93 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1fc94 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1fc95 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1fc96 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1fc97 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1fc98 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1fc99 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1fc9a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1fc9b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1fc9c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1fc9d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1fc9e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1fc9f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1fca0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1fca1 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1fca2 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1fca3 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1fca4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fca5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fca7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1fca8 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1fca9 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 ains routines us
1fcaa 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 ed for walking t
1fcab 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66 he parser tree f
1fcac 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61 or.** an SQL sta
1fcad 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a tement..*/.../*.
1fcae 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 ** Walk an expre
1fcaf 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 ssion tree. Inv
1fcb0 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b oke the callback
1fcb1 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e once for each n
1fcb2 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 ode.** of the ex
1fcb3 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 pression, while
1fcb4 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 decending. (In
1fcb5 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
1fcb6 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 callback.** is
1fcb7 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 invoked before v
1fcb8 69 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e isiting children
1fcb9 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 .).**.** The ret
1fcba 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 urn value from t
1fcbb 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 he callback shou
1fcbc 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 ld be one of the
1fcbd 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 WRC_*.** consta
1fcbe 6e 74 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 nts to specify h
1fcbf 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 ow to proceed wi
1fcc0 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a th the walk..**.
1fcc1 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e ** WRC_Contin
1fcc2 75 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 ue Continue
1fcc3 20 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e descending down
1fcc4 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a the tree..**.**
1fcc5 20 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 WRC_Prune
1fcc6 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 Do not des
1fcc7 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 cend into child
1fcc8 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f nodes. But allo
1fcc9 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 w.**
1fcca 20 20 20 20 20 20 20 20 20 20 74 68 65 20 77 61 the wa
1fccb 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 lk to continue w
1fccc 69 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 ith sibling node
1fccd 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f s..**.** WRC_
1fcce 41 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f Abort Do
1fccf 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 no more callbac
1fcd0 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 ks. Unwind the
1fcd1 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 stack and.**
1fcd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd3 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 return the top
1fcd4 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c -level walk call
1fcd5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
1fcd6 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 rn value from th
1fcd7 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 is routine is WR
1fcd8 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 C_Abort to aband
1fcd9 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b on the tree walk
1fcda 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 .** and WRC_Cont
1fcdb 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 inue to continue
1fcdc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1fcdd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
1fcde 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a alkExpr(Walker *
1fcdf 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 pWalker, Expr *p
1fce0 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b Expr){. int rc;
1fce1 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 . if( pExpr==0
1fce2 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e ) return WRC_Con
1fce3 74 69 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 tinue;. testcas
1fce4 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 e( ExprHasProper
1fce5 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b ty(pExpr, EP_Tok
1fce6 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 enOnly) );. tes
1fce7 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 tcase( ExprHasPr
1fce8 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1fce9 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 _Reduced) );. r
1fcea 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 c = pWalker->xEx
1fceb 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b prCallback(pWalk
1fcec 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 er, pExpr);. if
1fced 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e ( rc==WRC_Contin
1fcee 75 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ue.
1fcef 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 && !ExprHasAnyP
1fcf0 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 roperty(pExpr,EP
1fcf1 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 _TokenOnly) ){.
1fcf2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1fcf3 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 lkExpr(pWalker,
1fcf4 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 pExpr->pLeft) )
1fcf5 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fcf6 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1fcf7 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1fcf8 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 r, pExpr->pRight
1fcf9 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fcfa 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 bort;. if( Ex
1fcfb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
1fcfc 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
1fcfd 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 t) ){. if(
1fcfe 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1fcff 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 t(pWalker, pExpr
1fd00 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 ->x.pSelect) ) r
1fd01 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1fd02 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1fd03 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fd04 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 kExprList(pWalke
1fd05 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 r, pExpr->x.pLis
1fd06 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1fd07 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d Abort;. }. }
1fd08 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 . return rc & W
1fd09 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a RC_Abort;.}../*.
1fd0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 ** Call sqlite3W
1fd0b 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 alkExpr() for ev
1fd0c 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 ery expression i
1fd0d 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 n list p or unti
1fd0e 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 l.** an abort re
1fd0f 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a quest is seen..*
1fd10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1fd11 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b int sqlite3Walk
1fd12 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 ExprList(Walker
1fd13 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 *pWalker, ExprLi
1fd14 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b st *p){. int i;
1fd15 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
1fd16 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1fd17 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 if( p ){. f
1fd18 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 or(i=p->nExpr, p
1fd19 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 Item=p->a; i>0;
1fd1a 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i--, pItem++){.
1fd1b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fd1c 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1fd1d 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 , pItem->pExpr)
1fd1e 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fd1f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rt;. }. }.
1fd20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1fd21 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 nue;.}../*.** Wa
1fd22 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f lk all expressio
1fd23 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ns associated wi
1fd24 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d th SELECT statem
1fd25 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f ent p. Do.** no
1fd26 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c t invoke the SEL
1fd27 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 ECT callback on
1fd28 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f p, but do (of co
1fd29 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 urse) invoke.**
1fd2a 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 any expr callbac
1fd2b 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 ks and SELECT ca
1fd2c 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d llbacks that com
1fd2d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 e from subquerie
1fd2e 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 s..** Return WRC
1fd2f 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f _Abort or WRC_Co
1fd30 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ntinue..*/.SQLIT
1fd31 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fd32 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 lite3WalkSelectE
1fd33 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c xpr(Walker *pWal
1fd34 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b ker, Select *p){
1fd35 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 . if( sqlite3Wa
1fd36 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b lkExprList(pWalk
1fd37 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 er, p->pEList) )
1fd38 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fd39 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 t;. if( sqlite3
1fd3a 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1fd3b 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 , p->pWhere) ) r
1fd3c 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1fd3d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 . if( sqlite3Wa
1fd3e 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b lkExprList(pWalk
1fd3f 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 er, p->pGroupBy)
1fd40 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fd41 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1fd42 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b e3WalkExpr(pWalk
1fd43 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 er, p->pHaving)
1fd44 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fd45 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1fd46 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fd47 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 alker, p->pOrder
1fd48 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 By) ) return WRC
1fd49 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1fd4a 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1fd4b 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 alker, p->pLimit
1fd4c 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fd4d 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1fd4e 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fd4f 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 ker, p->pOffset)
1fd50 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fd51 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 ort;. return WR
1fd52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f C_Continue;.}../
1fd53 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 *.** Walk the pa
1fd54 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 rse trees associ
1fd55 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 ated with all su
1fd56 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a bqueries in the.
1fd57 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f ** FROM clause o
1fd58 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 f SELECT stateme
1fd59 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e nt p. Do not in
1fd5a 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a voke the select.
1fd5b 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 ** callback on p
1fd5c 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 , but do invoke
1fd5d 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 it on each FROM
1fd5e 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 0a clause subquery.
1fd5f 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 ** and on any su
1fd60 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65 72 bqueries further
1fd61 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 down in the tre
1fd62 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 e. Return .** W
1fd63 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f RC_Abort or WRC_
1fd64 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c Continue;.*/.SQL
1fd65 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1fd66 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1fd67 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 tFrom(Walker *pW
1fd68 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
1fd69 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 ){. SrcList *pS
1fd6a 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 rc;. int i;. s
1fd6b 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1fd6c 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 em *pItem;.. pS
1fd6d 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 rc = p->pSrc;.
1fd6e 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 if( ALWAYS(pSrc)
1fd6f 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 ){. for(i=pS
1fd70 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d rc->nSrc, pItem=
1fd71 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d pSrc->a; i>0; i-
1fd72 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
1fd73 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1fd74 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 lkSelect(pWalker
1fd75 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 , pItem->pSelect
1fd76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 ) ){. ret
1fd77 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1fd78 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1fd79 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f . return WRC_Co
1fd7a 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a ntinue;.} ../*.*
1fd7b 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 * Call sqlite3Wa
1fd7c 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 lkExpr() for eve
1fd7d 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ry expression in
1fd7e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e Select statemen
1fd7f 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 t p..** Invoke s
1fd80 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1fd81 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 () for subquerie
1fd82 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c s in the FROM cl
1fd83 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 ause and.** on t
1fd84 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 he compound sele
1fd85 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 ct chain, p->pPr
1fd86 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ior..**.** Retur
1fd87 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 n WRC_Continue u
1fd88 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 nder normal cond
1fd89 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 itions. Return
1fd8a 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 WRC_Abort if.**
1fd8b 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 there is an abor
1fd8c 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a t request..**.**
1fd8d 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 If the Walker d
1fd8e 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 oes not have an
1fd8f 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 xSelectCallback(
1fd90 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 ) then this rout
1fd91 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f ine.** is a no-o
1fd92 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f p returning WRC_
1fd93 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c Continue..*/.SQL
1fd94 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1fd95 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1fd96 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
1fd97 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 r, Select *p){.
1fd98 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 int rc;. if( p
1fd99 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e ==0 || pWalker->
1fd9a 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d xSelectCallback=
1fd9b 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f =0 ) return WRC_
1fd9c 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d Continue;. rc =
1fd9d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 WRC_Continue;.
1fd9e 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 while( p ){.
1fd9f 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e rc = pWalker->
1fda0 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 xSelectCallback(
1fda1 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 pWalker, p);.
1fda2 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b if( rc ) break;
1fda3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1fda4 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 WalkSelectExpr(p
1fda5 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 Walker, p) ) ret
1fda6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1fda7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 if( sqlite3Wa
1fda8 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 lkSelectFrom(pWa
1fda9 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 lker, p) ) retur
1fdaa 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1fdab 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a p = p->pPrior;.
1fdac 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
1fdad 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a & WRC_Abort;.}..
1fdae 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1fdaf 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 End of walker.c
1fdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1fdb3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1fdb4 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c Begin file resol
1fdb5 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve.c ***********
1fdb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1fdb8 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 /*.** 2008 Augus
1fdb9 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 18.**.** The a
1fdba 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1fdbb 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1fdbc 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1fdbd 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
1fdbe 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
1fdbf 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
1fdc0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
1fdc1 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
1fdc2 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1fdc3 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1fdc4 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1fdc5 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1fdc6 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1fdc7 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1fdc8 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1fdc9 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1fdca 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1fdcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fdcf 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
1fdd0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 ile contains rou
1fdd1 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 tines used for w
1fdd2 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 alking the parse
1fdd3 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 r tree and.** re
1fdd4 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 solve all identi
1fdd5 66 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 fiers by associa
1fdd6 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 ting them with a
1fdd7 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 particular.** t
1fdd8 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e able and column.
1fdd9 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .*/../*.** Turn
1fdda 74 68 65 20 70 45 78 70 72 20 65 78 70 72 65 73 the pExpr expres
1fddb 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 sion into an ali
1fddc 61 73 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d as for the iCol-
1fddd 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
1fdde 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 .** result set i
1fddf 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 n pEList..**.**
1fde0 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 If the result se
1fde1 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 t column is a si
1fde2 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 mple column refe
1fde3 72 65 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 rence, then this
1fde4 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 routine.** make
1fde5 73 20 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e s an exact copy.
1fde6 20 20 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 But for any ot
1fde7 68 65 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 her kind of expr
1fde8 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 ession, this.**
1fde9 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 routine make a c
1fdea 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c opy of the resul
1fdeb 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 t set column as
1fdec 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 the argument to
1fded 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 the.** TK_AS ope
1fdee 72 61 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 rator. The TK_A
1fdef 53 20 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 S operator cause
1fdf0 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e s the expression
1fdf1 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 to be.** evalua
1fdf2 74 65 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e ted just once an
1fdf3 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f d then reused fo
1fdf4 72 20 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a r each alias..**
1fdf5 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 .** The reason f
1fdf6 6f 72 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 or suppressing t
1fdf7 68 65 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 he TK_AS term wh
1fdf8 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f en the expressio
1fdf9 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a n is a simple.**
1fdfa 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 column referenc
1fdfb 65 20 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 e is so that the
1fdfc 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 column referenc
1fdfd 65 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e e will be recogn
1fdfe 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c ized as.** usabl
1fdff 65 20 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 e by indices wit
1fe00 68 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c hin the WHERE cl
1fe01 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 ause processing
1fe02 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 logic. .**.** Ha
1fe03 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f ck: The TK_AS o
1fe04 70 65 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 perator is inhib
1fe05 69 74 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d ited if zType[0]
1fe06 3d 3d 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 =='G'. This mea
1fe07 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 ns.** that in a
1fe08 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c GROUP BY clause,
1fe09 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1fe0a 69 73 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 is evaluated twi
1fe0b 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a ce. Hence:.**.*
1fe0c 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e * SELECT ran
1fe0d 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f dom()%5 AS x, co
1fe0e 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 unt(*) FROM tab
1fe0f 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a GROUP BY x.**.**
1fe10 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 Is equivalent t
1fe11 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c o:.**.** SEL
1fe12 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 ECT random()%5 A
1fe13 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 S x, count(*) FR
1fe14 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 OM tab GROUP BY
1fe15 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a random()%5.**.**
1fe16 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 The result of r
1fe17 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 andom()%5 in the
1fe18 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1fe19 20 69 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 is probably dif
1fe1a 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 ferent.** from t
1fe1b 68 65 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 he result in the
1fe1c 20 72 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 result-set. We
1fe1d 20 6d 69 67 68 74 20 66 69 78 20 74 68 69 73 20 might fix this
1fe1e 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 someday. Or.**
1fe1f 74 68 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d then again, we m
1fe20 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 ight not....*/.s
1fe21 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c tatic void resol
1fe22 76 65 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 veAlias(. Parse
1fe23 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1fe24 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1fe25 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 text */. ExprLi
1fe26 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 st *pEList,
1fe27 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 /* A result set
1fe28 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 */. int iCol,
1fe29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fe2a 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 A column in the
1fe2b 72 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e result set. 0..
1fe2c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 pEList->nExpr-1
1fe2d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
1fe2e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 , /* T
1fe2f 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e ransform this in
1fe30 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 to an alias to t
1fe31 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f he result set */
1fe32 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1fe33 54 79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 Type /* "GR
1fe34 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 OUP" or "ORDER"
1fe35 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 or "" */.){. Ex
1fe36 70 72 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 pr *pOrig;
1fe37 20 20 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c /* The iCol
1fe38 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 -th column of th
1fe39 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a e result set */.
1fe3a 20 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 Expr *pDup;
1fe3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 /* Copy
1fe3c 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 of pOrig */. s
1fe3d 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
1fe3e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
1fe3f 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1fe40 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
1fe41 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 Col>=0 && iCol<p
1fe42 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a EList->nExpr );.
1fe43 20 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 pOrig = pEList
1fe44 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b ->a[iCol].pExpr;
1fe45 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 . assert( pOrig
1fe46 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1fe47 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 pOrig->flags &
1fe48 45 50 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 EP_Resolved );.
1fe49 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1fe4a 3b 0a 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f ;. if( pOrig->o
1fe4b 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 p!=TK_COLUMN &&
1fe4c 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b zType[0]!='G' ){
1fe4d 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 . pDup = sqli
1fe4e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1fe4f 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 Orig, 0);. pD
1fe50 75 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 up = sqlite3PExp
1fe51 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c r(pParse, TK_AS,
1fe52 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 pDup, 0, 0);.
1fe53 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 if( pDup==0 )
1fe54 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 return;. if(
1fe55 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e pEList->a[iCol].
1fe56 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 iAlias==0 ){.
1fe57 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f pEList->a[iCo
1fe58 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 l].iAlias = (u16
1fe59 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 )(++pParse->nAli
1fe5a 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 as);. }. p
1fe5b 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 Dup->iTable = pE
1fe5c 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 List->a[iCol].iA
1fe5d 6c 69 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 lias;. }else if
1fe5e 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1fe5f 79 28 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 y(pOrig, EP_IntV
1fe60 61 6c 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e alue) || pOrig->
1fe61 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 u.zToken==0 ){.
1fe62 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1fe63 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 3ExprDup(db, pOr
1fe64 69 67 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ig, 0);. if(
1fe65 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e pDup==0 ) return
1fe66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 ;. }else{. c
1fe67 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f har *zToken = pO
1fe68 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 rig->u.zToken;.
1fe69 20 20 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 assert( zToke
1fe6a 6e 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 n!=0 );. pOri
1fe6b 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b g->u.zToken = 0;
1fe6c 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 . pDup = sqli
1fe6d 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1fe6e 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f Orig, 0);. pO
1fe6f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 rig->u.zToken =
1fe70 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 zToken;. if(
1fe71 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e pDup==0 ) return
1fe72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1fe73 44 75 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 Dup->flags & (EP
1fe74 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 _Reduced|EP_Toke
1fe75 6e 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 nOnly))==0 );.
1fe76 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c pDup->flags2 |
1fe77 3d 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f = EP2_MallocedTo
1fe78 6b 65 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 ken;. pDup->u
1fe79 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 .zToken = sqlite
1fe7a 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 3DbStrDup(db, zT
1fe7b 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 oken);. }. if(
1fe7c 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 pExpr->flags &
1fe7d 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b EP_ExpCollate ){
1fe7e 0a 20 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c . pDup->pColl
1fe7f 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b = pExpr->pColl;
1fe80 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 . pDup->flags
1fe81 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 |= EP_ExpCollat
1fe82 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 66 e;. }.. /* Bef
1fe83 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 ore calling sqli
1fe84 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 29 2c te3ExprDelete(),
1fe85 20 73 65 74 20 74 68 65 20 45 50 5f 53 74 61 74 set the EP_Stat
1fe86 69 63 20 66 6c 61 67 2e 20 54 68 69 73 20 0a 20 ic flag. This .
1fe87 20 2a 2a 20 70 72 65 76 65 6e 74 73 20 45 78 70 ** prevents Exp
1fe88 72 44 65 6c 65 74 65 28 29 20 66 72 6f 6d 20 64 rDelete() from d
1fe89 65 6c 65 74 69 6e 67 20 74 68 65 20 45 78 70 72 eleting the Expr
1fe8a 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c structure itsel
1fe8b 66 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 77 69 6e 67 f,. ** allowing
1fe8c 20 69 74 20 74 6f 20 62 65 20 72 65 70 6f 70 75 it to be repopu
1fe8d 6c 61 74 65 64 20 62 79 20 74 68 65 20 6d 65 6d lated by the mem
1fe8e 63 70 79 28 29 20 6f 6e 20 74 68 65 20 66 6f 6c cpy() on the fol
1fe8f 6c 6f 77 69 6e 67 20 6c 69 6e 65 2e 0a 20 20 2a lowing line.. *
1fe90 2f 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 /. ExprSetPrope
1fe91 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 53 74 rty(pExpr, EP_St
1fe92 61 74 69 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 atic);. sqlite3
1fe93 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1fe94 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 Expr);. memcpy(
1fe95 70 45 78 70 72 2c 20 70 44 75 70 2c 20 73 69 7a pExpr, pDup, siz
1fe96 65 6f 66 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 eof(*pExpr));.
1fe97 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1fe98 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , pDup);.}../*.*
1fe99 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 * Given the name
1fe9a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 of a column of
1fe9b 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f the form X.Y.Z o
1fe9c 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c r Y.Z or just Z,
1fe9d 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 look up.** that
1fe9e 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 65 74 name in the set
1fe9f 20 6f 66 20 73 6f 75 72 63 65 20 74 61 62 6c 65 of source table
1fea0 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20 61 6e s in pSrcList an
1fea1 64 20 6d 61 6b 65 20 74 68 65 20 70 45 78 70 72 d make the pExpr
1fea2 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 .** expression
1fea3 6e 6f 64 65 20 72 65 66 65 72 20 62 61 63 6b 20 node refer back
1fea4 74 6f 20 74 68 61 74 20 73 6f 75 72 63 65 20 63 to that source c
1fea5 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c olumn. The foll
1fea6 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a owing changes.**
1fea7 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78 are made to pEx
1fea8 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 pr:.**.** pEx
1fea9 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20 pr->iDb
1feaa 20 20 53 65 74 20 74 68 65 20 69 6e 64 65 78 20 Set the index
1feab 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 in db->aDb[] of
1feac 74 68 65 20 64 61 74 61 62 61 73 65 20 58 0a 2a the database X.*
1fead 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1feae 20 20 20 20 20 20 20 20 20 20 28 65 76 65 6e 20 (even
1feaf 69 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64 29 if X is implied)
1feb0 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 ..** pExpr->i
1feb1 54 61 62 6c 65 20 20 20 20 20 20 20 20 53 65 74 Table Set
1feb2 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6e to the cursor n
1feb3 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 umber for the ta
1feb4 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 ble obtained.**
1feb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1feb6 20 20 20 20 20 20 20 20 66 72 6f 6d 20 70 53 72 from pSr
1feb7 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 cList..** pEx
1feb8 70 72 2d 3e 70 54 61 62 20 20 20 20 20 20 20 20 pr->pTab
1feb9 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 Points to the
1feba 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 Table structure
1febb 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 69 66 0a of X.Y (even if.
1febc 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1febd 20 20 20 20 20 20 20 20 20 20 20 58 20 61 6e 64 X and
1febe 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 6c 69 65 /or Y are implie
1febf 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d d.).** pExpr-
1fec0 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 >iColumn S
1fec1 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e et to the column
1fec2 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 number within t
1fec3 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 he table..**
1fec4 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 pExpr->op
1fec5 20 20 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 Set to TK_C
1fec6 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 OLUMN..** pEx
1fec7 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20 pr->pLeft
1fec8 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e Any expression
1fec9 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 this points to
1feca 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 is deleted.**
1fecb 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20 pExpr->pRight
1fecc 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 Any expres
1fecd 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 sion this points
1fece 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a to is deleted..
1fecf 2a 2a 0a 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 **.** The zDb va
1fed0 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 riable is the na
1fed1 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1fed2 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 se (the "X"). T
1fed3 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 his value may be
1fed4 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 .** NULL meaning
1fed5 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 that name is of
1fed6 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 the form Y.Z or
1fed7 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 Z. Any availab
1fed8 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 le database.** c
1fed9 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 an be used. The
1feda 20 7a 54 61 62 6c 65 20 76 61 72 69 61 62 6c 65 zTable variable
1fedb 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
1fedc 74 68 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 the table (the "
1fedd 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 Y"). This.** va
1fede 6c 75 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 lue can be NULL
1fedf 69 66 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e if zDb is also N
1fee0 55 4c 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 ULL. If zTable
1fee1 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 is NULL it.** me
1fee2 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 ans that the for
1fee3 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 m of the name is
1fee4 20 5a 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 Z and that colu
1fee5 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 mns from any tab
1fee6 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 le.** can be use
1fee7 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1fee8 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 name cannot be r
1fee9 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 esolved unambigu
1feea 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 ously, leave an
1feeb 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
1feec 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 in pParse and r
1feed 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e eturn WRC_Abort.
1feee 20 20 52 65 74 75 72 6e 20 57 52 43 5f 50 72 75 Return WRC_Pru
1feef 6e 65 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a ne on success..*
1fef0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f /.static int loo
1fef1 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 kupName(. Parse
1fef2 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1fef3 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 /* The parsing c
1fef4 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 ontext */. cons
1fef5 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 t char *zDb,
1fef6 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1fef7 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1fef8 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 ing table, or NU
1fef9 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 LL */. const ch
1fefa 61 72 20 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 ar *zTab, /*
1fefb 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f Name of table co
1fefc 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c ntaining column,
1fefd 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f or NULL */. co
1fefe 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 nst char *zCol,
1feff 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1ff00 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e e column. */. N
1ff01 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
1ff02 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 /* The name
1ff03 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 context used to
1ff04 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 resolve the name
1ff05 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
1ff06 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 r /* Ma
1ff07 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 ke this EXPR nod
1ff08 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 e point to the s
1ff09 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a elected column *
1ff0a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b /.){. int i, j;
1ff0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1ff0c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a oop counters */.
1ff0d 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 int cnt = 0;
1ff0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff0f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1ff10 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e matching column
1ff11 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 names */. int
1ff12 63 6e 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 cntTab = 0;
1ff13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ff14 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 Number of match
1ff15 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 ing table names
1ff16 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1ff17 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 = pParse->db;
1ff18 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
1ff19 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1ff1a 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 n */. struct Sr
1ff1b 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 cList_item *pIte
1ff1c 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 m; /* Use
1ff1d 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 for looping over
1ff1e 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 pSrcList items
1ff1f 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c */. struct SrcL
1ff20 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 ist_item *pMatch
1ff21 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 = 0; /* The ma
1ff22 74 63 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 tching pSrcList
1ff23 69 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f item */. NameCo
1ff24 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 ntext *pTopNC =
1ff25 70 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 pNC; /* F
1ff26 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 irst namecontext
1ff27 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a in the list */.
1ff28 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
1ff29 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 a = 0;
1ff2a 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 /* Schema of
1ff2b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1ff2c 2a 2f 0a 20 20 69 6e 74 20 69 73 54 72 69 67 67 */. int isTrigg
1ff2d 65 72 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 er = 0;.. asser
1ff2e 74 28 20 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a t( pNC ); /*
1ff2f 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 the name contex
1ff30 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c t cannot be NULL
1ff31 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a . */. assert( z
1ff32 43 6f 6c 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 Col ); /* The
1ff33 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e Z in X.Y.Z cann
1ff34 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 ot be NULL */.
1ff35 61 73 73 65 72 74 28 20 7e 45 78 70 72 48 61 73 assert( ~ExprHas
1ff36 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
1ff37 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c r, EP_TokenOnly|
1ff38 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 0a EP_Reduced) );..
1ff39 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
1ff3a 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d the node to no-m
1ff3b 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d atch */. pExpr-
1ff3c 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 >iTable = -1;.
1ff3d 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b pExpr->pTab = 0;
1ff3e 0a 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75 . ExprSetIrredu
1ff3f 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 cible(pExpr);..
1ff40 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74 68 65 /* Start at the
1ff41 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 inner-most cont
1ff42 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 ext and move out
1ff43 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 ward until a mat
1ff44 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 ch is found */.
1ff45 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 63 while( pNC && c
1ff46 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 nt==0 ){. Exp
1ff47 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 rList *pEList;.
1ff48 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 SrcList *pSrc
1ff49 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 List = pNC->pSrc
1ff4a 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 List;.. if( p
1ff4b 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 SrcList ){.
1ff4c 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d for(i=0, pItem=
1ff4d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 pSrcList->a; i<p
1ff4e 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 SrcList->nSrc; i
1ff4f 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
1ff50 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
1ff51 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 b;. int i
1ff52 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 Db;. Colu
1ff53 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 mn *pCol;. .
1ff54 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 74 65 pTab = pIte
1ff55 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 m->pTab;.
1ff56 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 assert( pTab!=0
1ff57 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 && pTab->zName!
1ff58 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 44 =0 );. iD
1ff59 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1ff5a 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
1ff5b 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
1ff5c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 assert( pTa
1ff5d 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 b->nCol>0 );.
1ff5e 20 20 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b if( zTab ){
1ff5f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1ff60 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a Item->zAlias ){.
1ff61 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 char
1ff62 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 *zTabName = pIt
1ff63 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 em->zAlias;.
1ff64 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
1ff65 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e te3StrICmp(zTabN
1ff66 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 ame, zTab)!=0 )
1ff67 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
1ff68 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ff69 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 char *zTa
1ff6a 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e bName = pTab->zN
1ff6b 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ame;.
1ff6c 20 69 66 28 20 4e 45 56 45 52 28 7a 54 61 62 4e if( NEVER(zTabN
1ff6d 61 6d 65 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 ame==0) || sqlit
1ff6e 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 e3StrICmp(zTabNa
1ff6f 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a me, zTab)!=0 ){.
1ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f co
1ff71 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
1ff72 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1ff73 20 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 if( zDb!=0 &&
1ff74 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 sqlite3StrICmp(d
1ff75 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
1ff76 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 e, zDb)!=0 ){.
1ff77 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 cont
1ff78 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 inue;.
1ff79 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
1ff7a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1ff7b 20 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 if( 0==(cntTab
1ff7c 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ++) ){.
1ff7d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1ff7e 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b pItem->iCursor;
1ff7f 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 . pExpr
1ff80 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 ->pTab = pTab;.
1ff81 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 pSchema
1ff82 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 = pTab->pSchema
1ff83 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 ;. pMat
1ff84 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 ch = pItem;.
1ff85 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f }. fo
1ff86 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 r(j=0, pCol=pTab
1ff87 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e ->aCol; j<pTab->
1ff88 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b nCol; j++, pCol+
1ff89 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
1ff8a 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1ff8b 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 (pCol->zName, zC
1ff8c 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1ff8d 20 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 IdList *pU
1ff8e 73 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 sing;.
1ff8f 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 cnt++;.
1ff90 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1ff91 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 le = pItem->iCur
1ff92 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 sor;.
1ff93 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 pExpr->pTab = p
1ff94 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Tab;.
1ff95 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b pMatch = pItem;
1ff96 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 . pSc
1ff97 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1ff98 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 hema;.
1ff99 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 /* Substitute
1ff9a 74 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d the rowid (colum
1ff9b 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e n -1) for the IN
1ff9c 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
1ff9d 59 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 Y */.
1ff9e 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1ff9f 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 = j==pTab->iPKey
1ffa0 20 3f 20 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a ? -1 : (i16)j;.
1ffa1 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1ffa2 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 i<pSrcList->nSrc
1ffa3 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 -1 ){.
1ffa4 20 20 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d if( pItem[1]
1ffa5 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e .jointype & JT_N
1ffa6 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 ATURAL ){.
1ffa7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
1ffa8 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 this match occur
1ffa9 72 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20 red in the left
1ffaa 74 61 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 table of a natur
1ffab 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 al join,.
1ffac 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e ** then
1ffad 20 73 6b 69 70 20 74 68 65 20 72 69 67 68 74 20 skip the right
1ffae 74 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 table to avoid a
1ffaf 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 duplicate match
1ffb0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
1ffb1 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 pItem++;.
1ffb2 20 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b i++
1ffb3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1ffb4 7d 65 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e }else if( (pUsin
1ffb5 67 20 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 g = pItem[1].pUs
1ffb6 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ing)!=0 ){.
1ffb7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
1ffb8 20 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 this match occu
1ffb9 72 73 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 rs on a column t
1ffba 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 55 53 hat is in the US
1ffbb 49 4e 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20 ING clause.
1ffbc 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 ** of
1ffbd 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 a join, skip th
1ffbe 65 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 e search of the
1ffbf 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 right table of t
1ffc0 68 65 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 he join.
1ffc1 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 ** to av
1ffc2 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 oid a duplicate
1ffc3 6d 61 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a match there. */.
1ffc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffc5 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 int k;.
1ffc6 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 for(k=0;
1ffc7 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b k<pUsing->nId; k
1ffc8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
1ffc9 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
1ffca 65 33 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 e3StrICmp(pUsing
1ffcb 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 ->a[k].zName, zC
1ffcc 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1ffcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
1ffce 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 tem++;.
1ffcf 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a i++;.
1ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffd1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1ffd2 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
1ffd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d }
1ffd4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d . }
1ffd5 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
1ffd6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
1ffd7 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1ffd8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1ffd9 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }..#ifndef S
1ffda 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
1ffdb 45 52 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ER. /* If we
1ffdc 68 61 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 have not already
1ffdd 20 72 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 resolved the na
1ffde 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a me, then maybe .
1ffdf 20 20 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e ** it is a n
1ffe0 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 ew.* or old.* tr
1ffe1 69 67 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72 igger argument r
1ffe2 65 66 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a eference. */.
1ffe3 20 20 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 if( zDb==0 &
1ffe4 26 20 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 & zTab!=0 && cnt
1ffe5 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 ==0 && pParse->p
1ffe6 54 72 69 67 67 65 72 54 61 62 21 3d 30 20 29 7b TriggerTab!=0 ){
1ffe7 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 . int op =
1ffe8 70 50 61 72 73 65 2d 3e 65 54 72 69 67 67 65 72 pParse->eTrigger
1ffe9 4f 70 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 Op;. Table
1ffea 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 *pTab = 0;.
1ffeb 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f assert( op==TK_
1ffec 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b DELETE || op==TK
1ffed 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 _UPDATE || op==T
1ffee 4b 5f 49 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 K_INSERT );.
1ffef 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c if( op!=TK_DEL
1fff0 45 54 45 20 26 26 20 73 71 6c 69 74 65 33 53 74 ETE && sqlite3St
1fff1 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 rICmp("new",zTab
1fff2 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 ) == 0 ){.
1fff3 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1fff4 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 54 61 = 1;. pTa
1fff5 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 b = pParse->pTri
1fff6 67 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d ggerTab;. }
1fff7 65 6c 73 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f else if( op!=TK_
1fff8 49 4e 53 45 52 54 20 26 26 20 73 71 6c 69 74 65 INSERT && sqlite
1fff9 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 3StrICmp("old",z
1fffa 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Tab)==0 ){.
1fffb 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1fffc 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 = 0;. pT
1fffd 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 ab = pParse->pTr
1fffe 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20 iggerTab;.
1ffff 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 }.. if( pTa
20000 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e b ){ . in
20001 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 t iCol;.
20002 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
20003 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
20004 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 cntTab++;.
20005 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
20006 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 Rowid(zCol) ){.
20007 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 iCol =
20008 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 -1;. }els
20009 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 e{. for
2000a 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 (iCol=0; iCol<pT
2000b 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b ab->nCol; iCol++
2000c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 43 ){. C
2000d 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 olumn *pCol = &p
2000e 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b Tab->aCol[iCol];
2000f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
20010 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
20011 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f pCol->zName, zCo
20012 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
20013 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d if( iCol=
20014 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a =pTab->iPKey ){.
20015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20016 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 iCol = -1;.
20017 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
20018 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
20019 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
2001a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2001b 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
2001c 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 iCol<pTab->nCol
2001d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 ){. cnt
2001e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ++;. if
2001f 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 ( iCol<0 ){.
20020 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 pExpr->a
20021 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 ffinity = SQLITE
20022 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 _AFF_INTEGER;.
20023 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
20024 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d ( pExpr->iTable=
20025 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
20026 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
20027 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 ==31 );.
20028 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 testcase( iC
20029 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 ol==32 );.
2002a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c pParse->ol
2002b 64 6d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d dmask |= (iCol>=
2002c 33 32 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 32 ? 0xffffffff
2002d 3a 20 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f : (((u32)1)<<iCo
2002e 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d l));. }
2002f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
20030 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
20031 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 ==31 );.
20032 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 testcase( iC
20033 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 ol==32 );.
20034 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 65 pParse->ne
20035 77 6d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d wmask |= (iCol>=
20036 33 32 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 32 ? 0xffffffff
20037 3a 20 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f : (((u32)1)<<iCo
20038 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d l));. }
20039 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 . pExpr
2003a 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 ->iColumn = (i16
2003b 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 )iCol;.
2003c 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 pExpr->pTab = p
2003d 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Tab;. i
2003e 73 54 72 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 sTrigger = 1;.
2003f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
20040 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
20041 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
20042 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f OMIT_TRIGGER) */
20043 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
20044 50 65 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 Perhaps the name
20045 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 is a reference
20046 74 6f 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 to the ROWID.
20047 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d */. if( cnt=
20048 3d 30 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 =0 && cntTab==1
20049 26 26 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 && sqlite3IsRowi
2004a 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 d(zCol) ){.
2004b 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 cnt = 1;.
2004c 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
2004d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 -1;. pExpr
2004e 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c ->affinity = SQL
2004f 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b ITE_AFF_INTEGER;
20050 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 . }.. /*.
20051 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 ** If the inp
20052 75 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 ut is of the for
20053 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 m Z (not Y.Z or
20054 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 X.Y.Z) then the
20055 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 name Z. ** mi
20056 67 68 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 ght refer to an
20057 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 result-set alias
20058 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c . This happens,
20059 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 for example, wh
2005a 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 en. ** we are
2005b 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 resolving names
2005c 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c in the WHERE cl
2005d 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ause of the foll
2005e 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 owing command:.
2005f 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
20060 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 SELECT a+b AS x
20061 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 FROM table WHER
20062 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 E x<10;. **.
20063 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c ** In cases l
20064 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 ike this, replac
20065 65 20 70 45 78 70 72 20 77 69 74 68 20 61 20 63 e pExpr with a c
20066 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 opy of the expre
20067 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a ssion that. *
20068 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 * forms the resu
20069 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 lt set entry ("a
2006a 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 +b" in the examp
2006b 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 le) and return i
2006c 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 mmediately..
2006d 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
2006e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
2006f 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 he result set sh
20070 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 ould have alread
20071 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 y been. ** re
20072 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 solved by the ti
20073 6d 65 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 me the WHERE cla
20074 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e use is resolved.
20075 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
20076 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 cnt==0 && (pELis
20077 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 t = pNC->pEList)
20078 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 !=0 && zTab==0 )
20079 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b {. for(j=0;
2007a 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 j<pEList->nExpr
2007b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
2007c 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 char *zAs = pELi
2007d 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a st->a[j].zName;.
2007e 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 if( zAs!
2007f 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 =0 && sqlite3Str
20080 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d ICmp(zAs, zCol)=
20081 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
20082 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 Expr *pOrig;.
20083 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
20084 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 Expr->pLeft==0 &
20085 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d & pExpr->pRight=
20086 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
20087 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 assert( pExpr->x
20088 2e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 .pList==0 );.
20089 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
2008a 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d Expr->x.pSelect=
2008b 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
2008c 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e pOrig = pEList->
2008d 61 5b 6a 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[j].pExpr;.
2008e 20 20 20 20 20 20 69 66 28 20 21 70 4e 43 2d 3e if( !pNC->
2008f 61 6c 6c 6f 77 41 67 67 20 26 26 20 45 78 70 72 allowAgg && Expr
20090 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 HasProperty(pOri
20091 67 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 g, EP_Agg) ){.
20092 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
20093 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20094 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 , "misuse of ali
20095 61 73 65 64 20 61 67 67 72 65 67 61 74 65 20 25 ased aggregate %
20096 73 22 2c 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 s", zAs);.
20097 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
20098 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 _Abort;.
20099 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65 }. re
2009a 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 solveAlias(pPars
2009b 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 e, pEList, j, pE
2009c 78 70 72 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 xpr, "");.
2009d 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 cnt = 1;.
2009e 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 pMatch =
2009f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 0;. ass
200a0 65 72 74 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 ert( zTab==0 &&
200a1 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 zDb==0 );.
200a2 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e goto lookupn
200a3 61 6d 65 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 ame_end;.
200a4 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 }. } .
200a5 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 }.. /* Advanc
200a6 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 e to the next na
200a7 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 me context. The
200a8 20 6c 6f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 loop will exit
200a9 77 68 65 6e 20 65 69 74 68 65 72 0a 20 20 20 20 when either.
200aa 2a 2a 20 77 65 20 68 61 76 65 20 61 20 6d 61 74 ** we have a mat
200ab 63 68 20 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 ch (cnt>0) or wh
200ac 65 6e 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 en we run out of
200ad 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a name contexts..
200ae 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 */. if( c
200af 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 nt==0 ){. p
200b0 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b NC = pNC->pNext;
200b1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
200b2 0a 20 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 . ** If X and Y
200b3 20 61 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 are NULL (in ot
200b4 68 65 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c her words if onl
200b5 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d y the column nam
200b6 65 20 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 e Z is. ** supp
200b7 6c 69 65 64 29 20 61 6e 64 20 74 68 65 20 76 61 lied) and the va
200b8 6c 75 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c lue of Z is encl
200b9 6f 73 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 osed in double-q
200ba 75 6f 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a uotes, then. **
200bb 20 5a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c Z is a string l
200bc 69 74 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65 iteral if it doe
200bd 73 6e 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63 sn't match any c
200be 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e olumn names. In
200bf 20 74 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c that. ** case,
200c0 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75 we need to retu
200c1 72 6e 20 72 69 67 68 74 20 61 77 61 79 20 61 6e rn right away an
200c2 64 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 d not make any c
200c3 68 61 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 hanges to. ** p
200c4 45 78 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 Expr.. **. **
200c5 42 65 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72 Because no refer
200c6 65 6e 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f ence was made to
200c7 20 6f 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c outer contexts,
200c8 20 74 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 the pNC->nRef.
200c9 20 2a 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e ** fields are n
200ca 6f 74 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e ot changed in an
200cb 79 20 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a y context.. */.
200cc 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 if( cnt==0 &&
200cd 7a 54 61 62 3d 3d 30 20 26 26 20 45 78 70 72 48 zTab==0 && ExprH
200ce 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
200cf 2c 45 50 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 ,EP_DblQuoted) )
200d0 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 {. pExpr->op
200d1 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 = TK_STRING;.
200d2 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 pExpr->pTab = 0
200d3 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 ;. return WRC
200d4 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f _Prune;. }.. /
200d5 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 *. ** cnt==0 me
200d6 61 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f ans there was no
200d7 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 t match. cnt>1
200d8 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 means there were
200d9 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 two or. ** mor
200da 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 e matches. Eith
200db 65 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 er way, we have
200dc 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 an error.. */.
200dd 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 if( cnt!=1 ){.
200de 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
200df 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 Err;. zErr =
200e0 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 cnt==0 ? "no suc
200e1 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 h column" : "amb
200e2 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 iguous column na
200e3 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 me";. if( zDb
200e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
200e5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
200e6 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 , "%s: %s.%s.%s"
200e7 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 , zErr, zDb, zTa
200e8 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 b, zCol);. }e
200e9 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a lse if( zTab ){.
200ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
200eb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
200ec 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c s: %s.%s", zErr,
200ed 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 zTab, zCol);.
200ee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
200ef 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
200f0 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c Parse, "%s: %s",
200f1 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 zErr, zCol);.
200f2 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e }. pTopNC->
200f3 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f nErr++;. }.. /
200f4 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 * If a column fr
200f5 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53 om a table in pS
200f6 72 63 4c 69 73 74 20 69 73 20 72 65 66 65 72 65 rcList is refere
200f7 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 nced, then recor
200f8 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74 d. ** this fact
200f9 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74 in the pSrcList
200fa 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 .a[].colUsed bit
200fb 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 mask. Column 0
200fc 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 causes. ** bit
200fd 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 0 to be set. Co
200fe 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20 lumn 1 sets bit
200ff 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 1. And so forth
20100 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 . If the. ** c
20101 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 olumn number is
20102 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
20103 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
20104 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 in the bitmask.
20105 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65 ** then set the
20106 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 high-order bit
20107 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a of the bitmask..
20108 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 */. if( pExpr
20109 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 ->iColumn>=0 &&
2010a 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 pMatch!=0 ){.
2010b 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e int n = pExpr->
2010c 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 iColumn;. tes
2010d 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 tcase( n==BMS-1
2010e 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d );. if( n>=BM
2010f 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 S ){. n = B
20110 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 MS-1;. }.
20111 61 73 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e assert( pMatch->
20112 69 43 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e iCursor==pExpr->
20113 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d iTable );. pM
20114 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d atch->colUsed |=
20115 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e ((Bitmask)1)<<n
20116 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 ;. }.. /* Clea
20117 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a n up and return.
20118 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 */. sqlite3Ex
20119 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 prDelete(db, pEx
2011a 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 pr->pLeft);. pE
2011b 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a xpr->pLeft = 0;.
2011c 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
2011d 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 ete(db, pExpr->p
2011e 52 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d Right);. pExpr-
2011f 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 >pRight = 0;. p
20120 45 78 70 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 Expr->op = (isTr
20121 69 67 67 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 igger ? TK_TRIGG
20122 45 52 20 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b ER : TK_COLUMN);
20123 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a .lookupname_end:
20124 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b . if( cnt==1 ){
20125 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 . assert( pNC
20126 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 !=0 );. sqlit
20127 65 33 41 75 74 68 52 65 61 64 28 70 50 61 72 73 e3AuthRead(pPars
20128 65 2c 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d e, pExpr, pSchem
20129 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 a, pNC->pSrcList
2012a 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d );. /* Increm
2012b 65 6e 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c ent the nRef val
2012c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 ue on all name c
2012d 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 ontexts from Top
2012e 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 NC up to. **
2012f 74 68 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20 the point where
20130 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 the name matched
20131 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 . */. for(;;)
20132 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
20133 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 pTopNC!=0 );.
20134 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b pTopNC->nRef+
20135 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f +;. if( pTo
20136 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b pNC==pNC ) break
20137 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d ;. pTopNC =
20138 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a pTopNC->pNext;.
20139 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
2013a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 WRC_Prune;. }
2013b 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 else {. retur
2013c 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d n WRC_Abort;. }
2013d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
2013e 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 te and return a
2013f 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 pointer to an ex
20140 70 72 65 73 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 pression to load
20141 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c the column iCol
20142 0a 2a 2a 20 66 72 6f 6d 20 64 61 74 61 73 6f 75 .** from datasou
20143 72 63 65 20 69 53 72 63 20 64 61 74 61 73 6f 75 rce iSrc datasou
20144 72 63 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70 rce in SrcList p
20145 53 72 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 Src..*/.SQLITE_P
20146 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
20147 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e ite3CreateColumn
20148 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62 Expr(sqlite3 *db
20149 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c , SrcList *pSrc,
2014a 20 69 6e 74 20 69 53 72 63 2c 20 69 6e 74 20 69 int iSrc, int i
2014b 43 6f 6c 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 Col){. Expr *p
2014c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c = sqlite3ExprAll
2014d 6f 63 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e oc(db, TK_COLUMN
2014e 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 , 0, 0);. if( p
2014f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 ){. struct S
20150 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 rcList_item *pIt
20151 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 53 em = &pSrc->a[iS
20152 72 63 5d 3b 0a 20 20 20 20 70 2d 3e 70 54 61 62 rc];. p->pTab
20153 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a = pItem->pTab;.
20154 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 p->iTable =
20155 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a pItem->iCursor;.
20156 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 2d if( p->pTab-
20157 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a >iPKey==iCol ){.
20158 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e p->iColumn
20159 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 = -1;. }else
2015a 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 {. p->iColu
2015b 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69 43 6f 6c mn = (ynVar)iCol
2015c 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 63 ;. pItem->c
2015d 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d olUsed |= ((Bitm
2015e 61 73 6b 29 31 29 3c 3c 28 69 43 6f 6c 3e 3d 42 ask)1)<<(iCol>=B
2015f 4d 53 20 3f 20 42 4d 53 2d 31 20 3a 20 69 43 6f MS ? BMS-1 : iCo
20160 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 45 78 l);. }. Ex
20161 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c prSetProperty(p,
20162 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 20 EP_Resolved);.
20163 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
20164 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
20165 74 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b tine is callback
20166 20 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b for sqlite3Walk
20167 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 Expr()..**.** Re
20168 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e solve symbolic n
20169 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c ames into TK_COL
2016a 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f UMN operators fo
2016b 72 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a r the current.**
2016c 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 node in the exp
2016d 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 ression tree. R
2016e 65 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 eturn 0 to conti
2016f 6e 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64 nue the search d
20170 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 own.** the tree
20171 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 or 2 to abort th
20172 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a e tree walk..**.
20173 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
20174 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 also does error
20175 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d checking and nam
20176 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 e resolution for
20177 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d .** function nam
20178 65 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f es. The operato
20179 72 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 r for aggregate
2017a 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 functions is cha
2017b 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 nged.** to TK_AG
2017c 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 G_FUNCTION..*/.s
2017d 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
2017e 65 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 eExprStep(Walker
2017f 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 *pWalker, Expr
20180 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 *pExpr){. NameC
20181 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 ontext *pNC;. P
20182 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 arse *pParse;..
20183 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e pNC = pWalker->
20184 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 u.pNC;. assert(
20185 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 pNC!=0 );. pPa
20186 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 rse = pNC->pPars
20187 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 e;. assert( pPa
20188 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 rse==pWalker->pP
20189 61 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 arse );.. if( E
2018a 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
2018b 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f y(pExpr, EP_Reso
2018c 6c 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 lved) ) return W
2018d 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 RC_Prune;. Expr
2018e 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 SetProperty(pExp
2018f 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b r, EP_Resolved);
20190 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
20191 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c if( pNC->pSrcL
20192 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 ist && pNC->pSrc
20193 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 List->nAlloc>0 )
20194 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 {. SrcList *p
20195 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 SrcList = pNC->p
20196 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 SrcList;. int
20197 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b i;. for(i=0;
20198 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 i<pNC->pSrcList
20199 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 ->nSrc; i++){.
2019a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 assert( pSrc
2019b 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 List->a[i].iCurs
2019c 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 or>=0 && pSrcLis
2019d 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c t->a[i].iCursor<
2019e 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 pParse->nTab);.
2019f 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
201a0 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
201a1 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 >op ){..#if defi
201a2 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
201a3 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f E_UPDATE_DELETE_
201a4 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e LIMIT) && !defin
201a5 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
201a6 55 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 UBQUERY). /*
201a7 54 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 The special oper
201a8 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e ator TK_ROW mean
201a9 73 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 s use the rowid
201aa 66 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 for the first.
201ab 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 ** column in t
201ac 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 he FROM clause.
201ad 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
201ae 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f the LIMIT and O
201af 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 RDER BY. ** c
201b0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 lause processing
201b1 20 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 on UPDATE and D
201b2 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 ELETE statements
201b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
201b4 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 e TK_ROW: {.
201b5 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c SrcList *pSrcL
201b6 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c ist = pNC->pSrcL
201b7 69 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 ist;. struc
201b8 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
201b9 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 pItem;. ass
201ba 65 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 ert( pSrcList &&
201bb 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d pSrcList->nSrc=
201bc 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 =1 );. pIte
201bd 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b m = pSrcList->a;
201be 20 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f . pExpr->o
201bf 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 p = TK_COLUMN;.
201c0 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 pExpr->pTab
201c1 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a = pItem->pTab;.
201c2 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 pExpr->iTa
201c3 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 ble = pItem->iCu
201c4 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 rsor;. pExp
201c5 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b r->iColumn = -1;
201c6 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 . pExpr->af
201c7 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f finity = SQLITE_
201c8 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
201c9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
201ca 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 #endif /* define
201cb 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
201cc 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 UPDATE_DELETE_LI
201cd 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 MIT) && !defined
201ce 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
201cf 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f QUERY) */.. /
201d0 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 * A lone identif
201d1 69 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ier is the name
201d2 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 of a column..
201d3 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
201d4 49 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 ID: {. retu
201d5 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 rn lookupName(pP
201d6 61 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 arse, 0, 0, pExp
201d7 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 r->u.zToken, pNC
201d8 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a , pExpr);. }.
201d9 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c . /* A tabl
201da 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d e name and colum
201db 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 n name: ID.I
201dc 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 D. ** Or a da
201dd 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e tabase, table an
201de 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 d column: ID.ID
201df 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 .ID. */. c
201e0 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 ase TK_DOT: {.
201e1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
201e2 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 zColumn;. c
201e3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c onst char *zTabl
201e4 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 e;. const c
201e5 68 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 har *zDb;.
201e6 45 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 Expr *pRight;..
201e7 20 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 /* if( pSrc
201e8 4c 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b List==0 ) break;
201e9 20 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 */. pRight
201ea 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 = pExpr->pRight
201eb 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 ;. if( pRig
201ec 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b ht->op==TK_ID ){
201ed 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 . zDb = 0
201ee 3b 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 ;. zTable
201ef 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d = pExpr->pLeft-
201f0 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 >u.zToken;.
201f1 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 zColumn = pRi
201f2 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 ght->u.zToken;.
201f3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
201f4 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 assert( pRig
201f5 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 ht->op==TK_DOT )
201f6 3b 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 ;. zDb =
201f7 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e pExpr->pLeft->u.
201f8 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 zToken;.
201f9 7a 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d zTable = pRight-
201fa 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e >pLeft->u.zToken
201fb 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d ;. zColum
201fc 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 n = pRight->pRig
201fd 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 ht->u.zToken;.
201fe 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
201ff 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 rn lookupName(pP
20200 61 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c arse, zDb, zTabl
20201 65 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c e, zColumn, pNC,
20202 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a pExpr);. }..
20203 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 /* Resolve f
20204 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 unction names.
20205 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b */. case TK
20206 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 _CONST_FUNC:.
20207 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f case TK_FUNCTIO
20208 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c N: {. ExprL
20209 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 ist *pList = pEx
2020a 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 pr->x.pList;
2020b 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 /* The argument
2020c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e list */. in
2020d 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c t n = pList ? pL
2020e 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 ist->nExpr : 0;
2020f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
20210 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 arguments */.
20211 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 int no_such_f
20212 75 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f unc = 0; /
20213 2a 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 * True if no suc
20214 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 h function exist
20215 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 s */. int w
20216 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 rong_num_args =
20217 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 0; /* True i
20218 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f f wrong number o
20219 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 f arguments */.
2021a 20 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 int is_agg
2021b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
2021c 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 /* True if is a
2021d 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
2021e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e tion */. in
2021f 74 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 t auth;
20220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 /* Aut
20221 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 horization to us
20222 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a e the function *
20223 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b /. int nId;
20224 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20225 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
20226 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 characters in f
20227 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
20228 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
20229 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 *zId;
2022a 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f /* The functio
2022b 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 n name. */.
2022c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 FuncDef *pDef;
2022d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2022e 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
2022f 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a t the function *
20230 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d /. u8 enc =
20231 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 ENC(pParse->db)
20232 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 ; /* The datab
20233 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a ase encoding */.
20234 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20235 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 pExpr->op==TK_C
20236 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 ONST_FUNC );.
20237 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
20238 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
20239 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
2023a 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 );. zId =
2023b 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pExpr->u.zToken;
2023c 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c . nId = sql
2023d 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 ite3Strlen30(zId
2023e 29 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 );. pDef =
2023f 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
20240 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ion(pParse->db,
20241 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 zId, nId, n, enc
20242 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
20243 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pDef==0 ){.
20244 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 pDef = sqlite
20245 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 3FindFunction(pP
20246 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e arse->db, zId, n
20247 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b Id, -1, enc, 0);
20248 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65 . if( pDe
20249 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 f==0 ){.
2024a 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d no_such_func =
2024b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1;. }els
2024c 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f e{. wro
2024d 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b ng_num_args = 1;
2024e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2024f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
20250 69 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 is_agg = pDef->x
20251 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d Func==0;. }
20252 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20253 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
20254 4f 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 ON. if( pDe
20255 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 f ){. aut
20256 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 h = sqlite3AuthC
20257 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c heck(pParse, SQL
20258 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c ITE_FUNCTION, 0,
20259 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 pDef->zName, 0)
2025a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75 ;. if( au
2025b 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b th!=SQLITE_OK ){
2025c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 . if( a
2025d 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 uth==SQLITE_DENY
2025e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2025f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20260 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 pParse, "not aut
20261 68 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 horized to use f
20262 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 unction: %s",.
20263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20265 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a pDef->zName);.
20266 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d pNC-
20267 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 >nErr++;.
20268 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
20269 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 Expr->op = TK_NU
2026a 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 LL;. re
2026b 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
2026c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2026d 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 }.#endif. i
2026e 66 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e f( is_agg && !pN
2026f 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 C->allowAgg ){.
20270 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
20271 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
20272 6d 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 misuse of aggreg
20273 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a ate function %.*
20274 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a s()", nId,zId);.
20275 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 pNC->nEr
20276 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f r++;. is_
20277 61 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d agg = 0;. }
20278 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 else if( no_such
20279 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 _func ){.
2027a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
2027b 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 (pParse, "no suc
2027c 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 h function: %.*s
2027d 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 ", nId, zId);.
2027e 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b pNC->nErr+
2027f 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 +;. }else i
20280 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 f( wrong_num_arg
20281 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c s ){. sql
20282 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
20283 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 rse,"wrong numbe
20284 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
20285 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 o function %.*s(
20286 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 )",.
20287 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 nId, zId);.
20288 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b pNC->nErr++;
20289 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
2028a 66 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 f( is_agg ){.
2028b 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d pExpr->op =
2028c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
2028d 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 ;. pNC->h
2028e 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 asAgg = 1;.
2028f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f }. if( is_
20290 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 agg ) pNC->allow
20291 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 Agg = 0;. s
20292 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 qlite3WalkExprLi
20293 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 st(pWalker, pLis
20294 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 t);. if( is
20295 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f _agg ) pNC->allo
20296 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 wAgg = 1;.
20297 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 /* FIX ME: Comp
20298 75 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e ute pExpr->affin
20299 69 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ity based on the
2029a 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e expected return
2029b 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f . ** type o
2029c 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a f the function .
2029d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
2029e 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b eturn WRC_Prune;
2029f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
202a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
202a1 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ERY. case TK_
202a2 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 SELECT:. case
202a3 20 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 TK_EXISTS: tes
202a4 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
202a5 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 ==TK_EXISTS );.#
202a6 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 endif. case T
202a7 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 K_IN: {. te
202a8 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
202a9 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 p==TK_IN );.
202aa 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
202ab 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
202ac 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
202ad 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d int nRef =
202ae 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e pNC->nRef;.#ifn
202af 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
202b0 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 CHECK. if
202b1 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 ( pNC->isCheck )
202b2 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
202b3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
202b4 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 se,"subqueries p
202b5 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 rohibited in CHE
202b6 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 CK constraints")
202b7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 ;. }.#end
202b8 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 if. sqlit
202b9 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 e3WalkSelect(pWa
202ba 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 lker, pExpr->x.p
202bb 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 Select);.
202bc 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 assert( pNC->nR
202bd 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 ef>=nRef );.
202be 20 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e if( nRef!=pN
202bf 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 C->nRef ){.
202c0 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 ExprSetProp
202c1 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 erty(pExpr, EP_V
202c2 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 arSelect);.
202c3 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
202c4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
202c5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
202c6 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 MIT_CHECK. ca
202c7 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 se TK_VARIABLE:
202c8 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d {. if( pNC-
202c9 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 >isCheck ){.
202ca 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
202cb 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 Msg(pParse,"para
202cc 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 meters prohibite
202cd 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 d in CHECK const
202ce 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 raints");.
202cf 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
202d0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
202d1 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 return (pParse
202d2 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 ->nErr || pParse
202d3 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
202d4 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 ed) ? WRC_Abort
202d5 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a : WRC_Continue;.
202d6 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 }../*.** pEList
202d7 69 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 is a list of exp
202d8 72 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 ressions which a
202d9 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 re really the re
202da 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a sult set of the.
202db 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 ** a SELECT stat
202dc 65 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 ement. pE is a
202dd 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 term in an ORDER
202de 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 BY or GROUP BY
202df 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 clause..** This
202e0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 routine checks t
202e1 6f 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 o see if pE is a
202e2 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 simple identifi
202e3 65 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 er which corresp
202e4 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 onds.** to the A
202e5 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 S-name of one of
202e6 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 the terms of th
202e7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
202e8 74 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a t. If it is,.**
202e9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
202ea 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 turn an integer
202eb 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 between 1 and N
202ec 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e where N is the n
202ed 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d umber of.** elem
202ee 65 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 ents in pEList,
202ef 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
202f0 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e the matching en
202f1 74 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 try. If there i
202f2 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f s.** no match, o
202f3 72 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 r if pE is not a
202f4 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 simple identifi
202f5 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f er, then this ro
202f6 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 utine.** return
202f7 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 0..**.** pEList
202f8 68 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 has been resolve
202f9 64 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a d. pE has not..
202fa 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
202fb 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 solveAsName(. P
202fc 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
202fd 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
202fe 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
202ff 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 essages */. Exp
20300 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 rList *pEList,
20301 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 /* List of expre
20302 73 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a ssions to scan *
20303 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 /. Expr *pE
20304 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 /* Expres
20305 73 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 sion we are tryi
20306 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 ng to match */.)
20307 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
20308 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
20309 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 ounter */.. UNU
2030a 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 SED_PARAMETER(pP
2030b 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 arse);.. if( pE
2030c 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 ->op==TK_ID ){.
2030d 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 char *zCol =
2030e 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 pE->u.zToken;.
2030f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c for(i=0; i<pEL
20310 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 ist->nExpr; i++)
20311 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 {. char *zA
20312 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d s = pEList->a[i]
20313 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 .zName;. if
20314 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 ( zAs!=0 && sqli
20315 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 te3StrICmp(zAs,
20316 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 zCol)==0 ){.
20317 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a return i+1;.
20318 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
20319 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
2031a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 ./*.** pE is a p
2031b 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 ointer to an exp
2031c 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 ression which is
2031d 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 a single term i
2031e 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 n the.** ORDER B
2031f 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 Y of a compound
20320 53 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 SELECT. The exp
20321 72 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 ression has not
20322 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 been.** name res
20323 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 olved..**.** At
20324 74 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 the point this r
20325 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
20326 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f , we already kno
20327 77 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 w that the.** OR
20328 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e DER BY term is n
20329 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e ot an integer in
2032a 64 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 dex into the res
2032b 75 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a ult set. That.*
2032c 2a 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 * case is handle
2032d 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 d by the calling
2032e 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
2032f 41 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 Attempt to match
20330 20 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 pE against resu
20331 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 lt set columns i
20332 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a n the left-most.
20333 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ** SELECT statem
20334 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 ent. Return the
20335 20 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 index i of the
20336 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c matching column,
20337 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 .** as an indica
20338 74 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c tion to the call
20339 65 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c er that it shoul
2033a 64 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d d sort by the i-
2033b 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 th column..** Th
2033c 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 e left-most colu
2033d 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 mn is 1. In oth
2033e 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 er words, the va
2033f 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
20340 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 the.** same inte
20341 67 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 ger value that w
20342 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 ould be used in
20343 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
20344 74 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a t to indicate.**
20345 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a the column..**.
20346 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e ** If there is n
20347 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 o match, return
20348 30 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 0. Return -1 if
20349 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
2034a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
2034b 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 resolveOrderByTe
2034c 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 rmToExprList(.
2034d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
2034e 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
2034f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 ntext for error
20350 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 messages */. Se
20351 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
20352 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 /* The SELECT s
20353 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 tatement with th
20354 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
20355 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 e */. Expr *pE
20356 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
20357 20 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 specific ORDER
20358 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 BY term */.){.
20359 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
2035a 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
2035b 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 er */. ExprList
2035c 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 *pEList; /* Th
2035d 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 e columns of the
2035e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
2035f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b NameContext nc;
20360 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 /* Name cont
20361 65 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e ext for resolvin
20362 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 g pE */.. asser
20363 74 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 t( sqlite3ExprIs
20364 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d Integer(pE, &i)=
20365 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d =0 );. pEList =
20366 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 pSelect->pEList
20367 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 ;.. /* Resolve
20368 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 all names in the
20369 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 ORDER BY term e
2036a 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 xpression. */.
2036b 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 memset(&nc, 0,
2036c 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e sizeof(nc));. n
2036d 63 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 c.pParse = pPars
2036e 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 e;. nc.pSrcList
2036f 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 = pSelect->pSrc
20370 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 ;. nc.pEList =
20371 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c pEList;. nc.all
20372 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e owAgg = 1;. nc.
20373 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 nErr = 0;. if(
20374 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
20375 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 prNames(&nc, pE)
20376 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
20377 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 rrorClear(pParse
20378 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
20379 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 . }.. /* Try t
2037a 6f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 o match the ORDE
2037b 52 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 R BY expression
2037c 61 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 against an expre
2037d 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 ssion. ** in th
2037e 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 e result set. R
2037f 65 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 eturn an 1-based
20380 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 index of the ma
20381 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 tching. ** resu
20382 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 lt-set entry..
20383 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c */. for(i=0; i<
20384 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 pEList->nExpr; i
20385 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c ++){. if( sql
20386 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 ite3ExprCompare(
20387 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 pEList->a[i].pEx
20388 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 pr, pE) ){.
20389 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 return i+1;.
2038a 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
2038b 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e no match, return
2038c 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 0. */. return
2038d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 0;.}../*.** Gene
2038e 72 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 rate an ORDER BY
2038f 20 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 or GROUP BY ter
20390 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 m out-of-range e
20391 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rror..*/.static
20392 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f void resolveOutO
20393 66 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 fRangeError(. P
20394 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20395 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 /* The err
20396 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 or context into
20397 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 which to write t
20398 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f he error */. co
20399 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c nst char *zType,
2039a 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 /* "ORDER"
2039b 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 or "GROUP" */.
2039c 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 int i,
2039d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e /* The in
2039e 64 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 dex (1-based) of
2039f 20 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 the term out of
203a0 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 range */. int
203a1 6d 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 mx
203a2 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 /* Largest pe
203a3 72 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 rmissible value
203a4 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c of i */.){. sql
203a5 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
203a6 72 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 rse, . "%r %s
203a7 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 BY term out of
203a8 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 range - should b
203a9 65 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e e ". "between
203aa 20 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 1 and %d", i, z
203ab 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a Type, mx);.}../*
203ac 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 .** Analyze the
203ad 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
203ae 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 in a compound SE
203af 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 LECT statement.
203b0 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 Modify.** each
203b1 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 term of the ORD
203b2 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 ER BY clause is
203b3 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 a constant integ
203b4 65 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 er between 1.**
203b5 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 and N where N is
203b6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
203b7 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f olumns in the co
203b8 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a mpound SELECT..*
203b9 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 *.** ORDER BY te
203ba 72 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 rms that are alr
203bb 65 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 eady an integer
203bc 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 between 1 and N
203bd 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 are.** unmodifie
203be 64 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 d. ORDER BY ter
203bf 6d 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 ms that are inte
203c0 67 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 gers outside the
203c1 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 range of.** 1 t
203c2 68 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 hrough N generat
203c3 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 e an error. ORD
203c4 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 ER BY terms that
203c5 20 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 are expressions
203c6 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 .** are matched
203c7 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 against result s
203c8 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f et expressions o
203c9 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 f compound SELEC
203ca 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 T.** beginning w
203cb 69 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 ith the left-mos
203cc 74 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 t SELECT and wor
203cd 6b 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 king toward the
203ce 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 right..** At the
203cf 20 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 first match, th
203d0 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 e ORDER BY expre
203d1 73 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f ssion is transfo
203d2 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 rmed into.** the
203d3 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 integer column
203d4 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 number..**.** Re
203d5 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
203d6 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a of errors seen..
203d7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
203d8 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 solveCompoundOrd
203d9 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 erBy(. Parse *p
203da 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Parse, /*
203db 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
203dc 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d . Leave error m
203dd 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a essages here */.
203de 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
203df 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 t /* The S
203e0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
203e1 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f containing the O
203e2 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 RDER BY */.){.
203e3 69 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 int i;. ExprLis
203e4 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 t *pOrderBy;. E
203e5 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
203e6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
203e7 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d int moreToDo =
203e8 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 1;.. pOrderBy
203e9 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 = pSelect->pOrde
203ea 72 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 rBy;. if( pOrde
203eb 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 rBy==0 ) return
203ec 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 0;. db = pParse
203ed 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 ->db;.#if SQLITE
203ee 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 _MAX_COLUMN. if
203ef 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 ( pOrderBy->nExp
203f0 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c r>db->aLimit[SQL
203f1 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e ITE_LIMIT_COLUMN
203f2 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
203f3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
203f4 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 "too many terms
203f5 20 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 in ORDER BY cla
203f6 75 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 use");. retur
203f7 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 1;. }.#endif.
203f8 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 for(i=0; i<pOr
203f9 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
203fa 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 +){. pOrderBy
203fb 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b ->a[i].done = 0;
203fc 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e . }. pSelect->
203fd 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 pNext = 0;. whi
203fe 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 le( pSelect->pPr
203ff 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 ior ){. pSele
20400 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 ct->pPrior->pNex
20401 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 t = pSelect;.
20402 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 pSelect = pSele
20403 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a ct->pPrior;. }.
20404 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 while( pSelect
20405 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a && moreToDo ){.
20406 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
20407 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
20408 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 . moreToDo =
20409 30 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 0;. pEList =
2040a 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
2040b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c . assert( pEL
2040c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f ist!=0 );. fo
2040d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 r(i=0, pItem=pOr
2040e 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 derBy->a; i<pOrd
2040f 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
20410 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
20411 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b int iCol = -1;
20412 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c . Expr *pE,
20413 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 *pDup;. if
20414 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 ( pItem->done )
20415 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
20416 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 pE = pItem->pExp
20417 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c r;. if( sql
20418 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
20419 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a r(pE, &iCol) ){.
2041a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c if( iCol
2041b 3c 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 <=0 || iCol>pELi
2041c 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 st->nExpr ){.
2041d 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 resolveOu
2041e 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 tOfRangeError(pP
2041f 61 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 arse, "ORDER", i
20420 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 +1, pEList->nExp
20421 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 r);. re
20422 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 turn 1;.
20423 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20424 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 iCol = re
20425 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 solveAsName(pPar
20426 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b se, pEList, pE);
20427 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
20428 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 l==0 ){.
20429 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
2042a 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 ExprDup(db, pE,
2042b 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
2042c 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ( !db->mallocFai
2042d 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 led ){.
2042e 20 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b assert(pDup);
2042f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f . iCo
20430 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 l = resolveOrder
20431 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 ByTermToExprList
20432 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 (pParse, pSelect
20433 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 , pDup);.
20434 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
20435 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
20436 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 (db, pDup);.
20437 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
20438 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 if( iCol>0 )
20439 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 {. CollSe
2043a 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 q *pColl = pE->p
2043b 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e Coll;. in
2043c 74 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c t flags = pE->fl
2043d 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c ags & EP_ExpColl
2043e 61 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ate;. sql
2043f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
20440 62 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 b, pE);.
20441 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 pItem->pExpr = p
20442 45 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 E = sqlite3Expr(
20443 64 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 db, TK_INTEGER,
20444 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
20445 70 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 pE==0 ) return 1
20446 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 ;. pE->pC
20447 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 oll = pColl;.
20448 20 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c pE->flags |
20449 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 = EP_IntValue |
2044a 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 flags;. p
2044b 45 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 E->u.iValue = iC
2044c 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 ol;. pIte
2044d 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 m->iCol = (u16)i
2044e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 Col;. pIt
2044f 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 em->done = 1;.
20450 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20451 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b moreToDo = 1;
20452 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
20453 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 pSelect = pSe
20454 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d lect->pNext;. }
20455 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f . for(i=0; i<pO
20456 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 rderBy->nExpr; i
20457 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 ++){. if( pOr
20458 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 derBy->a[i].done
20459 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
2045a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
2045b 72 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 rse, "%r ORDER B
2045c 59 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 Y term does not
2045d 6d 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 match any ".
2045e 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 "column
2045f 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
20460 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 t", i+1);.
20461 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
20462 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
20463 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 }../*.** Check e
20464 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 very term in the
20465 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
20466 55 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 UP BY clause pOr
20467 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 derBy of.** the
20468 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
20469 20 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e pSelect. If an
2046a 79 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 y term is refere
2046b 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 nce to a.** resu
2046c 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f lt set expressio
2046d 6e 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 n (as determined
2046e 20 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 by the ExprList
2046f 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a .a.iCol field).*
20470 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 * then convert t
20471 68 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 hat term into a
20472 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 copy of the corr
20473 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 esponding result
20474 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a set.** column..
20475 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 **.** If any err
20476 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 ors are detected
20477 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d , add an error m
20478 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 essage to pParse
20479 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e and.** return n
2047a 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e on-zero. Return
2047b 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f zero if no erro
2047c 72 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a rs are seen..*/.
2047d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
2047e 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 nt sqlite3Resolv
2047f 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 eOrderGroupBy(.
20480 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
20481 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
20482 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 g context. Leav
20483 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 e error messages
20484 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 here */. Selec
20485 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 t *pSelect,
20486 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 /* The SELECT s
20487 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e tatement contain
20488 69 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a ing the clause *
20489 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
2048a 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 rderBy, /* The
2048b 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
2048c 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 UP BY clause to
2048d 62 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a be processed */.
2048e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
2048f 79 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 ype /* "ORDE
20490 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f R" or "GROUP" */
20491 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 .){. int i;. s
20492 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
20493 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c rse->db;. ExprL
20494 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 ist *pEList;. s
20495 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
20496 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 tem *pItem;.. i
20497 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c f( pOrderBy==0 |
20498 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 | pParse->db->ma
20499 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 llocFailed ) ret
2049a 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 urn 0;.#if SQLIT
2049b 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 E_MAX_COLUMN. i
2049c 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 f( pOrderBy->nEx
2049d 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 pr>db->aLimit[SQ
2049e 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d LITE_LIMIT_COLUM
2049f 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 N] ){. sqlite
204a0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
204a1 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d , "too many term
204a2 73 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 s in %s BY claus
204a3 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 e", zType);.
204a4 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
204a5 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 ndif. pEList =
204a6 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
204a7 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 . assert( pELis
204a8 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 t!=0 ); /* sqli
204a9 74 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 te3SelectNew() g
204aa 75 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a uarantees this *
204ab 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 /. for(i=0, pIt
204ac 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
204ad 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
204ae 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
204af 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d {. if( pItem-
204b0 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 >iCol ){. i
204b1 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 f( pItem->iCol>p
204b2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a EList->nExpr ){.
204b3 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f resolveO
204b4 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 utOfRangeError(p
204b5 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b Parse, zType, i+
204b6 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 1, pEList->nExpr
204b7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
204b8 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 1;. }.
204b9 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 resolveAlias(
204ba 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 pParse, pEList,
204bb 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 pItem->iCol-1, p
204bc 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 Item->pExpr, zTy
204bd 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 pe);. }. }.
204be 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
204bf 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 .** pOrderBy is
204c0 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 an ORDER BY or G
204c1 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 ROUP BY clause i
204c2 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 n SELECT stateme
204c3 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 nt pSelect..** T
204c4 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 he Name context
204c5 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 of the SELECT st
204c6 61 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 atement is pNC.
204c7 20 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72 zType is either
204c8 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 .** "ORDER" or "
204c9 47 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 GROUP" depending
204ca 20 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f on which type o
204cb 66 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 f clause pOrderB
204cc 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 y is..**.** This
204cd 20 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 routine resolve
204ce 73 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 s each term of t
204cf 68 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 he clause into a
204d0 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a n expression..**
204d1 20 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 If the order-by
204d2 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 term is an inte
204d3 67 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 ger I between 1
204d4 61 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 and N (where N i
204d5 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 s the.** number
204d6 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
204d7 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
204d8 74 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e the SELECT) then
204d9 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a the expression.
204da 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 ** in the resolu
204db 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f tion is a copy o
204dc 66 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c f the I-th resul
204dd 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
204de 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 . If.** the ord
204df 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e er-by term is an
204e0 20 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 identify that c
204e1 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
204e2 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 e AS-name of.**
204e3 61 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 a result-set exp
204e4 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 ression, then th
204e5 65 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 e term resolves
204e6 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 to a copy of the
204e7 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 .** result-set e
204e8 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 xpression. Othe
204e9 72 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 rwise, the expre
204ea 73 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 ssion is resolve
204eb 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 d in.** the usua
204ec 6c 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 l way - using sq
204ed 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
204ee 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 Names()..**.** T
204ef 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
204f0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
204f1 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 f errors. If er
204f2 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e rors occur, then
204f3 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 .** an appropria
204f4 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 te error message
204f5 20 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 might be left i
204f6 6e 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 n pParse. (OOM
204f7 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 errors.** except
204f8 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ed.).*/.static i
204f9 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 nt resolveOrderG
204fa 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f roupBy(. NameCo
204fb 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 ntext *pNC,
204fc 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 /* The name cont
204fd 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 ext of the SELEC
204fe 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 T statement */.
204ff 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
20500 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 , /* The SE
20501 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 LECT statement h
20502 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 olding pOrderBy
20503 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
20504 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e OrderBy, /* An
20505 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
20506 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 UP BY clause to
20507 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e resolve */. con
20508 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 st char *zType
20509 20 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 /* Either "OR
2050a 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c DER" or "GROUP",
2050b 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 as appropriate
2050c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 */.){. int i;
2050d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2050e 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
2050f 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
20510 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 iCol;
20511 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
20512 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 lumn number */.
20513 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
20514 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 _item *pItem;
20515 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 /* A term of the
20516 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
20517 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 */. Parse *pPa
20518 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 rse;
20519 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
2051a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
2051b 20 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 nResult;
2051c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
2051d 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 umber of terms i
2051e 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
2051f 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 */.. if( pOrde
20520 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 rBy==0 ) return
20521 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 0;. nResult = p
20522 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e Select->pEList->
20523 6e 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 nExpr;. pParse
20524 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 = pNC->pParse;.
20525 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d for(i=0, pItem=
20526 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 pOrderBy->a; i<p
20527 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 OrderBy->nExpr;
20528 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
20529 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 Expr *pE = pI
2052a 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 tem->pExpr;.
2052b 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 iCol = resolveAs
2052c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 Name(pParse, pSe
2052d 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 lect->pEList, pE
2052e 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e );. if( iCol>
2052f 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 0 ){. /* If
20530 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 an AS-name matc
20531 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b h is found, mark
20532 20 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 this ORDER BY c
20533 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 olumn as being.
20534 20 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f ** a copy o
20535 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 f the iCol-th re
20536 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e sult-set column.
20537 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 The subsequent
20538 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a call to. *
20539 2a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 * sqlite3Resolve
2053a 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 OrderGroupBy() w
2053b 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 ill convert the
2053c 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a expression to a.
2053d 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 ** copy of
2053e 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 the iCol-th res
2053f 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 ult-set expressi
20540 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 on. */. pIt
20541 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 em->iCol = (u16)
20542 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 iCol;. cont
20543 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 inue;. }.
20544 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 if( sqlite3ExprI
20545 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 sInteger(pE, &iC
20546 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ol) ){. /*
20547 54 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 The ORDER BY ter
20548 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 m is an integer
20549 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e constant. Again
2054a 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e , set the column
2054b 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 . ** number
2054c 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 so that sqlite3
2054d 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 ResolveOrderGrou
2054e 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 pBy() will conve
2054f 72 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 rt the. **
20550 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f order-by term to
20551 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 a copy of the r
20552 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 esult-set expres
20553 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 sion */. if
20554 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 ( iCol<1 ){.
20555 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 resolveOutOf
20556 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 RangeError(pPars
20557 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e e, zType, i+1, n
20558 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 Result);.
20559 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
2055a 20 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e }. pItem->
2055b 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c iCol = (u16)iCol
2055c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 ;. continue
2055d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
2055e 4f 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 Otherwise, treat
2055f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 the ORDER BY te
20560 72 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 rm as an ordinar
20561 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a y expression */.
20562 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 pItem->iCol
20563 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
20564 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
20565 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b ames(pNC, pE) ){
20566 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
20567 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
20568 75 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c urn sqlite3Resol
20569 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 veOrderGroupBy(p
2056a 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 Parse, pSelect,
2056b 70 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 pOrderBy, zType)
2056c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c ;.}../*.** Resol
2056d 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 ve names in the
2056e 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
2056f 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 p and all of it
20570 73 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a s descendents..*
20571 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
20572 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 olveSelectStep(W
20573 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
20574 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 Select *p){. Na
20575 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 meContext *pOute
20576 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 rNC; /* Context
20577 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
20578 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 his SELECT */.
20579 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b NameContext sNC;
2057a 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
2057b 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 context of this
2057c 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 SELECT */. int
2057d 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 isCompound;
2057e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
2057f 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 is a compound s
20580 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e elect */. int n
20581 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 Compound;
20582 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
20583 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 compound terms p
20584 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 rocessed so far
20585 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 */. Parse *pPar
20586 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 se; /*
20587 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
20588 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
20589 45 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 EList; /*
2058a 52 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 Result set expre
2058b 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 ssion list */.
2058c 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
2058d 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
2058e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 counter */. Exp
2058f 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b rList *pGroupBy;
20590 20 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 /* The GROU
20591 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 P BY clause */.
20592 20 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f Select *pLeftmo
20593 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 st; /* Left
20594 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 -most of SELECT
20595 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f of a compound */
20596 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
20597 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
20598 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
20599 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 n */. .. asser
2059a 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 t( p!=0 );. if(
2059b 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 p->selFlags & S
2059c 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 F_Resolved ){.
2059d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
2059e 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 ne;. }. pOuter
2059f 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e NC = pWalker->u.
205a0 70 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 pNC;. pParse =
205a1 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b pWalker->pParse;
205a2 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e . db = pParse->
205a3 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c db;.. /* Normal
205a4 6c 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ly sqlite3Select
205a5 45 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 Expand() will be
205a6 20 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e called first an
205a7 64 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a d will have. **
205a8 20 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 already expande
205a9 64 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 d this SELECT.
205aa 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 However, if this
205ab 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77 is a subquery w
205ac 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 ithin. ** an ex
205ad 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 pression, sqlite
205ae 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
205af 73 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c s() will be call
205b0 65 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a ed without a. *
205b1 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 * prior call to
205b2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
205b3 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 and(). When tha
205b4 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 t happens, let.
205b5 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ** sqlite3Selec
205b6 74 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f tPrep() do all o
205b7 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 f the processing
205b8 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 for this SELECT
205b9 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 .. ** sqlite3Se
205ba 6c 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 lectPrep() will
205bb 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 invoke both sqli
205bc 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
205bd 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 ) and. ** this
205be 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 routine in the c
205bf 6f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 orrect order..
205c0 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c */. if( (p->sel
205c1 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e Flags & SF_Expan
205c2 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 ded)==0 ){. s
205c3 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 qlite3SelectPrep
205c4 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 (pParse, p, pOut
205c5 65 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 erNC);. retur
205c6 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 n (pParse->nErr
205c7 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
205c8 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 led) ? WRC_Abort
205c9 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 : WRC_Prune;.
205ca 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 }.. isCompound
205cb 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a = p->pPrior!=0;.
205cc 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b nCompound = 0;
205cd 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 . pLeftmost = p
205ce 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a ;. while( p ){.
205cf 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
205d0 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 selFlags & SF_Ex
205d1 70 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 panded)!=0 );.
205d2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 assert( (p->se
205d3 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f lFlags & SF_Reso
205d4 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 lved)==0 );.
205d5 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 p->selFlags |= S
205d6 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 F_Resolved;..
205d7 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 /* Resolve the
205d8 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 expressions in t
205d9 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 he LIMIT and OFF
205da 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 SET clauses. The
205db 73 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f se. ** are no
205dc 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 t allowed to ref
205dd 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c er to any names,
205de 20 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 so pass an empt
205df 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 y NameContext..
205e0 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 */. memset
205e1 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 (&sNC, 0, sizeof
205e2 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e (sNC));. sNC.
205e3 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
205e4 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
205e5 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
205e6 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 (&sNC, p->pLimit
205e7 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c ) ||. sql
205e8 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
205e9 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f ames(&sNC, p->pO
205ea 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 ffset) ){.
205eb 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
205ec 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
205ed 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 * Set up the loc
205ee 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 al name-context
205ef 74 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 to pass to sqlit
205f0 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
205f1 65 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 es() to. ** r
205f2 65 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c esolve the resul
205f3 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
205f4 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 list.. */.
205f5 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d sNC.allowAgg =
205f6 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 1;. sNC.pSrc
205f7 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a List = p->pSrc;.
205f8 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 sNC.pNext =
205f9 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 pOuterNC;. .
205fa 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 /* Resolve name
205fb 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
205fc 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 set. */. pELi
205fd 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a st = p->pEList;.
205fe 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 assert( pELi
205ff 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 st!=0 );. for
20600 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e (i=0; i<pEList->
20601 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
20602 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 Expr *pX = pE
20603 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 List->a[i].pExpr
20604 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
20605 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
20606 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b mes(&sNC, pX) ){
20607 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
20608 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 WRC_Abort;.
20609 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
2060a 2f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 /* Recursively r
2060b 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 esolve names in
2060c 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 all subqueries.
2060d 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d */. for(i=
2060e 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 0; i<p->pSrc->nS
2060f 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 rc; i++){.
20610 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
20611 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d tem *pItem = &p-
20612 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 >pSrc->a[i];.
20613 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 if( pItem->pS
20614 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 elect ){.
20615 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 const char *zSa
20616 76 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 vedContext = pPa
20617 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
20618 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 t;. if( p
20619 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 Item->zName ) pP
2061a 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 arse->zAuthConte
2061b 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d xt = pItem->zNam
2061c 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e;. sqlit
2061d 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e e3ResolveSelectN
2061e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 ames(pParse, pIt
2061f 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 em->pSelect, pOu
20620 74 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 terNC);.
20621 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
20622 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e text = zSavedCon
20623 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 text;. if
20624 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
20625 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
20626 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f ed ) return WRC_
20627 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 Abort;. }.
20628 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
20629 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 f there are no a
2062a 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
2062b 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
2062c 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f -set, and no GRO
2062d 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 UP BY . ** ex
2062e 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 pression, do not
2062f 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 allow aggregate
20630 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 s in any of the
20631 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
20632 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 s.. */. as
20633 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 sert( (p->selFla
20634 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 gs & SF_Aggregat
20635 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 e)==0 );. pGr
20636 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 oupBy = p->pGrou
20637 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 pBy;. if( pGr
20638 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 oupBy || sNC.has
20639 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Agg ){. p->
2063a 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 selFlags |= SF_A
2063b 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 ggregate;. }e
2063c 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 lse{. sNC.a
2063d 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 llowAgg = 0;.
2063e 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
2063f 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 a HAVING clause
20640 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e is present, then
20641 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 there must be a
20642 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
20643 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
20644 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 p->pHaving && !
20645 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 pGroupBy ){.
20646 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20647 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f g(pParse, "a GRO
20648 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 UP BY clause is
20649 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 required before
2064a 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 HAVING");.
2064b 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
2064c 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
2064d 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 * Add the expres
2064e 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 sion list to the
2064f 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 name-context be
20650 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 fore parsing the
20651 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 . ** other ex
20652 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 pressions in the
20653 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
20654 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 t. This is so th
20655 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 at. ** expres
20656 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 sions in the WHE
20657 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 RE clause (etc.)
20658 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 can refer to ex
20659 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 pressions by.
2065a 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 ** aliases in t
2065b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 he result set..
2065c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e **. ** Min
2065d 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 or point: If thi
2065e 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 s is the case, t
2065f 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
20660 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a on will be. *
20661 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 * re-evaluated f
20662 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 or each referenc
20663 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a e to it.. */.
20664 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d sNC.pEList =
20665 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 p->pEList;.
20666 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c if( sqlite3Resol
20667 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 veExprNames(&sNC
20668 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a , p->pWhere) ||.
20669 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
2066a 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
2066b 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 sNC, p->pHaving)
2066c 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 . ){. re
2066d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
2066e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 }.. /* Th
2066f 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 e ORDER BY and G
20670 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 ROUP BY clauses
20671 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f may not refer to
20672 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a terms in. **
20673 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a outer queries .
20674 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 */. sNC.p
20675 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e Next = 0;. sN
20676 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a C.allowAgg = 1;.
20677 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 . /* Process
20678 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
20679 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f use for singleto
2067a 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 n SELECT stateme
2067b 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 nts.. ** The
2067c 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
2067d 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 for compounds SE
2067e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
2067f 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a is handled. *
20680 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 * below, after a
20681 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ll of the result
20682 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 -sets for all of
20683 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 the elements of
20684 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 . ** the comp
20685 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 ound have been r
20686 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a esolved.. */.
20687 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f if( !isCompo
20688 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 und && resolveOr
20689 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c derGroupBy(&sNC,
2068a 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c p, p->pOrderBy,
2068b 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 "ORDER") ){.
2068c 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
2068d 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ort;. }. i
2068e 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
2068f 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 led ){. ret
20690 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
20691 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 }. . /* R
20692 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 esolve the GROUP
20693 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 BY clause. At
20694 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d the same time, m
20695 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a ake sure . **
20696 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c the GROUP BY cl
20697 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f ause does not co
20698 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 ntain aggregate
20699 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a functions.. *
2069a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 /. if( pGroup
2069b 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 By ){. stru
2069c 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
2069d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 *pItem;. .
2069e 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f if( resolveO
2069f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 rderGroupBy(&sNC
206a0 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 , p, pGroupBy, "
206a1 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d GROUP") || db->m
206a2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
206a3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 return WR
206a4 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d C_Abort;. }
206a5 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 . for(i=0,
206a6 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e pItem=pGroupBy->
206a7 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e a; i<pGroupBy->n
206a8 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
206a9 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ++){. if(
206aa 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
206ab 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 (pItem->pExpr, E
206ac 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 P_Agg) ){.
206ad 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
206ae 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 Msg(pParse, "agg
206af 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
206b0 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 are not allowed
206b1 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 in ".
206b2 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 "the GROUP B
206b3 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 Y clause");.
206b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
206b5 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 _Abort;.
206b6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
206b7 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 . /* Advance
206b8 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d to the next term
206b9 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 of the compound
206ba 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 . */. p =
206bb 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e p->pPrior;. n
206bc 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a Compound++;. }.
206bd 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 . /* Resolve th
206be 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 e ORDER BY on a
206bf 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 compound SELECT
206c0 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 after all terms
206c1 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 of. ** the comp
206c2 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 ound have been r
206c3 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 esolved.. */.
206c4 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 if( isCompound &
206c5 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e & resolveCompoun
206c6 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c dOrderBy(pParse,
206c7 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 pLeftmost) ){.
206c8 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
206c9 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ort;. }.. retu
206ca 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a rn WRC_Prune;.}.
206cb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
206cc 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 ine walks an exp
206cd 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 ression tree and
206ce 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 resolves refere
206cf 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 nces to.** table
206d0 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 columns and res
206d1 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e ult-set columns.
206d2 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 At the same ti
206d3 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 me, do error.**
206d4 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 checking on func
206d5 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 tion usage and s
206d6 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 et a flag if any
206d7 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
206d8 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e ions.** are seen
206d9 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c ..**.** To resol
206da 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 ve table columns
206db 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c references we l
206dc 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f ook for nodes (o
206dd 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 r subtrees) of t
206de 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e he .** form X.Y.
206df 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 Z or Y.Z or just
206e0 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 Z where.**.**
206e1 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d X: The nam
206e2 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e e of a database.
206e3 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 Ex: "main" or
206e4 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 "temp" or.**
206e5 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 the symb
206e6 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e olic name assign
206e7 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d ed to an ATTACH-
206e8 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ed database..**.
206e9 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 ** Y: The
206ea 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 name of a table
206eb 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 in a FROM claus
206ec 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 e. Or in a trig
206ed 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ger.**
206ee 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 one of the spec
206ef 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 ial names "old"
206f0 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 or "new"..**.**
206f1 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 Z: The na
206f2 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 me of a column i
206f3 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a n table Y..**.**
206f4 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 The node at the
206f5 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 root of the sub
206f6 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 tree is modified
206f7 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
206f8 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 ** Expr.op
206f9 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 Changed to
206fa 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 TK_COLUMN.**
206fb 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 Expr.pTab P
206fc 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 oints to the Tab
206fd 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e le object for X.
206fe 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f Y.** Expr.iCo
206ff 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d lumn The colum
20700 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 n index in X.Y.
20701 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 -1 for the rowi
20702 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 d..** Expr.iT
20703 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 able The VDBE
20704 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 cursor number f
20705 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 or X.Y.**.**.**
20706 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c To resolve resul
20707 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 t-set references
20708 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 , look for expre
20709 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 ssion nodes of t
2070a 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 he.** form Z (wi
2070b 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 th no X and Y pr
2070c 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 efix) where the
2070d 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 Z matches the ri
2070e 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 ght-hand.** size
2070f 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 of an AS clause
20710 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 in the result-s
20711 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 et of a SELECT.
20712 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f The Z expressio
20713 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 n.** is replaced
20714 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 by a copy of th
20715 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 e left-hand side
20716 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 of the result-s
20717 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a et expression..*
20718 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 * Table-name and
20719 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 function resolu
2071a 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 tion occurs on t
2071b 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 he substituted e
2071c 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 xpression.** tre
2071d 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c e. For example,
2071e 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 in:.**.**
2071f 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c SELECT a+b AS x,
20720 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 c+d AS y FROM t
20721 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 1 ORDER BY x;.**
20722 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d .** The "x" term
20723 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 of the order by
20724 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 is replaced by
20725 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a "a+b" to render:
20726 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 .**.** SELE
20727 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 CT a+b AS x, c+d
20728 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 AS y FROM t1 OR
20729 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a DER BY a+b;.**.*
2072a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 * Function calls
2072b 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 are checked to
2072c 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
2072d 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a he function is .
2072e 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 ** defined and t
2072f 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 hat the correct
20730 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
20731 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 nts are specifie
20732 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e d..** If the fun
20733 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 ction is an aggr
20734 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 egate function,
20735 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 then the pNC->ha
20736 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 sAgg is.** set a
20737 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 nd the opcode is
20738 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b changed from TK
20739 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f _FUNCTION to TK_
2073a 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a AGG_FUNCTION..**
2073b 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f If an expressio
2073c 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 n contains aggre
2073d 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 gate functions t
2073e 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a hen the EP_Agg.*
2073f 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 * property on th
20740 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
20741 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 set..**.** An er
20742 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c ror message is l
20743 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 eft in pParse if
20744 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 anything is ami
20745 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a ss. The number.
20746 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 ** if errors is
20747 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
20748 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20749 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
2074a 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 prNames( . Name
2074b 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 Context *pNC,
2074c 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 /* Namespace
2074d 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 to resolve expr
2074e 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 essions in. */.
2074f 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 Expr *pExpr
20750 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20751 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 expression to be
20752 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b analyzed. */.){
20753 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 . int savedHasA
20754 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a gg;. Walker w;.
20755 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 . if( pExpr==0
20756 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 ) return 0;.#if
20757 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
20758 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 DEPTH>0. {.
20759 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 Parse *pParse =
2075a 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 pNC->pParse;.
2075b 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 if( sqlite3Expr
2075c 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 CheckHeight(pPar
2075d 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 se, pExpr->nHeig
2075e 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e ht+pNC->pParse->
2075f 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 nHeight) ){.
20760 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
20761 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 }. pParse->nH
20762 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e eight += pExpr->
20763 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e nHeight;. }.#en
20764 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 dif. savedHasAg
20765 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b g = pNC->hasAgg;
20766 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d . pNC->hasAgg =
20767 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 0;. w.xExprCal
20768 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 lback = resolveE
20769 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 xprStep;. w.xSe
2076a 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 lectCallback = r
2076b 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 esolveSelectStep
2076c 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 ;. w.pParse = p
2076d 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e NC->pParse;. w.
2076e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 u.pNC = pNC;. s
2076f 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
20770 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 w, pExpr);.#if S
20771 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
20772 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 EPTH>0. pNC->pP
20773 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d arse->nHeight -=
20774 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b pExpr->nHeight;
20775 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e .#endif. if( pN
20776 43 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 C->nErr>0 || w.p
20777 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b Parse->nErr>0 ){
20778 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 . ExprSetProp
20779 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 erty(pExpr, EP_E
2077a 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 rror);. }. if(
2077b 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a pNC->hasAgg ){.
2077c 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
2077d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 rty(pExpr, EP_Ag
2077e 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 g);. }else if(
2077f 73 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 savedHasAgg ){.
20780 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d pNC->hasAgg =
20781 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
20782 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
20783 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 (pExpr, EP_Error
20784 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 );.}.../*.** Res
20785 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 olve all names i
20786 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e n all expression
20787 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e s of a SELECT an
20788 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 d in all.** dece
20789 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 ndents of the SE
2078a 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 LECT, including
2078b 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 compounds off of
2078c 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 p->pPrior,.** s
2078d 75 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 ubqueries in exp
2078e 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 ressions, and su
2078f 62 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 bqueries used as
20790 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 FROM clause.**
20791 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 terms..**.** See
20792 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
20793 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 xprNames() for a
20794 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 description of
20795 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 the kinds of.**
20796 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 transformations
20797 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a that occur..**.*
20798 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 * All SELECT sta
20799 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 tements should h
2079a 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 ave been expande
2079b 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 d using.** sqlit
2079c 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 e3SelectExpand()
2079d 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 prior to invoki
2079e 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
2079f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
207a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
207a1 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 esolveSelectName
207a2 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 s(. Parse *pPar
207a3 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 se, /* T
207a4 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 he parser contex
207a5 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 t */. Select *p
207a6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
207a7 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 The SELECT stat
207a8 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 ement being code
207a9 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 d. */. NameCont
207aa 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f ext *pOuterNC /
207ab 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 * Name context f
207ac 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 or parent SELECT
207ad 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b statement */.){
207ae 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 . Walker w;..
207af 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
207b0 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
207b1 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 k = resolveExprS
207b2 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 tep;. w.xSelect
207b3 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c Callback = resol
207b4 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 veSelectStep;.
207b5 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 w.pParse = pPars
207b6 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 e;. w.u.pNC = p
207b7 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 OuterNC;. sqlit
207b8 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c e3WalkSelect(&w,
207b9 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a p);.}../*******
207ba 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 ******* End of r
207bb 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a esolve.c *******
207bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207be 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
207bf 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
207c0 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a le expr.c ******
207c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207c3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
207c4 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
207c5 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
207c6 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
207c7 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
207c8 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
207c9 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
207ca 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
207cb 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
207cc 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
207cd 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
207ce 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
207cf 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
207d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
207d1 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
207d2 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
207d3 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
207d4 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
207d5 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
207d6 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
207d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
207db 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
207dc 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 ains routines us
207dd 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 ed for analyzing
207de 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 expressions and
207df 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 .** for generati
207e0 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 ng VDBE code tha
207e1 74 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 t evaluates expr
207e2 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 essions in SQLit
207e3 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e..*/../*.** Ret
207e4 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 urn the 'affinit
207e5 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 y' of the expres
207e6 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e sion pExpr if an
207e7 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 y..**.** If pExp
207e8 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 r is a column, a
207e9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
207ea 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 column via an 'A
207eb 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 S' alias,.** or
207ec 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 a sub-select wit
207ed 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 h a column as th
207ee 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 e return value,
207ef 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 then the .** aff
207f0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f inity of that co
207f1 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 lumn is returned
207f2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 . Otherwise, 0x0
207f3 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 0 is returned,.*
207f4 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 * indicating no
207f5 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 affinity for the
207f6 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a expression..**.
207f7 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 ** i.e. the WHER
207f8 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 E clause express
207f9 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c sions in the fol
207fa 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
207fb 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e s all.** have an
207fc 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a affinity:.**.**
207fd 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 CREATE TABLE t1
207fe 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a (a);.** SELECT *
207ff 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 FROM t1 WHERE a
20800 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 ;.** SELECT a AS
20801 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 b FROM t1 WHERE
20802 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 b;.** SELECT *
20803 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 FROM t1 WHERE (s
20804 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 elect a from t1)
20805 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.*/.SQLITE_PRIV
20806 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
20807 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 ExprAffinity(Exp
20808 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
20809 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op = pExpr->op;
2080a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 . if( op==TK_SE
2080b 4c 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 LECT ){. asse
2080c 72 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 rt( pExpr->flags
2080d 26 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b &EP_xIsSelect );
2080e 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
2080f 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 te3ExprAffinity(
20810 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
20811 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 ->pEList->a[0].p
20812 45 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 Expr);. }.#ifnd
20813 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
20814 41 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b AST. if( op==TK
20815 5f 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 _CAST ){. ass
20816 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
20817 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
20818 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
20819 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 return sqlite3A
2081a 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 ffinityType(pExp
2081b 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 r->u.zToken);.
2081c 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 }.#endif. if( (
2081d 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d op==TK_AGG_COLUM
2081e 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 N || op==TK_COLU
2081f 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 MN || op==TK_REG
20820 49 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 ISTER) . && pE
20821 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 xpr->pTab!=0. )
20822 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f {. /* op==TK_
20823 52 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 REGISTER && pExp
20824 72 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 r->pTab!=0 happe
20825 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 ns when pExpr wa
20826 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 s originally.
20827 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 ** a TK_COLUMN
20828 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 but was previous
20829 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 ly evaluated and
2082a 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 cached in a reg
2082b 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 ister */. int
2082c 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c j = pExpr->iCol
2082d 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 umn;. if( j<0
2082e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
2082f 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 _AFF_INTEGER;.
20830 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
20831 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 >pTab && j<pExpr
20832 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a ->pTab->nCol );.
20833 20 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 return pExpr
20834 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e ->pTab->aCol[j].
20835 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 affinity;. }.
20836 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 return pExpr->af
20837 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a finity;.}../*.**
20838 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 Set the collati
20839 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 ng sequence for
2083a 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 expression pExpr
2083b 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 to be the colla
2083c 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ting.** sequence
2083d 20 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e named by pToken
2083e 2e 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 . Return a poi
2083f 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 nter to the revi
20840 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a sed expression..
20841 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 ** The collating
20842 20 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 sequence is mar
20843 6b 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 ked as "explicit
20844 22 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 " using the EP_E
20845 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 xpCollate.** fla
20846 67 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 g. An explicit
20847 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
20848 63 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 ce will override
20849 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c implicit.** col
2084a 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 lating sequences
2084b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
2084c 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
2084d 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 3ExprSetColl(Par
2084e 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
2084f 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a *pExpr, Token *
20850 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 pCollName){. ch
20851 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 ar *zColl = 0;
20852 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 /* Deq
20853 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f uoted name of co
20854 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
20855 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 */. CollSeq *p
20856 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 Coll;. sqlite3
20857 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
20858 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 ;. zColl = sqli
20859 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
2085a 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b (db, pCollName);
2085b 0a 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 . if( pExpr &&
2085c 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f zColl ){. pCo
2085d 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 ll = sqlite3Loca
2085e 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 teCollSeq(pParse
2085f 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 , zColl);. if
20860 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 ( pColl ){.
20861 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 pExpr->pColl =
20862 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 pColl;. pEx
20863 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f pr->flags |= EP_
20864 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 ExpCollate;.
20865 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 }. }. sqlite3D
20866 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 bFree(db, zColl)
20867 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 ;. return pExpr
20868 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
20869 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f n the default co
2086a 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2086b 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 for the express
2086c 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a ion pExpr. If.**
2086d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 there is no def
2086e 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 ault collation t
2086f 79 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a ype, return 0..*
20870 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20871 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 CollSeq *sqlite
20872 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 3ExprCollSeq(Par
20873 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
20874 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c *pExpr){. Coll
20875 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a Seq *pColl = 0;.
20876 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 Expr *p = pExp
20877 72 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 r;. while( ALWA
20878 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 YS(p) ){. int
20879 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d op;. pColl =
2087a 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 p->pColl;. i
2087b 66 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b f( pColl ) break
2087c 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 ;. op = p->op
2087d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 ;. if( p->pTa
2087e 62 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 b!=0 && (.
2087f 20 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c op==TK_AGG_COL
20880 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f UMN || op==TK_CO
20881 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 LUMN || op==TK_R
20882 45 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 EGISTER || op==T
20883 4b 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 K_TRIGGER. ))
20884 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 {. /* op==T
20885 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d K_REGISTER && p-
20886 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 >pTab!=0 happens
20887 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 when pExpr was
20888 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 originally.
20889 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 ** a TK_COLUMN
2088a 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 but was previous
2088b 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 ly evaluated and
2088c 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 cached in a reg
2088d 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 ister */. c
2088e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c onst char *zColl
2088f 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 ;. int j =
20890 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 p->iColumn;.
20891 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 if( j>=0 ){.
20892 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 sqlite3 *d
20893 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
20894 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 zColl =
20895 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d p->pTab->aCol[j]
20896 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 .zColl;.
20897 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 pColl = sqlite3F
20898 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 indCollSeq(db, E
20899 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 NC(db), zColl, 0
2089a 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 );. pExpr
2089b 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b ->pColl = pColl;
2089c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
2089d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
2089e 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 if( op!=TK_CAST
2089f 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 && op!=TK_UPLUS
208a0 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ){. break;.
208a1 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
208a2 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 >pLeft;. }. if
208a3 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f ( sqlite3CheckCo
208a4 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 llSeq(pParse, pC
208a5 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f oll) ){ . pCo
208a6 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ll = 0;. }. re
208a7 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f turn pColl;.}../
208a8 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e *.** pExpr is an
208a9 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f operand of a co
208aa 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
208ab 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a r. aff2 is the.
208ac 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
208ad 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 of the other op
208ae 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 erand. This rou
208af 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
208b0 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 .** type affinit
208b1 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 y that should be
208b2 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f used for the co
208b3 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f mparison operato
208b4 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
208b5 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
208b6 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
208b7 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
208b8 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 ar aff2){. char
208b9 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 aff1 = sqlite3E
208ba 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 xprAffinity(pExp
208bb 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 r);. if( aff1 &
208bc 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a & aff2 ){. /*
208bd 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 Both sides of t
208be 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 he comparison ar
208bf 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e e columns. If on
208c0 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 e has numeric.
208c1 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 ** affinity, u
208c2 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 se that. Otherwi
208c3 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 se use no affini
208c4 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ty.. */. i
208c5 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 f( sqlite3IsNume
208c6 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 ricAffinity(aff1
208c7 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 ) || sqlite3IsNu
208c8 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 mericAffinity(af
208c9 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 f2) ){. ret
208ca 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e urn SQLITE_AFF_N
208cb 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 UMERIC;. }els
208cc 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
208cd 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
208ce 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
208cf 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 f( !aff1 && !aff
208d0 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 2 ){. /* Neit
208d1 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 her side of the
208d2 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 comparison is a
208d3 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 column. Compare
208d4 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 the. ** resu
208d5 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 lts directly..
208d6 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 */. return
208d7 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b SQLITE_AFF_NONE;
208d8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
208d9 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 One side is a c
208da 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 olumn, the other
208db 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 is not. Use the
208dc 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 columns affinit
208dd 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 y. */. assert
208de 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 ( aff1==0 || aff
208df 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 2==0 );. retu
208e0 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 rn (aff1 + aff2)
208e1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 ;. }.}../*.** p
208e2 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 Expr is a compar
208e3 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 ison operator.
208e4 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 Return the type
208e5 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
208e6 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 ould.** be appli
208e7 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 ed to both opera
208e8 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 nds prior to doi
208e9 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ng the compariso
208ea 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 n..*/.static cha
208eb 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 r comparisonAffi
208ec 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
208ed 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 ){. char aff;.
208ee 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
208ef 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 op==TK_EQ || pEx
208f0 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c pr->op==TK_IN ||
208f1 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c pExpr->op==TK_L
208f2 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 T ||. p
208f3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 Expr->op==TK_GT
208f4 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b || pExpr->op==TK
208f5 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 _GE || pExpr->op
208f6 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 ==TK_LE ||.
208f7 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d pExpr->op==
208f8 54 4b 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e TK_NE || pExpr->
208f9 6f 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78 op==TK_IS || pEx
208fa 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 pr->op==TK_ISNOT
208fb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
208fc 78 70 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 xpr->pLeft );.
208fd 61 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 aff = sqlite3Exp
208fe 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d rAffinity(pExpr-
208ff 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 >pLeft);. if( p
20900 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a Expr->pRight ){.
20901 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 aff = sqlite
20902 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
20903 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 (pExpr->pRight,
20904 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 aff);. }else if
20905 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
20906 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
20907 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 elect) ){. af
20908 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 f = sqlite3Compa
20909 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 reAffinity(pExpr
2090a 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c ->x.pSelect->pEL
2090b 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c ist->a[0].pExpr,
2090c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 aff);. }else i
2090d 66 28 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 f( !aff ){. a
2090e 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
2090f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 NONE;. }. retu
20910 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn aff;.}../*.**
20911 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 pExpr is a comp
20912 61 72 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f arison expressio
20913 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c n, eg. '=', '<',
20914 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a IN(...) etc..**
20915 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 idx_affinity is
20916 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 the affinity of
20917 20 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 an indexed colu
20918 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a mn. Return true.
20919 2a 2a 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 ** if the index
2091a 77 69 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 with affinity id
2091b 78 5f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 x_affinity may b
2091c 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
2091d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 ent.** the compa
2091e 72 69 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a rison in pExpr..
2091f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20920 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 E int sqlite3Ind
20921 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 exAffinityOk(Exp
20922 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 r *pExpr, char i
20923 64 78 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 dx_affinity){.
20924 63 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 char aff = compa
20925 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 risonAffinity(pE
20926 78 70 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 xpr);. switch(
20927 61 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 aff ){. case
20928 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a SQLITE_AFF_NONE:
20929 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
2092a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
2092b 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 _AFF_TEXT:.
2092c 20 72 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 return idx_affi
2092d 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
2092e 5f 54 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 _TEXT;. defau
2092f 6c 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e lt:. return
20930 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 sqlite3IsNumeri
20931 63 41 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 cAffinity(idx_af
20932 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a finity);. }.}..
20933 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
20934 20 50 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 P5 value that s
20935 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
20936 72 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 r a binary compa
20937 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 rison.** opcode
20938 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 (OP_Eq, OP_Ge et
20939 63 2e 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 c.) used to comp
2093a 61 72 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 are pExpr1 and p
2093b 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 Expr2..*/.static
2093c 20 75 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 u8 binaryCompar
2093d 65 50 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 eP5(Expr *pExpr1
2093e 2c 20 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 , Expr *pExpr2,
2093f 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b int jumpIfNull){
20940 0a 20 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 . u8 aff = (cha
20941 72 29 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 r)sqlite3ExprAff
20942 69 6e 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 inity(pExpr2);.
20943 20 61 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 aff = (u8)sqlit
20944 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 e3CompareAffinit
20945 79 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c y(pExpr1, aff) |
20946 20 28 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b (u8)jumpIfNull;
20947 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d . return aff;.}
20948 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
20949 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
2094a 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
2094b 63 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 ce that should b
2094c 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 e used by.** a b
2094d 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e inary comparison
2094e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 operator compar
2094f 69 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 ing pLeft and pR
20950 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ight..**.** If t
20951 68 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 he left hand exp
20952 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f ression has a co
20953 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
20954 20 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 type, then it i
20955 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 s.** used. Other
20956 77 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 wise the collati
20957 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 on sequence for
20958 74 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 the right hand e
20959 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 xpression.** is
2095a 75 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 used, or the def
2095b 61 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 ault (BINARY) if
2095c 20 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73 neither express
2095d 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 ion has a collat
2095e 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a ing.** type..**.
2095f 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 ** Argument pRig
20960 68 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 ht (but not pLef
20961 74 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c t) may be a null
20962 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 pointer. In thi
20963 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 s case,.** it is
20964 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e not considered.
20965 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20966 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
20967 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 te3BinaryCompare
20968 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 CollSeq(. Parse
20969 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 *pParse, . Exp
2096a 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 r *pLeft, . Exp
2096b 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 r *pRight.){. C
2096c 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 ollSeq *pColl;.
2096d 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 assert( pLeft )
2096e 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 ;. if( pLeft->f
2096f 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
20970 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 late ){. asse
20971 72 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c rt( pLeft->pColl
20972 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 );. pColl =
20973 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 pLeft->pColl;.
20974 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 }else if( pRight
20975 20 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 && pRight->flag
20976 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 s & EP_ExpCollat
20977 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 e ){. assert(
20978 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 pRight->pColl )
20979 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 ;. pColl = pR
2097a 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d ight->pColl;. }
2097b 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 else{. pColl
2097c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c = sqlite3ExprCol
2097d 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 lSeq(pParse, pLe
2097e 66 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 ft);. if( !pC
2097f 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f oll ){. pCo
20980 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ll = sqlite3Expr
20981 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
20982 70 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 pRight);. }.
20983 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c }. return pCol
20984 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 l;.}../*.** Gene
20985 72 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 rate the operand
20986 73 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 s for a comparis
20987 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 on operation. B
20988 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 efore.** generat
20989 69 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 ing the code for
2098a 20 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 each operand, s
2098b 65 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 et the EP_AnyAff
2098c 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 .** flag on the
2098d 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 expression so th
2098e 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 at it will be ab
2098f 6c 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 le to used a.**
20990 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 cached column va
20991 6c 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65 lue that has pre
20992 76 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e viously undergon
20993 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 e an.** affinity
20994 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 change..*/.stat
20995 69 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 ic void codeComp
20996 61 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 areOperands(. P
20997 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20998 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 /* Parsing and
20999 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 code generating
2099a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
2099b 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f r *pLeft, /
2099c 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 * The left opera
2099d 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
2099e 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 gLeft, /* Reg
2099f 69 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 ister where left
209a0 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 operand is stor
209a1 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 ed */. int *pFr
209a2 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 eeLeft, /* Fre
209a3 65 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 e this register
209a4 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 when done */. E
209a5 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 xpr *pRight,
209a6 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 /* The right op
209a7 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a erand */. int *
209a8 70 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 pRegRight, /*
209a9 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 Register where r
209aa 69 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 ight operand is
209ab 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 stored */. int
209ac 2a 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a *pFreeRight /*
209ad 20 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 Write temp regi
209ae 73 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f ster for right o
209af 70 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a perand there */.
209b0 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 ){. while( pLef
209b1 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 t->op==TK_UPLUS
209b2 29 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d ) pLeft = pLeft-
209b3 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d >pLeft;. pLeft-
209b4 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 >flags |= EP_Any
209b5 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 Aff;. *pRegLeft
209b6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
209b7 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
209b8 4c 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 Left, pFreeLeft)
209b9 3b 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 ;. while( pRigh
209ba 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 t->op==TK_UPLUS
209bb 29 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 ) pRight = pRigh
209bc 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 t->pLeft;. pRig
209bd 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ht->flags |= EP_
209be 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 AnyAff;. *pRegR
209bf 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 ight = sqlite3Ex
209c0 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
209c1 65 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 e, pRight, pFree
209c2 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Right);.}../*.**
209c3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 Generate code f
209c4 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 or a comparison
209c5 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 operator..*/.sta
209c6 74 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 tic int codeComp
209c7 61 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 are(. Parse *pP
209c8 61 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 arse, /* The
209c9 70 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 parsing (and cod
209ca 65 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f e generating) co
209cb 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 ntext */. Expr
209cc 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 *pLeft, /*
209cd 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 The left operand
209ce 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 */. Expr *pRig
209cf 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 ht, /* The r
209d0 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
209d1 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 int opcode,
209d2 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 /* The compa
209d3 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a rison opcode */.
209d4 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 int in1, int i
209d5 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 n2, /* Register
209d6 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 holding operands
209d7 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 */. int dest,
209d8 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
209d9 68 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a here if true. *
209da 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 /. int jumpIfNu
209db 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 ll /* If true
209dc 2c 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 , jump if either
209dd 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
209de 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b */.){. int p5;
209df 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 . int addr;. C
209e0 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 ollSeq *p4;.. p
209e1 34 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 4 = sqlite3Binar
209e2 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 yCompareCollSeq(
209e3 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 pParse, pLeft, p
209e4 52 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 Right);. p5 = b
209e5 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 inaryCompareP5(p
209e6 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 Left, pRight, ju
209e7 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 mpIfNull);. add
209e8 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
209e9 64 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 ddOp4(pParse->pV
209ea 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 dbe, opcode, in2
209eb 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 , dest, in1,.
209ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
209ed 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 (void*)p
209ee 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 4, P4_COLLSEQ);.
209ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
209f0 6e 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 ngeP5(pParse->pV
209f1 64 62 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 dbe, (u8)p5);.
209f2 69 66 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 if( (p5 & SQLITE
209f3 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 _AFF_MASK)!=SQLI
209f4 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 TE_AFF_NONE ){.
209f5 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
209f6 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
209f7 65 28 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 e(pParse, in1, 1
209f8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 );. sqlite3Ex
209f9 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
209fa 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e hange(pParse, in
209fb 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 2, 1);. }. ret
209fc 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 urn addr;.}..#if
209fd 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 SQLITE_MAX_EXPR
209fe 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 _DEPTH>0./*.** C
209ff 68 65 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 heck that argume
20a00 6e 74 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 nt nHeight is le
20a01 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
20a02 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a to the maximum.
20a03 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 ** expression de
20a04 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 pth allowed. If
20a05 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 it is not, leave
20a06 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
20a07 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a e in.** pParse..
20a08 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20a09 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
20a0a 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 rCheckHeight(Par
20a0b 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
20a0c 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 nHeight){. int
20a0d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
20a0e 20 20 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d int mxHeight =
20a0f 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 pParse->db->aLi
20a10 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
20a11 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 _EXPR_DEPTH];.
20a12 69 66 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 if( nHeight>mxHe
20a13 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 ight ){. sqli
20a14 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20a15 73 65 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 se, . "Exp
20a16 72 65 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 ression tree is
20a17 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d too large (maxim
20a18 75 6d 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d um depth %d)", m
20a19 78 48 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 xHeight. );.
20a1a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
20a1b 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 RROR;. }. retu
20a1c 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 rn rc;.}../* The
20a1d 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 following three
20a1e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 functions, heig
20a1f 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 htOfExpr(), heig
20a20 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a htOfExprList().*
20a21 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 * and heightOfSe
20a22 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 lect(), are used
20a23 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
20a24 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 e maximum height
20a25 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 .** of any expre
20a26 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 ssion tree refer
20a27 65 6e 63 65 64 20 62 79 20 74 68 65 20 73 74 72 enced by the str
20a28 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 ucture passed as
20a29 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 the.** first ar
20a2a 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 gument..**.** If
20a2b 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 this maximum he
20a2c 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20 ight is greater
20a2d 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 than the current
20a2e 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a value pointed.*
20a2f 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 * to by pnHeight
20a30 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 , the second par
20a31 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 ameter, then set
20a32 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 *pnHeight to th
20a33 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a at.** value..*/.
20a34 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 static void heig
20a35 68 74 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 htOfExpr(Expr *p
20a36 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 , int *pnHeight)
20a37 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 {. if( p ){.
20a38 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e if( p->nHeight>
20a39 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 *pnHeight ){.
20a3a 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 *pnHeight = p
20a3b 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d ->nHeight;. }
20a3c 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
20a3d 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c id heightOfExprL
20a3e 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c ist(ExprList *p,
20a3f 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b int *pnHeight){
20a40 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
20a41 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
20a42 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 =0; i<p->nExpr;
20a43 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 i++){. heig
20a44 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d htOfExpr(p->a[i]
20a45 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 .pExpr, pnHeight
20a46 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 );. }. }.}.s
20a47 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 tatic void heigh
20a48 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 tOfSelect(Select
20a49 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 *p, int *pnHeig
20a4a 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a ht){. if( p ){.
20a4b 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 heightOfExpr
20a4c 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 (p->pWhere, pnHe
20a4d 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
20a4e 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 tOfExpr(p->pHavi
20a4f 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 ng, pnHeight);.
20a50 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 heightOfExpr(
20a51 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 p->pLimit, pnHei
20a52 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
20a53 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 OfExpr(p->pOffse
20a54 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, pnHeight);.
20a55 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 heightOfExprLi
20a56 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e st(p->pEList, pn
20a57 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
20a58 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d ghtOfExprList(p-
20a59 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 >pGroupBy, pnHei
20a5a 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
20a5b 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f OfExprList(p->pO
20a5c 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 rderBy, pnHeight
20a5d 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 );. heightOfS
20a5e 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c elect(p->pPrior,
20a5f 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a pnHeight);. }.
20a60 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
20a61 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 Expr.nHeight va
20a62 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 riable in the st
20a63 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 ructure passed a
20a64 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e s an .** argumen
20a65 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e t. An expression
20a66 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 with no childre
20a67 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 n, Expr.pList or
20a68 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 .** Expr.pSelec
20a69 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 t member has a h
20a6a 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 eight of 1. Any
20a6b 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
20a6c 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 .** has a height
20a6d 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 equal to the ma
20a6e 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 ximum height of
20a6f 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 any other .** re
20a70 66 65 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c ferenced Expr pl
20a71 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 us one..*/.stati
20a72 63 20 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 c void exprSetHe
20a73 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 ight(Expr *p){.
20a74 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 int nHeight = 0
20a75 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 ;. heightOfExpr
20a76 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 (p->pLeft, &nHei
20a77 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 ght);. heightOf
20a78 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 Expr(p->pRight,
20a79 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 &nHeight);. if(
20a7a 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
20a7b 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 (p, EP_xIsSelect
20a7c 29 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f ) ){. heightO
20a7d 66 53 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 fSelect(p->x.pSe
20a7e 6c 65 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b lect, &nHeight);
20a7f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 . }else{. he
20a80 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 ightOfExprList(p
20a81 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 ->x.pList, &nHei
20a82 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e ght);. }. p->n
20a83 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 Height = nHeight
20a84 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 + 1;.}../*.** S
20a85 65 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 et the Expr.nHei
20a86 67 68 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 ght variable usi
20a87 6e 67 20 74 68 65 20 65 78 70 72 53 65 74 48 65 ng the exprSetHe
20a88 69 67 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e ight() function.
20a89 20 49 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 If.** the heigh
20a8a 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 t is greater tha
20a8b 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c n the maximum al
20a8c 6c 6f 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e lowed expression
20a8d 20 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 depth,.** leave
20a8e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 an error in pPa
20a8f 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rse..*/.SQLITE_P
20a90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20a91 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 te3ExprSetHeight
20a92 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20a93 45 78 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 Expr *p){. expr
20a94 53 65 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 SetHeight(p);.
20a95 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b sqlite3ExprCheck
20a96 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 Height(pParse, p
20a97 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f ->nHeight);.}../
20a98 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
20a99 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f maximum height o
20a9a 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e f any expression
20a9b 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 tree referenced
20a9c 0a 2a 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 .** by the selec
20a9d 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 t statement pass
20a9e 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ed as an argumen
20a9f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
20aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20aa1 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 SelectExprHeight
20aa2 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 (Select *p){. i
20aa3 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a nt nHeight = 0;.
20aa4 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 heightOfSelect
20aa5 28 70 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 (p, &nHeight);.
20aa6 20 72 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b return nHeight;
20aa7 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 .}.#else. #defi
20aa8 6e 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74 ne exprSetHeight
20aa9 28 79 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 (y).#endif /* SQ
20aaa 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
20aab 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 PTH>0 */../*.**
20aac 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
20aad 74 68 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 the core allocat
20aae 6f 72 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 or for Expr node
20aaf 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 s..**.** Constru
20ab0 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 ct a new express
20ab1 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 ion node and ret
20ab2 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
20ab3 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 it. Memory.**
20ab4 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e for this node an
20ab5 64 20 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e d for the pToken
20ab6 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 argument is a s
20ab7 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e ingle allocation
20ab8 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f .** obtained fro
20ab9 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f m sqlite3DbMallo
20aba 63 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e c(). The callin
20abb 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 g function.** is
20abc 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
20abd 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 making sure the
20abe 20 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 node eventually
20abf 20 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a gets freed..**.
20ac0 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 ** If dequote is
20ac1 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
20ac2 74 6f 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 token (if it exi
20ac3 73 74 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 sts) is dequoted
20ac4 2e 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 ..** If dequote
20ac5 69 73 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 is false, no deq
20ac6 75 6f 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 uoting is perfor
20ac7 6d 61 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 mance. The deQu
20ac8 6f 74 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ote.** parameter
20ac9 20 69 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 is ignored if p
20aca 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 Token is NULL or
20acb 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f if the token do
20acc 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 es not.** appear
20acd 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 to be quoted.
20ace 49 66 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 If the quotes we
20acf 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 re of the form "
20ad0 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f ..." (double-quo
20ad1 74 65 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 tes).** then the
20ad2 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c EP_DblQuoted fl
20ad3 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 ag is set on the
20ad4 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 expression node
20ad5 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 ..**.** Special
20ad6 63 61 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b case: If op==TK
20ad7 5f 49 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f _INTEGER and pTo
20ad8 6b 65 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 ken points to a
20ad9 73 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 string that.** c
20ada 61 6e 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 an be translated
20adb 20 69 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 into a 32-bit i
20adc 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 nteger, then the
20add 20 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a token is not.**
20ade 20 73 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f stored in u.zTo
20adf 6b 65 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 ken. Instead, t
20ae0 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
20ae1 73 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 s is written.**
20ae2 69 6e 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e into u.iValue an
20ae3 64 20 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 d the EP_IntValu
20ae4 65 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 e flag is set.
20ae5 4e 6f 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 No extra storage
20ae6 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 .** is allocated
20ae7 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 to hold the int
20ae8 65 67 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 eger text and th
20ae9 65 20 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 e dequote flag i
20aea 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 s ignored..*/.SQ
20aeb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
20aec 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c r *sqlite3ExprAl
20aed 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a loc(. sqlite3 *
20aee 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f db, /
20aef 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c * Handle for sql
20af0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
20af1 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 () (may be null)
20af2 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 */. int op,
20af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20af4 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f Expression opco
20af5 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f de */. const To
20af6 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 ken *pToken,
20af7 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e /* Token argumen
20af8 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c t. Might be NUL
20af9 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f L */. int dequo
20afa 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f te /
20afb 2a 20 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 * True to dequot
20afc 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a e */.){. Expr *
20afd 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 pNew;. int nExt
20afe 72 61 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 ra = 0;. int iV
20aff 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 alue = 0;.. if(
20b00 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 pToken ){. i
20b01 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 f( op!=TK_INTEGE
20b02 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d R || pToken->z==
20b03 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 0. || s
20b04 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 qlite3GetInt32(p
20b05 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 Token->z, &iValu
20b06 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e e)==0 ){. n
20b07 45 78 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e Extra = pToken->
20b08 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n+1;. }. }.
20b09 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
20b0a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
20b0b 73 69 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 sizeof(Expr)+nEx
20b0c 74 72 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 tra);. if( pNew
20b0d 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 ){. pNew->op
20b0e 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 = (u8)op;. p
20b0f 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a New->iAgg = -1;.
20b10 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 if( pToken )
20b11 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 {. if( nExt
20b12 72 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ra==0 ){.
20b13 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 pNew->flags |=
20b14 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 EP_IntValue;.
20b15 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 pNew->u.iVa
20b16 6c 75 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 lue = iValue;.
20b17 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20b18 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 int c;.
20b19 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e pNew->u.zToken
20b1a 20 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b = (char*)&pNew[
20b1b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 1];. memc
20b1c 70 79 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 py(pNew->u.zToke
20b1d 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 n, pToken->z, pT
20b1e 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 oken->n);.
20b1f 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e pNew->u.zToken
20b20 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b [pToken->n] = 0;
20b21 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 65 71 . if( deq
20b22 75 6f 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d uote && nExtra>=
20b23 33 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3 .
20b24 26 26 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d && ((c = pToken-
20b25 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 >z[0])=='\'' ||
20b26 63 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 c=='"' || c=='['
20b27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 || c=='`') ){.
20b28 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20b29 44 65 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e Dequote(pNew->u.
20b2a 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 zToken);.
20b2b 20 20 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 if( c=='"' )
20b2c 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 pNew->flags |= E
20b2d 50 5f 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 P_DblQuoted;.
20b2e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
20b2f 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f }.#if SQLITE_
20b30 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 MAX_EXPR_DEPTH>0
20b31 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 . pNew->nHeig
20b32 68 74 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 ht = 1;.#endif
20b33 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e . }. return pN
20b34 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c ew;.}../*.** All
20b35 6f 63 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 ocate a new expr
20b36 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d ession node from
20b37 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
20b38 65 64 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 ed token that ha
20b39 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 s.** already bee
20b3a 6e 20 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 n dequoted..*/.S
20b3b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
20b3c 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 pr *sqlite3Expr(
20b3d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
20b3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
20b3f 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 ndle for sqlite3
20b40 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 DbMallocZero() (
20b41 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a may be null) */.
20b42 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
20b43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
20b44 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a ression opcode *
20b45 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
20b46 7a 54 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 zToken /* T
20b47 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 oken argument.
20b48 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f Might be NULL */
20b49 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 .){. Token x;.
20b4a 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 x.z = zToken;.
20b4b 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 x.n = zToken ?
20b4c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
20b4d 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 zToken) : 0;. r
20b4e 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 eturn sqlite3Exp
20b4f 72 41 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 rAlloc(db, op, &
20b50 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 x, 0);.}../*.**
20b51 41 74 74 61 63 68 20 73 75 62 74 72 65 65 73 20 Attach subtrees
20b52 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 pLeft and pRight
20b53 20 74 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 to the Expr nod
20b54 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 e pRoot..**.** I
20b55 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 f pRoot==NULL th
20b56 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
20b57 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
20b58 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 n error has occu
20b59 72 72 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 rred..** In that
20b5a 20 63 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 case, delete th
20b5b 65 20 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 e subtrees pLeft
20b5c 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a and pRight..*/.
20b5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20b5e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
20b5f 74 74 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 ttachSubtrees(.
20b60 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 sqlite3 *db,.
20b61 45 78 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 Expr *pRoot,. E
20b62 78 70 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 xpr *pLeft,. Ex
20b63 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 pr *pRight.){.
20b64 69 66 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a if( pRoot==0 ){.
20b65 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
20b66 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
20b67 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
20b68 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 elete(db, pLeft)
20b69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 ;. sqlite3Exp
20b6a 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 rDelete(db, pRig
20b6b 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ht);. }else{.
20b6c 20 20 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a if( pRight ){.
20b6d 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 pRoot->pRi
20b6e 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 ght = pRight;.
20b6f 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e if( pRight->
20b70 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
20b71 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 llate ){.
20b72 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d pRoot->flags |=
20b73 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a EP_ExpCollate;.
20b74 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 pRoot->p
20b75 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 Coll = pRight->p
20b76 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Coll;. }.
20b77 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 }. if( pLef
20b78 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 t ){. pRoot
20b79 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b ->pLeft = pLeft;
20b7a 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 . if( pLeft
20b7b 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 ->flags & EP_Exp
20b7c 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 Collate ){.
20b7d 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 pRoot->flags
20b7e 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 |= EP_ExpCollate
20b7f 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d ;. pRoot-
20b80 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e >pColl = pLeft->
20b81 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 pColl;. }.
20b82 20 20 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 }. exprSet
20b83 48 65 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 Height(pRoot);.
20b84 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f }.}../*.** Allo
20b85 63 61 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 cate a Expr node
20b86 20 77 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 which joins as
20b87 6d 61 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 many as two subt
20b88 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 rees..**.** One
20b89 6f 72 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 or both of the s
20b8a 75 62 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e ubtrees can be N
20b8b 55 4c 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 ULL. Return a p
20b8c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
20b8d 77 0a 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 w.** Expr node.
20b8e 20 4f 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 Or, if an OOM e
20b8f 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 rror occurs, set
20b90 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
20b91 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 locFailed,.** fr
20b92 65 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 ee the subtrees
20b93 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e and return NULL.
20b94 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20b95 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
20b96 50 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a PExpr(. Parse *
20b97 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
20b98 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20b99 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c ext */. int op,
20b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20b9b 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f /* Expression o
20b9c 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 pcode */. Expr
20b9d 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 *pLeft,
20b9e 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 /* Left opera
20b9f 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 nd */. Expr *pR
20ba0 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 ight,
20ba1 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 /* Right operand
20ba2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 */. const Toke
20ba3 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a n *pToken /*
20ba4 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 Argument token
20ba5 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 */.){. Expr *p
20ba6 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c = sqlite3ExprAll
20ba7 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f oc(pParse->db, o
20ba8 70 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 p, pToken, 1);.
20ba9 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 sqlite3ExprAtta
20baa 63 68 53 75 62 74 72 65 65 73 28 70 50 61 72 73 chSubtrees(pPars
20bab 65 2d 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c e->db, p, pLeft,
20bac 20 70 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 pRight);. retu
20bad 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a rn p;.}../*.** J
20bae 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 oin two expressi
20baf 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 ons using an AND
20bb0 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 operator. If e
20bb1 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e ither expression
20bb2 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 is.** NULL, the
20bb3 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 n just return th
20bb4 65 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 e other expressi
20bb5 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
20bb6 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
20bb7 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 te3ExprAnd(sqlit
20bb8 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c e3 *db, Expr *pL
20bb9 65 66 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 eft, Expr *pRigh
20bba 74 29 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d t){. if( pLeft=
20bbb 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
20bbc 20 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 pRight;. }else
20bbd 20 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 if( pRight==0 )
20bbe 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 {. return pLe
20bbf 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ft;. }else{.
20bc0 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 Expr *pNew = sq
20bc1 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 lite3ExprAlloc(d
20bc2 62 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 b, TK_AND, 0, 0)
20bc3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 ;. sqlite3Exp
20bc4 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 rAttachSubtrees(
20bc5 64 62 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c db, pNew, pLeft,
20bc6 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 pRight);. re
20bc7 74 75 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d turn pNew;. }.}
20bc8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 ../*.** Construc
20bc9 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 t a new expressi
20bca 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 on node for a fu
20bcb 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 nction with mult
20bcc 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 iple.** argument
20bcd 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
20bce 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
20bcf 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 e3ExprFunction(P
20bd0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
20bd1 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 prList *pList, T
20bd2 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 oken *pToken){.
20bd3 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 Expr *pNew;. s
20bd4 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
20bd5 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 rse->db;. asser
20bd6 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 t( pToken );. p
20bd7 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 New = sqlite3Exp
20bd8 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 rAlloc(db, TK_FU
20bd9 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 NCTION, pToken,
20bda 31 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 1);. if( pNew==
20bdb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
20bdc 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
20bdd 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 b, pList); /* Av
20bde 6f 69 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 oid memory leak
20bdf 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c when malloc fail
20be0 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 s */. return
20be1 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 0;. }. pNew->x
20be2 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a .pList = pList;.
20be3 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
20be4 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c asProperty(pNew,
20be5 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
20be6 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 ;. sqlite3ExprS
20be7 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c etHeight(pParse,
20be8 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e pNew);. return
20be9 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
20bea 41 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c Assign a variabl
20beb 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 e number to an e
20bec 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 xpression that e
20bed 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 ncodes a wildcar
20bee 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 d.** in the orig
20bef 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 inal SQL stateme
20bf0 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 nt. .**.** Wild
20bf1 63 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 cards consisting
20bf2 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 of a single "?"
20bf3 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 are assigned th
20bf4 65 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 e next sequentia
20bf5 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 l.** variable nu
20bf6 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 mber..**.** Wild
20bf7 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 cards of the for
20bf8 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 m "?nnn" are ass
20bf9 69 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 igned the number
20bfa 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 "nnn". We make
20bfb 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 .** sure "nnn" i
20bfc 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 s not too be to
20bfd 61 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f avoid a denial o
20bfe 66 20 73 65 72 76 69 63 65 20 61 74 74 61 63 6b f service attack
20bff 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c when.** the SQL
20c00 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 statement comes
20c01 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 from an externa
20c02 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 l source..**.**
20c03 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 Wildcards of the
20c04 20 66 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 form ":aaa", "@
20c05 61 61 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 aaa", or "$aaa"
20c06 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
20c07 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 same number.**
20c08 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 as the previous
20c09 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
20c0a 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 same wildcard.
20c0b 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 Or if this is th
20c0c 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 e first.** insta
20c0d 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 nce of the wildc
20c0e 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 ard, the next se
20c0f 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 quenial variable
20c10 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 number is.** as
20c11 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 signed..*/.SQLIT
20c12 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20c13 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e qlite3ExprAssign
20c14 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 VarNumber(Parse
20c15 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
20c16 45 78 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 Expr){. sqlite3
20c17 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
20c18 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b;. const char
20c19 2a 7a 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 *z;.. if( pExpr
20c1a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
20c1b 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
20c1c 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
20c1d 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 r, EP_IntValue|E
20c1e 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b P_Reduced|EP_Tok
20c1f 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d enOnly) );. z =
20c20 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
20c21 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 ;. assert( z!=0
20c22 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b );. assert( z[
20c23 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 0]!=0 );. if( z
20c24 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a [1]==0 ){. /*
20c25 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 Wildcard of the
20c26 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 form "?". Assi
20c27 67 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 gn the next vari
20c28 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 able number */.
20c29 20 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d assert( z[0]=
20c2a 3d 27 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 ='?' );. pExp
20c2b 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e r->iColumn = (yn
20c2c 56 61 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e Var)(++pParse->n
20c2d 56 61 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Var);. }else if
20c2e 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 ( z[0]=='?' ){.
20c2f 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f /* Wildcard o
20c30 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e f the form "?nnn
20c31 22 2e 20 20 43 6f 6e 76 65 72 74 20 22 6e 6e 6e ". Convert "nnn
20c32 22 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 " to an integer
20c33 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73 65 20 69 and. ** use i
20c34 74 20 61 73 20 74 68 65 20 76 61 72 69 61 62 6c t as the variabl
20c35 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
20c36 69 6e 74 20 69 20 3d 20 61 74 6f 69 28 28 63 68 int i = atoi((ch
20c37 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20 20 ar*)&z[1]);.
20c38 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
20c39 20 28 79 6e 56 61 72 29 69 3b 0a 20 20 20 20 74 (ynVar)i;. t
20c3a 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b estcase( i==0 );
20c3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 . testcase( i
20c3c 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 ==1 );. testc
20c3d 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d ase( i==db->aLim
20c3e 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
20c3f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d VARIABLE_NUMBER]
20c40 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 -1 );. testca
20c41 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 se( i==db->aLimi
20c42 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t[SQLITE_LIMIT_V
20c43 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
20c44 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31 20 7c );. if( i<1 |
20c45 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 | i>db->aLimit[S
20c46 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
20c47 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a ABLE_NUMBER] ){.
20c48 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
20c49 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 orMsg(pParse, "v
20c4a 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d ariable number m
20c4b 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f ust be between ?
20c4c 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 1 and ?%d",.
20c4d 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 db->aLimit
20c4e 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 [SQLITE_LIMIT_VA
20c4f 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b RIABLE_NUMBER]);
20c50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
20c51 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b >pParse->nVar ){
20c52 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e . pParse->n
20c53 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 Var = i;. }.
20c54 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 }else{. /* W
20c55 69 6c 64 63 61 72 64 73 20 6c 69 6b 65 20 22 3a ildcards like ":
20c56 61 61 61 22 2c 20 22 24 61 61 61 22 20 6f 72 20 aaa", "$aaa" or
20c57 22 40 61 61 61 22 2e 20 20 52 65 75 73 65 20 74 "@aaa". Reuse t
20c58 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 he same variable
20c59 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 . ** number a
20c5a 73 20 74 68 65 20 70 72 69 6f 72 20 61 70 70 65 s the prior appe
20c5b 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 arance of the sa
20c5c 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 me name, or if t
20c5d 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 he name. ** h
20c5e 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 72 65 as never appeare
20c5f 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20 d before, reuse
20c60 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c the same variabl
20c61 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a e number. */.
20c62 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 75 int i;. u
20c63 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 32 n;. n = sq
20c64 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 lite3Strlen30(z)
20c65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
20c66 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 <pParse->nVarExp
20c67 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 r; i++){. E
20c68 78 70 72 20 2a 70 45 20 3d 20 70 50 61 72 73 65 xpr *pE = pParse
20c69 2d 3e 61 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a ->apVarExpr[i];.
20c6a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
20c6b 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 !=0 );. if(
20c6c 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 memcmp(pE->u.zT
20c6d 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 oken, z, n)==0 &
20c6e 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e & pE->u.zToken[n
20c6f 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ]==0 ){.
20c70 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
20c71 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 pE->iColumn;.
20c72 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
20c73 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
20c74 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 f( i>=pParse->nV
20c75 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 arExpr ){.
20c76 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
20c77 20 28 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73 (ynVar)(++pPars
20c78 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 20 20 20 20 e->nVar);.
20c79 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 if( pParse->nVar
20c7a 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 Expr>=pParse->nV
20c7b 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b arExprAlloc-1 ){
20c7c 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
20c7d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b >nVarExprAlloc +
20c7e 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 = pParse->nVarEx
20c7f 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 prAlloc + 10;.
20c80 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 pParse->ap
20c81 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 VarExpr =.
20c82 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 sqlite3DbR
20c83 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 eallocOrFree(.
20c84 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a db,.
20c85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
20c86 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c arse->apVarExpr,
20c87 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
20c88 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
20c89 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 lloc*sizeof(pPar
20c8a 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d se->apVarExpr[0]
20c8b 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b ). );
20c8c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
20c8d 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
20c8e 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
20c8f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
20c90 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a apVarExpr!=0 );.
20c91 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
20c92 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 apVarExpr[pParse
20c93 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 ->nVarExpr++] =
20c94 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 pExpr;. }.
20c95 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 }. } . if(
20c96 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 !pParse->nErr &&
20c97 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 pParse->nVar>db
20c98 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
20c99 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
20c9a 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 UMBER] ){. sq
20c9b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20c9c 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
20c9d 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b SQL variables");
20c9e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
20c9f 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 cursively delete
20ca0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
20ca1 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ree..*/.SQLITE_P
20ca2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20ca3 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 71 te3ExprDelete(sq
20ca4 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
20ca5 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 *p){. if( p==0
20ca6 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
20ca7 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 !ExprHasAnyPrope
20ca8 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f rty(p, EP_TokenO
20ca9 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 nly) ){. sqli
20caa 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
20cab 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 , p->pLeft);.
20cac 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
20cad 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 te(db, p->pRight
20cae 29 3b 0a 20 20 20 20 69 66 28 20 21 45 78 70 72 );. if( !Expr
20caf 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
20cb0 50 5f 52 65 64 75 63 65 64 29 20 26 26 20 28 70 P_Reduced) && (p
20cb1 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d ->flags2 & EP2_M
20cb2 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 21 3d 30 allocedToken)!=0
20cb3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
20cb4 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 75 3DbFree(db, p->u
20cb5 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a .zToken);. }.
20cb6 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 if( ExprHasP
20cb7 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 roperty(p, EP_xI
20cb8 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 sSelect) ){.
20cb9 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
20cba 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 elete(db, p->x.p
20cbb 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c Select);. }el
20cbc 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
20cbd 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 3ExprListDelete(
20cbe 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b db, p->x.pList);
20cbf 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
20cc0 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
20cc1 79 28 70 2c 20 45 50 5f 53 74 61 74 69 63 29 20 y(p, EP_Static)
20cc2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
20cc3 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d Free(db, p);. }
20cc4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
20cc5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
20cc6 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 ytes allocated f
20cc7 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f or the expressio
20cc8 6e 20 73 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 n structure .**
20cc9 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
20cca 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 rst argument. Th
20ccb 69 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 is is always one
20ccc 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a of EXPR_FULLSIZ
20ccd 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 44 55 43 E,.** EXPR_REDUC
20cce 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52 5f 54 EDSIZE or EXPR_T
20ccf 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f OKENONLYSIZE..*/
20cd0 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 .static int expr
20cd1 53 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 StructSize(Expr
20cd2 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 72 48 *p){. if( ExprH
20cd3 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
20cd4 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65 _TokenOnly) ) re
20cd5 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f turn EXPR_TOKENO
20cd6 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 28 20 45 NLYSIZE;. if( E
20cd7 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20cd8 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 20 , EP_Reduced) )
20cd9 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45 44 55 return EXPR_REDU
20cda 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74 75 72 CEDSIZE;. retur
20cdb 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b n EXPR_FULLSIZE;
20cdc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 75 .}../*.** The du
20cdd 70 65 64 45 78 70 72 2a 53 69 7a 65 28 29 20 72 pedExpr*Size() r
20cde 6f 75 74 69 6e 65 73 20 65 61 63 68 20 72 65 74 outines each ret
20cdf 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
20ce0 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
20ce1 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 61 20 63 .** to store a c
20ce2 6f 70 79 20 6f 66 20 61 6e 20 65 78 70 72 65 73 opy of an expres
20ce3 73 69 6f 6e 20 6f 72 20 65 78 70 72 65 73 73 69 sion or expressi
20ce4 6f 6e 20 74 72 65 65 2e 20 20 54 68 65 79 20 64 on tree. They d
20ce5 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68 6f 77 20 iffer in.** how
20ce6 6d 75 63 68 20 6f 66 20 74 68 65 20 74 72 65 65 much of the tree
20ce7 20 69 73 20 6d 65 61 73 75 72 65 64 2e 0a 2a 2a is measured..**
20ce8 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 .** dupedExp
20ce9 72 53 74 72 75 63 74 53 69 7a 65 28 29 20 20 20 rStructSize()
20cea 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c 79 20 74 Size of only t
20ceb 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 he Expr structur
20cec 65 20 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 e .** dupedE
20ced 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 20 20 20 xprNodeSize()
20cee 20 20 20 20 53 69 7a 65 20 6f 66 20 45 78 70 72 Size of Expr
20cef 20 2b 20 73 70 61 63 65 20 66 6f 72 20 74 6f 6b + space for tok
20cf0 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 en.** dupedE
20cf1 78 70 72 53 69 7a 65 28 29 20 20 20 20 20 20 20 xprSize()
20cf2 20 20 20 20 45 78 70 72 20 2b 20 74 6f 6b 65 6e Expr + token
20cf3 20 2b 20 73 75 62 74 72 65 65 20 63 6f 6d 70 6f + subtree compo
20cf4 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a nents.**.*******
20cf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20cf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20cf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20cf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20cf9 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 ****.**.** The d
20cfa 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 upedExprStructSi
20cfb 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 ze() function re
20cfc 74 75 72 6e 73 20 74 77 6f 20 76 61 6c 75 65 73 turns two values
20cfd 20 4f 52 2d 65 64 20 74 6f 67 65 74 68 65 72 3a OR-ed together:
20cfe 20 20 0a 2a 2a 20 28 31 29 20 74 68 65 20 73 70 .** (1) the sp
20cff 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
20d00 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 a copy of the E
20d01 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 6e xpr structure on
20d02 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32 29 20 74 ly and .** (2) t
20d03 68 65 20 45 50 5f 78 78 78 20 66 6c 61 67 73 20 he EP_xxx flags
20d04 74 68 61 74 20 69 6e 64 69 63 61 74 65 20 77 68 that indicate wh
20d05 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65 at the structure
20d06 20 73 69 7a 65 20 73 68 6f 75 6c 64 20 62 65 2e size should be.
20d07 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 .** The return v
20d08 61 6c 75 65 73 20 69 73 20 61 6c 77 61 79 73 20 alues is always
20d09 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 one of:.**.**
20d0a 20 20 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 EXPR_FULLSIZE
20d0b 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 52 45 .** EXPR_RE
20d0c 44 55 43 45 44 53 49 5a 45 20 20 20 7c 20 45 50 DUCEDSIZE | EP
20d0d 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20 20 20 20 _Reduced.**
20d0e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 EXPR_TOKENONLYS
20d0f 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e IZE | EP_TokenOn
20d10 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a ly.**.** The siz
20d11 65 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 e of the structu
20d12 72 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 re can be found
20d13 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 20 72 by masking the r
20d14 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f eturn value.** o
20d15 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 f this routine w
20d16 69 74 68 20 30 78 66 66 66 2e 20 20 54 68 65 20 ith 0xfff. The
20d17 66 6c 61 67 73 20 63 61 6e 20 62 65 20 66 6f 75 flags can be fou
20d18 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 nd by masking th
20d19 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 e.** return valu
20d1a 65 20 77 69 74 68 20 45 50 5f 52 65 64 75 63 65 e with EP_Reduce
20d1b 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a d|EP_TokenOnly..
20d1c 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
20d1d 77 69 74 68 20 66 6c 61 67 73 3d 3d 45 58 50 52 with flags==EXPR
20d1e 44 55 50 5f 52 45 44 55 43 45 2c 20 74 68 69 73 DUP_REDUCE, this
20d1f 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 73 20 routines works
20d20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20 on full-size.**
20d21 28 75 6e 72 65 64 75 63 65 64 29 20 45 78 70 72 (unreduced) Expr
20d22 20 6f 62 6a 65 63 74 73 20 61 73 20 74 68 65 79 objects as they
20d23 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c 79 20 63 or originally c
20d24 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 74 68 onstructed by th
20d25 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 44 75 72 e parser..** Dur
20d26 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 20 61 ing expression a
20d27 6e 61 6c 79 73 69 73 2c 20 65 78 74 72 61 20 69 nalysis, extra i
20d28 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 63 6f nformation is co
20d29 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f 76 65 64 mputed and moved
20d2a 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65 72 20 70 into.** later p
20d2b 61 72 74 73 20 6f 66 20 74 65 68 20 45 78 70 72 arts of teh Expr
20d2c 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74 object and that
20d2d 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 extra informati
20d2e 6f 6e 20 6d 69 67 68 74 20 67 65 74 20 63 68 6f on might get cho
20d2f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69 66 20 74 pped.** off if t
20d30 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
20d31 20 72 65 64 75 63 65 64 2e 20 20 4e 6f 74 65 20 reduced. Note
20d32 61 6c 73 6f 20 74 68 61 74 20 69 74 20 64 6f 65 also that it doe
20d33 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a s not work to.**
20d34 20 6d 61 6b 65 20 61 20 45 58 50 52 44 55 50 5f make a EXPRDUP_
20d35 52 45 44 55 43 45 20 63 6f 70 79 20 6f 66 20 61 REDUCE copy of a
20d36 20 72 65 64 75 63 65 64 20 65 78 70 72 65 73 73 reduced express
20d37 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 ion. It is only
20d38 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20 72 65 64 legal.** to red
20d39 75 63 65 20 61 20 70 72 69 73 74 69 6e 65 20 65 uce a pristine e
20d3a 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 66 xpression tree f
20d3b 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72 2e 20 rom the parser.
20d3c 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
20d3d 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70 65 64 45 ion.** of dupedE
20d3e 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 20 xprStructSize()
20d3f 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69 70 6c 65 contain multiple
20d40 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
20d41 65 6e 74 73 20 74 68 61 74 20 61 74 74 65 6d 70 ents that attemp
20d42 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72 63 65 20 t.** to enforce
20d43 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e this constraint.
20d44 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
20d45 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 upedExprStructSi
20d46 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 ze(Expr *p, int
20d47 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53 flags){. int nS
20d48 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 66 ize;. assert( f
20d49 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 lags==EXPRDUP_RE
20d4a 44 55 43 45 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 DUCE || flags==0
20d4b 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 ); /* Only one
20d4c 66 6c 61 67 20 76 61 6c 75 65 20 61 6c 6c 6f 77 flag value allow
20d4d 65 64 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28 ed */. if( 0==(
20d4e 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 flags&EXPRDUP_RE
20d4f 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e 53 69 DUCE) ){. nSi
20d50 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49 ze = EXPR_FULLSI
20d51 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ZE;. }else{.
20d52 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
20d53 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 sAnyProperty(p,
20d54 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f EP_TokenOnly|EP_
20d55 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 Reduced) );.
20d56 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
20d57 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46 Property(p, EP_F
20d58 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a 20 20 20 romJoin) ); .
20d59 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 assert( (p->fla
20d5a 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 gs2 & EP2_Malloc
20d5b 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 edToken)==0 );.
20d5c 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 assert( (p->f
20d5d 6c 61 67 73 32 20 26 20 45 50 32 5f 49 72 72 65 lags2 & EP2_Irre
20d5e 64 75 63 69 62 6c 65 29 3d 3d 30 20 29 3b 0a 20 ducible)==0 );.
20d5f 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 if( p->pLeft
20d60 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20 || p->pRight ||
20d61 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 p->pColl || p->x
20d62 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 .pList ){.
20d63 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 52 45 44 nSize = EXPR_RED
20d64 55 43 45 44 53 49 5a 45 20 7c 20 45 50 5f 52 65 UCEDSIZE | EP_Re
20d65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65 6c 73 65 duced;. }else
20d66 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 {. nSize =
20d67 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 EXPR_TOKENONLYSI
20d68 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c ZE | EP_TokenOnl
20d69 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 y;. }. }. r
20d6a 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a eturn nSize;.}..
20d6b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
20d6c 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
20d6d 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 72 space in bytes r
20d6e 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 equired to store
20d6f 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 the copy .** of
20d70 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 the Expr struct
20d71 75 72 65 20 61 6e 64 20 61 20 63 6f 70 79 20 6f ure and a copy o
20d72 66 20 74 68 65 20 45 78 70 72 2e 75 2e 7a 54 6f f the Expr.u.zTo
20d73 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20 74 ken string (if t
20d74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 hat.** string is
20d75 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74 defined.).*/.st
20d76 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 45 78 atic int dupedEx
20d77 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 70 72 20 prNodeSize(Expr
20d78 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
20d79 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 64 75 int nByte = du
20d7a 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a pedExprStructSiz
20d7b 65 28 70 2c 20 66 6c 61 67 73 29 20 26 20 30 78 e(p, flags) & 0x
20d7c 66 66 66 3b 0a 20 20 69 66 28 20 21 45 78 70 72 fff;. if( !Expr
20d7d 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
20d7e 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 20 70 P_IntValue) && p
20d7f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 ->u.zToken ){.
20d80 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 nByte += sqlit
20d81 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e e3Strlen30(p->u.
20d82 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20 zToken)+1;. }.
20d83 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e return ROUND8(n
20d84 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Byte);.}../*.**
20d85 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
20d86 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
20d87 72 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 red to create a
20d88 64 75 70 6c 69 63 61 74 65 20 6f 66 20 74 68 65 duplicate of the
20d89 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 .** expression
20d8a 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
20d8b 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 rst argument. Th
20d8c 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
20d8d 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 6b 20 63 t is a.** mask c
20d8e 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 52 44 55 ontaining EXPRDU
20d8f 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a P_XXX flags..**.
20d90 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 ** The value ret
20d91 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 73 urned includes s
20d92 70 61 63 65 20 74 6f 20 63 72 65 61 74 65 20 61 pace to create a
20d93 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 copy of the Exp
20d94 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 74 73 65 r struct.** itse
20d95 6c 66 20 61 6e 64 20 74 68 65 20 62 75 66 66 65 lf and the buffe
20d96 72 20 72 65 66 65 72 72 65 64 20 74 6f 20 62 79 r referred to by
20d97 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20 Expr.u.zToken,
20d98 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 if any..**.** If
20d99 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44 the EXPRDUP_RED
20d9a 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c UCE flag is set,
20d9b 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e then the return
20d9c 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 value includes
20d9d 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 64 75 70 .** space to dup
20d9e 6c 69 63 61 74 65 20 61 6c 6c 20 45 78 70 72 20 licate all Expr
20d9f 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 74 72 65 nodes in the tre
20da0 65 20 66 6f 72 6d 65 64 20 62 79 20 45 78 70 72 e formed by Expr
20da1 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e 64 20 45 .pLeft .** and E
20da2 78 70 72 2e 70 52 69 67 68 74 20 76 61 72 69 61 xpr.pRight varia
20da3 62 6c 65 73 20 28 62 75 74 20 6e 6f 74 20 66 6f bles (but not fo
20da4 72 20 61 6e 79 20 73 74 72 75 63 74 75 72 65 73 r any structures
20da5 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f 72 20 0a pointed to or .
20da6 2a 2a 20 64 65 73 63 65 6e 64 65 64 20 66 72 6f ** descended fro
20da7 6d 20 74 68 65 20 45 78 70 72 2e 78 2e 70 4c 69 m the Expr.x.pLi
20da8 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 st or Expr.x.pSe
20da9 6c 65 63 74 20 76 61 72 69 61 62 6c 65 73 29 2e lect variables).
20daa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
20dab 75 70 65 64 45 78 70 72 53 69 7a 65 28 45 78 70 upedExprSize(Exp
20dac 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 r *p, int flags)
20dad 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 {. int nByte =
20dae 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 0;. if( p ){.
20daf 20 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45 nByte = dupedE
20db0 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 xprNodeSize(p, f
20db1 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 66 lags);. if( f
20db2 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 lags&EXPRDUP_RED
20db3 55 43 45 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 UCE ){. nBy
20db4 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 53 te += dupedExprS
20db5 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 6c ize(p->pLeft, fl
20db6 61 67 73 29 20 2b 20 64 75 70 65 64 45 78 70 72 ags) + dupedExpr
20db7 53 69 7a 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 Size(p->pRight,
20db8 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 flags);. }.
20db9 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79 74 65 }. return nByte
20dba 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
20dbb 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d 69 function is simi
20dbc 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 45 78 lar to sqlite3Ex
20dbd 70 72 44 75 70 28 29 2c 20 65 78 63 65 70 74 20 prDup(), except
20dbe 74 68 61 74 20 69 66 20 70 7a 42 75 66 66 65 72 that if pzBuffer
20dbf 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c .** is not NULL
20dc0 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65 72 20 then *pzBuffer
20dc1 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70 6f is assumed to po
20dc2 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 int to a buffer
20dc3 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a 2a 2a large enough .**
20dc4 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f to store the co
20dc5 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e py of expression
20dc6 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73 20 6f p, the copies o
20dc7 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a f p->u.zToken.**
20dc8 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 (if applicable)
20dc9 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69 65 73 , and the copies
20dca 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65 66 74 of the p->pLeft
20dcb 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74 20 65 and p->pRight e
20dcc 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69 xpressions,.** i
20dcd 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20 72 65 f any. Before re
20dce 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75 66 66 turning, *pzBuff
20dcf 65 72 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 er is set to the
20dd0 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 73 first byte pass
20dd1 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74 69 6f ed the.** portio
20dd2 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 n of the buffer
20dd3 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79 20 74 copied into by t
20dd4 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f his function..*/
20dd5 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a 65 78 .static Expr *ex
20dd6 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 prDup(sqlite3 *d
20dd7 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 b, Expr *p, int
20dd8 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a 42 75 flags, u8 **pzBu
20dd9 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20 2a 70 ffer){. Expr *p
20dda 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 New = 0;
20ddb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20ddc 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e Value to return
20ddd 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 */. if( p ){.
20dde 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 52 const int isR
20ddf 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 26 educed = (flags&
20de0 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b EXPRDUP_REDUCE);
20de1 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b . u8 *zAlloc;
20de2 0a 20 20 20 20 75 33 32 20 73 74 61 74 69 63 46 . u32 staticF
20de3 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20 20 61 73 lag = 0;.. as
20de4 73 65 72 74 28 20 70 7a 42 75 66 66 65 72 3d 3d sert( pzBuffer==
20de5 30 20 7c 7c 20 69 73 52 65 64 75 63 65 64 20 29 0 || isReduced )
20de6 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 ;.. /* Figure
20de7 20 6f 75 74 20 77 68 65 72 65 20 74 6f 20 77 72 out where to wr
20de8 69 74 65 20 74 68 65 20 6e 65 77 20 45 78 70 72 ite the new Expr
20de9 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 structure. */.
20dea 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 if( pzBuffer
20deb 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 ){. zAlloc
20dec 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20 20 20 = *pzBuffer;.
20ded 20 20 20 73 74 61 74 69 63 46 6c 61 67 20 3d 20 staticFlag =
20dee 45 50 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 7d EP_Static;. }
20def 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41 6c 6c else{. zAll
20df0 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
20df1 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75 70 65 llocRaw(db, dupe
20df2 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66 6c 61 dExprSize(p, fla
20df3 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 gs));. }.
20df4 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a 29 7a pNew = (Expr *)z
20df5 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66 28 20 Alloc;.. if(
20df6 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a pNew ){. /*
20df7 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 74 6f Set nNewSize to
20df8 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f 63 61 the size alloca
20df9 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 72 75 ted for the stru
20dfa 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f cture pointed to
20dfb 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 4e 65 . ** by pNe
20dfc 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 68 65 w. This is eithe
20dfd 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c r EXPR_FULLSIZE,
20dfe 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a EXPR_REDUCEDSIZ
20dff 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 45 58 E or. ** EX
20e00 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
20e01 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 . nToken is set
20e02 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
20e03 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 0a bytes consumed.
20e04 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 ** by the
20e05 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d 3e 75 copy of the p->u
20e06 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 .zToken string (
20e07 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a if any).. *
20e08 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e /. const un
20e09 73 69 67 6e 65 64 20 6e 53 74 72 75 63 74 53 69 signed nStructSi
20e0a 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 ze = dupedExprSt
20e0b 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 ructSize(p, flag
20e0c 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 s);. const
20e0d 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20 3d 20 6e int nNewSize = n
20e0e 53 74 72 75 63 74 53 69 7a 65 20 26 20 30 78 66 StructSize & 0xf
20e0f 66 66 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 ff;. int nT
20e10 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 oken;. if(
20e11 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
20e12 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 (p, EP_IntValue)
20e13 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 && p->u.zToken
20e14 29 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 ){. nToke
20e15 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
20e16 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 n30(p->u.zToken)
20e17 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 + 1;. }els
20e18 65 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 e{. nToke
20e19 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 n = 0;. }.
20e1a 20 20 20 20 20 69 66 28 20 69 73 52 65 64 75 63 if( isReduc
20e1b 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ed ){. as
20e1c 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 6f sert( ExprHasPro
20e1d 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 perty(p, EP_Redu
20e1e 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ced)==0 );.
20e1f 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 memcpy(zAlloc
20e20 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 3b 0a , p, nNewSize);.
20e21 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
20e22 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 20 3d int nSize =
20e23 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 exprStructSize(
20e24 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 p);. memc
20e25 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53 py(zAlloc, p, nS
20e26 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 ize);. me
20e27 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69 mset(&zAlloc[nSi
20e28 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46 55 4c ze], 0, EXPR_FUL
20e29 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20 20 LSIZE-nSize);.
20e2a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
20e2b 53 65 74 20 74 68 65 20 45 50 5f 52 65 64 75 63 Set the EP_Reduc
20e2c 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ed, EP_TokenOnly
20e2d 2c 20 61 6e 64 20 45 50 5f 53 74 61 74 69 63 20 , and EP_Static
20e2e 66 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 flags appropriat
20e2f 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e ely. */. pN
20e30 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45 ew->flags &= ~(E
20e31 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b P_Reduced|EP_Tok
20e32 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61 74 69 63 enOnly|EP_Static
20e33 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 );. pNew->f
20e34 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75 63 74 53 lags |= nStructS
20e35 69 7a 65 20 26 20 28 45 50 5f 52 65 64 75 63 65 ize & (EP_Reduce
20e36 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b d|EP_TokenOnly);
20e37 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 . pNew->fla
20e38 67 73 20 7c 3d 20 73 74 61 74 69 63 46 6c 61 67 gs |= staticFlag
20e39 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 ;.. /* Copy
20e3a 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e the p->u.zToken
20e3b 20 73 74 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e string, if any.
20e3c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 */. if( nT
20e3d 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 oken ){.
20e3e 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 char *zToken = p
20e3f 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 New->u.zToken =
20e40 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e (char*)&zAlloc[n
20e41 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 NewSize];.
20e42 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c memcpy(zToken,
20e43 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 p->u.zToken, nT
20e44 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 0a oken);. }..
20e45 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 28 70 if( 0==((p
20e46 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c ->flags|pNew->fl
20e47 61 67 73 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f ags) & EP_TokenO
20e48 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 nly) ){.
20e49 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 /* Fill in the p
20e4a 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f New->x.pSelect o
20e4b 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 r pNew->x.pList
20e4c 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 member. */.
20e4d 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
20e4e 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 operty(p, EP_xIs
20e4f 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 Select) ){.
20e50 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65 pNew->x.pSe
20e51 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 lect = sqlite3Se
20e52 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 lectDup(db, p->x
20e53 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 64 75 .pSelect, isRedu
20e54 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 ced);. }e
20e55 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 lse{. p
20e56 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73 New->x.pList = s
20e57 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
20e58 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 p(db, p->x.pList
20e59 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 , isReduced);.
20e5a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
20e5b 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 . /* Fill i
20e5c 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e n pNew->pLeft an
20e5d 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20 d pNew->pRight.
20e5e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 */. if( Exp
20e5f 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
20e60 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64 pNew, EP_Reduced
20e61 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 |EP_TokenOnly) )
20e62 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63 {. zAlloc
20e63 20 2b 3d 20 64 75 70 65 64 45 78 70 72 4e 6f 64 += dupedExprNod
20e64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b eSize(p, flags);
20e65 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 . if( Exp
20e66 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 rHasProperty(pNe
20e67 77 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 w, EP_Reduced) )
20e68 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 {. pNew
20e69 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70 72 44 75 ->pLeft = exprDu
20e6a 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 p(db, p->pLeft,
20e6b 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 EXPRDUP_REDUCE,
20e6c 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 &zAlloc);.
20e6d 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 pNew->pRight
20e6e 20 3d 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 = exprDup(db, p
20e6f 2d 3e 70 52 69 67 68 74 2c 20 45 58 50 52 44 55 ->pRight, EXPRDU
20e70 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f P_REDUCE, &zAllo
20e71 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 c);. }.
20e72 20 20 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 if( pzBuff
20e73 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 er ){.
20e74 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c *pzBuffer = zAll
20e75 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 oc;. }.
20e76 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20e77 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 32 20 pNew->flags2
20e78 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 0;. if(
20e79 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 !ExprHasAnyProp
20e7a 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e erty(p, EP_Token
20e7b 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 Only) ){.
20e7c 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d pNew->pLeft =
20e7d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
20e7e 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29 db, p->pLeft, 0)
20e7f 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 ;. pNew
20e80 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 ->pRight = sqlit
20e81 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d e3ExprDup(db, p-
20e82 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 >pRight, 0);.
20e83 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a }. }..
20e84 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
20e85 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
20e86 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
20e87 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 group of routine
20e88 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69 s make deep copi
20e89 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e es of expression
20e8a 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e s,.** expression
20e8b 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73 lists, ID lists
20e8c 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61 , and select sta
20e8d 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f tements. The co
20e8e 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 pies can.** be d
20e8f 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67 eleted (by being
20e90 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72 passed to their
20e91 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 respective ...D
20e92 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73 elete() routines
20e93 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66 ).** without eff
20e94 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69 ecting the origi
20e95 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nals..**.** The
20e96 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c expression list,
20e97 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 ID, and source
20e98 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20 lists return by
20e99 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20e9a 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 up(),.** sqlite3
20e9b 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64 IdListDup(), and
20e9c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 sqlite3SrcListD
20e9d 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 up() can not be
20e9e 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64 further expanded
20e9f 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65 .** by subseque
20ea0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 nt calls to sqli
20ea1 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20 te*ListAppend()
20ea2 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 routines..**.**
20ea3 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20 Any tables that
20ea4 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68 the SrcList migh
20ea5 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e t point to are n
20ea6 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a ot duplicated..*
20ea7 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70 *.** The flags p
20ea8 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61 69 6e arameter contain
20ea9 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 s a combination
20eaa 6f 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 58 of the EXPRDUP_X
20eab 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20 49 66 20 XX flags..** If
20eac 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 the EXPRDUP_REDU
20ead 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 CE flag is set,
20eae 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 then the structu
20eaf 72 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 re returned is a
20eb0 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 76 65 .** truncated ve
20eb1 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 73 75 rsion of the usu
20eb2 61 6c 20 45 78 70 72 20 73 74 72 75 63 74 75 72 al Expr structur
20eb3 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 e that will be s
20eb4 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 tored as.** part
20eb5 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
20eb6 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e y representation
20eb7 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
20eb8 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 schema..*/.SQLI
20eb9 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
20eba 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 *sqlite3ExprDup(
20ebb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
20ebc 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 r *p, int flags)
20ebd 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 44 {. return exprD
20ebe 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c up(db, p, flags,
20ebf 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
20ec0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
20ec1 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20ec2 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
20ec3 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 ExprList *p, int
20ec4 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 70 72 4c flags){. ExprL
20ec5 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 ist *pNew;. str
20ec6 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
20ec7 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 m *pItem, *pOldI
20ec8 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 tem;. int i;.
20ec9 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
20eca 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 n 0;. pNew = sq
20ecb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
20ecc 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 (db, sizeof(*pNe
20ecd 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 w) );. if( pNew
20ece 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
20ecf 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 pNew->iECursor
20ed0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 = 0;. pNew->nE
20ed1 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c xpr = pNew->nAll
20ed2 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 oc = p->nExpr;.
20ed3 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d pNew->a = pItem
20ed4 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
20ed5 6f 63 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 ocRaw(db, p->nE
20ed6 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b xpr*sizeof(p->a[
20ed7 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74 0]) );. if( pIt
20ed8 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c em==0 ){. sql
20ed9 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20eda 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e New);. return
20edb 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 0;. } . pOldI
20edc 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f tem = p->a;. fo
20edd 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 r(i=0; i<p->nExp
20ede 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c r; i++, pItem++,
20edf 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 pOldItem++){.
20ee0 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 78 70 72 Expr *pOldExpr
20ee1 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 = pOldItem->pEx
20ee2 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 pr;. pItem->p
20ee3 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 Expr = sqlite3Ex
20ee4 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 prDup(db, pOldEx
20ee5 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 pr, flags);.
20ee6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 pItem->zName = s
20ee7 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
20ee8 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 b, pOldItem->zNa
20ee9 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e me);. pItem->
20eea 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 33 44 zSpan = sqlite3D
20eeb 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20eec 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 Item->zSpan);.
20eed 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 pItem->sortOrd
20eee 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 er = pOldItem->s
20eef 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 ortOrder;. pI
20ef0 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 tem->done = 0;.
20ef1 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
20ef2 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b pOldItem->iCol;
20ef3 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 . pItem->iAli
20ef4 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 as = pOldItem->i
20ef5 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 Alias;. }. ret
20ef6 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
20ef7 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c 20 74 ** If cursors, t
20ef8 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 20 61 riggers, views a
20ef9 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 61 72 nd subqueries ar
20efa 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 e all omitted fr
20efb 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c om.** the build,
20efc 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 then none of th
20efd 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
20efe 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 6f 72 ines, except for
20eff 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 .** sqlite3Sele
20f00 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 65 20 ctDup(), can be
20f01 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 called. sqlite3S
20f02 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 73 6f electDup() is so
20f03 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 metimes.** calle
20f04 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 d with a NULL ar
20f05 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 gument..*/.#if !
20f06 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
20f07 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
20f08 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
20f09 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c T_TRIGGER) \. ||
20f0a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
20f0b 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a _OMIT_SUBQUERY).
20f0c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
20f0d 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
20f0e 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 rcListDup(sqlite
20f0f 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 3 *db, SrcList *
20f10 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
20f11 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a SrcList *pNew;.
20f12 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
20f13 42 79 74 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 Byte;. if( p==0
20f14 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e ) return 0;. n
20f15 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 Byte = sizeof(*p
20f16 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f ) + (p->nSrc>0 ?
20f17 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 sizeof(p->a[0])
20f18 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a * (p->nSrc-1) :
20f19 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 0);. pNew = sq
20f1a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
20f1b 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 (db, nByte );.
20f1c 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
20f1d 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e turn 0;. pNew->
20f1e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c nSrc = pNew->nAl
20f1f 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 loc = p->nSrc;.
20f20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
20f21 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 Src; i++){. s
20f22 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
20f23 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 em *pNewItem = &
20f24 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 pNew->a[i];.
20f25 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
20f26 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 tem *pOldItem =
20f27 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61 &p->a[i];. Ta
20f28 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 70 ble *pTab;. p
20f29 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 NewItem->zDataba
20f2a 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 se = sqlite3DbSt
20f2b 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
20f2c 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 m->zDatabase);.
20f2d 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 pNewItem->zNa
20f2e 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
20f2f 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
20f30 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 m->zName);. p
20f31 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 NewItem->zAlias
20f32 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20f33 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20f34 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 zAlias);. pNe
20f35 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 wItem->jointype
20f36 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e = pOldItem->join
20f37 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 type;. pNewIt
20f38 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f em->iCursor = pO
20f39 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b ldItem->iCursor;
20f3a 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 . pNewItem->i
20f3b 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c sPopulated = pOl
20f3c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 dItem->isPopulat
20f3d 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d ed;. pNewItem
20f3e 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 ->zIndex = sqlit
20f3f 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
20f40 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 OldItem->zIndex)
20f41 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
20f42 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c notIndexed = pOl
20f43 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 dItem->notIndexe
20f44 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d d;. pNewItem-
20f45 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74 >pIndex = pOldIt
20f46 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 em->pIndex;.
20f47 70 54 61 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d pTab = pNewItem-
20f48 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d >pTab = pOldItem
20f49 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 ->pTab;. if(
20f4a 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 54 pTab ){. pT
20f4b 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 ab->nRef++;.
20f4c 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e }. pNewItem->
20f4d 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 pSelect = sqlite
20f4e 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
20f4f 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 OldItem->pSelect
20f50 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e , flags);. pN
20f51 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 ewItem->pOn = sq
20f52 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20f53 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 pOldItem->pOn,
20f54 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 flags);. pNew
20f55 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73 Item->pUsing = s
20f56 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 qlite3IdListDup(
20f57 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 db, pOldItem->pU
20f58 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 sing);. pNewI
20f59 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 tem->colUsed = p
20f5a 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 OldItem->colUsed
20f5b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
20f5c 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 New;.}.SQLITE_PR
20f5d 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 IVATE IdList *sq
20f5e 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 lite3IdListDup(s
20f5f 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 qlite3 *db, IdLi
20f60 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74 st *p){. IdList
20f61 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b *pNew;. int i;
20f62 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
20f63 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d turn 0;. pNew =
20f64 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20f65 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a Raw(db, sizeof(*
20f66 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 pNew) );. if( p
20f67 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
20f68 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 0;. pNew->nId =
20f69 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 pNew->nAlloc =
20f6a 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e p->nId;. pNew->
20f6b 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c a = sqlite3DbMal
20f6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49 locRaw(db, p->nI
20f6d 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d d*sizeof(p->a[0]
20f6e 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d ) );. if( pNew-
20f6f 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c >a==0 ){. sql
20f70 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20f71 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e New);. return
20f72 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 0;. }. for(i=
20f73 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 0; i<p->nId; i++
20f74 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 ){. struct Id
20f75 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 List_item *pNewI
20f76 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 tem = &pNew->a[i
20f77 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 ];. struct Id
20f78 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 List_item *pOldI
20f79 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a tem = &p->a[i];.
20f7a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e pNewItem->zN
20f7b 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ame = sqlite3DbS
20f7c 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 trDup(db, pOldIt
20f7d 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 em->zName);.
20f7e 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 pNewItem->idx =
20f7f 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 pOldItem->idx;.
20f80 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
20f81 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
20f82 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
20f83 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 e3SelectDup(sqli
20f84 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 te3 *db, Select
20f85 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
20f86 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a Select *pNew;.
20f87 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
20f88 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 urn 0;. pNew =
20f89 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
20f8a 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 aw(db, sizeof(*p
20f8b 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d ) );. if( pNew=
20f8c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
20f8d 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 pNew->pEList =
20f8e 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20f8f 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 up(db, p->pEList
20f90 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
20f91 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 ->pSrc = sqlite3
20f92 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20 70 SrcListDup(db, p
20f93 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29 3b 0a ->pSrc, flags);.
20f94 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d pNew->pWhere =
20f95 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
20f96 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 66 db, p->pWhere, f
20f97 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 lags);. pNew->p
20f98 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 GroupBy = sqlite
20f99 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 3ExprListDup(db,
20f9a 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 66 6c p->pGroupBy, fl
20f9b 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 ags);. pNew->pH
20f9c 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 aving = sqlite3E
20f9d 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 48 xprDup(db, p->pH
20f9e 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 3b 0a 20 aving, flags);.
20f9f 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 pNew->pOrderBy
20fa0 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
20fa1 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64 tDup(db, p->pOrd
20fa2 65 72 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 erBy, flags);.
20fa3 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 pNew->op = p->op
20fa4 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 ;. pNew->pPrior
20fa5 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 = sqlite3Select
20fa6 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f Dup(db, p->pPrio
20fa7 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 r, flags);. pNe
20fa8 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 w->pLimit = sqli
20fa9 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
20faa 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 67 73 29 ->pLimit, flags)
20fab 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 ;. pNew->pOffse
20fac 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 t = sqlite3ExprD
20fad 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 up(db, p->pOffse
20fae 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 t, flags);. pNe
20faf 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 w->iLimit = 0;.
20fb0 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d pNew->iOffset =
20fb1 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 0;. pNew->selF
20fb2 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 lags = p->selFla
20fb3 67 73 20 26 20 7e 53 46 5f 55 73 65 73 45 70 68 gs & ~SF_UsesEph
20fb4 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e emeral;. pNew->
20fb5 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a pRightmost = 0;.
20fb6 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e pNew->addrOpen
20fb7 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 Ephm[0] = -1;.
20fb8 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 pNew->addrOpenEp
20fb9 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e hm[1] = -1;. pN
20fba 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d ew->addrOpenEphm
20fbb 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 [2] = -1;. retu
20fbc 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 rn pNew;.}.#else
20fbd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20fbe 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 Select *sqlite3S
20fbf 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 electDup(sqlite3
20fc0 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c *db, Select *p,
20fc1 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 61 int flags){. a
20fc2 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20 ssert( p==0 );.
20fc3 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e return 0;.}.#en
20fc4 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 dif.../*.** Add
20fc5 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f a new element to
20fc6 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e 20 65 the end of an e
20fc7 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 xpression list.
20fc8 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 If pList is.**
20fc9 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 initially NULL,
20fca 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 then create a ne
20fcb 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 w expression lis
20fcc 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 t..**.** If a me
20fcd 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
20fce 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
20fcf 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20 69 73 e entire list is
20fd0 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a 20 4e 55 freed and.** NU
20fd1 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 LL is returned.
20fd2 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20 If non-NULL is
20fd3 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 69 returned, then i
20fd4 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a t is guaranteed.
20fd5 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 ** that the new
20fd6 65 6e 74 72 79 20 77 61 73 20 73 75 63 63 65 73 entry was succes
20fd7 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 65 64 2e sfully appended.
20fd8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20fd9 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c TE ExprList *sql
20fda 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 ite3ExprListAppe
20fdb 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 nd(. Parse *pPa
20fdc 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a rse, /*
20fdd 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
20fde 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
20fdf 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a pList, /*
20fe0 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 List to which t
20fe1 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 o append. Might
20fe2 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 be NULL */. Exp
20fe3 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
20fe4 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
20fe5 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65 on to be appende
20fe6 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c d. Might be NULL
20fe7 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
20fe8 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
20fe9 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d b;. if( pList==
20fea 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 0 ){. pList =
20feb 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20fec 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
20fed 45 78 70 72 4c 69 73 74 29 20 29 3b 0a 20 20 20 ExprList) );.
20fee 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b if( pList==0 ){
20fef 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d . goto no_m
20ff0 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 em;. }. as
20ff1 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 41 6c sert( pList->nAl
20ff2 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 loc==0 );. }.
20ff3 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f if( pList->nAllo
20ff4 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 c<=pList->nExpr
20ff5 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 ){. struct Ex
20ff6 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a prList_item *a;.
20ff7 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 int n = pLis
20ff8 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b t->nAlloc*2 + 4;
20ff9 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 . a = sqlite3
20ffa 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c DbRealloc(db, pL
20ffb 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 ist->a, n*sizeof
20ffc 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a (pList->a[0]));.
20ffd 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a if( a==0 ){.
20ffe 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 goto no_me
20fff 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 m;. }. pLi
21000 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70 st->a = a;. p
21001 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 List->nAlloc = s
21002 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
21003 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a 65 6f 66 ze(db, a)/sizeof
21004 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 (a[0]);. }. as
21005 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d sert( pList->a!=
21006 30 20 29 3b 0a 20 20 69 66 28 20 31 20 29 7b 0a 0 );. if( 1 ){.
21007 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
21008 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 ist_item *pItem
21009 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 = &pList->a[pLis
2100a 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 t->nExpr++];.
2100b 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c 20 30 memset(pItem, 0
2100c 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 6d 29 , sizeof(*pItem)
2100d 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 );. pItem->pE
2100e 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 7d xpr = pExpr;. }
2100f 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b . return pList;
21010 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 ..no_mem: .
21011 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e /* Avoid leakin
21012 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c g memory if mall
21013 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 2a oc has failed. *
21014 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 /. sqlite3ExprD
21015 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29 elete(db, pExpr)
21016 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c ;. sqlite3ExprL
21017 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c istDelete(db, pL
21018 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 ist);. return 0
21019 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
2101a 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e he ExprList.a[].
2101b 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74 20 6f 66 zName element of
2101c 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
2101d 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a ly added item.**
2101e 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 on the expressi
2101f 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 on list..**.** p
21020 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55 List might be NU
21021 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 LL following an
21022 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 OOM error. But
21023 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 6e 65 76 pName should nev
21024 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 er be.** NULL.
21025 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
21026 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 cation fails, th
21027 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 e pParse->db->ma
21028 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a llocFailed flag.
21029 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 ** is set..*/.SQ
2102a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
2102b 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
2102c 74 53 65 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 tSetName(. Pars
2102d 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
2102e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
2102f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
21030 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 List *pList,
21031 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 /* List to w
21032 68 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20 hich to add the
21033 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e span. */. Token
21034 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *pName,
21035 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f 20 62 65 /* Name to be
21036 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 added */. int
21037 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 dequote
21038 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 /* True to c
21039 61 75 73 65 20 74 68 65 20 6e 61 6d 65 20 74 6f ause the name to
2103a 20 62 65 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a be dequoted */.
2103b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 ){. assert( pLi
2103c 73 74 21 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d st!=0 || pParse-
2103d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
2103e 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c d!=0 );. if( pL
2103f 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ist ){. struc
21040 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
21041 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61 73 73 65 *pItem;. asse
21042 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 rt( pList->nExpr
21043 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 20 >0 );. pItem
21044 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 = &pList->a[pLis
21045 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 t->nExpr-1];.
21046 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e assert( pItem->
21047 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 zName==0 );.
21048 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 pItem->zName = s
21049 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
2104a 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d pParse->db, pNam
2104b 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b e->z, pName->n);
2104c 0a 20 20 20 20 69 66 28 20 64 65 71 75 6f 74 65 . if( dequote
2104d 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 && pItem->zName
2104e 20 29 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 ) sqlite3Dequot
2104f 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b e(pItem->zName);
21050 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
21051 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 t the ExprList.a
21052 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d 65 6e 74 [].zSpan element
21053 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
21054 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d ently added item
21055 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65 .** on the expre
21056 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a ssion list..**.*
21057 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65 * pList might be
21058 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 NULL following
21059 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 an OOM error. B
2105a 75 74 20 70 53 70 61 6e 20 73 68 6f 75 6c 64 20 ut pSpan should
2105b 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c never be.** NULL
2105c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 . If a memory a
2105d 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
2105e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d the pParse->db-
2105f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c >mallocFailed fl
21060 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f ag.** is set..*/
21061 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21062 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
21063 4c 69 73 74 53 65 74 53 70 61 6e 28 0a 20 20 50 ListSetSpan(. P
21064 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
21065 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
21066 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
21067 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
21068 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 /* List t
21069 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74 o which to add t
2106a 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 45 78 he span. */. Ex
2106b 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 20 20 20 prSpan *pSpan
2106c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 61 /* The spa
2106d 6e 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f n to be added */
2106e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
2106f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
21070 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 assert( pList!
21071 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 =0 || db->malloc
21072 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 Failed!=0 );. i
21073 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 f( pList ){.
21074 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
21075 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
21076 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e List->a[pList->n
21077 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73 Expr-1];. ass
21078 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 ert( pList->nExp
21079 72 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 r>0 );. asser
2107a 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 t( db->mallocFai
2107b 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d 3e 70 45 led || pItem->pE
2107c 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70 45 78 70 xpr==pSpan->pExp
2107d 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 r );. sqlite3
2107e 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d DbFree(db, pItem
2107f 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70 49 ->zSpan);. pI
21080 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c tem->zSpan = sql
21081 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 ite3DbStrNDup(db
21082 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e , (char*)pSpan->
21083 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20 zStart,.
21084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21085 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 (int
21086 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 )(pSpan->zEnd -
21087 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b pSpan->zStart));
21088 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
21089 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
2108a 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 list pEList cont
2108b 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 ains more than i
2108c 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a Limit elements,.
2108d 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f ** leave an erro
2108e 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 r message in pPa
2108f 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rse..*/.SQLITE_P
21090 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21091 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b te3ExprListCheck
21092 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 65 20 Length(. Parse
21093 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 72 4c *pParse,. ExprL
21094 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 ist *pEList,. c
21095 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 onst char *zObje
21096 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d ct.){. int mx =
21097 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 pParse->db->aLi
21098 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
21099 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 _COLUMN];. test
2109a 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 case( pEList &&
2109b 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d pEList->nExpr==m
2109c 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 x );. testcase(
2109d 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 pEList && pELis
2109e 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 t->nExpr==mx+1 )
2109f 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 ;. if( pEList &
210a0 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e & pEList->nExpr>
210a1 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 mx ){. sqlite
210a2 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
210a3 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 , "too many colu
210a4 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a mns in %s", zObj
210a5 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ect);. }.}../*.
210a6 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 ** Delete an ent
210a7 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c ire expression l
210a8 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ist..*/.SQLITE_P
210a9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
210aa 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
210ab 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 e(sqlite3 *db, E
210ac 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b xprList *pList){
210ad 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
210ae 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
210af 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 *pItem;. if( p
210b0 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e List==0 ) return
210b1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 ;. assert( pLis
210b2 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 t->a!=0 || (pLis
210b3 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 t->nExpr==0 && p
210b4 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 List->nAlloc==0)
210b5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c );. assert( pL
210b6 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 ist->nExpr<=pLis
210b7 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 t->nAlloc );. f
210b8 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e or(pItem=pList->
210b9 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d a, i=0; i<pList-
210ba 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 >nExpr; i++, pIt
210bb 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 em++){. sqlit
210bc 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
210bd 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a pItem->pExpr);.
210be 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
210bf 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 e(db, pItem->zNa
210c0 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 me);. sqlite3
210c1 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d DbFree(db, pItem
210c2 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d 0a 20 20 ->zSpan);. }.
210c3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
210c4 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 , pList->a);. s
210c5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
210c6 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pList);.}../*.*
210c7 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
210c8 20 61 72 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c are Walker call
210c9 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 backs. Walker.u
210ca 2e 70 69 20 69 73 20 61 20 70 6f 69 6e 74 65 72 .pi is a pointer
210cb 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 .** to an intege
210cc 72 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e r. These routin
210cd 65 73 20 61 72 65 20 63 68 65 63 6b 69 6e 67 20 es are checking
210ce 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f an expression to
210cf 20 73 65 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 see.** if it is
210d0 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 a constant. Se
210d1 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 t *Walker.u.pi t
210d2 6f 20 30 20 69 66 20 74 68 65 20 65 78 70 72 65 o 0 if the expre
210d3 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 ssion is.** not
210d4 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 constant..**.**
210d5 54 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 These callback r
210d6 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
210d7 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
210d8 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
210d9 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 ** sqlite3Ex
210da 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a prIsConstant().*
210db 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 * sqlite3Exp
210dc 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f rIsConstantNotJo
210dd 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 in().** sqli
210de 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
210df 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a tOrFunction().**
210e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 .*/.static int e
210e1 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e xprNodeIsConstan
210e2 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
210e3 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b r, Expr *pExpr){
210e4 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 .. /* If pWalke
210e5 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 65 6e r->u.i is 3 then
210e6 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65 any term of the
210e7 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
210e8 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a comes from. **
210e9 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 the ON or USING
210ea 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f clauses of a jo
210eb 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73 20 in disqualifies
210ec 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 the expression.
210ed 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 ** from being c
210ee 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 74 61 onsidered consta
210ef 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 nt. */. if( pWa
210f0 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 lker->u.i==3 &&
210f1 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
210f2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f ty(pExpr, EP_Fro
210f3 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 mJoin) ){. pW
210f4 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a alker->u.i = 0;.
210f5 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
210f6 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 bort;. }.. swi
210f7 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
210f8 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 {. /* Conside
210f9 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 r functions to b
210fa 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c e constant if al
210fb 6c 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74 l their argument
210fc 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 s are constant.
210fd 20 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 ** and pWalke
210fe 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 r->u.i==2 */.
210ff 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f case TK_FUNCTIO
21100 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 70 57 61 N:. if( pWa
21101 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 lker->u.i==2 ) r
21102 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f eturn 0;. /
21103 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a * Fall through *
21104 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 /. case TK_ID
21105 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f :. case TK_CO
21106 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 LUMN:. case T
21107 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a K_AGG_FUNCTION:.
21108 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
21109 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20 20 74 65 COLUMN:. te
2110a 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
2110b 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20 p==TK_ID );.
2110c 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
2110d 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e r->op==TK_COLUMN
2110e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2110f 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
21110 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 K_AGG_FUNCTION )
21111 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21112 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
21113 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 AGG_COLUMN );.
21114 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 pWalker->u.i
21115 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
21116 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
21117 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
21118 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
21119 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 ->op==TK_SELECT
2111a 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 ); /* selectNode
2111b 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 IsConstant will
2111c 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 disallow */.
2111d 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
2111e 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 r->op==TK_EXISTS
2111f 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 ); /* selectNod
21120 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c eIsConstant will
21121 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 disallow */.
21122 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f return WRC_Co
21123 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 ntinue;. }.}.st
21124 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e atic int selectN
21125 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 odeIsConstant(Wa
21126 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
21127 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b elect *NotUsed){
21128 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
21129 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
2112a 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 pWalker->u.i = 0
2112b 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ;. return WRC_A
2112c 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 bort;.}.static i
2112d 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74 28 45 nt exprIsConst(E
2112e 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 xpr *p, int init
2112f 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 Flag){. Walker
21130 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 w;. w.u.i = ini
21131 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 tFlag;. w.xExpr
21132 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e Callback = exprN
21133 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 odeIsConstant;.
21134 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 w.xSelectCallba
21135 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 ck = selectNodeI
21136 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c sConstant;. sql
21137 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c ite3WalkExpr(&w,
21138 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e p);. return w.
21139 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 u.i;.}../*.** Wa
2113a 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e lk an expression
2113b 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 tree. Return 1
2113c 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
2113d 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a on is constant.*
2113e 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20 69 6e * and 0 if it in
2113f 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 volves variables
21140 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c or function cal
21141 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ls..**.** For th
21142 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
21143 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 is function, a d
21144 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ouble-quoted str
21145 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a ing (ex: "abc").
21146 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
21147 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 a variable but
21148 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 a single-quoted
21149 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 string (ex: 'abc
2114a 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 ') is.** a const
2114b 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ant..*/.SQLITE_P
2114c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2114d 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
2114e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 (Expr *p){. ret
2114f 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 urn exprIsConst(
21150 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, 1);.}../*.**
21151 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 Walk an expressi
21152 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
21153 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73 1 if the expres
21154 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 sion is constant
21155 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f .** that does no
21156 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 originate from
21157 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 the ON or USING
21158 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 clauses of a joi
21159 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 n..** Return 0 i
2115a 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 f it involves va
2115b 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 riables or funct
2115c 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 ion calls or ter
2115d 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e ms from.** an ON
2115e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 or USING clause
2115f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21160 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
21161 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 xprIsConstantNot
21162 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 Join(Expr *p){.
21163 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f return exprIsCo
21164 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a nst(p, 3);.}../*
21165 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 .** Walk an expr
21166 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
21167 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 turn 1 if the ex
21168 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 pression is cons
21169 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e tant.** or a fun
2116a 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 ction call with
2116b 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e constant argumen
2116c 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e 64 20 ts. Return and
2116d 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 0 if there.** ar
2116e 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 73 2e e any variables.
2116f 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
21170 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
21171 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 function, a doub
21172 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 le-quoted string
21173 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 (ex: "abc").**
21174 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 is considered a
21175 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 variable but a s
21176 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ingle-quoted str
21177 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 ing (ex: 'abc')
21178 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 is.** a constant
21179 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
2117a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
2117b 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 xprIsConstantOrF
2117c 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 unction(Expr *p)
2117d 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 {. return exprI
2117e 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a sConst(p, 2);.}.
2117f 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 ./*.** If the ex
21180 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 pression p codes
21181 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 a constant inte
21182 67 65 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c ger that is smal
21183 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 l enough.** to f
21184 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69 it in a 32-bit i
21185 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31 nteger, return 1
21186 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c and put the val
21187 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ue of the intege
21188 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e r.** in *pValue.
21189 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 If the express
2118a 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e ion is not an in
2118b 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 20 69 teger or if it i
2118c 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 s too big.** to
2118d 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20 fit in a signed
2118e 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 32-bit integer,
2118f 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 return 0 and lea
21190 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 ve *pValue uncha
21191 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nged..*/.SQLITE_
21192 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21193 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
21194 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 (Expr *p, int *p
21195 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 Value){. int rc
21196 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 = 0;. if( p->f
21197 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c lags & EP_IntVal
21198 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 ue ){. *pValu
21199 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c 75 65 3b e = p->u.iValue;
2119a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
2119b 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e }. switch( p->
2119c 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
2119d 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 K_INTEGER: {.
2119e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 rc = sqlite3G
2119f 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a 54 6f etInt32(p->u.zTo
211a0 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 ken, pValue);.
211a1 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
211a2 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
211a3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
211a4 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 TK_UPLUS: {.
211a5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 rc = sqlite3E
211a6 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e xprIsInteger(p->
211a7 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a pLeft, pValue);.
211a8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
211a9 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
211aa 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 MINUS: {. i
211ab 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 nt v;. if(
211ac 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
211ad 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 eger(p->pLeft, &
211ae 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 v) ){. *p
211af 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 Value = -v;.
211b0 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 rc = 1;.
211b1 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
211b2 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
211b3 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 lt: break;. }.
211b4 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 61 if( rc ){. a
211b5 73 73 65 72 74 28 20 45 78 70 72 48 61 73 41 6e ssert( ExprHasAn
211b6 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f yProperty(p, EP_
211b7 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
211b8 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20 20 20 20 Only).
211b9 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 || (p->flag
211ba 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 s2 & EP2_Malloce
211bb 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 dToken)==0 );.
211bc 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 p->op = TK_INT
211bd 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 EGER;. p->fla
211be 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 gs |= EP_IntValu
211bf 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69 56 61 6c e;. p->u.iVal
211c0 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20 ue = *pValue;.
211c1 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
211c2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 46 ../*.** Return F
211c3 41 4c 53 45 20 69 66 20 74 68 65 72 65 20 69 73 ALSE if there is
211c4 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 no chance that
211c5 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 the expression c
211c6 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a an be NULL..**.*
211c7 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
211c8 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 4e 55 4c ion might be NUL
211c9 4c 20 6f 72 20 69 66 20 74 68 65 20 65 78 70 72 L or if the expr
211ca 65 73 73 69 6f 6e 20 69 73 20 74 6f 6f 20 63 6f ession is too co
211cb 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20 74 65 6c 6c mplex.** to tell
211cc 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 0a return TRUE. .
211cd 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
211ce 6e 65 20 69 73 20 75 73 65 64 20 61 73 20 61 6e ne is used as an
211cf 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 optimization, t
211d0 6f 20 73 6b 69 70 20 4f 50 5f 49 73 4e 75 6c 6c o skip OP_IsNull
211d1 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 77 68 65 6e opcodes.** when
211d2 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 61 20 we know that a
211d3 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 value cannot be
211d4 4e 55 4c 4c 2e 20 20 48 65 6e 63 65 2c 20 61 20 NULL. Hence, a
211d5 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 0a 2a false positive.*
211d6 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 54 52 55 * (returning TRU
211d7 45 20 77 68 65 6e 20 69 6e 20 66 61 63 74 20 74 E when in fact t
211d8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 he expression ca
211d9 6e 20 6e 65 76 65 72 20 62 65 20 4e 55 4c 4c 29 n never be NULL)
211da 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 61 20 73 might.** be a s
211db 6d 61 6c 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 mall performance
211dc 20 68 69 74 20 62 75 74 20 69 73 20 6f 74 68 65 hit but is othe
211dd 72 77 69 73 65 20 68 61 72 6d 6c 65 73 73 2e 20 rwise harmless.
211de 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a On the other.**
211df 20 68 61 6e 64 2c 20 61 20 66 61 6c 73 65 20 6e hand, a false n
211e0 65 67 61 74 69 76 65 20 28 72 65 74 75 72 6e 69 egative (returni
211e1 6e 67 20 46 41 4c 53 45 20 77 68 65 6e 20 74 68 ng FALSE when th
211e2 65 20 72 65 73 75 6c 74 20 63 6f 75 6c 64 20 62 e result could b
211e3 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77 69 6c 6c 20 e NULL).** will
211e4 6c 69 6b 65 6c 79 20 72 65 73 75 6c 74 20 69 6e likely result in
211e5 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 61 6e an incorrect an
211e6 73 77 65 72 2e 20 20 53 6f 20 77 68 65 6e 20 69 swer. So when i
211e7 6e 20 64 6f 75 62 74 2c 20 72 65 74 75 72 6e 0a n doubt, return.
211e8 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a 53 51 4c 49 ** TRUE..*/.SQLI
211e9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
211ea 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e qlite3ExprCanBeN
211eb 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 20 2a ull(const Expr *
211ec 70 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 77 p){. u8 op;. w
211ed 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f hile( p->op==TK_
211ee 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f 70 3d 3d UPLUS || p->op==
211ef 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 70 20 3d TK_UMINUS ){ p =
211f0 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a 20 20 6f p->pLeft; }. o
211f1 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 69 66 28 p = p->op;. if(
211f2 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 op==TK_REGISTER
211f3 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 32 3b 0a ) op = p->op2;.
211f4 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
211f5 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 case TK_INTE
211f6 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b GER:. case TK
211f7 5f 53 54 52 49 4e 47 3a 0a 20 20 20 20 63 61 73 _STRING:. cas
211f8 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 e TK_FLOAT:.
211f9 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 20 20 case TK_BLOB:.
211fa 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
211fb 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
211fc 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d return 1;. }.}
211fd 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
211fe 20 61 6e 20 4f 50 5f 49 73 4e 75 6c 6c 20 69 6e an OP_IsNull in
211ff 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 74 struction that t
21200 65 73 74 73 20 72 65 67 69 73 74 65 72 20 69 52 ests register iR
21201 65 67 20 61 6e 64 20 6a 75 6d 70 73 0a 2a 2a 20 eg and jumps.**
21202 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 44 65 73 to location iDes
21203 74 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 t if the value i
21204 6e 20 69 52 65 67 20 69 73 20 4e 55 4c 4c 2e 20 n iReg is NULL.
21205 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 69 52 The value in iR
21206 65 67 20 0a 2a 2a 20 77 61 73 20 63 6f 6d 70 75 eg .** was compu
21207 74 65 64 20 62 79 20 70 45 78 70 72 2e 20 20 49 ted by pExpr. I
21208 66 20 77 65 20 63 61 6e 20 6c 6f 6f 6b 20 61 74 f we can look at
21209 20 70 45 78 70 72 20 61 74 20 63 6f 6d 70 69 6c pExpr at compil
2120a 65 2d 74 69 6d 65 20 61 6e 64 0a 2a 2a 20 64 65 e-time and.** de
2120b 74 65 72 6d 69 6e 65 20 74 68 61 74 20 69 74 20 termine that it
2120c 63 61 6e 20 6e 65 76 65 72 20 67 65 6e 65 72 61 can never genera
2120d 74 65 20 61 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 te a NULL, then
2120e 74 68 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 6f 70 the OP_IsNull op
2120f 65 72 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 eration.** can b
21210 65 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51 e omitted..*/.SQ
21211 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21212 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
21213 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 0a 20 20 56 eIsNullJump(. V
21214 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 dbe *v,
21215 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 75 /* The VDBE u
21216 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
21217 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 45 78 70 n */. const Exp
21218 72 20 2a 70 45 78 70 72 2c 20 20 2f 2a 20 4f 6e r *pExpr, /* On
21219 6c 79 20 67 65 6e 65 72 61 74 65 20 4f 50 5f 49 ly generate OP_I
2121a 73 4e 75 6c 6c 20 69 66 20 74 68 69 73 20 65 78 sNull if this ex
2121b 70 72 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 2a pr can be NULL *
2121c 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20 /. int iReg,
2121d 20 20 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 /* Test
2121e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 69 the value in thi
2121f 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 4e s register for N
21220 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 ULL */. int iDe
21221 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 st /*
21222 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 Jump here if the
21223 20 76 61 6c 75 65 20 69 73 20 6e 75 6c 6c 20 2a value is null *
21224 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 /.){. if( sqlit
21225 65 33 45 78 70 72 43 61 6e 42 65 4e 75 6c 6c 28 e3ExprCanBeNull(
21226 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 73 71 pExpr) ){. sq
21227 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21228 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 v, OP_IsNull, iR
21229 65 67 2c 20 69 44 65 73 74 29 3b 0a 20 20 7d 0a eg, iDest);. }.
2122a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
2122b 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 TRUE if the give
2122c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 n expression is
2122d 61 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 63 68 a constant which
2122e 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 6e 63 would be.** unc
2122f 68 61 6e 67 65 64 20 62 79 20 4f 50 5f 41 66 66 hanged by OP_Aff
21230 69 6e 69 74 79 20 77 69 74 68 20 74 68 65 20 61 inity with the a
21231 66 66 69 6e 69 74 79 20 67 69 76 65 6e 20 69 6e ffinity given in
21232 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 the second.** a
21233 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
21234 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
21235 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
21236 20 69 66 20 74 68 65 20 4f 50 5f 41 66 66 69 6e if the OP_Affin
21237 69 74 79 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a ity operation.**
21238 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e can be omitted.
21239 20 20 57 68 65 6e 20 69 6e 20 64 6f 75 62 74 20 When in doubt
2123a 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 20 20 41 return FALSE. A
2123b 20 66 61 6c 73 65 20 6e 65 67 61 74 69 76 65 0a false negative.
2123c 2a 2a 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 ** is harmless.
2123d 20 41 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76 A false positiv
2123e 65 2c 20 68 6f 77 65 76 65 72 2c 20 63 61 6e 20 e, however, can
2123f 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 77 72 result in the wr
21240 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65 72 2e 0a 2a ong.** answer..*
21241 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21242 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
21243 4e 65 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 NeedsNoAffinityC
21244 68 61 6e 67 65 28 63 6f 6e 73 74 20 45 78 70 72 hange(const Expr
21245 20 2a 70 2c 20 63 68 61 72 20 61 66 66 29 7b 0a *p, char aff){.
21246 20 20 75 38 20 6f 70 3b 0a 20 20 69 66 28 20 61 u8 op;. if( a
21247 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
21248 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ONE ) return 1;.
21249 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d while( p->op==
2124a 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f TK_UPLUS || p->o
2124b 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 p==TK_UMINUS ){
2124c 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a p = p->pLeft; }.
2124d 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 op = p->op;.
2124e 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 if( op==TK_REGIS
2124f 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 TER ) op = p->op
21250 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 2;. switch( op
21251 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 ){. case TK_I
21252 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 NTEGER: {.
21253 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 return aff==SQLI
21254 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c TE_AFF_INTEGER |
21255 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 | aff==SQLITE_AF
21256 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d F_NUMERIC;. }
21257 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f . case TK_FLO
21258 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 AT: {. retu
21259 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 rn aff==SQLITE_A
2125a 46 46 5f 52 45 41 4c 20 7c 7c 20 61 66 66 3d 3d FF_REAL || aff==
2125b 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
2125c 49 43 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 IC;. }. ca
2125d 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a se TK_STRING: {.
2125e 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 66 66 return aff
2125f 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 ==SQLITE_AFF_TEX
21260 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 T;. }. cas
21261 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 e TK_BLOB: {.
21262 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
21263 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 }. case TK_C
21264 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 61 OLUMN: {. a
21265 73 73 65 72 74 28 20 70 2d 3e 69 54 61 62 6c 65 ssert( p->iTable
21266 3e 3d 30 20 29 3b 20 20 2f 2a 20 70 20 63 61 6e >=0 ); /* p can
21267 6e 6f 74 20 62 65 20 70 61 72 74 20 6f 66 20 61 not be part of a
21268 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e CHECK constrain
21269 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 t */. retur
2126a 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 0a 20 n p->iColumn<0.
2126b 20 20 20 20 20 20 20 20 20 26 26 20 28 61 66 66 && (aff
2126c 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 ==SQLITE_AFF_INT
2126d 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c EGER || aff==SQL
2126e 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 ITE_AFF_NUMERIC)
2126f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
21270 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 ult: {. ret
21271 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
21272 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
21273 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 TRUE if the giv
21274 65 6e 20 73 74 72 69 6e 67 20 69 73 20 61 20 72 en string is a r
21275 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d ow-id column nam
21276 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
21277 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21278 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68 IsRowid(const ch
21279 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71 ar *z){. if( sq
2127a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 lite3StrICmp(z,
2127b 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20 "_ROWID_")==0 )
2127c 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
2127d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
2127e 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20 , "ROWID")==0 )
2127f 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
21280 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
21281 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65 , "OID")==0 ) re
21282 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e turn 1;. return
21283 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 0;.}../*.** Ret
21284 75 72 6e 20 74 72 75 65 20 69 66 20 77 65 20 61 urn true if we a
21285 72 65 20 61 62 6c 65 20 74 6f 20 74 68 65 20 49 re able to the I
21286 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70 74 69 6d N operator optim
21287 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a 2a 2a 20 ization on a.**
21288 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f 72 query of the for
21289 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 m.**.** x
2128a 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a IN (SELECT ...).
2128b 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74 68 65 20 **.** Where the
2128c 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61 75 73 65 SELECT... clause
2128d 20 69 73 20 61 73 20 73 70 65 63 69 66 69 65 64 is as specified
2128e 20 62 79 20 74 68 65 20 70 61 72 61 6d 65 74 65 by the paramete
2128f 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20 72 6f 75 r to this.** rou
21290 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tine..**.** The
21291 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 70 61 Select object pa
21292 73 73 65 64 20 69 6e 20 68 61 73 20 61 6c 72 65 ssed in has alre
21293 61 64 79 20 62 65 65 6e 20 70 72 65 70 72 6f 63 ady been preproc
21294 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 essed and no.**
21295 65 72 72 6f 72 73 20 68 61 76 65 20 62 65 65 6e errors have been
21296 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69 66 6e 64 found..*/.#ifnd
21297 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
21298 55 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 UBQUERY.static i
21299 6e 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f nt isCandidateFo
2129a 72 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 rInOpt(Select *p
2129b 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 ){. SrcList *pS
2129c 72 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a rc;. ExprList *
2129d 70 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 pEList;. Table
2129e 2a 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d *pTab;. if( p==
2129f 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 0 ) return 0;
212a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
212a1 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 /* right-hand si
212a2 64 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 de of IN is SELE
212a3 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 CT */. if( p->p
212a4 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 Prior ) return 0
212a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
212a6 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 * Not a compound
212a7 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 SELECT */. if(
212a8 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 p->selFlags & (
212a9 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 SF_Distinct|SF_A
212aa 67 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 ggregate) ){.
212ab 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73 testcase( (p->s
212ac 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 elFlags & (SF_Di
212ad 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 stinct|SF_Aggreg
212ae 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e ate))==SF_Distin
212af 63 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 ct );. testca
212b0 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 se( (p->selFlags
212b1 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c & (SF_Distinct|
212b2 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d SF_Aggregate))==
212b3 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 3b 0a SF_Aggregate );.
212b4 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a return 0; /*
212b5 20 4e 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79 No DISTINCT key
212b6 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 word and no aggr
212b7 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
212b8 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 */. }. assert(
212b9 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 p->pGroupBy==0
212ba 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
212bb 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 /* Has no GROUP
212bc 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 BY clause */. i
212bd 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 f( p->pLimit ) r
212be 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
212bf 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 /* Has no
212c0 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a LIMIT clause */.
212c1 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66 assert( p->pOf
212c2 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 fset==0 );
212c3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 4c /* No L
212c4 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f 20 4f 46 IMIT means no OF
212c5 46 53 45 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d FSET */. if( p-
212c6 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e >pWhere ) return
212c7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
212c8 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45 /* Has no WHERE
212c9 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 clause */. pSr
212ca 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 c = p->pSrc;. a
212cb 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 ssert( pSrc!=0 )
212cc 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 ;. if( pSrc->nS
212cd 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 rc!=1 ) return 0
212ce 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 ; /* Si
212cf 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f ngle term in FRO
212d0 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 M clause */. if
212d1 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 ( pSrc->a[0].pSe
212d2 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b lect ) return 0;
212d3 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 69 73 20 /* FROM is
212d4 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 20 6f not a subquery o
212d5 72 20 76 69 65 77 20 2a 2f 0a 20 20 70 54 61 62 r view */. pTab
212d6 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 = pSrc->a[0].pT
212d7 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 ab;. if( NEVER(
212d8 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72 pTab==0) ) retur
212d9 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n 0;. assert( p
212da 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 Tab->pSelect==0
212db 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ); /*
212dc 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 FROM clause is
212dd 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 not a view */.
212de 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 if( IsVirtual(pT
212df 61 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ab) ) return 0;
212e0 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 /* FROM c
212e1 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 69 72 74 lause not a virt
212e2 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 70 ual table */. p
212e3 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 EList = p->pELis
212e4 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d t;. if( pEList-
212e5 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 >nExpr!=1 ) retu
212e6 72 6e 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f rn 0; /* O
212e7 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 ne column in the
212e8 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
212e9 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 if( pEList->a[0
212ea 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ].pExpr->op!=TK_
212eb 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 COLUMN ) return
212ec 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 69 73 20 0; /* Result is
212ed 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65 a column */. re
212ee 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
212ef 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
212f0 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a SUBQUERY */../*.
212f1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
212f2 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
212f3 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
212f4 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e 29 20 6f f the IN (...) o
212f5 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 74 27 73 perator..** It's
212f6 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 6e 64 20 job is to find
212f7 6f 72 20 63 72 65 61 74 65 20 61 20 62 2d 74 72 or create a b-tr
212f8 65 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ee structure tha
212f9 74 20 6d 61 79 20 62 65 20 75 73 65 64 0a 2a 2a t may be used.**
212fa 20 65 69 74 68 65 72 20 74 6f 20 74 65 73 74 20 either to test
212fb 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 6f for membership o
212fc 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 20 f the (...) set
212fd 6f 72 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 or to iterate th
212fe 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 6d 65 6d rough.** its mem
212ff 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 64 bers, skipping d
21300 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a uplicates..**.**
21301 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 The index of th
21302 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 e cursor opened
21303 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 28 64 on the b-tree (d
21304 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64 atabase table, d
21305 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a atabase index .*
21306 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61 * or ephermal ta
21307 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20 69 ble) is stored i
21308 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 n pX->iTable bef
21309 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
2130a 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 n returns..** Th
2130b 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 e returned value
2130c 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
2130d 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 n indicates the
2130e 62 2d 74 72 65 65 20 74 79 70 65 2c 20 61 73 20 b-tree type, as
2130f 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
21310 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 IN_INDEX_ROWID
21311 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 - The cursor was
21312 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 opened on a dat
21313 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 abase table..**
21314 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 IN_INDEX_INDEX
21315 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 - The cursor wa
21316 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 s opened on a da
21317 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a tabase index..**
21318 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 IN_INDEX_EPH
21319 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 - The cursor w
2131a 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 as opened on a s
2131b 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 pecially created
2131c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
2131d 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c popul
2131e 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 ated epheremal t
2131f 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 able..**.** An e
21320 78 69 73 74 69 6e 67 20 62 2d 74 72 65 65 20 6d xisting b-tree m
21321 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 ay only be used
21322 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 if the SELECT is
21323 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a of the simple.*
21324 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 * form:.**.**
21325 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e SELECT <column
21326 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a > FROM <table>.*
21327 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f *.** If the prNo
21328 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 tFound parameter
21329 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 is 0, then the
2132a 62 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 b-tree will be u
2132b 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a 2a sed to iterate.*
2132c 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 65 * through the se
2132d 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 t members, skipp
2132e 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 74 ing any duplicat
2132f 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 es. In this case
21330 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c an.** epheremal
21331 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75 table must be u
21332 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 73 sed unless the s
21333 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e elected <column>
21334 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a is guaranteed.*
21335 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 2d * to be unique -
21336 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 either because
21337 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 it is an INTEGER
21338 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 PRIMARY KEY or
21339 69 74 0a 2a 2a 20 68 61 73 20 61 20 55 4e 49 51 it.** has a UNIQ
2133a 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 UE constraint or
2133b 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 0a 2a UNIQUE index..*
2133c 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f *.** If the prNo
2133d 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 tFound parameter
2133e 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 is not 0, then
2133f 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 the b-tree will
21340 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 be used .** for
21341 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73 fast set members
21342 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68 hip tests. In th
21343 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 65 72 is case an epher
21344 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 emal table must
21345 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65 .** be used unle
21346 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61 ss <column> is a
21347 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 n INTEGER PRIMAR
21348 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65 Y KEY or an inde
21349 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75 x can .** be fou
2134a 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e nd with <column>
2134b 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73 as its left-mos
2134c 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 t column..**.**
2134d 57 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 When the b-tree
2134e 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f is being used fo
2134f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 r membership tes
21350 74 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ts, the calling
21351 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 function.** need
21352 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 s to know whethe
21353 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72 r or not the str
21354 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
21355 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 an SQL NULL .**
21356 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 value in order t
21357 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c o correctly eval
21358 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 uate expressions
21359 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 like "X IN (Y,
2135a 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 Z)"..** If there
2135b 20 69 73 20 61 6e 79 20 63 68 61 6e 63 65 20 74 is any chance t
2135c 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 20 6d 69 hat the (...) mi
2135d 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 ght contain a NU
2135e 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 LL value at.** r
2135f 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 untime, then a r
21360 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 egister is alloc
21361 61 74 65 64 20 61 6e 64 20 74 68 65 20 72 65 67 ated and the reg
21362 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69 ister number wri
21363 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f tten.** to *prNo
21364 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65 tFound. If there
21365 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 is no chance th
21366 61 74 20 74 68 65 20 28 2e 2e 2e 29 20 63 6f 6e at the (...) con
21367 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20 tains a.** NULL
21368 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e value, then *prN
21369 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 20 otFound is left
2136a 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
2136b 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 69 If a register i
2136c 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
2136d 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f its location sto
2136e 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75 red in *prNotFou
2136f 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20 nd, then.** its
21370 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 73 initial value is
21371 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 28 NULL. If the (
21372 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f 74 20 72 65 ...) does not re
21373 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a main constant.**
21374 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f for the duratio
21375 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28 n of the query (
21376 69 2e 65 2e 20 74 68 65 20 53 45 4c 45 43 54 20 i.e. the SELECT
21377 77 69 74 68 69 6e 20 74 68 65 20 28 2e 2e 2e 29 within the (...)
21378 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72 65 6c 61 .** is a correla
21379 74 65 64 20 73 75 62 71 75 65 72 79 29 20 74 68 ted subquery) th
2137a 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 en the value of
2137b 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 the allocated re
2137c 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 72 65 73 gister is.** res
2137d 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 et to NULL each
2137e 74 69 6d 65 20 74 68 65 20 73 75 62 71 75 65 72 time the subquer
2137f 79 20 69 73 20 72 65 72 75 6e 2e 20 54 68 69 73 y is rerun. This
21380 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a 20 63 allows the.** c
21381 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64 62 aller to use vdb
21382 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c 65 6e e code equivalen
21383 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 t to the followi
21384 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 ng:.**.** if(
21385 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 register==NULL )
21386 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c {.** has_nul
21387 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64 61 74 l = <test if dat
21388 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 a structure cont
21389 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 ains null>.**
2138a 20 20 72 65 67 69 73 74 65 72 20 3d 20 31 0a 2a register = 1.*
2138b 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f * }.**.** in o
2138c 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 72 75 rder to avoid ru
2138d 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73 74 20 nning the <test
2138e 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75 72 if data structur
2138f 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e e contains null>
21390 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20 6f 66 .** test more of
21391 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65 63 65 ten than is nece
21392 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ssary..*/.#ifnde
21393 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 f SQLITE_OMIT_SU
21394 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 BQUERY.SQLITE_PR
21395 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21396 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 3FindInIndex(Par
21397 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
21398 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 *pX, int *prNot
21399 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74 Found){. Select
2139a 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
2139b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2139c 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74 68 65 /* SELECT to the
2139d 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f 70 65 right of IN ope
2139e 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65 rator */. int e
2139f 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 Type = 0;
213a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213a1 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48 53 20 /* Type of RHS
213a2 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 5f table. IN_INDEX_
213a3 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 * */. int iTab
213a4 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
213a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
213a6 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20 52 48 Cursor of the RH
213a7 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 S table */. int
213a8 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20 mustBeUnique =
213a9 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 3b (prNotFound==0);
213aa 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52 48 /* True if RH
213ab 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 S must be unique
213ac 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
213ad 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a X->op==TK_IN );.
213ae 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 . /* Check to s
213af 65 65 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e ee if an existin
213b0 67 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 g table or index
213b1 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a can be used to.
213b2 20 20 2a 2a 20 73 61 74 69 73 66 79 20 74 68 65 ** satisfy the
213b3 20 71 75 65 72 79 2e 20 20 54 68 69 73 20 69 73 query. This is
213b4 20 70 72 65 66 65 72 61 62 6c 65 20 74 6f 20 67 preferable to g
213b5 65 6e 65 72 61 74 69 6e 67 20 61 20 6e 65 77 20 enerating a new
213b6 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72 61 6c 20 . ** ephemeral
213b7 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 table.. */. p
213b8 3d 20 28 45 78 70 72 48 61 73 50 72 6f 70 65 72 = (ExprHasProper
213b9 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c ty(pX, EP_xIsSel
213ba 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65 ect) ? pX->x.pSe
213bb 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 lect : 0);. if(
213bc 20 41 4c 57 41 59 53 28 70 50 61 72 73 65 2d 3e ALWAYS(pParse->
213bd 6e 45 72 72 3d 3d 30 29 20 26 26 20 69 73 43 61 nErr==0) && isCa
213be 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 ndidateForInOpt(
213bf 70 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 p) ){. sqlite
213c0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
213c1 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 db;
213c2 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e /* Database con
213c3 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 nection */. E
213c4 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e xpr *pExpr = p->
213c5 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
213c6 70 72 3b 20 20 20 2f 2a 20 45 78 70 72 65 73 73 pr; /* Express
213c7 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a ion <column> */.
213c8 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 int iCol = p
213c9 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 Expr->iColumn;
213ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
213cb 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63 dex of column <c
213cc 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64 olumn> */. Vd
213cd 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
213ce 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 20 etVdbe(pParse);
213cf 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 /* Virtual
213d0 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f machine being co
213d1 64 65 64 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 ded */. Table
213d2 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 *pTab = p->pSrc
213d3 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20 ->a[0].pTab;
213d4 20 20 2f 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c /* Table <tabl
213d5 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 e>. */. int i
213d6 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
213d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213d8 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 64 /* Database id
213d9 78 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 x for pTab */.
213da 20 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e . /* Code an
213db 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 OP_VerifyCookie
213dc 20 61 6e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 and OP_TableLoc
213dd 6b 20 66 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a k for <table>. *
213de 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 /. iDb = sqli
213df 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
213e0 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 (db, pTab->pSche
213e1 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ma);. sqlite3
213e2 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 CodeVerifySchema
213e3 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 (pParse, iDb);.
213e4 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c sqlite3TableL
213e5 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c ock(pParse, iDb,
213e6 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 pTab->tnum, 0,
213e7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 pTab->zName);..
213e8 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 /* This funct
213e9 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ion is only call
213ea 65 64 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 ed from two plac
213eb 65 73 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65 es. In both case
213ec 73 20 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a s the vdbe. *
213ed 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
213ee 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f en allocated. So
213ef 20 61 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47 assume sqlite3G
213f0 65 74 56 64 62 65 28 29 20 69 73 20 61 6c 77 61 etVdbe() is alwa
213f1 79 73 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73 ys. ** succes
213f2 73 66 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a sful here.. *
213f3 2f 0a 20 20 20 20 61 73 73 65 72 74 28 76 29 3b /. assert(v);
213f4 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 . if( iCol<0
213f5 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 ){. int iMe
213f6 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d m = ++pParse->nM
213f7 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41 em;. int iA
213f8 64 64 72 3b 0a 0a 20 20 20 20 20 20 69 41 64 64 ddr;.. iAdd
213f9 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
213fa 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 ddOp1(v, OP_If,
213fb 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c iMem);. sql
213fc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
213fd 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c , OP_Integer, 1,
213fe 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73 iMem);.. s
213ff 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 qlite3OpenTable(
21400 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44 pParse, iTab, iD
21401 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e b, pTab, OP_Open
21402 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54 79 Read);. eTy
21403 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f pe = IN_INDEX_RO
21404 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 WID;.. sqli
21405 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
21406 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 7d v, iAddr);. }
21407 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 else{. Inde
21408 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 x *pIdx;
21409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2140a 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
2140b 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 iable */..
2140c 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e /* The collation
2140d 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 62 sequence used b
2140e 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e y the comparison
2140f 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20 69 73 . If an index is
21410 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 to. ** be
21411 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 used in place of
21412 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 a temp-table, i
21413 74 20 6d 75 73 74 20 62 65 20 6f 72 64 65 72 65 t must be ordere
21414 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 d according.
21415 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c ** to this col
21416 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e lation sequence.
21417 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 */. CollS
21418 65 71 20 2a 70 52 65 71 20 3d 20 73 71 6c 69 74 eq *pReq = sqlit
21419 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 e3BinaryCompareC
2141a 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
2141b 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 29 X->pLeft, pExpr)
2141c 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 ;.. /* Chec
2141d 6b 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e k that the affin
2141e 69 74 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ity that will be
2141f 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d used to perform
21420 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 the . ** c
21421 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 68 65 omparison is the
21422 20 73 61 6d 65 20 61 73 20 74 68 65 20 61 66 66 same as the aff
21423 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c inity of the col
21424 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a umn. If. **
21425 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 74 20 69 it is not, it i
21426 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
21427 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 65 78 2e o use any index.
21428 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
21429 63 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 char aff = compa
2142a 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 58 risonAffinity(pX
2142b 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 );. int aff
2142c 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62 inity_ok = (pTab
2142d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 ->aCol[iCol].aff
2142e 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d inity==aff||aff=
2142f 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 =SQLITE_AFF_NONE
21430 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 );.. for(pI
21431 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
21432 20 70 49 64 78 20 26 26 20 65 54 79 70 65 3d 3d pIdx && eType==
21433 30 20 26 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 0 && affinity_ok
21434 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
21435 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 xt){. if(
21436 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e (pIdx->aiColumn
21437 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 [0]==iCol).
21438 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 && sqlite3Fi
21439 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e ndCollSeq(db, EN
2143a 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 C(db), pIdx->azC
2143b 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d 70 52 65 71 oll[0], 0)==pReq
2143c 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 6d . && (!m
2143d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 ustBeUnique || (
2143e 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 pIdx->nColumn==1
2143f 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f && pIdx->onErro
21440 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 r!=OE_None)).
21441 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
21442 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 int iMem = ++p
21443 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
21444 20 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 int iAddr
21445 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 ;. char
21446 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20 *pKey;. .
21447 20 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68 61 pKey = (cha
21448 72 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65 78 r *)sqlite3Index
21449 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 Keyinfo(pParse,
2144a 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 pIdx);.
2144b 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 iAddr = sqlite3
2144c 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
2144d 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 _If, iMem);.
2144e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2144f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21450 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b teger, 1, iMem);
21451 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 . . sq
21452 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
21453 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 v, OP_OpenRead,
21454 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d iTab, pIdx->tnum
21455 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 , iDb,.
21456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21457 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45 pKey,P4_KE
21458 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a YINFO_HANDOFF);.
21459 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43 6f VdbeCo
2145a 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 mment((v, "%s",
2145b 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 pIdx->zName));.
2145c 20 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d eType =
2145d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b IN_INDEX_INDEX;
2145e 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 .. sqli
2145f 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
21460 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 20 v, iAddr);.
21461 20 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f if( prNotFo
21462 75 6e 64 20 26 26 20 21 70 54 61 62 2d 3e 61 43 und && !pTab->aC
21463 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c ol[iCol].notNull
21464 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
21465 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b *prNotFound = ++
21466 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
21467 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21468 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
21469 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 54 79 }. }.. if( eTy
2146a 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 pe==0 ){. /*
2146b 43 6f 75 6c 64 20 6e 6f 74 20 66 6f 75 6e 64 20 Could not found
2146c 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c an existing tabl
2146d 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 75 73 e or index to us
2146e 65 20 61 73 20 74 68 65 20 52 48 53 20 62 2d 74 e as the RHS b-t
2146f 72 65 65 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77 ree.. ** We w
21470 69 6c 6c 20 68 61 76 65 20 74 6f 20 67 65 6e 65 ill have to gene
21471 72 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72 61 rate an ephemera
21472 6c 20 74 61 62 6c 65 20 74 6f 20 64 6f 20 74 68 l table to do th
21473 65 20 6a 6f 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 e job.. */.
21474 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 int rMayHaveNu
21475 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 65 54 79 70 ll = 0;. eTyp
21476 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 e = IN_INDEX_EPH
21477 3b 0a 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 ;. if( prNotF
21478 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 ound ){. *p
21479 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79 rNotFound = rMay
2147a 48 61 76 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 HaveNull = ++pPa
2147b 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d rse->nMem;. }
2147c 65 6c 73 65 20 69 66 28 20 70 58 2d 3e 70 4c 65 else if( pX->pLe
2147d 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 ft->iColumn<0 &&
2147e 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 !ExprHasAnyProp
2147f 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 erty(pX, EP_xIsS
21480 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
21481 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 eType = IN_INDEX
21482 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 _ROWID;. }.
21483 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 sqlite3CodeSub
21484 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 select(pParse, p
21485 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c X, rMayHaveNull,
21486 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 eType==IN_INDEX
21487 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 _ROWID);. }else
21488 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 {. pX->iTable
21489 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 = iTab;. }. r
2148a 65 74 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 eturn eType;.}.#
2148b 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e endif../*.** Gen
2148c 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 erate code for s
2148d 63 61 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 calar subqueries
2148e 20 75 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 used as an expr
2148f 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e ession.** and IN
21490 20 6f 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 operators. Exa
21491 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 mples:.**.**
21492 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 (SELECT a FROM
21493 62 29 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 b) -- s
21494 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 ubquery.** E
21495 58 49 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 XISTS (SELECT a
21496 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 FROM b) -- EXI
21497 53 54 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 STS subquery.**
21498 20 20 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 x IN (4,5,11
21499 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d ) -
2149a 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 - IN operator wi
2149b 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 th list on right
2149c 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 -hand side.**
2149d 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 x IN (SELECT a
2149e 20 46 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 FROM b) --
2149f 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 IN operator with
214a0 20 73 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 subquery on the
214a1 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 right.**.** The
214a2 20 70 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 pExpr parameter
214a3 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 describes the e
214a4 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 xpression that c
214a5 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a ontains the IN.*
214a6 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 * operator or su
214a7 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 bquery..**.** If
214a8 20 70 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77 parameter isRow
214a9 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 id is non-zero,
214aa 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 then expression
214ab 70 45 78 70 72 20 69 73 20 67 75 61 72 61 6e 74 pExpr is guarant
214ac 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 eed.** to be of
214ad 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 the form "<rowid
214ae 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c > IN (?, ?, ?)",
214af 20 77 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 where <rowid> i
214b0 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a s a reference.**
214b1 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 to some integer
214b2 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 key column of a
214b3 20 74 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 table B-Tree. I
214b4 6e 20 74 68 69 73 20 63 61 73 65 2c 20 75 73 65 n this case, use
214b5 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d an.** intkey B-
214b6 54 72 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68 Tree to store th
214b7 65 20 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 e set of IN(...)
214b8 20 76 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20 values instead
214b9 6f 66 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 of the usual.**
214ba 28 73 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c (slower) variabl
214bb 65 20 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d e length keys B-
214bc 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 Tree..**.** If r
214bd 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 6e MayHaveNull is n
214be 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 on-zero, that me
214bf 61 6e 73 20 74 68 61 74 20 74 68 65 20 6f 70 65 ans that the ope
214c0 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 0a ration is an IN.
214c1 2a 2a 20 28 6e 6f 74 20 61 20 53 45 4c 45 43 54 ** (not a SELECT
214c2 20 6f 72 20 45 58 49 53 54 53 29 20 61 6e 64 20 or EXISTS) and
214c3 74 68 61 74 20 74 68 65 20 52 48 53 20 6d 69 67 that the RHS mig
214c4 68 74 20 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c ht contains NULL
214c5 73 2e 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 s..** Furthermor
214c6 65 2c 20 74 68 65 20 49 4e 20 69 73 20 69 6e 20 e, the IN is in
214c7 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61 a WHERE clause a
214c8 6e 64 20 74 68 61 74 20 77 65 20 72 65 61 6c 6c nd that we reall
214c9 79 20 77 61 6e 74 0a 2a 2a 20 74 6f 20 69 74 65 y want.** to ite
214ca 72 61 74 65 20 6f 76 65 72 20 74 68 65 20 52 48 rate over the RH
214cb 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 S of the IN oper
214cc 61 74 6f 72 20 69 6e 20 6f 72 64 65 72 20 74 6f ator in order to
214cd 20 71 75 69 63 6b 6c 79 20 6c 6f 63 61 74 65 0a quickly locate.
214ce 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 73 70 6f 6e ** all correspon
214cf 64 69 6e 67 20 4c 48 53 20 65 6c 65 6d 65 6e 74 ding LHS element
214d0 73 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 s. All this rou
214d1 74 69 6e 65 20 64 6f 65 73 20 69 73 20 69 6e 69 tine does is ini
214d2 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 72 tialize.** the r
214d3 65 67 69 73 74 65 72 20 67 69 76 65 6e 20 62 79 egister given by
214d4 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 74 6f rMayHaveNull to
214d5 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c 69 6e 67 20 NULL. Calling
214d6 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 74 61 routines will ta
214d7 6b 65 0a 2a 2a 20 63 61 72 65 20 6f 66 20 63 68 ke.** care of ch
214d8 61 6e 67 69 6e 67 20 74 68 69 73 20 72 65 67 69 anging this regi
214d9 73 74 65 72 20 76 61 6c 75 65 20 74 6f 20 6e 6f ster value to no
214da 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 65 20 52 48 n-NULL if the RH
214db 53 20 69 73 20 4e 55 4c 4c 2d 66 72 65 65 2e 0a S is NULL-free..
214dc 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 **.** If rMayHav
214dd 65 4e 75 6c 6c 20 69 73 20 7a 65 72 6f 2c 20 74 eNull is zero, t
214de 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 hat means that t
214df 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 62 he subquery is b
214e0 65 69 6e 67 20 75 73 65 64 0a 2a 2a 20 66 6f 72 eing used.** for
214e1 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 membership test
214e2 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65 72 65 ing only. There
214e3 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 is no need to i
214e4 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 0a 2a 2a nitialize any.**
214e5 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 69 6e registers to in
214e6 64 69 63 61 74 65 20 74 68 65 20 70 72 65 73 65 dicate the prese
214e7 6e 73 65 20 6f 72 20 61 62 73 65 6e 63 65 20 6f nse or absence o
214e8 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 68 65 20 52 f NULLs on the R
214e9 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 HS..**.** For a
214ea 53 45 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 SELECT or EXISTS
214eb 20 6f 70 65 72 61 74 6f 72 2c 20 72 65 74 75 72 operator, retur
214ec 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 74 n the register t
214ed 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a hat holds the.**
214ee 20 72 65 73 75 6c 74 2e 20 20 46 6f 72 20 49 4e result. For IN
214ef 20 6f 70 65 72 61 74 6f 72 73 20 6f 72 20 69 66 operators or if
214f0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
214f1 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
214f2 75 65 20 69 73 20 30 2e 0a 2a 2f 0a 23 69 66 6e ue is 0..*/.#ifn
214f3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
214f4 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f SUBQUERY.SQLITE_
214f5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
214f6 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 te3CodeSubselect
214f7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
214f8 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e, /* P
214f9 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
214fa 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c /. Expr *pExpr,
214fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
214fc 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f he IN, SELECT, o
214fd 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f r EXISTS operato
214fe 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48 r */. int rMayH
214ff 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f aveNull, /
21500 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20 * Register that
21501 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 20 records whether
21502 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20 52 NULLs exist in R
21503 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f HS */. int isRo
21504 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 wid
21505 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53 20 /* If true, LHS
21506 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 of IN operator i
21507 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a s a rowid */.){.
21508 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20 3d int testAddr =
21509 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
2150a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
2150b 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72 65 -time test addre
2150c 73 73 20 2a 2f 0a 20 20 69 6e 74 20 72 52 65 67 ss */. int rReg
2150d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
2150e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2150f 2f 2a 20 52 65 67 69 73 74 65 72 20 73 74 6f 72 /* Register stor
21510 69 6e 67 20 72 65 73 75 6c 74 69 6e 67 20 2a 2f ing resulting */
21511 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c . Vdbe *v = sql
21512 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
21513 73 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 se);. if( NEVER
21514 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 (v==0) ) return
21515 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 0;. sqlite3Expr
21516 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 CachePush(pParse
21517 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f );.. /* This co
21518 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69 de must be run i
21519 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 65 n its entirety e
2151a 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 20 very time it is
2151b 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a encountered. **
2151c 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 if any of the f
2151d 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 ollowing is true
2151e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a :. **. ** *
2151f 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 The right-hand
21520 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 65 side is a corre
21521 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a 20 lated subquery.
21522 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 ** * The ri
21523 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 ght-hand side is
21524 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c an expression l
21525 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 ist containing v
21526 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 ariables. **
21527 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64 * We are insid
21528 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a e a trigger. **
21529 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 . ** If all of
2152a 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 61 the above are fa
2152b 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e lse, then we can
2152c 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a run this code j
2152d 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 ust once. ** sa
2152e 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20 ve the results,
2152f 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 61 and reuse the sa
21530 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 me result on sub
21531 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 sequent invocati
21532 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ons.. */. if(
21533 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 !ExprHasAnyPrope
21534 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 rty(pExpr, EP_Va
21535 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 61 rSelect) && !pPa
21536 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 rse->pTriggerTab
21537 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 ){. int mem
21538 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
21539 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
2153a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 eAddOp1(v, OP_If
2153b 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 , mem);. test
2153c 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 Addr = sqlite3Vd
2153d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
2153e 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b nteger, 1, mem);
2153f 0a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 . assert( tes
21540 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 tAddr>0 || pPars
21541 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
21542 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 led );. }.. sw
21543 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 itch( pExpr->op
21544 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 ){. case TK_I
21545 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 N: {. char
21546 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 affinity;.
21547 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b KeyInfo keyInfo;
21548 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b . int addr;
21549 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
2154a 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 ss of OP_OpenEph
2154b 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 emeral instructi
2154c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 on */. Expr
2154d 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d *pLeft = pExpr-
2154e 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 >pLeft;.. i
2154f 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 f( rMayHaveNull
21550 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
21551 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21552 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 OP_Null, 0, rMay
21553 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 HaveNull);.
21554 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 }.. affini
21555 74 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ty = sqlite3Expr
21556 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b Affinity(pLeft);
21557 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 .. /* Wheth
21558 65 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 er this is an 'x
21559 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 IN(SELECT...)'
2155a 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 or an 'x IN(<exp
2155b 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a rlist>)'. *
2155c 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 * expression it
2155d 69 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 is handled the s
2155e 61 6d 65 20 77 61 79 2e 20 20 41 6e 20 65 70 68 ame way. An eph
2155f 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69 73 20 emeral table is
21560 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 . ** filled
21561 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 with single-fie
21562 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 ld index keys re
21563 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 presenting the r
21564 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 esults. **
21565 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 from the SELECT
21566 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 or the <exprlist
21567 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 >.. **.
21568 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 ** If the 'x'
21569 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
2156a 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 column value, or
2156b 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 the SELECT....
2156c 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e ** statemen
2156d 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 t returns a colu
2156e 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 mn value, then t
2156f 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
21570 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c hat. ** col
21571 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 umn is used to b
21572 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b uild the index k
21573 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 eys. If both 'x'
21574 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a and the. *
21575 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 * SELECT... stat
21576 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e ement are column
21577 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 s, then numeric
21578 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 affinity is used
21579 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 . ** if eit
2157a 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e her column has N
2157b 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 UMERIC or INTEGE
2157c 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e R affinity. If n
2157d 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 either. **
2157e 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 'x' nor the SELE
2157f 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 CT... statement
21580 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 are columns, the
21581 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 n numeric affini
21582 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 ty. ** is u
21583 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 sed.. */.
21584 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
21585 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 e = pParse->nTab
21586 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d ++;. addr =
21587 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21588 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 p2(v, OP_OpenEph
21589 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 emeral, pExpr->i
2158a 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29 Table, !isRowid)
2158b 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 ;. memset(&
2158c 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 keyInfo, 0, size
2158d 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 of(keyInfo));.
2158e 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 keyInfo.nFie
2158f 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 ld = 1;.. i
21590 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
21591 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
21592 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 Select) ){.
21593 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 /* Case 1:
21594 20 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 expr IN (SELEC
21595 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a T ...). *
21596 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e *. ** Gen
21597 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 erate code to wr
21598 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 ite the results
21599 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e of the select in
2159a 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 to the temporary
2159b 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c . ** tabl
2159c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 e allocated and
2159d 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20 opened above..
2159e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
2159f 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
215a0 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 ;. ExprLi
215a1 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20 st *pEList;..
215a2 20 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 assert( !is
215a3 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20 Rowid );.
215a4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
215a5 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 stInit(&dest, SR
215a6 54 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54 T_Set, pExpr->iT
215a7 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64 able);. d
215a8 65 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28 est.affinity = (
215a9 75 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 u8)affinity;.
215aa 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45 assert( (pE
215ab 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30 xpr->iTable&0x00
215ac 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e 00FFFF)==pExpr->
215ad 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 iTable );.
215ae 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c if( sqlite3Sel
215af 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 ect(pParse, pExp
215b0 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64 r->x.pSelect, &d
215b1 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 est) ){.
215b2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
215b3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 45 }. pE
215b4 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e List = pExpr->x.
215b5 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
215b6 0a 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 . if( ALW
215b7 41 59 53 28 70 45 4c 69 73 74 21 3d 30 20 26 26 AYS(pEList!=0 &&
215b8 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 pEList->nExpr>0
215b9 29 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 ) ){ .
215ba 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d keyInfo.aColl[0]
215bb 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 = sqlite3Binary
215bc 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 CompareCollSeq(p
215bd 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
215be 65 66 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 eft,.
215bf 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e pEList->a[0].
215c0 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 pExpr);.
215c1 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
215c2 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 ( pExpr->x.pList
215c3 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f !=0 ){. /
215c4 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 * Case 2: ex
215c5 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 pr IN (exprlist)
215c6 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
215c7 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 ** For each
215c8 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c expression, buil
215c9 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 d an index key f
215ca 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 rom the evaluati
215cb 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a on and. *
215cc 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 * store it in th
215cd 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c e temporary tabl
215ce 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 e. If <expr> is
215cf 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 a column, then u
215d0 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 se. ** th
215d1 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e at columns affin
215d2 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e ity when buildin
215d3 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 g index keys. If
215d4 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 <expr> is not.
215d5 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 ** a colu
215d6 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 mn, use numeric
215d7 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 affinity..
215d8 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
215d9 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 i;. Expr
215da 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 List *pList = pE
215db 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 xpr->x.pList;.
215dc 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 struct Exp
215dd 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
215de 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 m;. int r
215df 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 1, r2, r3;..
215e0 20 20 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 if( !affinit
215e1 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 y ){. a
215e2 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 ffinity = SQLITE
215e3 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 _AFF_NONE;.
215e4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 }. key
215e5 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 Info.aColl[0] =
215e6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
215e7 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 eq(pParse, pExpr
215e8 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 ->pLeft);..
215e9 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 /* Loop throu
215ea 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73 69 gh each expressi
215eb 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e on in <exprlist>
215ec 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 . */. r1
215ed 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
215ee 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 Reg(pParse);.
215ef 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 r2 = sqlite
215f0 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
215f1 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c se);. sql
215f2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
215f3 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 , OP_Null, 0, r2
215f4 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 );. for(i
215f5 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 =pList->nExpr, p
215f6 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 Item=pList->a; i
215f7 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b >0; i--, pItem++
215f8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 ){. Exp
215f9 72 20 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e r *pE2 = pItem->
215fa 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 pExpr;.
215fb 20 69 6e 74 20 69 56 61 6c 54 6f 49 6e 73 3b 0a int iValToIns;.
215fc 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 . /* If
215fd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
215fe 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 is not constant
215ff 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65 65 then we will nee
21600 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a d to. *
21601 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 * disable the te
21602 73 74 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 st that was gene
21603 72 61 74 65 64 20 61 62 6f 76 65 20 74 68 61 74 rated above that
21604 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20 20 20 makes sure.
21605 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f ** this co
21606 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74 65 73 de only executes
21607 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73 65 20 once. Because
21608 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74 61 for a non-consta
21609 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 nt. **
2160a 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20 6e 65 expression we ne
2160b 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68 69 73 ed to rerun this
2160c 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d 65 2e code each time.
2160d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 . */.
2160e 20 20 20 20 20 20 20 20 69 66 28 20 74 65 73 74 if( test
2160f 41 64 64 72 20 26 26 20 21 73 71 6c 69 74 65 33 Addr && !sqlite3
21610 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 ExprIsConstant(p
21611 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 E2) ){.
21612 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
21613 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 74 65 angeToNoop(v, te
21614 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a 20 20 stAddr-1, 2);.
21615 20 20 20 20 20 20 20 20 20 20 74 65 73 74 41 64 testAd
21616 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 dr = 0;.
21617 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f }.. /
21618 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 65 * Evaluate the e
21619 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 69 6e xpression and in
2161a 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74 68 65 sert it into the
2161b 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 temp table */.
2161c 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52 if( isR
2161d 6f 77 69 64 20 26 26 20 73 71 6c 69 74 65 33 45 owid && sqlite3E
2161e 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 32 xprIsInteger(pE2
2161f 2c 20 26 69 56 61 6c 54 6f 49 6e 73 29 20 29 7b , &iValToIns) ){
21620 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
21621 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
21622 2c 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 2c 20 , OP_InsertInt,
21623 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 pExpr->iTable, r
21624 32 2c 20 69 56 61 6c 54 6f 49 6e 73 29 3b 0a 20 2, iValToIns);.
21625 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
21626 20 20 20 20 20 20 20 20 20 20 20 20 72 33 20 3d r3 =
21627 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21628 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
21629 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 E2, r1);.
2162a 20 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 if( isRowid
2162b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2162c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2162d 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 Op2(v, OP_MustBe
2162e 49 6e 74 2c 20 72 33 2c 0a 20 20 20 20 20 20 20 Int, r3,.
2162f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21630 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21631 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
21632 76 29 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 v)+2);.
21633 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21634 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
21635 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 ert, pExpr->iTab
21636 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20 20 le, r2, r3);.
21637 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
21638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
21639 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
2163a 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 v, OP_MakeRecord
2163b 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 , r3, 1, r2, &af
2163c 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 finity, 1);.
2163d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
2163e 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
2163f 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
21640 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 r3, 1);.
21641 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21642 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
21643 64 78 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d dxInsert, pExpr-
21644 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20 >iTable, r2);.
21645 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
21646 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21647 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
21648 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
21649 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 pParse, r1);.
2164a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
2164b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
2164c 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a e, r2);. }.
2164d 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77 if( !isRow
2164e 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 id ){. sq
2164f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
21650 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64 4(v, addr, (void
21651 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f *)&keyInfo, P4_
21652 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 KEYINFO);.
21653 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
21654 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 }.. case T
21655 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 K_EXISTS:. ca
21656 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 se TK_SELECT:.
21657 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
21658 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 68 61 /* If this ha
21659 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72 s to be a scalar
2165a 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61 SELECT. Genera
2165b 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74 te code to put t
2165c 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 he. ** valu
2165d 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74 e of this select
2165e 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c in a memory cel
2165f 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65 l and record the
21660 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a number. **
21661 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 of the memory c
21662 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 20 ell in iColumn.
21663 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 45 If this is an E
21664 58 49 53 54 53 2c 20 77 72 69 74 65 0a 20 20 20 XISTS, write.
21665 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 ** an integer
21666 20 30 20 28 6e 6f 74 20 65 78 69 73 74 73 29 20 0 (not exists)
21667 6f 72 20 31 20 28 65 78 69 73 74 73 29 20 69 6e or 1 (exists) in
21668 74 6f 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c to a memory cell
21669 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 . ** and re
2166a 63 6f 72 64 20 74 68 61 74 20 6d 65 6d 6f 72 79 cord that memory
2166b 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e cell in iColumn
2166c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
2166d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f static const To
2166e 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 22 31 22 2c ken one = { "1",
2166f 20 31 20 7d 3b 20 20 2f 2a 20 54 6f 6b 65 6e 20 1 }; /* Token
21670 66 6f 72 20 6c 69 74 65 72 61 6c 20 76 61 6c 75 for literal valu
21671 65 20 31 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c e 1 */. Sel
21672 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20 20 20 ect *pSel;
21673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21674 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 /* SELECT sta
21675 74 65 6d 65 6e 74 20 74 6f 20 65 6e 63 6f 64 65 tement to encode
21676 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 */. Select
21677 44 65 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 Dest dest;
21678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21679 2f 2a 20 48 6f 77 20 74 6f 20 64 65 61 6c 20 77 /* How to deal w
2167a 69 74 68 20 53 45 4c 45 43 74 20 72 65 73 75 6c ith SELECt resul
2167b 74 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74 t */.. test
2167c 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
2167d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 =TK_EXISTS );.
2167e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
2167f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 xpr->op==TK_SELE
21680 43 54 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 CT );. asse
21681 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 rt( pExpr->op==T
21682 4b 5f 45 58 49 53 54 53 20 7c 7c 20 70 45 78 70 K_EXISTS || pExp
21683 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 r->op==TK_SELECT
21684 20 29 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 );.. asser
21685 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 t( ExprHasProper
21686 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
21687 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
21688 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e 78 pSel = pExpr->x
21689 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 .pSelect;.
2168a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
2168b 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20 tInit(&dest, 0,
2168c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b ++pParse->nMem);
2168d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 . if( pExpr
2168e 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 ->op==TK_SELECT
2168f 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e ){. dest.
21690 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b eDest = SRT_Mem;
21691 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21692 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21693 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69 _Null, 0, dest.i
21694 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 Parm);. V
21695 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
21696 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65 Init subquery re
21697 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d sult"));. }
21698 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65 else{. de
21699 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45 st.eDest = SRT_E
2169a 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 xists;. s
2169b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
2169c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
2169d 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 0, dest.iParm);.
2169e 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
2169f 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58 ent((v, "Init EX
216a0 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a ISTS result"));.
216a1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
216a2 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
216a3 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c pParse->db, pSel
216a4 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 ->pLimit);.
216a5 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 pSel->pLimit =
216a6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 sqlite3PExpr(pPa
216a7 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c rse, TK_INTEGER,
216a8 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20 0, 0, &one);.
216a9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
216aa 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 elect(pParse, pS
216ab 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 el, &dest) ){.
216ac 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
216ad 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 52 }. rR
216ae 65 67 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b eg = dest.iParm;
216af 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72 . ExprSetIr
216b0 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 reducible(pExpr)
216b1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
216b2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
216b3 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20 testAddr ){.
216b4 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
216b5 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 72 2d ere(v, testAddr-
216b6 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 1);. }. sqlite
216b7 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 3ExprCachePop(pP
216b8 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72 65 74 arse, 1);.. ret
216b9 75 72 6e 20 72 52 65 67 3b 0a 7d 0a 23 65 6e 64 urn rReg;.}.#end
216ba 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
216bb 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 23 T_SUBQUERY */..#
216bc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
216bd 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a IT_SUBQUERY./*.*
216be 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
216bf 66 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73 for an IN expres
216c0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 sion..**.**
216c1 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e x IN (SELECT ..
216c2 2e 29 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e 20 .).** x IN
216c3 28 76 61 6c 75 65 2c 20 76 61 6c 75 65 2c 20 2e (value, value, .
216c4 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 ..).**.** The le
216c5 66 74 2d 68 61 6e 64 20 73 69 64 65 20 28 4c 48 ft-hand side (LH
216c6 53 29 20 69 73 20 61 20 73 63 61 6c 61 72 20 65 S) is a scalar e
216c7 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20 xpression. The
216c8 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 right-hand side
216c9 28 52 48 53 29 0a 2a 2a 20 69 73 20 61 6e 20 61 (RHS).** is an a
216ca 72 72 61 79 20 6f 66 20 7a 65 72 6f 20 6f 72 20 rray of zero or
216cb 6d 6f 72 65 20 76 61 6c 75 65 73 2e 20 20 54 68 more values. Th
216cc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
216cd 74 72 75 65 20 69 66 20 74 68 65 20 4c 48 53 20 true if the LHS
216ce 69 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20 is.** contained
216cf 77 69 74 68 69 6e 20 74 68 65 20 52 48 53 2e 20 within the RHS.
216d0 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 The value of th
216d1 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
216d2 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c 4c 29 0a 2a unknown (NULL).*
216d3 2a 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20 * if the LHS is
216d4 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 4c NULL or if the L
216d5 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 HS is not contai
216d6 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 ned within the R
216d7 48 53 20 61 6e 64 20 74 68 65 0a 2a 2a 20 52 48 HS and the.** RH
216d8 53 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f S contains one o
216d9 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 r more NULL valu
216da 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 es..**.** This r
216db 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 outine generates
216dc 20 63 6f 64 65 20 77 69 6c 6c 20 6a 75 6d 70 20 code will jump
216dd 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 20 69 to destIfFalse i
216de 66 20 74 68 65 20 4c 48 53 20 69 73 20 6e 6f 74 f the LHS is not
216df 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77 .** contained w
216e0 69 74 68 69 6e 20 74 68 65 20 52 48 53 2e 20 20 ithin the RHS.
216e1 49 66 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20 If due to NULLs
216e2 77 65 20 63 61 6e 6e 6f 74 20 64 65 74 65 72 6d we cannot determ
216e3 69 6e 65 20 69 66 20 74 68 65 20 4c 48 53 0a 2a ine if the LHS.*
216e4 2a 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 * is contained i
216e5 6e 20 74 68 65 20 52 48 53 20 74 68 65 6e 20 6a n the RHS then j
216e6 75 6d 70 20 74 6f 20 64 65 73 74 49 66 4e 75 6c ump to destIfNul
216e7 6c 2e 20 20 49 66 20 74 68 65 20 4c 48 53 20 69 l. If the LHS i
216e8 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 77 s contained.** w
216e9 69 74 68 69 6e 20 74 68 65 20 52 48 53 20 74 68 ithin the RHS th
216ea 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e en fall through.
216eb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
216ec 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 sqlite3ExprCodeI
216ed 4e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 N(. Parse *pPar
216ee 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 se, /* Pa
216ef 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 rsing and code g
216f0 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 enerating contex
216f1 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 t */. Expr *pEx
216f2 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pr, /*
216f3 54 68 65 20 49 4e 20 65 78 70 72 65 73 73 69 6f The IN expressio
216f4 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49 n */. int destI
216f5 66 46 61 6c 73 65 2c 20 20 20 20 20 20 2f 2a 20 fFalse, /*
216f6 4a 75 6d 70 20 68 65 72 65 20 69 66 20 4c 48 53 Jump here if LHS
216f7 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 is not containe
216f8 64 20 69 6e 20 74 68 65 20 52 48 53 20 2a 2f 0a d in the RHS */.
216f9 20 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c int destIfNull
216fa 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
216fb 68 65 72 65 20 69 66 20 74 68 65 20 72 65 73 75 here if the resu
216fc 6c 74 73 20 61 72 65 20 75 6e 6b 6e 6f 77 6e 20 lts are unknown
216fd 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f 0a due to NULLs */.
216fe 29 7b 0a 20 20 69 6e 74 20 72 52 68 73 48 61 73 ){. int rRhsHas
216ff 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f 2a 20 52 65 Null = 0; /* Re
21700 67 69 73 74 65 72 20 74 68 61 74 20 69 73 20 74 gister that is t
21701 72 75 65 20 69 66 20 52 48 53 20 63 6f 6e 74 61 rue if RHS conta
21702 69 6e 73 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 ins NULL values
21703 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 */. char affini
21704 74 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ty; /* Co
21705 6d 70 61 72 69 73 6f 6e 20 61 66 66 69 6e 69 74 mparison affinit
21706 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e y to use */. in
21707 74 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 t eType;
21708 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 /* Type of t
21709 68 65 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 he RHS */. int
2170a 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r1;
2170b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75 /* Temporary u
2170c 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 se register */.
2170d 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
2170e 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d /* Statem
2170f 65 6e 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 ent under constr
21710 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 uction */.. /*
21711 43 6f 6d 70 75 74 65 20 74 68 65 20 52 48 53 2e Compute the RHS.
21712 20 20 20 41 66 74 65 72 20 74 68 69 73 20 73 74 After this st
21713 65 70 2c 20 74 68 65 20 74 61 62 6c 65 20 77 69 ep, the table wi
21714 74 68 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 70 th cursor. ** p
21715 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 77 69 6c Expr->iTable wil
21716 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 l contains the v
21717 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20 alues that make
21718 75 70 20 74 68 65 20 52 48 53 2e 0a 20 20 2a 2f up the RHS.. */
21719 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 . v = pParse->p
2171a 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Vdbe;. assert(
2171b 76 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a v!=0 ); /*
2171c 20 4f 4f 4d 20 64 65 74 65 63 74 65 64 20 70 72 OOM detected pr
2171d 69 6f 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74 ior to this rout
2171e 69 6e 65 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f ine */. VdbeNoo
2171f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 65 pComment((v, "be
21720 67 69 6e 20 49 4e 20 65 78 70 72 22 29 29 3b 0a gin IN expr"));.
21721 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 eType = sqlite
21722 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 3FindInIndex(pPa
21723 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 52 68 rse, pExpr, &rRh
21724 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a sHasNull);.. /*
21725 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 Figure out the
21726 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 affinity to use
21727 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 20 to create a key
21728 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 73 from the results
21729 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 . ** of the exp
2172a 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74 ression. affinit
2172b 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74 yStr stores a st
2172c 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74 atic string suit
2172d 61 62 6c 65 20 66 6f 72 0a 20 20 2a 2a 20 50 34 able for. ** P4
2172e 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 of OP_MakeRecor
2172f 64 2e 0a 20 20 2a 2f 0a 20 20 61 66 66 69 6e 69 d.. */. affini
21730 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 ty = comparisonA
21731 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a ffinity(pExpr);.
21732 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4c . /* Code the L
21733 48 53 2c 20 74 68 65 20 3c 65 78 70 72 3e 20 66 HS, the <expr> f
21734 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 rom "<expr> IN (
21735 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a 20 20 73 71 ...)".. */. sq
21736 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
21737 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 72 31 sh(pParse);. r1
21738 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
21739 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
2173a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
2173b 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
2173c 4c 65 66 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c Left, r1);. sql
2173d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2173e 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c , OP_IsNull, r1,
2173f 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 0a 0a destIfNull);...
21740 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f if( eType==IN_
21741 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 INDEX_ROWID ){.
21742 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 /* In this ca
21743 73 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 74 se, the RHS is t
21744 68 65 20 52 4f 57 49 44 20 6f 66 20 74 61 62 6c he ROWID of tabl
21745 65 20 62 2d 74 72 65 65 0a 20 20 20 20 2a 2f 0a e b-tree. */.
21746 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21747 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 ddOp2(v, OP_Must
21748 42 65 49 6e 74 2c 20 72 31 2c 20 64 65 73 74 49 BeInt, r1, destI
21749 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 73 71 6c fFalse);. sql
2174a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
2174b 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 , OP_NotExists,
2174c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64 pExpr->iTable, d
2174d 65 73 74 49 66 46 61 6c 73 65 2c 20 72 31 29 3b estIfFalse, r1);
2174e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
2174f 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 In this case, t
21750 68 65 20 52 48 53 20 69 73 20 61 6e 20 69 6e 64 he RHS is an ind
21751 65 78 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a ex b-tree.. *
21752 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
21753 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 66 eAddOp4(v, OP_Af
21754 66 69 6e 69 74 79 2c 20 72 31 2c 20 31 2c 20 30 finity, r1, 1, 0
21755 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b , &affinity, 1);
21756 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
21757 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 set membership t
21758 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 est fails, then
21759 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
2175a 65 20 0a 20 20 20 20 2a 2a 20 22 78 20 49 4e 20 e . ** "x IN
2175b 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69 6f (...)" expressio
2175c 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 n must be either
2175d 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 74 0 or NULL. If t
2175e 68 65 20 73 65 74 0a 20 20 20 20 2a 2a 20 63 6f he set. ** co
2175f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 ntains no NULL v
21760 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 alues, then the
21761 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20 result is 0. If
21762 74 68 65 20 73 65 74 20 0a 20 20 20 20 2a 2a 20 the set . **
21763 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 contains one or
21764 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73 more NULL values
21765 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
21766 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 t of the. **
21767 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c expression is al
21768 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a so NULL.. */.
21769 20 20 20 20 69 66 28 20 72 52 68 73 48 61 73 4e if( rRhsHasN
2176a 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65 73 74 49 66 ull==0 || destIf
2176b 46 61 6c 73 65 3d 3d 64 65 73 74 49 66 4e 75 6c False==destIfNul
2176c 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 l ){. /* Th
2176d 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 is branch runs i
2176e 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 f it is known at
2176f 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 compile time th
21770 61 74 20 74 68 65 20 52 48 53 0a 20 20 20 20 20 at the RHS.
21771 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61 ** cannot conta
21772 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 in NULL values.
21773 54 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 This happens as
21774 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 the result.
21775 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 ** of a "NOT NU
21776 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 LL" constraint i
21777 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 n the database s
21778 63 68 65 6d 61 2e 0a 20 20 20 20 20 20 2a 2a 0a chema.. **.
21779 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 20 72 75 ** Also ru
2177a 6e 20 74 68 69 73 20 62 72 61 6e 63 68 20 69 66 n this branch if
2177b 20 4e 55 4c 4c 20 69 73 20 65 71 75 69 76 61 6c NULL is equival
2177c 65 6e 74 20 74 6f 20 46 41 4c 53 45 0a 20 20 20 ent to FALSE.
2177d 20 20 20 2a 2a 20 66 6f 72 20 74 68 69 73 20 70 ** for this p
2177e 61 72 74 69 63 75 6c 61 72 20 49 4e 20 6f 70 65 articular IN ope
2177f 72 61 74 6f 72 2e 0a 20 20 20 20 20 20 2a 2f 0a rator.. */.
21780 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21781 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
21782 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70 45 78 70 72 _NotFound, pExpr
21783 2d 3e 69 54 61 62 6c 65 2c 20 64 65 73 74 49 66 ->iTable, destIf
21784 46 61 6c 73 65 2c 20 72 31 2c 20 31 29 3b 0a 0a False, r1, 1);..
21785 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21786 20 2f 2a 20 49 6e 20 74 68 69 73 20 62 72 61 6e /* In this bran
21787 63 68 2c 20 74 68 65 20 52 48 53 20 6f 66 20 74 ch, the RHS of t
21788 68 65 20 49 4e 20 6d 69 67 68 74 20 63 6f 6e 74 he IN might cont
21789 61 69 6e 20 61 20 4e 55 4c 4c 20 61 6e 64 0a 20 ain a NULL and.
2178a 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 73 ** the pres
2178b 65 6e 63 65 20 6f 66 20 61 20 4e 55 4c 4c 20 6f ence of a NULL o
2178c 6e 20 74 68 65 20 52 48 53 20 6d 61 6b 65 73 20 n the RHS makes
2178d 61 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20 a difference in
2178e 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74 the. ** out
2178f 63 6f 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 come.. */.
21790 20 20 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32 2c int j1, j2,
21791 20 6a 33 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 j3;.. /* F
21792 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65 irst check to se
21793 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20 e if the LHS is
21794 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 contained in the
21795 20 52 48 53 2e 20 20 49 66 20 73 6f 2c 0a 20 20 RHS. If so,.
21796 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ** then the
21797 70 72 65 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c presence of NULL
21798 73 20 69 6e 20 74 68 65 20 52 48 53 20 64 6f 65 s in the RHS doe
21799 73 20 6e 6f 74 20 6d 61 74 74 65 72 2c 20 73 6f s not matter, so
2179a 20 6a 75 6d 70 0a 20 20 20 20 20 20 2a 2a 20 6f jump. ** o
2179b 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 ver all of the c
2179c 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ode that follows
2179d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
2179e 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j1 = sqlite3Vdb
2179f 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
217a0 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 _Found, pExpr->i
217a1 54 61 62 6c 65 2c 20 30 2c 20 72 31 2c 20 31 29 Table, 0, r1, 1)
217a2 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 48 65 72 65 ;.. /* Here
217a3 20 77 65 20 62 65 67 69 6e 20 67 65 6e 65 72 61 we begin genera
217a4 74 69 6e 67 20 63 6f 64 65 20 74 68 61 74 20 72 ting code that r
217a5 75 6e 73 20 69 66 20 74 68 65 20 4c 48 53 20 69 uns if the LHS i
217a6 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 63 s not. ** c
217a7 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 ontained within
217a8 74 68 65 20 52 48 53 2e 20 20 47 65 6e 65 72 61 the RHS. Genera
217a9 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f te additional co
217aa 64 65 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a de that. **
217ab 20 74 65 73 74 73 20 74 68 65 20 52 48 53 20 66 tests the RHS f
217ac 6f 72 20 4e 55 4c 4c 73 2e 20 20 49 66 20 74 68 or NULLs. If th
217ad 65 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 61 e RHS contains a
217ae 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 20 20 20 NULL then.
217af 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 ** jump to dest
217b0 49 66 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 72 IfNull. If ther
217b1 65 20 61 72 65 20 6e 6f 20 4e 55 4c 4c 73 20 69 e are no NULLs i
217b2 6e 20 74 68 65 20 52 48 53 20 74 68 65 6e 0a 20 n the RHS then.
217b3 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 ** jump to
217b4 64 65 73 74 49 66 46 61 6c 73 65 2e 0a 20 20 20 destIfFalse..
217b5 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 32 20 3d */. j2 =
217b6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
217b7 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c p1(v, OP_NotNull
217b8 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a , rRhsHasNull);.
217b9 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 j3 = sqlit
217ba 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 e3VdbeAddOp4Int(
217bb 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 v, OP_Found, pEx
217bc 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 pr->iTable, 0, r
217bd 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 29 3b 0a RhsHasNull, 1);.
217be 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
217bf 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
217c0 74 65 67 65 72 2c 20 2d 31 2c 20 72 52 68 73 48 teger, -1, rRhsH
217c1 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 asNull);. s
217c2 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
217c3 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 re(v, j3);.
217c4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
217c5 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c p2(v, OP_AddImm,
217c6 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 29 rRhsHasNull, 1)
217c7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
217c8 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
217c9 32 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 2);.. /* Ju
217ca 6d 70 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 mp to the approp
217cb 72 69 61 74 65 20 74 61 72 67 65 74 20 64 65 70 riate target dep
217cc 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
217cd 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 20 20 2a r or not. *
217ce 2a 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69 * the RHS contai
217cf 6e 73 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 ns a NULL.
217d0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
217d1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
217d2 5f 49 66 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c _If, rRhsHasNull
217d3 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 , destIfNull);.
217d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
217d5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 AddOp2(v, OP_Got
217d6 6f 2c 20 30 2c 20 64 65 73 74 49 66 46 61 6c 73 o, 0, destIfFals
217d7 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 e);.. /* Th
217d8 65 20 4f 50 5f 46 6f 75 6e 64 20 61 74 20 74 68 e OP_Found at th
217d9 65 20 74 6f 70 20 6f 66 20 74 68 69 73 20 62 72 e top of this br
217da 61 6e 63 68 20 6a 75 6d 70 73 20 68 65 72 65 20 anch jumps here
217db 77 68 65 6e 20 74 72 75 65 2c 20 0a 20 20 20 20 when true, .
217dc 20 20 2a 2a 20 63 61 75 73 69 6e 67 20 74 68 65 ** causing the
217dd 20 6f 76 65 72 61 6c 6c 20 49 4e 20 65 78 70 72 overall IN expr
217de 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 69 6f ession evaluatio
217df 6e 20 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67 n to fall throug
217e0 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 h.. */.
217e1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
217e2 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 pHere(v, j1);.
217e3 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
217e4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
217e5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 73 pParse, r1);. s
217e6 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
217e7 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 op(pParse, 1);.
217e8 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
217e9 20 22 65 6e 64 20 49 4e 20 65 78 70 72 22 29 29 "end IN expr"))
217ea 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
217eb 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
217ec 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 RY */../*.** Dup
217ed 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 79 74 65 licate an 8-byte
217ee 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 74 69 63 value.*/.static
217ef 20 63 68 61 72 20 2a 64 75 70 38 62 79 74 65 73 char *dup8bytes
217f0 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 (Vdbe *v, const
217f1 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 63 68 61 char *in){. cha
217f2 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 74 65 33 r *out = sqlite3
217f3 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 DbMallocRaw(sqli
217f4 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 38 29 te3VdbeDb(v), 8)
217f5 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 ;. if( out ){.
217f6 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 2c 20 69 memcpy(out, i
217f7 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 72 65 74 n, 8);. }. ret
217f8 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn out;.}../*.*
217f9 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e * Generate an in
217fa 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 struction that w
217fb 69 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f 61 ill put the floa
217fc 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 ting point.** va
217fd 6c 75 65 20 64 65 73 63 72 69 62 65 64 20 62 79 lue described by
217fe 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 z[0..n-1] into
217ff 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a register iMem..*
21800 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 *.** The z[] str
21801 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c ing will probabl
21802 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 y not be zero-te
21803 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 rminated. But t
21804 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 he .** z[n] char
21805 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 acter is guarant
21806 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 eed to be someth
21807 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f ing that does no
21808 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 t look.** like t
21809 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 he continuation
2180a 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a of the number..*
2180b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
2180c 64 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c 20 deReal(Vdbe *v,
2180d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
2180e 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 nt negateFlag, i
2180f 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 nt iMem){. if(
21810 41 4c 57 41 59 53 28 7a 21 3d 30 29 20 29 7b 0a ALWAYS(z!=0) ){.
21811 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 double value
21812 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a ;. char *zV;.
21813 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 sqlite3AtoF(
21814 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 z, &value);.
21815 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 assert( !sqlite3
21816 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29 3b 20 IsNaN(value) );
21817 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f 46 20 /* The new AtoF
21818 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20 4e 61 never returns Na
21819 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 67 N */. if( neg
2181a 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65 20 ateFlag ) value
2181b 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 7a 56 = -value;. zV
2181c 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 = dup8bytes(v,
2181d 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a (char*)&value);.
2181e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2181f 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c ddOp4(v, OP_Real
21820 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 , 0, iMem, 0, zV
21821 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 7d 0a , P4_REAL);. }.
21822 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 }.../*.** Genera
21823 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f te an instructio
21824 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 n that will put
21825 74 68 65 20 69 6e 74 65 67 65 72 20 64 65 73 63 the integer desc
21826 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 78 74 20 ribe by.** text
21827 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 z[0..n-1] into r
21828 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a egister iMem..**
21829 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 69 .** The z[] stri
2182a 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 ng will probably
2182b 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 not be zero-ter
2182c 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 68 minated. But th
2182d 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 e .** z[n] chara
2182e 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 cter is guarante
2182f 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 ed to be somethi
21830 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ng that does not
21831 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 look.** like th
21832 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f e continuation o
21833 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f f the number..*/
21834 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 .static void cod
21835 65 49 6e 74 65 67 65 72 28 56 64 62 65 20 2a 76 eInteger(Vdbe *v
21836 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 , Expr *pExpr, i
21837 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 6e 74 20 nt negFlag, int
21838 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 45 78 iMem){. if( pEx
21839 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 pr->flags & EP_I
2183a 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 69 ntValue ){. i
2183b 6e 74 20 69 20 3d 20 70 45 78 70 72 2d 3e 75 2e nt i = pExpr->u.
2183c 69 56 61 6c 75 65 3b 0a 20 20 20 20 69 66 28 20 iValue;. if(
2183d 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d 69 negFlag ) i = -i
2183e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
2183f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21840 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b teger, i, iMem);
21841 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f . }else{. co
21842 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 45 nst char *z = pE
21843 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 xpr->u.zToken;.
21844 20 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 assert( z!=0
21845 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 );. if( sqlit
21846 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 7a e3FitsIn64Bits(z
21847 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 20 , negFlag) ){.
21848 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 i64 value;.
21849 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 char *zV;.
2184a 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 69 sqlite3Atoi
2184b 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 64(z, &value);.
2184c 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67 if( negFlag
2184d 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 ) value = -valu
2184e 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75 e;. zV = du
2184f 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72 p8bytes(v, (char
21850 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 *)&value);.
21851 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21852 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 p4(v, OP_Int64,
21853 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 0, iMem, 0, zV,
21854 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 7d P4_INT64);. }
21855 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 65 else{. code
21856 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65 67 46 6c Real(v, z, negFl
21857 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d ag, iMem);. }
21858 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c . }.}../*.** Cl
21859 65 61 72 20 61 20 63 61 63 68 65 20 65 6e 74 72 ear a cache entr
2185a 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
2185b 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 d cacheEntryClea
2185c 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c r(Parse *pParse,
2185d 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
2185e 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e e *p){. if( p->
2185f 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20 20 69 tempReg ){. i
21860 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 f( pParse->nTemp
21861 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50 Reg<ArraySize(pP
21862 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20 arse->aTempReg)
21863 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d ){. pParse-
21864 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65 >aTempReg[pParse
21865 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 ->nTempReg++] =
21866 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 p->iReg;. }.
21867 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 p->tempReg =
21868 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 0;. }.}.../*.**
21869 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20 63 Record in the c
2186a 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 61 74 olumn cache that
2186b 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f a particular co
2186c 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 lumn from a.** p
2186d 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 articular table
2186e 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 70 is stored in a p
2186f 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 articular regist
21870 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
21871 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21872 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 e3ExprCacheStore
21873 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21874 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20 69 43 int iTab, int iC
21875 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 ol, int iReg){.
21876 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 69 int i;. int mi
21877 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64 78 4c nLru;. int idxL
21878 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f ru;. struct yCo
21879 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 61 73 lCache *p;.. as
2187a 73 65 72 74 28 20 69 52 65 67 3e 30 20 29 3b 20 sert( iReg>0 );
2187b 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6e 75 6d /* Register num
2187c 62 65 72 73 20 61 72 65 20 61 6c 77 61 79 73 20 bers are always
2187d 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61 73 positive */. as
2187e 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d 31 20 26 sert( iCol>=-1 &
2187f 26 20 69 43 6f 6c 3c 33 32 37 36 38 20 29 3b 20 & iCol<32768 );
21880 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f 6c 75 6d /* Finite colum
21881 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 0a 20 20 n numbers */..
21882 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63 65 /* First replace
21883 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65 6e any existing en
21884 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 try */. for(i=0
21885 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
21886 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
21887 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
21888 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
21889 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54 61 ->iReg && p->iTa
2188a 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d 3e ble==iTab && p->
2188b 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b iColumn==iCol ){
2188c 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 . cacheEntr
2188d 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 yClear(pParse, p
2188e 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 );. p->iLev
2188f 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 el = pParse->iCa
21890 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 cheLevel;.
21891 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a p->iReg = iReg;.
21892 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e p->affChan
21893 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d ge = 0;. p-
21894 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 >lru = pParse->i
21895 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 CacheCnt++;.
21896 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
21897 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61 }.. /* Find a
21898 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e 64 n empty slot and
21899 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a 20 replace it */.
2189a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
2189b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
2189c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
2189d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
2189e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d if( p->iReg==
2189f 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 0 ){. p->iL
218a0 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 evel = pParse->i
218a1 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 CacheLevel;.
218a2 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 p->iTable = iT
218a3 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f ab;. p->iCo
218a4 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 lumn = iCol;.
218a5 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 p->iReg = iRe
218a6 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43 g;. p->affC
218a7 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 hange = 0;.
218a8 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b p->tempReg = 0;
218a9 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 . p->lru =
218aa 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e pParse->iCacheCn
218ab 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 t++;. retur
218ac 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 n;. }. }..
218ad 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 6c /* Replace the l
218ae 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65 ast recently use
218af 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d 20 d */. minLru =
218b0 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69 64 0x7fffffff;. id
218b1 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f 72 xLru = -1;. for
218b2 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
218b3 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
218b4 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
218b5 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
218b6 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75 f( p->lru<minLru
218b7 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72 75 ){. idxLru
218b8 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e 4c = i;. minL
218b9 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20 20 ru = p->lru;.
218ba 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 41 4c 57 }. }. if( ALW
218bb 41 59 53 28 69 64 78 4c 72 75 3e 3d 30 29 20 29 AYS(idxLru>=0) )
218bc 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 {. p = &pPars
218bd 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 78 e->aColCache[idx
218be 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c 65 Lru];. p->iLe
218bf 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 vel = pParse->iC
218c0 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 70 acheLevel;. p
218c1 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b ->iTable = iTab;
218c2 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 . p->iColumn
218c3 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 = iCol;. p->i
218c4 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 Reg = iReg;.
218c5 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 p->affChange = 0
218c6 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 ;. p->tempReg
218c7 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 75 = 0;. p->lru
218c8 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
218c9 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 75 eCnt++;. retu
218ca 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rn;. }.}../*.**
218cb 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 61 Indicate that a
218cc 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65 69 register is bei
218cd 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
218ce 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69 73 Purge the regis
218cf 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ter.** from the
218d0 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f column cache..*/
218d1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
218d2 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
218d3 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73 CacheRemove(Pars
218d4 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
218d5 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 Reg){. int i;.
218d6 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
218d7 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c e *p;. for(i=0,
218d8 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
218d9 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
218da 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
218db 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
218dc 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 >iReg==iReg ){.
218dd 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 cacheEntryC
218de 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b lear(pParse, p);
218df 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d . p->iReg =
218e0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
218e1 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 ./*.** Remember
218e2 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c 75 the current colu
218e3 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78 74 mn cache context
218e4 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72 69 . Any new entri
218e5 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64 65 es added.** adde
218e6 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 d to the column
218e7 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 cache after this
218e8 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 65 call are remove
218e9 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63 6f d when the.** co
218ea 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 20 rresponding pop
218eb 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 occurs..*/.SQLIT
218ec 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
218ed 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
218ee 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 ush(Parse *pPars
218ef 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 e){. pParse->iC
218f0 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a acheLevel++;.}..
218f1 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72 6f /*.** Remove fro
218f2 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 m the column cac
218f3 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74 he any entries t
218f4 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20 73 hat were added s
218f5 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 ince the.** the
218f6 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68 20 previous N Push
218f7 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e 20 operations. In
218f8 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 73 other words, res
218f9 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a 2a tore the cache.*
218fa 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69 * to the state i
218fb 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68 65 t was in N Pushe
218fc 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 s ago..*/.SQLITE
218fd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
218fe 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
218ff 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c p(Parse *pParse,
21900 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20 69 int N){. int i
21901 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
21902 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 ache *p;. asser
21903 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73 65 t( N>0 );. asse
21904 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 rt( pParse->iCac
21905 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20 heLevel>=N );.
21906 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
21907 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28 vel -= N;. for(
21908 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
21909 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
2190a 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
2190b 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
2190c 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e ( p->iReg && p->
2190d 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e 69 iLevel>pParse->i
2190e 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20 20 CacheLevel ){.
2190f 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c cacheEntryCl
21910 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a ear(pParse, p);.
21911 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 p->iReg =
21912 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
21913 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 63 /*.** When a cac
21914 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 hed column is re
21915 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 used, make sure
21916 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74 65 that its registe
21917 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 r is.** no longe
21918 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 61 r available as a
21919 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e 20 temp register.
2191a 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20 20 ticket #3879:
2191b 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65 67 that same.** reg
2191c 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20 69 ister might be i
2191d 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 6d n the cache in m
2191e 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c 20 ultiple places,
2191f 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a 2a so be sure to.**
21920 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a get them all..*
21921 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
21922 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69 lite3ExprCachePi
21923 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65 20 nRegister(Parse
21924 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 *pParse, int iRe
21925 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 g){. int i;. s
21926 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
21927 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 *p;. for(i=0, p
21928 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
21929 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
2192a 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
2192b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 +){. if( p->i
2192c 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 Reg==iReg ){.
2192d 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 p->tempReg =
2192e 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
2192f 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
21930 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78 ode that will ex
21931 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d tract the iColum
21932 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d n-th column from
21933 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61 .** table pTab a
21934 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c nd store the col
21935 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72 umn value in a r
21936 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66 egister. An eff
21937 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 ort.** is made t
21938 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 o store the colu
21939 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 mn value in regi
2193a 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74 ster iReg, but t
2193b 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 his is.** not gu
2193c 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c aranteed. The l
2193d 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 ocation of the c
2193e 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 olumn value is r
2193f 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
21940 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 here must be an
21941 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 open cursor to p
21942 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 Tab in iTable wh
21943 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
21944 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 ** is called. I
21945 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e f iColumn<0 then
21946 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 code is generat
21947 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73 ed that extracts
21948 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a the rowid..**.*
21949 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
2194a 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20 ight attempt to
2194b 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 reuse the value
2194c 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 of the column th
2194d 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 at.** has alread
2194e 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e y been loaded in
2194f 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 to a register.
21950 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 The value will a
21951 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 lways.** be used
21952 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75 if it has not u
21953 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 ndergone any aff
21954 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20 inity changes.
21955 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 But if.** an aff
21956 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 inity change has
21957 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 occurred, then
21958 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65 the cached value
21959 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a will only be.**
2195a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 used if allowAf
2195b 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a fChng is true..*
2195c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2195d 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
2195e 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 CodeGetColumn(.
2195f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
21960 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 /* Parsing and
21961 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
21962 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 context */. Ta
21963 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f ble *pTab, /
21964 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 * Description of
21965 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 the table we ar
21966 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a e reading from *
21967 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c /. int iColumn,
21968 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
21969 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d the table colum
2196a 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c n */. int iTabl
2196b 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 e, /* The c
2196c 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
2196d 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 o the table */.
2196e 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20 int iReg,
2196f 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c /* Store resul
21970 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ts here */. int
21971 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a allowAffChng /*
21972 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61 True if prior a
21973 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20 ffinity changes
21974 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 are OK */.){. V
21975 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
21976 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b >pVdbe;. int i;
21977 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 . struct yColCa
21978 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 che *p;.. for(i
21979 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
2197a 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
2197b 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
2197c 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, p++){. if(
2197d 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70 2d p->iReg>0 && p-
2197e 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 >iTable==iTable
2197f 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 && p->iColumn==i
21980 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 Column.
21981 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61 && (!p->affCha
21982 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 nge || allowAffC
21983 68 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 70 2d hng) ){. p-
21984 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 >lru = pParse->i
21985 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 CacheCnt++;.
21986 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21987 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 70 50 hePinRegister(pP
21988 61 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b 0a arse, p->iReg);.
21989 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e return p->
2198a 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 iReg;. }. }
2198b 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 . assert( v!=0
2198c 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d );. if( iColum
2198d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 n<0 ){. sqlit
2198e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2198f 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c 65 OP_Rowid, iTable
21990 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 , iReg);. }else
21991 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 if( ALWAYS(pTab
21992 21 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 !=0) ){. int
21993 6f 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70 op = IsVirtual(p
21994 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d Tab) ? OP_VColum
21995 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 n : OP_Column;.
21996 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21997 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 dOp3(v, op, iTab
21998 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 le, iColumn, iRe
21999 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 g);. sqlite3C
2199a 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 olumnDefault(v,
2199b 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 pTab, iColumn, i
2199c 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Reg);. }. sqli
2199d 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 te3ExprCacheStor
2199e 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 6c 65 e(pParse, iTable
2199f 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 , iColumn, iReg)
219a0 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b ;. return iReg;
219a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
219a2 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 all column cache
219a3 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c entries..*/.SQL
219a4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
219a5 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
219a6 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 eClear(Parse *pP
219a7 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a arse){. int i;.
219a8 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
219a9 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d he *p;.. for(i=
219aa 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 0, p=pParse->aCo
219ab 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 lCache; i<SQLITE
219ac 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b _N_COLCACHE; i++
219ad 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 , p++){. if(
219ae 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20 20 20 p->iReg ){.
219af 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 cacheEntryClear
219b0 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 (pParse, p);.
219b1 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a p->iReg = 0;.
219b2 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
219b3 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61 ** Record the fa
219b4 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 69 6e ct that an affin
219b5 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f ity change has o
219b6 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f 75 6e ccurred on iCoun
219b7 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 73 t.** registers s
219b8 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 53 74 tarting with iSt
219b9 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 art..*/.SQLITE_P
219ba 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
219bb 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
219bc 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73 65 nityChange(Parse
219bd 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 53 *pParse, int iS
219be 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 6e 74 tart, int iCount
219bf 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 ){. int iEnd =
219c0 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e 74 20 iStart + iCount
219c1 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 - 1;. int i;.
219c2 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
219c3 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 *p;. for(i=0,
219c4 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
219c5 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
219c6 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
219c7 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d ++){. int r =
219c8 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 p->iReg;. if
219c9 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26 20 72 ( r>=iStart && r
219ca 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 <=iEnd ){.
219cb 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 31 p->affChange = 1
219cc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
219cd 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
219ce 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 de to move conte
219cf 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 nt from register
219d0 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b s iFrom...iFrom+
219d1 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 nReg-1.** over t
219d2 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d o iTo..iTo+nReg-
219d3 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f 6c 75 1. Keep the colu
219d4 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f 2d 64 mn cache up-to-d
219d5 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ate..*/.SQLITE_P
219d6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
219d7 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 te3ExprCodeMove(
219d8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
219d9 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 nt iFrom, int iT
219da 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 o, int nReg){.
219db 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
219dc 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 yColCache *p;.
219dd 69 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d if( NEVER(iFrom=
219de 3d 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a =iTo) ) return;.
219df 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
219e0 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62 Op3(pParse->pVdb
219e1 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f e, OP_Move, iFro
219e2 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 m, iTo, nReg);.
219e3 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
219e4 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
219e5 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
219e6 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
219e7 20 20 20 69 6e 74 20 78 20 3d 20 70 2d 3e 69 52 int x = p->iR
219e8 65 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d 69 eg;. if( x>=i
219e9 46 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d 2b From && x<iFrom+
219ea 6e 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 2d nReg ){. p-
219eb 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46 72 >iReg += iTo-iFr
219ec 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a om;. }. }.}.
219ed 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
219ee 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f 6e code to copy con
219ef 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 tent from regist
219f0 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f ers iFrom...iFro
219f1 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 m+nReg-1.** over
219f2 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 to iTo..iTo+nRe
219f3 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 g-1..*/.SQLITE_P
219f4 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
219f5 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 te3ExprCodeCopy(
219f6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
219f7 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 nt iFrom, int iT
219f8 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 o, int nReg){.
219f9 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 4e 45 56 int i;. if( NEV
219fa 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 ER(iFrom==iTo) )
219fb 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 return;. for(i
219fc 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 =0; i<nReg; i++)
219fd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
219fe 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e eAddOp2(pParse->
219ff 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 pVdbe, OP_Copy,
21a00 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b iFrom+i, iTo+i);
21a01 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
21a02 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79 turn true if any
21a03 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 register in the
21a04 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54 range iFrom..iT
21a05 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a o (inclusive).**
21a06 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 is used as part
21a07 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 of the column c
21a08 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
21a09 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e int usedAsColumn
21a0a 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 Cache(Parse *pPa
21a0b 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
21a0c 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20 int iTo){. int
21a0d 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
21a0e 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 Cache *p;. for(
21a0f 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
21a10 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
21a11 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
21a12 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e ++, p++){. in
21a13 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 t r = p->iReg;.
21a14 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d 20 if( r>=iFrom
21a15 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 75 && r<=iTo ) retu
21a16 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn 1;. }. retu
21a17 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 rn 0;.}../*.** I
21a18 66 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 f the last instr
21a19 75 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73 20 uction coded is
21a1a 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f 70 an ephemeral cop
21a1b 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 y of any of.** t
21a1c 68 65 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 he registers in
21a1d 74 68 65 20 6e 52 65 67 20 72 65 67 69 73 74 65 the nReg registe
21a1e 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 rs beginning wit
21a1f 68 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 h iReg, then.**
21a20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73 74 convert the last
21a21 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72 6f instruction fro
21a22 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50 m OP_SCopy to OP
21a23 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 _Copy..*/.SQLITE
21a24 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21a25 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 lite3ExprHardCop
21a26 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c y(Parse *pParse,
21a27 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e int iReg, int n
21a28 52 65 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a Reg){. VdbeOp *
21a29 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a pOp;. Vdbe *v;.
21a2a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
21a2b 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
21a2c 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76 20 3d 20 led==0 );. v =
21a2d 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
21a2e 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b assert( v!=0 );
21a2f 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 . pOp = sqlite3
21a30 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 VdbeGetOp(v, -1)
21a31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 21 ;. assert( pOp!
21a32 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d =0 );. if( pOp-
21a33 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 >opcode==OP_SCop
21a34 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 y && pOp->p1>=iR
21a35 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52 eg && pOp->p1<iR
21a36 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 70 eg+nReg ){. p
21a37 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f Op->opcode = OP_
21a38 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a Copy;. }.}../*.
21a39 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
21a3a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61 to store the va
21a3b 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69 61 lue of the iAlia
21a3c 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72 65 s-th alias in re
21a3d 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65 74 gister.** target
21a3e 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d . The first tim
21a3f 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 e this is called
21a40 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c 75 , pExpr is evalu
21a41 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a ated to compute.
21a42 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
21a43 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65 20 the alias. The
21a44 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 value is stored
21a45 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79 20 in an auxiliary
21a46 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 register.** and
21a47 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 the number of th
21a48 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20 72 at register is r
21a49 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 eturned. On sub
21a4a 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a sequent calls,.*
21a4b 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e * the register n
21a4c 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e 65 umber is returne
21a4d 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72 61 d without genera
21a4e 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a ting any code..*
21a4f 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 *.** Note that i
21a50 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73 n order for this
21a51 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d to work, code m
21a52 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65 64 ust be generated
21a53 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 in the.** same
21a54 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69 73 order that it is
21a55 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a executed..**.**
21a56 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75 6d Aliases are num
21a57 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77 bered starting w
21a58 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69 61 ith 1. So iAlia
21a59 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e 67 s is in the rang
21a5a 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61 e.** of 1 to pPa
21a5b 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c rse->nAlias incl
21a5c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 usive. .**.** p
21a5d 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 Parse->aAlias[iA
21a5e 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73 20 lias-1] records
21a5f 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d the register num
21a60 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76 61 ber where the va
21a61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41 lue.** of the iA
21a62 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 73 lias-th alias is
21a63 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65 72 stored. If zer
21a64 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 o, that means th
21a65 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 at the.** alias
21a66 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
21a67 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74 computed..*/.st
21a68 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 69 atic int codeAli
21a69 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 as(Parse *pParse
21a6a 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45 78 , int iAlias, Ex
21a6b 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 pr *pExpr, int t
21a6c 61 72 67 65 74 29 7b 0a 23 69 66 20 30 0a 20 20 arget){.#if 0.
21a6d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
21a6e 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
21a6f 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72 iReg;. if( pPar
21a70 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c se->nAliasAlloc<
21a71 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 pParse->nAlias )
21a72 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 {. pParse->aA
21a73 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 lias = sqlite3Db
21a74 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 ReallocOrFree(db
21a75 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 , pParse->aAlias
21a76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21a77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21a78 20 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73 65 sizeof(pParse
21a79 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 ->aAlias[0])*pPa
21a7a 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 rse->nAlias );.
21a7b 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d testcase( db-
21a7c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 >mallocFailed &&
21a7d 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 pParse->nAliasA
21a7e 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 lloc>0 );. if
21a7f 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
21a80 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 ed ) return 0;.
21a81 20 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 memset(&pPars
21a82 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73 65 e->aAlias[pParse
21a83 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 ->nAliasAlloc],
21a84 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 0,. (p
21a85 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 Parse->nAlias-pP
21a86 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f arse->nAliasAllo
21a87 63 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 c)*sizeof(pParse
21a88 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 ->aAlias[0]));.
21a89 20 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 pParse->nAlia
21a8a 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d sAlloc = pParse-
21a8b 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 >nAlias;. }. a
21a8c 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30 20 ssert( iAlias>0
21a8d 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 && iAlias<=pPars
21a8e 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 e->nAlias );. i
21a8f 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 Reg = pParse->aA
21a90 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a lias[iAlias-1];.
21a91 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29 7b if( iReg==0 ){
21a92 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d . if( pParse-
21a93 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 30 20 29 >iCacheLevel>0 )
21a94 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 73 {. iReg = s
21a95 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21a96 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21a97 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 pr, target);.
21a98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 52 }else{. iR
21a99 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e eg = ++pParse->n
21a9a 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 Mem;. sqlit
21a9b 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
21a9c 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 3b e, pExpr, iReg);
21a9d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 . pParse->a
21a9e 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 Alias[iAlias-1]
21a9f 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 = iReg;. }.
21aa0 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b }. return iReg;
21aa1 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f .#else. UNUSED_
21aa2 50 41 52 41 4d 45 54 45 52 28 69 41 6c 69 61 73 PARAMETER(iAlias
21aa3 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 );. return sqli
21aa4 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
21aa5 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c t(pParse, pExpr,
21aa6 20 74 61 72 67 65 74 29 3b 0a 23 65 6e 64 69 66 target);.#endif
21aa7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
21aa8 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 te code into the
21aa9 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f current Vdbe to
21aaa 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 69 evaluate the gi
21aab 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f ven.** expressio
21aac 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 73 n. Attempt to s
21aad 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
21aae 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 61 in register "ta
21aaf 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e rget"..** Return
21ab0 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77 68 the register wh
21ab1 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 ere results are
21ab2 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 stored..**.** Wi
21ab3 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c th this routine,
21ab4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 61 there is no gua
21ab5 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 75 rantee that resu
21ab6 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73 lts will.** be s
21ab7 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 2e tored in target.
21ab8 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 67 The result mig
21ab9 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 ht be stored in
21aba 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65 some other.** re
21abb 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73 20 gister if it is
21abc 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f convenient to do
21abd 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e so. The callin
21abe 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 g function.** mu
21abf 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 st check the ret
21ac0 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76 urn code and mov
21ac1 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 6f e the results to
21ac2 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 the desired.**
21ac3 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c register..*/.SQL
21ac4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
21ac5 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21ac6 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 61 arget(Parse *pPa
21ac7 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
21ac8 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 , int target){.
21ac9 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 Vdbe *v = pPars
21aca 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 68 e->pVdbe; /* Th
21acb 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74 e VM under const
21acc 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 ruction */. int
21acd 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 op;
21ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 /* The op
21acf 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 64 code being coded
21ad0 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 20 */. int inReg
21ad1 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 = target;
21ad2 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 65 /* Results store
21ad3 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 6e d in register in
21ad4 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 Reg */. int reg
21ad5 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 20 Free1 = 0;
21ad6 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 /* If non-zer
21ad7 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 o free this temp
21ad8 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a orary register *
21ad9 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32 /. int regFree2
21ada 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
21adb 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 If non-zero fre
21adc 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 e this temporary
21add 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 register */. i
21ade 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 72 nt r1, r2, r3, r
21adf 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 4; /* Vari
21ae0 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 6d ous register num
21ae1 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 bers */. sqlite
21ae2 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
21ae3 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 74 61 62 db; /* The datab
21ae4 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
21ae5 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 /.. assert( tar
21ae6 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c get>0 && target<
21ae7 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b =pParse->nMem );
21ae8 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 . if( v==0 ){.
21ae9 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 assert( pPars
21aea 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
21aeb 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 led );. retur
21aec 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 n 0;. }.. if(
21aed 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 pExpr==0 ){.
21aee 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 op = TK_NULL;.
21aef 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d 20 }else{. op =
21af0 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 pExpr->op;. }.
21af1 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 switch( op ){.
21af2 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 case TK_AGG_C
21af3 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 41 OLUMN: {. A
21af4 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f ggInfo *pAggInfo
21af5 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e = pExpr->pAggIn
21af6 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 fo;. struct
21af7 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 AggInfo_col *pC
21af8 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e ol = &pAggInfo->
21af9 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 67 aCol[pExpr->iAgg
21afa 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 41 ];. if( !pA
21afb 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f ggInfo->directMo
21afc 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 de ){. as
21afd 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d sert( pCol->iMem
21afe 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e >0 );. in
21aff 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d Reg = pCol->iMem
21b00 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
21b01 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
21b02 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 6f pAggInfo->useSo
21b03 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 20 rtingIdx ){.
21b04 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21b05 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
21b06 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 6f mn, pAggInfo->so
21b07 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 20 rtingIdx,.
21b08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b09 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 pCol->iS
21b0a 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 orterColumn, tar
21b0b 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 get);. br
21b0c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
21b0d 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c /* Otherwise,
21b0e 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 fall thru into
21b0f 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 the TK_COLUMN ca
21b10 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 se */. }.
21b11 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 case TK_COLUMN:
21b12 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 {. if( pExp
21b13 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20 r->iTable<0 ){.
21b14 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f /* This o
21b15 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e nly happens when
21b16 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 6f coding check co
21b17 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20 nstraints */.
21b18 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
21b19 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 3b rse->ckBase>0 );
21b1a 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d . inReg =
21b1b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
21b1c 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 + pParse->ckBase
21b1d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21b1e 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
21b1f 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 (pExpr->flags &
21b20 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 29 EP_AnyAff)!=0 )
21b21 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 ;. inReg
21b22 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
21b23 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 eGetColumn(pPars
21b24 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c 0a e, pExpr->pTab,.
21b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b27 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c pExpr->iColumn,
21b28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
21b29 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 target,.
21b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b2b 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
21b2c 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 flags & EP_AnyAf
21b2d 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 f);. }.
21b2e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
21b2f 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 case TK_INTEG
21b30 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65 ER: {. code
21b31 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70 72 Integer(v, pExpr
21b32 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
21b33 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21b34 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f . case TK_FLO
21b35 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 AT: {. asse
21b36 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
21b37 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
21b38 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
21b39 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70 45 codeReal(v, pE
21b3a 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 xpr->u.zToken, 0
21b3b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21b3c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21b3d 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 case TK_STRING
21b3e 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
21b3f 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21b40 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
21b41 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
21b42 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21b43 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 4(v, OP_String8,
21b44 20 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 70 0, target, 0, p
21b45 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 Expr->u.zToken,
21b46 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0);. break;
21b47 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21b48 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 TK_NULL: {.
21b49 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b4a 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
21b4b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21b4c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 break;. }.#i
21b4d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21b4e 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 T_BLOB_LITERAL.
21b4f 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a case TK_BLOB:
21b50 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a {. int n;.
21b51 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
21b52 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20 *z;. char
21b53 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73 *zBlob;. as
21b54 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21b55 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21b56 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21b57 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
21b58 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d r->u.zToken[0]==
21b59 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 'x' || pExpr->u.
21b5a 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 zToken[0]=='X' )
21b5b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21b5c 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pExpr->u.zToken[
21b5d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 1]=='\'' );.
21b5e 20 20 7a 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e z = &pExpr->u.
21b5f 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 20 zToken[2];.
21b60 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
21b61 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a 20 20 20 en30(z) - 1;.
21b62 20 20 20 61 73 73 65 72 74 28 20 7a 5b 6e 5d 3d assert( z[n]=
21b63 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a ='\'' );. z
21b64 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 48 65 Blob = sqlite3He
21b65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 56 xToBlob(sqlite3V
21b66 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 29 3b dbeDb(v), z, n);
21b67 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
21b68 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 beAddOp4(v, OP_B
21b69 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 65 74 lob, n/2, target
21b6a 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f 44 , 0, zBlob, P4_D
21b6b 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 62 YNAMIC);. b
21b6c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
21b6d 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 if. case TK_V
21b6e 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 ARIABLE: {.
21b6f 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 VdbeOp *pOp;.
21b70 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
21b71 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
21b72 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pr, EP_IntValue)
21b73 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21b74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 ( pExpr->u.zToke
21b75 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 n!=0 );. as
21b76 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a sert( pExpr->u.z
21b77 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 Token[0]!=0 );.
21b78 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
21b79 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a 20 u.zToken[1]==0.
21b7a 20 20 20 20 20 20 20 20 26 26 20 28 70 4f 70 20 && (pOp
21b7b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 = sqlite3VdbeGet
21b7c 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f 70 63 6f Op(v, -1))->opco
21b7d 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 0a de==OP_Variable.
21b7e 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d && pOp-
21b7f 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45 78 >p1+pOp->p3==pEx
21b80 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 pr->iColumn.
21b81 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32 2b && pOp->p2+
21b82 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74 0a pOp->p3==target.
21b83 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d && pOp-
21b84 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 29 >p4.z==0. )
21b85 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
21b86 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e 73 the previous ins
21b87 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20 63 truction was a c
21b88 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76 69 opy of the previ
21b89 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20 20 ous unnamed.
21b8a 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 ** parameter
21b8b 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69 6f into the previo
21b8c 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68 65 us register, the
21b8d 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d 65 n simply increme
21b8e 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a nt the. *
21b8f 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20 6f * repeat count o
21b90 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73 74 n the prior inst
21b91 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20 74 ruction rather t
21b92 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65 77 han making a new
21b93 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 . ** inst
21b94 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 ruction..
21b95 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70 2d */. pOp-
21b96 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c >p3++;. }el
21b97 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
21b98 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
21b99 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 OP_Variable, pE
21b9a 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 74 61 xpr->iColumn, ta
21b9b 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 rget, 1);.
21b9c 20 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a if( pExpr->u.z
21b9d 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 7b 0a 20 Token[1]!=0 ){.
21b9e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21b9f 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
21ba0 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f -1, pExpr->u.zTo
21ba1 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ken, 0);.
21ba2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
21ba3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21ba4 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 case TK_REGIST
21ba5 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 ER: {. inRe
21ba6 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c g = pExpr->iTabl
21ba7 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
21ba8 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21ba9 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e K_AS: {. in
21baa 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 Reg = codeAlias(
21bab 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69 pParse, pExpr->i
21bac 54 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c Table, pExpr->pL
21bad 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 eft, target);.
21bae 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21baf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
21bb0 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61 OMIT_CAST. ca
21bb1 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 se TK_CAST: {.
21bb2 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
21bb3 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 ns of the form:
21bb4 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53 20 CAST(pLeft AS
21bb5 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20 token) */.
21bb6 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a int aff, to_op;.
21bb7 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 inReg = sq
21bb8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
21bb9 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
21bba 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 r->pLeft, target
21bbb 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21bbc 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
21bbd 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 y(pExpr, EP_IntV
21bbe 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 alue) );. a
21bbf 66 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 ff = sqlite3Affi
21bc0 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d 3e nityType(pExpr->
21bc1 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 u.zToken);.
21bc2 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20 53 to_op = aff - S
21bc3 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 2b QLITE_AFF_TEXT +
21bc4 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20 20 OP_ToText;.
21bc5 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d assert( to_op=
21bc6 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c 7c =OP_ToText ||
21bc7 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 aff!=SQLITE_AFF
21bc8 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20 20 _TEXT );.
21bc9 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d assert( to_op=
21bca 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c 7c =OP_ToBlob ||
21bcb 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 aff!=SQLITE_AFF
21bcc 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20 20 _NONE );.
21bcd 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d assert( to_op=
21bce 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c 7c =OP_ToNumeric ||
21bcf 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 aff!=SQLITE_AFF
21bd0 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 _NUMERIC );.
21bd1 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d assert( to_op=
21bd2 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c 7c =OP_ToInt ||
21bd3 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 aff!=SQLITE_AFF
21bd4 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20 _INTEGER );.
21bd5 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d assert( to_op=
21bd6 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c 7c =OP_ToReal ||
21bd7 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 aff!=SQLITE_AFF
21bd8 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20 20 _REAL );.
21bd9 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f testcase( to_o
21bda 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b 0a p==OP_ToText );.
21bdb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21bdc 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 to_op==OP_ToBlob
21bdd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21bde 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
21bdf 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20 20 Numeric );.
21be0 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 testcase( to_op
21be1 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 20 ==OP_ToInt );.
21be2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f testcase( to
21be3 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 29 _op==OP_ToReal )
21be4 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52 65 ;. if( inRe
21be5 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20 20 g!=target ){.
21be6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21be7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f AddOp2(v, OP_SCo
21be8 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65 py, inReg, targe
21be9 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 t);. inRe
21bea 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 g = target;.
21beb 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
21bec 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 74 3VdbeAddOp1(v, t
21bed 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 o_op, inReg);.
21bee 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75 73 testcase( us
21bef 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 edAsColumnCache(
21bf0 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 69 pParse, inReg, i
21bf1 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20 73 nReg) );. s
21bf2 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
21bf3 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
21bf4 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b arse, inReg, 1);
21bf5 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21bf6 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
21bf7 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a LITE_OMIT_CAST *
21bf8 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 /. case TK_LT
21bf9 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 :. case TK_LE
21bfa 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 :. case TK_GT
21bfb 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 :. case TK_GE
21bfc 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 :. case TK_NE
21bfd 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 :. case TK_EQ
21bfe 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 : {. assert
21bff 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 ( TK_LT==OP_Lt )
21c00 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21c01 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a TK_LE==OP_Le );.
21c02 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21c03 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 _GT==OP_Gt );.
21c04 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 assert( TK_G
21c05 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 E==OP_Ge );.
21c06 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d assert( TK_EQ=
21c07 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 =OP_Eq );.
21c08 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f assert( TK_NE==O
21c09 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 P_Ne );. te
21c0a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c stcase( op==TK_L
21c0b 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21c0c 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 ase( op==TK_LE )
21c0d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c0e 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 ( op==TK_GT );.
21c0f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21c10 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 p==TK_GE );.
21c11 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21c12 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 TK_EQ );. t
21c13 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21c14 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 NE );. code
21c15 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
21c16 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21c17 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 Left, &r1, ®F
21c18 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 ree1,.
21c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c1a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
21c1b 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 Right, &r2, ®
21c1c 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f Free2);. co
21c1d 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 deCompare(pParse
21c1e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21c1f 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f pExpr->pRight, o
21c20 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 p,.
21c21 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 r1, r2, inR
21c22 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 eg, SQLITE_STORE
21c23 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 P2);. testc
21c24 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
21c25 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c26 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
21c27 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21c28 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21c29 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20 54 K_IS:. case T
21c2a 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 K_ISNOT: {.
21c2b 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21c2c 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74 65 K_IS );. te
21c2d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 stcase( op==TK_I
21c2e 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63 6f SNOT );. co
21c2f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 deCompareOperand
21c30 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d s(pParse, pExpr-
21c31 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 >pLeft, &r1, &re
21c32 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 gFree1,.
21c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c34 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
21c35 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 >pRight, &r2, &r
21c36 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
21c37 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53 29 op = (op==TK_IS)
21c38 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45 ? TK_EQ : TK_NE
21c39 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 ;. codeComp
21c3a 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 are(pParse, pExp
21c3b 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d r->pLeft, pExpr-
21c3c 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 >pRight, op,.
21c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 r
21c3e 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 51 1, r2, inReg, SQ
21c3f 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 7c 20 53 LITE_STOREP2 | S
21c40 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 QLITE_NULLEQ);.
21c41 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21c42 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
21c43 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
21c44 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 gFree2==0 );.
21c45 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21c46 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a case TK_AND:
21c47 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a . case TK_OR:
21c48 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55 . case TK_PLU
21c49 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 S:. case TK_S
21c4a 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b TAR:. case TK
21c4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 65 _MINUS:. case
21c4c 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 73 TK_REM:. cas
21c4d 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 20 e TK_BITAND:.
21c4e 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a 0a case TK_BITOR:.
21c4f 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 53 case TK_SLAS
21c50 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c H:. case TK_L
21c51 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 20 SHIFT:. case
21c52 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 20 TK_RSHIFT: .
21c53 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 case TK_CONCAT:
21c54 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
21c55 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 TK_AND==OP_And )
21c56 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21c57 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a TK_OR==OP_Or );.
21c58 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21c59 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 3b _PLUS==OP_Add );
21c5a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21c5b 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 K_MINUS==OP_Subt
21c5c 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 73 ract );. as
21c5d 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 sert( TK_REM==OP
21c5e 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 20 _Remainder );.
21c5f 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 assert( TK_B
21c60 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 ITAND==OP_BitAnd
21c61 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21c62 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 ( TK_BITOR==OP_B
21c63 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 itOr );. as
21c64 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d 3d sert( TK_SLASH==
21c65 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 20 OP_Divide );.
21c66 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 53 assert( TK_LS
21c67 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 HIFT==OP_ShiftLe
21c68 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ft );. asse
21c69 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d 4f rt( TK_RSHIFT==O
21c6a 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a P_ShiftRight );.
21c6b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21c6c 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 _CONCAT==OP_Conc
21c6d 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 at );. test
21c6e 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 case( op==TK_AND
21c6f 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c70 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b se( op==TK_OR );
21c71 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c72 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a op==TK_PLUS );.
21c73 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21c74 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a op==TK_MINUS );.
21c75 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21c76 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 op==TK_REM );.
21c77 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21c78 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a 20 ==TK_BITAND );.
21c79 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21c7a 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 p==TK_BITOR );.
21c7b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21c7c 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 p==TK_SLASH );.
21c7d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21c7e 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b 0a p==TK_LSHIFT );.
21c7f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21c80 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 3b op==TK_RSHIFT );
21c81 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c82 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 op==TK_CONCAT )
21c83 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c ;. r1 = sql
21c84 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
21c85 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21c86 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 pLeft, ®Free1
21c87 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 );. r2 = sq
21c88 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21c89 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
21c8a 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 >pRight, ®Fre
21c8b 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 e2);. sqlit
21c8c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21c8d 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 op, r2, r1, targ
21c8e 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 et);. testc
21c8f 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
21c90 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c91 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
21c92 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21c93 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21c94 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 K_UMINUS: {.
21c95 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 Expr *pLeft =
21c96 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 pExpr->pLeft;.
21c97 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 assert( pLef
21c98 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 t );. if( p
21c99 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f Left->op==TK_FLO
21c9a 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 AT ){. as
21c9b 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21c9c 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21c9d 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21c9e 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 codeReal(v
21c9f 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 , pLeft->u.zToke
21ca0 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 n, 1, target);.
21ca1 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
21ca2 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 Left->op==TK_INT
21ca3 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 EGER ){.
21ca4 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 codeInteger(v, p
21ca5 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29 Left, 1, target)
21ca6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21ca7 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 regFree1
21ca8 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 = r1 = sqlite3Ge
21ca9 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
21caa 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21cab 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21cac 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 P_Integer, 0, r1
21cad 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 );. r2 =
21cae 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21caf 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
21cb0 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
21cb1 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ee2);. sq
21cb2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21cb3 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 v, OP_Subtract,
21cb4 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b r2, r1, target);
21cb5 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
21cb6 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 e( regFree2==0 )
21cb7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21cb8 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a inReg = target;.
21cb9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21cba 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 }. case TK_B
21cbb 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 ITNOT:. case
21cbc 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 TK_NOT: {.
21cbd 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f assert( TK_BITNO
21cbe 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a T==OP_BitNot );.
21cbf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21cc0 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a _NOT==OP_Not );.
21cc1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21cc2 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b op==TK_BITNOT );
21cc3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21cc4 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 op==TK_NOT );.
21cc5 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
21cc6 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
21cc7 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21cc8 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a ft, ®Free1);.
21cc9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21cca 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
21ccb 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 inReg = tar
21ccc 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 get;. sqlit
21ccd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21cce 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b 0a op, r1, inReg);.
21ccf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21cd0 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 }. case TK_I
21cd1 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 SNULL:. case
21cd2 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 TK_NOTNULL: {.
21cd3 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 int addr;.
21cd4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 assert( TK_I
21cd5 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c SNULL==OP_IsNull
21cd6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21cd7 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 ( TK_NOTNULL==OP
21cd8 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 _NotNull );.
21cd9 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21cda 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 TK_ISNULL );.
21cdb 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21cdc 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 =TK_NOTNULL );.
21cdd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21cde 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
21cdf 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 eger, 1, target)
21ce0 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c ;. r1 = sql
21ce1 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
21ce2 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21ce3 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 pLeft, ®Free1
21ce4 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21ce5 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
21ce6 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 ;. addr = s
21ce7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
21ce8 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 20 (v, op, r1);.
21ce9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21cea 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
21ceb 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b 0a m, target, -1);.
21cec 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21ced 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 eJumpHere(v, add
21cee 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b r);. break;
21cef 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21cf0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a TK_AGG_FUNCTION:
21cf1 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f {. AggInfo
21cf2 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d *pInfo = pExpr-
21cf3 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 >pAggInfo;.
21cf4 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b if( pInfo==0 ){
21cf5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
21cf6 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
21cf7 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 y(pExpr, EP_IntV
21cf8 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 alue) );.
21cf9 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
21cfa 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 (pParse, "misuse
21cfb 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 of aggregate: %
21cfc 73 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a s()", pExpr->u.z
21cfd 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 65 Token);. }e
21cfe 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52 lse{. inR
21cff 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e eg = pInfo->aFun
21d00 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69 c[pExpr->iAgg].i
21d01 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Mem;. }.
21d02 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21d03 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 case TK_CONS
21d04 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 T_FUNC:. case
21d05 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a TK_FUNCTION: {.
21d06 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a ExprList *
21d07 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 pFarg; /*
21d08 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e List of function
21d09 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
21d0a 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20 int nFarg;
21d0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
21d0c 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e mber of function
21d0d 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
21d0e 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 FuncDef *pDe
21d0f 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 f; /* Th
21d10 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e e function defin
21d11 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a ition object */.
21d12 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 int nId;
21d13 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21d14 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75 Length of the fu
21d15 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 nction name in b
21d16 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f ytes */. co
21d17 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 nst char *zId;
21d18 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 /* The func
21d19 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 tion name */.
21d1a 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b int constMask
21d1b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73 = 0; /* Mas
21d1c 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 k of function ar
21d1d 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 guments that are
21d1e 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20 constant */.
21d1f 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
21d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
21d21 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 p counter */.
21d22 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 u8 enc = ENC(
21d23 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 db); /* The
21d24 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 text encoding u
21d25 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 sed by this data
21d26 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f base */. Co
21d27 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 llSeq *pColl = 0
21d28 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 ; /* A collat
21d29 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
21d2a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
21d2b 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
21d2c 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
21d2d 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65 ect) );. te
21d2e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 stcase( op==TK_C
21d2f 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 ONST_FUNC );.
21d30 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21d31 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a =TK_FUNCTION );.
21d32 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
21d33 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 sAnyProperty(pEx
21d34 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 pr, EP_TokenOnly
21d35 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 61 ) ){. pFa
21d36 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 rg = 0;. }e
21d37 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 61 lse{. pFa
21d38 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c rg = pExpr->x.pL
21d39 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ist;. }.
21d3a 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72 67 nFarg = pFarg
21d3b 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72 20 ? pFarg->nExpr
21d3c 3a 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 : 0;. asser
21d3d 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
21d3e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e rty(pExpr, EP_In
21d3f 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 tValue) );.
21d40 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e zId = pExpr->u.
21d41 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 zToken;. nI
21d42 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 d = sqlite3Strle
21d43 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 n30(zId);.
21d44 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 pDef = sqlite3Fi
21d45 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a ndFunction(db, z
21d46 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c 20 Id, nId, nFarg,
21d47 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 enc, 0);. i
21d48 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 f( pDef==0 ){.
21d49 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
21d4a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 orMsg(pParse, "u
21d4b 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e 3a nknown function:
21d4c 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20 7a %.*s()", nId, z
21d4d 49 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 Id);. bre
21d4e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 ak;. }..
21d4f 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 61 20 /* Attempt a
21d50 64 69 72 65 63 74 20 69 6d 70 6c 65 6d 65 6e 74 direct implement
21d51 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 69 ation of the bui
21d52 6c 74 2d 69 6e 20 43 4f 41 4c 45 53 43 45 28 29 lt-in COALESCE()
21d53 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 49 46 and. ** IF
21d54 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f 6e 73 NULL() functions
21d55 2e 20 20 54 68 69 73 20 61 76 6f 69 64 73 20 75 . This avoids u
21d56 6e 6e 65 63 65 73 73 61 72 79 20 65 76 61 6c 61 nnecessary evala
21d57 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a tion of. **
21d58 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 74 20 arguments past
21d59 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 the first non-NU
21d5a 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 20 20 20 LL argument..
21d5b 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
21d5c 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
21d5d 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 LITE_FUNC_COALES
21d5e 43 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e CE ){. in
21d5f 74 20 65 6e 64 43 6f 61 6c 65 73 63 65 20 3d 20 t endCoalesce =
21d60 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
21d61 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 abel(v);.
21d62 20 61 73 73 65 72 74 28 20 6e 46 61 72 67 3e 3d assert( nFarg>=
21d63 32 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 2 );. sql
21d64 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
21d65 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 30 5d rse, pFarg->a[0]
21d66 2e 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b .pExpr, target);
21d67 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31 . for(i=1
21d68 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b ; i<nFarg; i++){
21d69 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
21d6a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21d6b 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 74 61 72 67 OP_NotNull, targ
21d6c 65 74 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29 et, endCoalesce)
21d6d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
21d6e 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f te3ExprCacheRemo
21d6f 76 65 28 70 50 61 72 73 65 2c 20 74 61 72 67 65 ve(pParse, targe
21d70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 t);. sq
21d71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
21d72 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 sh(pParse);.
21d73 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21d74 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46 rCode(pParse, pF
21d75 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c arg->a[i].pExpr,
21d76 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
21d77 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21d78 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 achePop(pParse,
21d79 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1);. }.
21d7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21d7b 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
21d7c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29 3b 0a 20 endCoalesce);.
21d7d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
21d7e 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 69 66 }... if
21d7f 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20 ( pFarg ){.
21d80 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 r1 = sqlite3G
21d81 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 etTempRange(pPar
21d82 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 se, nFarg);.
21d83 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21d84 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 achePush(pParse)
21d85 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 ; /* Ticket
21d86 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 2ea2425d34be */.
21d87 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21d88 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 xprCodeExprList(
21d89 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c 20 72 pParse, pFarg, r
21d8a 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 1, 1);. s
21d8b 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
21d8c 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 20 20 op(pParse, 1);
21d8d 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 32 34 /* Ticket 2ea24
21d8e 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 25d34be */.
21d8f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
21d90 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a r1 = 0;. }.
21d91 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21d92 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
21d93 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 . /* Possib
21d94 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 ly overload the
21d95 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 function if the
21d96 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
21d97 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 s. ** a vir
21d98 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d tual table colum
21d99 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 n.. **.
21d9a 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 ** For infix f
21d9b 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 unctions (LIKE,
21d9c 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e GLOB, REGEXP, an
21d9d 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 d MATCH) use the
21d9e 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 . ** second
21d9f 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 argument, not t
21da0 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 he first, as the
21da1 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 argument to tes
21da2 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 t to. ** se
21da3 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c e if it is a col
21da4 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c umn in a virtual
21da5 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 table. This is
21da6 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 done because.
21da7 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 ** the left
21da8 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 operand of infix
21da9 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 functions (the
21daa 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 operand we want
21dab 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 to. ** cont
21dac 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 rol overloading)
21dad 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 ends up as the
21dae 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
21daf 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 to the. **
21db0 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 function. The e
21db1 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f xpression "A glo
21db2 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 b B" is equivale
21db3 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 nt to . **
21db4 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 "glob(B,A). We
21db5 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 want to use the
21db6 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 A in "A glob B"
21db7 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a to test. **
21db8 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 for function ov
21db9 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 erloading. But
21dba 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72 we use the B ter
21dbb 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 m in "glob(B,A)"
21dbc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
21dbd 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 if( nFarg>=2 &&
21dbe 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 (pExpr->flags &
21dbf 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 EP_InfixFunc) )
21dc0 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d {. pDef =
21dc1 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 sqlite3VtabOver
21dc2 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c loadFunction(db,
21dc3 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 pDef, nFarg, pF
21dc4 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 arg->a[1].pExpr)
21dc5 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
21dc6 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 ( nFarg>0 ){.
21dc7 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 pDef = sqli
21dc8 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 te3VtabOverloadF
21dc9 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 unction(db, pDef
21dca 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e , nFarg, pFarg->
21dcb 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[0].pExpr);.
21dcc 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
21dcd 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 for(i=0; i<nFa
21dce 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 rg; i++){.
21dcf 20 20 69 66 28 20 69 3c 33 32 20 26 26 20 73 71 if( i<32 && sq
21dd0 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
21dd1 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e ant(pFarg->a[i].
21dd2 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
21dd3 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d constMask |=
21dd4 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 (1<<i);.
21dd5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 }. if( (
21dd6 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
21dd7 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
21dd8 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c LL)!=0 && !pColl
21dd9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 ){. pC
21dda 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
21ddb 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
21ddc 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 pFarg->a[i].pEx
21ddd 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pr);. }.
21dde 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
21ddf 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 pDef->flags & S
21de0 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
21de1 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 OLL ){. i
21de2 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c f( !pColl ) pCol
21de3 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c l = db->pDfltCol
21de4 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l; . sqli
21de5 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
21de6 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 OP_CollSeq, 0,
21de7 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 0, 0, (char *)pC
21de8 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 oll, P4_COLLSEQ)
21de9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21dea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21deb 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 4(v, OP_Function
21dec 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c , constMask, r1,
21ded 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 target,.
21dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21def 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 (char*)pDef, P4
21df0 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 _FUNCDEF);.
21df1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
21df2 67 65 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72 geP5(v, (u8)nFar
21df3 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 g);. if( nF
21df4 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 arg ){. s
21df5 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21df6 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 pRange(pParse, r
21df7 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 1, nFarg);.
21df8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
21df9 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 ExprCacheAffinit
21dfa 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 yChange(pParse,
21dfb 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 r1, nFarg);.
21dfc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
21dfd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
21dfe 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 IT_SUBQUERY.
21dff 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a case TK_EXISTS:.
21e00 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 case TK_SELE
21e01 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 CT: {. test
21e02 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 case( op==TK_EXI
21e03 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 STS );. tes
21e04 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 tcase( op==TK_SE
21e05 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 69 6e LECT );. in
21e06 52 65 67 20 3d 20 73 71 6c 69 74 65 33 43 6f 64 Reg = sqlite3Cod
21e07 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 eSubselect(pPars
21e08 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b e, pExpr, 0, 0);
21e09 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21e0a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21e0b 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 IN: {. int
21e0c 64 65 73 74 49 66 46 61 6c 73 65 20 3d 20 73 71 destIfFalse = sq
21e0d 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
21e0e 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 6e 74 el(v);. int
21e0f 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20 73 71 destIfNull = sq
21e10 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
21e11 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c el(v);. sql
21e12 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21e13 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 , OP_Null, 0, ta
21e14 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
21e15 69 74 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70 ite3ExprCodeIN(p
21e16 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65 Parse, pExpr, de
21e17 73 74 49 66 46 61 6c 73 65 2c 20 64 65 73 74 49 stIfFalse, destI
21e18 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 fNull);. sq
21e19 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21e1a 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 v, OP_Integer, 1
21e1b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21e1c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
21e1d 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73 74 lveLabel(v, dest
21e1e 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 20 20 IfFalse);.
21e1f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21e20 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2(v, OP_AddImm,
21e21 74 61 72 67 65 74 2c 20 30 29 3b 0a 20 20 20 20 target, 0);.
21e22 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
21e23 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73 olveLabel(v, des
21e24 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 tIfNull);.
21e25 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
21e26 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
21e27 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a IT_SUBQUERY */..
21e28 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 . /*. **
21e29 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e x BETWEEN y AN
21e2a 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a D z. **. *
21e2b 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61 * This is equiva
21e2c 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 lent to. **.
21e2d 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e ** x>=y AN
21e2e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 D x<=z. **.
21e2f 20 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64 ** X is stored
21e30 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 in pExpr->pLeft
21e31 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74 .. ** Y is st
21e32 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 ored in pExpr->p
21e33 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 List->a[0].pExpr
21e34 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 .. ** Z is st
21e35 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 ored in pExpr->p
21e36 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 List->a[1].pExpr
21e37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
21e38 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a e TK_BETWEEN: {.
21e39 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 Expr *pLef
21e3a 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 t = pExpr->pLeft
21e3b 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 ;. struct E
21e3c 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c xprList_item *pL
21e3d 49 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e Item = pExpr->x.
21e3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 pList->a;.
21e3f 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 Expr *pRight = p
21e40 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 LItem->pExpr;..
21e41 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
21e42 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c Operands(pParse,
21e43 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 pLeft, &r1, &re
21e44 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 gFree1,.
21e45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e46 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 pRight
21e47 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 , &r2, ®Free2
21e48 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21e49 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
21e4a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21e4b 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
21e4c 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69 . r3 = sqli
21e4d 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
21e4e 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34 20 arse);. r4
21e4f 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
21e50 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 Reg(pParse);.
21e51 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 codeCompare(p
21e52 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 Parse, pLeft, pR
21e53 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20 ight, OP_Ge,.
21e54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 r
21e55 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49 54 1, r2, r3, SQLIT
21e56 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 E_STOREP2);.
21e57 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 pLItem++;.
21e58 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 pRight = pLIte
21e59 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 m->pExpr;.
21e5a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
21e5b 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 mpReg(pParse, re
21e5c 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 72 gFree2);. r
21e5d 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 2 = sqlite3ExprC
21e5e 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
21e5f 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 pRight, ®Free
21e60 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 2);. testca
21e61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
21e62 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
21e63 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 pare(pParse, pLe
21e64 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c ft, pRight, OP_L
21e65 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20 53 e, r1, r2, r4, S
21e66 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a QLITE_STOREP2);.
21e67 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21e68 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e eAddOp3(v, OP_An
21e69 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67 65 d, r3, r4, targe
21e6a 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 t);. sqlite
21e6b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
21e6c 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20 20 pParse, r3);.
21e6d 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
21e6e 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
21e6f 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 r4);. brea
21e70 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21e71 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 e TK_UPLUS: {.
21e72 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 inReg = sqli
21e73 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
21e74 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
21e75 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
21e76 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21e77 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b }.. case TK
21e78 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20 20 20 20 _TRIGGER: {.
21e79 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f /* If the opco
21e7a 64 65 20 69 73 20 54 4b 5f 54 52 49 47 47 45 52 de is TK_TRIGGER
21e7b 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 , then the expre
21e7c 73 73 69 6f 6e 20 69 73 20 61 20 72 65 66 65 72 ssion is a refer
21e7d 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f ence. ** to
21e7e 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 a column in the
21e7f 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 new.* or old.*
21e80 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 61 76 pseudo-tables av
21e81 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 ailable to.
21e82 20 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 ** trigger prog
21e83 72 61 6d 73 2e 20 49 6e 20 74 68 69 73 20